[Telepathy-commits] [telepathy-gabble/master] offer-private-dbus-tube-ibb.py: use BytestreamIBB
Guillaume Desmottes
guillaume.desmottes at collabora.co.uk
Fri Feb 27 06:04:52 PST 2009
---
tests/twisted/tubes/offer-private-dbus-tube-ibb.py | 154 +++++++++-----------
1 files changed, 67 insertions(+), 87 deletions(-)
diff --git a/tests/twisted/tubes/offer-private-dbus-tube-ibb.py b/tests/twisted/tubes/offer-private-dbus-tube-ibb.py
index 4cef37e..ed10908 100644
--- a/tests/twisted/tubes/offer-private-dbus-tube-ibb.py
+++ b/tests/twisted/tubes/offer-private-dbus-tube-ibb.py
@@ -1,18 +1,17 @@
"""Test D-Bus private tube support"""
-import base64
-
import dbus
from dbus.connection import Connection
from dbus.lowlevel import SignalMessage
-from servicetest import call_async, EventPattern
-from gabbletest import exec_test, make_result_iq, acknowledge_iq, sync_stream
-from constants import *
+from servicetest import call_async, EventPattern, unwrap
+from gabbletest import exec_test, make_result_iq, sync_stream
+import constants as cs
import tubetestutil as t
from twisted.words.xish import domish, xpath
-from ns import DISCO_INFO, TUBES, SI, FEATURE_NEG, IBB, MUC_BYTESTREAM, X_DATA
+import ns
+from bytestream import parse_si_offer, create_si_reply, BytestreamIBB
sample_parameters = dbus.Dictionary({
's': 'hello',
@@ -49,14 +48,18 @@ def make_caps_disco_reply(stream, req, features):
def alice_accepts_tube(q, stream, iq_event, dbus_tube_id):
iq = iq_event.stanza
+ profile, dbus_stream_id, bytestreams = parse_si_offer(iq)
+
+ assert profile == ns.TUBES
+ assert bytestreams == [ns.BYTESTREAMS, ns.IBB]
+
tube_nodes = xpath.queryForNodes('/iq/si/tube[@xmlns="%s"]'
- % TUBES, iq)
+ % ns.TUBES, iq)
assert len(tube_nodes) == 1
tube = tube_nodes[0]
tube['type'] = 'dbus'
assert tube['initiator'] == 'test at localhost'
assert tube['service'] == 'com.example.TestCase'
- dbus_stream_id = tube['stream-id']
assert tube['id'] == str(dbus_tube_id)
params = {}
@@ -70,49 +73,29 @@ def alice_accepts_tube(q, stream, iq_event, dbus_tube_id):
'u': ('uint', '123'),
}
- # Alice accepts the tube and wants to use IBB
- result = make_result_iq(stream, iq)
- result['from'] = 'alice at localhost/Test'
- si = result.firstChildElement()
- feature = si.addElement((FEATURE_NEG, 'feature'))
- x = feature.addElement((X_DATA, 'x'))
- x['type'] = 'submit'
- field = x.addElement((None, 'field'))
- field['var'] = 'stream-method'
- value = field.addElement((None, 'value'))
- value.addContent(IBB)
- si.addElement((TUBES, 'tube'))
+ # Alice accepts the tube
+ bytestream = BytestreamIBB(stream, q, dbus_stream_id, 'test at localhost/Resource',
+ 'alice at localhost/Test', False)
+
+ result, si = create_si_reply(stream, iq, bytestream.initiator, bytestream.get_ns())
+ si.addElement((ns.TUBES, 'tube'))
stream.send(result)
- # wait IBB init IQ
- event = q.expect('stream-iq', to='alice at localhost/Test',
- query_name='open', query_ns=IBB)
- iq = event.stanza
- open = xpath.queryForNodes('/iq/open', iq)[0]
- assert open['sid'] == dbus_stream_id
+ bytestream.wait_bytestream_open()
- # open the IBB bytestream
- reply = make_result_iq(stream, iq)
- stream.send(reply)
+ q.expect('dbus-signal', signal='TubeStateChanged',
+ args=[dbus_tube_id, cs.TUBE_STATE_OPEN])
- return dbus_stream_id
+ return bytestream
-def send_dbus_message_to_alice(q, stream, dbus_tube_adr, dbus_stream_id):
+def send_dbus_message_to_alice(q, stream, dbus_tube_adr, bytestream):
tube = Connection(dbus_tube_adr)
signal = SignalMessage('/', 'foo.bar', 'baz')
signal.append(42, signature='u')
tube.send_message(signal)
- event = q.expect('stream-message', to='alice at localhost/Test')
- message = event.stanza
+ binary = bytestream.get_data()
- data_nodes = xpath.queryForNodes('/message/data[@xmlns="%s"]' % IBB,
- message)
- assert data_nodes is not None
- assert len(data_nodes) == 1
- ibb_data = data_nodes[0]
- assert ibb_data['sid'] == dbus_stream_id
- binary = base64.b64decode(str(ibb_data))
# little and big endian versions of: SIGNAL, NO_REPLY, protocol v1,
# 4-byte payload
assert binary.startswith('l\x04\x01\x01' '\x04\x00\x00\x00') or \
@@ -125,19 +108,19 @@ def send_dbus_message_to_alice(q, stream, dbus_tube_adr, dbus_stream_id):
def offer_old_dbus_tube(q, bus, conn, stream, self_handle, alice_handle):
# request tubes channel (old API)
- tubes_path = conn.RequestChannel(CHANNEL_TYPE_TUBES, HT_CONTACT,
+ tubes_path = conn.RequestChannel(cs.CHANNEL_TYPE_TUBES, cs.HT_CONTACT,
alice_handle, True)
tubes_chan = bus.get_object(conn.bus_name, tubes_path)
- tubes_iface = dbus.Interface(tubes_chan, CHANNEL_TYPE_TUBES)
- tubes_chan_iface = dbus.Interface(tubes_chan, CHANNEL)
+ tubes_iface = dbus.Interface(tubes_chan, cs.CHANNEL_TYPE_TUBES)
+ tubes_chan_iface = dbus.Interface(tubes_chan, cs.CHANNEL)
# Exercise basic Channel Properties from spec 0.17.7
- channel_props = tubes_chan.GetAll(CHANNEL, dbus_interface=PROPERTIES_IFACE)
+ channel_props = tubes_chan.GetAll(cs.CHANNEL, dbus_interface=cs.PROPERTIES_IFACE)
assert channel_props.get('TargetHandle') == alice_handle,\
(channel_props.get('TargetHandle'), alice_handle)
- assert channel_props.get('TargetHandleType') == HT_CONTACT,\
+ assert channel_props.get('TargetHandleType') == cs.HT_CONTACT,\
channel_props.get('TargetHandleType')
- assert channel_props.get('ChannelType') == CHANNEL_TYPE_TUBES,\
+ assert channel_props.get('ChannelType') == cs.CHANNEL_TYPE_TUBES,\
channel_props.get('ChannelType')
assert 'Interfaces' in channel_props, channel_props
assert channel_props['Interfaces'] == [], channel_props['Interfaces']
@@ -162,24 +145,21 @@ def offer_old_dbus_tube(q, bus, conn, stream, self_handle, alice_handle):
assert new_tube_event.args[2] == 0 # DBUS
assert new_tube_event.args[3] == 'com.example.TestCase'
assert new_tube_event.args[4] == sample_parameters
- assert new_tube_event.args[5] == TUBE_STATE_REMOTE_PENDING
+ assert new_tube_event.args[5] == cs.TUBE_STATE_REMOTE_PENDING
# handle offer_return_event
assert offer_return_event.value[0] == dbus_tube_id
tubes = tubes_iface.ListTubes(byte_arrays=True)
assert len(tubes) == 1
- expected_tube = (dbus_tube_id, self_handle, TUBE_TYPE_DBUS,
- 'com.example.TestCase', sample_parameters, TUBE_STATE_REMOTE_PENDING)
+ expected_tube = (dbus_tube_id, self_handle, cs.TUBE_TYPE_DBUS,
+ 'com.example.TestCase', sample_parameters, cs.TUBE_STATE_REMOTE_PENDING)
t.check_tube_in_tubes(expected_tube, tubes)
- dbus_stream_id = alice_accepts_tube(q, stream, iq_event, dbus_tube_id)
-
- q.expect('dbus-signal', signal='TubeStateChanged', interface=CHANNEL_TYPE_TUBES,
- args=[dbus_tube_id, 2])
+ bytestream = alice_accepts_tube(q, stream, iq_event, dbus_tube_id)
dbus_tube_adr = tubes_iface.GetDBusTubeAddress(dbus_tube_id)
- send_dbus_message_to_alice(q, stream, dbus_tube_adr, dbus_stream_id)
+ send_dbus_message_to_alice(q, stream, dbus_tube_adr, bytestream)
# close the tube
tubes_iface.CloseTube(dbus_tube_id)
@@ -191,15 +171,15 @@ def offer_old_dbus_tube(q, bus, conn, stream, self_handle, alice_handle):
def offer_new_dbus_tube(q, bus, conn, stream, self_handle, alice_handle):
- requestotron = dbus.Interface(conn, CONN_IFACE_REQUESTS)
+ requestotron = dbus.Interface(conn, cs.CONN_IFACE_REQUESTS)
# Offer a tube to Alice (new API)
call_async(q, requestotron, 'CreateChannel',
- {CHANNEL_TYPE: CHANNEL_TYPE_DBUS_TUBE,
- TARGET_HANDLE_TYPE: HT_CONTACT,
- TARGET_ID: 'alice at localhost',
- DBUS_TUBE_SERVICE_NAME: 'com.example.TestCase'
+ {cs.CHANNEL_TYPE: cs.CHANNEL_TYPE_DBUS_TUBE,
+ cs.TARGET_HANDLE_TYPE: cs.HT_CONTACT,
+ cs.TARGET_ID: 'alice at localhost',
+ cs.DBUS_TUBE_SERVICE_NAME: 'com.example.TestCase'
}, byte_arrays=True)
cc_ret, nc = q.expect_many(
EventPattern('dbus-return', method='CreateChannel'),
@@ -209,18 +189,18 @@ def offer_new_dbus_tube(q, bus, conn, stream, self_handle, alice_handle):
new_channel_details = nc.args[0]
# check tube channel properties
- assert tube_props[CHANNEL_TYPE] == CHANNEL_TYPE_DBUS_TUBE
- assert tube_props[INTERFACES] == [CHANNEL_IFACE_TUBE]
- assert tube_props[TARGET_HANDLE_TYPE] == HT_CONTACT
- assert tube_props[TARGET_HANDLE] == alice_handle
- assert tube_props[TARGET_ID] == 'alice at localhost'
- assert tube_props[REQUESTED] == True
- assert tube_props[INITIATOR_HANDLE] == self_handle
- assert tube_props[INITIATOR_ID] == "test at localhost"
- assert tube_props[DBUS_TUBE_SERVICE_NAME] == 'com.example.TestCase'
- assert DBUS_TUBE_DBUS_NAMES not in tube_props
- assert TUBE_PARAMETERS not in tube_props
- assert TUBE_STATE not in tube_props
+ assert tube_props[cs.CHANNEL_TYPE] == cs.CHANNEL_TYPE_DBUS_TUBE
+ assert tube_props[cs.INTERFACES] == [cs.CHANNEL_IFACE_TUBE]
+ assert tube_props[cs.TARGET_HANDLE_TYPE] == cs.HT_CONTACT
+ assert tube_props[cs.TARGET_HANDLE] == alice_handle
+ assert tube_props[cs.TARGET_ID] == 'alice at localhost'
+ assert tube_props[cs.REQUESTED] == True
+ assert tube_props[cs.INITIATOR_HANDLE] == self_handle
+ assert tube_props[cs.INITIATOR_ID] == "test at localhost"
+ assert tube_props[cs.DBUS_TUBE_SERVICE_NAME] == 'com.example.TestCase'
+ assert cs.DBUS_TUBE_DBUS_NAMES not in tube_props
+ assert cs.TUBE_PARAMETERS not in tube_props
+ assert cs.TUBE_STATE not in tube_props
# Under the current implementation, creating a new-style Tube channel
# ensures that an old-style Tubes channel exists, even though Tube channels
@@ -233,11 +213,11 @@ def offer_new_dbus_tube(q, bus, conn, stream, self_handle, alice_handle):
found_tubes = False
found_tube = False
for path, details in new_channel_details:
- if details[CHANNEL_TYPE] == CHANNEL_TYPE_TUBES:
+ if details[cs.CHANNEL_TYPE] == cs.CHANNEL_TYPE_TUBES:
found_tubes = True
tubes_chan = bus.get_object(conn.bus_name, path)
- tubes_iface = dbus.Interface(tubes_chan, CHANNEL_TYPE_TUBES)
- elif details[CHANNEL_TYPE] == CHANNEL_TYPE_DBUS_TUBE:
+ tubes_iface = dbus.Interface(tubes_chan, cs.CHANNEL_TYPE_TUBES)
+ elif details[cs.CHANNEL_TYPE] == cs.CHANNEL_TYPE_DBUS_TUBE:
found_tube = True
assert tube_path == path, (tube_path, path)
else:
@@ -249,16 +229,16 @@ def offer_new_dbus_tube(q, bus, conn, stream, self_handle, alice_handle):
assert len(tubes) == 0, tubes
tube_chan = bus.get_object(conn.bus_name, tube_path)
- tube_chan_iface = dbus.Interface(tube_chan, CHANNEL)
- dbus_tube_iface = dbus.Interface(tube_chan, CHANNEL_TYPE_DBUS_TUBE)
+ tube_chan_iface = dbus.Interface(tube_chan, cs.CHANNEL)
+ dbus_tube_iface = dbus.Interface(tube_chan, cs.CHANNEL_TYPE_DBUS_TUBE)
# check State and Parameters
- props = tube_chan.GetAll(CHANNEL_IFACE_TUBE, dbus_interface=PROPERTIES_IFACE,
+ props = tube_chan.GetAll(cs.CHANNEL_IFACE_TUBE, dbus_interface=cs.PROPERTIES_IFACE,
byte_arrays=True)
- assert props['State'] == TUBE_CHANNEL_STATE_NOT_OFFERED
+ assert props['State'] == cs.TUBE_CHANNEL_STATE_NOT_OFFERED
# check ServiceName and DBusNames
- props = tube_chan.GetAll(CHANNEL_TYPE_DBUS_TUBE, dbus_interface=PROPERTIES_IFACE)
+ props = tube_chan.GetAll(cs.CHANNEL_TYPE_DBUS_TUBE, dbus_interface=cs.PROPERTIES_IFACE)
assert props['ServiceName'] == 'com.example.TestCase'
assert props['DBusNames'] == {}
@@ -277,17 +257,17 @@ def offer_new_dbus_tube(q, bus, conn, stream, self_handle, alice_handle):
tube_address = offer_return_event.value[0]
assert len(tube_address) > 0
- assert state_event.args[0] == TUBE_CHANNEL_STATE_REMOTE_PENDING
+ assert state_event.args[0] == cs.TUBE_CHANNEL_STATE_REMOTE_PENDING
# Now the tube's been offered, it should be shown on the old interface
tubes = tubes_iface.ListTubes(byte_arrays=True)
assert len(tubes) == 1
- expected_tube = (None, self_handle, TUBE_TYPE_DBUS, 'com.example.TestCase',
- sample_parameters, TUBE_STATE_REMOTE_PENDING)
+ expected_tube = (None, self_handle, cs.TUBE_TYPE_DBUS, 'com.example.TestCase',
+ sample_parameters, cs.TUBE_STATE_REMOTE_PENDING)
t.check_tube_in_tubes(expected_tube, tubes)
- status = tube_chan.Get(CHANNEL_IFACE_TUBE, 'State', dbus_interface=PROPERTIES_IFACE)
- assert status == TUBE_STATE_REMOTE_PENDING
+ status = tube_chan.Get(cs.CHANNEL_IFACE_TUBE, 'State', dbus_interface=cs.PROPERTIES_IFACE)
+ assert status == cs.TUBE_STATE_REMOTE_PENDING
tube_chan_iface.Close()
@@ -303,7 +283,7 @@ def test(q, bus, conn, stream):
t.check_conn_properties(q, conn)
self_handle = conn.GetSelfHandle()
- alice_handle = conn.RequestHandles(HT_CONTACT, ["alice at localhost"])[0]
+ alice_handle = conn.RequestHandles(cs.HT_CONTACT, ["alice at localhost"])[0]
# send Alice's presence
caps = { 'ext': '', 'ver': '0.0.0',
@@ -315,8 +295,8 @@ def test(q, bus, conn, stream):
args = [{alice_handle: (2L, u'available', u'')}])
# reply to disco query
- event = q.expect('stream-iq', to='alice at localhost/Test', query_ns=DISCO_INFO)
- stream.send(make_caps_disco_reply(stream, event.stanza, [TUBES]))
+ event = q.expect('stream-iq', to='alice at localhost/Test', query_ns=ns.DISCO_INFO)
+ stream.send(make_caps_disco_reply(stream, event.stanza, [ns.TUBES]))
sync_stream(q, stream)
--
1.5.6.5
More information about the telepathy-commits
mailing list