From 2d624a7b965c65450ae48a73b5a347dc83f365f0 Mon Sep 17 00:00:00 2001 From: Jefry Lagrange Date: Sun, 17 Jul 2011 18:28:38 -0400 Subject: [PATCH] test case for jingle --- src/common/jingle_ft.py | 8 +- test/unit/test_jingle.py | 157 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+), 4 deletions(-) create mode 100644 test/unit/test_jingle.py diff --git a/src/common/jingle_ft.py b/src/common/jingle_ft.py index 757b96d83..ea87c7e43 100644 --- a/src/common/jingle_ft.py +++ b/src/common/jingle_ft.py @@ -74,12 +74,12 @@ class JingleFileTransfer(JingleContent): self.file_props['transfered_size'] = [] log.info("FT request: %s" % file_props) - + if transport is None: self.transport = JingleTransportSocks5() - self.transport.set_connection(session.connection) - self.transport.set_file_props(self.file_props) - self.transport.set_our_jid(session.ourjid) + self.transport.set_connection(session.connection) + self.transport.set_file_props(self.file_props) + self.transport.set_our_jid(session.ourjid) log.info('ourjid: %s' % session.ourjid) if self.file_props is not None: diff --git a/test/unit/test_jingle.py b/test/unit/test_jingle.py new file mode 100644 index 000000000..94131ede7 --- /dev/null +++ b/test/unit/test_jingle.py @@ -0,0 +1,157 @@ +''' +Tests for dispatcher_nb.py +''' +import unittest + +import lib +lib.setup_env() + +from mock import Mock + +from common.protocol.bytestream import ConnectionIBBytestream, ConnectionSocks5Bytestream +from common.xmpp import dispatcher_nb +from common.xmpp import protocol +from common.jingle import ConnectionJingle +from common import gajim +from common.socks5 import SocksQueue +import common + + +session_init = ''' + + + + + + + + + + + + + + + + + + + + + + ''' + + +transport_info = ''' + + + + + + + + + + +''' + +class Connection(Mock, ConnectionJingle, ConnectionSocks5Bytestream, + ConnectionIBBytestream): + + def __init__(self): + Mock.__init__(self) + ConnectionJingle.__init__(self) + ConnectionSocks5Bytestream.__init__(self) + ConnectionIBBytestream.__init__(self) + self.connected = 2 # This tells gajim we are connected + + + def send(self, stanza=None, when=None): + # Called when gajim wants to send something + print str(stanza) + +class TestJingle(unittest.TestCase): + + def setUp(self): + self.dispatcher = dispatcher_nb.XMPPDispatcher() + gajim.nec = Mock() + gajim.socks5queue = SocksQueue(Mock()) + # Setup mock client + self.client = Connection() + self.client.__str__ = lambda: 'Mock' # FIXME: why do I need this one? + self.client._caller = Connection() + self.client.defaultNamespace = protocol.NS_CLIENT + self.client.Connection = Connection() # mock transport + self.con = self.client.Connection + self.con.server_resource = None + self.con.connection = Connection() + + ''' + Fake file_props when we recieve a file. Gajim creates a file_props + out of a FileRequestRecieve event and from then on it changes in + a lot of places. It is easier to just copy it in here. + If the session_initiate stanza changes, this also must change. + ''' + self.recieve_file = {'stream-methods': + 'http://jabber.org/protocol/bytestreams', + 'sender': u'jtest@thiessen.im/Gajim', + 'file-name': u'test_recieved_file', + 'request-id': u'43', 'sid': u'39', + 'session-sid': u'38', 'session-type': 'jingle', + 'transfered_size': [], 'receiver': + u'jingleft@thiessen.im/Gajim', 'desc': '', + u'size': u'2273', 'type': 'r', + 'streamhosts': [{'initiator': + u'jtest@thiessen.im/Gajim', + 'target': u'jingleft@thiessen.im/Gajim', + 'cid': u'41', 'state': 0, 'host': u'192.168.2.100', + 'type': u'direct', 'port': u'28011'}, + {'initiator': u'jtest@thiessen.im/Gajim', + 'target': u'jingleft@thiessen.im/Gajim', + 'cid': u'42', 'state': 0, 'host': u'192.168.2.100', + 'type': u'proxy', 'port': u'5000'}], + u'name': u'to'} + + def tearDown(self): + # Unplug if needed + if hasattr(self.dispatcher, '_owner'): + self.dispatcher.PlugOut() + + def _simulate_connect(self): + self.dispatcher.PlugIn(self.client) # client is owner + # Simulate that we have established a connection + self.dispatcher.StreamInit() + self.dispatcher.ProcessNonBlocking("") + + def _simulate_jingle_session(self): + + self.dispatcher.RegisterHandler('iq', self.con._JingleCB, 'set' + , common.xmpp.NS_JINGLE) + self.dispatcher.ProcessNonBlocking(session_init) + session = self.con._sessions.values()[0] # The only session we have + jft = session.contents.values()[0] # jingleFT object + jft.file_props = self.recieve_file # We plug file_props manually + # The user accepts to recieve the file + # we have to manually simulate this behavior + session.approve_session() + self.con.send_file_approval(self.recieve_file) + + self.dispatcher.ProcessNonBlocking(transport_info) + + + def test_jingle_session(self): + self._simulate_connect() + self._simulate_jingle_session() + + + + +if __name__ == '__main__': + unittest.main()