[email protected] | 45ab7ab | 2012-06-06 12:57:44 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Generator script for proxy tests. |
| 7 | |
| 8 | See AndroidProxySelectorTest.java |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 9 | and net/proxy_resolution/proxy_config_service_android_unittest.cc |
[email protected] | 45ab7ab | 2012-06-06 12:57:44 | [diff] [blame] | 10 | |
| 11 | To generate C++, run this script without arguments. |
| 12 | To generate Java, run this script with -j argument. |
| 13 | |
| 14 | Note that this generator is not run as part of the build process because |
| 15 | we are assuming that these test cases will not change often. |
| 16 | """ |
| 17 | |
| 18 | import optparse |
| 19 | |
| 20 | test_cases = [ |
| 21 | { |
| 22 | "name": "NoProxy", |
| 23 | "description" : "Test direct mapping when no proxy defined.", |
| 24 | "properties" : { |
| 25 | }, |
| 26 | "mappings" : { |
| 27 | "http://example.com/" : "DIRECT", |
| 28 | "ftp://example.com/" : "DIRECT", |
| 29 | "https://example.com/" : "DIRECT", |
| 30 | } |
| 31 | }, |
| 32 | { |
| 33 | "name": "HttpProxyHostAndPort", |
| 34 | "description" : "Test http.proxyHost and http.proxyPort works.", |
| 35 | "properties" : { |
| 36 | "http.proxyHost" : "httpproxy.com", |
| 37 | "http.proxyPort" : "8080", |
| 38 | }, |
| 39 | "mappings" : { |
| 40 | "http://example.com/" : "PROXY httpproxy.com:8080", |
| 41 | "ftp://example.com/" : "DIRECT", |
| 42 | "https://example.com/" : "DIRECT", |
| 43 | } |
| 44 | }, |
| 45 | { |
| 46 | "name": "HttpProxyHostOnly", |
| 47 | "description" : "We should get the default port (80) for proxied hosts.", |
| 48 | "properties" : { |
| 49 | "http.proxyHost" : "httpproxy.com", |
| 50 | }, |
| 51 | "mappings" : { |
| 52 | "http://example.com/" : "PROXY httpproxy.com:80", |
| 53 | "ftp://example.com/" : "DIRECT", |
| 54 | "https://example.com/" : "DIRECT", |
| 55 | } |
| 56 | }, |
| 57 | { |
| 58 | "name": "HttpProxyPortOnly", |
| 59 | "description" : |
| 60 | "http.proxyPort only should not result in any hosts being proxied.", |
| 61 | "properties" : { |
| 62 | "http.proxyPort" : "8080", |
| 63 | }, |
| 64 | "mappings" : { |
| 65 | "http://example.com/" : "DIRECT", |
| 66 | "ftp://example.com/" : "DIRECT", |
| 67 | "https://example.com/" : "DIRECT" |
| 68 | } |
| 69 | }, |
| 70 | { |
| 71 | "name": "HttpNonProxyHosts1", |
| 72 | "description" : "Test that HTTP non proxy hosts are mapped correctly", |
| 73 | "properties" : { |
| 74 | "http.nonProxyHosts" : "slashdot.org", |
| 75 | "http.proxyHost" : "httpproxy.com", |
| 76 | "http.proxyPort" : "8080", |
| 77 | }, |
| 78 | "mappings" : { |
| 79 | "http://example.com/" : "PROXY httpproxy.com:8080", |
| 80 | "http://slashdot.org/" : "DIRECT", |
| 81 | } |
| 82 | }, |
| 83 | { |
| 84 | "name": "HttpNonProxyHosts2", |
| 85 | "description" : "Test that | pattern works.", |
| 86 | "properties" : { |
| 87 | "http.nonProxyHosts" : "slashdot.org|freecode.net", |
| 88 | "http.proxyHost" : "httpproxy.com", |
| 89 | "http.proxyPort" : "8080", |
| 90 | }, |
| 91 | "mappings" : { |
| 92 | "http://example.com/" : "PROXY httpproxy.com:8080", |
| 93 | "http://slashdot.org/" : "DIRECT", |
| 94 | "http://freecode.net/" : "DIRECT", |
| 95 | } |
| 96 | }, |
| 97 | { |
| 98 | "name": "HttpNonProxyHosts3", |
| 99 | "description" : "Test that * pattern works.", |
| 100 | "properties" : { |
| 101 | "http.nonProxyHosts" : "*example.com", |
| 102 | "http.proxyHost" : "httpproxy.com", |
| 103 | "http.proxyPort" : "8080", |
| 104 | }, |
| 105 | "mappings" : { |
| 106 | "http://example.com/" : "DIRECT", |
| 107 | "http://www.example.com/" : "DIRECT", |
| 108 | "http://slashdot.org/" : "PROXY httpproxy.com:8080", |
| 109 | } |
| 110 | }, |
| 111 | { |
| 112 | "name": "FtpNonProxyHosts", |
| 113 | "description" : "Test that FTP non proxy hosts are mapped correctly", |
| 114 | "properties" : { |
| 115 | "ftp.nonProxyHosts" : "slashdot.org", |
| 116 | "ftp.proxyHost" : "httpproxy.com", |
| 117 | "ftp.proxyPort" : "8080", |
| 118 | }, |
| 119 | "mappings" : { |
| 120 | "http://example.com/" : "DIRECT", |
| 121 | "ftp://example.com/" : "PROXY httpproxy.com:8080", |
| 122 | } |
| 123 | }, |
| 124 | { |
| 125 | "name": "FtpProxyHostAndPort", |
| 126 | "description" : "Test ftp.proxyHost and ftp.proxyPort works.", |
| 127 | "properties" : { |
| 128 | "ftp.proxyHost" : "httpproxy.com", |
| 129 | "ftp.proxyPort" : "8080", |
| 130 | }, |
| 131 | "mappings" : { |
| 132 | "ftp://example.com/" : "PROXY httpproxy.com:8080", |
| 133 | "http://example.com/" : "DIRECT", |
| 134 | "https://example.com/" : "DIRECT", |
| 135 | } |
| 136 | }, |
| 137 | { |
| 138 | "name": "FtpProxyHostOnly", |
| 139 | "description" : "Test ftp.proxyHost and default port.", |
| 140 | "properties" : { |
| 141 | "ftp.proxyHost" : "httpproxy.com", |
| 142 | }, |
| 143 | "mappings" : { |
| 144 | "ftp://example.com/" : "PROXY httpproxy.com:80", |
| 145 | "http://example.com/" : "DIRECT", |
| 146 | "https://example.com/" : "DIRECT", |
| 147 | } |
| 148 | }, |
| 149 | { |
| 150 | "name": "HttpsProxyHostAndPort", |
| 151 | "description" : "Test https.proxyHost and https.proxyPort works.", |
| 152 | "properties" : { |
| 153 | "https.proxyHost" : "httpproxy.com", |
| 154 | "https.proxyPort" : "8080", |
| 155 | }, |
| 156 | "mappings" : { |
[email protected] | 4429523 | 2012-12-10 12:49:38 | [diff] [blame] | 157 | "https://example.com/" : "PROXY httpproxy.com:8080", |
[email protected] | 45ab7ab | 2012-06-06 12:57:44 | [diff] [blame] | 158 | "http://example.com/" : "DIRECT", |
| 159 | "ftp://example.com/" : "DIRECT", |
| 160 | } |
| 161 | }, |
| 162 | { |
| 163 | "name": "HttpsProxyHostOnly", |
| 164 | "description" : "Test https.proxyHost and default port.", |
[email protected] | 4429523 | 2012-12-10 12:49:38 | [diff] [blame] | 165 | # Chromium differs from the Android platform by connecting to port 80 for |
| 166 | # HTTPS connections by default, hence cpp-only. |
| 167 | "cpp-only" : "", |
[email protected] | 45ab7ab | 2012-06-06 12:57:44 | [diff] [blame] | 168 | "properties" : { |
| 169 | "https.proxyHost" : "httpproxy.com", |
| 170 | }, |
| 171 | "mappings" : { |
[email protected] | 4429523 | 2012-12-10 12:49:38 | [diff] [blame] | 172 | "https://example.com/" : "PROXY httpproxy.com:80", |
[email protected] | 45ab7ab | 2012-06-06 12:57:44 | [diff] [blame] | 173 | "http://example.com/" : "DIRECT", |
| 174 | "ftp://example.com/" : "DIRECT", |
| 175 | } |
| 176 | }, |
| 177 | { |
| 178 | "name": "HttpProxyHostIPv6", |
| 179 | "description" : "Test IPv6 https.proxyHost and default port.", |
[email protected] | 2e53058 | 2012-10-20 00:28:35 | [diff] [blame] | 180 | "cpp-only" : "", |
[email protected] | 45ab7ab | 2012-06-06 12:57:44 | [diff] [blame] | 181 | "properties" : { |
| 182 | "http.proxyHost" : "a:b:c::d:1", |
| 183 | }, |
| 184 | "mappings" : { |
| 185 | "http://example.com/" : "PROXY [a:b:c::d:1]:80", |
| 186 | "ftp://example.com/" : "DIRECT", |
| 187 | } |
| 188 | }, |
| 189 | { |
| 190 | "name": "HttpProxyHostAndPortIPv6", |
| 191 | "description" : "Test IPv6 http.proxyHost and http.proxyPort works.", |
[email protected] | 2e53058 | 2012-10-20 00:28:35 | [diff] [blame] | 192 | "cpp-only" : "", |
[email protected] | 45ab7ab | 2012-06-06 12:57:44 | [diff] [blame] | 193 | "properties" : { |
| 194 | "http.proxyHost" : "a:b:c::d:1", |
| 195 | "http.proxyPort" : "8080", |
| 196 | }, |
| 197 | "mappings" : { |
| 198 | "http://example.com/" : "PROXY [a:b:c::d:1]:8080", |
| 199 | "ftp://example.com/" : "DIRECT", |
| 200 | } |
| 201 | }, |
| 202 | { |
| 203 | "name": "HttpProxyHostAndInvalidPort", |
| 204 | "description" : "Test invalid http.proxyPort does not crash.", |
[email protected] | 2e53058 | 2012-10-20 00:28:35 | [diff] [blame] | 205 | "cpp-only" : "", |
[email protected] | 45ab7ab | 2012-06-06 12:57:44 | [diff] [blame] | 206 | "properties" : { |
| 207 | "http.proxyHost" : "a:b:c::d:1", |
| 208 | "http.proxyPort" : "65536", |
| 209 | }, |
| 210 | "mappings" : { |
| 211 | "http://example.com/" : "DIRECT", |
| 212 | "ftp://example.com/" : "DIRECT", |
| 213 | } |
| 214 | }, |
| 215 | { |
| 216 | "name": "DefaultProxyExplictPort", |
| 217 | "description" : |
| 218 | "Default http proxy is used if a scheme-specific one is not found.", |
| 219 | "properties" : { |
| 220 | "proxyHost" : "defaultproxy.com", |
| 221 | "proxyPort" : "8080", |
| 222 | "ftp.proxyHost" : "httpproxy.com", |
| 223 | "ftp.proxyPort" : "8080", |
| 224 | }, |
| 225 | "mappings" : { |
| 226 | "http://example.com/" : "PROXY defaultproxy.com:8080", |
[email protected] | 4429523 | 2012-12-10 12:49:38 | [diff] [blame] | 227 | "https://example.com/" : "PROXY defaultproxy.com:8080", |
[email protected] | 45ab7ab | 2012-06-06 12:57:44 | [diff] [blame] | 228 | "ftp://example.com/" : "PROXY httpproxy.com:8080", |
| 229 | } |
| 230 | }, |
| 231 | { |
| 232 | "name": "DefaultProxyDefaultPort", |
| 233 | "description" : "Check that the default proxy port is as expected.", |
[email protected] | 4429523 | 2012-12-10 12:49:38 | [diff] [blame] | 234 | # Chromium differs from the Android platform by connecting to port 80 for |
| 235 | # HTTPS connections by default, hence cpp-only. |
| 236 | "cpp-only" : "", |
[email protected] | 45ab7ab | 2012-06-06 12:57:44 | [diff] [blame] | 237 | "properties" : { |
| 238 | "proxyHost" : "defaultproxy.com", |
| 239 | }, |
| 240 | "mappings" : { |
| 241 | "http://example.com/" : "PROXY defaultproxy.com:80", |
[email protected] | 4429523 | 2012-12-10 12:49:38 | [diff] [blame] | 242 | "https://example.com/" : "PROXY defaultproxy.com:80", |
[email protected] | 45ab7ab | 2012-06-06 12:57:44 | [diff] [blame] | 243 | } |
| 244 | }, |
| 245 | { |
| 246 | "name": "FallbackToSocks", |
| 247 | "description" : "SOCKS proxy is used if scheme-specific one is not found.", |
| 248 | "properties" : { |
| 249 | "http.proxyHost" : "defaultproxy.com", |
| 250 | "socksProxyHost" : "socksproxy.com" |
| 251 | }, |
| 252 | "mappings" : { |
| 253 | "http://example.com/" : "PROXY defaultproxy.com:80", |
| 254 | "https://example.com/" : "SOCKS5 socksproxy.com:1080", |
| 255 | "ftp://example.com" : "SOCKS5 socksproxy.com:1080", |
| 256 | } |
| 257 | }, |
| 258 | { |
| 259 | "name": "SocksExplicitPort", |
| 260 | "description" : "SOCKS proxy port is used if specified", |
| 261 | "properties" : { |
| 262 | "socksProxyHost" : "socksproxy.com", |
| 263 | "socksProxyPort" : "9000", |
| 264 | }, |
| 265 | "mappings" : { |
| 266 | "http://example.com/" : "SOCKS5 socksproxy.com:9000", |
| 267 | } |
| 268 | }, |
| 269 | { |
| 270 | "name": "HttpProxySupercedesSocks", |
| 271 | "description" : "SOCKS proxy is ignored if default HTTP proxy defined.", |
| 272 | "properties" : { |
| 273 | "proxyHost" : "defaultproxy.com", |
| 274 | "socksProxyHost" : "socksproxy.com", |
| 275 | "socksProxyPort" : "9000", |
| 276 | }, |
| 277 | "mappings" : { |
| 278 | "http://example.com/" : "PROXY defaultproxy.com:80", |
| 279 | } |
| 280 | }, |
| 281 | ] |
| 282 | |
| 283 | class GenerateCPlusPlus: |
| 284 | """Generate C++ test cases""" |
| 285 | |
| 286 | def Generate(self): |
| 287 | for test_case in test_cases: |
| 288 | print ("TEST_F(ProxyConfigServiceAndroidTest, %s) {" % test_case["name"]) |
| 289 | if "description" in test_case: |
| 290 | self._GenerateDescription(test_case["description"]); |
| 291 | self._GenerateConfiguration(test_case["properties"]) |
| 292 | self._GenerateMappings(test_case["mappings"]) |
| 293 | print "}" |
| 294 | print "" |
| 295 | |
| 296 | def _GenerateDescription(self, description): |
| 297 | print " // %s" % description |
| 298 | |
| 299 | def _GenerateConfiguration(self, properties): |
| 300 | for key in sorted(properties.iterkeys()): |
| 301 | print " AddProperty(\"%s\", \"%s\");" % (key, properties[key]) |
| 302 | print " ProxySettingsChanged();" |
| 303 | |
| 304 | def _GenerateMappings(self, mappings): |
| 305 | for url in sorted(mappings.iterkeys()): |
| 306 | print " TestMapping(\"%s\", \"%s\");" % (url, mappings[url]) |
| 307 | |
| 308 | |
| 309 | class GenerateJava: |
| 310 | """Generate Java test cases""" |
| 311 | |
| 312 | def Generate(self): |
| 313 | for test_case in test_cases: |
Raul Tambre | 901361f | 2019-09-23 14:59:09 | [diff] [blame] | 314 | if "cpp-only" in test_case: |
[email protected] | 2e53058 | 2012-10-20 00:28:35 | [diff] [blame] | 315 | continue |
[email protected] | 45ab7ab | 2012-06-06 12:57:44 | [diff] [blame] | 316 | if "description" in test_case: |
| 317 | self._GenerateDescription(test_case["description"]); |
| 318 | print " @SmallTest" |
[email protected] | 4429523 | 2012-12-10 12:49:38 | [diff] [blame] | 319 | print " @Feature({\"AndroidWebView\"})" |
[email protected] | 45ab7ab | 2012-06-06 12:57:44 | [diff] [blame] | 320 | print " public void test%s() throws Exception {" % test_case["name"] |
| 321 | self._GenerateConfiguration(test_case["properties"]) |
| 322 | self._GenerateMappings(test_case["mappings"]) |
| 323 | print " }" |
| 324 | print "" |
| 325 | |
| 326 | def _GenerateDescription(self, description): |
| 327 | print " /**" |
| 328 | print " * %s" % description |
| 329 | print " *" |
| 330 | print " * @throws Exception" |
| 331 | print " */" |
| 332 | |
| 333 | def _GenerateConfiguration(self, properties): |
| 334 | for key in sorted(properties.iterkeys()): |
| 335 | print " System.setProperty(\"%s\", \"%s\");" % ( |
| 336 | key, properties[key]) |
| 337 | |
| 338 | def _GenerateMappings(self, mappings): |
| 339 | for url in sorted(mappings.iterkeys()): |
[email protected] | 2e53058 | 2012-10-20 00:28:35 | [diff] [blame] | 340 | mapping = mappings[url] |
| 341 | if 'HTTPS' in mapping: |
| 342 | mapping = mapping.replace('HTTPS', 'PROXY') |
| 343 | print " checkMapping(\"%s\", \"%s\");" % (url, mapping) |
[email protected] | 45ab7ab | 2012-06-06 12:57:44 | [diff] [blame] | 344 | |
| 345 | |
| 346 | def main(): |
| 347 | parser = optparse.OptionParser() |
| 348 | parser.add_option("-j", "--java", |
| 349 | action="store_true", dest="java"); |
| 350 | (options, args) = parser.parse_args(); |
| 351 | if options.java: |
| 352 | generator = GenerateJava() |
| 353 | else: |
| 354 | generator = GenerateCPlusPlus() |
| 355 | generator.Generate() |
| 356 | |
| 357 | if __name__ == '__main__': |
| 358 | main() |