Merge lp:~facundo/magicicada-client/kill-qt4 into lp:magicicada-client
- kill-qt4
- Merge into trunk
Proposed by
Facundo Batista
Status: | Merged |
---|---|
Approved by: | Natalia Bidart |
Approved revision: | 1439 |
Merged at revision: | 1439 |
Proposed branch: | lp:~facundo/magicicada-client/kill-qt4 |
Merge into: | lp:magicicada-client |
Diff against target: |
3154 lines (+27/-2840) 24 files modified
bin/ubuntuone-proxy-tunnel (+0/-36) contrib/check-reactor-import (+0/-76) contrib/testing/testcase.py (+2/-16) dependencies.txt (+0/-2) run-tests (+2/-9) run-tests.bat (+0/-2) setup.py (+1/-1) ubuntuone/proxy/__init__.py (+0/-27) ubuntuone/proxy/common.py (+0/-73) ubuntuone/proxy/logger.py (+0/-48) ubuntuone/proxy/tests/__init__.py (+0/-156) ubuntuone/proxy/tests/ssl/dummy.cert (+0/-19) ubuntuone/proxy/tests/ssl/dummy.key (+0/-16) ubuntuone/proxy/tests/test_tunnel_client.py (+0/-225) ubuntuone/proxy/tests/test_tunnel_server.py (+0/-743) ubuntuone/proxy/tunnel_client.py (+0/-202) ubuntuone/proxy/tunnel_server.py (+0/-405) ubuntuone/syncdaemon/action_queue.py (+3/-11) ubuntuone/syncdaemon/tests/test_action_queue.py (+17/-57) ubuntuone/syncdaemon/tests/test_tunnel_runner.py (+0/-185) ubuntuone/syncdaemon/tunnel_runner.py (+0/-86) ubuntuone/syncdaemon/utils.py (+2/-9) ubuntuone/utils/gsettings.py (+0/-114) ubuntuone/utils/tests/test_gsettings.py (+0/-322) |
To merge this branch: | bzr merge lp:~facundo/magicicada-client/kill-qt4 |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Natalia Bidart | Approve | ||
Review via email: mp+341428@code.launchpad.net |
Commit message
Removed everything PyQt related, including the HTTP(S) Proxy.
Description of the change
Removed everything PyQt related, including the HTTP(S) Proxy.
To post a comment you must log in.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === removed file 'bin/ubuntuone-proxy-tunnel' | |||
2 | --- bin/ubuntuone-proxy-tunnel 2015-09-29 21:05:26 +0000 | |||
3 | +++ bin/ubuntuone-proxy-tunnel 1970-01-01 00:00:00 +0000 | |||
4 | @@ -1,36 +0,0 @@ | |||
5 | 1 | #!/usr/bin/python | ||
6 | 2 | # | ||
7 | 3 | # Copyright 2012 Canonical Ltd. | ||
8 | 4 | # | ||
9 | 5 | # This program is free software: you can redistribute it and/or modify it | ||
10 | 6 | # under the terms of the GNU General Public License version 3, as published | ||
11 | 7 | # by the Free Software Foundation. | ||
12 | 8 | # | ||
13 | 9 | # This program is distributed in the hope that it will be useful, but | ||
14 | 10 | # WITHOUT ANY WARRANTY; without even the implied warranties of | ||
15 | 11 | # MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR | ||
16 | 12 | # PURPOSE. See the GNU General Public License for more details. | ||
17 | 13 | # | ||
18 | 14 | # You should have received a copy of the GNU General Public License along | ||
19 | 15 | # with this program. If not, see <http://www.gnu.org/licenses/>. | ||
20 | 16 | # | ||
21 | 17 | # In addition, as a special exception, the copyright holders give | ||
22 | 18 | # permission to link the code of portions of this program with the | ||
23 | 19 | # OpenSSL library under certain conditions as described in each | ||
24 | 20 | # individual source file, and distribute linked combinations | ||
25 | 21 | # including the two. | ||
26 | 22 | # You must obey the GNU General Public License in all respects | ||
27 | 23 | # for all of the code used other than OpenSSL. If you modify | ||
28 | 24 | # file(s) with this exception, you may extend this exception to your | ||
29 | 25 | # version of the file(s), but you are not obligated to do so. If you | ||
30 | 26 | # do not wish to do so, delete this exception statement from your | ||
31 | 27 | # version. If you delete this exception statement from all source | ||
32 | 28 | # files in the program, then also delete it here. | ||
33 | 29 | """Tunnel for proxy support.""" | ||
34 | 30 | |||
35 | 31 | import sys | ||
36 | 32 | |||
37 | 33 | from ubuntuone.proxy.tunnel_server import main | ||
38 | 34 | |||
39 | 35 | if __name__ == "__main__": | ||
40 | 36 | main(sys.argv) | ||
41 | 37 | 0 | ||
42 | === removed file 'contrib/check-reactor-import' | |||
43 | --- contrib/check-reactor-import 2016-05-28 23:32:02 +0000 | |||
44 | +++ contrib/check-reactor-import 1970-01-01 00:00:00 +0000 | |||
45 | @@ -1,76 +0,0 @@ | |||
46 | 1 | #! /usr/bin/python | ||
47 | 2 | # | ||
48 | 3 | # Copyright (C) 2012 Canonical Ltd. | ||
49 | 4 | # | ||
50 | 5 | # This program is free software: you can redistribute it and/or modify it | ||
51 | 6 | # under the terms of the GNU General Public License version 3, as published | ||
52 | 7 | # by the Free Software Foundation. | ||
53 | 8 | # | ||
54 | 9 | # This program is distributed in the hope that it will be useful, but | ||
55 | 10 | # WITHOUT ANY WARRANTY; without even the implied warranties of | ||
56 | 11 | # MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR | ||
57 | 12 | # PURPOSE. See the GNU General Public License for more details. | ||
58 | 13 | # | ||
59 | 14 | # You should have received a copy of the GNU General Public License along | ||
60 | 15 | # with this program. If not, see <http://www.gnu.org/licenses/>. | ||
61 | 16 | # | ||
62 | 17 | # In addition, as a special exception, the copyright holders give | ||
63 | 18 | # permission to link the code of portions of this program with the | ||
64 | 19 | # OpenSSL library under certain conditions as described in each | ||
65 | 20 | # individual source file, and distribute linked combinations | ||
66 | 21 | # including the two. | ||
67 | 22 | # You must obey the GNU General Public License in all respects | ||
68 | 23 | # for all of the code used other than OpenSSL. If you modify | ||
69 | 24 | # file(s) with this exception, you may extend this exception to your | ||
70 | 25 | # version of the file(s), but you are not obligated to do so. If you | ||
71 | 26 | # do not wish to do so, delete this exception statement from your | ||
72 | 27 | # version. If you delete this exception statement from all source | ||
73 | 28 | # files in the program, then also delete it here. | ||
74 | 29 | """A script that checks for unintended imports of twisted.internet.reactor.""" | ||
75 | 30 | |||
76 | 31 | # NOTE: the goal of this script is to avoid a bug that affects | ||
77 | 32 | # ubuntuone-control-panel on windows and darwin. Those platforms use | ||
78 | 33 | # the qt4reactor, and will break if the default reactor is installed | ||
79 | 34 | # first. This can happen if a module used by control-panel imports reactor. | ||
80 | 35 | # Only sub-modules that are not used by ubuntuone-control-panel can safely | ||
81 | 36 | # import reactor at module-level. | ||
82 | 37 | |||
83 | 38 | from __future__ import (unicode_literals, print_function) | ||
84 | 39 | |||
85 | 40 | import __builtin__ | ||
86 | 41 | |||
87 | 42 | import os | ||
88 | 43 | import sys | ||
89 | 44 | import traceback | ||
90 | 45 | |||
91 | 46 | sys.path.append(os.path.abspath(os.getcwd())) | ||
92 | 47 | |||
93 | 48 | |||
94 | 49 | def fake_import(*args, **kwargs): | ||
95 | 50 | """A wrapper for __import__ that dies when importing reactor.""" | ||
96 | 51 | imp_name_base = args[0] | ||
97 | 52 | |||
98 | 53 | if len(args) == 4 and args[3] is not None: | ||
99 | 54 | imp_names = ["{0}.{1}".format(imp_name_base, sm) | ||
100 | 55 | for sm in args[3]] | ||
101 | 56 | else: | ||
102 | 57 | imp_names = [imp_name_base] | ||
103 | 58 | |||
104 | 59 | for imp_name in imp_names: | ||
105 | 60 | if 'twisted.internet.reactor' == imp_name: | ||
106 | 61 | print("ERROR: should not import reactor here:") | ||
107 | 62 | traceback.print_stack() | ||
108 | 63 | sys.exit(1) | ||
109 | 64 | |||
110 | 65 | r = real_import(*args, **kwargs) | ||
111 | 66 | return r | ||
112 | 67 | |||
113 | 68 | |||
114 | 69 | if __name__ == '__main__': | ||
115 | 70 | |||
116 | 71 | real_import = __builtin__.__import__ | ||
117 | 72 | __builtin__.__import__ = fake_import | ||
118 | 73 | |||
119 | 74 | subs = ["", ".tools", ".logger"] | ||
120 | 75 | for module in ["ubuntuone.platform" + p for p in subs]: | ||
121 | 76 | m = __import__(module) | ||
122 | 77 | 0 | ||
123 | === modified file 'contrib/testing/testcase.py' | |||
124 | --- contrib/testing/testcase.py 2016-09-17 01:06:23 +0000 | |||
125 | +++ contrib/testing/testcase.py 2018-03-14 21:14:07 +0000 | |||
126 | @@ -1,7 +1,7 @@ | |||
127 | 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
128 | 2 | # | 2 | # |
129 | 3 | # Copyright 2009-2015 Canonical Ltd. | 3 | # Copyright 2009-2015 Canonical Ltd. |
131 | 4 | # Copyright 2015-2016 Chicharreros (https://launchpad.net/~chicharreros) | 4 | # Copyright 2015-2018 Chicharreros (https://launchpad.net/~chicharreros) |
132 | 5 | # | 5 | # |
133 | 6 | # This program is free software: you can redistribute it and/or modify it | 6 | # This program is free software: you can redistribute it and/or modify it |
134 | 7 | # under the terms of the GNU General Public License version 3, as published | 7 | # under the terms of the GNU General Public License version 3, as published |
135 | @@ -41,7 +41,7 @@ | |||
136 | 41 | from collections import defaultdict | 41 | from collections import defaultdict |
137 | 42 | from functools import wraps | 42 | from functools import wraps |
138 | 43 | 43 | ||
140 | 44 | from twisted.internet import defer, reactor | 44 | from twisted.internet import defer |
141 | 45 | from twisted.trial.unittest import TestCase as TwistedTestCase | 45 | from twisted.trial.unittest import TestCase as TwistedTestCase |
142 | 46 | from ubuntuone.devtools.testcases import skipIfOS | 46 | from ubuntuone.devtools.testcases import skipIfOS |
143 | 47 | from zope.interface import implements | 47 | from zope.interface import implements |
144 | @@ -312,17 +312,6 @@ | |||
145 | 312 | return defer.succeed(True) | 312 | return defer.succeed(True) |
146 | 313 | 313 | ||
147 | 314 | 314 | ||
148 | 315 | class FakeTunnelRunner(object): | ||
149 | 316 | """A fake proxy.tunnel_client.TunnelRunner.""" | ||
150 | 317 | |||
151 | 318 | def __init__(self, *args): | ||
152 | 319 | """Fake a proxy tunnel.""" | ||
153 | 320 | |||
154 | 321 | def get_client(self): | ||
155 | 322 | """Always return the reactor.""" | ||
156 | 323 | return defer.succeed(reactor) | ||
157 | 324 | |||
158 | 325 | |||
159 | 326 | class BaseTwistedTestCase(TwistedTestCase): | 315 | class BaseTwistedTestCase(TwistedTestCase): |
160 | 327 | """Base TestCase with helper methods to handle temp dir. | 316 | """Base TestCase with helper methods to handle temp dir. |
161 | 328 | 317 | ||
162 | @@ -332,7 +321,6 @@ | |||
163 | 332 | makedirs(path): support read-only shares | 321 | makedirs(path): support read-only shares |
164 | 333 | """ | 322 | """ |
165 | 334 | MAX_FILENAME = 32 # some platforms limit lengths of filenames | 323 | MAX_FILENAME = 32 # some platforms limit lengths of filenames |
166 | 335 | tunnel_runner_class = FakeTunnelRunner | ||
167 | 336 | 324 | ||
168 | 337 | def mktemp(self, name='temp'): | 325 | def mktemp(self, name='temp'): |
169 | 338 | """ Customized mktemp that accepts an optional name argument. """ | 326 | """ Customized mktemp that accepts an optional name argument. """ |
170 | @@ -439,8 +427,6 @@ | |||
171 | 439 | self.log = logging.getLogger("ubuntuone.SyncDaemon.TEST") | 427 | self.log = logging.getLogger("ubuntuone.SyncDaemon.TEST") |
172 | 440 | self.log.info("starting test %s.%s", self.__class__.__name__, | 428 | self.log.info("starting test %s.%s", self.__class__.__name__, |
173 | 441 | self._testMethodName) | 429 | self._testMethodName) |
174 | 442 | self.patch(action_queue.tunnel_runner, "TunnelRunner", | ||
175 | 443 | self.tunnel_runner_class) | ||
176 | 444 | 430 | ||
177 | 445 | 431 | ||
178 | 446 | class FakeMainTestCase(BaseTwistedTestCase): | 432 | class FakeMainTestCase(BaseTwistedTestCase): |
179 | 447 | 433 | ||
180 | === modified file 'dependencies.txt' | |||
181 | --- dependencies.txt 2017-01-07 18:51:07 +0000 | |||
182 | +++ dependencies.txt 2018-03-14 21:14:07 +0000 | |||
183 | @@ -6,6 +6,4 @@ | |||
184 | 6 | python-gi | 6 | python-gi |
185 | 7 | python-protobuf | 7 | python-protobuf |
186 | 8 | python-pyinotify | 8 | python-pyinotify |
187 | 9 | python-qt4-dbus | ||
188 | 10 | python-qt4reactor | ||
189 | 11 | python-twisted | 9 | python-twisted |
190 | 12 | 10 | ||
191 | === modified file 'run-tests' | |||
192 | --- run-tests 2016-05-29 20:16:26 +0000 | |||
193 | +++ run-tests 2018-03-14 21:14:07 +0000 | |||
194 | @@ -1,6 +1,7 @@ | |||
195 | 1 | #! /bin/bash | 1 | #! /bin/bash |
196 | 2 | # | 2 | # |
197 | 3 | # Copyright 2012-2013 Canonical Ltd. | 3 | # Copyright 2012-2013 Canonical Ltd. |
198 | 4 | # Copyright 2015-2018 Chicharreros (https://launchpad.net/~chicharreros) | ||
199 | 4 | # | 5 | # |
200 | 5 | # This program is free software: you can redistribute it and/or modify it | 6 | # This program is free software: you can redistribute it and/or modify it |
201 | 6 | # under the terms of the GNU General Public License version 3, as published | 7 | # under the terms of the GNU General Public License version 3, as published |
202 | @@ -27,8 +28,6 @@ | |||
203 | 27 | # version. If you delete this exception statement from all source | 28 | # version. If you delete this exception statement from all source |
204 | 28 | # files in the program, then also delete it here. | 29 | # files in the program, then also delete it here. |
205 | 29 | 30 | ||
206 | 30 | PROXY_TESTS_PATH="ubuntuone/proxy/tests" | ||
207 | 31 | |||
208 | 32 | # Allow alternative python executable via environment variable. This is | 31 | # Allow alternative python executable via environment variable. This is |
209 | 33 | # useful for virtualenv testing. | 32 | # useful for virtualenv testing. |
210 | 34 | PYTHON=${PYTHON:-'python'} | 33 | PYTHON=${PYTHON:-'python'} |
211 | @@ -47,22 +46,16 @@ | |||
212 | 47 | if [ "$SYSNAME" == "Darwin" ]; then | 46 | if [ "$SYSNAME" == "Darwin" ]; then |
213 | 48 | IGNORE_FILES="test_linux.py,test_windows.py" | 47 | IGNORE_FILES="test_linux.py,test_windows.py" |
214 | 49 | IGNORE_PATHS="ubuntuone/platform/tests/linux" | 48 | IGNORE_PATHS="ubuntuone/platform/tests/linux" |
215 | 50 | REACTOR=qt4 | ||
216 | 51 | else | 49 | else |
217 | 52 | # Linux | 50 | # Linux |
218 | 53 | IGNORE_FILES="test_darwin.py,test_fsevents_daemon.py,test_windows.py" | 51 | IGNORE_FILES="test_darwin.py,test_fsevents_daemon.py,test_windows.py" |
219 | 54 | IGNORE_PATHS="ubuntuone/platform/tests/windows" | 52 | IGNORE_PATHS="ubuntuone/platform/tests/windows" |
220 | 55 | REACTOR=gi | ||
221 | 56 | fi | 53 | fi |
222 | 57 | 54 | ||
223 | 58 | echo "*** Running test suite for ""$MODULE"" ***" | 55 | echo "*** Running test suite for ""$MODULE"" ***" |
224 | 59 | export SSL_CERTIFICATES_DIR=/etc/ssl/certs | 56 | export SSL_CERTIFICATES_DIR=/etc/ssl/certs |
225 | 60 | $PYTHON ./setup.py build | 57 | $PYTHON ./setup.py build |
229 | 61 | u1trial --reactor=$REACTOR -i "$IGNORE_FILES" -p "$IGNORE_PATHS,$PROXY_TESTS_PATH" "$MODULE" | 58 | u1trial -i "$IGNORE_FILES" -p "$IGNORE_PATHS" "$MODULE" |
227 | 62 | echo "*** Running tests for ubuntuone-client-proxy ***" | ||
228 | 63 | u1trial --reactor=qt4 -i "$IGNORE_FILES" -p "$IGNORE_PATHS" "$PROXY_TESTS_PATH" | ||
230 | 64 | $PYTHON ./setup.py clean | 59 | $PYTHON ./setup.py clean |
231 | 65 | rm -rf _trial_temp | 60 | rm -rf _trial_temp |
232 | 66 | rm -rf build | 61 | rm -rf build |
233 | 67 | |||
234 | 68 | $PYTHON contrib/check-reactor-import | ||
235 | 69 | 62 | ||
236 | === modified file 'run-tests.bat' | |||
237 | --- run-tests.bat 2013-06-10 19:27:21 +0000 | |||
238 | +++ run-tests.bat 2018-03-14 21:14:07 +0000 | |||
239 | @@ -87,8 +87,6 @@ | |||
240 | 87 | ECHO Performing style checks... | 87 | ECHO Performing style checks... |
241 | 88 | "%LINTPATH%" | 88 | "%LINTPATH%" |
242 | 89 | 89 | ||
243 | 90 | "%PYTHONEXEPATH%" contrib\check-reactor-import | ||
244 | 91 | |||
245 | 92 | :: if pep8 is not present, move to the end | 90 | :: if pep8 is not present, move to the end |
246 | 93 | IF EXIST "%PEP8PATH%" ( | 91 | IF EXIST "%PEP8PATH%" ( |
247 | 94 | "%PEP8PATH%" --repeat ubuntuone | 92 | "%PEP8PATH%" --repeat ubuntuone |
248 | 95 | 93 | ||
249 | === modified file 'setup.py' | |||
250 | --- setup.py 2016-08-06 20:02:14 +0000 | |||
251 | +++ setup.py 2018-03-14 21:14:07 +0000 | |||
252 | @@ -1,6 +1,7 @@ | |||
253 | 1 | #!/usr/bin/python | 1 | #!/usr/bin/python |
254 | 2 | # | 2 | # |
255 | 3 | # Copyright 2013 Canonical Ltd. | 3 | # Copyright 2013 Canonical Ltd. |
256 | 4 | # Copyright 2015-2018 Chicharreros (https://launchpad.net/~chicharreros) | ||
257 | 4 | # | 5 | # |
258 | 5 | # This program is free software: you can redistribute it and/or modify it | 6 | # This program is free software: you can redistribute it and/or modify it |
259 | 6 | # under the terms of the GNU General Public License version 3, as published | 7 | # under the terms of the GNU General Public License version 3, as published |
260 | @@ -203,7 +204,6 @@ | |||
261 | 203 | ] | 204 | ] |
262 | 204 | 205 | ||
263 | 205 | libexec_scripts = [ | 206 | libexec_scripts = [ |
264 | 206 | 'bin/ubuntuone-proxy-tunnel', | ||
265 | 207 | 'bin/ubuntuone-syncdaemon', | 207 | 'bin/ubuntuone-syncdaemon', |
266 | 208 | ] | 208 | ] |
267 | 209 | 209 | ||
268 | 210 | 210 | ||
269 | === removed directory 'ubuntuone/proxy' | |||
270 | === removed file 'ubuntuone/proxy/__init__.py' | |||
271 | --- ubuntuone/proxy/__init__.py 2016-05-29 00:50:05 +0000 | |||
272 | +++ ubuntuone/proxy/__init__.py 1970-01-01 00:00:00 +0000 | |||
273 | @@ -1,27 +0,0 @@ | |||
274 | 1 | # Copyright 2012 Canonical Ltd. | ||
275 | 2 | # | ||
276 | 3 | # This program is free software: you can redistribute it and/or modify it | ||
277 | 4 | # under the terms of the GNU General Public License version 3, as published | ||
278 | 5 | # by the Free Software Foundation. | ||
279 | 6 | # | ||
280 | 7 | # This program is distributed in the hope that it will be useful, but | ||
281 | 8 | # WITHOUT ANY WARRANTY; without even the implied warranties of | ||
282 | 9 | # MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR | ||
283 | 10 | # PURPOSE. See the GNU General Public License for more details. | ||
284 | 11 | # | ||
285 | 12 | # You should have received a copy of the GNU General Public License along | ||
286 | 13 | # with this program. If not, see <http://www.gnu.org/licenses/>. | ||
287 | 14 | # | ||
288 | 15 | # In addition, as a special exception, the copyright holders give | ||
289 | 16 | # permission to link the code of portions of this program with the | ||
290 | 17 | # OpenSSL library under certain conditions as described in each | ||
291 | 18 | # individual source file, and distribute linked combinations | ||
292 | 19 | # including the two. | ||
293 | 20 | # You must obey the GNU General Public License in all respects | ||
294 | 21 | # for all of the code used other than OpenSSL. If you modify | ||
295 | 22 | # file(s) with this exception, you may extend this exception to your | ||
296 | 23 | # version of the file(s), but you are not obligated to do so. If you | ||
297 | 24 | # do not wish to do so, delete this exception statement from your | ||
298 | 25 | # version. If you delete this exception statement from all source | ||
299 | 26 | # files in the program, then also delete it here. | ||
300 | 27 | """Proxy support.""" | ||
301 | 28 | 0 | ||
302 | === removed file 'ubuntuone/proxy/common.py' | |||
303 | --- ubuntuone/proxy/common.py 2012-04-09 20:08:42 +0000 | |||
304 | +++ ubuntuone/proxy/common.py 1970-01-01 00:00:00 +0000 | |||
305 | @@ -1,73 +0,0 @@ | |||
306 | 1 | # -*- coding: utf-8 -*- | ||
307 | 2 | # | ||
308 | 3 | # Copyright 2012 Canonical Ltd. | ||
309 | 4 | # | ||
310 | 5 | # This program is free software: you can redistribute it and/or modify it | ||
311 | 6 | # under the terms of the GNU General Public License version 3, as published | ||
312 | 7 | # by the Free Software Foundation. | ||
313 | 8 | # | ||
314 | 9 | # This program is distributed in the hope that it will be useful, but | ||
315 | 10 | # WITHOUT ANY WARRANTY; without even the implied warranties of | ||
316 | 11 | # MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR | ||
317 | 12 | # PURPOSE. See the GNU General Public License for more details. | ||
318 | 13 | # | ||
319 | 14 | # You should have received a copy of the GNU General Public License along | ||
320 | 15 | # with this program. If not, see <http://www.gnu.org/licenses/>. | ||
321 | 16 | # | ||
322 | 17 | # In addition, as a special exception, the copyright holders give | ||
323 | 18 | # permission to link the code of portions of this program with the | ||
324 | 19 | # OpenSSL library under certain conditions as described in each | ||
325 | 20 | # individual source file, and distribute linked combinations | ||
326 | 21 | # including the two. | ||
327 | 22 | # You must obey the GNU General Public License in all respects | ||
328 | 23 | # for all of the code used other than OpenSSL. If you modify | ||
329 | 24 | # file(s) with this exception, you may extend this exception to your | ||
330 | 25 | # version of the file(s), but you are not obligated to do so. If you | ||
331 | 26 | # do not wish to do so, delete this exception statement from your | ||
332 | 27 | # version. If you delete this exception statement from all source | ||
333 | 28 | # files in the program, then also delete it here. | ||
334 | 29 | """Common classes to the tunnel client and server.""" | ||
335 | 30 | |||
336 | 31 | from twisted.protocols import basic | ||
337 | 32 | |||
338 | 33 | CRLF = "\r\n" | ||
339 | 34 | TUNNEL_PORT_LABEL = "Tunnel port" | ||
340 | 35 | TUNNEL_COOKIE_LABEL = "Tunnel cookie" | ||
341 | 36 | TUNNEL_COOKIE_HEADER = "Proxy-Tunnel-Cookie" | ||
342 | 37 | |||
343 | 38 | |||
344 | 39 | class BaseTunnelProtocol(basic.LineReceiver): | ||
345 | 40 | """CONNECT base protocol for tunnelling connections.""" | ||
346 | 41 | |||
347 | 42 | delimiter = CRLF | ||
348 | 43 | |||
349 | 44 | def __init__(self): | ||
350 | 45 | """Initialize this protocol.""" | ||
351 | 46 | self._first_line = True | ||
352 | 47 | self.received_headers = [] | ||
353 | 48 | |||
354 | 49 | def header_line(self, line): | ||
355 | 50 | """Handle each header line received.""" | ||
356 | 51 | key, value = line.split(":", 1) | ||
357 | 52 | value = value.strip() | ||
358 | 53 | self.received_headers.append((key, value)) | ||
359 | 54 | |||
360 | 55 | def lineReceived(self, line): | ||
361 | 56 | """Process a line in the header.""" | ||
362 | 57 | if self._first_line: | ||
363 | 58 | self._first_line = False | ||
364 | 59 | self.handle_first_line(line) | ||
365 | 60 | else: | ||
366 | 61 | if line: | ||
367 | 62 | self.header_line(line) | ||
368 | 63 | else: | ||
369 | 64 | self.setRawMode() | ||
370 | 65 | self.headers_done() | ||
371 | 66 | |||
372 | 67 | def remote_disconnected(self): | ||
373 | 68 | """The remote end closed the connection.""" | ||
374 | 69 | self.transport.loseConnection() | ||
375 | 70 | |||
376 | 71 | def format_headers(self, headers): | ||
377 | 72 | """Format some headers as a few response lines.""" | ||
378 | 73 | return "".join("%s: %s" % item + CRLF for item in headers.items()) | ||
379 | 74 | 0 | ||
380 | === removed file 'ubuntuone/proxy/logger.py' | |||
381 | --- ubuntuone/proxy/logger.py 2012-06-21 18:58:50 +0000 | |||
382 | +++ ubuntuone/proxy/logger.py 1970-01-01 00:00:00 +0000 | |||
383 | @@ -1,48 +0,0 @@ | |||
384 | 1 | # ubuntuone.syncdaemon.logger - logging utilities | ||
385 | 2 | # | ||
386 | 3 | # Copyright 2009-2012 Canonical Ltd. | ||
387 | 4 | # | ||
388 | 5 | # This program is free software: you can redistribute it and/or modify it | ||
389 | 6 | # under the terms of the GNU General Public License version 3, as published | ||
390 | 7 | # by the Free Software Foundation. | ||
391 | 8 | # | ||
392 | 9 | # This program is distributed in the hope that it will be useful, but | ||
393 | 10 | # WITHOUT ANY WARRANTY; without even the implied warranties of | ||
394 | 11 | # MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR | ||
395 | 12 | # PURPOSE. See the GNU General Public License for more details. | ||
396 | 13 | # | ||
397 | 14 | # You should have received a copy of the GNU General Public License along | ||
398 | 15 | # with this program. If not, see <http://www.gnu.org/licenses/>. | ||
399 | 16 | # | ||
400 | 17 | # In addition, as a special exception, the copyright holders give | ||
401 | 18 | # permission to link the code of portions of this program with the | ||
402 | 19 | # OpenSSL library under certain conditions as described in each | ||
403 | 20 | # individual source file, and distribute linked combinations | ||
404 | 21 | # including the two. | ||
405 | 22 | # You must obey the GNU General Public License in all respects | ||
406 | 23 | # for all of the code used other than OpenSSL. If you modify | ||
407 | 24 | # file(s) with this exception, you may extend this exception to your | ||
408 | 25 | # version of the file(s), but you are not obligated to do so. If you | ||
409 | 26 | # do not wish to do so, delete this exception statement from your | ||
410 | 27 | # version. If you delete this exception statement from all source | ||
411 | 28 | # files in the program, then also delete it here. | ||
412 | 29 | """SyncDaemon logging utilities and config.""" | ||
413 | 30 | |||
414 | 31 | import logging | ||
415 | 32 | import os | ||
416 | 33 | |||
417 | 34 | from ubuntuone.logger import ( | ||
418 | 35 | basic_formatter, | ||
419 | 36 | CustomRotatingFileHandler, | ||
420 | 37 | ) | ||
421 | 38 | |||
422 | 39 | from ubuntuone.platform.logger import ubuntuone_log_dir | ||
423 | 40 | |||
424 | 41 | |||
425 | 42 | LOGFILENAME = os.path.join(ubuntuone_log_dir, 'proxy.log') | ||
426 | 43 | logger = logging.getLogger("ubuntuone.proxy") | ||
427 | 44 | logger.setLevel(logging.DEBUG) | ||
428 | 45 | handler = CustomRotatingFileHandler(filename=LOGFILENAME) | ||
429 | 46 | handler.setFormatter(basic_formatter) | ||
430 | 47 | handler.setLevel(logging.DEBUG) | ||
431 | 48 | logger.addHandler(handler) | ||
432 | 49 | 0 | ||
433 | === removed directory 'ubuntuone/proxy/tests' | |||
434 | === removed file 'ubuntuone/proxy/tests/__init__.py' | |||
435 | --- ubuntuone/proxy/tests/__init__.py 2016-06-01 18:28:19 +0000 | |||
436 | +++ ubuntuone/proxy/tests/__init__.py 1970-01-01 00:00:00 +0000 | |||
437 | @@ -1,156 +0,0 @@ | |||
438 | 1 | # Copyright 2012 Canonical Ltd. | ||
439 | 2 | # | ||
440 | 3 | # This program is free software: you can redistribute it and/or modify it | ||
441 | 4 | # under the terms of the GNU General Public License version 3, as published | ||
442 | 5 | # by the Free Software Foundation. | ||
443 | 6 | # | ||
444 | 7 | # This program is distributed in the hope that it will be useful, but | ||
445 | 8 | # WITHOUT ANY WARRANTY; without even the implied warranties of | ||
446 | 9 | # MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR | ||
447 | 10 | # PURPOSE. See the GNU General Public License for more details. | ||
448 | 11 | # | ||
449 | 12 | # You should have received a copy of the GNU General Public License along | ||
450 | 13 | # with this program. If not, see <http://www.gnu.org/licenses/>. | ||
451 | 14 | # | ||
452 | 15 | # In addition, as a special exception, the copyright holders give | ||
453 | 16 | # permission to link the code of portions of this program with the | ||
454 | 17 | # OpenSSL library under certain conditions as described in each | ||
455 | 18 | # individual source file, and distribute linked combinations | ||
456 | 19 | # including the two. | ||
457 | 20 | # You must obey the GNU General Public License in all respects | ||
458 | 21 | # for all of the code used other than OpenSSL. If you modify | ||
459 | 22 | # file(s) with this exception, you may extend this exception to your | ||
460 | 23 | # version of the file(s), but you are not obligated to do so. If you | ||
461 | 24 | # do not wish to do so, delete this exception statement from your | ||
462 | 25 | # version. If you delete this exception statement from all source | ||
463 | 26 | # files in the program, then also delete it here. | ||
464 | 27 | """Tests for the Magicicada proxy support.""" | ||
465 | 28 | |||
466 | 29 | from os import path | ||
467 | 30 | from StringIO import StringIO | ||
468 | 31 | |||
469 | 32 | from twisted.application import internet, service | ||
470 | 33 | from twisted.internet import defer, ssl | ||
471 | 34 | from twisted.web import http, resource, server | ||
472 | 35 | |||
473 | 36 | SAMPLE_CONTENT = "hello world!" | ||
474 | 37 | SIMPLERESOURCE = "simpleresource" | ||
475 | 38 | DUMMY_KEY_FILENAME = "dummy.key" | ||
476 | 39 | DUMMY_CERT_FILENAME = "dummy.cert" | ||
477 | 40 | FAKE_COOKIE = "fa:ke:co:ok:ie" | ||
478 | 41 | |||
479 | 42 | |||
480 | 43 | class SaveHTTPChannel(http.HTTPChannel): | ||
481 | 44 | """A save protocol to be used in tests.""" | ||
482 | 45 | |||
483 | 46 | protocolInstance = None | ||
484 | 47 | |||
485 | 48 | def connectionMade(self): | ||
486 | 49 | """Keep track of the given protocol.""" | ||
487 | 50 | SaveHTTPChannel.protocolInstance = self | ||
488 | 51 | http.HTTPChannel.connectionMade(self) | ||
489 | 52 | |||
490 | 53 | |||
491 | 54 | class SaveSite(server.Site): | ||
492 | 55 | """A site that let us know when it's closed.""" | ||
493 | 56 | |||
494 | 57 | protocol = SaveHTTPChannel | ||
495 | 58 | |||
496 | 59 | def __init__(self, *args, **kwargs): | ||
497 | 60 | """Create a new instance.""" | ||
498 | 61 | server.Site.__init__(self, *args, **kwargs) | ||
499 | 62 | self.timeOut = None | ||
500 | 63 | |||
501 | 64 | |||
502 | 65 | class BaseMockWebServer(object): | ||
503 | 66 | """A mock webserver for testing""" | ||
504 | 67 | |||
505 | 68 | def __init__(self): | ||
506 | 69 | """Start up this instance.""" | ||
507 | 70 | self.root = self.get_root_resource() | ||
508 | 71 | self.site = SaveSite(self.root) | ||
509 | 72 | application = service.Application('web') | ||
510 | 73 | self.service_collection = service.IServiceCollection(application) | ||
511 | 74 | self.tcpserver = internet.TCPServer(0, self.site) | ||
512 | 75 | self.tcpserver.setServiceParent(self.service_collection) | ||
513 | 76 | self.sslserver = internet.SSLServer(0, self.site, self.get_context()) | ||
514 | 77 | self.sslserver.setServiceParent(self.service_collection) | ||
515 | 78 | self.service_collection.startService() | ||
516 | 79 | |||
517 | 80 | def get_dummy_path(self, filename): | ||
518 | 81 | """Path pointing at the dummy certificate files.""" | ||
519 | 82 | base_path = path.dirname(__file__) | ||
520 | 83 | return path.join(base_path, "ssl", filename) | ||
521 | 84 | |||
522 | 85 | def get_context(self): | ||
523 | 86 | """Return an ssl context.""" | ||
524 | 87 | key_path = self.get_dummy_path(DUMMY_KEY_FILENAME) | ||
525 | 88 | cert_path = self.get_dummy_path(DUMMY_CERT_FILENAME) | ||
526 | 89 | return ssl.DefaultOpenSSLContextFactory(key_path, cert_path) | ||
527 | 90 | |||
528 | 91 | def get_root_resource(self): | ||
529 | 92 | """Get the root resource with all the children.""" | ||
530 | 93 | raise NotImplementedError | ||
531 | 94 | |||
532 | 95 | def get_iri(self): | ||
533 | 96 | """Build the iri for this mock server.""" | ||
534 | 97 | port_num = self.tcpserver._port.getHost().port | ||
535 | 98 | return u"http://0.0.0.0:%d/" % port_num | ||
536 | 99 | |||
537 | 100 | def get_ssl_iri(self): | ||
538 | 101 | """Build the iri for the ssl mock server.""" | ||
539 | 102 | port_num = self.sslserver._port.getHost().port | ||
540 | 103 | return u"https://0.0.0.0:%d/" % port_num | ||
541 | 104 | |||
542 | 105 | def stop(self): | ||
543 | 106 | """Shut it down.""" | ||
544 | 107 | if self.site.protocol.protocolInstance: | ||
545 | 108 | self.site.protocol.protocolInstance.timeoutConnection() | ||
546 | 109 | return self.service_collection.stopService() | ||
547 | 110 | |||
548 | 111 | |||
549 | 112 | class SimpleResource(resource.Resource): | ||
550 | 113 | """A simple web resource.""" | ||
551 | 114 | |||
552 | 115 | def __init__(self): | ||
553 | 116 | """Initialize this mock resource.""" | ||
554 | 117 | resource.Resource.__init__(self) | ||
555 | 118 | self.rendered = defer.Deferred() | ||
556 | 119 | |||
557 | 120 | def render_GET(self, request): | ||
558 | 121 | """Make a bit of html out of the resource's content.""" | ||
559 | 122 | if not self.rendered.called: | ||
560 | 123 | self.rendered.callback(None) | ||
561 | 124 | return SAMPLE_CONTENT | ||
562 | 125 | |||
563 | 126 | |||
564 | 127 | class MockWebServer(BaseMockWebServer): | ||
565 | 128 | """A mock webserver.""" | ||
566 | 129 | |||
567 | 130 | def __init__(self): | ||
568 | 131 | """Initialize this mock server.""" | ||
569 | 132 | self.simple_resource = SimpleResource() | ||
570 | 133 | super(MockWebServer, self).__init__() | ||
571 | 134 | |||
572 | 135 | def get_root_resource(self): | ||
573 | 136 | """Get the root resource with all the children.""" | ||
574 | 137 | root = resource.Resource() | ||
575 | 138 | root.putChild(SIMPLERESOURCE, self.simple_resource) | ||
576 | 139 | return root | ||
577 | 140 | |||
578 | 141 | |||
579 | 142 | class FakeTransport(StringIO): | ||
580 | 143 | """A fake transport that stores everything written to it.""" | ||
581 | 144 | |||
582 | 145 | connected = True | ||
583 | 146 | disconnecting = False | ||
584 | 147 | cookie = None | ||
585 | 148 | |||
586 | 149 | def loseConnection(self): | ||
587 | 150 | """Mark the connection as lost.""" | ||
588 | 151 | self.connected = False | ||
589 | 152 | self.disconnecting = True | ||
590 | 153 | |||
591 | 154 | def getPeer(self): | ||
592 | 155 | """Return the peer IAddress.""" | ||
593 | 156 | return None | ||
594 | 157 | 0 | ||
595 | === removed directory 'ubuntuone/proxy/tests/ssl' | |||
596 | === removed file 'ubuntuone/proxy/tests/ssl/dummy.cert' | |||
597 | --- ubuntuone/proxy/tests/ssl/dummy.cert 2012-02-23 23:58:33 +0000 | |||
598 | +++ ubuntuone/proxy/tests/ssl/dummy.cert 1970-01-01 00:00:00 +0000 | |||
599 | @@ -1,19 +0,0 @@ | |||
600 | 1 | -----BEGIN CERTIFICATE----- | ||
601 | 2 | MIIDEDCCAnmgAwIBAgIJAM/bIJ77awBCMA0GCSqGSIb3DQEBBQUAMIGgMQswCQYD | ||
602 | 3 | VQQGEwJBUjETMBEGA1UECAwKRmFrZSBTdGF0ZTESMBAGA1UEBwwJRmFrZSBDaXR5 | ||
603 | 4 | MRUwEwYDVQQKDAxGYWtlIENvbXBhbnkxFjAUBgNVBAsMDUZha2UgRGl2aXNpb24x | ||
604 | 5 | EjAQBgNVBAMMCUZha2UgTmFtZTElMCMGCSqGSIb3DQEJARYWZmFrZUBlbWFpbC5h | ||
605 | 6 | ZGRyZXNzLm5vdDAeFw0xMjAyMjIxOTI0MjBaFw0yMjAyMjMxOTI0MjBaMIGgMQsw | ||
606 | 7 | CQYDVQQGEwJBUjETMBEGA1UECAwKRmFrZSBTdGF0ZTESMBAGA1UEBwwJRmFrZSBD | ||
607 | 8 | aXR5MRUwEwYDVQQKDAxGYWtlIENvbXBhbnkxFjAUBgNVBAsMDUZha2UgRGl2aXNp | ||
608 | 9 | b24xEjAQBgNVBAMMCUZha2UgTmFtZTElMCMGCSqGSIb3DQEJARYWZmFrZUBlbWFp | ||
609 | 10 | bC5hZGRyZXNzLm5vdDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA0hbliGty | ||
610 | 11 | HwfZixU609UHBQdbfO+oObrPIrIawWX5FxD6KhX4ei23idmpyYEcXLK4ivNlT4dW | ||
611 | 12 | 27bvhtpf6/FBbu9e1YdwcdDNoXajr9Ia4NZJyANgo9b5UIsnyTc45NlnpZgRg5zc | ||
612 | 13 | Oz7Vwwr4qf6r1ljK/I2mAO7rlpH5Ak9J+RkCAwEAAaNQME4wHQYDVR0OBBYEFLwr | ||
613 | 14 | ps/JLNcfpSuuylMnkvImVvkgMB8GA1UdIwQYMBaAFLwrps/JLNcfpSuuylMnkvIm | ||
614 | 15 | VvkgMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADgYEAWYDBAr0MgpnBxIne | ||
615 | 16 | WRz8MX0/c7IqrEuZCYMSGnU7PoX3GdNk1Lkif1ufELKSoG8jY16CDgEl26GPxA1k | ||
616 | 17 | Tho7MWSLikLbuQYJs2saF9by0Y/Mrau0auxEnpHZ7pkybeKFnrIqiNKvTVMnjo5T | ||
617 | 18 | FMET5qEOKKvp9IOnezCYX1nYXyY= | ||
618 | 19 | -----END CERTIFICATE----- | ||
619 | 20 | 0 | ||
620 | === removed file 'ubuntuone/proxy/tests/ssl/dummy.key' | |||
621 | --- ubuntuone/proxy/tests/ssl/dummy.key 2012-02-23 23:58:33 +0000 | |||
622 | +++ ubuntuone/proxy/tests/ssl/dummy.key 1970-01-01 00:00:00 +0000 | |||
623 | @@ -1,16 +0,0 @@ | |||
624 | 1 | -----BEGIN PRIVATE KEY----- | ||
625 | 2 | MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBANIW5Yhrch8H2YsV | ||
626 | 3 | OtPVBwUHW3zvqDm6zyKyGsFl+RcQ+ioV+Hott4nZqcmBHFyyuIrzZU+HVtu274ba | ||
627 | 4 | X+vxQW7vXtWHcHHQzaF2o6/SGuDWScgDYKPW+VCLJ8k3OOTZZ6WYEYOc3Ds+1cMK | ||
628 | 5 | +Kn+q9ZYyvyNpgDu65aR+QJPSfkZAgMBAAECgYBSxFh7TTExjmsjAyMg700LqyFc | ||
629 | 6 | 8CHLVJBkL9ygkqb2cmbMC8nPgJFNSqY8T5Q35OUVQNyJ31zVxJVLAF9H2c0Xy48K | ||
630 | 7 | IkbS/hntyqlJYK1yfTbTHkDiweToE3Lm+55Do1TX04AyvBrwA1O/jNGi4xIlUEAy | ||
631 | 8 | 1Bs8MrJ1E/j/XDn9/QJBAOuhPTgG3F7bKuBrQzv98CvC5o2Txf3vLY8nL8V24b3l | ||
632 | 9 | XgqzkDLhUxReBmmkGxZfKAju3+gXFvGGpbP7V8zShg8CQQDkQGs7kArFq/KR/GCh | ||
633 | 10 | CAmJaDWy4LJkSqzDHoJbTrS7YuqN6X6mW1xPRnWpYSxae38fJsCpG3Vq8Mv1Zl32 | ||
634 | 11 | VPZXAkEAsAeE9JYri7GwFngLgoXzJr4z/xCmmU5VetyLk7l8a6Eu4E/FKj2rE0wq | ||
635 | 12 | /kDa+5ubDRFntLuLKGSu5gafUST1gQJABhdmBTfp4a6eEaFPntyNDJq4XCa8/Ao2 | ||
636 | 13 | JBrrVa57Ckkwg0sI8z2a8A6sUzHhsiR7lwQ8vgaakpkMiGcL+Of5jwJAA/qX3PW+ | ||
637 | 14 | 9JXbjWxpgh7FHnZJNRZ8xSe47REGA7qS/nIlV9iRuf/9M+k3A5VqitfFxrjPwSyI | ||
638 | 15 | rvKTYkk13dL4hg== | ||
639 | 16 | -----END PRIVATE KEY----- | ||
640 | 17 | 0 | ||
641 | === removed file 'ubuntuone/proxy/tests/test_tunnel_client.py' | |||
642 | --- ubuntuone/proxy/tests/test_tunnel_client.py 2016-06-01 18:28:19 +0000 | |||
643 | +++ ubuntuone/proxy/tests/test_tunnel_client.py 1970-01-01 00:00:00 +0000 | |||
644 | @@ -1,225 +0,0 @@ | |||
645 | 1 | # -*- coding: utf-8 -*- | ||
646 | 2 | # | ||
647 | 3 | # Copyright 2012 Canonical Ltd. | ||
648 | 4 | # | ||
649 | 5 | # This program is free software: you can redistribute it and/or modify it | ||
650 | 6 | # under the terms of the GNU General Public License version 3, as published | ||
651 | 7 | # by the Free Software Foundation. | ||
652 | 8 | # | ||
653 | 9 | # This program is distributed in the hope that it will be useful, but | ||
654 | 10 | # WITHOUT ANY WARRANTY; without even the implied warranties of | ||
655 | 11 | # MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR | ||
656 | 12 | # PURPOSE. See the GNU General Public License for more details. | ||
657 | 13 | # | ||
658 | 14 | # You should have received a copy of the GNU General Public License along | ||
659 | 15 | # with this program. If not, see <http://www.gnu.org/licenses/>. | ||
660 | 16 | # | ||
661 | 17 | # In addition, as a special exception, the copyright holders give | ||
662 | 18 | # permission to link the code of portions of this program with the | ||
663 | 19 | # OpenSSL library under certain conditions as described in each | ||
664 | 20 | # individual source file, and distribute linked combinations | ||
665 | 21 | # including the two. | ||
666 | 22 | # You must obey the GNU General Public License in all respects | ||
667 | 23 | # for all of the code used other than OpenSSL. If you modify | ||
668 | 24 | # file(s) with this exception, you may extend this exception to your | ||
669 | 25 | # version of the file(s), but you are not obligated to do so. If you | ||
670 | 26 | # do not wish to do so, delete this exception statement from your | ||
671 | 27 | # version. If you delete this exception statement from all source | ||
672 | 28 | # files in the program, then also delete it here. | ||
673 | 29 | """Tests for the proxy tunnel.""" | ||
674 | 30 | |||
675 | 31 | from twisted.internet import defer, protocol, ssl | ||
676 | 32 | from twisted.trial.unittest import TestCase | ||
677 | 33 | from twisted.web import client | ||
678 | 34 | |||
679 | 35 | from ubuntuone.devtools.testcases.squid import SquidTestCase | ||
680 | 36 | |||
681 | 37 | from ubuntuone.proxy.tests import ( | ||
682 | 38 | FakeTransport, | ||
683 | 39 | FAKE_COOKIE, | ||
684 | 40 | MockWebServer, | ||
685 | 41 | SAMPLE_CONTENT, | ||
686 | 42 | SIMPLERESOURCE, | ||
687 | 43 | ) | ||
688 | 44 | from ubuntuone.proxy import tunnel_client | ||
689 | 45 | from ubuntuone.proxy.tunnel_client import CRLF, TunnelClient | ||
690 | 46 | from ubuntuone.proxy.tunnel_server import TunnelServer | ||
691 | 47 | |||
692 | 48 | |||
693 | 49 | FAKE_HEADER = ( | ||
694 | 50 | "HTTP/1.0 200 Connected!" + CRLF + | ||
695 | 51 | "Header1: value1" + CRLF + | ||
696 | 52 | "Header2: value2" + CRLF + | ||
697 | 53 | CRLF | ||
698 | 54 | ) | ||
699 | 55 | |||
700 | 56 | |||
701 | 57 | class SavingProtocol(protocol.Protocol): | ||
702 | 58 | """A protocol that saves all that it receives.""" | ||
703 | 59 | |||
704 | 60 | def __init__(self): | ||
705 | 61 | """Initialize this protocol.""" | ||
706 | 62 | self.saved_data = None | ||
707 | 63 | |||
708 | 64 | def connectionMade(self): | ||
709 | 65 | """The connection was made, start saving.""" | ||
710 | 66 | self.saved_data = [] | ||
711 | 67 | |||
712 | 68 | def dataReceived(self, data): | ||
713 | 69 | """Save the data received.""" | ||
714 | 70 | self.saved_data.append(data) | ||
715 | 71 | |||
716 | 72 | @property | ||
717 | 73 | def content(self): | ||
718 | 74 | """All the content so far.""" | ||
719 | 75 | return "".join(self.saved_data) | ||
720 | 76 | |||
721 | 77 | |||
722 | 78 | class TunnelClientProtocolTestCase(TestCase): | ||
723 | 79 | """Tests for the client side tunnel protocol.""" | ||
724 | 80 | |||
725 | 81 | timeout = 3 | ||
726 | 82 | |||
727 | 83 | @defer.inlineCallbacks | ||
728 | 84 | def setUp(self): | ||
729 | 85 | """Initialize this testcase.""" | ||
730 | 86 | yield super(TunnelClientProtocolTestCase, self).setUp() | ||
731 | 87 | self.host, self.port = "9.9.9.9", 8765 | ||
732 | 88 | fake_addr = object() | ||
733 | 89 | self.cookie = FAKE_COOKIE | ||
734 | 90 | self.other_proto = SavingProtocol() | ||
735 | 91 | other_factory = protocol.ClientFactory() | ||
736 | 92 | other_factory.buildProtocol = lambda _addr: self.other_proto | ||
737 | 93 | tunnel_client_factory = tunnel_client.TunnelClientFactory( | ||
738 | 94 | self.host, self.port, other_factory, self.cookie) | ||
739 | 95 | tunnel_client_proto = tunnel_client_factory.buildProtocol(fake_addr) | ||
740 | 96 | tunnel_client_proto.transport = FakeTransport() | ||
741 | 97 | tunnel_client_proto.connectionMade() | ||
742 | 98 | self.tunnel_client_proto = tunnel_client_proto | ||
743 | 99 | |||
744 | 100 | def test_sends_connect_request(self): | ||
745 | 101 | """Sends the expected CONNECT request.""" | ||
746 | 102 | expected = tunnel_client.METHOD_LINE % (self.host, self.port) | ||
747 | 103 | written = self.tunnel_client_proto.transport.getvalue() | ||
748 | 104 | first_line = written.split(CRLF)[0] | ||
749 | 105 | self.assertEqual(first_line + CRLF, expected) | ||
750 | 106 | self.assertTrue(written.endswith(CRLF * 2), | ||
751 | 107 | "Ends with a double CRLF") | ||
752 | 108 | |||
753 | 109 | def test_sends_cookie_header(self): | ||
754 | 110 | """Sends the expected cookie header.""" | ||
755 | 111 | expected = "%s: %s" % (tunnel_client.TUNNEL_COOKIE_HEADER, self.cookie) | ||
756 | 112 | written = self.tunnel_client_proto.transport.getvalue() | ||
757 | 113 | headers = written.split(CRLF)[1:] | ||
758 | 114 | self.assertIn(expected, headers) | ||
759 | 115 | |||
760 | 116 | def test_handles_successful_connection(self): | ||
761 | 117 | """A successful connection is handled.""" | ||
762 | 118 | self.tunnel_client_proto.dataReceived(FAKE_HEADER) | ||
763 | 119 | self.assertEqual(self.tunnel_client_proto.status_code, "200") | ||
764 | 120 | |||
765 | 121 | def test_protocol_is_switched(self): | ||
766 | 122 | """The protocol is switched after the headers are received.""" | ||
767 | 123 | expected = (SAMPLE_CONTENT + CRLF) * 2 | ||
768 | 124 | self.tunnel_client_proto.dataReceived(FAKE_HEADER + SAMPLE_CONTENT) | ||
769 | 125 | self.other_proto.dataReceived(CRLF + SAMPLE_CONTENT + CRLF) | ||
770 | 126 | self.assertEqual(self.other_proto.content, expected) | ||
771 | 127 | |||
772 | 128 | |||
773 | 129 | class FakeOtherFactory(object): | ||
774 | 130 | """A fake factory.""" | ||
775 | 131 | |||
776 | 132 | def __init__(self): | ||
777 | 133 | """Initialize this fake.""" | ||
778 | 134 | self.started_called = None | ||
779 | 135 | self.failed_called = None | ||
780 | 136 | self.lost_called = None | ||
781 | 137 | |||
782 | 138 | def startedConnecting(self, *args): | ||
783 | 139 | """Store the call.""" | ||
784 | 140 | self.started_called = args | ||
785 | 141 | |||
786 | 142 | def clientConnectionFailed(self, *args): | ||
787 | 143 | """Store the call.""" | ||
788 | 144 | self.failed_called = args | ||
789 | 145 | |||
790 | 146 | def clientConnectionLost(self, *args): | ||
791 | 147 | """Store the call.""" | ||
792 | 148 | self.lost_called = args | ||
793 | 149 | |||
794 | 150 | |||
795 | 151 | class TunnelClientFactoryTestCase(TestCase): | ||
796 | 152 | """Tests for the TunnelClientFactory.""" | ||
797 | 153 | |||
798 | 154 | def test_forwards_started(self): | ||
799 | 155 | """The factory forwards the startedConnecting call.""" | ||
800 | 156 | fake_other_factory = FakeOtherFactory() | ||
801 | 157 | tcf = tunnel_client.TunnelClientFactory(None, None, fake_other_factory, | ||
802 | 158 | FAKE_COOKIE) | ||
803 | 159 | fake_connector = object() | ||
804 | 160 | tcf.startedConnecting(fake_connector) | ||
805 | 161 | self.assertEqual(fake_other_factory.started_called, (fake_connector,)) | ||
806 | 162 | |||
807 | 163 | def test_forwards_failed(self): | ||
808 | 164 | """The factory forwards the clientConnectionFailed call.""" | ||
809 | 165 | fake_reason = object() | ||
810 | 166 | fake_other_factory = FakeOtherFactory() | ||
811 | 167 | tcf = tunnel_client.TunnelClientFactory(None, None, fake_other_factory, | ||
812 | 168 | FAKE_COOKIE) | ||
813 | 169 | fake_connector = object() | ||
814 | 170 | tcf.clientConnectionFailed(fake_connector, fake_reason) | ||
815 | 171 | self.assertEqual(fake_other_factory.failed_called, | ||
816 | 172 | (fake_connector, fake_reason)) | ||
817 | 173 | |||
818 | 174 | def test_forwards_lost(self): | ||
819 | 175 | """The factory forwards the clientConnectionLost call.""" | ||
820 | 176 | fake_reason = object() | ||
821 | 177 | fake_other_factory = FakeOtherFactory() | ||
822 | 178 | tcf = tunnel_client.TunnelClientFactory(None, None, fake_other_factory, | ||
823 | 179 | FAKE_COOKIE) | ||
824 | 180 | fake_connector = object() | ||
825 | 181 | tcf.clientConnectionLost(fake_connector, fake_reason) | ||
826 | 182 | self.assertEqual(fake_other_factory.lost_called, | ||
827 | 183 | (fake_connector, fake_reason)) | ||
828 | 184 | |||
829 | 185 | |||
830 | 186 | class TunnelClientTestCase(SquidTestCase): | ||
831 | 187 | """Test the client for the tunnel.""" | ||
832 | 188 | |||
833 | 189 | timeout = 3 | ||
834 | 190 | |||
835 | 191 | @defer.inlineCallbacks | ||
836 | 192 | def setUp(self): | ||
837 | 193 | """Initialize this testcase.""" | ||
838 | 194 | yield super(TunnelClientTestCase, self).setUp() | ||
839 | 195 | self.ws = MockWebServer() | ||
840 | 196 | self.addCleanup(self.ws.stop) | ||
841 | 197 | self.dest_url = self.ws.get_iri().encode("utf-8") + SIMPLERESOURCE | ||
842 | 198 | self.dest_ssl_url = ( | ||
843 | 199 | self.ws.get_ssl_iri().encode("utf-8") + SIMPLERESOURCE) | ||
844 | 200 | self.cookie = FAKE_COOKIE | ||
845 | 201 | self.tunnel_server = TunnelServer(self.cookie) | ||
846 | 202 | self.addCleanup(self.tunnel_server.shutdown) | ||
847 | 203 | |||
848 | 204 | @defer.inlineCallbacks | ||
849 | 205 | def test_connects_right(self): | ||
850 | 206 | """Uses the CONNECT method on the tunnel.""" | ||
851 | 207 | tunnel_client = TunnelClient("0.0.0.0", self.tunnel_server.port, | ||
852 | 208 | self.cookie) | ||
853 | 209 | factory = client.HTTPClientFactory(self.dest_url) | ||
854 | 210 | scheme, host, port, path = client._parse(self.dest_url) | ||
855 | 211 | tunnel_client.connectTCP(host, port, factory) | ||
856 | 212 | result = yield factory.deferred | ||
857 | 213 | self.assertEqual(result, SAMPLE_CONTENT) | ||
858 | 214 | |||
859 | 215 | @defer.inlineCallbacks | ||
860 | 216 | def test_starts_tls_connection(self): | ||
861 | 217 | """TLS is started after connecting; control passed to the client.""" | ||
862 | 218 | tunnel_client = TunnelClient( | ||
863 | 219 | "0.0.0.0", self.tunnel_server.port, self.cookie) | ||
864 | 220 | factory = client.HTTPClientFactory(self.dest_ssl_url) | ||
865 | 221 | scheme, host, port, path = client._parse(self.dest_ssl_url) | ||
866 | 222 | context_factory = ssl.ClientContextFactory() | ||
867 | 223 | tunnel_client.connectSSL(host, port, factory, context_factory) | ||
868 | 224 | result = yield factory.deferred | ||
869 | 225 | self.assertEqual(result, SAMPLE_CONTENT) | ||
870 | 226 | 0 | ||
871 | === removed file 'ubuntuone/proxy/tests/test_tunnel_server.py' | |||
872 | --- ubuntuone/proxy/tests/test_tunnel_server.py 2016-06-01 18:28:19 +0000 | |||
873 | +++ ubuntuone/proxy/tests/test_tunnel_server.py 1970-01-01 00:00:00 +0000 | |||
874 | @@ -1,743 +0,0 @@ | |||
875 | 1 | # -*- coding: utf-8 -*- | ||
876 | 2 | # | ||
877 | 3 | # Copyright 2012-2013 Canonical Ltd. | ||
878 | 4 | # | ||
879 | 5 | # This program is free software: you can redistribute it and/or modify it | ||
880 | 6 | # under the terms of the GNU General Public License version 3, as published | ||
881 | 7 | # by the Free Software Foundation. | ||
882 | 8 | # | ||
883 | 9 | # This program is distributed in the hope that it will be useful, but | ||
884 | 10 | # WITHOUT ANY WARRANTY; without even the implied warranties of | ||
885 | 11 | # MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR | ||
886 | 12 | # PURPOSE. See the GNU General Public License for more details. | ||
887 | 13 | # | ||
888 | 14 | # You should have received a copy of the GNU General Public License along | ||
889 | 15 | # with this program. If not, see <http://www.gnu.org/licenses/>. | ||
890 | 16 | # | ||
891 | 17 | # In addition, as a special exception, the copyright holders give | ||
892 | 18 | # permission to link the code of portions of this program with the | ||
893 | 19 | # OpenSSL library under certain conditions as described in each | ||
894 | 20 | # individual source file, and distribute linked combinations | ||
895 | 21 | # including the two. | ||
896 | 22 | # You must obey the GNU General Public License in all respects | ||
897 | 23 | # for all of the code used other than OpenSSL. If you modify | ||
898 | 24 | # file(s) with this exception, you may extend this exception to your | ||
899 | 25 | # version of the file(s), but you are not obligated to do so. If you | ||
900 | 26 | # do not wish to do so, delete this exception statement from your | ||
901 | 27 | # version. If you delete this exception statement from all source | ||
902 | 28 | # files in the program, then also delete it here. | ||
903 | 29 | """Tests for the proxy tunnel.""" | ||
904 | 30 | |||
905 | 31 | from StringIO import StringIO | ||
906 | 32 | from urlparse import urlparse | ||
907 | 33 | |||
908 | 34 | from twisted.internet import defer, protocol, reactor | ||
909 | 35 | from twisted.trial.unittest import TestCase | ||
910 | 36 | from PyQt4.QtCore import QCoreApplication | ||
911 | 37 | from PyQt4.QtNetwork import QAuthenticator | ||
912 | 38 | from ubuntuone.devtools.testcases import skipIfOS | ||
913 | 39 | from ubuntuone.devtools.testcases.squid import SquidTestCase | ||
914 | 40 | |||
915 | 41 | from ubuntuone.proxy.tests import ( | ||
916 | 42 | FakeTransport, | ||
917 | 43 | FAKE_COOKIE, | ||
918 | 44 | MockWebServer, | ||
919 | 45 | SAMPLE_CONTENT, | ||
920 | 46 | SIMPLERESOURCE, | ||
921 | 47 | ) | ||
922 | 48 | from ubuntuone.proxy import tunnel_server | ||
923 | 49 | from ubuntuone.proxy.tunnel_server import CRLF | ||
924 | 50 | |||
925 | 51 | |||
926 | 52 | FAKE_SESSION_TEMPLATE = ( | ||
927 | 53 | "CONNECT %s HTTP/1.0" + CRLF + | ||
928 | 54 | "Header1: value1" + CRLF + | ||
929 | 55 | "Header2: value2" + CRLF + | ||
930 | 56 | tunnel_server.TUNNEL_COOKIE_HEADER + ": %s" + CRLF + | ||
931 | 57 | CRLF + | ||
932 | 58 | "GET %s HTTP/1.0" + CRLF + CRLF | ||
933 | 59 | ) | ||
934 | 60 | |||
935 | 61 | FAKE_SETTINGS = { | ||
936 | 62 | "http": { | ||
937 | 63 | "host": "myhost", | ||
938 | 64 | "port": 8888, | ||
939 | 65 | } | ||
940 | 66 | } | ||
941 | 67 | |||
942 | 68 | FAKE_AUTH_SETTINGS = { | ||
943 | 69 | "http": { | ||
944 | 70 | "host": "myhost", | ||
945 | 71 | "port": 8888, | ||
946 | 72 | "username": "fake_user", | ||
947 | 73 | "password": "fake_password", | ||
948 | 74 | } | ||
949 | 75 | } | ||
950 | 76 | |||
951 | 77 | SAMPLE_HOST = "samplehost.com" | ||
952 | 78 | SAMPLE_PORT = 443 | ||
953 | 79 | |||
954 | 80 | FAKE_CREDS = { | ||
955 | 81 | "username": "rhea", | ||
956 | 82 | "password": "caracolcaracola", | ||
957 | 83 | } | ||
958 | 84 | |||
959 | 85 | |||
960 | 86 | class DisconnectingProtocol(protocol.Protocol): | ||
961 | 87 | """A protocol that just disconnects.""" | ||
962 | 88 | |||
963 | 89 | def connectionMade(self): | ||
964 | 90 | """Upon connecting: just disconnect.""" | ||
965 | 91 | self.transport.loseConnection() | ||
966 | 92 | |||
967 | 93 | |||
968 | 94 | class DisconnectingClientFactory(protocol.ClientFactory): | ||
969 | 95 | """A factory that fires a deferred on connection.""" | ||
970 | 96 | |||
971 | 97 | def __init__(self): | ||
972 | 98 | """Initialize this instance.""" | ||
973 | 99 | self.connected = defer.Deferred() | ||
974 | 100 | |||
975 | 101 | def buildProtocol(self, addr): | ||
976 | 102 | """The connection was made.""" | ||
977 | 103 | proto = DisconnectingProtocol() | ||
978 | 104 | if not self.connected.called: | ||
979 | 105 | self.connected.callback(proto) | ||
980 | 106 | return proto | ||
981 | 107 | |||
982 | 108 | |||
983 | 109 | class FakeProtocol(protocol.Protocol): | ||
984 | 110 | """A protocol that forwards some data.""" | ||
985 | 111 | |||
986 | 112 | def __init__(self, factory, data): | ||
987 | 113 | """Initialize this fake.""" | ||
988 | 114 | self.factory = factory | ||
989 | 115 | self.data = data | ||
990 | 116 | self.received_data = [] | ||
991 | 117 | |||
992 | 118 | def connectionMade(self): | ||
993 | 119 | """Upon connection: send the stored data.""" | ||
994 | 120 | self.transport.write(self.data) | ||
995 | 121 | |||
996 | 122 | def dataReceived(self, data): | ||
997 | 123 | """Some data was received.""" | ||
998 | 124 | self.received_data.append(data) | ||
999 | 125 | |||
1000 | 126 | def connectionLost(self, reason): | ||
1001 | 127 | """The connection was lost, return the response.""" | ||
1002 | 128 | response = "".join(self.received_data) | ||
1003 | 129 | if not self.factory.response.called: | ||
1004 | 130 | self.factory.response.callback(response) | ||
1005 | 131 | |||
1006 | 132 | |||
1007 | 133 | class FakeClientFactory(protocol.ClientFactory): | ||
1008 | 134 | """A factory that forwards some data to the protocol.""" | ||
1009 | 135 | |||
1010 | 136 | def __init__(self, data): | ||
1011 | 137 | """Initialize this fake.""" | ||
1012 | 138 | self.data = data | ||
1013 | 139 | self.response = defer.Deferred() | ||
1014 | 140 | |||
1015 | 141 | def buildProtocol(self, addr): | ||
1016 | 142 | """The connection was made.""" | ||
1017 | 143 | return FakeProtocol(self, self.data) | ||
1018 | 144 | |||
1019 | 145 | |||
1020 | 146 | class TunnelIntegrationTestCase(SquidTestCase): | ||
1021 | 147 | """Basic tunnel integration tests.""" | ||
1022 | 148 | |||
1023 | 149 | timeout = 3 | ||
1024 | 150 | |||
1025 | 151 | @defer.inlineCallbacks | ||
1026 | 152 | def setUp(self): | ||
1027 | 153 | """Initialize this testcase.""" | ||
1028 | 154 | yield super(TunnelIntegrationTestCase, self).setUp() | ||
1029 | 155 | self.ws = MockWebServer() | ||
1030 | 156 | self.addCleanup(self.ws.stop) | ||
1031 | 157 | self.dest_url = self.ws.get_iri().encode("utf-8") + SIMPLERESOURCE | ||
1032 | 158 | self.cookie = FAKE_COOKIE | ||
1033 | 159 | self.tunnel_server = tunnel_server.TunnelServer(self.cookie) | ||
1034 | 160 | self.addCleanup(self.tunnel_server.shutdown) | ||
1035 | 161 | |||
1036 | 162 | def test_init(self): | ||
1037 | 163 | """The tunnel is started.""" | ||
1038 | 164 | self.assertNotEqual(self.tunnel_server.port, 0) | ||
1039 | 165 | |||
1040 | 166 | @defer.inlineCallbacks | ||
1041 | 167 | def test_accepts_connections(self): | ||
1042 | 168 | """The tunnel accepts incoming connections.""" | ||
1043 | 169 | ncf = DisconnectingClientFactory() | ||
1044 | 170 | reactor.connectTCP("0.0.0.0", self.tunnel_server.port, ncf) | ||
1045 | 171 | yield ncf.connected | ||
1046 | 172 | |||
1047 | 173 | @defer.inlineCallbacks | ||
1048 | 174 | def test_complete_connection(self): | ||
1049 | 175 | """Test from the tunnel server down.""" | ||
1050 | 176 | url = urlparse(self.dest_url) | ||
1051 | 177 | fake_session = FAKE_SESSION_TEMPLATE % ( | ||
1052 | 178 | url.netloc, self.cookie, url.path) | ||
1053 | 179 | client = FakeClientFactory(fake_session) | ||
1054 | 180 | reactor.connectTCP("0.0.0.0", self.tunnel_server.port, client) | ||
1055 | 181 | response = yield client.response | ||
1056 | 182 | self.assertIn(SAMPLE_CONTENT, response) | ||
1057 | 183 | |||
1058 | 184 | |||
1059 | 185 | class FakeClient(object): | ||
1060 | 186 | """A fake destination client.""" | ||
1061 | 187 | |||
1062 | 188 | protocol = None | ||
1063 | 189 | connection_result = defer.succeed(True) | ||
1064 | 190 | credentials = None | ||
1065 | 191 | check_credentials = False | ||
1066 | 192 | proxy_domain = None | ||
1067 | 193 | |||
1068 | 194 | def connect(self, hostport): | ||
1069 | 195 | """Establish a connection with the other end.""" | ||
1070 | 196 | if (self.check_credentials and | ||
1071 | 197 | self.protocol.proxy_credentials != FAKE_CREDS): | ||
1072 | 198 | self.proxy_domain = "fake domain" | ||
1073 | 199 | return defer.fail(tunnel_server.ProxyAuthenticationError()) | ||
1074 | 200 | return self.connection_result | ||
1075 | 201 | |||
1076 | 202 | def write(self, data): | ||
1077 | 203 | """Write some data to the other end.""" | ||
1078 | 204 | if data == 'GET /simpleresource HTTP/1.0\r\n\r\n': | ||
1079 | 205 | self.protocol.transport.write(SAMPLE_CONTENT) | ||
1080 | 206 | |||
1081 | 207 | def stop(self): | ||
1082 | 208 | """Stop this fake client.""" | ||
1083 | 209 | |||
1084 | 210 | def close(self): | ||
1085 | 211 | """Reset this client.""" | ||
1086 | 212 | |||
1087 | 213 | |||
1088 | 214 | class ServerTunnelProtocolTestCase(SquidTestCase): | ||
1089 | 215 | """Tests for the ServerTunnelProtocol.""" | ||
1090 | 216 | |||
1091 | 217 | @defer.inlineCallbacks | ||
1092 | 218 | def setUp(self): | ||
1093 | 219 | """Initialize this test instance.""" | ||
1094 | 220 | yield super(ServerTunnelProtocolTestCase, self).setUp() | ||
1095 | 221 | self.ws = MockWebServer() | ||
1096 | 222 | self.addCleanup(self.ws.stop) | ||
1097 | 223 | self.dest_url = self.ws.get_iri().encode("utf-8") + SIMPLERESOURCE | ||
1098 | 224 | self.transport = FakeTransport() | ||
1099 | 225 | self.transport.cookie = FAKE_COOKIE | ||
1100 | 226 | self.fake_client = FakeClient() | ||
1101 | 227 | self.proto = tunnel_server.ServerTunnelProtocol( | ||
1102 | 228 | lambda _: self.fake_client) | ||
1103 | 229 | self.fake_client.protocol = self.proto | ||
1104 | 230 | self.proto.transport = self.transport | ||
1105 | 231 | self.cookie_line = "%s: %s" % (tunnel_server.TUNNEL_COOKIE_HEADER, | ||
1106 | 232 | FAKE_COOKIE) | ||
1107 | 233 | |||
1108 | 234 | def test_broken_request(self): | ||
1109 | 235 | """Broken request.""" | ||
1110 | 236 | self.proto.dataReceived("Broken request." + CRLF) | ||
1111 | 237 | self.assertTrue(self.transport.getvalue().startswith("HTTP/1.0 400 "), | ||
1112 | 238 | "A broken request must fail.") | ||
1113 | 239 | |||
1114 | 240 | def test_wrong_method(self): | ||
1115 | 241 | """Wrong method.""" | ||
1116 | 242 | self.proto.dataReceived("GET http://slashdot.org HTTP/1.0" + CRLF) | ||
1117 | 243 | self.assertTrue(self.transport.getvalue().startswith("HTTP/1.0 405 "), | ||
1118 | 244 | "Using a wrong method fails.") | ||
1119 | 245 | |||
1120 | 246 | def test_invalid_http_version(self): | ||
1121 | 247 | """Invalid HTTP version.""" | ||
1122 | 248 | self.proto.dataReceived("CONNECT 127.0.0.1:9999 HTTP/1.1" + CRLF) | ||
1123 | 249 | self.assertTrue(self.transport.getvalue().startswith("HTTP/1.0 505 "), | ||
1124 | 250 | "Invalid http version is not allowed.") | ||
1125 | 251 | |||
1126 | 252 | def test_connection_is_established(self): | ||
1127 | 253 | """The response code is sent.""" | ||
1128 | 254 | expected = "HTTP/1.0 200 Proxy connection established" + CRLF | ||
1129 | 255 | self.proto.dataReceived("CONNECT 127.0.0.1:9999 HTTP/1.0" + CRLF + | ||
1130 | 256 | self.cookie_line + CRLF * 2) | ||
1131 | 257 | self.assertTrue(self.transport.getvalue().startswith(expected), | ||
1132 | 258 | "First line must be the response status") | ||
1133 | 259 | |||
1134 | 260 | def test_connection_fails(self): | ||
1135 | 261 | """The connection to the other end fails, and it's handled.""" | ||
1136 | 262 | error = tunnel_server.ConnectionError() | ||
1137 | 263 | self.patch(self.fake_client, "connection_result", defer.fail(error)) | ||
1138 | 264 | expected = "HTTP/1.0 500 Connection error" + CRLF | ||
1139 | 265 | self.proto.dataReceived("CONNECT 127.0.0.1:9999 HTTP/1.0" + CRLF + | ||
1140 | 266 | self.cookie_line + CRLF * 2) | ||
1141 | 267 | self.assertTrue(self.transport.getvalue().startswith(expected), | ||
1142 | 268 | "The connection should fail at this point.") | ||
1143 | 269 | |||
1144 | 270 | def test_headers_stored(self): | ||
1145 | 271 | """The request headers are stored.""" | ||
1146 | 272 | expected = [ | ||
1147 | 273 | ("Header1", "value1"), | ||
1148 | 274 | ("Header2", "value2"), | ||
1149 | 275 | ] | ||
1150 | 276 | self.proto.dataReceived("CONNECT 127.0.0.1:9999 HTTP/1.0" + CRLF + | ||
1151 | 277 | "Header1: value1" + CRLF + | ||
1152 | 278 | "Header2: value2" + CRLF + CRLF) | ||
1153 | 279 | self.assertEqual(self.proto.received_headers, expected) | ||
1154 | 280 | |||
1155 | 281 | def test_cookie_header_present(self): | ||
1156 | 282 | """The cookie header must be present.""" | ||
1157 | 283 | self.proto.received_headers = [ | ||
1158 | 284 | (tunnel_server.TUNNEL_COOKIE_HEADER, FAKE_COOKIE), | ||
1159 | 285 | ] | ||
1160 | 286 | self.proto.verify_cookie() | ||
1161 | 287 | |||
1162 | 288 | def test_cookie_header_absent(self): | ||
1163 | 289 | """The tunnel should refuse connections without the cookie.""" | ||
1164 | 290 | self.proto.received_headers = [] | ||
1165 | 291 | exception = self.assertRaises(tunnel_server.ConnectionError, | ||
1166 | 292 | self.proto.verify_cookie) | ||
1167 | 293 | self.assertEqual(exception.code, 418) | ||
1168 | 294 | |||
1169 | 295 | def test_successful_connect(self): | ||
1170 | 296 | """A successful connect thru the tunnel.""" | ||
1171 | 297 | url = urlparse(self.dest_url) | ||
1172 | 298 | data = FAKE_SESSION_TEMPLATE % (url.netloc, self.transport.cookie, | ||
1173 | 299 | url.path) | ||
1174 | 300 | self.proto.dataReceived(data) | ||
1175 | 301 | lines = self.transport.getvalue().split(CRLF) | ||
1176 | 302 | self.assertEqual(lines[-1], SAMPLE_CONTENT) | ||
1177 | 303 | |||
1178 | 304 | def test_header_split(self): | ||
1179 | 305 | """Test a header with many colons.""" | ||
1180 | 306 | self.proto.header_line("key: host:port") | ||
1181 | 307 | self.assertIn("key", dict(self.proto.received_headers)) | ||
1182 | 308 | |||
1183 | 309 | @defer.inlineCallbacks | ||
1184 | 310 | def test_keyring_credentials_are_retried(self): | ||
1185 | 311 | """Wrong credentials are retried with values from keyring.""" | ||
1186 | 312 | self.fake_client.check_credentials = True | ||
1187 | 313 | self.patch(self.proto, "verify_cookie", lambda: None) | ||
1188 | 314 | self.patch(self.proto, "error_response", | ||
1189 | 315 | lambda code, desc: self.fail(desc)) | ||
1190 | 316 | self.proto.proxy_domain = "xxx" | ||
1191 | 317 | self.patch(tunnel_server.Keyring, "get_credentials", | ||
1192 | 318 | lambda _, domain: defer.succeed(FAKE_CREDS)) | ||
1193 | 319 | yield self.proto.headers_done() | ||
1194 | 320 | |||
1195 | 321 | def test_creds_are_not_logged(self): | ||
1196 | 322 | """The proxy credentials are not logged.""" | ||
1197 | 323 | log = [] | ||
1198 | 324 | self.patch(tunnel_server.logger, "info", | ||
1199 | 325 | lambda text, *args: log.append(text % args)) | ||
1200 | 326 | proxy = tunnel_server.build_proxy(FAKE_AUTH_SETTINGS) | ||
1201 | 327 | authenticator = QAuthenticator() | ||
1202 | 328 | username = FAKE_AUTH_SETTINGS["http"]["username"] | ||
1203 | 329 | password = FAKE_AUTH_SETTINGS["http"]["password"] | ||
1204 | 330 | self.proto.proxy_credentials = { | ||
1205 | 331 | "username": username, | ||
1206 | 332 | "password": password, | ||
1207 | 333 | } | ||
1208 | 334 | self.proto.proxy_domain = proxy.hostName() | ||
1209 | 335 | |||
1210 | 336 | self.proto.proxy_auth_required(proxy, authenticator) | ||
1211 | 337 | |||
1212 | 338 | for line in log: | ||
1213 | 339 | self.assertNotIn(username, line) | ||
1214 | 340 | self.assertNotIn(password, line) | ||
1215 | 341 | |||
1216 | 342 | |||
1217 | 343 | class FakeServerTunnelProtocol(object): | ||
1218 | 344 | """A fake ServerTunnelProtocol.""" | ||
1219 | 345 | |||
1220 | 346 | def __init__(self): | ||
1221 | 347 | """Initialize this fake tunnel.""" | ||
1222 | 348 | self.response_received = defer.Deferred() | ||
1223 | 349 | self.proxy_credentials = None | ||
1224 | 350 | |||
1225 | 351 | def response_data_received(self, data): | ||
1226 | 352 | """Fire the response deferred.""" | ||
1227 | 353 | if not self.response_received.called: | ||
1228 | 354 | self.response_received.callback(data) | ||
1229 | 355 | |||
1230 | 356 | def remote_disconnected(self): | ||
1231 | 357 | """The remote server disconnected.""" | ||
1232 | 358 | |||
1233 | 359 | def proxy_auth_required(self, proxy, authenticator): | ||
1234 | 360 | """Proxy credentials are needed.""" | ||
1235 | 361 | if self.proxy_credentials: | ||
1236 | 362 | authenticator.setUser(self.proxy_credentials["username"]) | ||
1237 | 363 | authenticator.setPassword(self.proxy_credentials["password"]) | ||
1238 | 364 | |||
1239 | 365 | |||
1240 | 366 | class BuildProxyTestCase(TestCase): | ||
1241 | 367 | """Tests for the build_proxy function.""" | ||
1242 | 368 | |||
1243 | 369 | def test_socks_is_preferred(self): | ||
1244 | 370 | """Socks overrides all protocols.""" | ||
1245 | 371 | settings = { | ||
1246 | 372 | "http": {"host": "httphost", "port": 3128}, | ||
1247 | 373 | "https": {"host": "httpshost", "port": 3129}, | ||
1248 | 374 | "socks": {"host": "sockshost", "port": 1080}, | ||
1249 | 375 | } | ||
1250 | 376 | proxy = tunnel_server.build_proxy(settings) | ||
1251 | 377 | self.assertEqual(proxy.type(), proxy.Socks5Proxy) | ||
1252 | 378 | self.assertEqual(proxy.hostName(), "sockshost") | ||
1253 | 379 | self.assertEqual(proxy.port(), 1080) | ||
1254 | 380 | |||
1255 | 381 | def test_https_beats_http(self): | ||
1256 | 382 | """HTTPS wins over HTTP, since all of SD traffic is https.""" | ||
1257 | 383 | settings = { | ||
1258 | 384 | "http": {"host": "httphost", "port": 3128}, | ||
1259 | 385 | "https": {"host": "httpshost", "port": 3129}, | ||
1260 | 386 | } | ||
1261 | 387 | proxy = tunnel_server.build_proxy(settings) | ||
1262 | 388 | self.assertEqual(proxy.type(), proxy.HttpProxy) | ||
1263 | 389 | self.assertEqual(proxy.hostName(), "httpshost") | ||
1264 | 390 | self.assertEqual(proxy.port(), 3129) | ||
1265 | 391 | |||
1266 | 392 | def test_http_if_no_other_choice(self): | ||
1267 | 393 | """Finally, we use the host configured for HTTP.""" | ||
1268 | 394 | settings = { | ||
1269 | 395 | "http": {"host": "httphost", "port": 3128}, | ||
1270 | 396 | } | ||
1271 | 397 | proxy = tunnel_server.build_proxy(settings) | ||
1272 | 398 | self.assertEqual(proxy.type(), proxy.HttpProxy) | ||
1273 | 399 | self.assertEqual(proxy.hostName(), "httphost") | ||
1274 | 400 | self.assertEqual(proxy.port(), 3128) | ||
1275 | 401 | |||
1276 | 402 | def test_use_noproxy_as_fallback(self): | ||
1277 | 403 | """If nothing useful, revert to no proxy.""" | ||
1278 | 404 | settings = {} | ||
1279 | 405 | proxy = tunnel_server.build_proxy(settings) | ||
1280 | 406 | self.assertEqual(proxy.type(), proxy.DefaultProxy) | ||
1281 | 407 | |||
1282 | 408 | |||
1283 | 409 | class RemoteSocketTestCase(SquidTestCase): | ||
1284 | 410 | """Tests for the client that connects to the other side.""" | ||
1285 | 411 | |||
1286 | 412 | timeout = 3 | ||
1287 | 413 | |||
1288 | 414 | def get_proxy_settings(self): | ||
1289 | 415 | return {} | ||
1290 | 416 | |||
1291 | 417 | @defer.inlineCallbacks | ||
1292 | 418 | def setUp(self): | ||
1293 | 419 | """Initialize this testcase.""" | ||
1294 | 420 | yield super(RemoteSocketTestCase, self).setUp() | ||
1295 | 421 | self.ws = MockWebServer() | ||
1296 | 422 | self.addCleanup(self.ws.stop) | ||
1297 | 423 | self.dest_url = self.ws.get_iri().encode("utf-8") + SIMPLERESOURCE | ||
1298 | 424 | |||
1299 | 425 | self.addCleanup(tunnel_server.QNetworkProxy.setApplicationProxy, | ||
1300 | 426 | tunnel_server.QNetworkProxy.applicationProxy()) | ||
1301 | 427 | settings = {"http": self.get_proxy_settings()} | ||
1302 | 428 | proxy = tunnel_server.build_proxy(settings) | ||
1303 | 429 | tunnel_server.QNetworkProxy.setApplicationProxy(proxy) | ||
1304 | 430 | |||
1305 | 431 | def test_invalid_port(self): | ||
1306 | 432 | """A request with an invalid port fails with a 400.""" | ||
1307 | 433 | protocol = tunnel_server.ServerTunnelProtocol( | ||
1308 | 434 | tunnel_server.RemoteSocket) | ||
1309 | 435 | protocol.transport = FakeTransport() | ||
1310 | 436 | protocol.dataReceived("CONNECT 127.0.0.1:wrong_port HTTP/1.0" + | ||
1311 | 437 | CRLF * 2) | ||
1312 | 438 | |||
1313 | 439 | status_line = protocol.transport.getvalue() | ||
1314 | 440 | self.assertTrue(status_line.startswith("HTTP/1.0 400 "), | ||
1315 | 441 | "The port must be an integer.") | ||
1316 | 442 | |||
1317 | 443 | @defer.inlineCallbacks | ||
1318 | 444 | def test_connection_is_finished_when_stopping(self): | ||
1319 | 445 | """The client disconnects when requested.""" | ||
1320 | 446 | fake_protocol = FakeServerTunnelProtocol() | ||
1321 | 447 | client = tunnel_server.RemoteSocket(fake_protocol) | ||
1322 | 448 | url = urlparse(self.dest_url) | ||
1323 | 449 | yield client.connect(url.netloc) | ||
1324 | 450 | yield client.stop() | ||
1325 | 451 | |||
1326 | 452 | @defer.inlineCallbacks | ||
1327 | 453 | def test_stop_but_never_connected(self): | ||
1328 | 454 | """Stop but it was never connected.""" | ||
1329 | 455 | fake_protocol = FakeServerTunnelProtocol() | ||
1330 | 456 | client = tunnel_server.RemoteSocket(fake_protocol) | ||
1331 | 457 | yield client.stop() | ||
1332 | 458 | |||
1333 | 459 | @defer.inlineCallbacks | ||
1334 | 460 | def test_client_write(self): | ||
1335 | 461 | """Data written to the client is sent to the other side.""" | ||
1336 | 462 | fake_protocol = FakeServerTunnelProtocol() | ||
1337 | 463 | client = tunnel_server.RemoteSocket(fake_protocol) | ||
1338 | 464 | self.addCleanup(client.stop) | ||
1339 | 465 | url = urlparse(self.dest_url) | ||
1340 | 466 | yield client.connect(url.netloc) | ||
1341 | 467 | client.write("GET /simpleresource HTTP/1.0" + CRLF * 2) | ||
1342 | 468 | yield self.ws.simple_resource.rendered | ||
1343 | 469 | |||
1344 | 470 | @defer.inlineCallbacks | ||
1345 | 471 | def test_client_read(self): | ||
1346 | 472 | """Data received by the client is written into the transport.""" | ||
1347 | 473 | fake_protocol = FakeServerTunnelProtocol() | ||
1348 | 474 | client = tunnel_server.RemoteSocket(fake_protocol) | ||
1349 | 475 | self.addCleanup(client.stop) | ||
1350 | 476 | url = urlparse(self.dest_url) | ||
1351 | 477 | yield client.connect(url.netloc) | ||
1352 | 478 | client.write("GET /simpleresource HTTP/1.0" + CRLF * 2) | ||
1353 | 479 | yield self.ws.simple_resource.rendered | ||
1354 | 480 | data = yield fake_protocol.response_received | ||
1355 | 481 | _headers, content = str(data).split(CRLF * 2, 1) | ||
1356 | 482 | self.assertEqual(content, SAMPLE_CONTENT) | ||
1357 | 483 | |||
1358 | 484 | |||
1359 | 485 | class AnonProxyRemoteSocketTestCase(RemoteSocketTestCase): | ||
1360 | 486 | """Tests for the client going thru an anonymous proxy.""" | ||
1361 | 487 | |||
1362 | 488 | get_proxy_settings = RemoteSocketTestCase.get_nonauth_proxy_settings | ||
1363 | 489 | |||
1364 | 490 | def parse_headers(self, raw_headers): | ||
1365 | 491 | """Parse the headers.""" | ||
1366 | 492 | lines = raw_headers.split(CRLF) | ||
1367 | 493 | header_lines = lines[1:] | ||
1368 | 494 | headers_pairs = (l.split(":", 1) for l in header_lines) | ||
1369 | 495 | return dict((k.lower(), v.strip()) for k, v in headers_pairs) | ||
1370 | 496 | |||
1371 | 497 | @defer.inlineCallbacks | ||
1372 | 498 | def test_verify_client_uses_proxy(self): | ||
1373 | 499 | """Verify that the client uses the proxy.""" | ||
1374 | 500 | fake_protocol = FakeServerTunnelProtocol() | ||
1375 | 501 | client = tunnel_server.RemoteSocket(fake_protocol) | ||
1376 | 502 | self.addCleanup(client.stop) | ||
1377 | 503 | url = urlparse(self.dest_url) | ||
1378 | 504 | yield client.connect(url.netloc) | ||
1379 | 505 | client.write("GET /simpleresource HTTP/1.0" + CRLF * 2) | ||
1380 | 506 | yield self.ws.simple_resource.rendered | ||
1381 | 507 | data = yield fake_protocol.response_received | ||
1382 | 508 | raw_headers, _content = str(data).split(CRLF * 2, 1) | ||
1383 | 509 | self.parse_headers(raw_headers) | ||
1384 | 510 | |||
1385 | 511 | |||
1386 | 512 | @skipIfOS('linux2', 'LP: #1111880 - ncsa_auth crashing for auth proxy tests.') | ||
1387 | 513 | class AuthenticatedProxyRemoteSocketTestCase(AnonProxyRemoteSocketTestCase): | ||
1388 | 514 | """Tests for the client going thru an authenticated proxy.""" | ||
1389 | 515 | |||
1390 | 516 | get_proxy_settings = RemoteSocketTestCase.get_auth_proxy_settings | ||
1391 | 517 | |||
1392 | 518 | @defer.inlineCallbacks | ||
1393 | 519 | def test_proxy_authentication_error(self): | ||
1394 | 520 | """The proxy credentials were wrong on purpose.""" | ||
1395 | 521 | settings = {"http": self.get_proxy_settings()} | ||
1396 | 522 | settings["http"]["password"] = "wrong password!!!" | ||
1397 | 523 | proxy = tunnel_server.build_proxy(settings) | ||
1398 | 524 | tunnel_server.QNetworkProxy.setApplicationProxy(proxy) | ||
1399 | 525 | fake_protocol = FakeServerTunnelProtocol() | ||
1400 | 526 | client = tunnel_server.RemoteSocket(fake_protocol) | ||
1401 | 527 | self.addCleanup(client.stop) | ||
1402 | 528 | url = urlparse(self.dest_url) | ||
1403 | 529 | yield self.assertFailure(client.connect(url.netloc), | ||
1404 | 530 | tunnel_server.ProxyAuthenticationError) | ||
1405 | 531 | |||
1406 | 532 | @defer.inlineCallbacks | ||
1407 | 533 | def test_proxy_nobody_listens(self): | ||
1408 | 534 | """The proxy settings point to a proxy that's unreachable.""" | ||
1409 | 535 | settings = dict(http={ | ||
1410 | 536 | "host": "127.0.0.1", | ||
1411 | 537 | "port": 83, # unused port according to /etc/services | ||
1412 | 538 | }) | ||
1413 | 539 | proxy = tunnel_server.build_proxy(settings) | ||
1414 | 540 | tunnel_server.QNetworkProxy.setApplicationProxy(proxy) | ||
1415 | 541 | fake_protocol = FakeServerTunnelProtocol() | ||
1416 | 542 | client = tunnel_server.RemoteSocket(fake_protocol) | ||
1417 | 543 | self.addCleanup(client.stop) | ||
1418 | 544 | url = urlparse(self.dest_url) | ||
1419 | 545 | yield self.assertFailure(client.connect(url.netloc), | ||
1420 | 546 | tunnel_server.ConnectionError) | ||
1421 | 547 | |||
1422 | 548 | def test_use_credentials(self): | ||
1423 | 549 | """The credentials are used if present.""" | ||
1424 | 550 | fake_protocol = FakeServerTunnelProtocol() | ||
1425 | 551 | client = tunnel_server.RemoteSocket(fake_protocol) | ||
1426 | 552 | proxy = tunnel_server.build_proxy(FAKE_SETTINGS) | ||
1427 | 553 | authenticator = QAuthenticator() | ||
1428 | 554 | |||
1429 | 555 | client.proxyAuthenticationRequired.emit(proxy, authenticator) | ||
1430 | 556 | self.assertEqual(proxy.user(), "") | ||
1431 | 557 | self.assertEqual(proxy.password(), "") | ||
1432 | 558 | fake_protocol.proxy_credentials = FAKE_CREDS | ||
1433 | 559 | |||
1434 | 560 | client.proxyAuthenticationRequired.emit(proxy, authenticator) | ||
1435 | 561 | self.assertEqual(authenticator.user(), FAKE_CREDS["username"]) | ||
1436 | 562 | self.assertEqual(authenticator.password(), FAKE_CREDS["password"]) | ||
1437 | 563 | |||
1438 | 564 | |||
1439 | 565 | class FakeNetworkProxyFactoryClass(object): | ||
1440 | 566 | """A fake QNetworkProxyFactory.""" | ||
1441 | 567 | last_query = None | ||
1442 | 568 | use_system = False | ||
1443 | 569 | |||
1444 | 570 | def __init__(self, enabled): | ||
1445 | 571 | """Initialize this fake instance.""" | ||
1446 | 572 | if enabled: | ||
1447 | 573 | self.proxy_type = tunnel_server.QNetworkProxy.HttpProxy | ||
1448 | 574 | else: | ||
1449 | 575 | self.proxy_type = tunnel_server.QNetworkProxy.NoProxy | ||
1450 | 576 | |||
1451 | 577 | def type(self): | ||
1452 | 578 | """Return the proxy type configured.""" | ||
1453 | 579 | return self.proxy_type | ||
1454 | 580 | |||
1455 | 581 | @classmethod | ||
1456 | 582 | def setUseSystemConfiguration(cls, new_value): | ||
1457 | 583 | """Save the system configuration requested.""" | ||
1458 | 584 | cls.use_system = new_value | ||
1459 | 585 | |||
1460 | 586 | @classmethod | ||
1461 | 587 | def useSystemConfiguration(cls): | ||
1462 | 588 | """Is the system configured for proxies?""" | ||
1463 | 589 | return cls.use_system | ||
1464 | 590 | |||
1465 | 591 | def systemProxyForQuery(self, query): | ||
1466 | 592 | """A list of proxies, but only type() will be called on the first.""" | ||
1467 | 593 | return [self] | ||
1468 | 594 | |||
1469 | 595 | |||
1470 | 596 | class CheckProxyEnabledTestCase(TestCase): | ||
1471 | 597 | """Tests for the check_proxy_enabled function.""" | ||
1472 | 598 | |||
1473 | 599 | @defer.inlineCallbacks | ||
1474 | 600 | def setUp(self): | ||
1475 | 601 | """Initialize this testcase.""" | ||
1476 | 602 | yield super(CheckProxyEnabledTestCase, self).setUp() | ||
1477 | 603 | self.app_proxy = [] | ||
1478 | 604 | |||
1479 | 605 | def _assert_proxy_state(self, platform, state, assertion): | ||
1480 | 606 | """Assert the proxy is in a given state.""" | ||
1481 | 607 | self.patch(tunnel_server.QNetworkProxy, "setApplicationProxy", | ||
1482 | 608 | lambda proxy: self.app_proxy.append(proxy)) | ||
1483 | 609 | self.patch(tunnel_server.sys, "platform", platform) | ||
1484 | 610 | ret = tunnel_server.check_proxy_enabled(SAMPLE_HOST, str(SAMPLE_PORT)) | ||
1485 | 611 | self.assertTrue(ret == state, assertion) | ||
1486 | 612 | |||
1487 | 613 | def _assert_proxy_enabled(self, platform): | ||
1488 | 614 | """Assert that the proxy is enabled.""" | ||
1489 | 615 | self._assert_proxy_state(platform, True, "Proxy is enabled.") | ||
1490 | 616 | |||
1491 | 617 | def _assert_proxy_disabled(self, platform): | ||
1492 | 618 | """Assert that the proxy is disabled.""" | ||
1493 | 619 | self._assert_proxy_state(platform, False, "Proxy is disabled.") | ||
1494 | 620 | |||
1495 | 621 | def test_platform_linux_enabled(self): | ||
1496 | 622 | """Tests for the linux platform with proxies enabled.""" | ||
1497 | 623 | self.patch(tunnel_server.gsettings, "get_proxy_settings", | ||
1498 | 624 | lambda: FAKE_SETTINGS) | ||
1499 | 625 | self._assert_proxy_enabled("linux3") | ||
1500 | 626 | self.assertEqual(len(self.app_proxy), 1) | ||
1501 | 627 | |||
1502 | 628 | def test_platform_linux_disabled(self): | ||
1503 | 629 | """Tests for the linux platform with proxies disabled.""" | ||
1504 | 630 | self.patch(tunnel_server.gsettings, "get_proxy_settings", lambda: {}) | ||
1505 | 631 | self._assert_proxy_disabled("linux3") | ||
1506 | 632 | self.assertEqual(len(self.app_proxy), 0) | ||
1507 | 633 | |||
1508 | 634 | def test_platform_other_enabled(self): | ||
1509 | 635 | """Tests for any other platform with proxies enabled.""" | ||
1510 | 636 | fake_netproxfact = FakeNetworkProxyFactoryClass(True) | ||
1511 | 637 | self.patch(tunnel_server, "QNetworkProxyFactory", fake_netproxfact) | ||
1512 | 638 | self._assert_proxy_enabled("windows 1.0") | ||
1513 | 639 | self.assertEqual(len(self.app_proxy), 0) | ||
1514 | 640 | self.assertTrue(fake_netproxfact.useSystemConfiguration()) | ||
1515 | 641 | |||
1516 | 642 | def test_platform_other_disabled(self): | ||
1517 | 643 | """Tests for any other platform with proxies disabled.""" | ||
1518 | 644 | fake_netproxfact = FakeNetworkProxyFactoryClass(False) | ||
1519 | 645 | self.patch(tunnel_server, "QNetworkProxyFactory", fake_netproxfact) | ||
1520 | 646 | self._assert_proxy_disabled("windows 1.0") | ||
1521 | 647 | self.assertEqual(len(self.app_proxy), 0) | ||
1522 | 648 | self.assertTrue(fake_netproxfact.useSystemConfiguration()) | ||
1523 | 649 | |||
1524 | 650 | |||
1525 | 651 | class FakeQCoreApp(object): | ||
1526 | 652 | """A fake QCoreApplication.""" | ||
1527 | 653 | |||
1528 | 654 | fake_instance = None | ||
1529 | 655 | |||
1530 | 656 | def __init__(self, argv): | ||
1531 | 657 | """Initialize this fake.""" | ||
1532 | 658 | self.executed = False | ||
1533 | 659 | self.argv = argv | ||
1534 | 660 | FakeQCoreApp.fake_instance = self | ||
1535 | 661 | |||
1536 | 662 | def exec_(self): | ||
1537 | 663 | """Fake the execution of this app.""" | ||
1538 | 664 | self.executed = True | ||
1539 | 665 | |||
1540 | 666 | @staticmethod | ||
1541 | 667 | def instance(): | ||
1542 | 668 | """But return the real instance.""" | ||
1543 | 669 | return QCoreApplication.instance() | ||
1544 | 670 | |||
1545 | 671 | |||
1546 | 672 | class MainFunctionTestCase(TestCase): | ||
1547 | 673 | """Tests for the main function of the tunnel server.""" | ||
1548 | 674 | |||
1549 | 675 | @defer.inlineCallbacks | ||
1550 | 676 | def setUp(self): | ||
1551 | 677 | """Initialize these testcases.""" | ||
1552 | 678 | yield super(MainFunctionTestCase, self).setUp() | ||
1553 | 679 | self.called = [] | ||
1554 | 680 | self.proxies_enabled = False | ||
1555 | 681 | |||
1556 | 682 | def fake_is_proxy_enabled(*args): | ||
1557 | 683 | """Store the call, return false.""" | ||
1558 | 684 | self.called.append(args) | ||
1559 | 685 | return self.proxies_enabled | ||
1560 | 686 | |||
1561 | 687 | self.patch(tunnel_server, "check_proxy_enabled", fake_is_proxy_enabled) | ||
1562 | 688 | self.fake_stdout = StringIO() | ||
1563 | 689 | self.patch(tunnel_server.sys, "stdout", self.fake_stdout) | ||
1564 | 690 | self.patch(tunnel_server, "QCoreApplication", FakeQCoreApp) | ||
1565 | 691 | |||
1566 | 692 | def test_checks_proxies(self): | ||
1567 | 693 | """Main checks that the proxies are enabled.""" | ||
1568 | 694 | tunnel_server.main([]) | ||
1569 | 695 | self.assertEqual(len(self.called), 1) | ||
1570 | 696 | |||
1571 | 697 | def test_on_proxies_enabled_prints_port_and_cookie(self): | ||
1572 | 698 | """With proxies enabled print port to stdout and start the mainloop.""" | ||
1573 | 699 | self.patch(tunnel_server.uuid, "uuid4", lambda: FAKE_COOKIE) | ||
1574 | 700 | self.proxies_enabled = True | ||
1575 | 701 | port = 443 | ||
1576 | 702 | tunnel_server.main(["example.com", str(port)]) | ||
1577 | 703 | stdout = self.fake_stdout.getvalue() | ||
1578 | 704 | |||
1579 | 705 | self.assertIn(tunnel_server.TUNNEL_PORT_LABEL + ": ", stdout) | ||
1580 | 706 | cookie_line = tunnel_server.TUNNEL_COOKIE_LABEL + ": " + FAKE_COOKIE | ||
1581 | 707 | self.assertIn(cookie_line, stdout) | ||
1582 | 708 | |||
1583 | 709 | def test_on_proxies_disabled_exit(self): | ||
1584 | 710 | """With proxies disabled, print a message and exit gracefully.""" | ||
1585 | 711 | self.proxies_enabled = False | ||
1586 | 712 | tunnel_server.main(["example.com", "443"]) | ||
1587 | 713 | self.assertIn("Proxy not enabled.", self.fake_stdout.getvalue()) | ||
1588 | 714 | self.assertEqual(FakeQCoreApp.fake_instance, None) | ||
1589 | 715 | |||
1590 | 716 | def test_qtdbus_installed_on_linux(self): | ||
1591 | 717 | """The QtDbus mainloop is installed.""" | ||
1592 | 718 | self.patch(tunnel_server.sys, "platform", "linux123") | ||
1593 | 719 | installed = [] | ||
1594 | 720 | self.patch( | ||
1595 | 721 | tunnel_server, "install_qt_dbus", lambda: installed.append(None)) | ||
1596 | 722 | self.proxies_enabled = True | ||
1597 | 723 | tunnel_server.main(["example.com", "443"]) | ||
1598 | 724 | self.assertEqual(len(installed), 1) | ||
1599 | 725 | |||
1600 | 726 | def test_qtdbus_not_installed_on_windows(self): | ||
1601 | 727 | """The QtDbus mainloop is installed.""" | ||
1602 | 728 | self.patch(tunnel_server.sys, "platform", "win98") | ||
1603 | 729 | installed = [] | ||
1604 | 730 | self.patch( | ||
1605 | 731 | tunnel_server, "install_qt_dbus", lambda: installed.append(None)) | ||
1606 | 732 | self.proxies_enabled = True | ||
1607 | 733 | tunnel_server.main(["example.com", "443"]) | ||
1608 | 734 | self.assertEqual(len(installed), 0) | ||
1609 | 735 | |||
1610 | 736 | def test_fix_turkish_locale_called(self): | ||
1611 | 737 | """The fix_turkish_locale function is called, always.""" | ||
1612 | 738 | called = [] | ||
1613 | 739 | self.patch( | ||
1614 | 740 | tunnel_server, "fix_turkish_locale", | ||
1615 | 741 | lambda *args, **kwargs: called.append((args, kwargs))) | ||
1616 | 742 | tunnel_server.main(["localhost", "443"]) | ||
1617 | 743 | self.assertEqual(called, [((), {})]) | ||
1618 | 744 | 0 | ||
1619 | === removed file 'ubuntuone/proxy/tunnel_client.py' | |||
1620 | --- ubuntuone/proxy/tunnel_client.py 2016-05-29 00:50:05 +0000 | |||
1621 | +++ ubuntuone/proxy/tunnel_client.py 1970-01-01 00:00:00 +0000 | |||
1622 | @@ -1,202 +0,0 @@ | |||
1623 | 1 | # -*- coding: utf-8 -*- | ||
1624 | 2 | # | ||
1625 | 3 | # Copyright 2012 Canonical Ltd. | ||
1626 | 4 | # | ||
1627 | 5 | # This program is free software: you can redistribute it and/or modify it | ||
1628 | 6 | # under the terms of the GNU General Public License version 3, as published | ||
1629 | 7 | # by the Free Software Foundation. | ||
1630 | 8 | # | ||
1631 | 9 | # This program is distributed in the hope that it will be useful, but | ||
1632 | 10 | # WITHOUT ANY WARRANTY; without even the implied warranties of | ||
1633 | 11 | # MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR | ||
1634 | 12 | # PURPOSE. See the GNU General Public License for more details. | ||
1635 | 13 | # | ||
1636 | 14 | # You should have received a copy of the GNU General Public License along | ||
1637 | 15 | # with this program. If not, see <http://www.gnu.org/licenses/>. | ||
1638 | 16 | # | ||
1639 | 17 | # In addition, as a special exception, the copyright holders give | ||
1640 | 18 | # permission to link the code of portions of this program with the | ||
1641 | 19 | # OpenSSL library under certain conditions as described in each | ||
1642 | 20 | # individual source file, and distribute linked combinations | ||
1643 | 21 | # including the two. | ||
1644 | 22 | # You must obey the GNU General Public License in all respects | ||
1645 | 23 | # for all of the code used other than OpenSSL. If you modify | ||
1646 | 24 | # file(s) with this exception, you may extend this exception to your | ||
1647 | 25 | # version of the file(s), but you are not obligated to do so. If you | ||
1648 | 26 | # do not wish to do so, delete this exception statement from your | ||
1649 | 27 | # version. If you delete this exception statement from all source | ||
1650 | 28 | # files in the program, then also delete it here. | ||
1651 | 29 | """Client for the tunnel protocol.""" | ||
1652 | 30 | |||
1653 | 31 | import logging | ||
1654 | 32 | |||
1655 | 33 | from twisted.internet import protocol, reactor | ||
1656 | 34 | |||
1657 | 35 | from ubuntuone.clientdefs import NAME | ||
1658 | 36 | from ubuntuone.proxy.common import ( | ||
1659 | 37 | BaseTunnelProtocol, | ||
1660 | 38 | CRLF, | ||
1661 | 39 | TUNNEL_COOKIE_LABEL, | ||
1662 | 40 | TUNNEL_COOKIE_HEADER, | ||
1663 | 41 | TUNNEL_PORT_LABEL, | ||
1664 | 42 | ) | ||
1665 | 43 | |||
1666 | 44 | METHOD_LINE = "CONNECT %s:%d HTTP/1.0" + CRLF | ||
1667 | 45 | LOCALHOST = "127.0.0.1" | ||
1668 | 46 | |||
1669 | 47 | logger = logging.getLogger("ubuntuone.SyncDaemon.TunnelClient") | ||
1670 | 48 | |||
1671 | 49 | |||
1672 | 50 | class TunnelClientProtocol(BaseTunnelProtocol): | ||
1673 | 51 | """Client protocol for the handshake part of the tunnel.""" | ||
1674 | 52 | |||
1675 | 53 | def connectionMade(self): | ||
1676 | 54 | """The connection to the tunnel was made so send request.""" | ||
1677 | 55 | method_line = METHOD_LINE % (self.factory.tunnel_host, | ||
1678 | 56 | self.factory.tunnel_port) | ||
1679 | 57 | headers = { | ||
1680 | 58 | "User-Agent": "%s tunnel client" % NAME, | ||
1681 | 59 | TUNNEL_COOKIE_HEADER: self.factory.cookie, | ||
1682 | 60 | } | ||
1683 | 61 | self.transport.write(method_line + | ||
1684 | 62 | self.format_headers(headers) + | ||
1685 | 63 | CRLF) | ||
1686 | 64 | |||
1687 | 65 | def handle_first_line(self, line): | ||
1688 | 66 | """The first line received is the status line.""" | ||
1689 | 67 | try: | ||
1690 | 68 | proto_version, self.status_code, description = line.split(" ", 2) | ||
1691 | 69 | except ValueError: | ||
1692 | 70 | self.transport.loseConnection() | ||
1693 | 71 | |||
1694 | 72 | def headers_done(self): | ||
1695 | 73 | """All the headers have arrived. Time to switch protocols.""" | ||
1696 | 74 | remaining_data = self.clearLineBuffer() | ||
1697 | 75 | if self.status_code != "200": | ||
1698 | 76 | self.transport.loseConnection() | ||
1699 | 77 | return | ||
1700 | 78 | addr = self.transport.getPeer() | ||
1701 | 79 | other_protocol = self.factory.other_factory.buildProtocol(addr) | ||
1702 | 80 | self.transport.protocol = other_protocol | ||
1703 | 81 | other_protocol.transport = self.transport | ||
1704 | 82 | self.transport = None | ||
1705 | 83 | if self.factory.context_factory: | ||
1706 | 84 | other_protocol.transport.startTLS(self.factory.context_factory) | ||
1707 | 85 | other_protocol.connectionMade() | ||
1708 | 86 | if remaining_data: | ||
1709 | 87 | other_protocol.dataReceived(remaining_data) | ||
1710 | 88 | |||
1711 | 89 | |||
1712 | 90 | class TunnelClientFactory(protocol.ClientFactory): | ||
1713 | 91 | """A factory for Tunnel Client Protocols.""" | ||
1714 | 92 | |||
1715 | 93 | protocol = TunnelClientProtocol | ||
1716 | 94 | |||
1717 | 95 | def __init__(self, tunnel_host, tunnel_port, other_factory, cookie, | ||
1718 | 96 | context_factory=None): | ||
1719 | 97 | """Initialize this factory.""" | ||
1720 | 98 | self.tunnel_host = tunnel_host | ||
1721 | 99 | self.tunnel_port = tunnel_port | ||
1722 | 100 | self.other_factory = other_factory | ||
1723 | 101 | self.context_factory = context_factory | ||
1724 | 102 | self.cookie = cookie | ||
1725 | 103 | |||
1726 | 104 | def startedConnecting(self, connector): | ||
1727 | 105 | """Forward this call to the other factory.""" | ||
1728 | 106 | self.other_factory.startedConnecting(connector) | ||
1729 | 107 | |||
1730 | 108 | def clientConnectionFailed(self, connector, reason): | ||
1731 | 109 | """Forward this call to the other factory.""" | ||
1732 | 110 | self.other_factory.clientConnectionFailed(connector, reason) | ||
1733 | 111 | |||
1734 | 112 | def clientConnectionLost(self, connector, reason): | ||
1735 | 113 | """Forward this call to the other factory.""" | ||
1736 | 114 | self.other_factory.clientConnectionLost(connector, reason) | ||
1737 | 115 | |||
1738 | 116 | |||
1739 | 117 | class TunnelClient(object): | ||
1740 | 118 | """A client for the proxy tunnel.""" | ||
1741 | 119 | |||
1742 | 120 | def __init__(self, tunnel_host, tunnel_port, cookie): | ||
1743 | 121 | """Initialize this client.""" | ||
1744 | 122 | self.tunnel_host = tunnel_host | ||
1745 | 123 | self.tunnel_port = tunnel_port | ||
1746 | 124 | self.cookie = cookie | ||
1747 | 125 | |||
1748 | 126 | def connectTCP(self, host, port, factory, *args, **kwargs): | ||
1749 | 127 | """A connectTCP going thru the tunnel.""" | ||
1750 | 128 | logger.info("Connecting (TCP) to %r:%r via tunnel at %r:%r", | ||
1751 | 129 | host, port, self.tunnel_host, self.tunnel_port) | ||
1752 | 130 | tunnel_factory = TunnelClientFactory(host, port, factory, self.cookie) | ||
1753 | 131 | return reactor.connectTCP(self.tunnel_host, self.tunnel_port, | ||
1754 | 132 | tunnel_factory, *args, **kwargs) | ||
1755 | 133 | |||
1756 | 134 | def connectSSL(self, host, port, factory, | ||
1757 | 135 | contextFactory, *args, **kwargs): | ||
1758 | 136 | """A connectSSL going thru the tunnel.""" | ||
1759 | 137 | logger.info("Connecting (SSL) to %r:%r via tunnel at %r:%r", | ||
1760 | 138 | host, port, self.tunnel_host, self.tunnel_port) | ||
1761 | 139 | tunnel_factory = TunnelClientFactory( | ||
1762 | 140 | host, port, factory, self.cookie, contextFactory) | ||
1763 | 141 | return reactor.connectTCP(self.tunnel_host, self.tunnel_port, | ||
1764 | 142 | tunnel_factory, *args, **kwargs) | ||
1765 | 143 | |||
1766 | 144 | |||
1767 | 145 | class TunnelProcessProtocol(protocol.ProcessProtocol): | ||
1768 | 146 | """The dialog thru stdout with the tunnel server.""" | ||
1769 | 147 | |||
1770 | 148 | timeout = 30 | ||
1771 | 149 | |||
1772 | 150 | def __init__(self, client_d): | ||
1773 | 151 | """Initialize this protocol.""" | ||
1774 | 152 | self.client_d = client_d | ||
1775 | 153 | self.timer = None | ||
1776 | 154 | self.port = None | ||
1777 | 155 | self.cookie = None | ||
1778 | 156 | |||
1779 | 157 | def connectionMade(self): | ||
1780 | 158 | """The process has started, start a timer.""" | ||
1781 | 159 | logger.info("Tunnel process started.") | ||
1782 | 160 | self.timer = reactor.callLater(self.timeout, self.process_timeouted) | ||
1783 | 161 | |||
1784 | 162 | def process_timeouted(self): | ||
1785 | 163 | """The process took too long to reply.""" | ||
1786 | 164 | if not self.client_d.called: | ||
1787 | 165 | logger.info("Timeout while waiting for tunnel process.") | ||
1788 | 166 | self.client_d.callback(reactor) | ||
1789 | 167 | |||
1790 | 168 | def finish_timeout(self): | ||
1791 | 169 | """Stop the timer from firing.""" | ||
1792 | 170 | if self.timer and self.timer.active(): | ||
1793 | 171 | logger.debug("canceling timer before connection timeout") | ||
1794 | 172 | self.timer.cancel() | ||
1795 | 173 | |||
1796 | 174 | def processExited(self, status): | ||
1797 | 175 | """The tunnel process has exited with some error code.""" | ||
1798 | 176 | self.finish_timeout() | ||
1799 | 177 | logger.info("Tunnel process exit status %r.", status) | ||
1800 | 178 | if not self.client_d.called: | ||
1801 | 179 | logger.debug("Tunnel process exited before TunnelClient created. " | ||
1802 | 180 | "Falling back to reactor") | ||
1803 | 181 | self.client_d.callback(reactor) | ||
1804 | 182 | |||
1805 | 183 | def outReceived(self, data): | ||
1806 | 184 | """Receive the port number.""" | ||
1807 | 185 | if self.client_d.called: | ||
1808 | 186 | return | ||
1809 | 187 | |||
1810 | 188 | for line in data.split("\n"): | ||
1811 | 189 | if line.startswith(TUNNEL_PORT_LABEL): | ||
1812 | 190 | _header, port = line.split(":", 1) | ||
1813 | 191 | self.port = int(port.strip()) | ||
1814 | 192 | if line.startswith(TUNNEL_COOKIE_LABEL): | ||
1815 | 193 | _header, cookie = line.split(":", 1) | ||
1816 | 194 | self.cookie = cookie.strip() | ||
1817 | 195 | |||
1818 | 196 | if self.port and self.cookie: | ||
1819 | 197 | logger.info("Tunnel process listening on port %r.", self.port) | ||
1820 | 198 | client = TunnelClient(LOCALHOST, self.port, self.cookie) | ||
1821 | 199 | self.client_d.callback(client) | ||
1822 | 200 | |||
1823 | 201 | def errReceived(self, data): | ||
1824 | 202 | logger.debug("Got stderr from tunnel process: %r", data) | ||
1825 | 203 | 0 | ||
1826 | === removed file 'ubuntuone/proxy/tunnel_server.py' | |||
1827 | --- ubuntuone/proxy/tunnel_server.py 2017-01-07 18:51:07 +0000 | |||
1828 | +++ ubuntuone/proxy/tunnel_server.py 1970-01-01 00:00:00 +0000 | |||
1829 | @@ -1,405 +0,0 @@ | |||
1830 | 1 | # -*- coding: utf-8 -*- | ||
1831 | 2 | # | ||
1832 | 3 | # Copyright 2012-2013 Canonical Ltd. | ||
1833 | 4 | # Copyright 2015-2017 Chicharreros (https://launchpad.net/~chicharreros) | ||
1834 | 5 | # | ||
1835 | 6 | # This program is free software: you can redistribute it and/or modify it | ||
1836 | 7 | # under the terms of the GNU General Public License version 3, as published | ||
1837 | 8 | # by the Free Software Foundation. | ||
1838 | 9 | # | ||
1839 | 10 | # This program is distributed in the hope that it will be useful, but | ||
1840 | 11 | # WITHOUT ANY WARRANTY; without even the implied warranties of | ||
1841 | 12 | # MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR | ||
1842 | 13 | # PURPOSE. See the GNU General Public License for more details. | ||
1843 | 14 | # | ||
1844 | 15 | # You should have received a copy of the GNU General Public License along | ||
1845 | 16 | # with this program. If not, see <http://www.gnu.org/licenses/>. | ||
1846 | 17 | # | ||
1847 | 18 | # In addition, as a special exception, the copyright holders give | ||
1848 | 19 | # permission to link the code of portions of this program with the | ||
1849 | 20 | # OpenSSL library under certain conditions as described in each | ||
1850 | 21 | # individual source file, and distribute linked combinations | ||
1851 | 22 | # including the two. | ||
1852 | 23 | # You must obey the GNU General Public License in all respects | ||
1853 | 24 | # for all of the code used other than OpenSSL. If you modify | ||
1854 | 25 | # file(s) with this exception, you may extend this exception to your | ||
1855 | 26 | # version of the file(s), but you are not obligated to do so. If you | ||
1856 | 27 | # do not wish to do so, delete this exception statement from your | ||
1857 | 28 | # version. If you delete this exception statement from all source | ||
1858 | 29 | # files in the program, then also delete it here. | ||
1859 | 30 | """A tunnel through proxies. | ||
1860 | 31 | |||
1861 | 32 | The layers in a tunneled proxied connection: | ||
1862 | 33 | |||
1863 | 34 | ↓ tunnelclient - initiates tcp to tunnelserver, request outward connection | ||
1864 | 35 | ↕ client protocol - started after the tunneclient gets connected | ||
1865 | 36 | ---process boundary--- | ||
1866 | 37 | ↕ tunnelserver - creates a tunnel instance per incoming connection | ||
1867 | 38 | ↕ tunnel - hold a qtcpsocket to tunnelclient, and srvtunnelproto to the remote | ||
1868 | 39 | ↕ servertunnelprotocol - gets CONNECT from tunnelclient, creates a remotesocket | ||
1869 | 40 | ↕ remotesocket - connects to the destination server via a proxy | ||
1870 | 41 | ↕ proxy server - goes thru firewalls | ||
1871 | 42 | ↑ server - dialogues with the client protocol | ||
1872 | 43 | |||
1873 | 44 | """ | ||
1874 | 45 | |||
1875 | 46 | import sys | ||
1876 | 47 | import uuid | ||
1877 | 48 | |||
1878 | 49 | from PyQt4.QtCore import QCoreApplication, QTimer | ||
1879 | 50 | from PyQt4.QtNetwork import ( | ||
1880 | 51 | QAbstractSocket, | ||
1881 | 52 | QHostAddress, | ||
1882 | 53 | QNetworkProxy, | ||
1883 | 54 | QNetworkProxyQuery, | ||
1884 | 55 | QNetworkProxyFactory, | ||
1885 | 56 | QTcpServer, | ||
1886 | 57 | QTcpSocket, | ||
1887 | 58 | ) | ||
1888 | 59 | from twisted.internet import defer, interfaces | ||
1889 | 60 | from zope.interface import implements | ||
1890 | 61 | |||
1891 | 62 | from ubuntuone.clientdefs import NAME | ||
1892 | 63 | from ubuntuone.keyring import Keyring | ||
1893 | 64 | from ubuntuone.utils import gsettings | ||
1894 | 65 | from ubuntuone.proxy.common import ( | ||
1895 | 66 | BaseTunnelProtocol, | ||
1896 | 67 | CRLF, | ||
1897 | 68 | TUNNEL_COOKIE_HEADER, | ||
1898 | 69 | TUNNEL_COOKIE_LABEL, | ||
1899 | 70 | TUNNEL_PORT_LABEL, | ||
1900 | 71 | ) | ||
1901 | 72 | from ubuntuone.proxy.logger import logger | ||
1902 | 73 | try: | ||
1903 | 74 | from ubuntuone.utils.locale import fix_turkish_locale | ||
1904 | 75 | except ImportError: | ||
1905 | 76 | def fix_turkish_locale(): | ||
1906 | 77 | return None | ||
1907 | 78 | |||
1908 | 79 | |||
1909 | 80 | DEFAULT_CODE = 500 | ||
1910 | 81 | DEFAULT_DESCRIPTION = "Connection error" | ||
1911 | 82 | |||
1912 | 83 | |||
1913 | 84 | class ConnectionError(Exception): | ||
1914 | 85 | """The client failed connecting to the destination.""" | ||
1915 | 86 | |||
1916 | 87 | def __init__(self, code=DEFAULT_CODE, description=DEFAULT_DESCRIPTION): | ||
1917 | 88 | self.code = code | ||
1918 | 89 | self.description = description | ||
1919 | 90 | |||
1920 | 91 | |||
1921 | 92 | class ProxyAuthenticationError(ConnectionError): | ||
1922 | 93 | """Credentials mismatch going thru a proxy.""" | ||
1923 | 94 | |||
1924 | 95 | |||
1925 | 96 | def build_proxy(settings_groups): | ||
1926 | 97 | """Create a QNetworkProxy from these settings.""" | ||
1927 | 98 | proxy_groups = [ | ||
1928 | 99 | ("socks", QNetworkProxy.Socks5Proxy), | ||
1929 | 100 | ("https", QNetworkProxy.HttpProxy), | ||
1930 | 101 | ("http", QNetworkProxy.HttpProxy), | ||
1931 | 102 | ] | ||
1932 | 103 | for group, proxy_type in proxy_groups: | ||
1933 | 104 | if group not in settings_groups: | ||
1934 | 105 | continue | ||
1935 | 106 | settings = settings_groups[group] | ||
1936 | 107 | if "host" in settings and "port" in settings: | ||
1937 | 108 | return QNetworkProxy(proxy_type, | ||
1938 | 109 | hostName=settings.get("host", ""), | ||
1939 | 110 | port=settings.get("port", 0), | ||
1940 | 111 | user=settings.get("username", ""), | ||
1941 | 112 | password=settings.get("password", "")) | ||
1942 | 113 | logger.error("No proxy correctly configured.") | ||
1943 | 114 | return QNetworkProxy(QNetworkProxy.DefaultProxy) | ||
1944 | 115 | |||
1945 | 116 | |||
1946 | 117 | class RemoteSocket(QTcpSocket): | ||
1947 | 118 | """A dumb connection through a proxy to a destination hostport.""" | ||
1948 | 119 | |||
1949 | 120 | def __init__(self, tunnel_protocol): | ||
1950 | 121 | """Initialize this object.""" | ||
1951 | 122 | super(RemoteSocket, self).__init__() | ||
1952 | 123 | self.protocol = tunnel_protocol | ||
1953 | 124 | self.connected_d = defer.Deferred() | ||
1954 | 125 | self.connected.connect(self.handle_connected) | ||
1955 | 126 | self.proxyAuthenticationRequired.connect(self.handle_auth_required) | ||
1956 | 127 | self.buffered_data = [] | ||
1957 | 128 | |||
1958 | 129 | def handle_connected(self): | ||
1959 | 130 | """When connected, send all pending data.""" | ||
1960 | 131 | self.disconnected.connect(self.handle_disconnected) | ||
1961 | 132 | self.connected_d.callback(None) | ||
1962 | 133 | for d in self.buffered_data: | ||
1963 | 134 | logger.debug("writing remote: %d bytes", len(d)) | ||
1964 | 135 | super(RemoteSocket, self).write(d) | ||
1965 | 136 | self.buffered_data = [] | ||
1966 | 137 | |||
1967 | 138 | def handle_disconnected(self): | ||
1968 | 139 | """Do something with disconnections.""" | ||
1969 | 140 | logger.debug("Remote socket disconnected") | ||
1970 | 141 | self.protocol.remote_disconnected() | ||
1971 | 142 | |||
1972 | 143 | def write(self, data): | ||
1973 | 144 | """Write data to the remote end, buffering if not connected.""" | ||
1974 | 145 | if self.state() == QAbstractSocket.ConnectedState: | ||
1975 | 146 | logger.debug("writing remote: %d bytes", len(data)) | ||
1976 | 147 | super(RemoteSocket, self).write(data) | ||
1977 | 148 | else: | ||
1978 | 149 | self.buffered_data.append(data) | ||
1979 | 150 | |||
1980 | 151 | def connect(self, hostport): | ||
1981 | 152 | """Try to establish the connection to the remote end.""" | ||
1982 | 153 | host, port = hostport.split(":") | ||
1983 | 154 | |||
1984 | 155 | try: | ||
1985 | 156 | port = int(port) | ||
1986 | 157 | except ValueError: | ||
1987 | 158 | raise ConnectionError(400, "Destination port must be an integer.") | ||
1988 | 159 | |||
1989 | 160 | self.readyRead.connect(self.handle_ready_read) | ||
1990 | 161 | self.error.connect(self.handle_error) | ||
1991 | 162 | self.connectToHost(host, port) | ||
1992 | 163 | |||
1993 | 164 | return self.connected_d | ||
1994 | 165 | |||
1995 | 166 | def handle_auth_required(self, proxy, authenticator): | ||
1996 | 167 | """Handle the proxyAuthenticationRequired signal.""" | ||
1997 | 168 | self.protocol.proxy_auth_required(proxy, authenticator) | ||
1998 | 169 | |||
1999 | 170 | def handle_error(self, socket_error): | ||
2000 | 171 | """Some error happened while connecting.""" | ||
2001 | 172 | error_description = "%s (%d)" % (self.errorString(), socket_error) | ||
2002 | 173 | logger.error("connection error: %s", error_description) | ||
2003 | 174 | if self.connected_d.called: | ||
2004 | 175 | return | ||
2005 | 176 | |||
2006 | 177 | if socket_error == self.ProxyAuthenticationRequiredError: | ||
2007 | 178 | error = ProxyAuthenticationError(407, error_description) | ||
2008 | 179 | else: | ||
2009 | 180 | error = ConnectionError(500, error_description) | ||
2010 | 181 | |||
2011 | 182 | self.connected_d.errback(error) | ||
2012 | 183 | |||
2013 | 184 | def handle_ready_read(self): | ||
2014 | 185 | """Forward data from the remote end to the parent protocol.""" | ||
2015 | 186 | data = self.readAll() | ||
2016 | 187 | self.protocol.response_data_received(data) | ||
2017 | 188 | |||
2018 | 189 | @defer.inlineCallbacks | ||
2019 | 190 | def stop(self): | ||
2020 | 191 | """Finish and cleanup.""" | ||
2021 | 192 | self.disconnectFromHost() | ||
2022 | 193 | while self.state() != self.UnconnectedState: | ||
2023 | 194 | d = defer.Deferred() | ||
2024 | 195 | QTimer.singleShot(100, lambda: d.callback(None)) | ||
2025 | 196 | yield d | ||
2026 | 197 | |||
2027 | 198 | |||
2028 | 199 | class ServerTunnelProtocol(BaseTunnelProtocol): | ||
2029 | 200 | """CONNECT sever protocol for tunnelling connections.""" | ||
2030 | 201 | |||
2031 | 202 | def __init__(self, client_class): | ||
2032 | 203 | """Initialize this protocol.""" | ||
2033 | 204 | BaseTunnelProtocol.__init__(self) | ||
2034 | 205 | self.hostport = "" | ||
2035 | 206 | self.client = None | ||
2036 | 207 | self.client_class = client_class | ||
2037 | 208 | self.proxy_credentials = None | ||
2038 | 209 | self.proxy_domain = None | ||
2039 | 210 | |||
2040 | 211 | def error_response(self, code, description): | ||
2041 | 212 | """Write a response with an error, and disconnect.""" | ||
2042 | 213 | self.write_transport("HTTP/1.0 %d %s" % (code, description) + CRLF * 2) | ||
2043 | 214 | self.transport.loseConnection() | ||
2044 | 215 | if self.client: | ||
2045 | 216 | self.client.stop() | ||
2046 | 217 | self.clearLineBuffer() | ||
2047 | 218 | |||
2048 | 219 | def write_transport(self, data): | ||
2049 | 220 | """Write a response in the transport.""" | ||
2050 | 221 | self.transport.write(data) | ||
2051 | 222 | |||
2052 | 223 | def proxy_auth_required(self, proxy, authenticator): | ||
2053 | 224 | """Proxy authentication is required.""" | ||
2054 | 225 | logger.info("auth_required %r, %r", | ||
2055 | 226 | proxy.hostName(), self.proxy_domain) | ||
2056 | 227 | if self.proxy_credentials and proxy.hostName() == self.proxy_domain: | ||
2057 | 228 | logger.info("Credentials added to authenticator.") | ||
2058 | 229 | authenticator.setUser(self.proxy_credentials["username"]) | ||
2059 | 230 | authenticator.setPassword(self.proxy_credentials["password"]) | ||
2060 | 231 | else: | ||
2061 | 232 | logger.info("Credentials needed, but none available.") | ||
2062 | 233 | self.proxy_domain = proxy.hostName() | ||
2063 | 234 | |||
2064 | 235 | def handle_first_line(self, line): | ||
2065 | 236 | """Special handling for the first line received.""" | ||
2066 | 237 | try: | ||
2067 | 238 | method, hostport, proto_version = line.split(" ", 2) | ||
2068 | 239 | if proto_version != "HTTP/1.0": | ||
2069 | 240 | self.error_response(505, "HTTP Version Not Supported") | ||
2070 | 241 | return | ||
2071 | 242 | if method != "CONNECT": | ||
2072 | 243 | self.error_response(405, "Only the CONNECT method is allowed") | ||
2073 | 244 | return | ||
2074 | 245 | self.hostport = hostport | ||
2075 | 246 | except ValueError: | ||
2076 | 247 | self.error_response(400, "Bad request") | ||
2077 | 248 | |||
2078 | 249 | def verify_cookie(self): | ||
2079 | 250 | """Fail if the cookie is wrong or missing.""" | ||
2080 | 251 | cookie_received = dict(self.received_headers).get(TUNNEL_COOKIE_HEADER) | ||
2081 | 252 | if cookie_received != self.transport.cookie: | ||
2082 | 253 | raise ConnectionError(418, "Please see RFC 2324") | ||
2083 | 254 | |||
2084 | 255 | @defer.inlineCallbacks | ||
2085 | 256 | def headers_done(self): | ||
2086 | 257 | """An empty line was received, start connecting and switch mode.""" | ||
2087 | 258 | try: | ||
2088 | 259 | self.verify_cookie() | ||
2089 | 260 | try: | ||
2090 | 261 | logger.info("Connecting once") | ||
2091 | 262 | self.client = self.client_class(self) | ||
2092 | 263 | yield self.client.connect(self.hostport) | ||
2093 | 264 | except ProxyAuthenticationError: | ||
2094 | 265 | if not self.proxy_domain: | ||
2095 | 266 | logger.info("No proxy domain defined") | ||
2096 | 267 | raise | ||
2097 | 268 | |||
2098 | 269 | credentials = yield Keyring().get_credentials( | ||
2099 | 270 | str(self.proxy_domain)) | ||
2100 | 271 | if "username" in credentials: | ||
2101 | 272 | self.proxy_credentials = credentials | ||
2102 | 273 | logger.info("Connecting again with keyring credentials") | ||
2103 | 274 | self.client = self.client_class(self) | ||
2104 | 275 | yield self.client.connect(self.hostport) | ||
2105 | 276 | logger.info("Connected with keyring credentials") | ||
2106 | 277 | |||
2107 | 278 | response_headers = { | ||
2108 | 279 | "Server": "%s proxy tunnel" % NAME, | ||
2109 | 280 | } | ||
2110 | 281 | self.write_transport("HTTP/1.0 200 Proxy connection established" + | ||
2111 | 282 | CRLF + self.format_headers(response_headers) + | ||
2112 | 283 | CRLF) | ||
2113 | 284 | except ConnectionError as e: | ||
2114 | 285 | logger.exception("Connection error") | ||
2115 | 286 | self.error_response(e.code, e.description) | ||
2116 | 287 | except Exception: | ||
2117 | 288 | logger.exception("Unhandled problem while connecting") | ||
2118 | 289 | |||
2119 | 290 | def rawDataReceived(self, data): | ||
2120 | 291 | """Tunnel all raw data straight to the other side.""" | ||
2121 | 292 | self.client.write(data) | ||
2122 | 293 | |||
2123 | 294 | def response_data_received(self, data): | ||
2124 | 295 | """Return data coming from the other side.""" | ||
2125 | 296 | self.write_transport(data) | ||
2126 | 297 | |||
2127 | 298 | |||
2128 | 299 | class Tunnel(object): | ||
2129 | 300 | """An instance of a running tunnel.""" | ||
2130 | 301 | |||
2131 | 302 | implements(interfaces.ITransport) | ||
2132 | 303 | |||
2133 | 304 | def __init__(self, local_socket, cookie): | ||
2134 | 305 | """Initialize this Tunnel instance.""" | ||
2135 | 306 | self.cookie = cookie | ||
2136 | 307 | self.disconnecting = False | ||
2137 | 308 | self.local_socket = local_socket | ||
2138 | 309 | self.protocol = ServerTunnelProtocol(RemoteSocket) | ||
2139 | 310 | self.protocol.transport = self | ||
2140 | 311 | local_socket.readyRead.connect(self.server_ready_read) | ||
2141 | 312 | local_socket.disconnected.connect(self.local_disconnected) | ||
2142 | 313 | |||
2143 | 314 | def server_ready_read(self): | ||
2144 | 315 | """Data available on the local end. Move it forward.""" | ||
2145 | 316 | data = bytes(self.local_socket.readAll()) | ||
2146 | 317 | self.protocol.dataReceived(data) | ||
2147 | 318 | |||
2148 | 319 | def write(self, data): | ||
2149 | 320 | """Data available on the remote end. Bring it back.""" | ||
2150 | 321 | logger.debug("writing local: %d bytes", len(data)) | ||
2151 | 322 | self.local_socket.write(data) | ||
2152 | 323 | |||
2153 | 324 | def loseConnection(self): | ||
2154 | 325 | """The remote end disconnected.""" | ||
2155 | 326 | logger.debug("disconnecting local end.") | ||
2156 | 327 | self.local_socket.close() | ||
2157 | 328 | |||
2158 | 329 | def local_disconnected(self): | ||
2159 | 330 | """The local end disconnected.""" | ||
2160 | 331 | logger.debug("The local socket got disconnected.") | ||
2161 | 332 | # TODO: handle this case in an upcoming branch | ||
2162 | 333 | |||
2163 | 334 | |||
2164 | 335 | class TunnelServer(object): | ||
2165 | 336 | """A server for tunnel instances.""" | ||
2166 | 337 | |||
2167 | 338 | def __init__(self, cookie): | ||
2168 | 339 | """Initialize this tunnel instance.""" | ||
2169 | 340 | self.tunnels = [] | ||
2170 | 341 | self.cookie = cookie | ||
2171 | 342 | self.server = QTcpServer(QCoreApplication.instance()) | ||
2172 | 343 | self.server.newConnection.connect(self.new_connection) | ||
2173 | 344 | self.server.listen(QHostAddress.LocalHost, 0) | ||
2174 | 345 | logger.info("Starting tunnel server at port %d", self.port) | ||
2175 | 346 | |||
2176 | 347 | def new_connection(self): | ||
2177 | 348 | """On a new connection create a new tunnel instance.""" | ||
2178 | 349 | logger.info("New connection made") | ||
2179 | 350 | local_socket = self.server.nextPendingConnection() | ||
2180 | 351 | tunnel = Tunnel(local_socket, self.cookie) | ||
2181 | 352 | self.tunnels.append(tunnel) | ||
2182 | 353 | |||
2183 | 354 | def shutdown(self): | ||
2184 | 355 | """Terminate every connection.""" | ||
2185 | 356 | # TODO: handle this gracefully in an upcoming branch | ||
2186 | 357 | |||
2187 | 358 | @property | ||
2188 | 359 | def port(self): | ||
2189 | 360 | """The port where this server listens.""" | ||
2190 | 361 | return self.server.serverPort() | ||
2191 | 362 | |||
2192 | 363 | |||
2193 | 364 | def check_proxy_enabled(host, port): | ||
2194 | 365 | """Check if the proxy is enabled.""" | ||
2195 | 366 | port = int(port) | ||
2196 | 367 | if sys.platform.startswith("linux"): | ||
2197 | 368 | settings = gsettings.get_proxy_settings() | ||
2198 | 369 | enabled = len(settings) > 0 | ||
2199 | 370 | if enabled: | ||
2200 | 371 | proxy = build_proxy(settings) | ||
2201 | 372 | QNetworkProxy.setApplicationProxy(proxy) | ||
2202 | 373 | else: | ||
2203 | 374 | logger.info("Proxy is disabled.") | ||
2204 | 375 | return enabled | ||
2205 | 376 | else: | ||
2206 | 377 | QNetworkProxyFactory.setUseSystemConfiguration(True) | ||
2207 | 378 | query = QNetworkProxyQuery(host, port) | ||
2208 | 379 | proxies = QNetworkProxyFactory.systemProxyForQuery(query) | ||
2209 | 380 | return len(proxies) and proxies[0].type() != QNetworkProxy.NoProxy | ||
2210 | 381 | |||
2211 | 382 | |||
2212 | 383 | def install_qt_dbus(): | ||
2213 | 384 | """Import and install the qt+dbus integration.""" | ||
2214 | 385 | from dbus.mainloop.qt import DBusQtMainLoop | ||
2215 | 386 | DBusQtMainLoop(set_as_default=True) | ||
2216 | 387 | |||
2217 | 388 | |||
2218 | 389 | def main(argv): | ||
2219 | 390 | """The main function for the tunnel server.""" | ||
2220 | 391 | fix_turkish_locale() | ||
2221 | 392 | if not check_proxy_enabled(*argv[1:]): | ||
2222 | 393 | sys.stdout.write("Proxy not enabled.") | ||
2223 | 394 | sys.stdout.flush() | ||
2224 | 395 | else: | ||
2225 | 396 | if sys.platform.startswith("linux"): | ||
2226 | 397 | install_qt_dbus() | ||
2227 | 398 | |||
2228 | 399 | app = QCoreApplication(argv) | ||
2229 | 400 | cookie = str(uuid.uuid4()) | ||
2230 | 401 | tunnel_server = TunnelServer(cookie) | ||
2231 | 402 | sys.stdout.write("%s: %d\n" % (TUNNEL_PORT_LABEL, tunnel_server.port) + | ||
2232 | 403 | "%s: %s\n" % (TUNNEL_COOKIE_LABEL, cookie)) | ||
2233 | 404 | sys.stdout.flush() | ||
2234 | 405 | app.exec_() | ||
2235 | 406 | 0 | ||
2236 | === modified file 'ubuntuone/syncdaemon/action_queue.py' | |||
2237 | --- ubuntuone/syncdaemon/action_queue.py 2017-02-10 01:15:07 +0000 | |||
2238 | +++ ubuntuone/syncdaemon/action_queue.py 2018-03-14 21:14:07 +0000 | |||
2239 | @@ -1,7 +1,7 @@ | |||
2240 | 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
2241 | 2 | # | 2 | # |
2242 | 3 | # Copyright 2009-2015 Canonical Ltd. | 3 | # Copyright 2009-2015 Canonical Ltd. |
2244 | 4 | # Copyright 2015-2017 Chicharreros (https://launchpad.net/~chicharreros) | 4 | # Copyright 2015-2018 Chicharreros (https://launchpad.net/~chicharreros) |
2245 | 5 | # | 5 | # |
2246 | 6 | # This program is free software: you can redistribute it and/or modify it | 6 | # This program is free software: you can redistribute it and/or modify it |
2247 | 7 | # under the terms of the GNU General Public License version 3, as published | 7 | # under the terms of the GNU General Public License version 3, as published |
2248 | @@ -59,7 +59,6 @@ | |||
2249 | 59 | from ubuntuone.syncdaemon.interfaces import IActionQueue, IMarker | 59 | from ubuntuone.syncdaemon.interfaces import IActionQueue, IMarker |
2250 | 60 | from ubuntuone.syncdaemon.logger import mklog, TRACE | 60 | from ubuntuone.syncdaemon.logger import mklog, TRACE |
2251 | 61 | from ubuntuone.syncdaemon import config, offload_queue | 61 | from ubuntuone.syncdaemon import config, offload_queue |
2252 | 62 | from ubuntuone.syncdaemon import tunnel_runner | ||
2253 | 63 | 62 | ||
2254 | 64 | logger = logging.getLogger("ubuntuone.SyncDaemon.ActionQueue") | 63 | logger = logging.getLogger("ubuntuone.SyncDaemon.ActionQueue") |
2255 | 65 | 64 | ||
2256 | @@ -856,27 +855,20 @@ | |||
2257 | 856 | self.event_queue.push('SV_VOLUME_NEW_GENERATION', | 855 | self.event_queue.push('SV_VOLUME_NEW_GENERATION', |
2258 | 857 | volume_id=volume_id, generation=generation) | 856 | volume_id=volume_id, generation=generation) |
2259 | 858 | 857 | ||
2260 | 859 | def _get_tunnel_runner(self, host, port): | ||
2261 | 860 | """Build the tunnel runner.""" | ||
2262 | 861 | return tunnel_runner.TunnelRunner(host, port) | ||
2263 | 862 | |||
2264 | 863 | @defer.inlineCallbacks | ||
2265 | 864 | def _make_connection(self): | 858 | def _make_connection(self): |
2266 | 865 | """Do the real connect call.""" | 859 | """Do the real connect call.""" |
2267 | 866 | connection_info = self.connection_info.next() | 860 | connection_info = self.connection_info.next() |
2268 | 867 | logger.info("Attempting connection to %s", connection_info) | 861 | logger.info("Attempting connection to %s", connection_info) |
2269 | 868 | host = connection_info['host'] | 862 | host = connection_info['host'] |
2270 | 869 | port = connection_info['port'] | 863 | port = connection_info['port'] |
2271 | 870 | tunnelrunner = self._get_tunnel_runner(host, port) | ||
2272 | 871 | client = yield tunnelrunner.get_client() | ||
2273 | 872 | if connection_info['use_ssl']: | 864 | if connection_info['use_ssl']: |
2274 | 873 | ssl_context = get_ssl_context( | 865 | ssl_context = get_ssl_context( |
2275 | 874 | connection_info['disable_ssl_verify'], host) | 866 | connection_info['disable_ssl_verify'], host) |
2277 | 875 | self.connector = client.connectSSL( | 867 | self.connector = reactor.connectSSL( |
2278 | 876 | host, port, factory=self, contextFactory=ssl_context, | 868 | host, port, factory=self, contextFactory=ssl_context, |
2279 | 877 | timeout=self.connection_timeout) | 869 | timeout=self.connection_timeout) |
2280 | 878 | else: | 870 | else: |
2282 | 879 | self.connector = client.connectTCP( | 871 | self.connector = reactor.connectTCP( |
2283 | 880 | host, port, self, timeout=self.connection_timeout) | 872 | host, port, self, timeout=self.connection_timeout) |
2284 | 881 | 873 | ||
2285 | 882 | def connect(self): | 874 | def connect(self): |
2286 | 883 | 875 | ||
2287 | === modified file 'ubuntuone/syncdaemon/tests/test_action_queue.py' | |||
2288 | --- ubuntuone/syncdaemon/tests/test_action_queue.py 2018-03-08 19:39:13 +0000 | |||
2289 | +++ ubuntuone/syncdaemon/tests/test_action_queue.py 2018-03-14 21:14:07 +0000 | |||
2290 | @@ -1,7 +1,7 @@ | |||
2291 | 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
2292 | 2 | # | 2 | # |
2293 | 3 | # Copyright 2009-2015 Canonical Ltd. | 3 | # Copyright 2009-2015 Canonical Ltd. |
2295 | 4 | # Copyright 2016-2017 Chicharreros (https://launchpad.net/~chicharreros) | 4 | # Copyright 2015-2018 Chicharreros (https://launchpad.net/~chicharreros) |
2296 | 5 | # | 5 | # |
2297 | 6 | # This program is free software: you can redistribute it and/or modify it | 6 | # This program is free software: you can redistribute it and/or modify it |
2298 | 7 | # under the terms of the GNU General Public License version 3, as published | 7 | # under the terms of the GNU General Public License version 3, as published |
2299 | @@ -1384,9 +1384,13 @@ | |||
2300 | 1384 | self.assertTrue(self.handler.check_info("Connection started", | 1384 | self.assertTrue(self.handler.check_info("Connection started", |
2301 | 1385 | "host 1.2.3.4", "port 4321")) | 1385 | "host 1.2.3.4", "port 4321")) |
2302 | 1386 | 1386 | ||
2303 | 1387 | @defer.inlineCallbacks | ||
2304 | 1388 | def test_connection_info_rotation(self): | 1387 | def test_connection_info_rotation(self): |
2305 | 1389 | """It tries to connect to different servers.""" | 1388 | """It tries to connect to different servers.""" |
2306 | 1389 | # store how connectTCP is called | ||
2307 | 1390 | called = [] | ||
2308 | 1391 | self.patch( | ||
2309 | 1392 | reactor, 'connectTCP', | ||
2310 | 1393 | lambda host, port, *a, **k: called.append((host, port))) | ||
2311 | 1390 | 1394 | ||
2312 | 1391 | multiple_conn = [ | 1395 | multiple_conn = [ |
2313 | 1392 | {'host': 'host1', 'port': 'port1', 'use_ssl': False}, | 1396 | {'host': 'host1', 'port': 'port1', 'use_ssl': False}, |
2314 | @@ -1394,61 +1398,14 @@ | |||
2315 | 1394 | ] | 1398 | ] |
2316 | 1395 | self.action_queue.connection_info = itertools.cycle(multiple_conn) | 1399 | self.action_queue.connection_info = itertools.cycle(multiple_conn) |
2317 | 1396 | 1400 | ||
2373 | 1397 | self.tunnel_runner = None | 1401 | self.action_queue._make_connection() |
2374 | 1398 | 1402 | self.assertEqual(called[-1], ('host1', 'port1')) | |
2375 | 1399 | def mitm(*args): | 1403 | |
2376 | 1400 | tunnel_runner = SavingConnectionTunnelRunner(*args) | 1404 | self.action_queue._make_connection() |
2377 | 1401 | self.tunnel_runner = tunnel_runner | 1405 | self.assertEqual(called[-1], ('host2', 'port2')) |
2378 | 1402 | return tunnel_runner | 1406 | |
2379 | 1403 | 1407 | self.action_queue._make_connection() | |
2380 | 1404 | self.action_queue._get_tunnel_runner = mitm | 1408 | self.assertEqual(called[-1], ('host1', 'port1')) |
2326 | 1405 | |||
2327 | 1406 | yield self.action_queue._make_connection() | ||
2328 | 1407 | self.assertEqual(self.tunnel_runner.host, 'host1') | ||
2329 | 1408 | self.assertEqual(self.tunnel_runner.port, 'port1') | ||
2330 | 1409 | |||
2331 | 1410 | yield self.action_queue._make_connection() | ||
2332 | 1411 | self.assertEqual(self.tunnel_runner.host, 'host2') | ||
2333 | 1412 | self.assertEqual(self.tunnel_runner.port, 'port2') | ||
2334 | 1413 | |||
2335 | 1414 | yield self.action_queue._make_connection() | ||
2336 | 1415 | self.assertEqual(self.tunnel_runner.host, 'host1') | ||
2337 | 1416 | self.assertEqual(self.tunnel_runner.port, 'port1') | ||
2338 | 1417 | |||
2339 | 1418 | |||
2340 | 1419 | class TunnelRunnerTestCase(FactoryBaseTestCase): | ||
2341 | 1420 | """Tests for the tunnel runner.""" | ||
2342 | 1421 | |||
2343 | 1422 | tunnel_runner_class = SavingConnectionTunnelRunner | ||
2344 | 1423 | |||
2345 | 1424 | def setUp(self): | ||
2346 | 1425 | result = super(TunnelRunnerTestCase, self).setUp() | ||
2347 | 1426 | self.tunnel_runner = None | ||
2348 | 1427 | orig_get_tunnel_runner = self.action_queue._get_tunnel_runner | ||
2349 | 1428 | |||
2350 | 1429 | def mitm(*args): | ||
2351 | 1430 | tunnel_runner = orig_get_tunnel_runner(*args) | ||
2352 | 1431 | self.tunnel_runner = tunnel_runner | ||
2353 | 1432 | return tunnel_runner | ||
2354 | 1433 | |||
2355 | 1434 | self.action_queue._get_tunnel_runner = mitm | ||
2356 | 1435 | return result | ||
2357 | 1436 | |||
2358 | 1437 | @defer.inlineCallbacks | ||
2359 | 1438 | def test_make_connection_uses_tunnelrunner_non_ssl(self): | ||
2360 | 1439 | """Check that _make_connection uses TunnelRunner.""" | ||
2361 | 1440 | self._patch_connection_info(use_ssl=False) | ||
2362 | 1441 | yield self.action_queue._make_connection() | ||
2363 | 1442 | self.assertTrue(self.tunnel_runner.client.tcp_connected, | ||
2364 | 1443 | "connectTCP is called on the client.") | ||
2365 | 1444 | |||
2366 | 1445 | @defer.inlineCallbacks | ||
2367 | 1446 | def test_make_connection_uses_tunnelrunner_ssl(self): | ||
2368 | 1447 | """Check that _make_connection uses TunnelRunner.""" | ||
2369 | 1448 | self._patch_connection_info(use_ssl=True, disable_ssl_verify=False) | ||
2370 | 1449 | yield self.action_queue._make_connection() | ||
2371 | 1450 | self.assertTrue(self.tunnel_runner.client.ssl_connected, | ||
2372 | 1451 | "connectSSL is called on the client.") | ||
2381 | 1452 | 1409 | ||
2382 | 1453 | 1410 | ||
2383 | 1454 | class ContextRequestedWithHost(FactoryBaseTestCase): | 1411 | class ContextRequestedWithHost(FactoryBaseTestCase): |
2384 | @@ -1459,6 +1416,9 @@ | |||
2385 | 1459 | @defer.inlineCallbacks | 1416 | @defer.inlineCallbacks |
2386 | 1460 | def test_context_request_passes_host(self): | 1417 | def test_context_request_passes_host(self): |
2387 | 1461 | """The context is requested passing the host.""" | 1418 | """The context is requested passing the host.""" |
2388 | 1419 | # avoid a real connection | ||
2389 | 1420 | self.patch(reactor, 'connectSSL', lambda *a, **k: None) | ||
2390 | 1421 | |||
2391 | 1462 | fake_host = "fake_host" | 1422 | fake_host = "fake_host" |
2392 | 1463 | fake_disable_ssl_verify = False | 1423 | fake_disable_ssl_verify = False |
2393 | 1464 | 1424 | ||
2394 | 1465 | 1425 | ||
2395 | === removed file 'ubuntuone/syncdaemon/tests/test_tunnel_runner.py' | |||
2396 | --- ubuntuone/syncdaemon/tests/test_tunnel_runner.py 2016-06-04 21:14:35 +0000 | |||
2397 | +++ ubuntuone/syncdaemon/tests/test_tunnel_runner.py 1970-01-01 00:00:00 +0000 | |||
2398 | @@ -1,185 +0,0 @@ | |||
2399 | 1 | # -*- coding: utf-8 -*- | ||
2400 | 2 | # | ||
2401 | 3 | # Copyright 2012 Canonical Ltd. | ||
2402 | 4 | # | ||
2403 | 5 | # This program is free software: you can redistribute it and/or modify it | ||
2404 | 6 | # under the terms of the GNU General Public License version 3, as published | ||
2405 | 7 | # by the Free Software Foundation. | ||
2406 | 8 | # | ||
2407 | 9 | # This program is distributed in the hope that it will be useful, but | ||
2408 | 10 | # WITHOUT ANY WARRANTY; without even the implied warranties of | ||
2409 | 11 | # MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR | ||
2410 | 12 | # PURPOSE. See the GNU General Public License for more details. | ||
2411 | 13 | # | ||
2412 | 14 | # You should have received a copy of the GNU General Public License along | ||
2413 | 15 | # with this program. If not, see <http://www.gnu.org/licenses/>. | ||
2414 | 16 | # | ||
2415 | 17 | # In addition, as a special exception, the copyright holders give | ||
2416 | 18 | # permission to link the code of portions of this program with the | ||
2417 | 19 | # OpenSSL library under certain conditions as described in each | ||
2418 | 20 | # individual source file, and distribute linked combinations | ||
2419 | 21 | # including the two. | ||
2420 | 22 | # You must obey the GNU General Public License in all respects | ||
2421 | 23 | # for all of the code used other than OpenSSL. If you modify | ||
2422 | 24 | # file(s) with this exception, you may extend this exception to your | ||
2423 | 25 | # version of the file(s), but you are not obligated to do so. If you | ||
2424 | 26 | # do not wish to do so, delete this exception statement from your | ||
2425 | 27 | # version. If you delete this exception statement from all source | ||
2426 | 28 | # files in the program, then also delete it here. | ||
2427 | 29 | """Tests for the proxy tunnel runner.""" | ||
2428 | 30 | |||
2429 | 31 | from twisted.internet import defer, error, reactor, task | ||
2430 | 32 | from twisted.trial.unittest import TestCase | ||
2431 | 33 | |||
2432 | 34 | from ubuntuone.proxy.tests import FAKE_COOKIE | ||
2433 | 35 | from ubuntuone.proxy import tunnel_client | ||
2434 | 36 | from ubuntuone.syncdaemon import tunnel_runner | ||
2435 | 37 | |||
2436 | 38 | FAKE_HOST = "fs-1.two.ubuntu.com" | ||
2437 | 39 | FAKE_PORT = 443 | ||
2438 | 40 | |||
2439 | 41 | |||
2440 | 42 | class TunnelRunnerConstructorTestCase(TestCase): | ||
2441 | 43 | """Test the tunnel runner constructor.""" | ||
2442 | 44 | |||
2443 | 45 | timeout = 3 | ||
2444 | 46 | |||
2445 | 47 | def raise_import_error(self, *args): | ||
2446 | 48 | """Raise an import error.""" | ||
2447 | 49 | raise ImportError | ||
2448 | 50 | |||
2449 | 51 | @defer.inlineCallbacks | ||
2450 | 52 | def test_proxy_support_not_installed(self): | ||
2451 | 53 | """The proxy support binary package is not installed.""" | ||
2452 | 54 | self.patch(tunnel_runner.TunnelRunner, "start_process", | ||
2453 | 55 | self.raise_import_error) | ||
2454 | 56 | tr = tunnel_runner.TunnelRunner(FAKE_HOST, FAKE_PORT) | ||
2455 | 57 | client = yield tr.get_client() | ||
2456 | 58 | self.assertEqual(client, reactor) | ||
2457 | 59 | |||
2458 | 60 | @defer.inlineCallbacks | ||
2459 | 61 | def test_executable_not_found(self): | ||
2460 | 62 | """The executable is not found anywhere.""" | ||
2461 | 63 | self.patch(tunnel_runner, "get_tunnel_bin_cmd", | ||
2462 | 64 | lambda *args, **kwargs: ["this_does_not_exist"]) | ||
2463 | 65 | tr = tunnel_runner.TunnelRunner(FAKE_HOST, FAKE_PORT) | ||
2464 | 66 | client = yield tr.get_client() | ||
2465 | 67 | self.assertEqual(client, reactor) | ||
2466 | 68 | |||
2467 | 69 | |||
2468 | 70 | class FakeProcessTransport(object): | ||
2469 | 71 | """A fake ProcessTransport.""" | ||
2470 | 72 | |||
2471 | 73 | pid = 0 | ||
2472 | 74 | |||
2473 | 75 | def __init__(self): | ||
2474 | 76 | """Initialize this fake.""" | ||
2475 | 77 | self._signals_sent = [] | ||
2476 | 78 | |||
2477 | 79 | def signalProcess(self, signalID): | ||
2478 | 80 | """Send a signal to the process.""" | ||
2479 | 81 | self._signals_sent.append(signalID) | ||
2480 | 82 | |||
2481 | 83 | |||
2482 | 84 | class TunnelRunnerTestCase(TestCase): | ||
2483 | 85 | """Tests for the TunnelRunner.""" | ||
2484 | 86 | |||
2485 | 87 | timeout = 3 | ||
2486 | 88 | |||
2487 | 89 | @defer.inlineCallbacks | ||
2488 | 90 | def setUp(self): | ||
2489 | 91 | """Initialize this testcase.""" | ||
2490 | 92 | yield super(TunnelRunnerTestCase, self).setUp() | ||
2491 | 93 | self.spawned = [] | ||
2492 | 94 | self.triggers = [] | ||
2493 | 95 | self.fake_process_transport = FakeProcessTransport() | ||
2494 | 96 | |||
2495 | 97 | def fake_spawn_process(*args, **kwargs): | ||
2496 | 98 | """A fake spawnProcess.""" | ||
2497 | 99 | self.spawned.append((args, kwargs)) | ||
2498 | 100 | return self.fake_process_transport | ||
2499 | 101 | |||
2500 | 102 | self.patch(tunnel_client.reactor, "spawnProcess", fake_spawn_process) | ||
2501 | 103 | |||
2502 | 104 | def fake_add_system_event_trigger(*args, **kwargs): | ||
2503 | 105 | """A fake addSystemEventTrigger.""" | ||
2504 | 106 | self.triggers.append((args, kwargs)) | ||
2505 | 107 | |||
2506 | 108 | self.patch(tunnel_client.reactor, "addSystemEventTrigger", | ||
2507 | 109 | fake_add_system_event_trigger) | ||
2508 | 110 | self.process_protocol = None | ||
2509 | 111 | self.process_protocol_class = tunnel_client.TunnelProcessProtocol | ||
2510 | 112 | self.patch(tunnel_client, "TunnelProcessProtocol", | ||
2511 | 113 | self.storing_process_protocol_factory) | ||
2512 | 114 | self.tr = tunnel_runner.TunnelRunner("fs-1.one.ubuntu.com", 443) | ||
2513 | 115 | |||
2514 | 116 | def storing_process_protocol_factory(self, *args, **kwargs): | ||
2515 | 117 | """Store the process protocol just created.""" | ||
2516 | 118 | self.process_protocol = self.process_protocol_class(*args, **kwargs) | ||
2517 | 119 | return self.process_protocol | ||
2518 | 120 | |||
2519 | 121 | def test_tunnel_process_is_started(self): | ||
2520 | 122 | """The tunnel process is started.""" | ||
2521 | 123 | self.assertEqual( | ||
2522 | 124 | len(self.spawned), 1, "The tunnel process is started.") | ||
2523 | 125 | |||
2524 | 126 | def test_system_event_finished(self): | ||
2525 | 127 | """An event is added to stop the process with the reactor.""" | ||
2526 | 128 | expected = [(("before", "shutdown", self.tr.stop), {})] | ||
2527 | 129 | self.assertEqual(self.triggers, expected) | ||
2528 | 130 | |||
2529 | 131 | def test_stop_process(self): | ||
2530 | 132 | """The process is stopped if still running.""" | ||
2531 | 133 | self.tr.process_transport.pid = 1234 | ||
2532 | 134 | self.tr.stop() | ||
2533 | 135 | self.assertEqual(self.fake_process_transport._signals_sent, ["KILL"]) | ||
2534 | 136 | |||
2535 | 137 | def test_not_stopped_if_already_finished(self): | ||
2536 | 138 | """Do not stop the tunnel process if it's already finished.""" | ||
2537 | 139 | self.tr.process_transport.pid = None | ||
2538 | 140 | self.tr.stop() | ||
2539 | 141 | self.assertEqual(self.fake_process_transport._signals_sent, []) | ||
2540 | 142 | |||
2541 | 143 | @defer.inlineCallbacks | ||
2542 | 144 | def test_tunnel_process_get_client_yielded_twice(self): | ||
2543 | 145 | """The get_client method can be yielded twice.""" | ||
2544 | 146 | self.process_protocol.processExited(error.ProcessTerminated(1)) | ||
2545 | 147 | client = yield self.tr.get_client() | ||
2546 | 148 | client = yield self.tr.get_client() | ||
2547 | 149 | self.assertNotEqual(client, None) | ||
2548 | 150 | |||
2549 | 151 | @defer.inlineCallbacks | ||
2550 | 152 | def test_tunnel_process_exits_with_error(self): | ||
2551 | 153 | """The tunnel process exits with an error.""" | ||
2552 | 154 | self.process_protocol.processExited(error.ProcessTerminated(1)) | ||
2553 | 155 | client = yield self.tr.get_client() | ||
2554 | 156 | self.assertEqual(client, reactor) | ||
2555 | 157 | |||
2556 | 158 | @defer.inlineCallbacks | ||
2557 | 159 | def test_tunnel_process_exits_gracefully(self): | ||
2558 | 160 | """The tunnel process exits gracefully.""" | ||
2559 | 161 | self.process_protocol.processExited(error.ProcessDone(0)) | ||
2560 | 162 | client = yield self.tr.get_client() | ||
2561 | 163 | self.assertEqual(client, reactor) | ||
2562 | 164 | |||
2563 | 165 | @defer.inlineCallbacks | ||
2564 | 166 | def test_tunnel_process_prints_random_garbage_and_timeouts(self): | ||
2565 | 167 | """The tunnel process prints garbage and timeouts.""" | ||
2566 | 168 | clock = task.Clock() | ||
2567 | 169 | self.patch(tunnel_client, "reactor", clock) | ||
2568 | 170 | self.process_protocol.connectionMade() | ||
2569 | 171 | self.process_protocol.outReceived("Random garbage") | ||
2570 | 172 | clock.advance(self.process_protocol.timeout) | ||
2571 | 173 | client = yield self.tr.get_client() | ||
2572 | 174 | self.assertEqual(client, clock) | ||
2573 | 175 | |||
2574 | 176 | @defer.inlineCallbacks | ||
2575 | 177 | def test_tunnel_process_prints_port_number_and_cookie(self): | ||
2576 | 178 | """The tunnel process prints the port number.""" | ||
2577 | 179 | received = "%s: %d\n%s: %s\n" % ( | ||
2578 | 180 | tunnel_client.TUNNEL_PORT_LABEL, FAKE_PORT, | ||
2579 | 181 | tunnel_client.TUNNEL_COOKIE_LABEL, FAKE_COOKIE) | ||
2580 | 182 | self.process_protocol.outReceived(received) | ||
2581 | 183 | client = yield self.tr.get_client() | ||
2582 | 184 | self.assertEqual(client.tunnel_port, FAKE_PORT) | ||
2583 | 185 | self.assertEqual(client.cookie, FAKE_COOKIE) | ||
2584 | 186 | 0 | ||
2585 | === removed file 'ubuntuone/syncdaemon/tunnel_runner.py' | |||
2586 | --- ubuntuone/syncdaemon/tunnel_runner.py 2012-11-28 08:08:10 +0000 | |||
2587 | +++ ubuntuone/syncdaemon/tunnel_runner.py 1970-01-01 00:00:00 +0000 | |||
2588 | @@ -1,86 +0,0 @@ | |||
2589 | 1 | # -*- coding: utf-8 -*- | ||
2590 | 2 | # | ||
2591 | 3 | # Copyright 2012 Canonical Ltd. | ||
2592 | 4 | # | ||
2593 | 5 | # This program is free software: you can redistribute it and/or modify it | ||
2594 | 6 | # under the terms of the GNU General Public License version 3, as published | ||
2595 | 7 | # by the Free Software Foundation. | ||
2596 | 8 | # | ||
2597 | 9 | # This program is distributed in the hope that it will be useful, but | ||
2598 | 10 | # WITHOUT ANY WARRANTY; without even the implied warranties of | ||
2599 | 11 | # MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR | ||
2600 | 12 | # PURPOSE. See the GNU General Public License for more details. | ||
2601 | 13 | # | ||
2602 | 14 | # You should have received a copy of the GNU General Public License along | ||
2603 | 15 | # with this program. If not, see <http://www.gnu.org/licenses/>. | ||
2604 | 16 | # | ||
2605 | 17 | # In addition, as a special exception, the copyright holders give | ||
2606 | 18 | # permission to link the code of portions of this program with the | ||
2607 | 19 | # OpenSSL library under certain conditions as described in each | ||
2608 | 20 | # individual source file, and distribute linked combinations | ||
2609 | 21 | # including the two. | ||
2610 | 22 | # You must obey the GNU General Public License in all respects | ||
2611 | 23 | # for all of the code used other than OpenSSL. If you modify | ||
2612 | 24 | # file(s) with this exception, you may extend this exception to your | ||
2613 | 25 | # version of the file(s), but you are not obligated to do so. If you | ||
2614 | 26 | # do not wish to do so, delete this exception statement from your | ||
2615 | 27 | # version. If you delete this exception statement from all source | ||
2616 | 28 | # files in the program, then also delete it here. | ||
2617 | 29 | """Run the tunnel process and start a client, with a reactor as a fallback.""" | ||
2618 | 30 | |||
2619 | 31 | import logging | ||
2620 | 32 | |||
2621 | 33 | from twisted.internet import defer, reactor | ||
2622 | 34 | |||
2623 | 35 | from ubuntuone.clientdefs import LIBEXECDIR | ||
2624 | 36 | from ubuntuone.syncdaemon.utils import get_tunnel_bin_cmd | ||
2625 | 37 | |||
2626 | 38 | logger = logging.getLogger("ubuntuone.SyncDaemon.TunnelRunner") | ||
2627 | 39 | |||
2628 | 40 | |||
2629 | 41 | class TunnelRunner(object): | ||
2630 | 42 | """Run a tunnel process.""" | ||
2631 | 43 | |||
2632 | 44 | def __init__(self, host, port): | ||
2633 | 45 | """Start this runner instance.""" | ||
2634 | 46 | self.client_d = defer.Deferred() | ||
2635 | 47 | self.process_transport = None | ||
2636 | 48 | try: | ||
2637 | 49 | self.start_process(host, port) | ||
2638 | 50 | except ImportError: | ||
2639 | 51 | logger.info("Proxy support not installed.") | ||
2640 | 52 | self.client_d.callback(reactor) | ||
2641 | 53 | except Exception: | ||
2642 | 54 | logger.exception("Error while starting tunnel process:") | ||
2643 | 55 | self.client_d.callback(reactor) | ||
2644 | 56 | |||
2645 | 57 | def start_process(self, host, port): | ||
2646 | 58 | """Start the tunnel process.""" | ||
2647 | 59 | from ubuntuone.proxy.tunnel_client import TunnelProcessProtocol | ||
2648 | 60 | protocol = TunnelProcessProtocol(self.client_d) | ||
2649 | 61 | tunnel_cmd = get_tunnel_bin_cmd(extra_fallbacks=[LIBEXECDIR]) | ||
2650 | 62 | |||
2651 | 63 | args = tunnel_cmd + [host, str(port)] | ||
2652 | 64 | |||
2653 | 65 | self.process_transport = reactor.spawnProcess(protocol, args[0], | ||
2654 | 66 | env=None, args=args) | ||
2655 | 67 | reactor.addSystemEventTrigger("before", "shutdown", self.stop) | ||
2656 | 68 | |||
2657 | 69 | def stop(self): | ||
2658 | 70 | """Stop the tunnel process if still running.""" | ||
2659 | 71 | logger.info("Stopping process %r", self.process_transport.pid) | ||
2660 | 72 | if self.process_transport.pid is not None: | ||
2661 | 73 | self.process_transport.signalProcess("KILL") | ||
2662 | 74 | |||
2663 | 75 | def get_client(self): | ||
2664 | 76 | """A deferred with the reactor or a tunnel client.""" | ||
2665 | 77 | |||
2666 | 78 | def client_selected(result, d): | ||
2667 | 79 | """The tunnel_client or the reactor were selected.""" | ||
2668 | 80 | d.callback(result) | ||
2669 | 81 | # make sure the result is available for next callback | ||
2670 | 82 | return result | ||
2671 | 83 | |||
2672 | 84 | d = defer.Deferred() | ||
2673 | 85 | self.client_d.addCallback(client_selected, d) | ||
2674 | 86 | return d | ||
2675 | 87 | 0 | ||
2676 | === modified file 'ubuntuone/syncdaemon/utils.py' | |||
2677 | --- ubuntuone/syncdaemon/utils.py 2015-09-20 00:03:47 +0000 | |||
2678 | +++ ubuntuone/syncdaemon/utils.py 2018-03-14 21:14:07 +0000 | |||
2679 | @@ -1,6 +1,7 @@ | |||
2680 | 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
2681 | 2 | # | 2 | # |
2682 | 3 | # Copyright 2012 Canonical Ltd. | 3 | # Copyright 2012 Canonical Ltd. |
2683 | 4 | # Copyright 2015-2018 Chicharreros (https://launchpad.net/~chicharreros) | ||
2684 | 4 | # | 5 | # |
2685 | 5 | # This program is free software: you can redistribute it and/or modify it | 6 | # This program is free software: you can redistribute it and/or modify it |
2686 | 6 | # under the terms of the GNU General Public License version 3, as published | 7 | # under the terms of the GNU General Public License version 3, as published |
2687 | @@ -42,10 +43,8 @@ | |||
2688 | 42 | 43 | ||
2689 | 43 | 44 | ||
2690 | 44 | SYNCDAEMON_EXECUTABLE = 'ubuntuone-syncdaemon' | 45 | SYNCDAEMON_EXECUTABLE = 'ubuntuone-syncdaemon' |
2691 | 45 | TUNNEL_EXECUTABLE = 'ubuntuone-proxy-tunnel' | ||
2692 | 46 | 46 | ||
2695 | 47 | DARWIN_APP_NAMES = {SYNCDAEMON_EXECUTABLE: 'UbuntuOne Syncdaemon.app', | 47 | DARWIN_APP_NAMES = {SYNCDAEMON_EXECUTABLE: 'UbuntuOne Syncdaemon.app'} |
2694 | 48 | TUNNEL_EXECUTABLE: 'UbuntuOne Proxy Tunnel.app'} | ||
2696 | 49 | 48 | ||
2697 | 50 | 49 | ||
2698 | 51 | def _get_bin_cmd(exe_name, extra_fallbacks=[]): | 50 | def _get_bin_cmd(exe_name, extra_fallbacks=[]): |
2699 | @@ -73,9 +72,3 @@ | |||
2700 | 73 | def get_sd_bin_cmd(): | 72 | def get_sd_bin_cmd(): |
2701 | 74 | """Get cmd + args to launch syncdaemon executable.""" | 73 | """Get cmd + args to launch syncdaemon executable.""" |
2702 | 75 | return _get_bin_cmd(SYNCDAEMON_EXECUTABLE) | 74 | return _get_bin_cmd(SYNCDAEMON_EXECUTABLE) |
2703 | 76 | |||
2704 | 77 | |||
2705 | 78 | def get_tunnel_bin_cmd(extra_fallbacks): | ||
2706 | 79 | """Get cmd + args to launch proxy tunnel.""" | ||
2707 | 80 | return _get_bin_cmd(TUNNEL_EXECUTABLE, | ||
2708 | 81 | extra_fallbacks=extra_fallbacks) | ||
2709 | 82 | 75 | ||
2710 | === removed file 'ubuntuone/utils/gsettings.py' | |||
2711 | --- ubuntuone/utils/gsettings.py 2017-01-07 18:51:07 +0000 | |||
2712 | +++ ubuntuone/utils/gsettings.py 1970-01-01 00:00:00 +0000 | |||
2713 | @@ -1,114 +0,0 @@ | |||
2714 | 1 | # -*- coding: utf-8 -*- | ||
2715 | 2 | # | ||
2716 | 3 | # Copyright 2011-2012 Canonical Ltd. | ||
2717 | 4 | # Copyright 2015-2016 Chicharreros (https://launchpad.net/~chicharreros) | ||
2718 | 5 | # | ||
2719 | 6 | # This program is free software: you can redistribute it and/or modify it | ||
2720 | 7 | # under the terms of the GNU General Public License version 3, as published | ||
2721 | 8 | # by the Free Software Foundation. | ||
2722 | 9 | # | ||
2723 | 10 | # This program is distributed in the hope that it will be useful, but | ||
2724 | 11 | # WITHOUT ANY WARRANTY; without even the implied warranties of | ||
2725 | 12 | # MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR | ||
2726 | 13 | # PURPOSE. See the GNU General Public License for more details. | ||
2727 | 14 | # | ||
2728 | 15 | # You should have received a copy of the GNU General Public License along | ||
2729 | 16 | # with this program. If not, see <http://www.gnu.org/licenses/>. | ||
2730 | 17 | # | ||
2731 | 18 | # In addition, as a special exception, the copyright holders give | ||
2732 | 19 | # permission to link the code of portions of this program with the | ||
2733 | 20 | # OpenSSL library under certain conditions as described in each | ||
2734 | 21 | # individual source file, and distribute linked combinations | ||
2735 | 22 | # including the two. | ||
2736 | 23 | # You must obey the GNU General Public License in all respects | ||
2737 | 24 | # for all of the code used other than OpenSSL. If you modify | ||
2738 | 25 | # file(s) with this exception, you may extend this exception to your | ||
2739 | 26 | # version of the file(s), but you are not obligated to do so. If you | ||
2740 | 27 | # do not wish to do so, delete this exception statement from your | ||
2741 | 28 | # version. If you delete this exception statement from all source | ||
2742 | 29 | # files in the program, then also delete it here. | ||
2743 | 30 | |||
2744 | 31 | """Retrieve the proxy configuration from Gnome.""" | ||
2745 | 32 | |||
2746 | 33 | import logging | ||
2747 | 34 | import subprocess | ||
2748 | 35 | |||
2749 | 36 | |||
2750 | 37 | logger = logging.getLogger(__name__) | ||
2751 | 38 | GSETTINGS_CMDLINE = "gsettings list-recursively org.gnome.system.proxy" | ||
2752 | 39 | CANNOT_PARSE_WARNING = "Cannot parse gsettings value: %r" | ||
2753 | 40 | |||
2754 | 41 | |||
2755 | 42 | def parse_proxy_host(hostname): | ||
2756 | 43 | """Parse the host to get username and password.""" | ||
2757 | 44 | username = None | ||
2758 | 45 | password = None | ||
2759 | 46 | if "@" in hostname: | ||
2760 | 47 | username, hostname = hostname.rsplit("@", 1) | ||
2761 | 48 | if ":" in username: | ||
2762 | 49 | username, password = username.split(":", 1) | ||
2763 | 50 | return hostname, username, password | ||
2764 | 51 | |||
2765 | 52 | |||
2766 | 53 | def parse_manual_proxy_settings(scheme, gsettings): | ||
2767 | 54 | """Parse the settings for a given scheme.""" | ||
2768 | 55 | host, username, pwd = parse_proxy_host(gsettings[scheme + ".host"]) | ||
2769 | 56 | # if the user did not set a proxy for a type (http/https/ftp) we should | ||
2770 | 57 | # return None to ensure that it is not used | ||
2771 | 58 | if host == '': | ||
2772 | 59 | return None | ||
2773 | 60 | |||
2774 | 61 | settings = { | ||
2775 | 62 | "host": host, | ||
2776 | 63 | "port": gsettings[scheme + ".port"], | ||
2777 | 64 | } | ||
2778 | 65 | if scheme == "http" and gsettings["http.use-authentication"]: | ||
2779 | 66 | username = gsettings["http.authentication-user"] | ||
2780 | 67 | pwd = gsettings["http.authentication-password"] | ||
2781 | 68 | if username is not None and pwd is not None: | ||
2782 | 69 | settings.update({ | ||
2783 | 70 | "username": username, | ||
2784 | 71 | "password": pwd, | ||
2785 | 72 | }) | ||
2786 | 73 | return settings | ||
2787 | 74 | |||
2788 | 75 | |||
2789 | 76 | def get_proxy_settings(): | ||
2790 | 77 | """Parse the proxy settings as returned by the gsettings executable.""" | ||
2791 | 78 | output = subprocess.check_output(GSETTINGS_CMDLINE.split()) | ||
2792 | 79 | gsettings = {} | ||
2793 | 80 | base_len = len("org.gnome.system.proxy.") | ||
2794 | 81 | |||
2795 | 82 | for line in output.split("\n"): | ||
2796 | 83 | try: | ||
2797 | 84 | path, key, value = line.split(" ", 2) | ||
2798 | 85 | except ValueError: | ||
2799 | 86 | continue | ||
2800 | 87 | if value.startswith("'"): | ||
2801 | 88 | parsed_value = value[1:-1] | ||
2802 | 89 | elif value.startswith(('[', '@')): | ||
2803 | 90 | parsed_value = value | ||
2804 | 91 | elif value in ('true', 'false'): | ||
2805 | 92 | parsed_value = (value == 'true') | ||
2806 | 93 | elif value.isdigit(): | ||
2807 | 94 | parsed_value = int(value) | ||
2808 | 95 | else: | ||
2809 | 96 | logger.warning(CANNOT_PARSE_WARNING, value) | ||
2810 | 97 | parsed_value = value | ||
2811 | 98 | relative_key = (path + "." + key)[base_len:] | ||
2812 | 99 | gsettings[relative_key] = parsed_value | ||
2813 | 100 | mode = gsettings["mode"] | ||
2814 | 101 | if mode == "none": | ||
2815 | 102 | settings = {} | ||
2816 | 103 | elif mode == "manual": | ||
2817 | 104 | settings = {} | ||
2818 | 105 | for scheme in ["http", "https"]: | ||
2819 | 106 | scheme_settings = parse_manual_proxy_settings(scheme, gsettings) | ||
2820 | 107 | if scheme_settings is not None: | ||
2821 | 108 | settings[scheme] = scheme_settings | ||
2822 | 109 | else: | ||
2823 | 110 | # If mode is automatic the PAC javascript should be interpreted | ||
2824 | 111 | # on each request. That is out of scope so it's ignored for now | ||
2825 | 112 | settings = {} | ||
2826 | 113 | |||
2827 | 114 | return settings | ||
2828 | 115 | 0 | ||
2829 | === removed file 'ubuntuone/utils/tests/test_gsettings.py' | |||
2830 | --- ubuntuone/utils/tests/test_gsettings.py 2017-01-07 18:51:07 +0000 | |||
2831 | +++ ubuntuone/utils/tests/test_gsettings.py 1970-01-01 00:00:00 +0000 | |||
2832 | @@ -1,322 +0,0 @@ | |||
2833 | 1 | # -*- coding: utf-8 -*- | ||
2834 | 2 | # | ||
2835 | 3 | # Copyright 2011-2012 Canonical Ltd. | ||
2836 | 4 | # Copyright 2015-2017 Chicharreros (https://launchpad.net/~chicharreros) | ||
2837 | 5 | # | ||
2838 | 6 | # This program is free software: you can redistribute it and/or modify it | ||
2839 | 7 | # under the terms of the GNU General Public License version 3, as published | ||
2840 | 8 | # by the Free Software Foundation. | ||
2841 | 9 | # | ||
2842 | 10 | # This program is distributed in the hope that it will be useful, but | ||
2843 | 11 | # WITHOUT ANY WARRANTY; without even the implied warranties of | ||
2844 | 12 | # MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR | ||
2845 | 13 | # PURPOSE. See the GNU General Public License for more details. | ||
2846 | 14 | # | ||
2847 | 15 | # You should have received a copy of the GNU General Public License along | ||
2848 | 16 | # with this program. If not, see <http://www.gnu.org/licenses/>. | ||
2849 | 17 | # | ||
2850 | 18 | # In addition, as a special exception, the copyright holders give | ||
2851 | 19 | # permission to link the code of portions of this program with the | ||
2852 | 20 | # OpenSSL library under certain conditions as described in each | ||
2853 | 21 | # individual source file, and distribute linked combinations | ||
2854 | 22 | # including the two. | ||
2855 | 23 | # You must obey the GNU General Public License in all respects | ||
2856 | 24 | # for all of the code used other than OpenSSL. If you modify | ||
2857 | 25 | # file(s) with this exception, you may extend this exception to your | ||
2858 | 26 | # version of the file(s), but you are not obligated to do so. If you | ||
2859 | 27 | # do not wish to do so, delete this exception statement from your | ||
2860 | 28 | # version. If you delete this exception statement from all source | ||
2861 | 29 | # files in the program, then also delete it here. | ||
2862 | 30 | |||
2863 | 31 | """Test the gsettings parser.""" | ||
2864 | 32 | |||
2865 | 33 | import logging | ||
2866 | 34 | |||
2867 | 35 | from twisted.trial.unittest import TestCase | ||
2868 | 36 | from ubuntuone.devtools.handlers import MementoHandler | ||
2869 | 37 | |||
2870 | 38 | from ubuntuone.utils import gsettings | ||
2871 | 39 | |||
2872 | 40 | TEMPLATE_GSETTINGS_OUTPUT = """\ | ||
2873 | 41 | org.gnome.system.proxy autoconfig-url '{autoconfig_url}' | ||
2874 | 42 | org.gnome.system.proxy ignore-hosts {ignore_hosts:s} | ||
2875 | 43 | org.gnome.system.proxy mode '{mode}' | ||
2876 | 44 | org.gnome.system.proxy.ftp host '{ftp_host}' | ||
2877 | 45 | org.gnome.system.proxy.ftp port {ftp_port} | ||
2878 | 46 | org.gnome.system.proxy.http authentication-password '{auth_password}' | ||
2879 | 47 | org.gnome.system.proxy.http authentication-user '{auth_user}' | ||
2880 | 48 | org.gnome.system.proxy.http host '{http_host}' | ||
2881 | 49 | org.gnome.system.proxy.http port {http_port} | ||
2882 | 50 | org.gnome.system.proxy.http use-authentication {http_use_auth} | ||
2883 | 51 | org.gnome.system.proxy.https host '{https_host}' | ||
2884 | 52 | org.gnome.system.proxy.https port {https_port} | ||
2885 | 53 | org.gnome.system.proxy.socks host '{socks_host}' | ||
2886 | 54 | org.gnome.system.proxy.socks port {socks_port} | ||
2887 | 55 | """ | ||
2888 | 56 | |||
2889 | 57 | BASE_GSETTINGS_VALUES = { | ||
2890 | 58 | "autoconfig_url": "", | ||
2891 | 59 | "ignore_hosts": ["localhost", "127.0.0.0/8"], | ||
2892 | 60 | "mode": "none", | ||
2893 | 61 | "ftp_host": "", | ||
2894 | 62 | "ftp_port": 0, | ||
2895 | 63 | "auth_password": "", | ||
2896 | 64 | "auth_user": "", | ||
2897 | 65 | "http_host": "", | ||
2898 | 66 | "http_port": 0, | ||
2899 | 67 | "http_use_auth": "false", | ||
2900 | 68 | "https_host": "", | ||
2901 | 69 | "https_port": 0, | ||
2902 | 70 | "socks_host": "", | ||
2903 | 71 | "socks_port": 0, | ||
2904 | 72 | } | ||
2905 | 73 | |||
2906 | 74 | |||
2907 | 75 | class ProxySettingsTestCase(TestCase): | ||
2908 | 76 | """Test the getting of the proxy settings.""" | ||
2909 | 77 | |||
2910 | 78 | def test_gsettings_cmdline_correct(self): | ||
2911 | 79 | """The command line used to get the proxy settings is the right one.""" | ||
2912 | 80 | expected = "gsettings list-recursively org.gnome.system.proxy".split() | ||
2913 | 81 | called = [] | ||
2914 | 82 | |||
2915 | 83 | def append_output(args): | ||
2916 | 84 | """Append the output and return some settings.""" | ||
2917 | 85 | called.append(args) | ||
2918 | 86 | return TEMPLATE_GSETTINGS_OUTPUT.format(**BASE_GSETTINGS_VALUES) | ||
2919 | 87 | |||
2920 | 88 | self.patch(gsettings.subprocess, "check_output", append_output) | ||
2921 | 89 | gsettings.get_proxy_settings() | ||
2922 | 90 | self.assertEqual(called[0], expected) | ||
2923 | 91 | |||
2924 | 92 | def test_gsettings_parser_none(self): | ||
2925 | 93 | """Test a parser of gsettings.""" | ||
2926 | 94 | expected = {} | ||
2927 | 95 | fake_output = TEMPLATE_GSETTINGS_OUTPUT.format(**BASE_GSETTINGS_VALUES) | ||
2928 | 96 | self.patch(gsettings.subprocess, "check_output", | ||
2929 | 97 | lambda _: fake_output) | ||
2930 | 98 | ps = gsettings.get_proxy_settings() | ||
2931 | 99 | self.assertEqual(ps, expected) | ||
2932 | 100 | |||
2933 | 101 | def _assert_parser_anonymous(self, scheme): | ||
2934 | 102 | """Assert the parsing of anonymous settings.""" | ||
2935 | 103 | template_values = dict(BASE_GSETTINGS_VALUES) | ||
2936 | 104 | expected_host = "expected_host" | ||
2937 | 105 | expected_port = 54321 | ||
2938 | 106 | expected = { | ||
2939 | 107 | "host": expected_host, | ||
2940 | 108 | "port": expected_port, | ||
2941 | 109 | } | ||
2942 | 110 | template_values.update({ | ||
2943 | 111 | "mode": "manual", | ||
2944 | 112 | scheme + "_host": expected_host, | ||
2945 | 113 | scheme + "_port": expected_port, | ||
2946 | 114 | }) | ||
2947 | 115 | fake_output = TEMPLATE_GSETTINGS_OUTPUT.format(**template_values) | ||
2948 | 116 | self.patch(gsettings.subprocess, "check_output", | ||
2949 | 117 | lambda _: fake_output) | ||
2950 | 118 | ps = gsettings.get_proxy_settings() | ||
2951 | 119 | self.assertEqual(ps[scheme], expected) | ||
2952 | 120 | |||
2953 | 121 | def test_gsettings_parser_http_anonymous(self): | ||
2954 | 122 | """Test a parser of gsettings.""" | ||
2955 | 123 | self._assert_parser_anonymous('http') | ||
2956 | 124 | |||
2957 | 125 | def test_gsettings_parser_https_anonymus(self): | ||
2958 | 126 | """Test a parser of gsettings.""" | ||
2959 | 127 | self._assert_parser_anonymous('https') | ||
2960 | 128 | |||
2961 | 129 | def test_gsettings_empty_ignore_hosts(self): | ||
2962 | 130 | """Missing values in the ignore hosts.""" | ||
2963 | 131 | troublesome_value = "@as []" | ||
2964 | 132 | template_values = dict(BASE_GSETTINGS_VALUES) | ||
2965 | 133 | template_values["ignore_hosts"] = troublesome_value | ||
2966 | 134 | fake_output = TEMPLATE_GSETTINGS_OUTPUT.format(**template_values) | ||
2967 | 135 | self.patch(gsettings.subprocess, "check_output", | ||
2968 | 136 | lambda _: fake_output) | ||
2969 | 137 | ps = gsettings.get_proxy_settings() | ||
2970 | 138 | self.assertEqual(ps, {}) | ||
2971 | 139 | |||
2972 | 140 | def test_gsettings_cannot_parse(self): | ||
2973 | 141 | """Some weird setting that cannot be parsed is logged with warning.""" | ||
2974 | 142 | memento = MementoHandler() | ||
2975 | 143 | memento.setLevel(logging.DEBUG) | ||
2976 | 144 | gsettings.logger.addHandler(memento) | ||
2977 | 145 | self.addCleanup(gsettings.logger.removeHandler, memento) | ||
2978 | 146 | |||
2979 | 147 | troublesome_value = "#bang" | ||
2980 | 148 | template_values = dict(BASE_GSETTINGS_VALUES) | ||
2981 | 149 | template_values["ignore_hosts"] = troublesome_value | ||
2982 | 150 | fake_output = TEMPLATE_GSETTINGS_OUTPUT.format(**template_values) | ||
2983 | 151 | self.patch(gsettings.subprocess, "check_output", | ||
2984 | 152 | lambda _: fake_output) | ||
2985 | 153 | ps = gsettings.get_proxy_settings() | ||
2986 | 154 | self.assertTrue(memento.check_warning(gsettings.CANNOT_PARSE_WARNING % | ||
2987 | 155 | troublesome_value)) | ||
2988 | 156 | self.assertEqual(ps, {}) | ||
2989 | 157 | |||
2990 | 158 | def test_gsettings_parser_http_authenticated(self): | ||
2991 | 159 | """Test a parser of gsettings.""" | ||
2992 | 160 | template_values = dict(BASE_GSETTINGS_VALUES) | ||
2993 | 161 | expected_host = "expected_host" | ||
2994 | 162 | expected_port = 54321 | ||
2995 | 163 | expected_user = "carlitos" | ||
2996 | 164 | expected_password = "very secret password" | ||
2997 | 165 | expected = { | ||
2998 | 166 | "host": expected_host, | ||
2999 | 167 | "port": expected_port, | ||
3000 | 168 | "username": expected_user, | ||
3001 | 169 | "password": expected_password, | ||
3002 | 170 | } | ||
3003 | 171 | template_values.update({ | ||
3004 | 172 | "mode": "manual", | ||
3005 | 173 | "http_host": expected_host, | ||
3006 | 174 | "http_port": expected_port, | ||
3007 | 175 | "auth_user": expected_user, | ||
3008 | 176 | "auth_password": expected_password, | ||
3009 | 177 | "http_use_auth": "true", | ||
3010 | 178 | }) | ||
3011 | 179 | fake_output = TEMPLATE_GSETTINGS_OUTPUT.format(**template_values) | ||
3012 | 180 | self.patch(gsettings.subprocess, "check_output", | ||
3013 | 181 | lambda _: fake_output) | ||
3014 | 182 | ps = gsettings.get_proxy_settings() | ||
3015 | 183 | self.assertEqual(ps["http"], expected) | ||
3016 | 184 | |||
3017 | 185 | def _assert_parser_authenticated_url(self, scheme): | ||
3018 | 186 | """Test a parser of gsettings with creds in the url.""" | ||
3019 | 187 | template_values = dict(BASE_GSETTINGS_VALUES) | ||
3020 | 188 | expected_host = "expected_host" | ||
3021 | 189 | expected_port = 54321 | ||
3022 | 190 | expected_user = "carlitos" | ||
3023 | 191 | expected_password = "very secret password" | ||
3024 | 192 | composed_url = '%s:%s@%s' % (expected_user, expected_password, | ||
3025 | 193 | expected_host) | ||
3026 | 194 | expected = { | ||
3027 | 195 | "host": expected_host, | ||
3028 | 196 | "port": expected_port, | ||
3029 | 197 | "username": expected_user, | ||
3030 | 198 | "password": expected_password, | ||
3031 | 199 | } | ||
3032 | 200 | template_values.update({ | ||
3033 | 201 | "mode": "manual", | ||
3034 | 202 | scheme + "_host": composed_url, | ||
3035 | 203 | scheme + "_port": expected_port, | ||
3036 | 204 | "http_use_auth": "false", | ||
3037 | 205 | }) | ||
3038 | 206 | fake_output = TEMPLATE_GSETTINGS_OUTPUT.format(**template_values) | ||
3039 | 207 | self.patch(gsettings.subprocess, "check_output", | ||
3040 | 208 | lambda _: fake_output) | ||
3041 | 209 | ps = gsettings.get_proxy_settings() | ||
3042 | 210 | self.assertEqual(ps[scheme], expected) | ||
3043 | 211 | |||
3044 | 212 | def test_gsettings_parser_http_authenticated_url(self): | ||
3045 | 213 | """Test a parser of gsettings with creds in the url.""" | ||
3046 | 214 | self._assert_parser_authenticated_url('http') | ||
3047 | 215 | |||
3048 | 216 | def test_gsettings_parser_https_authenticated_url(self): | ||
3049 | 217 | """Test a parser of gsettings with creds in the url.""" | ||
3050 | 218 | self._assert_parser_authenticated_url('https') | ||
3051 | 219 | |||
3052 | 220 | def test_gsettings_auth_over_url(self): | ||
3053 | 221 | """Test that the settings are more important that the url.""" | ||
3054 | 222 | template_values = dict(BASE_GSETTINGS_VALUES) | ||
3055 | 223 | expected_host = "expected_host" | ||
3056 | 224 | expected_port = 54321 | ||
3057 | 225 | expected_user = "carlitos" | ||
3058 | 226 | expected_password = "very secret password" | ||
3059 | 227 | composed_url = '%s:%s@%s' % ('user', 'random', | ||
3060 | 228 | expected_host) | ||
3061 | 229 | http_expected = { | ||
3062 | 230 | "host": expected_host, | ||
3063 | 231 | "port": expected_port, | ||
3064 | 232 | "username": expected_user, | ||
3065 | 233 | "password": expected_password, | ||
3066 | 234 | } | ||
3067 | 235 | template_values.update({ | ||
3068 | 236 | "mode": "manual", | ||
3069 | 237 | "http_host": composed_url, | ||
3070 | 238 | "http_port": expected_port, | ||
3071 | 239 | "auth_user": expected_user, | ||
3072 | 240 | "auth_password": expected_password, | ||
3073 | 241 | "http_use_auth": "true", | ||
3074 | 242 | }) | ||
3075 | 243 | fake_output = TEMPLATE_GSETTINGS_OUTPUT.format(**template_values) | ||
3076 | 244 | self.patch(gsettings.subprocess, "check_output", | ||
3077 | 245 | lambda _: fake_output) | ||
3078 | 246 | ps = gsettings.get_proxy_settings() | ||
3079 | 247 | self.assertEqual(ps["http"], http_expected) | ||
3080 | 248 | |||
3081 | 249 | def _assert_parser_empty_url(self, scheme): | ||
3082 | 250 | """Assert the parsing of an empty url.""" | ||
3083 | 251 | template_values = dict(BASE_GSETTINGS_VALUES) | ||
3084 | 252 | template_values.update({ | ||
3085 | 253 | "mode": "manual", | ||
3086 | 254 | scheme + "_host": '', | ||
3087 | 255 | scheme + "_port": 0, | ||
3088 | 256 | "http_use_auth": "false", | ||
3089 | 257 | }) | ||
3090 | 258 | fake_output = TEMPLATE_GSETTINGS_OUTPUT.format(**template_values) | ||
3091 | 259 | self.patch(gsettings.subprocess, "check_output", | ||
3092 | 260 | lambda _: fake_output) | ||
3093 | 261 | ps = gsettings.get_proxy_settings() | ||
3094 | 262 | self.assertNotIn(scheme, ps) | ||
3095 | 263 | |||
3096 | 264 | def test_gsettings_parser_empty_http_url(self): | ||
3097 | 265 | """Test when there is no http proxy set.""" | ||
3098 | 266 | self._assert_parser_empty_url('http') | ||
3099 | 267 | |||
3100 | 268 | def test_gsettings_parser_empty_https_url(self): | ||
3101 | 269 | """Test when there is no https proxy set.""" | ||
3102 | 270 | self._assert_parser_empty_url('https') | ||
3103 | 271 | |||
3104 | 272 | |||
3105 | 273 | class ParseProxyHostTestCase(TestCase): | ||
3106 | 274 | """Test the parsing of the domain.""" | ||
3107 | 275 | |||
3108 | 276 | def test_onlyhost(self): | ||
3109 | 277 | """Parse a host with no username or password.""" | ||
3110 | 278 | sample = "hostname" | ||
3111 | 279 | hostname, username, password = gsettings.parse_proxy_host(sample) | ||
3112 | 280 | self.assertEqual(username, None) | ||
3113 | 281 | self.assertEqual(password, None) | ||
3114 | 282 | self.assertEqual(hostname, "hostname") | ||
3115 | 283 | |||
3116 | 284 | def test_user_and_host(self): | ||
3117 | 285 | """Parse host just with the username.""" | ||
3118 | 286 | sample = "username@hostname" | ||
3119 | 287 | hostname, username, password = gsettings.parse_proxy_host(sample) | ||
3120 | 288 | self.assertEqual(username, "username") | ||
3121 | 289 | self.assertEqual(password, None) | ||
3122 | 290 | self.assertEqual(hostname, "hostname") | ||
3123 | 291 | |||
3124 | 292 | def test_user_pass_and_host(self): | ||
3125 | 293 | """Test parsing a host with a username and password.""" | ||
3126 | 294 | sample = "username:password@hostname" | ||
3127 | 295 | hostname, username, password = gsettings.parse_proxy_host(sample) | ||
3128 | 296 | self.assertEqual(username, "username") | ||
3129 | 297 | self.assertEqual(password, "password") | ||
3130 | 298 | self.assertEqual(hostname, "hostname") | ||
3131 | 299 | |||
3132 | 300 | def test_username_with_at(self): | ||
3133 | 301 | """Test parsing the host with a username with @.""" | ||
3134 | 302 | sample = "username@company.com:password@hostname" | ||
3135 | 303 | hostname, username, password = gsettings.parse_proxy_host(sample) | ||
3136 | 304 | self.assertEqual(username, "username@company.com") | ||
3137 | 305 | self.assertEqual(password, "password") | ||
3138 | 306 | self.assertEqual(hostname, "hostname") | ||
3139 | 307 | |||
3140 | 308 | def test_username_with_at_nopass(self): | ||
3141 | 309 | """Test parsing the host without a password.""" | ||
3142 | 310 | sample = "username@company.com@hostname" | ||
3143 | 311 | hostname, username, password = gsettings.parse_proxy_host(sample) | ||
3144 | 312 | self.assertEqual(username, "username@company.com") | ||
3145 | 313 | self.assertEqual(password, None) | ||
3146 | 314 | self.assertEqual(hostname, "hostname") | ||
3147 | 315 | |||
3148 | 316 | def test_user_pass_with_colon_and_host(self): | ||
3149 | 317 | """Test parsing the host with a password that contains :.""" | ||
3150 | 318 | sample = "username:pass:word@hostname" | ||
3151 | 319 | hostname, username, password = gsettings.parse_proxy_host(sample) | ||
3152 | 320 | self.assertEqual(username, "username") | ||
3153 | 321 | self.assertEqual(password, "pass:word") | ||
3154 | 322 | self.assertEqual(hostname, "hostname") |
Thanks!