2015-04-14 17:19:46 +00:00
|
|
|
import asyncio
|
2012-02-03 15:03:46 +00:00
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
|
2013-07-26 11:02:26 +00:00
|
|
|
import unittest
|
2014-07-17 12:19:04 +00:00
|
|
|
from slixmpp.test import SlixTest
|
2012-02-03 15:03:46 +00:00
|
|
|
|
|
|
|
|
2014-07-17 12:19:04 +00:00
|
|
|
class TestInBandByteStreams(SlixTest):
|
2012-10-31 07:03:55 +00:00
|
|
|
|
2012-02-03 15:03:46 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.stream_start(plugins=['xep_0047', 'xep_0030'])
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.stream_close()
|
|
|
|
|
2021-02-14 11:11:58 +00:00
|
|
|
def testOpenStream(self):
|
2012-10-31 07:03:55 +00:00
|
|
|
"""Test requesting a stream, successfully"""
|
2012-02-03 15:03:46 +00:00
|
|
|
|
|
|
|
events = []
|
|
|
|
|
|
|
|
def on_stream_start(stream):
|
|
|
|
events.append('ibb_stream_start')
|
|
|
|
|
|
|
|
|
|
|
|
self.xmpp.add_event_handler('ibb_stream_start', on_stream_start)
|
2012-10-31 07:03:55 +00:00
|
|
|
|
2021-02-14 11:11:58 +00:00
|
|
|
self.xmpp.wrap(self.xmpp['xep_0047'].open_stream('tester@localhost/receiver',
|
|
|
|
sid='testing'))
|
|
|
|
self.wait_()
|
2012-02-03 15:03:46 +00:00
|
|
|
|
|
|
|
self.send("""
|
|
|
|
<iq type="set" to="tester@localhost/receiver" id="1">
|
2012-10-31 07:03:55 +00:00
|
|
|
<open xmlns="http://jabber.org/protocol/ibb"
|
2012-02-03 15:03:46 +00:00
|
|
|
sid="testing"
|
|
|
|
block-size="4096"
|
|
|
|
stanza="iq" />
|
|
|
|
</iq>
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.recv("""
|
|
|
|
<iq type="result" id="1"
|
|
|
|
to="tester@localhost"
|
|
|
|
from="tester@localhost/receiver" />
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.assertEqual(events, ['ibb_stream_start'])
|
|
|
|
|
2021-02-14 11:11:58 +00:00
|
|
|
def testAysncOpenStream(self):
|
2012-02-03 15:03:46 +00:00
|
|
|
"""Test requesting a stream, aysnc"""
|
|
|
|
|
|
|
|
events = set()
|
|
|
|
|
|
|
|
def on_stream_start(stream):
|
|
|
|
events.add('ibb_stream_start')
|
|
|
|
|
|
|
|
def stream_callback(iq):
|
|
|
|
events.add('callback')
|
|
|
|
|
|
|
|
self.xmpp.add_event_handler('ibb_stream_start', on_stream_start)
|
2012-10-31 07:03:55 +00:00
|
|
|
|
2021-02-14 11:11:58 +00:00
|
|
|
self.xmpp.wrap(self.xmpp['xep_0047'].open_stream('tester@localhost/receiver',
|
|
|
|
sid='testing',
|
|
|
|
callback=stream_callback))
|
|
|
|
self.wait_()
|
2012-02-03 15:03:46 +00:00
|
|
|
|
|
|
|
self.send("""
|
|
|
|
<iq type="set" to="tester@localhost/receiver" id="1">
|
2012-10-31 07:03:55 +00:00
|
|
|
<open xmlns="http://jabber.org/protocol/ibb"
|
2012-02-03 15:03:46 +00:00
|
|
|
sid="testing"
|
|
|
|
block-size="4096"
|
|
|
|
stanza="iq" />
|
|
|
|
</iq>
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.recv("""
|
|
|
|
<iq type="result" id="1"
|
|
|
|
to="tester@localhost"
|
|
|
|
from="tester@localhost/receiver" />
|
|
|
|
""")
|
|
|
|
|
2016-10-22 12:35:54 +00:00
|
|
|
self.assertEqual(events, {'ibb_stream_start', 'callback'})
|
2012-02-03 15:03:46 +00:00
|
|
|
|
2021-02-14 11:11:58 +00:00
|
|
|
def testSendData(self):
|
2012-02-03 15:29:38 +00:00
|
|
|
"""Test sending data over an in-band bytestream."""
|
2012-02-03 15:03:46 +00:00
|
|
|
|
2012-02-03 15:29:38 +00:00
|
|
|
streams = []
|
|
|
|
data = []
|
|
|
|
|
|
|
|
def on_stream_start(stream):
|
|
|
|
streams.append(stream)
|
|
|
|
|
|
|
|
def on_stream_data(d):
|
2021-02-14 11:11:58 +00:00
|
|
|
data.append(d.read())
|
2012-02-03 15:29:38 +00:00
|
|
|
|
|
|
|
self.xmpp.add_event_handler('ibb_stream_start', on_stream_start)
|
|
|
|
self.xmpp.add_event_handler('ibb_stream_data', on_stream_data)
|
2012-10-31 07:03:55 +00:00
|
|
|
|
2021-02-14 11:11:58 +00:00
|
|
|
self.xmpp.wrap(self.xmpp['xep_0047'].open_stream('tester@localhost/receiver',
|
|
|
|
sid='testing'))
|
|
|
|
self.wait_()
|
2012-02-03 15:29:38 +00:00
|
|
|
|
|
|
|
self.send("""
|
|
|
|
<iq type="set" to="tester@localhost/receiver" id="1">
|
2012-10-31 07:03:55 +00:00
|
|
|
<open xmlns="http://jabber.org/protocol/ibb"
|
2012-02-03 15:29:38 +00:00
|
|
|
sid="testing"
|
|
|
|
block-size="4096"
|
|
|
|
stanza="iq" />
|
|
|
|
</iq>
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.recv("""
|
|
|
|
<iq type="result" id="1"
|
|
|
|
to="tester@localhost"
|
|
|
|
from="tester@localhost/receiver" />
|
|
|
|
""")
|
|
|
|
|
|
|
|
stream = streams[0]
|
|
|
|
|
|
|
|
|
|
|
|
# Test sending data out
|
2021-02-14 11:11:58 +00:00
|
|
|
self.xmpp.wrap(stream.send("Testing"))
|
|
|
|
self.wait_()
|
2012-02-03 15:29:38 +00:00
|
|
|
|
|
|
|
self.send("""
|
|
|
|
<iq type="set" id="2"
|
|
|
|
from="tester@localhost"
|
|
|
|
to="tester@localhost/receiver">
|
2012-10-31 07:03:55 +00:00
|
|
|
<data xmlns="http://jabber.org/protocol/ibb"
|
|
|
|
seq="0"
|
2012-02-03 15:29:38 +00:00
|
|
|
sid="testing">
|
|
|
|
VGVzdGluZw==
|
|
|
|
</data>
|
|
|
|
</iq>
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.recv("""
|
|
|
|
<iq type="result" id="2"
|
|
|
|
to="tester@localhost"
|
|
|
|
from="tester@localhost/receiver" />
|
|
|
|
""")
|
|
|
|
|
|
|
|
# Test receiving data
|
|
|
|
self.recv("""
|
|
|
|
<iq type="set" id="A"
|
|
|
|
to="tester@localhost"
|
|
|
|
from="tester@localhost/receiver">
|
2012-10-31 07:03:55 +00:00
|
|
|
<data xmlns="http://jabber.org/protocol/ibb"
|
|
|
|
seq="0"
|
2012-02-03 15:29:38 +00:00
|
|
|
sid="testing">
|
|
|
|
aXQgd29ya3Mh
|
|
|
|
</data>
|
|
|
|
</iq>
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.send("""
|
|
|
|
<iq type="result" id="A"
|
|
|
|
to="tester@localhost/receiver" />
|
|
|
|
""")
|
|
|
|
|
2012-10-31 07:03:55 +00:00
|
|
|
self.assertEqual(data, [b'it works!'])
|
2012-02-03 15:03:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
suite = unittest.TestLoader().loadTestsFromTestCase(TestInBandByteStreams)
|