summaryrefslogtreecommitdiffstats
path: root/testing/marionette/harness/marionette_harness/tests/unit/test_proxy.py
blob: 887d69b764c5d6d3347961ac2b745981f315ef8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from marionette_driver.errors import InvalidArgumentException

from marionette_harness import MarionetteTestCase


class TestProxy(MarionetteTestCase):

    def setUp(self):
        super(TestProxy, self).setUp()
        self.marionette.delete_session()

    def test_that_we_can_set_a_autodetect_proxy(self):
        capabilities = {"requiredCapabilities":
                            {
                                "proxy":{
                                    "proxyType": "autodetect",
                                }
                            }
                        }
        self.marionette.start_session(capabilities)
        result = None
        with self.marionette.using_context('chrome'):
            result = self.marionette.execute_script("""return {
                "proxyType" : Services.prefs.getIntPref('network.proxy.type'),
                }
            """)

        self.assertEqual(result["proxyType"], 4)

    def test_that_capabilities_returned_have_proxy_details(self):
        capabilities = {"requiredCapabilities":
                            {
                                "proxy":{
                                    "proxyType": "autodetect",
                                    }
                            }
                        }
        self.marionette.start_session(capabilities)
        result = self.marionette.session_capabilities

        self.assertEqual(result["proxy"]["proxyType"], "autodetect")

    def test_that_we_can_set_a_system_proxy(self):
        capabilities = {"requiredCapabilities":
                            {
                                "proxy":{
                                    "proxyType": "system",
                                    }
                            }
                       }
        self.marionette.start_session(capabilities)
        result = None
        with self.marionette.using_context('chrome'):
            result = self.marionette.execute_script("""return {
                "proxyType" : Services.prefs.getIntPref('network.proxy.type'),
                }
            """)

        self.assertEqual(result["proxyType"], 5)

    def test_we_can_set_a_pac_proxy(self):
        url = "http://marionette.test"
        capabilities = {"requiredCapabilities":
                            {
                                "proxy":{
                                    "proxyType": "pac",
                                    "proxyAutoconfigUrl": url,
                                }
                            }
                        }
        self.marionette.start_session(capabilities)
        result = None
        with self.marionette.using_context('chrome'):
            result = self.marionette.execute_script("""return {
                "proxyType" : Services.prefs.getIntPref('network.proxy.type'),
                "proxyAutoconfigUrl" : Services.prefs.getCharPref('network.proxy.autoconfig_url'),
                }
            """)

        self.assertEqual(result["proxyType"], 2)
        self.assertEqual(result["proxyAutoconfigUrl"], url, 'proxyAutoconfigUrl was not set')

    def test_that_we_can_set_a_manual_proxy(self):
        port = 4444
        url = "http://marionette.test"
        capabilities = {"requiredCapabilities":
                            {
                                "proxy":{
                                    "proxyType": "manual",
                                    "ftpProxy": url,
                                    "ftpProxyPort": port,
                                    "httpProxy": url,
                                    "httpProxyPort": port,
                                    "sslProxy": url,
                                    "sslProxyPort": port,
                                }
                            }
                        }
        self.marionette.start_session(capabilities)
        result = None
        with self.marionette.using_context('chrome'):
            result = self.marionette.execute_script("""return {
                "proxyType" : Services.prefs.getIntPref('network.proxy.type'),
                "httpProxy" : Services.prefs.getCharPref('network.proxy.http'),
                "httpProxyPort": Services.prefs.getIntPref('network.proxy.http_port'),
                "sslProxy": Services.prefs.getCharPref('network.proxy.ssl'),
                "sslProxyPort": Services.prefs.getIntPref('network.proxy.ssl_port'),
                "ftpProxy": Services.prefs.getCharPref('network.proxy.ftp'),
                "ftpProxyPort": Services.prefs.getIntPref('network.proxy.ftp_port'),
                }
            """)

        self.assertEqual(result["proxyType"], 1)
        self.assertEqual(result["httpProxy"], url, 'httpProxy was not set')
        self.assertEqual(result["httpProxyPort"], port, 'httpProxyPort was not set')
        self.assertEqual(result["sslProxy"], url, 'sslProxy url was not set')
        self.assertEqual(result["sslProxyPort"], port, 'sslProxyPort was not set')
        self.assertEqual(result["ftpProxy"], url, 'ftpProxy was not set')
        self.assertEqual(result["ftpProxyPort"], port, 'ftpProxyPort was not set')

    def test_we_can_set_a_manual_proxy_with_a_socks_proxy_with_socks_version(self):
        port = 4444
        url = "http://marionette.test"
        capabilities = {"requiredCapabilities":
                            {
                                "proxy":{
                                    "proxyType": "manual",
                                    "socksProxy": url,
                                    "socksProxyPort": port,
                                    "socksVersion": 4,
                                    "socksUsername": "cake",
                                    "socksPassword": "made with cake"
                                }
                            }
                        }
        self.marionette.start_session(capabilities)
        result = None
        with self.marionette.using_context('chrome'):
            result = self.marionette.execute_script("""return {
                "proxyType" : Services.prefs.getIntPref('network.proxy.type'),
                "socksProxy" : Services.prefs.getCharPref('network.proxy.socks'),
                "socksProxyPort": Services.prefs.getIntPref('network.proxy.socks_port'),
                "socksVersion": Services.prefs.getIntPref('network.proxy.socks_version'),
                }
            """)
        self.assertEqual(result["socksProxy"], url, 'socksProxy was not set')
        self.assertEqual(result["socksProxyPort"], port, 'socksProxyPort was not set')
        self.assertEqual(result["socksVersion"], 4, 'socksVersion was not set to 4')

    def test_we_can_set_a_manual_proxy_with_a_socks_proxy_with_no_socks_version(self):
        port = 4444
        url = "http://marionette.test"
        capabilities = {"requiredCapabilities":
                                    {
                                    "proxy":{
                                        "proxyType": "manual",
                                        "socksProxy": url,
                                        "socksProxyPort": port,
                                        "socksUsername": "cake",
                                        "socksPassword": "made with cake"
                                     }
                                    }
        }
        self.marionette.start_session(capabilities)
        result = None
        with self.marionette.using_context('chrome'):
            result = self.marionette.execute_script("""return {
                "proxyType" : Services.prefs.getIntPref('network.proxy.type'),
                "socksProxy" : Services.prefs.getCharPref('network.proxy.socks'),
                "socksProxyPort": Services.prefs.getIntPref('network.proxy.socks_port'),
                "socksVersion": Services.prefs.getIntPref('network.proxy.socks_version'),

                }
            """)
        self.assertEqual(result["socksProxy"], url, 'socksProxy was not set')
        self.assertEqual(result["socksProxyPort"], port, 'socksProxyPort was not set')
        self.assertEqual(result["socksVersion"], 5, 'socksVersion was not set to 5')

    def test_when_not_all_manual_proxy_details_are_in_capabilities(self):
        port = 4444
        url = "http://marionette.test"
        capabilities = {"requiredCapabilities":
                                    {
                                    "proxy":{
                                        "proxyType": "manual",
                                        "ftpProxy": url,
                                        "ftpProxyPort": port,
                                     }
                                    }
        }
        self.marionette.start_session(capabilities)
        result = None
        with self.marionette.using_context('chrome'):
            result = self.marionette.execute_script("""return {
                "proxyType" : Services.prefs.getIntPref('network.proxy.type'),
                "httpProxy" : Services.prefs.getCharPref('network.proxy.http'),
                "httpProxyPort": Services.prefs.getIntPref('network.proxy.http_port'),
                "sslProxy": Services.prefs.getCharPref('network.proxy.ssl'),
                "sslProxyPort": Services.prefs.getIntPref('network.proxy.ssl_port'),
                "ftpProxy": Services.prefs.getCharPref('network.proxy.ftp'),
                "ftpProxyPort": Services.prefs.getIntPref('network.proxy.ftp_port'),
                }
            """)

        self.assertEqual(result["proxyType"], 1)
        self.assertNotEqual(result["httpProxy"], url,
                            'httpProxy was set. {}'.format(result["httpProxy"]))
        self.assertNotEqual(result["httpProxyPort"], port, 'httpProxyPort was set')
        self.assertNotEqual(result["sslProxy"], url, 'sslProxy url was set')
        self.assertNotEqual(result["sslProxyPort"], port, 'sslProxyPort was set')
        self.assertEqual(result["ftpProxy"], url, 'ftpProxy was set')
        self.assertEqual(result["ftpProxyPort"], port, 'ftpProxyPort was set')



    def test_proxy_is_a_string_should_throw_invalid_argument(self):
        capabilities = {"requiredCapabilities":
                                    {
                                    "proxy":"I really should be a dictionary"
                                    }
                            }
        try:
            self.marionette.start_session(capabilities)
            self.fail("We should have started a session because proxy should be a dict")
        except InvalidArgumentException as e:
            assert e.message == "Value of 'proxy' should be an object"

    def test_proxy_is_passed_in_with_no_proxy_doesnt_set_it(self):
        capabilities = {"requiredCapabilities":
            {
                "proxy": {"proxyType": "NOPROXY"},
            }
        }
        self.marionette.start_session(capabilities)
        result = None
        with self.marionette.using_context('chrome'):
            result = self.marionette.execute_script("""return {
                "proxyType": Services.prefs.getIntPref('network.proxy.type'),
                };
            """)

        self.assertEqual(result["proxyType"], 0)

    def tearDown(self):
        if not self.marionette.session:
            self.marionette.start_session()
        else:
            self.marionette.restart(clean=True)