[telepathy-gabble/telepathy-gabble-0.8] test-fallback-socks5-proxy.py: factor out test so we can easily re-use part of it

Guillaume Desmottes guillaume.desmottes at collabora.co.uk
Thu Nov 26 06:35:50 PST 2009


---
 tests/twisted/test-fallback-socks5-proxy.py |   68 ++++++++++++++------------
 1 files changed, 37 insertions(+), 31 deletions(-)

diff --git a/tests/twisted/test-fallback-socks5-proxy.py b/tests/twisted/test-fallback-socks5-proxy.py
index 3b74b18..52d4183 100644
--- a/tests/twisted/test-fallback-socks5-proxy.py
+++ b/tests/twisted/test-fallback-socks5-proxy.py
@@ -9,32 +9,20 @@ import ns
 import constants as cs
 from bytestream import create_from_si_offer, BytestreamS5B
 
-def test(q, bus, conn, stream):
-    conn.Connect()
+proxy_query_events = [
+    EventPattern('stream-iq', to='fallback1-proxy.localhost', iq_type='get', query_ns=ns.BYTESTREAMS),
+    EventPattern('stream-iq', to='fallback2-proxy.localhost', iq_type='get', query_ns=ns.BYTESTREAMS)]
 
-    proxy_query_events = [
-        EventPattern('stream-iq', to='fallback1-proxy.localhost', iq_type='get', query_ns=ns.BYTESTREAMS),
-        EventPattern('stream-iq', to='fallback2-proxy.localhost', iq_type='get', query_ns=ns.BYTESTREAMS)]
+proxy_port = {'fallback1-proxy.localhost': '12345', 'fallback2-proxy.localhost': '6789'}
 
+def connect_and_announce_alice(q, bus, conn, stream):
     q.forbid_events(proxy_query_events)
 
+    conn.Connect()
+
     q.expect('dbus-signal', signal='StatusChanged',
             args=[cs.CONN_STATUS_CONNECTED, cs.CSR_REQUESTED])
 
-    proxy_port = {'fallback1-proxy.localhost': '12345', 'fallback2-proxy.localhost': '6789'}
-
-    def send_socks5_reply(iq):
-        jid = iq['to']
-        port = proxy_port[jid]
-
-        reply = elem_iq(stream, 'result', id=iq['id'], from_=jid)(
-            elem(ns.BYTESTREAMS, 'query')(
-                elem('streamhost', jid=jid, host='127.0.0.1', port=port)()))
-        stream.send(reply)
-
-    # Offer a private D-Bus tube just to check if the proxy is present in the
-    # SOCKS5 offer
-
     # Send Alice's presence
     caps =  { 'ext': '', 'ver': '0.0.0',
         'node': 'http://example.com/fake-client0' }
@@ -49,6 +37,31 @@ def test(q, bus, conn, stream):
 
     q.unforbid_events(proxy_query_events)
 
+def send_socks5_reply(stream, iq):
+    jid = iq['to']
+    port = proxy_port[jid]
+
+    reply = elem_iq(stream, 'result', id=iq['id'], from_=jid)(
+        elem(ns.BYTESTREAMS, 'query')(
+            elem('streamhost', jid=jid, host='127.0.0.1', port=port)()))
+
+    stream.send(reply)
+
+def check_socks5_stanza(stanza):
+    tmp = proxy_port.copy()
+    nodes = xpath.queryForNodes('/iq/query/streamhost', stanza)
+    for node in nodes:
+        if node['jid'] in tmp:
+            assert node['host'] == '127.0.0.1'
+            assert node['port'] == tmp.pop(node['jid'])
+    assert tmp == {}
+
+def offer_dbus_tube(q, bus, conn, stream):
+    connect_and_announce_alice(q, bus, conn, stream)
+
+    # Offer a private D-Bus tube just to check if the proxy is present in the
+    # SOCKS5 offer
+
     path, props = conn.Requests.CreateChannel({cs.CHANNEL_TYPE: cs.CHANNEL_TYPE_DBUS_TUBE,
         cs.TARGET_HANDLE_TYPE: cs.HT_CONTACT,
         cs.TARGET_ID: 'alice at localhost',
@@ -57,8 +70,8 @@ def test(q, bus, conn, stream):
     # Proxy queries are send when creating the channel
     e1, e2 = q.expect_many(*proxy_query_events)
 
-    send_socks5_reply(e1.stanza)
-    send_socks5_reply(e2.stanza)
+    send_socks5_reply(stream, e1.stanza)
+    send_socks5_reply(stream, e2.stanza)
 
     tube_chan = bus.get_object(conn.bus_name, path)
     dbus_tube_iface = dbus.Interface(tube_chan, cs.CHANNEL_TYPE_DBUS_TUBE)
@@ -76,15 +89,8 @@ def test(q, bus, conn, stream):
     stream.send(result)
 
     e = q.expect('stream-iq', to='alice at localhost/Test')
-
-    found = False
-    nodes = xpath.queryForNodes('/iq/query/streamhost', e.stanza)
-    for node in nodes:
-        if node['jid'] in proxy_port:
-            assert node['host'] == '127.0.0.1'
-            assert node['port'] == proxy_port.pop(node['jid'])
-    assert proxy_port == {}
+    check_socks5_stanza(e.stanza)
 
 if __name__ == '__main__':
-    exec_test(test, params={'fallback-socks5-proxies':
-        ['fallback1-proxy.localhost', 'fallback2-proxy.localhost']})
+    params = {'fallback-socks5-proxies': ['fallback1-proxy.localhost', 'fallback2-proxy.localhost']}
+    exec_test(offer_dbus_tube, params=params)
-- 
1.5.6.5




More information about the telepathy-commits mailing list