[telepathy-gabble/master] Don't hardcode caps hashes in tests

Will Thompson will.thompson at collabora.co.uk
Mon Jun 15 11:22:11 PDT 2009


---
 tests/twisted/caps_helper.py     |   27 ++++++++++-
 tests/twisted/test-caps-cache.py |   54 ++++++--------------
 tests/twisted/test-caps-hash.py  |  103 +++++++++++++------------------------
 3 files changed, 79 insertions(+), 105 deletions(-)

diff --git a/tests/twisted/caps_helper.py b/tests/twisted/caps_helper.py
index b1fdcc5..47b180d 100644
--- a/tests/twisted/caps_helper.py
+++ b/tests/twisted/caps_helper.py
@@ -45,6 +45,15 @@ ft_allowed_properties = dbus.Array([
     cs.CHANNEL_TYPE_FILE_TRANSFER + '.Description',
     cs.CHANNEL_TYPE_FILE_TRANSFER + '.Date'])
 
+fake_client_dataforms = {
+    'urn:xmpp:dataforms:softwareinfo':
+    {'software': ['A Fake Client with Twisted'],
+        'software_version': ['5.11.2-svn-20080512'],
+        'os': ['Debian GNU/Linux unstable (sid) unstable sid'],
+        'os_version': ['2.6.24-1-amd64'],
+    },
+}
+
 def compute_caps_hash(identities, features, dataforms):
     """
     Accepts a list of slash-separated identities, a list of feature namespaces,
@@ -75,7 +84,7 @@ def compute_caps_hash(identities, features, dataforms):
     m.update('<'.join(components))
     return base64.b64encode(m.digest())
 
-def make_caps_disco_reply(stream, req, features):
+def make_caps_disco_reply(stream, req, features, dataforms={}):
     iq = make_result_iq(stream, req)
     query = iq.firstChildElement()
 
@@ -84,6 +93,22 @@ def make_caps_disco_reply(stream, req, features):
         el['var'] = f
         query.addChild(el)
 
+    for type, fields in dataforms.iteritems():
+        x = query.addElement((ns.X_DATA, 'x'))
+        x['type'] = 'result'
+
+        field = x.addElement('field')
+        field['var'] = 'FORM_TYPE'
+        field['type'] = 'hidden'
+        field.addElement('value', content=type)
+
+        for var, values in fields.iteritems():
+            field = x.addElement('field')
+            field['var'] = var
+
+            for value in values:
+                field.addElement('value', content=value)
+
     return iq
 
 def receive_presence_and_ask_caps(q, stream):
diff --git a/tests/twisted/test-caps-cache.py b/tests/twisted/test-caps-cache.py
index 93055c0..de3b63f 100644
--- a/tests/twisted/test-caps-cache.py
+++ b/tests/twisted/test-caps-cache.py
@@ -9,6 +9,9 @@ from twisted.words.xish import xpath
 from servicetest import EventPattern
 from gabbletest import exec_test, make_result_iq, make_presence
 import constants as cs
+from caps_helper import (
+    compute_caps_hash, make_caps_disco_reply, fake_client_dataforms,
+    )
 
 def presence_add_caps(presence, ver, client, hash=None):
     c = presence.addElement(('http://jabber.org/protocol/caps', 'c'))
@@ -50,14 +53,11 @@ def _test_without_hash(q, bus, conn, stream, contact, contact_handle, client, di
             client + '#' + '0.1'
 
         # send good reply
-        result = make_result_iq(stream, event.stanza)
-        query = result.firstChildElement()
-        feature = query.addElement('feature')
-        feature['var'] = 'http://jabber.org/protocol/jingle'
-        feature = query.addElement('feature')
-        feature['var'] = 'http://jabber.org/protocol/jingle/description/audio'
-        feature = query.addElement('feature')
-        feature['var'] = 'http://www.google.com/transport/p2p'
+        result = make_caps_disco_reply(stream, event.stanza,
+            ['http://jabber.org/protocol/jingle',
+             'http://jabber.org/protocol/jingle/description/audio',
+             'http://www.google.com/transport/p2p',
+            ])
         stream.send(result)
 
     # we can now do audio calls
@@ -80,11 +80,17 @@ def _test_with_hash(q, bus, conn, stream, contact, contact_handle, client, disco
     basic_caps = [(contact_handle, cs.CHANNEL_TYPE_TEXT, 3, 0)]
     assert conn.Capabilities.GetCapabilities([contact_handle]) == basic_caps
 
+    features = [
+        'http://jabber.org/protocol/jingle',
+        'http://jabber.org/protocol/jingle/description/audio',
+        'http://www.google.com/transport/p2p',
+        ]
+
     # send updated presence with Jingle caps info
     presence = make_presence(contact, status='hello')
     c = presence.addElement(('http://jabber.org/protocol/caps', 'c'))
     c['node'] = client
-    c['ver'] = 'CzO+nkbflbxu1pgzOQSIi8gOyDc=' # good hash
+    c['ver'] = compute_caps_hash([], features, fake_client_dataforms)
     c['hash'] = 'sha-1'
     stream.send(presence)
 
@@ -97,34 +103,8 @@ def _test_with_hash(q, bus, conn, stream, contact, contact_handle, client, disco
             client + '#' + c['ver']
 
         # send good reply
-        result = make_result_iq(stream, event.stanza)
-        query = result.firstChildElement()
-        query['node'] = client + '#' + c['ver']
-        feature = query.addElement('feature')
-        feature['var'] = 'http://jabber.org/protocol/jingle'
-        feature = query.addElement('feature')
-        feature['var'] = 'http://jabber.org/protocol/jingle/description/audio'
-        feature = query.addElement('feature')
-        feature['var'] = 'http://www.google.com/transport/p2p'
-        query.addRawXml("""
-<x type='result' xmlns='jabber:x:data'>
-<field var='FORM_TYPE' type='hidden'>
-<value>urn:xmpp:dataforms:softwareinfo</value>
-</field>
-<field var='software'>
-<value>A Fake Client with Twisted</value>
-</field>
-<field var='software_version'>
-<value>5.11.2-svn-20080512</value>
-</field>
-<field var='os'>
-<value>Debian GNU/Linux unstable (sid) unstable sid</value>
-</field>
-<field var='os_version'>
-<value>2.6.24-1-amd64</value>
-</field>
-</x>
-        """)
+        result = make_caps_disco_reply(stream, event.stanza, features,
+            fake_client_dataforms)
         stream.send(result)
 
     # we can now do audio calls
diff --git a/tests/twisted/test-caps-hash.py b/tests/twisted/test-caps-hash.py
index 6695145..ce8a1c9 100644
--- a/tests/twisted/test-caps-hash.py
+++ b/tests/twisted/test-caps-hash.py
@@ -24,9 +24,13 @@ import dbus
 
 from twisted.words.xish import xpath
 
-from gabbletest import exec_test, make_result_iq, make_presence
+from gabbletest import exec_test, make_result_iq, make_presence, sync_stream
 from servicetest import sync_dbus, EventPattern
 import constants as cs
+import ns
+from caps_helper import (
+    compute_caps_hash, make_caps_disco_reply, fake_client_dataforms,
+    )
 
 caps_changed_flag = False
 
@@ -75,21 +79,16 @@ def test_hash(q, bus, conn, stream, contact, contact_handle, client):
         client + '#' + '0.1'
 
     # send good reply
-    result = make_result_iq(stream, event.stanza)
-    query = result.firstChildElement()
-    feature = query.addElement('feature')
-    feature['var'] = 'http://jabber.org/protocol/jingle'
-    feature = query.addElement('feature')
-    feature['var'] = 'http://jabber.org/protocol/jingle/description/audio'
-    feature = query.addElement('feature')
-    feature['var'] = 'http://www.google.com/transport/p2p'
-    stream.send(result)
+    stream.send(make_caps_disco_reply(stream, event.stanza,
+        [ 'http://jabber.org/protocol/jingle',
+          'http://jabber.org/protocol/jingle/description/audio',
+          'http://www.google.com/transport/p2p',
+        ]))
 
     # we can now do audio calls
     event = q.expect('dbus-signal', signal='CapabilitiesChanged')
     caps_changed_flag = False
 
-
     # send bogus presence
     presence = make_presence(contact, status='hello')
     c = presence.addElement(('http://jabber.org/protocol/caps', 'c'))
@@ -106,11 +105,8 @@ def test_hash(q, bus, conn, stream, contact, contact_handle, client):
         client + '#' + c['ver']
 
     # send bogus reply
-    result = make_result_iq(stream, event.stanza)
-    query = result.firstChildElement()
-    feature = query.addElement('feature')
-    feature['var'] = 'http://jabber.org/protocol/bogus-feature'
-    stream.send(result)
+    stream.send(make_caps_disco_reply(stream, event.stanza,
+        ['http://jabber.org/protocol/bogus-feature']))
 
     # don't receive any D-Bus signal
     sync_dbus(bus, q, conn)
@@ -144,11 +140,18 @@ def test_hash(q, bus, conn, stream, contact, contact_handle, client):
     assert caps_changed_flag == True
     caps_changed_flag = False
 
+
     # send correct presence
+    features = [
+        'http://jabber.org/protocol/jingle',
+        'http://jabber.org/protocol/jingle/description/audio',
+        'http://www.google.com/transport/p2p',
+        ]
+
     presence = make_presence(contact, status='hello')
     c = presence.addElement(('http://jabber.org/protocol/caps', 'c'))
     c['node'] = client
-    c['ver'] = 'CzO+nkbflbxu1pgzOQSIi8gOyDc=' # good hash
+    c['ver'] = compute_caps_hash([], features, fake_client_dataforms)
     c['hash'] = 'sha-1'
     stream.send(presence)
 
@@ -164,39 +167,13 @@ def test_hash(q, bus, conn, stream, contact, contact_handle, client):
     assert caps_changed_flag == False
 
     # send good reply
-    result = make_result_iq(stream, event.stanza)
-    query = result.firstChildElement()
-    query['node'] = client + '#' + c['ver']
-    feature = query.addElement('feature')
-    feature['var'] = 'http://jabber.org/protocol/jingle'
-    feature = query.addElement('feature')
-    feature['var'] = 'http://jabber.org/protocol/jingle/description/audio'
-    feature = query.addElement('feature')
-    feature['var'] = 'http://www.google.com/transport/p2p'
-
-    query.addRawXml("""
-<x type='result' xmlns='jabber:x:data'>
-<field var='FORM_TYPE' type='hidden'>
-<value>urn:xmpp:dataforms:softwareinfo</value>
-</field>
-<field var='software'>
-<value>A Fake Client with Twisted</value>
-</field>
-<field var='software_version'>
-<value>5.11.2-svn-20080512</value>
-</field>
-<field var='os'>
-<value>Debian GNU/Linux unstable (sid) unstable sid</value>
-</field>
-<field var='os_version'>
-<value>2.6.24-1-amd64</value>
-</field>
-</x>
-    """)
+    result = make_caps_disco_reply(stream, event.stanza, features,
+        fake_client_dataforms)
     stream.send(result)
 
     # we can now do audio calls
-    event = q.expect('dbus-signal', signal='CapabilitiesChanged')
+    event = q.expect('dbus-signal', signal='CapabilitiesChanged',
+    )
     assert caps_changed_flag == True
     caps_changed_flag = False
 
@@ -234,7 +211,12 @@ def test_two_clients(q, bus, conn, stream, contact1, contact2,
 
     # send updated presence with Jingle caps info
     presence = make_presence(contact1, status='hello')
-    ver = 'JpaYgiKL0y4fUOCTwN3WLGpaftM='
+    features = [
+        'http://jabber.org/protocol/jingle',
+        'http://jabber.org/protocol/jingle/description/audio',
+        'http://www.google.com/transport/p2p',
+        ]
+    ver = compute_caps_hash([], features, {})
     presence = presence_add_caps(presence, ver, client,
             hash='sha-1')
     stream.send(presence)
@@ -254,19 +236,13 @@ def test_two_clients(q, bus, conn, stream, contact1, contact2,
     sync_dbus(bus, q, conn)
     assert caps_changed_flag == False
 
-    # send good reply
-    result = make_result_iq(stream, event.stanza)
-    query = result.firstChildElement()
-    feature = query.addElement('feature')
-    feature['var'] = 'http://jabber.org/protocol/jingle'
-    feature = query.addElement('feature')
-    feature['var'] = 'http://jabber.org/protocol/jingle/description/audio'
-    feature = query.addElement('feature')
-    feature['var'] = 'http://www.google.com/transport/p2p'
+    result = make_caps_disco_reply(stream, event.stanza, features)
+
     if broken_hash:
         # make the hash break!
-        feature = query.addElement('feature')
-        feature['var'] = 'http://broken-feature'
+        query = result.firstChildElement()
+        query.addElement('feature')['var'] = 'http://example.com/another-feature'
+
     stream.send(result)
 
     if broken_hash:
@@ -283,14 +259,7 @@ def test_two_clients(q, bus, conn, stream, contact1, contact2,
         assert caps_changed_flag == False
 
         # send good reply
-        result = make_result_iq(stream, event.stanza)
-        query = result.firstChildElement()
-        feature = query.addElement('feature')
-        feature['var'] = 'http://jabber.org/protocol/jingle'
-        feature = query.addElement('feature')
-        feature['var'] = 'http://jabber.org/protocol/jingle/description/audio'
-        feature = query.addElement('feature')
-        feature['var'] = 'http://www.google.com/transport/p2p'
+        result = make_caps_disco_reply(stream, event.stanza, features)
         stream.send(result)
 
     # we can now do audio calls with both contacts
-- 
1.5.6.5




More information about the telepathy-commits mailing list