Add include.
[chromium-blink-merge.git] / net / android / tools / proxy_test_cases.py
blob3d6e8ec74791e0f459a32dfe39fb3283828a90ca
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.
6 """Generator script for proxy tests.
8 See AndroidProxySelectorTest.java
9 and net/proxy/proxy_config_service_android_unittest.cc
11 To generate C++, run this script without arguments.
12 To generate Java, run this script with -j argument.
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 """
18 import optparse
20 test_cases = [
22 "name": "NoProxy",
23 "description" : "Test direct mapping when no proxy defined.",
24 "properties" : {
26 "mappings" : {
27 "http://example.com/" : "DIRECT",
28 "ftp://example.com/" : "DIRECT",
29 "https://example.com/" : "DIRECT",
33 "name": "HttpProxyHostAndPort",
34 "description" : "Test http.proxyHost and http.proxyPort works.",
35 "properties" : {
36 "http.proxyHost" : "httpproxy.com",
37 "http.proxyPort" : "8080",
39 "mappings" : {
40 "http://example.com/" : "PROXY httpproxy.com:8080",
41 "ftp://example.com/" : "DIRECT",
42 "https://example.com/" : "DIRECT",
46 "name": "HttpProxyHostOnly",
47 "description" : "We should get the default port (80) for proxied hosts.",
48 "properties" : {
49 "http.proxyHost" : "httpproxy.com",
51 "mappings" : {
52 "http://example.com/" : "PROXY httpproxy.com:80",
53 "ftp://example.com/" : "DIRECT",
54 "https://example.com/" : "DIRECT",
58 "name": "HttpProxyPortOnly",
59 "description" :
60 "http.proxyPort only should not result in any hosts being proxied.",
61 "properties" : {
62 "http.proxyPort" : "8080",
64 "mappings" : {
65 "http://example.com/" : "DIRECT",
66 "ftp://example.com/" : "DIRECT",
67 "https://example.com/" : "DIRECT"
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",
78 "mappings" : {
79 "http://example.com/" : "PROXY httpproxy.com:8080",
80 "http://slashdot.org/" : "DIRECT",
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",
91 "mappings" : {
92 "http://example.com/" : "PROXY httpproxy.com:8080",
93 "http://slashdot.org/" : "DIRECT",
94 "http://freecode.net/" : "DIRECT",
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",
105 "mappings" : {
106 "http://example.com/" : "DIRECT",
107 "http://www.example.com/" : "DIRECT",
108 "http://slashdot.org/" : "PROXY httpproxy.com:8080",
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",
119 "mappings" : {
120 "http://example.com/" : "DIRECT",
121 "ftp://example.com/" : "PROXY httpproxy.com:8080",
125 "name": "FtpProxyHostAndPort",
126 "description" : "Test ftp.proxyHost and ftp.proxyPort works.",
127 "properties" : {
128 "ftp.proxyHost" : "httpproxy.com",
129 "ftp.proxyPort" : "8080",
131 "mappings" : {
132 "ftp://example.com/" : "PROXY httpproxy.com:8080",
133 "http://example.com/" : "DIRECT",
134 "https://example.com/" : "DIRECT",
138 "name": "FtpProxyHostOnly",
139 "description" : "Test ftp.proxyHost and default port.",
140 "properties" : {
141 "ftp.proxyHost" : "httpproxy.com",
143 "mappings" : {
144 "ftp://example.com/" : "PROXY httpproxy.com:80",
145 "http://example.com/" : "DIRECT",
146 "https://example.com/" : "DIRECT",
150 "name": "HttpsProxyHostAndPort",
151 "description" : "Test https.proxyHost and https.proxyPort works.",
152 "properties" : {
153 "https.proxyHost" : "httpproxy.com",
154 "https.proxyPort" : "8080",
156 "mappings" : {
157 "https://example.com/" : "PROXY httpproxy.com:8080",
158 "http://example.com/" : "DIRECT",
159 "ftp://example.com/" : "DIRECT",
163 "name": "HttpsProxyHostOnly",
164 "description" : "Test https.proxyHost and default port.",
165 # Chromium differs from the Android platform by connecting to port 80 for
166 # HTTPS connections by default, hence cpp-only.
167 "cpp-only" : "",
168 "properties" : {
169 "https.proxyHost" : "httpproxy.com",
171 "mappings" : {
172 "https://example.com/" : "PROXY httpproxy.com:80",
173 "http://example.com/" : "DIRECT",
174 "ftp://example.com/" : "DIRECT",
178 "name": "HttpProxyHostIPv6",
179 "description" : "Test IPv6 https.proxyHost and default port.",
180 "cpp-only" : "",
181 "properties" : {
182 "http.proxyHost" : "a:b:c::d:1",
184 "mappings" : {
185 "http://example.com/" : "PROXY [a:b:c::d:1]:80",
186 "ftp://example.com/" : "DIRECT",
190 "name": "HttpProxyHostAndPortIPv6",
191 "description" : "Test IPv6 http.proxyHost and http.proxyPort works.",
192 "cpp-only" : "",
193 "properties" : {
194 "http.proxyHost" : "a:b:c::d:1",
195 "http.proxyPort" : "8080",
197 "mappings" : {
198 "http://example.com/" : "PROXY [a:b:c::d:1]:8080",
199 "ftp://example.com/" : "DIRECT",
203 "name": "HttpProxyHostAndInvalidPort",
204 "description" : "Test invalid http.proxyPort does not crash.",
205 "cpp-only" : "",
206 "properties" : {
207 "http.proxyHost" : "a:b:c::d:1",
208 "http.proxyPort" : "65536",
210 "mappings" : {
211 "http://example.com/" : "DIRECT",
212 "ftp://example.com/" : "DIRECT",
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",
225 "mappings" : {
226 "http://example.com/" : "PROXY defaultproxy.com:8080",
227 "https://example.com/" : "PROXY defaultproxy.com:8080",
228 "ftp://example.com/" : "PROXY httpproxy.com:8080",
232 "name": "DefaultProxyDefaultPort",
233 "description" : "Check that the default proxy port is as expected.",
234 # Chromium differs from the Android platform by connecting to port 80 for
235 # HTTPS connections by default, hence cpp-only.
236 "cpp-only" : "",
237 "properties" : {
238 "proxyHost" : "defaultproxy.com",
240 "mappings" : {
241 "http://example.com/" : "PROXY defaultproxy.com:80",
242 "https://example.com/" : "PROXY defaultproxy.com:80",
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"
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",
259 "name": "SocksExplicitPort",
260 "description" : "SOCKS proxy port is used if specified",
261 "properties" : {
262 "socksProxyHost" : "socksproxy.com",
263 "socksProxyPort" : "9000",
265 "mappings" : {
266 "http://example.com/" : "SOCKS5 socksproxy.com:9000",
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",
277 "mappings" : {
278 "http://example.com/" : "PROXY defaultproxy.com:80",
283 class GenerateCPlusPlus:
284 """Generate C++ test cases"""
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 ""
296 def _GenerateDescription(self, description):
297 print " // %s" % description
299 def _GenerateConfiguration(self, properties):
300 for key in sorted(properties.iterkeys()):
301 print " AddProperty(\"%s\", \"%s\");" % (key, properties[key])
302 print " ProxySettingsChanged();"
304 def _GenerateMappings(self, mappings):
305 for url in sorted(mappings.iterkeys()):
306 print " TestMapping(\"%s\", \"%s\");" % (url, mappings[url])
309 class GenerateJava:
310 """Generate Java test cases"""
312 def Generate(self):
313 for test_case in test_cases:
314 if test_case.has_key("cpp-only"):
315 continue
316 if "description" in test_case:
317 self._GenerateDescription(test_case["description"]);
318 print " @SmallTest"
319 print " @Feature({\"AndroidWebView\"})"
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 ""
326 def _GenerateDescription(self, description):
327 print " /**"
328 print " * %s" % description
329 print " *"
330 print " * @throws Exception"
331 print " */"
333 def _GenerateConfiguration(self, properties):
334 for key in sorted(properties.iterkeys()):
335 print " System.setProperty(\"%s\", \"%s\");" % (
336 key, properties[key])
338 def _GenerateMappings(self, mappings):
339 for url in sorted(mappings.iterkeys()):
340 mapping = mappings[url]
341 if 'HTTPS' in mapping:
342 mapping = mapping.replace('HTTPS', 'PROXY')
343 print " checkMapping(\"%s\", \"%s\");" % (url, mapping)
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()
357 if __name__ == '__main__':
358 main()