[telepathy-gabble/master] Don't assume resource='Resource' in Authenticators

Will Thompson will.thompson at collabora.co.uk
Wed May 13 10:34:49 PDT 2009


If 'resource' is set in the connection parameters, then check it's
correct in the Authenticators.
---
 tests/twisted/connect/test-twice.py |    6 ++++--
 tests/twisted/gabbletest.py         |   26 ++++++++++++++++++--------
 2 files changed, 22 insertions(+), 10 deletions(-)

diff --git a/tests/twisted/connect/test-twice.py b/tests/twisted/connect/test-twice.py
index 4336494..a3fddb1 100644
--- a/tests/twisted/connect/test-twice.py
+++ b/tests/twisted/connect/test-twice.py
@@ -56,7 +56,8 @@ if __name__ == '__main__':
         'port': dbus.UInt32(4242),
         }
     conn1 = make_connection(bus, queue.append, params)
-    authenticator = JabberAuthenticator('test1', 'pass')
+    authenticator = JabberAuthenticator('test1', 'pass',
+        resource=params['resource'])
     stream1 = make_stream(queue.append, authenticator, protocol=JabberXmlStream,
                           port=4242)
 
@@ -68,7 +69,8 @@ if __name__ == '__main__':
         'port': dbus.UInt32(4343),
         }
     conn2 = make_connection(bus, queue.append, params)
-    authenticator = XmppAuthenticator('test2', 'pass')
+    authenticator = XmppAuthenticator('test2', 'pass',
+        resource=params['resource'])
     stream2 = make_stream(queue.append, authenticator, protocol=XmppXmlStream,
                           port=4343)
 
diff --git a/tests/twisted/gabbletest.py b/tests/twisted/gabbletest.py
index 8e66b22..8f2058c 100644
--- a/tests/twisted/gabbletest.py
+++ b/tests/twisted/gabbletest.py
@@ -11,6 +11,7 @@ import random
 
 import ns
 import servicetest
+from servicetest import assertEquals, assertLength
 import twisted
 from twisted.words.xish import domish, xpath
 from twisted.words.protocols.jabber.client import IQ
@@ -84,9 +85,10 @@ def sync_stream(q, stream):
 class JabberAuthenticator(xmlstream.Authenticator):
     "Trivial XML stream authenticator that accepts one username/digest pair."
 
-    def __init__(self, username, password):
+    def __init__(self, username, password, resource=None):
         self.username = username
         self.password = password
+        self.resource = resource
         xmlstream.Authenticator.__init__(self)
 
     # Patch in fix from http://twistedmatrix.com/trac/changeset/23418.
@@ -134,7 +136,9 @@ class JabberAuthenticator(xmlstream.Authenticator):
         assert map(str, digest) == [expect]
 
         resource = xpath.queryForNodes('/iq/query/resource', iq)
-        assert map(str, resource) == ['Resource']
+        assertLength(1, resource)
+        if self.resource is not None:
+            assertEquals(self.resource, str(resource[0]))
 
         result = IQ(self.xmlstream, "result")
         result["id"] = iq["id"]
@@ -143,10 +147,11 @@ class JabberAuthenticator(xmlstream.Authenticator):
 
 
 class XmppAuthenticator(xmlstream.Authenticator):
-    def __init__(self, username, password):
+    def __init__(self, username, password, resource=None):
         xmlstream.Authenticator.__init__(self)
         self.username = username
         self.password = password
+        self.resource = resource
         self.authenticated = False
 
     def streamStarted(self, root=None):
@@ -182,12 +187,16 @@ class XmppAuthenticator(xmlstream.Authenticator):
         self.authenticated = True
 
     def bindIq(self, iq):
-        assert xpath.queryForString('/iq/bind/resource', iq) == 'Resource'
+        resource = xpath.queryForString('/iq/bind/resource', iq)
+        if self.resource is not None:
+            assertEquals(self.resource, resource)
+        else:
+            assert resource is not None
 
         result = IQ(self.xmlstream, "result")
         result["id"] = iq["id"]
         bind = result.addElement((NS_XMPP_BIND, 'bind'))
-        jid = bind.addElement('jid', content='test at localhost/Resource')
+        jid = bind.addElement('jid', content=('test at localhost/%s' % resource))
         self.xmlstream.send(result)
 
         self.xmlstream.dispatch(self.xmlstream, xmlstream.STREAM_AUTHD_EVENT)
@@ -301,11 +310,11 @@ def make_connection(bus, event_func, params=None):
     return servicetest.make_connection(bus, event_func, 'gabble', 'jabber',
         default_params)
 
-def make_stream(event_func, authenticator=None, protocol=None, port=4242):
+def make_stream(event_func, authenticator=None, protocol=None, port=4242, resource=None):
     # set up Jabber server
 
     if authenticator is None:
-        authenticator = JabberAuthenticator('test', 'pass')
+        authenticator = JabberAuthenticator('test', 'pass', resource=resource)
 
     if protocol is None:
         protocol = JabberXmlStream
@@ -357,8 +366,9 @@ def exec_test_deferred (funs, params, protocol=None, timeout=None,
 
     bus = dbus.SessionBus()
     # conn = make_connection(bus, queue.append, params)
+    resource = params.get('resource') if params is not None else None
     (stream, port) = make_stream(queue.append, protocol=protocol,
-        authenticator=authenticator)
+        authenticator=authenticator, resource=resource)
 
     error = None
 
-- 
1.5.6.5




More information about the telepathy-commits mailing list