[Telepathy-commits] [telepathy-salut/master] 1-1 stream tubes: new test: create a tube between 2 salut accounts and send some data through it in both directions

Alban Crequy alban.crequy at collabora.co.uk
Tue Nov 25 03:59:41 PST 2008


---
 tests/twisted/Makefile.am             |    1 +
 tests/twisted/avahi/test-two-tubes.py |  142 +++++++++++++++++++++++++++++++++
 2 files changed, 143 insertions(+), 0 deletions(-)
 create mode 100644 tests/twisted/avahi/test-two-tubes.py

diff --git a/tests/twisted/Makefile.am b/tests/twisted/Makefile.am
index 8c3415f..60e731a 100644
--- a/tests/twisted/Makefile.am
+++ b/tests/twisted/Makefile.am
@@ -9,6 +9,7 @@ TWISTED_AVAHI_TESTS = \
 	avahi/test-ichat-composing.py \
 	avahi/test-ichat-incoming-msg.py \
 	avahi/test-tube.py \
+	avahi/test-two-tubes.py \
 	avahi/test-tube-close.py
 
 TWISTED_AVAHI_OLPC_TESTS = \
diff --git a/tests/twisted/avahi/test-two-tubes.py b/tests/twisted/avahi/test-two-tubes.py
new file mode 100644
index 0000000..8a6af22
--- /dev/null
+++ b/tests/twisted/avahi/test-two-tubes.py
@@ -0,0 +1,142 @@
+from saluttest import exec_test, make_connection
+from avahitest import AvahiAnnouncer, AvahiListener
+from avahitest import get_host_name
+import avahi
+import dbus
+import os
+import errno
+import string
+
+from xmppstream import setup_stream_listener, connect_to_stream
+from servicetest import make_channel_proxy, Event
+
+from twisted.words.xish import xpath, domish
+from twisted.internet.protocol import Factory, Protocol, ClientCreator
+from twisted.internet import reactor
+
+CHANNEL_TYPE_TUBES = "org.freedesktop.Telepathy.Channel.Type.Tubes"
+HT_CONTACT = 1
+HT_CONTACT_LIST = 3
+TEXT_MESSAGE_TYPE_NORMAL = dbus.UInt32(0)
+SOCKET_ADDRESS_TYPE_UNIX = dbus.UInt32(0)
+SOCKET_ADDRESS_TYPE_IPV4 = dbus.UInt32(2)
+SOCKET_ACCESS_CONTROL_LOCALHOST = dbus.UInt32(0)
+
+sample_parameters = dbus.Dictionary({
+    's': 'hello',
+    'ay': dbus.ByteArray('hello'),
+    'u': dbus.UInt32(123),
+    'i': dbus.Int32(-123),
+    }, signature='sv')
+
+test_string = "This string travels on a tube !"
+
+def test(q, bus, conn):
+
+    # define a basic tcp server that echoes what the client says, but with
+    # swapcase
+    class TrivialServer(Protocol):
+        def dataReceived(self, data):
+            self.transport.write(string.swapcase(data))
+            e = Event('server-data-received', service = self, data = data)
+            q.append(e)
+
+    # define a basic tcp client
+    class ClientGreeter(Protocol):
+        def dataReceived(self, data):
+            e = Event('client-data-received', service = self, data = data)
+            q.append(e)
+    def client_connected_cb(p):
+        e = Event('client-connected', transport = p.transport)
+        q.append(e)
+
+    # create the server
+    factory = Factory()
+    factory.protocol = TrivialServer
+    server_socket_address = os.getcwd() + '/stream'
+    try:
+        os.remove(server_socket_address)
+    except OSError, e:
+        if e.errno != errno.ENOENT:
+            raise
+    l = reactor.listenUNIX(server_socket_address, factory)
+
+    # first connection
+    conn.Connect()
+    q.expect('dbus-signal', signal='StatusChanged', args=[0L, 0L])
+
+    # second connection
+    conn2_params = {
+        'published-name': 'testsuite2',
+        'first-name': 'test2',
+        'last-name': 'suite2',
+        }
+    conn2 = make_connection(bus, q.append, conn2_params)
+    conn2.Connect()
+    q.expect('dbus-signal', signal='StatusChanged', args=[0L, 0L])
+    contact2_name = "testsuite2" + "@" + get_host_name()
+
+    publish_handle = conn.RequestHandles(HT_CONTACT_LIST, ["publish"])[0]
+    publish = conn.RequestChannel(
+        "org.freedesktop.Telepathy.Channel.Type.ContactList",
+        HT_CONTACT_LIST, publish_handle, False)
+
+    handle = 0
+    # Wait until the record shows up in publish
+    while handle == 0:
+        e = q.expect('dbus-signal', signal='MembersChanged', path=publish)
+        for h in e.args[1]:
+            name = conn.InspectHandles(HT_CONTACT, [h])[0]
+            if name == contact2_name:
+                handle = h
+
+    t = conn.RequestChannel(CHANNEL_TYPE_TUBES, HT_CONTACT, handle,
+        True)
+    contact1_tubes_channel = make_channel_proxy(conn, t, "Channel.Type.Tubes")
+
+    tube_id = contact1_tubes_channel.OfferStreamTube("http", sample_parameters,
+            SOCKET_ADDRESS_TYPE_UNIX, dbus.ByteArray(server_socket_address),
+            SOCKET_ACCESS_CONTROL_LOCALHOST, "")
+
+    contact2_channeltype = None
+    while contact2_channeltype == None:
+        e = q.expect('dbus-signal', signal='NewChannel')
+        if (e.args[1] == CHANNEL_TYPE_TUBES) and (e.path.endswith("testsuite2") == True):
+            contact2_objpath = e.args[0]
+            contact2_channeltype = e.args[1]
+
+    contact2_tubes_channel = make_channel_proxy(conn2, contact2_objpath, "Channel.Type.Tubes")
+
+    contact2_tubes = contact2_tubes_channel.ListTubes()
+    assert len(contact2_tubes) == 1
+    contact2_tube = contact2_tubes[0]
+    assert contact2_tube[0] is not None # tube id
+    assert contact2_tube[1] is not None # initiator
+    assert contact2_tube[2] == 1 # type = stream tube
+    assert contact2_tube[3] == 'http' # service = http
+    assert contact2_tube[4] is not None # parameters
+    assert contact2_tube[5] == 0, contact2_tube[5] # status = local pending
+
+    unix_socket_adr= contact2_tubes_channel.AcceptStreamTube(
+            contact2_tube[0], 0, 0, '', byte_arrays=True)
+
+    client = ClientCreator(reactor, ClientGreeter)
+    client.connectUNIX(unix_socket_adr).addCallback(client_connected_cb)
+
+    e = q.expect('client-connected')
+    client_transport = e.transport
+    client_transport.write(test_string)
+
+    e = q.expect('server-data-received')
+    assert e.data == test_string
+
+    e = q.expect('client-data-received')
+    assert e.data == string.swapcase(test_string)
+
+    # Close the tube propertly
+    contact1_tubes_channel.CloseTube(tube_id)
+    conn.Disconnect()
+    conn2.Disconnect()
+
+if __name__ == '__main__':
+    exec_test(test)
-- 
1.5.6.5




More information about the Telepathy-commits mailing list