2010-06-27 21:33:43 +00:00
|
|
|
"""
|
|
|
|
SleekXMPP: The Sleek XMPP Library
|
|
|
|
Copyright (C) 2010 Nathanael C. Fritz, Lance J.T. Stout
|
|
|
|
This file is part of SleekXMPP.
|
|
|
|
|
2010-07-20 15:19:49 +00:00
|
|
|
See the file LICENSE for copying permission.
|
2010-06-27 21:33:43 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
import unittest
|
2012-02-20 04:27:19 +00:00
|
|
|
from xml.parsers.expat import ExpatError
|
2010-08-12 05:25:42 +00:00
|
|
|
|
2010-10-06 22:10:04 +00:00
|
|
|
from sleekxmpp import ClientXMPP, ComponentXMPP
|
2012-07-24 09:39:54 +00:00
|
|
|
from sleekxmpp.util import Queue
|
2010-08-12 05:25:42 +00:00
|
|
|
from sleekxmpp.stanza import Message, Iq, Presence
|
2010-10-07 23:43:51 +00:00
|
|
|
from sleekxmpp.test import TestSocket, TestLiveSocket
|
2013-07-26 11:04:08 +00:00
|
|
|
from sleekxmpp.xmlstream import ET
|
|
|
|
from sleekxmpp.xmlstream import ElementBase
|
2010-08-06 16:04:52 +00:00
|
|
|
from sleekxmpp.xmlstream.tostring import tostring
|
2014-02-04 01:15:08 +00:00
|
|
|
from sleekxmpp.xmlstream.matcher import StanzaPath, MatcherId, MatchIDSender
|
2010-11-17 18:37:03 +00:00
|
|
|
from sleekxmpp.xmlstream.matcher import MatchXMLMask, MatchXPath
|
2010-07-19 17:58:53 +00:00
|
|
|
|
2010-06-27 21:33:43 +00:00
|
|
|
|
|
|
|
class SleekTest(unittest.TestCase):
|
2010-08-12 05:25:42 +00:00
|
|
|
|
2010-06-27 21:33:43 +00:00
|
|
|
"""
|
|
|
|
A SleekXMPP specific TestCase class that provides
|
|
|
|
methods for comparing message, iq, and presence stanzas.
|
2010-08-12 05:25:42 +00:00
|
|
|
|
|
|
|
Methods:
|
2010-10-07 13:22:27 +00:00
|
|
|
Message -- Create a Message stanza object.
|
|
|
|
Iq -- Create an Iq stanza object.
|
|
|
|
Presence -- Create a Presence stanza object.
|
2010-11-05 18:45:58 +00:00
|
|
|
check_jid -- Check a JID and its component parts.
|
|
|
|
check -- Compare a stanza against an XML string.
|
2010-10-07 13:22:27 +00:00
|
|
|
stream_start -- Initialize a dummy XMPP client.
|
|
|
|
stream_close -- Disconnect the XMPP client.
|
2010-11-05 18:45:58 +00:00
|
|
|
make_header -- Create a stream header.
|
|
|
|
send_header -- Check that the given header has been sent.
|
|
|
|
send_feature -- Send a raw XML element.
|
|
|
|
send -- Check that the XMPP client sent the given
|
|
|
|
generic stanza.
|
|
|
|
recv -- Queue data for XMPP client to receive, or
|
|
|
|
verify the data that was received from a
|
|
|
|
live connection.
|
|
|
|
recv_header -- Check that a given stream header
|
|
|
|
was received.
|
|
|
|
recv_feature -- Check that a given, raw XML element
|
|
|
|
was recveived.
|
2010-10-07 13:22:27 +00:00
|
|
|
fix_namespaces -- Add top-level namespace to an XML object.
|
|
|
|
compare -- Compare XML objects against each other.
|
2010-06-27 21:33:43 +00:00
|
|
|
"""
|
|
|
|
|
2010-12-07 22:19:39 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
unittest.TestCase.__init__(self, *args, **kwargs)
|
|
|
|
self.xmpp = None
|
|
|
|
|
2010-10-07 23:43:51 +00:00
|
|
|
def parse_xml(self, xml_string):
|
|
|
|
try:
|
|
|
|
xml = ET.fromstring(xml_string)
|
|
|
|
return xml
|
2012-02-20 04:27:19 +00:00
|
|
|
except (SyntaxError, ExpatError) as e:
|
|
|
|
msg = e.msg if hasattr(e, 'msg') else e.message
|
|
|
|
if 'unbound' in msg:
|
2010-10-07 23:43:51 +00:00
|
|
|
known_prefixes = {
|
|
|
|
'stream': 'http://etherx.jabber.org/streams'}
|
|
|
|
|
|
|
|
prefix = xml_string.split('<')[1].split(':')[0]
|
|
|
|
if prefix in known_prefixes:
|
|
|
|
xml_string = '<fixns xmlns:%s="%s">%s</fixns>' % (
|
|
|
|
prefix,
|
|
|
|
known_prefixes[prefix],
|
|
|
|
xml_string)
|
|
|
|
xml = self.parse_xml(xml_string)
|
2012-06-19 16:47:31 +00:00
|
|
|
xml = list(xml)[0]
|
2010-10-07 23:43:51 +00:00
|
|
|
return xml
|
2010-12-08 04:04:37 +00:00
|
|
|
else:
|
|
|
|
self.fail("XML data was mal-formed:\n%s" % xml_string)
|
2010-10-07 23:43:51 +00:00
|
|
|
|
2010-07-14 19:24:37 +00:00
|
|
|
# ------------------------------------------------------------------
|
|
|
|
# Shortcut methods for creating stanza objects
|
|
|
|
|
2010-06-27 21:33:43 +00:00
|
|
|
def Message(self, *args, **kwargs):
|
2010-08-12 05:25:42 +00:00
|
|
|
"""
|
|
|
|
Create a Message stanza.
|
|
|
|
|
|
|
|
Uses same arguments as StanzaBase.__init__
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
xml -- An XML object to use for the Message's values.
|
|
|
|
"""
|
2010-12-07 22:19:39 +00:00
|
|
|
return Message(self.xmpp, *args, **kwargs)
|
2010-06-27 21:33:43 +00:00
|
|
|
|
|
|
|
def Iq(self, *args, **kwargs):
|
2010-08-12 05:25:42 +00:00
|
|
|
"""
|
|
|
|
Create an Iq stanza.
|
|
|
|
|
|
|
|
Uses same arguments as StanzaBase.__init__
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
xml -- An XML object to use for the Iq's values.
|
|
|
|
"""
|
2010-12-07 22:19:39 +00:00
|
|
|
return Iq(self.xmpp, *args, **kwargs)
|
2010-06-27 21:33:43 +00:00
|
|
|
|
|
|
|
def Presence(self, *args, **kwargs):
|
2010-08-12 05:25:42 +00:00
|
|
|
"""
|
|
|
|
Create a Presence stanza.
|
|
|
|
|
|
|
|
Uses same arguments as StanzaBase.__init__
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
xml -- An XML object to use for the Iq's values.
|
|
|
|
"""
|
2010-12-07 22:19:39 +00:00
|
|
|
return Presence(self.xmpp, *args, **kwargs)
|
2010-06-27 21:33:43 +00:00
|
|
|
|
2010-11-05 18:45:58 +00:00
|
|
|
def check_jid(self, jid, user=None, domain=None, resource=None,
|
|
|
|
bare=None, full=None, string=None):
|
2010-10-24 22:22:41 +00:00
|
|
|
"""
|
|
|
|
Verify the components of a JID.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
jid -- The JID object to test.
|
|
|
|
user -- Optional. The user name portion of the JID.
|
|
|
|
domain -- Optional. The domain name portion of the JID.
|
|
|
|
resource -- Optional. The resource portion of the JID.
|
|
|
|
bare -- Optional. The bare JID.
|
|
|
|
full -- Optional. The full JID.
|
|
|
|
string -- Optional. The string version of the JID.
|
|
|
|
"""
|
|
|
|
if user is not None:
|
|
|
|
self.assertEqual(jid.user, user,
|
|
|
|
"User does not match: %s" % jid.user)
|
|
|
|
if domain is not None:
|
|
|
|
self.assertEqual(jid.domain, domain,
|
|
|
|
"Domain does not match: %s" % jid.domain)
|
|
|
|
if resource is not None:
|
|
|
|
self.assertEqual(jid.resource, resource,
|
|
|
|
"Resource does not match: %s" % jid.resource)
|
|
|
|
if bare is not None:
|
|
|
|
self.assertEqual(jid.bare, bare,
|
|
|
|
"Bare JID does not match: %s" % jid.bare)
|
|
|
|
if full is not None:
|
|
|
|
self.assertEqual(jid.full, full,
|
|
|
|
"Full JID does not match: %s" % jid.full)
|
|
|
|
if string is not None:
|
|
|
|
self.assertEqual(str(jid), string,
|
|
|
|
"String does not match: %s" % str(jid))
|
|
|
|
|
2010-10-27 03:47:17 +00:00
|
|
|
def check_roster(self, owner, jid, name=None, subscription=None,
|
|
|
|
afrom=None, ato=None, pending_out=None, pending_in=None,
|
|
|
|
groups=None):
|
2010-10-27 12:09:50 +00:00
|
|
|
roster = self.xmpp.roster[owner][jid]
|
2010-10-27 03:47:17 +00:00
|
|
|
if name is not None:
|
|
|
|
self.assertEqual(roster['name'], name,
|
|
|
|
"Incorrect name value: %s" % roster['name'])
|
|
|
|
if subscription is not None:
|
|
|
|
self.assertEqual(roster['subscription'], subscription,
|
|
|
|
"Incorrect subscription: %s" % roster['subscription'])
|
|
|
|
if afrom is not None:
|
|
|
|
self.assertEqual(roster['from'], afrom,
|
|
|
|
"Incorrect from state: %s" % roster['from'])
|
|
|
|
if ato is not None:
|
|
|
|
self.assertEqual(roster['to'], ato,
|
|
|
|
"Incorrect to state: %s" % roster['to'])
|
|
|
|
if pending_out is not None:
|
|
|
|
self.assertEqual(roster['pending_out'], pending_out,
|
|
|
|
"Incorrect pending_out state: %s" % roster['pending_out'])
|
|
|
|
if pending_in is not None:
|
|
|
|
self.assertEqual(roster['pending_in'], pending_out,
|
|
|
|
"Incorrect pending_in state: %s" % roster['pending_in'])
|
|
|
|
if groups is not None:
|
|
|
|
self.assertEqual(roster['groups'], groups,
|
|
|
|
"Incorrect groups: %s" % roster['groups'])
|
|
|
|
|
2010-07-14 19:24:37 +00:00
|
|
|
# ------------------------------------------------------------------
|
|
|
|
# Methods for comparing stanza objects to XML strings
|
|
|
|
|
2010-11-17 18:37:03 +00:00
|
|
|
def check(self, stanza, criteria, method='exact',
|
2010-11-05 18:45:58 +00:00
|
|
|
defaults=None, use_values=True):
|
2010-08-13 16:23:34 +00:00
|
|
|
"""
|
|
|
|
Create and compare several stanza objects to a correct XML string.
|
|
|
|
|
2011-01-26 16:27:41 +00:00
|
|
|
If use_values is False, tests using stanza.values will not be used.
|
2010-08-13 16:23:34 +00:00
|
|
|
|
|
|
|
Some stanzas provide default values for some interfaces, but
|
|
|
|
these defaults can be problematic for testing since they can easily
|
|
|
|
be forgotten when supplying the XML string. A list of interfaces that
|
|
|
|
use defaults may be provided and the generated stanzas will use the
|
|
|
|
default values for those interfaces if needed.
|
|
|
|
|
|
|
|
However, correcting the supplied XML is not possible for interfaces
|
|
|
|
that add or remove XML elements. Only interfaces that map to XML
|
|
|
|
attributes may be set using the defaults parameter. The supplied XML
|
|
|
|
must take into account any extra elements that are included by default.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
stanza -- The stanza object to test.
|
2010-11-17 18:37:03 +00:00
|
|
|
criteria -- An expression the stanza must match against.
|
2010-12-08 04:04:37 +00:00
|
|
|
method -- The type of matching to use; one of:
|
2010-11-17 18:37:03 +00:00
|
|
|
'exact', 'mask', 'id', 'xpath', and 'stanzapath'.
|
|
|
|
Defaults to the value of self.match_method.
|
2010-08-13 16:23:34 +00:00
|
|
|
defaults -- A list of stanza interfaces that have default
|
|
|
|
values. These interfaces will be set to their
|
|
|
|
defaults for the given and generated stanzas to
|
|
|
|
prevent unexpected test failures.
|
2011-01-26 16:27:41 +00:00
|
|
|
use_values -- Indicates if testing using stanza.values should
|
|
|
|
be used. Defaults to True.
|
2010-08-13 16:23:34 +00:00
|
|
|
"""
|
2010-11-17 18:37:03 +00:00
|
|
|
if method is None and hasattr(self, 'match_method'):
|
|
|
|
method = getattr(self, 'match_method')
|
|
|
|
|
|
|
|
if method != 'exact':
|
|
|
|
matchers = {'stanzapath': StanzaPath,
|
|
|
|
'xpath': MatchXPath,
|
|
|
|
'mask': MatchXMLMask,
|
2014-02-04 01:15:08 +00:00
|
|
|
'idsender': MatchIDSender,
|
2010-11-17 18:37:03 +00:00
|
|
|
'id': MatcherId}
|
|
|
|
Matcher = matchers.get(method, None)
|
|
|
|
if Matcher is None:
|
|
|
|
raise ValueError("Unknown matching method.")
|
|
|
|
test = Matcher(criteria)
|
|
|
|
self.failUnless(test.match(stanza),
|
|
|
|
"Stanza did not match using %s method:\n" % method + \
|
|
|
|
"Criteria:\n%s\n" % str(criteria) + \
|
|
|
|
"Stanza:\n%s" % str(stanza))
|
2010-08-13 16:23:34 +00:00
|
|
|
else:
|
2010-11-17 18:37:03 +00:00
|
|
|
stanza_class = stanza.__class__
|
2011-05-20 21:42:40 +00:00
|
|
|
if not isinstance(criteria, ElementBase):
|
2010-11-17 21:07:27 +00:00
|
|
|
xml = self.parse_xml(criteria)
|
|
|
|
else:
|
|
|
|
xml = criteria.xml
|
2010-08-13 16:23:34 +00:00
|
|
|
|
2010-11-17 18:37:03 +00:00
|
|
|
# Ensure that top level namespaces are used, even if they
|
|
|
|
# were not provided.
|
|
|
|
self.fix_namespaces(stanza.xml, 'jabber:client')
|
|
|
|
self.fix_namespaces(xml, 'jabber:client')
|
|
|
|
|
|
|
|
stanza2 = stanza_class(xml=xml)
|
|
|
|
|
|
|
|
if use_values:
|
2011-01-26 16:27:41 +00:00
|
|
|
# Using stanza.values will add XML for any interface that
|
|
|
|
# has a default value. We need to set those defaults on
|
|
|
|
# the existing stanzas and XML so that they will compare
|
|
|
|
# correctly.
|
2010-11-17 18:37:03 +00:00
|
|
|
default_stanza = stanza_class()
|
|
|
|
if defaults is None:
|
|
|
|
known_defaults = {
|
|
|
|
Message: ['type'],
|
|
|
|
Presence: ['priority']
|
|
|
|
}
|
|
|
|
defaults = known_defaults.get(stanza_class, [])
|
|
|
|
for interface in defaults:
|
|
|
|
stanza[interface] = stanza[interface]
|
|
|
|
stanza2[interface] = stanza2[interface]
|
|
|
|
# Can really only automatically add defaults for top
|
|
|
|
# level attribute values. Anything else must be accounted
|
|
|
|
# for in the provided XML string.
|
|
|
|
if interface not in xml.attrib:
|
|
|
|
if interface in default_stanza.xml.attrib:
|
|
|
|
value = default_stanza.xml.attrib[interface]
|
|
|
|
xml.attrib[interface] = value
|
|
|
|
|
2011-01-26 16:27:41 +00:00
|
|
|
values = stanza2.values
|
2010-11-17 18:37:03 +00:00
|
|
|
stanza3 = stanza_class()
|
2011-01-26 16:27:41 +00:00
|
|
|
stanza3.values = values
|
2010-11-17 18:37:03 +00:00
|
|
|
|
|
|
|
debug = "Three methods for creating stanzas do not match.\n"
|
|
|
|
debug += "Given XML:\n%s\n" % tostring(xml)
|
|
|
|
debug += "Given stanza:\n%s\n" % tostring(stanza.xml)
|
|
|
|
debug += "Generated stanza:\n%s\n" % tostring(stanza2.xml)
|
|
|
|
debug += "Second generated stanza:\n%s\n" % tostring(stanza3.xml)
|
|
|
|
result = self.compare(xml, stanza.xml, stanza2.xml, stanza3.xml)
|
|
|
|
else:
|
|
|
|
debug = "Two methods for creating stanzas do not match.\n"
|
|
|
|
debug += "Given XML:\n%s\n" % tostring(xml)
|
|
|
|
debug += "Given stanza:\n%s\n" % tostring(stanza.xml)
|
|
|
|
debug += "Generated stanza:\n%s\n" % tostring(stanza2.xml)
|
|
|
|
result = self.compare(xml, stanza.xml, stanza2.xml)
|
|
|
|
|
|
|
|
self.failUnless(result, debug)
|
2010-08-13 16:23:34 +00:00
|
|
|
|
2010-07-14 19:24:37 +00:00
|
|
|
# ------------------------------------------------------------------
|
|
|
|
# Methods for simulating stanza streams.
|
|
|
|
|
2011-05-27 18:01:30 +00:00
|
|
|
def stream_disconnect(self):
|
|
|
|
"""
|
|
|
|
Simulate a stream disconnection.
|
|
|
|
"""
|
|
|
|
if self.xmpp:
|
|
|
|
self.xmpp.socket.disconnect_error()
|
|
|
|
|
2014-08-17 22:52:24 +00:00
|
|
|
def stream_start(self, mode='client', skip=True, header=None, socket='mock', jid='tester@localhost',
|
|
|
|
password='test', server='localhost', port=5222, sasl_mech=None, plugins=None, plugin_config=None):
|
2010-08-12 05:25:42 +00:00
|
|
|
"""
|
|
|
|
Initialize an XMPP client or component using a dummy XML stream.
|
|
|
|
|
|
|
|
Arguments:
|
2010-10-07 23:43:51 +00:00
|
|
|
mode -- Either 'client' or 'component'. Defaults to 'client'.
|
|
|
|
skip -- Indicates if the first item in the sent queue (the
|
|
|
|
stream header) should be removed. Tests that wish
|
|
|
|
to test initializing the stream should set this to
|
|
|
|
False. Otherwise, the default of True should be used.
|
|
|
|
socket -- Either 'mock' or 'live' to indicate if the socket
|
|
|
|
should be a dummy, mock socket or a live, functioning
|
|
|
|
socket. Defaults to 'mock'.
|
|
|
|
jid -- The JID to use for the connection.
|
|
|
|
Defaults to 'tester@localhost'.
|
|
|
|
password -- The password to use for the connection.
|
|
|
|
Defaults to 'test'.
|
|
|
|
server -- The name of the XMPP server. Defaults to 'localhost'.
|
|
|
|
port -- The port to use when connecting to the server.
|
|
|
|
Defaults to 5222.
|
2010-12-08 04:04:37 +00:00
|
|
|
plugins -- List of plugins to register. By default, all plugins
|
2010-11-18 20:50:45 +00:00
|
|
|
are loaded.
|
2010-08-12 05:25:42 +00:00
|
|
|
"""
|
2014-08-17 22:52:24 +00:00
|
|
|
if not plugin_config:
|
|
|
|
plugin_config = {}
|
|
|
|
|
2010-07-14 19:24:37 +00:00
|
|
|
if mode == 'client':
|
2011-09-29 02:48:30 +00:00
|
|
|
self.xmpp = ClientXMPP(jid, password,
|
|
|
|
sasl_mech=sasl_mech,
|
|
|
|
plugin_config=plugin_config)
|
2010-10-06 22:10:04 +00:00
|
|
|
elif mode == 'component':
|
2010-10-07 23:43:51 +00:00
|
|
|
self.xmpp = ComponentXMPP(jid, password,
|
2011-09-28 21:26:29 +00:00
|
|
|
server, port,
|
|
|
|
plugin_config=plugin_config)
|
2010-10-06 22:10:04 +00:00
|
|
|
else:
|
|
|
|
raise ValueError("Unknown XMPP connection mode.")
|
2010-07-14 19:24:37 +00:00
|
|
|
|
2012-04-09 01:15:53 +00:00
|
|
|
# Remove unique ID prefix to make it easier to test
|
|
|
|
self.xmpp._id_prefix = ''
|
2012-04-23 01:13:36 +00:00
|
|
|
self.xmpp._disconnect_wait_for_threads = False
|
2012-06-05 23:54:26 +00:00
|
|
|
self.xmpp.default_lang = None
|
|
|
|
self.xmpp.peer_default_lang = None
|
|
|
|
|
2010-11-17 20:45:16 +00:00
|
|
|
# We will use this to wait for the session_start event
|
|
|
|
# for live connections.
|
2012-07-24 09:39:54 +00:00
|
|
|
skip_queue = Queue()
|
2010-11-17 20:45:16 +00:00
|
|
|
|
2010-10-07 23:43:51 +00:00
|
|
|
if socket == 'mock':
|
|
|
|
self.xmpp.set_socket(TestSocket())
|
|
|
|
|
|
|
|
# Simulate connecting for mock sockets.
|
2010-10-14 22:53:10 +00:00
|
|
|
self.xmpp.auto_reconnect = False
|
|
|
|
self.xmpp.state._set_state('connected')
|
2010-10-07 23:43:51 +00:00
|
|
|
|
|
|
|
# Must have the stream header ready for xmpp.process() to work.
|
|
|
|
if not header:
|
|
|
|
header = self.xmpp.stream_header
|
|
|
|
self.xmpp.socket.recv_data(header)
|
|
|
|
elif socket == 'live':
|
|
|
|
self.xmpp.socket_class = TestLiveSocket
|
2011-08-05 05:37:22 +00:00
|
|
|
|
2010-11-17 20:45:16 +00:00
|
|
|
def wait_for_session(x):
|
|
|
|
self.xmpp.socket.clear()
|
|
|
|
skip_queue.put('started')
|
2011-08-05 05:37:22 +00:00
|
|
|
|
2010-11-17 20:45:16 +00:00
|
|
|
self.xmpp.add_event_handler('session_start', wait_for_session)
|
2011-09-28 21:26:29 +00:00
|
|
|
if server is not None:
|
|
|
|
self.xmpp.connect((server, port))
|
|
|
|
else:
|
|
|
|
self.xmpp.connect()
|
2010-10-07 23:43:51 +00:00
|
|
|
else:
|
|
|
|
raise ValueError("Unknown socket type.")
|
2010-07-14 19:24:37 +00:00
|
|
|
|
2010-11-18 20:50:45 +00:00
|
|
|
if plugins is None:
|
|
|
|
self.xmpp.register_plugins()
|
|
|
|
else:
|
|
|
|
for plugin in plugins:
|
|
|
|
self.xmpp.register_plugin(plugin)
|
2012-09-25 19:35:53 +00:00
|
|
|
|
|
|
|
# Some plugins require messages to have ID values. Set
|
|
|
|
# this to True in tests related to those plugins.
|
|
|
|
self.xmpp.use_message_ids = False
|
|
|
|
|
2010-07-14 19:24:37 +00:00
|
|
|
self.xmpp.process(threaded=True)
|
2010-08-03 22:31:22 +00:00
|
|
|
if skip:
|
2010-11-17 20:45:16 +00:00
|
|
|
if socket != 'live':
|
2011-05-27 23:59:52 +00:00
|
|
|
# Mark send queue as usable
|
2014-02-14 21:50:21 +00:00
|
|
|
self.xmpp.session_bind_event.set()
|
2011-05-27 23:59:52 +00:00
|
|
|
self.xmpp.session_started_event.set()
|
2010-11-17 20:45:16 +00:00
|
|
|
# Clear startup stanzas
|
2010-10-07 23:43:51 +00:00
|
|
|
self.xmpp.socket.next_sent(timeout=1)
|
2010-11-17 20:45:16 +00:00
|
|
|
if mode == 'component':
|
|
|
|
self.xmpp.socket.next_sent(timeout=1)
|
|
|
|
else:
|
|
|
|
skip_queue.get(block=True, timeout=10)
|
2010-07-14 19:24:37 +00:00
|
|
|
|
2010-11-05 18:45:58 +00:00
|
|
|
def make_header(self, sto='',
|
|
|
|
sfrom='',
|
|
|
|
sid='',
|
|
|
|
stream_ns="http://etherx.jabber.org/streams",
|
|
|
|
default_ns="jabber:client",
|
2012-06-05 23:54:26 +00:00
|
|
|
default_lang="en",
|
2010-11-05 18:45:58 +00:00
|
|
|
version="1.0",
|
|
|
|
xml_header=True):
|
2010-10-06 22:10:04 +00:00
|
|
|
"""
|
|
|
|
Create a stream header to be received by the test XMPP agent.
|
|
|
|
|
2010-10-07 13:22:27 +00:00
|
|
|
The header must be saved and passed to stream_start.
|
2010-10-06 22:10:04 +00:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
sto -- The recipient of the stream header.
|
|
|
|
sfrom -- The agent sending the stream header.
|
|
|
|
sid -- The stream's id.
|
|
|
|
stream_ns -- The namespace of the stream's root element.
|
|
|
|
default_ns -- The default stanza namespace.
|
|
|
|
version -- The stream version.
|
|
|
|
xml_header -- Indicates if the XML version header should be
|
|
|
|
appended before the stream header.
|
|
|
|
"""
|
|
|
|
header = '<stream:stream %s>'
|
2010-10-06 22:46:23 +00:00
|
|
|
parts = []
|
2010-10-06 22:10:04 +00:00
|
|
|
if xml_header:
|
|
|
|
header = '<?xml version="1.0"?>' + header
|
|
|
|
if sto:
|
|
|
|
parts.append('to="%s"' % sto)
|
|
|
|
if sfrom:
|
|
|
|
parts.append('from="%s"' % sfrom)
|
|
|
|
if sid:
|
|
|
|
parts.append('id="%s"' % sid)
|
2012-06-05 23:54:26 +00:00
|
|
|
if default_lang:
|
|
|
|
parts.append('xml:lang="%s"' % default_lang)
|
2010-10-06 22:10:04 +00:00
|
|
|
parts.append('version="%s"' % version)
|
|
|
|
parts.append('xmlns:stream="%s"' % stream_ns)
|
|
|
|
parts.append('xmlns="%s"' % default_ns)
|
|
|
|
return header % ' '.join(parts)
|
|
|
|
|
2014-08-17 22:52:24 +00:00
|
|
|
def recv(self, data, defaults=None, method='exact', use_values=True, timeout=1):
|
2010-10-07 23:43:51 +00:00
|
|
|
"""
|
|
|
|
Pass data to the dummy XMPP client as if it came from an XMPP server.
|
|
|
|
|
|
|
|
If using a live connection, verify what the server has sent.
|
|
|
|
|
|
|
|
Arguments:
|
2010-11-17 18:37:03 +00:00
|
|
|
data -- If a dummy socket is being used, the XML that is to
|
|
|
|
be received next. Otherwise it is the criteria used
|
|
|
|
to match against live data that is received.
|
2010-10-07 23:43:51 +00:00
|
|
|
defaults -- A list of stanza interfaces with default values that
|
|
|
|
may interfere with comparisons.
|
2010-11-17 18:37:03 +00:00
|
|
|
method -- Select the type of comparison to use for
|
|
|
|
verifying the received stanza. Options are 'exact',
|
|
|
|
'id', 'stanzapath', 'xpath', and 'mask'.
|
|
|
|
Defaults to the value of self.match_method.
|
2010-10-07 23:43:51 +00:00
|
|
|
use_values -- Indicates if stanza comparisons should test using
|
2011-01-26 16:27:41 +00:00
|
|
|
stanza.values. Defaults to True.
|
2010-10-07 23:43:51 +00:00
|
|
|
timeout -- Time to wait in seconds for data to be received by
|
|
|
|
a live connection.
|
|
|
|
"""
|
2014-08-17 22:52:24 +00:00
|
|
|
if not defaults:
|
|
|
|
defaults = []
|
|
|
|
|
2010-10-07 23:43:51 +00:00
|
|
|
if self.xmpp.socket.is_live:
|
|
|
|
# we are working with a live connection, so we should
|
|
|
|
# verify what has been received instead of simulating
|
|
|
|
# receiving data.
|
|
|
|
recv_data = self.xmpp.socket.next_recv(timeout)
|
|
|
|
if recv_data is None:
|
2010-12-08 04:04:37 +00:00
|
|
|
self.fail("No stanza was received.")
|
2010-11-17 18:37:03 +00:00
|
|
|
xml = self.parse_xml(recv_data)
|
|
|
|
self.fix_namespaces(xml, 'jabber:client')
|
|
|
|
stanza = self.xmpp._build_stanza(xml, 'jabber:client')
|
|
|
|
self.check(stanza, data,
|
|
|
|
method=method,
|
|
|
|
defaults=defaults,
|
|
|
|
use_values=use_values)
|
2010-10-07 23:43:51 +00:00
|
|
|
else:
|
|
|
|
# place the data in the dummy socket receiving queue.
|
|
|
|
data = str(data)
|
|
|
|
self.xmpp.socket.recv_data(data)
|
|
|
|
|
2010-11-05 18:45:58 +00:00
|
|
|
def recv_header(self, sto='',
|
|
|
|
sfrom='',
|
|
|
|
sid='',
|
|
|
|
stream_ns="http://etherx.jabber.org/streams",
|
|
|
|
default_ns="jabber:client",
|
|
|
|
version="1.0",
|
|
|
|
xml_header=False,
|
|
|
|
timeout=1):
|
2010-10-07 23:43:51 +00:00
|
|
|
"""
|
|
|
|
Check that a given stream header was received.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
sto -- The recipient of the stream header.
|
|
|
|
sfrom -- The agent sending the stream header.
|
|
|
|
sid -- The stream's id. Set to None to ignore.
|
|
|
|
stream_ns -- The namespace of the stream's root element.
|
|
|
|
default_ns -- The default stanza namespace.
|
|
|
|
version -- The stream version.
|
|
|
|
xml_header -- Indicates if the XML version header should be
|
|
|
|
appended before the stream header.
|
|
|
|
timeout -- Length of time to wait in seconds for a
|
|
|
|
response.
|
|
|
|
"""
|
2010-11-05 18:45:58 +00:00
|
|
|
header = self.make_header(sto, sfrom, sid,
|
|
|
|
stream_ns=stream_ns,
|
|
|
|
default_ns=default_ns,
|
|
|
|
version=version,
|
|
|
|
xml_header=xml_header)
|
2010-10-07 23:43:51 +00:00
|
|
|
recv_header = self.xmpp.socket.next_recv(timeout)
|
|
|
|
if recv_header is None:
|
|
|
|
raise ValueError("Socket did not return data.")
|
|
|
|
|
|
|
|
# Apply closing elements so that we can construct
|
|
|
|
# XML objects for comparison.
|
|
|
|
header2 = header + '</stream:stream>'
|
|
|
|
recv_header2 = recv_header + '</stream:stream>'
|
|
|
|
|
|
|
|
xml = self.parse_xml(header2)
|
|
|
|
recv_xml = self.parse_xml(recv_header2)
|
|
|
|
|
|
|
|
if sid is None:
|
|
|
|
# Ignore the id sent by the server since
|
|
|
|
# we can't know in advance what it will be.
|
|
|
|
if 'id' in recv_xml.attrib:
|
|
|
|
del recv_xml.attrib['id']
|
|
|
|
|
|
|
|
# Ignore the xml:lang attribute for now.
|
|
|
|
if 'xml:lang' in recv_xml.attrib:
|
|
|
|
del recv_xml.attrib['xml:lang']
|
|
|
|
xml_ns = 'http://www.w3.org/XML/1998/namespace'
|
|
|
|
if '{%s}lang' % xml_ns in recv_xml.attrib:
|
|
|
|
del recv_xml.attrib['{%s}lang' % xml_ns]
|
|
|
|
|
2012-06-19 16:47:31 +00:00
|
|
|
if list(recv_xml):
|
2010-10-07 23:43:51 +00:00
|
|
|
# We received more than just the header
|
2012-06-19 16:47:31 +00:00
|
|
|
for xml in recv_xml:
|
2010-10-07 23:43:51 +00:00
|
|
|
self.xmpp.socket.recv_data(tostring(xml))
|
|
|
|
|
|
|
|
attrib = recv_xml.attrib
|
|
|
|
recv_xml.clear()
|
|
|
|
recv_xml.attrib = attrib
|
|
|
|
|
|
|
|
self.failUnless(
|
|
|
|
self.compare(xml, recv_xml),
|
|
|
|
"Stream headers do not match:\nDesired:\n%s\nReceived:\n%s" % (
|
|
|
|
'%s %s' % (xml.tag, xml.attrib),
|
|
|
|
'%s %s' % (recv_xml.tag, recv_xml.attrib)))
|
|
|
|
|
2010-11-17 18:37:03 +00:00
|
|
|
def recv_feature(self, data, method='mask', use_values=True, timeout=1):
|
2010-10-07 23:43:51 +00:00
|
|
|
"""
|
|
|
|
"""
|
2010-11-17 18:37:03 +00:00
|
|
|
if method is None and hasattr(self, 'match_method'):
|
|
|
|
method = getattr(self, 'match_method')
|
|
|
|
|
2010-10-07 23:43:51 +00:00
|
|
|
if self.xmpp.socket.is_live:
|
|
|
|
# we are working with a live connection, so we should
|
|
|
|
# verify what has been received instead of simulating
|
|
|
|
# receiving data.
|
|
|
|
recv_data = self.xmpp.socket.next_recv(timeout)
|
|
|
|
xml = self.parse_xml(data)
|
|
|
|
recv_xml = self.parse_xml(recv_data)
|
2010-11-17 18:37:03 +00:00
|
|
|
if recv_data is None:
|
2010-12-08 04:04:37 +00:00
|
|
|
self.fail("No stanza was received.")
|
2010-11-17 18:37:03 +00:00
|
|
|
if method == 'exact':
|
|
|
|
self.failUnless(self.compare(xml, recv_xml),
|
|
|
|
"Features do not match.\nDesired:\n%s\nReceived:\n%s" % (
|
|
|
|
tostring(xml), tostring(recv_xml)))
|
|
|
|
elif method == 'mask':
|
|
|
|
matcher = MatchXMLMask(xml)
|
2010-12-08 04:04:37 +00:00
|
|
|
self.failUnless(matcher.match(recv_xml),
|
2010-11-17 18:37:03 +00:00
|
|
|
"Stanza did not match using %s method:\n" % method + \
|
|
|
|
"Criteria:\n%s\n" % tostring(xml) + \
|
|
|
|
"Stanza:\n%s" % tostring(recv_xml))
|
|
|
|
else:
|
|
|
|
raise ValueError("Uknown matching method: %s" % method)
|
2010-10-07 23:43:51 +00:00
|
|
|
else:
|
|
|
|
# place the data in the dummy socket receiving queue.
|
|
|
|
data = str(data)
|
|
|
|
self.xmpp.socket.recv_data(data)
|
|
|
|
|
2010-11-05 18:45:58 +00:00
|
|
|
def send_header(self, sto='',
|
|
|
|
sfrom='',
|
|
|
|
sid='',
|
|
|
|
stream_ns="http://etherx.jabber.org/streams",
|
|
|
|
default_ns="jabber:client",
|
2012-06-05 23:54:26 +00:00
|
|
|
default_lang="en",
|
2010-11-05 18:45:58 +00:00
|
|
|
version="1.0",
|
|
|
|
xml_header=False,
|
|
|
|
timeout=1):
|
2010-10-06 22:10:04 +00:00
|
|
|
"""
|
|
|
|
Check that a given stream header was sent.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
sto -- The recipient of the stream header.
|
|
|
|
sfrom -- The agent sending the stream header.
|
|
|
|
sid -- The stream's id.
|
|
|
|
stream_ns -- The namespace of the stream's root element.
|
|
|
|
default_ns -- The default stanza namespace.
|
|
|
|
version -- The stream version.
|
|
|
|
xml_header -- Indicates if the XML version header should be
|
|
|
|
appended before the stream header.
|
|
|
|
timeout -- Length of time to wait in seconds for a
|
|
|
|
response.
|
|
|
|
"""
|
2010-11-05 18:45:58 +00:00
|
|
|
header = self.make_header(sto, sfrom, sid,
|
|
|
|
stream_ns=stream_ns,
|
|
|
|
default_ns=default_ns,
|
2012-06-05 23:54:26 +00:00
|
|
|
default_lang=default_lang,
|
2010-11-05 18:45:58 +00:00
|
|
|
version=version,
|
|
|
|
xml_header=xml_header)
|
2010-10-07 13:22:27 +00:00
|
|
|
sent_header = self.xmpp.socket.next_sent(timeout)
|
2010-10-06 22:10:04 +00:00
|
|
|
if sent_header is None:
|
|
|
|
raise ValueError("Socket did not return data.")
|
|
|
|
|
|
|
|
# Apply closing elements so that we can construct
|
|
|
|
# XML objects for comparison.
|
2010-10-06 22:46:23 +00:00
|
|
|
header2 = header + '</stream:stream>'
|
2010-10-14 23:08:50 +00:00
|
|
|
sent_header2 = sent_header + b'</stream:stream>'
|
2010-10-06 22:10:04 +00:00
|
|
|
|
2010-10-07 23:43:51 +00:00
|
|
|
xml = self.parse_xml(header2)
|
|
|
|
sent_xml = self.parse_xml(sent_header2)
|
2010-10-06 22:10:04 +00:00
|
|
|
|
|
|
|
self.failUnless(
|
|
|
|
self.compare(xml, sent_xml),
|
|
|
|
"Stream headers do not match:\nDesired:\n%s\nSent:\n%s" % (
|
|
|
|
header, sent_header))
|
|
|
|
|
2010-11-17 18:37:03 +00:00
|
|
|
def send_feature(self, data, method='mask', use_values=True, timeout=1):
|
2010-10-07 23:43:51 +00:00
|
|
|
"""
|
|
|
|
"""
|
|
|
|
sent_data = self.xmpp.socket.next_sent(timeout)
|
|
|
|
xml = self.parse_xml(data)
|
|
|
|
sent_xml = self.parse_xml(sent_data)
|
2010-11-17 18:37:03 +00:00
|
|
|
if sent_data is None:
|
2010-12-08 04:04:37 +00:00
|
|
|
self.fail("No stanza was sent.")
|
2010-11-17 18:37:03 +00:00
|
|
|
if method == 'exact':
|
|
|
|
self.failUnless(self.compare(xml, sent_xml),
|
|
|
|
"Features do not match.\nDesired:\n%s\nReceived:\n%s" % (
|
|
|
|
tostring(xml), tostring(sent_xml)))
|
|
|
|
elif method == 'mask':
|
|
|
|
matcher = MatchXMLMask(xml)
|
2010-12-08 04:04:37 +00:00
|
|
|
self.failUnless(matcher.match(sent_xml),
|
2010-11-17 18:37:03 +00:00
|
|
|
"Stanza did not match using %s method:\n" % method + \
|
|
|
|
"Criteria:\n%s\n" % tostring(xml) + \
|
|
|
|
"Stanza:\n%s" % tostring(sent_xml))
|
|
|
|
else:
|
|
|
|
raise ValueError("Uknown matching method: %s" % method)
|
2010-10-07 23:43:51 +00:00
|
|
|
|
2010-11-17 18:37:03 +00:00
|
|
|
def send(self, data, defaults=None, use_values=True,
|
2011-03-24 00:23:49 +00:00
|
|
|
timeout=.5, method='exact'):
|
2010-10-07 13:22:27 +00:00
|
|
|
"""
|
|
|
|
Check that the XMPP client sent the given stanza XML.
|
|
|
|
|
|
|
|
Extracts the next sent stanza and compares it with the given
|
2010-11-05 18:45:58 +00:00
|
|
|
XML using check.
|
2010-10-07 13:22:27 +00:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
stanza_class -- The class of the sent stanza object.
|
|
|
|
data -- The XML string of the expected Message stanza,
|
|
|
|
or an equivalent stanza object.
|
|
|
|
use_values -- Modifies the type of tests used by check_message.
|
|
|
|
defaults -- A list of stanza interfaces that have defaults
|
|
|
|
values which may interfere with comparisons.
|
|
|
|
timeout -- Time in seconds to wait for a stanza before
|
|
|
|
failing the check.
|
2010-11-17 18:37:03 +00:00
|
|
|
method -- Select the type of comparison to use for
|
|
|
|
verifying the sent stanza. Options are 'exact',
|
|
|
|
'id', 'stanzapath', 'xpath', and 'mask'.
|
|
|
|
Defaults to the value of self.match_method.
|
2010-10-07 13:22:27 +00:00
|
|
|
"""
|
2010-11-17 18:37:03 +00:00
|
|
|
sent = self.xmpp.socket.next_sent(timeout)
|
2011-04-26 20:32:58 +00:00
|
|
|
if data is None and sent is None:
|
|
|
|
return
|
|
|
|
if data is None and sent is not None:
|
|
|
|
self.fail("Stanza data was sent: %s" % sent)
|
2010-11-17 20:45:16 +00:00
|
|
|
if sent is None:
|
2010-12-08 04:04:37 +00:00
|
|
|
self.fail("No stanza was sent.")
|
2011-05-20 21:42:40 +00:00
|
|
|
|
2010-11-17 20:45:16 +00:00
|
|
|
xml = self.parse_xml(sent)
|
|
|
|
self.fix_namespaces(xml, 'jabber:client')
|
|
|
|
sent = self.xmpp._build_stanza(xml, 'jabber:client')
|
|
|
|
self.check(sent, data,
|
2010-11-17 18:37:03 +00:00
|
|
|
method=method,
|
|
|
|
defaults=defaults,
|
|
|
|
use_values=use_values)
|
2010-10-07 13:22:27 +00:00
|
|
|
|
|
|
|
def stream_close(self):
|
2010-08-12 05:25:42 +00:00
|
|
|
"""
|
|
|
|
Disconnect the dummy XMPP client.
|
|
|
|
|
2010-10-07 13:22:27 +00:00
|
|
|
Can be safely called even if stream_start has not been called.
|
2010-08-12 05:25:42 +00:00
|
|
|
|
|
|
|
Must be placed in the tearDown method of a test class to ensure
|
|
|
|
that the XMPP client is disconnected after an error.
|
|
|
|
"""
|
2010-08-11 22:41:57 +00:00
|
|
|
if hasattr(self, 'xmpp') and self.xmpp is not None:
|
2010-10-07 13:22:27 +00:00
|
|
|
self.xmpp.socket.recv_data(self.xmpp.stream_footer)
|
2010-10-14 23:27:44 +00:00
|
|
|
self.xmpp.disconnect()
|
2010-07-14 19:24:37 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
# XML Comparison and Cleanup
|
|
|
|
|
|
|
|
def fix_namespaces(self, xml, ns):
|
|
|
|
"""
|
2010-08-03 22:31:22 +00:00
|
|
|
Assign a namespace to an element and any children that
|
2010-07-14 19:24:37 +00:00
|
|
|
don't have a namespace.
|
2010-08-12 05:25:42 +00:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
xml -- The XML object to fix.
|
|
|
|
ns -- The namespace to add to the XML object.
|
2010-07-14 19:24:37 +00:00
|
|
|
"""
|
|
|
|
if xml.tag.startswith('{'):
|
|
|
|
return
|
|
|
|
xml.tag = '{%s}%s' % (ns, xml.tag)
|
2012-06-19 16:47:31 +00:00
|
|
|
for child in xml:
|
2010-07-14 19:24:37 +00:00
|
|
|
self.fix_namespaces(child, ns)
|
|
|
|
|
2010-08-12 05:25:42 +00:00
|
|
|
def compare(self, xml, *other):
|
2010-06-27 21:33:43 +00:00
|
|
|
"""
|
|
|
|
Compare XML objects.
|
|
|
|
|
2010-08-12 05:25:42 +00:00
|
|
|
Arguments:
|
2010-10-06 22:10:04 +00:00
|
|
|
xml -- The XML object to compare against.
|
2010-08-12 05:25:42 +00:00
|
|
|
*other -- The list of XML objects to compare.
|
2010-06-27 21:33:43 +00:00
|
|
|
"""
|
2010-08-12 05:25:42 +00:00
|
|
|
if not other:
|
|
|
|
return False
|
2010-06-27 21:33:43 +00:00
|
|
|
|
|
|
|
# Compare multiple objects
|
2010-08-12 05:25:42 +00:00
|
|
|
if len(other) > 1:
|
|
|
|
for xml2 in other:
|
|
|
|
if not self.compare(xml, xml2):
|
2010-06-27 21:33:43 +00:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2010-08-12 05:25:42 +00:00
|
|
|
other = other[0]
|
|
|
|
|
2010-06-27 21:33:43 +00:00
|
|
|
# Step 1: Check tags
|
2010-08-12 05:25:42 +00:00
|
|
|
if xml.tag != other.tag:
|
2010-06-27 21:33:43 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
# Step 2: Check attributes
|
2010-08-12 05:25:42 +00:00
|
|
|
if xml.attrib != other.attrib:
|
2010-06-27 21:33:43 +00:00
|
|
|
return False
|
|
|
|
|
2010-08-27 19:48:48 +00:00
|
|
|
# Step 3: Check text
|
|
|
|
if xml.text is None:
|
|
|
|
xml.text = ""
|
|
|
|
if other.text is None:
|
|
|
|
other.text = ""
|
|
|
|
xml.text = xml.text.strip()
|
|
|
|
other.text = other.text.strip()
|
|
|
|
|
|
|
|
if xml.text != other.text:
|
|
|
|
return False
|
|
|
|
|
2010-10-07 23:43:51 +00:00
|
|
|
# Step 4: Check children count
|
2012-06-19 16:47:31 +00:00
|
|
|
if len(list(xml)) != len(list(other)):
|
2010-10-07 23:43:51 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
# Step 5: Recursively check children
|
2010-08-12 05:25:42 +00:00
|
|
|
for child in xml:
|
|
|
|
child2s = other.findall("%s" % child.tag)
|
2010-06-27 21:33:43 +00:00
|
|
|
if child2s is None:
|
|
|
|
return False
|
|
|
|
for child2 in child2s:
|
|
|
|
if self.compare(child, child2):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2010-10-07 23:43:51 +00:00
|
|
|
# Step 6: Recursively check children the other way.
|
|
|
|
for child in other:
|
|
|
|
child2s = xml.findall("%s" % child.tag)
|
|
|
|
if child2s is None:
|
|
|
|
return False
|
|
|
|
for child2 in child2s:
|
|
|
|
if self.compare(child, child2):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2010-06-27 21:33:43 +00:00
|
|
|
# Everything matches
|
|
|
|
return True
|