1# This Source Code Form is subject to the terms of the Mozilla Public
2# License, v. 2.0. If a copy of the MPL was not distributed with this
3# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5from marionette_driver.errors import InvalidArgumentException
6
7from marionette_harness import MarionetteTestCase
8
9
10class TestProxy(MarionetteTestCase):
11
12    def setUp(self):
13        super(TestProxy, self).setUp()
14        self.marionette.delete_session()
15
16    def test_that_we_can_set_a_autodetect_proxy(self):
17        capabilities = {"requiredCapabilities":
18                            {
19                                "proxy":{
20                                    "proxyType": "autodetect",
21                                }
22                            }
23                        }
24        self.marionette.start_session(capabilities)
25        result = None
26        with self.marionette.using_context('chrome'):
27            result = self.marionette.execute_script("""return {
28                "proxyType" : Services.prefs.getIntPref('network.proxy.type'),
29                }
30            """)
31
32        self.assertEqual(result["proxyType"], 4)
33
34    def test_that_capabilities_returned_have_proxy_details(self):
35        capabilities = {"requiredCapabilities":
36                            {
37                                "proxy":{
38                                    "proxyType": "autodetect",
39                                    }
40                            }
41                        }
42        self.marionette.start_session(capabilities)
43        result = self.marionette.session_capabilities
44
45        self.assertEqual(result["proxy"]["proxyType"], "autodetect")
46
47    def test_that_we_can_set_a_system_proxy(self):
48        capabilities = {"requiredCapabilities":
49                            {
50                                "proxy":{
51                                    "proxyType": "system",
52                                    }
53                            }
54                       }
55        self.marionette.start_session(capabilities)
56        result = None
57        with self.marionette.using_context('chrome'):
58            result = self.marionette.execute_script("""return {
59                "proxyType" : Services.prefs.getIntPref('network.proxy.type'),
60                }
61            """)
62
63        self.assertEqual(result["proxyType"], 5)
64
65    def test_we_can_set_a_pac_proxy(self):
66        url = "http://marionette.test"
67        capabilities = {"requiredCapabilities":
68                            {
69                                "proxy":{
70                                    "proxyType": "pac",
71                                    "proxyAutoconfigUrl": url,
72                                }
73                            }
74                        }
75        self.marionette.start_session(capabilities)
76        result = None
77        with self.marionette.using_context('chrome'):
78            result = self.marionette.execute_script("""return {
79                "proxyType" : Services.prefs.getIntPref('network.proxy.type'),
80                "proxyAutoconfigUrl" : Services.prefs.getCharPref('network.proxy.autoconfig_url'),
81                }
82            """)
83
84        self.assertEqual(result["proxyType"], 2)
85        self.assertEqual(result["proxyAutoconfigUrl"], url, 'proxyAutoconfigUrl was not set')
86
87    def test_that_we_can_set_a_manual_proxy(self):
88        port = 4444
89        url = "http://marionette.test"
90        capabilities = {"requiredCapabilities":
91                            {
92                                "proxy":{
93                                    "proxyType": "manual",
94                                    "ftpProxy": url,
95                                    "ftpProxyPort": port,
96                                    "httpProxy": url,
97                                    "httpProxyPort": port,
98                                    "sslProxy": url,
99                                    "sslProxyPort": port,
100                                }
101                            }
102                        }
103        self.marionette.start_session(capabilities)
104        result = None
105        with self.marionette.using_context('chrome'):
106            result = self.marionette.execute_script("""return {
107                "proxyType" : Services.prefs.getIntPref('network.proxy.type'),
108                "httpProxy" : Services.prefs.getCharPref('network.proxy.http'),
109                "httpProxyPort": Services.prefs.getIntPref('network.proxy.http_port'),
110                "sslProxy": Services.prefs.getCharPref('network.proxy.ssl'),
111                "sslProxyPort": Services.prefs.getIntPref('network.proxy.ssl_port'),
112                "ftpProxy": Services.prefs.getCharPref('network.proxy.ftp'),
113                "ftpProxyPort": Services.prefs.getIntPref('network.proxy.ftp_port'),
114                }
115            """)
116
117        self.assertEqual(result["proxyType"], 1)
118        self.assertEqual(result["httpProxy"], url, 'httpProxy was not set')
119        self.assertEqual(result["httpProxyPort"], port, 'httpProxyPort was not set')
120        self.assertEqual(result["sslProxy"], url, 'sslProxy url was not set')
121        self.assertEqual(result["sslProxyPort"], port, 'sslProxyPort was not set')
122        self.assertEqual(result["ftpProxy"], url, 'ftpProxy was not set')
123        self.assertEqual(result["ftpProxyPort"], port, 'ftpProxyPort was not set')
124
125    def test_we_can_set_a_manual_proxy_with_a_socks_proxy_with_socks_version(self):
126        port = 4444
127        url = "http://marionette.test"
128        capabilities = {"requiredCapabilities":
129                            {
130                                "proxy":{
131                                    "proxyType": "manual",
132                                    "socksProxy": url,
133                                    "socksProxyPort": port,
134                                    "socksVersion": 4,
135                                    "socksUsername": "cake",
136                                    "socksPassword": "made with cake"
137                                }
138                            }
139                        }
140        self.marionette.start_session(capabilities)
141        result = None
142        with self.marionette.using_context('chrome'):
143            result = self.marionette.execute_script("""return {
144                "proxyType" : Services.prefs.getIntPref('network.proxy.type'),
145                "socksProxy" : Services.prefs.getCharPref('network.proxy.socks'),
146                "socksProxyPort": Services.prefs.getIntPref('network.proxy.socks_port'),
147                "socksVersion": Services.prefs.getIntPref('network.proxy.socks_version'),
148                }
149            """)
150        self.assertEqual(result["socksProxy"], url, 'socksProxy was not set')
151        self.assertEqual(result["socksProxyPort"], port, 'socksProxyPort was not set')
152        self.assertEqual(result["socksVersion"], 4, 'socksVersion was not set to 4')
153
154    def test_we_can_set_a_manual_proxy_with_a_socks_proxy_with_no_socks_version(self):
155        port = 4444
156        url = "http://marionette.test"
157        capabilities = {"requiredCapabilities":
158                                    {
159                                    "proxy":{
160                                        "proxyType": "manual",
161                                        "socksProxy": url,
162                                        "socksProxyPort": port,
163                                        "socksUsername": "cake",
164                                        "socksPassword": "made with cake"
165                                     }
166                                    }
167        }
168        self.marionette.start_session(capabilities)
169        result = None
170        with self.marionette.using_context('chrome'):
171            result = self.marionette.execute_script("""return {
172                "proxyType" : Services.prefs.getIntPref('network.proxy.type'),
173                "socksProxy" : Services.prefs.getCharPref('network.proxy.socks'),
174                "socksProxyPort": Services.prefs.getIntPref('network.proxy.socks_port'),
175                "socksVersion": Services.prefs.getIntPref('network.proxy.socks_version'),
176
177                }
178            """)
179        self.assertEqual(result["socksProxy"], url, 'socksProxy was not set')
180        self.assertEqual(result["socksProxyPort"], port, 'socksProxyPort was not set')
181        self.assertEqual(result["socksVersion"], 5, 'socksVersion was not set to 5')
182
183    def test_when_not_all_manual_proxy_details_are_in_capabilities(self):
184        port = 4444
185        url = "http://marionette.test"
186        capabilities = {"requiredCapabilities":
187                                    {
188                                    "proxy":{
189                                        "proxyType": "manual",
190                                        "ftpProxy": url,
191                                        "ftpProxyPort": port,
192                                     }
193                                    }
194        }
195        self.marionette.start_session(capabilities)
196        result = None
197        with self.marionette.using_context('chrome'):
198            result = self.marionette.execute_script("""return {
199                "proxyType" : Services.prefs.getIntPref('network.proxy.type'),
200                "httpProxy" : Services.prefs.getCharPref('network.proxy.http'),
201                "httpProxyPort": Services.prefs.getIntPref('network.proxy.http_port'),
202                "sslProxy": Services.prefs.getCharPref('network.proxy.ssl'),
203                "sslProxyPort": Services.prefs.getIntPref('network.proxy.ssl_port'),
204                "ftpProxy": Services.prefs.getCharPref('network.proxy.ftp'),
205                "ftpProxyPort": Services.prefs.getIntPref('network.proxy.ftp_port'),
206                }
207            """)
208
209        self.assertEqual(result["proxyType"], 1)
210        self.assertNotEqual(result["httpProxy"], url,
211                            'httpProxy was set. {}'.format(result["httpProxy"]))
212        self.assertNotEqual(result["httpProxyPort"], port, 'httpProxyPort was set')
213        self.assertNotEqual(result["sslProxy"], url, 'sslProxy url was set')
214        self.assertNotEqual(result["sslProxyPort"], port, 'sslProxyPort was set')
215        self.assertEqual(result["ftpProxy"], url, 'ftpProxy was set')
216        self.assertEqual(result["ftpProxyPort"], port, 'ftpProxyPort was set')
217
218
219
220    def test_proxy_is_a_string_should_throw_invalid_argument(self):
221        capabilities = {"requiredCapabilities":
222                                    {
223                                    "proxy":"I really should be a dictionary"
224                                    }
225                            }
226        try:
227            self.marionette.start_session(capabilities)
228            self.fail("We should have started a session because proxy should be a dict")
229        except InvalidArgumentException as e:
230            assert e.message == "Value of 'proxy' should be an object"
231
232    def test_proxy_is_passed_in_with_no_proxy_doesnt_set_it(self):
233        capabilities = {"requiredCapabilities":
234            {
235                "proxy": {"proxyType": "NOPROXY"},
236            }
237        }
238        self.marionette.start_session(capabilities)
239        result = None
240        with self.marionette.using_context('chrome'):
241            result = self.marionette.execute_script("""return {
242                "proxyType": Services.prefs.getIntPref('network.proxy.type'),
243                };
244            """)
245
246        self.assertEqual(result["proxyType"], 0)
247
248    def tearDown(self):
249        if not self.marionette.session:
250            self.marionette.start_session()
251        else:
252            self.marionette.restart(clean=True)
253