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
|
2010-07-14 19:24:37 +00:00
|
|
|
import socket
|
|
|
|
try:
|
2010-08-12 05:25:42 +00:00
|
|
|
import queue
|
2010-07-14 19:24:37 +00:00
|
|
|
except ImportError:
|
2010-08-12 05:25:42 +00:00
|
|
|
import Queue as queue
|
|
|
|
|
2010-07-14 19:24:37 +00:00
|
|
|
from sleekxmpp import ClientXMPP
|
2010-08-12 05:25:42 +00:00
|
|
|
from sleekxmpp.stanza import Message, Iq, Presence
|
|
|
|
from sleekxmpp.xmlstream.stanzabase import registerStanzaPlugin, ET
|
2010-08-06 16:04:52 +00:00
|
|
|
from sleekxmpp.xmlstream.tostring import tostring
|
2010-07-19 17:58:53 +00:00
|
|
|
|
2010-06-27 21:33:43 +00:00
|
|
|
|
2010-07-14 19:24:37 +00:00
|
|
|
class TestSocket(object):
|
2010-08-03 22:31:22 +00:00
|
|
|
|
2010-08-12 05:25:42 +00:00
|
|
|
"""
|
|
|
|
A dummy socket that reads and writes to queues instead
|
|
|
|
of an actual networking socket.
|
|
|
|
|
|
|
|
Methods:
|
|
|
|
nextSent -- Return the next sent stanza.
|
|
|
|
recvData -- Make a stanza available to read next.
|
|
|
|
recv -- Read the next stanza from the socket.
|
|
|
|
send -- Write a stanza to the socket.
|
|
|
|
makefile -- Dummy call, returns self.
|
|
|
|
read -- Read the next stanza from the socket.
|
|
|
|
"""
|
|
|
|
|
2010-07-14 19:24:37 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
2010-08-12 05:25:42 +00:00
|
|
|
"""
|
|
|
|
Create a new test socket.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
Same as arguments for socket.socket
|
|
|
|
"""
|
2010-07-14 19:24:37 +00:00
|
|
|
self.socket = socket.socket(*args, **kwargs)
|
|
|
|
self.recv_queue = queue.Queue()
|
|
|
|
self.send_queue = queue.Queue()
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
2010-08-12 05:25:42 +00:00
|
|
|
"""
|
|
|
|
Return attribute values of internal, dummy socket.
|
|
|
|
|
|
|
|
Some attributes and methods are disabled to prevent the
|
|
|
|
socket from connecting to the network.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
name -- Name of the attribute requested.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def dummy(*args):
|
|
|
|
"""Method to do nothing and prevent actual socket connections."""
|
|
|
|
return None
|
|
|
|
|
|
|
|
overrides = {'connect': dummy,
|
|
|
|
'close': dummy,
|
|
|
|
'shutdown': dummy}
|
|
|
|
|
2010-07-14 19:24:37 +00:00
|
|
|
return overrides.get(name, getattr(self.socket, name))
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
# Testing Interface
|
|
|
|
|
|
|
|
def nextSent(self, timeout=None):
|
2010-08-12 05:25:42 +00:00
|
|
|
"""
|
|
|
|
Get the next stanza that has been 'sent'.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
timeout -- Optional timeout for waiting for a new value.
|
|
|
|
"""
|
2010-07-14 19:24:37 +00:00
|
|
|
args = {'block': False}
|
|
|
|
if timeout is not None:
|
|
|
|
args = {'block': True, 'timeout': timeout}
|
|
|
|
try:
|
|
|
|
return self.send_queue.get(**args)
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def recvData(self, data):
|
2010-08-12 05:25:42 +00:00
|
|
|
"""
|
|
|
|
Add data to the receiving queue.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
data -- String data to 'write' to the socket to be received
|
|
|
|
by the XMPP client.
|
|
|
|
"""
|
2010-07-14 19:24:37 +00:00
|
|
|
self.recv_queue.put(data)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
# Socket Interface
|
|
|
|
|
|
|
|
def recv(self, *args, **kwargs):
|
2010-08-12 05:25:42 +00:00
|
|
|
"""
|
|
|
|
Read a value from the received queue.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
Placeholders. Same as for socket.Socket.recv.
|
|
|
|
"""
|
2010-07-14 19:24:37 +00:00
|
|
|
return self.read(block=True)
|
|
|
|
|
|
|
|
def send(self, data):
|
2010-08-12 05:25:42 +00:00
|
|
|
"""
|
|
|
|
Send data by placing it in the send queue.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
data -- String value to write.
|
|
|
|
"""
|
2010-07-14 19:24:37 +00:00
|
|
|
self.send_queue.put(data)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
# File Socket
|
|
|
|
|
2010-08-12 05:25:42 +00:00
|
|
|
def makefile(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
File socket version to use with ElementTree.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
Placeholders, same as socket.Socket.makefile()
|
|
|
|
"""
|
2010-07-14 19:24:37 +00:00
|
|
|
return self
|
|
|
|
|
2010-08-12 05:25:42 +00:00
|
|
|
def read(self, block=True, timeout=None, **kwargs):
|
|
|
|
"""
|
|
|
|
Implement the file socket interface.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
block -- Indicate if the read should block until a
|
|
|
|
value is ready.
|
|
|
|
timeout -- Time in seconds a block should last before
|
|
|
|
returning None.
|
|
|
|
"""
|
2010-07-14 19:24:37 +00:00
|
|
|
if timeout is not None:
|
|
|
|
block = True
|
|
|
|
try:
|
|
|
|
return self.recv_queue.get(block, timeout)
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
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:
|
|
|
|
Message -- Create a Message stanza object.
|
|
|
|
Iq -- Create an Iq stanza object.
|
|
|
|
Presence -- Create a Presence stanza object.
|
|
|
|
checkMessage -- Compare a Message stanza against an XML string.
|
|
|
|
checkIq -- Compare an Iq stanza against an XML string.
|
|
|
|
checkPresence -- Compare a Presence stanza against an XML string.
|
|
|
|
streamStart -- Initialize a dummy XMPP client.
|
|
|
|
streamRecv -- Queue data for XMPP client to receive.
|
|
|
|
streamSendMessage -- Check that the XMPP client sent the given
|
|
|
|
Message stanza.
|
|
|
|
streamSendIq -- Check that the XMPP client sent the given
|
|
|
|
Iq stanza.
|
|
|
|
streamSendPresence -- Check taht the XMPP client sent the given
|
|
|
|
Presence stanza.
|
|
|
|
streamClose -- Disconnect the XMPP client.
|
|
|
|
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-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-06-27 21:33:43 +00:00
|
|
|
return Message(None, *args, **kwargs)
|
|
|
|
|
|
|
|
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-06-27 21:33:43 +00:00
|
|
|
return Iq(None, *args, **kwargs)
|
|
|
|
|
|
|
|
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-06-27 21:33:43 +00:00
|
|
|
return Presence(None, *args, **kwargs)
|
|
|
|
|
2010-07-14 19:24:37 +00:00
|
|
|
# ------------------------------------------------------------------
|
|
|
|
# Methods for comparing stanza objects to XML strings
|
|
|
|
|
2010-06-27 21:33:43 +00:00
|
|
|
def checkMessage(self, msg, xml_string, use_values=True):
|
|
|
|
"""
|
2010-08-03 22:31:22 +00:00
|
|
|
Create and compare several message stanza objects to a
|
|
|
|
correct XML string.
|
2010-06-27 21:33:43 +00:00
|
|
|
|
2010-08-12 05:25:42 +00:00
|
|
|
If use_values is False, the test using getStanzaValues() and
|
|
|
|
setStanzaValues() will not be used.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
msg -- The Message stanza object to check.
|
|
|
|
xml_string -- The XML contents to compare against.
|
|
|
|
use_values -- Indicates if the test using getStanzaValues
|
|
|
|
and setStanzaValues should be used. Defaults
|
|
|
|
to True.
|
2010-06-27 21:33:43 +00:00
|
|
|
"""
|
|
|
|
|
2010-07-14 19:24:37 +00:00
|
|
|
self.fix_namespaces(msg.xml, 'jabber:client')
|
2010-08-06 16:04:52 +00:00
|
|
|
debug = "Given Stanza:\n%s\n" % tostring(msg.xml)
|
2010-06-27 21:33:43 +00:00
|
|
|
|
|
|
|
xml = ET.fromstring(xml_string)
|
2010-07-14 19:24:37 +00:00
|
|
|
self.fix_namespaces(xml, 'jabber:client')
|
|
|
|
|
2010-08-06 16:04:52 +00:00
|
|
|
debug += "XML String:\n%s\n" % tostring(xml)
|
2010-08-03 22:31:22 +00:00
|
|
|
|
2010-06-27 21:33:43 +00:00
|
|
|
msg2 = self.Message(xml)
|
2010-08-06 16:04:52 +00:00
|
|
|
debug += "Constructed Stanza:\n%s\n" % tostring(msg2.xml)
|
2010-08-03 22:31:22 +00:00
|
|
|
|
2010-06-27 21:33:43 +00:00
|
|
|
if use_values:
|
|
|
|
# Ugly, but need to make sure the type attribute is set.
|
|
|
|
msg['type'] = msg['type']
|
|
|
|
if xml.attrib.get('type', None) is None:
|
|
|
|
xml.attrib['type'] = 'normal'
|
2010-07-20 15:19:49 +00:00
|
|
|
msg2['type'] = msg2['type']
|
2010-08-06 16:04:52 +00:00
|
|
|
debug += "XML String:\n%s\n" % tostring(xml)
|
2010-06-27 21:33:43 +00:00
|
|
|
|
2010-07-19 23:25:01 +00:00
|
|
|
values = msg2.getStanzaValues()
|
2010-06-27 21:33:43 +00:00
|
|
|
msg3 = self.Message()
|
2010-07-19 23:25:01 +00:00
|
|
|
msg3.setStanzaValues(values)
|
2010-08-03 22:31:22 +00:00
|
|
|
|
2010-08-06 16:04:52 +00:00
|
|
|
debug += "Second Constructed Stanza:\n%s\n" % tostring(msg3.xml)
|
2010-06-27 21:33:43 +00:00
|
|
|
debug = "Three methods for creating stanza do not match:\n" + debug
|
2010-08-12 05:25:42 +00:00
|
|
|
self.failUnless(self.compare(xml, msg.xml, msg2.xml, msg3.xml),
|
2010-06-27 21:33:43 +00:00
|
|
|
debug)
|
|
|
|
else:
|
|
|
|
debug = "Two methods for creating stanza do not match:\n" + debug
|
2010-08-12 05:25:42 +00:00
|
|
|
self.failUnless(self.compare(xml, msg.xml, msg2.xml), debug)
|
2010-06-27 21:33:43 +00:00
|
|
|
|
|
|
|
def checkIq(self, iq, xml_string, use_values=True):
|
|
|
|
"""
|
2010-08-03 22:31:22 +00:00
|
|
|
Create and compare several iq stanza objects to a
|
|
|
|
correct XML string.
|
2010-06-27 21:33:43 +00:00
|
|
|
|
2010-08-12 05:25:42 +00:00
|
|
|
If use_values is False, the test using getStanzaValues() and
|
|
|
|
setStanzaValues() will not be used.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
iq -- The Iq stanza object to check.
|
|
|
|
xml_string -- The XML contents to compare against.
|
|
|
|
use_values -- Indicates if the test using getStanzaValues
|
|
|
|
and setStanzaValues should be used. Defaults
|
|
|
|
to True.
|
2010-06-27 21:33:43 +00:00
|
|
|
"""
|
2010-07-14 19:24:37 +00:00
|
|
|
|
|
|
|
self.fix_namespaces(iq.xml, 'jabber:client')
|
2010-08-06 16:04:52 +00:00
|
|
|
debug = "Given Stanza:\n%s\n" % tostring(iq.xml)
|
2010-06-27 21:33:43 +00:00
|
|
|
|
|
|
|
xml = ET.fromstring(xml_string)
|
2010-07-14 19:24:37 +00:00
|
|
|
self.fix_namespaces(xml, 'jabber:client')
|
2010-08-06 16:04:52 +00:00
|
|
|
debug += "XML String:\n%s\n" % tostring(xml)
|
2010-08-03 22:31:22 +00:00
|
|
|
|
2010-06-27 21:33:43 +00:00
|
|
|
iq2 = self.Iq(xml)
|
2010-08-06 16:04:52 +00:00
|
|
|
debug += "Constructed Stanza:\n%s\n" % tostring(iq2.xml)
|
2010-08-03 22:31:22 +00:00
|
|
|
|
2010-06-27 21:33:43 +00:00
|
|
|
if use_values:
|
2010-07-19 23:26:25 +00:00
|
|
|
values = iq.getStanzaValues()
|
2010-06-27 21:33:43 +00:00
|
|
|
iq3 = self.Iq()
|
2010-07-19 23:26:25 +00:00
|
|
|
iq3.setStanzaValues(values)
|
2010-06-27 21:33:43 +00:00
|
|
|
|
2010-08-06 16:04:52 +00:00
|
|
|
debug += "Second Constructed Stanza:\n%s\n" % tostring(iq3.xml)
|
2010-06-27 21:33:43 +00:00
|
|
|
debug = "Three methods for creating stanza do not match:\n" + debug
|
2010-08-12 05:25:42 +00:00
|
|
|
self.failUnless(self.compare(xml, iq.xml, iq2.xml, iq3.xml),
|
2010-06-27 21:33:43 +00:00
|
|
|
debug)
|
|
|
|
else:
|
|
|
|
debug = "Two methods for creating stanza do not match:\n" + debug
|
2010-08-12 05:25:42 +00:00
|
|
|
self.failUnless(self.compare(xml, iq.xml, iq2.xml), debug)
|
2010-06-27 21:33:43 +00:00
|
|
|
|
|
|
|
def checkPresence(self, pres, xml_string, use_values=True):
|
|
|
|
"""
|
2010-08-03 22:31:22 +00:00
|
|
|
Create and compare several presence stanza objects to a
|
|
|
|
correct XML string.
|
2010-06-27 21:33:43 +00:00
|
|
|
|
2010-08-12 05:25:42 +00:00
|
|
|
If use_values is False, the test using getStanzaValues() and
|
|
|
|
setStanzaValues() will not be used.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
iq -- The Iq stanza object to check.
|
|
|
|
xml_string -- The XML contents to compare against.
|
|
|
|
use_values -- Indicates if the test using getStanzaValues
|
|
|
|
and setStanzaValues should be used. Defaults
|
|
|
|
to True.
|
2010-06-27 21:33:43 +00:00
|
|
|
"""
|
2010-08-12 05:25:42 +00:00
|
|
|
|
2010-07-20 03:58:33 +00:00
|
|
|
self.fix_namespaces(pres.xml, 'jabber:client')
|
|
|
|
|
|
|
|
xml = ET.fromstring(xml_string)
|
|
|
|
self.fix_namespaces(xml, 'jabber:client')
|
2010-08-03 22:31:22 +00:00
|
|
|
|
2010-07-20 03:58:33 +00:00
|
|
|
pres2 = self.Presence(xml)
|
|
|
|
|
|
|
|
# Ugly, but 'priority' has a default value and need to make
|
|
|
|
# sure it is set
|
|
|
|
pres['priority'] = pres['priority']
|
|
|
|
pres2['priority'] = pres2['priority']
|
|
|
|
|
2010-08-06 16:04:52 +00:00
|
|
|
debug = "Given Stanza:\n%s\n" % tostring(pres.xml)
|
|
|
|
debug += "XML String:\n%s\n" % tostring(xml)
|
|
|
|
debug += "Constructed Stanza:\n%s\n" % tostring(pres2.xml)
|
2010-08-03 22:31:22 +00:00
|
|
|
|
2010-07-20 03:58:33 +00:00
|
|
|
if use_values:
|
|
|
|
values = pres.getStanzaValues()
|
|
|
|
pres3 = self.Presence()
|
|
|
|
pres3.setStanzaValues(values)
|
|
|
|
|
2010-08-06 16:04:52 +00:00
|
|
|
debug += "Second Constructed Stanza:\n%s\n" % tostring(pres3.xml)
|
2010-07-20 03:58:33 +00:00
|
|
|
debug = "Three methods for creating stanza do not match:\n" + debug
|
2010-08-12 05:25:42 +00:00
|
|
|
self.failUnless(self.compare(xml, pres.xml, pres2.xml, pres3.xml),
|
2010-07-20 03:58:33 +00:00
|
|
|
debug)
|
|
|
|
else:
|
|
|
|
debug = "Two methods for creating stanza do not match:\n" + debug
|
2010-08-12 05:25:42 +00:00
|
|
|
self.failUnless(self.compare(xml, pres.xml, pres2.xml), debug)
|
2010-06-27 21:33:43 +00:00
|
|
|
|
2010-07-14 19:24:37 +00:00
|
|
|
# ------------------------------------------------------------------
|
|
|
|
# Methods for simulating stanza streams.
|
|
|
|
|
|
|
|
def streamStart(self, mode='client', skip=True):
|
2010-08-12 05:25:42 +00:00
|
|
|
"""
|
|
|
|
Initialize an XMPP client or component using a dummy XML stream.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
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.
|
|
|
|
"""
|
2010-07-14 19:24:37 +00:00
|
|
|
if mode == 'client':
|
|
|
|
self.xmpp = ClientXMPP('tester@localhost', 'test')
|
|
|
|
self.xmpp.setSocket(TestSocket())
|
|
|
|
|
|
|
|
self.xmpp.state.set('reconnect', False)
|
|
|
|
self.xmpp.state.set('is client', True)
|
|
|
|
self.xmpp.state.set('connected', True)
|
|
|
|
|
|
|
|
# Must have the stream header ready for xmpp.process() to work
|
|
|
|
self.xmpp.socket.recvData(self.xmpp.stream_header)
|
|
|
|
|
|
|
|
self.xmpp.connectTCP = lambda a, b, c, d: True
|
|
|
|
self.xmpp.startTLS = lambda: True
|
|
|
|
self.xmpp.process(threaded=True)
|
2010-08-03 22:31:22 +00:00
|
|
|
if skip:
|
2010-07-14 19:24:37 +00:00
|
|
|
# Clear startup stanzas
|
2010-07-30 03:15:49 +00:00
|
|
|
self.xmpp.socket.nextSent(timeout=0.01)
|
2010-07-14 19:24:37 +00:00
|
|
|
|
|
|
|
def streamRecv(self, data):
|
2010-08-12 05:25:42 +00:00
|
|
|
"""
|
|
|
|
Pass data to the dummy XMPP client as if it came from an XMPP server.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
data -- String stanza XML to be received and processed by the
|
|
|
|
XMPP client or component.
|
|
|
|
"""
|
2010-07-14 19:24:37 +00:00
|
|
|
data = str(data)
|
|
|
|
self.xmpp.socket.recvData(data)
|
|
|
|
|
2010-07-30 03:15:49 +00:00
|
|
|
def streamSendMessage(self, data, use_values=True, timeout=.1):
|
2010-08-12 05:25:42 +00:00
|
|
|
"""
|
|
|
|
Check that the XMPP client sent the given stanza XML.
|
|
|
|
|
|
|
|
Extracts the next sent stanza and compares it with the given
|
|
|
|
XML using checkMessage.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
data -- The XML string of the expected Message stanza,
|
|
|
|
or an equivalent stanza object.
|
|
|
|
use_values -- Modifies the type of tests used by checkMessage.
|
|
|
|
timeout -- Time in seconds to wait for a stanza before
|
|
|
|
failing the check.
|
|
|
|
"""
|
2010-07-14 19:24:37 +00:00
|
|
|
if isinstance(data, str):
|
|
|
|
data = self.Message(xml=ET.fromstring(data))
|
2010-07-30 03:15:49 +00:00
|
|
|
sent = self.xmpp.socket.nextSent(timeout)
|
2010-07-14 19:24:37 +00:00
|
|
|
self.checkMessage(data, sent, use_values)
|
2010-08-03 22:31:22 +00:00
|
|
|
|
2010-07-30 03:15:49 +00:00
|
|
|
def streamSendIq(self, data, use_values=True, timeout=.1):
|
2010-08-12 05:25:42 +00:00
|
|
|
"""
|
|
|
|
Check that the XMPP client sent the given stanza XML.
|
|
|
|
|
|
|
|
Extracts the next sent stanza and compares it with the given
|
|
|
|
XML using checkIq.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
data -- The XML string of the expected Iq stanza,
|
|
|
|
or an equivalent stanza object.
|
|
|
|
use_values -- Modifies the type of tests used by checkIq.
|
|
|
|
timeout -- Time in seconds to wait for a stanza before
|
|
|
|
failing the check.
|
|
|
|
"""
|
2010-07-14 19:24:37 +00:00
|
|
|
if isinstance(data, str):
|
|
|
|
data = self.Iq(xml=ET.fromstring(data))
|
|
|
|
sent = self.xmpp.socket.nextSent(timeout)
|
|
|
|
self.checkIq(data, sent, use_values)
|
|
|
|
|
2010-07-30 03:15:49 +00:00
|
|
|
def streamSendPresence(self, data, use_values=True, timeout=.1):
|
2010-08-12 05:25:42 +00:00
|
|
|
"""
|
|
|
|
Check that the XMPP client sent the given stanza XML.
|
|
|
|
|
|
|
|
Extracts the next sent stanza and compares it with the given
|
|
|
|
XML using checkPresence.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
data -- The XML string of the expected Presence stanza,
|
|
|
|
or an equivalent stanza object.
|
|
|
|
use_values -- Modifies the type of tests used by checkPresence.
|
|
|
|
timeout -- Time in seconds to wait for a stanza before
|
|
|
|
failing the check.
|
|
|
|
"""
|
2010-07-14 19:24:37 +00:00
|
|
|
if isinstance(data, str):
|
|
|
|
data = self.Presence(xml=ET.fromstring(data))
|
|
|
|
sent = self.xmpp.socket.nextSent(timeout)
|
|
|
|
self.checkPresence(data, sent, use_values)
|
|
|
|
|
|
|
|
def streamClose(self):
|
2010-08-12 05:25:42 +00:00
|
|
|
"""
|
|
|
|
Disconnect the dummy XMPP client.
|
|
|
|
|
|
|
|
Can be safely called even if streamStart has not been called.
|
|
|
|
|
|
|
|
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-07-14 19:24:37 +00:00
|
|
|
self.xmpp.disconnect()
|
|
|
|
self.xmpp.socket.recvData(self.xmpp.stream_footer)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
# 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)
|
|
|
|
for child in xml.getchildren():
|
|
|
|
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:
|
|
|
|
xml -- The XML object to compare against.
|
|
|
|
*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
|
|
|
|
|
|
|
|
# Step 3: 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
|
|
|
|
|
|
|
|
# Everything matches
|
|
|
|
return True
|