Merge lp:~alecu/ubuntuone-client/proxy-tunnel-fix into lp:ubuntuone-client

Proposed by Alejandro J. Cura on 2012-03-14
Status: Merged
Approved by: Alejandro J. Cura on 2012-03-15
Approved revision: 1234
Merged at revision: 1209
Proposed branch: lp:~alecu/ubuntuone-client/proxy-tunnel-fix
Merge into: lp:ubuntuone-client
Prerequisite: lp:~alecu/ubuntuone-client/proxy-tunnel-webcalls
Diff against target: 255 lines (+101/-12)
5 files modified
tests/proxy/test_tunnel_client.py (+54/-0)
tests/proxy/test_tunnel_server.py (+20/-6)
ubuntuone/proxy/logger.py (+2/-3)
ubuntuone/proxy/tunnel_client.py (+12/-0)
ubuntuone/proxy/tunnel_server.py (+13/-3)
To merge this branch: bzr merge lp:~alecu/ubuntuone-client/proxy-tunnel-fix
Reviewer Review Type Date Requested Status
Diego Sarmentero (community) Approve on 2012-03-15
Eric Casteleijn (community) 2012-03-14 Approve on 2012-03-14
Review via email: mp+97475@code.launchpad.net

Commit Message

QNetwork must use the proxy built; forward disconnections in the tunnel client.

Description of the Change

QNetwork must use the proxy built; forward disconnections in the tunnel client.

To post a comment you must log in.
Eric Casteleijn (thisfred) wrote :

Looking good

review: Approve
Diego Sarmentero (diegosarmentero) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'tests/proxy/test_tunnel_client.py'
2--- tests/proxy/test_tunnel_client.py 2012-03-10 00:39:26 +0000
3+++ tests/proxy/test_tunnel_client.py 2012-03-14 17:56:29 +0000
4@@ -104,6 +104,60 @@
5 self.assertEqual(self.other_proto.content, expected)
6
7
8+class FakeOtherFactory(object):
9+ """A fake factory."""
10+
11+ def __init__(self):
12+ """Initialize this fake."""
13+ self.started_called = None
14+ self.failed_called = None
15+ self.lost_called = None
16+
17+ def startedConnecting(self, *args):
18+ """Store the call."""
19+ self.started_called = args
20+
21+ def clientConnectionFailed(self, *args):
22+ """Store the call."""
23+ self.failed_called = args
24+
25+ def clientConnectionLost(self, *args):
26+ """Store the call."""
27+ self.lost_called = args
28+
29+
30+class TunnelClientFactoryTestCase(TestCase):
31+ """Tests for the TunnelClientFactory."""
32+
33+ def test_forwards_started(self):
34+ """The factory forwards the startedConnecting call."""
35+ fake_other_factory = FakeOtherFactory()
36+ tcf = tunnel_client.TunnelClientFactory(None, None, fake_other_factory)
37+ fake_connector = object()
38+ tcf.startedConnecting(fake_connector)
39+ self.assertEqual(fake_other_factory.started_called, (fake_connector,))
40+
41+ def test_forwards_failed(self):
42+ """The factory forwards the clientConnectionFailed call."""
43+ fake_reason = object()
44+ fake_other_factory = FakeOtherFactory()
45+ tcf = tunnel_client.TunnelClientFactory(None, None, fake_other_factory)
46+ fake_connector = object()
47+ tcf.clientConnectionFailed(fake_connector, fake_reason)
48+ self.assertEqual(fake_other_factory.failed_called,
49+ (fake_connector, fake_reason))
50+
51+ def test_forwards_lost(self):
52+ """The factory forwards the clientConnectionLost call."""
53+ fake_reason = object()
54+ fake_other_factory = FakeOtherFactory()
55+ tcf = tunnel_client.TunnelClientFactory(None, None, fake_other_factory)
56+ fake_connector = object()
57+ tcf.clientConnectionLost(fake_connector, fake_reason)
58+ self.assertEqual(fake_other_factory.lost_called,
59+ (fake_connector, fake_reason))
60+
61+
62 class TunnelClientTestCase(SquidTestCase):
63 """Test the client for the tunnel."""
64
65
66=== modified file 'tests/proxy/test_tunnel_server.py'
67--- tests/proxy/test_tunnel_server.py 2012-03-09 22:36:14 +0000
68+++ tests/proxy/test_tunnel_server.py 2012-03-14 17:56:29 +0000
69@@ -42,8 +42,10 @@
70 )
71
72 FAKE_SETTINGS = {
73- "host": "myhost",
74- "port": "8888",
75+ "http": {
76+ "host": "myhost",
77+ "port": 8888,
78+ }
79 }
80
81 SAMPLE_HOST = "samplehost.com"
82@@ -384,13 +386,21 @@
83 return [self]
84
85
86-class IsProxyEnabledTestCase(TestCase):
87- """Tests for the is_proxy_enabled function."""
88+class CheckProxyEnabledTestCase(TestCase):
89+ """Tests for the check_proxy_enabled function."""
90+
91+ @defer.inlineCallbacks
92+ def setUp(self):
93+ """Initialize this testcase."""
94+ yield super(CheckProxyEnabledTestCase, self).setUp()
95+ self.app_proxy = []
96
97 def _assert_proxy_state(self, platform, state, assertion):
98 """Assert the proxy is in a given state."""
99+ self.patch(tunnel_server.QNetworkProxy, "setApplicationProxy",
100+ lambda proxy: self.app_proxy.append(proxy))
101 self.patch(tunnel_server.sys, "platform", platform)
102- ret = tunnel_server.is_proxy_enabled(SAMPLE_HOST, str(SAMPLE_PORT))
103+ ret = tunnel_server.check_proxy_enabled(SAMPLE_HOST, str(SAMPLE_PORT))
104 self.assertTrue(ret == state, assertion)
105
106 def _assert_proxy_enabled(self, platform):
107@@ -406,23 +416,27 @@
108 self.patch(tunnel_server.gsettings, "get_proxy_settings",
109 lambda: FAKE_SETTINGS)
110 self._assert_proxy_enabled("linux3")
111+ self.assertEqual(len(self.app_proxy), 1)
112
113 def test_platform_linux_disabled(self):
114 """Tests for the linux platform with proxies disabled."""
115 self.patch(tunnel_server.gsettings, "get_proxy_settings", lambda: {})
116 self._assert_proxy_disabled("linux3")
117+ self.assertEqual(len(self.app_proxy), 0)
118
119 def test_platform_other_enabled(self):
120 """Tests for any other platform with proxies enabled."""
121 fake_netproxfact = FakeNetworkProxyFactoryClass(True)
122 self.patch(tunnel_server, "QNetworkProxyFactory", fake_netproxfact)
123 self._assert_proxy_enabled("windows 1.0")
124+ self.assertEqual(len(self.app_proxy), 0)
125
126 def test_platform_other_disabled(self):
127 """Tests for any other platform with proxies disabled."""
128 fake_netproxfact = FakeNetworkProxyFactoryClass(False)
129 self.patch(tunnel_server, "QNetworkProxyFactory", fake_netproxfact)
130 self._assert_proxy_disabled("windows 1.0")
131+ self.assertEqual(len(self.app_proxy), 0)
132
133
134 class FakeQCoreApp(object):
135@@ -461,7 +475,7 @@
136 self.called.append(args)
137 return self.proxies_enabled
138
139- self.patch(tunnel_server, "is_proxy_enabled", fake_is_proxy_enabled)
140+ self.patch(tunnel_server, "check_proxy_enabled", fake_is_proxy_enabled)
141 self.fake_stdout = StringIO()
142 self.patch(tunnel_server.sys, "stdout", self.fake_stdout)
143 self.patch(tunnel_server, "QCoreApplication", FakeQCoreApp)
144
145=== modified file 'ubuntuone/proxy/logger.py'
146--- ubuntuone/proxy/logger.py 2012-02-23 23:58:33 +0000
147+++ ubuntuone/proxy/logger.py 2012-03-14 17:56:29 +0000
148@@ -20,7 +20,6 @@
149 import os
150
151 from ubuntuone.logger import (
152- _DEBUG_LOG_LEVEL,
153 basic_formatter,
154 CustomRotatingFileHandler,
155 )
156@@ -30,8 +29,8 @@
157
158 LOGFILENAME = os.path.join(ubuntuone_log_dir, 'proxy.log')
159 logger = logging.getLogger("ubuntuone.proxy")
160-logger.setLevel(_DEBUG_LOG_LEVEL)
161+logger.setLevel(logging.DEBUG)
162 handler = CustomRotatingFileHandler(filename=LOGFILENAME)
163 handler.setFormatter(basic_formatter)
164-handler.setLevel(_DEBUG_LOG_LEVEL)
165+handler.setLevel(logging.DEBUG)
166 logger.addHandler(handler)
167
168=== modified file 'ubuntuone/proxy/tunnel_client.py'
169--- ubuntuone/proxy/tunnel_client.py 2012-03-10 00:39:26 +0000
170+++ ubuntuone/proxy/tunnel_client.py 2012-03-14 17:56:29 +0000
171@@ -78,6 +78,18 @@
172 self.other_factory = other_factory
173 self.context_factory = context_factory
174
175+ def startedConnecting(self, connector):
176+ """Forward this call to the other factory."""
177+ self.other_factory.startedConnecting(connector)
178+
179+ def clientConnectionFailed(self, connector, reason):
180+ """Forward this call to the other factory."""
181+ self.other_factory.clientConnectionFailed(connector, reason)
182+
183+ def clientConnectionLost(self, connector, reason):
184+ """Forward this call to the other factory."""
185+ self.other_factory.clientConnectionLost(connector, reason)
186+
187
188 class TunnelClient(object):
189 """A client for the proxy tunnel."""
190
191=== modified file 'ubuntuone/proxy/tunnel_server.py'
192--- ubuntuone/proxy/tunnel_server.py 2012-03-09 22:36:14 +0000
193+++ ubuntuone/proxy/tunnel_server.py 2012-03-14 17:56:29 +0000
194@@ -93,6 +93,7 @@
195 """When connected, send all pending data."""
196 self.connected_d.callback(None)
197 for d in self.buffered_data:
198+ logger.debug("writing remote: %d bytes", len(d))
199 super(RemoteSocket, self).write(d)
200 self.buffered_data = []
201
202@@ -104,6 +105,7 @@
203 def write(self, data):
204 """Write data to the remote end, buffering if not connected."""
205 if self.state() == QAbstractSocket.ConnectedState:
206+ logger.debug("writing remote: %d bytes", len(data))
207 super(RemoteSocket, self).write(data)
208 else:
209 self.buffered_data.append(data)
210@@ -218,6 +220,7 @@
211
212 def write(self, data):
213 """Data available on the remote end. Bring it back."""
214+ logger.debug("writing local: %d bytes", len(data))
215 self.local_socket.write(data)
216
217 def loseConnection(self):
218@@ -227,6 +230,7 @@
219
220 def local_disconnected(self):
221 """The local end disconnected."""
222+ logger.debug("The local socket got disconnected.")
223 # TODO: handle this case in an upcoming branch
224
225
226@@ -258,12 +262,18 @@
227 return self.server.serverPort()
228
229
230-def is_proxy_enabled(host, port):
231+def check_proxy_enabled(host, port):
232 """Check if the proxy is enabled."""
233 port = int(port)
234 if sys.platform.startswith("linux"):
235 settings = gsettings.get_proxy_settings()
236- return len(settings) > 0
237+ enabled = len(settings) > 0
238+ if enabled:
239+ proxy = build_proxy(settings["http"])
240+ QNetworkProxy.setApplicationProxy(proxy)
241+ else:
242+ logger.info("Proxy is disabled.")
243+ return enabled
244 else:
245 query = QNetworkProxyQuery(host, port)
246 proxies = QNetworkProxyFactory.systemProxyForQuery(query)
247@@ -272,7 +282,7 @@
248
249 def main(argv):
250 """The main function for the tunnel server."""
251- if not is_proxy_enabled(*argv[1:]):
252+ if not check_proxy_enabled(*argv[1:]):
253 sys.stdout.write("Proxy not enabled.")
254 sys.stdout.flush()
255 else:

Subscribers

People subscribed via source and target branches