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:
|
|
|
|
import queue
|
|
|
|
except ImportError:
|
|
|
|
import Queue as queue
|
2010-06-27 21:33:43 +00:00
|
|
|
from xml.etree import cElementTree as ET
|
2010-07-14 19:24:37 +00:00
|
|
|
from sleekxmpp import ClientXMPP
|
2010-06-27 21:33:43 +00:00
|
|
|
from sleekxmpp import Message, Iq
|
|
|
|
from sleekxmpp.stanza.presence import Presence
|
|
|
|
from sleekxmpp.xmlstream.matcher.stanzapath import StanzaPath
|
2010-07-19 17:58:53 +00:00
|
|
|
from sleekxmpp.xmlstream.stanzabase import registerStanzaPlugin
|
|
|
|
|
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-07-14 19:24:37 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.socket = socket.socket(*args, **kwargs)
|
|
|
|
self.recv_queue = queue.Queue()
|
|
|
|
self.send_queue = queue.Queue()
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
"""Pass requests through to actual socket"""
|
|
|
|
# Override a few methods to prevent actual socket connections
|
|
|
|
overrides = {'connect': lambda *args: None,
|
|
|
|
'close': lambda *args: None,
|
|
|
|
'shutdown': lambda *args: None}
|
|
|
|
return overrides.get(name, getattr(self.socket, name))
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
# Testing Interface
|
|
|
|
|
|
|
|
def nextSent(self, timeout=None):
|
|
|
|
"""Get the next stanza that has been 'sent'"""
|
|
|
|
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):
|
|
|
|
"""Add data to the receiving queue"""
|
|
|
|
self.recv_queue.put(data)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
# Socket Interface
|
|
|
|
|
|
|
|
def recv(self, *args, **kwargs):
|
|
|
|
return self.read(block=True)
|
|
|
|
|
|
|
|
def send(self, data):
|
|
|
|
self.send_queue.put(data)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
# File Socket
|
|
|
|
|
|
|
|
def makefile(self, mode='r', bufsize=-1):
|
|
|
|
"""File socket version to use with ElementTree"""
|
|
|
|
return self
|
|
|
|
|
|
|
|
def read(self, size=4096, block=True, timeout=None):
|
|
|
|
"""Implement the file socket interface"""
|
|
|
|
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):
|
|
|
|
"""
|
|
|
|
A SleekXMPP specific TestCase class that provides
|
|
|
|
methods for comparing message, iq, and presence stanzas.
|
|
|
|
"""
|
|
|
|
|
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):
|
|
|
|
"""Create a message stanza."""
|
|
|
|
return Message(None, *args, **kwargs)
|
|
|
|
|
|
|
|
def Iq(self, *args, **kwargs):
|
|
|
|
"""Create an iq stanza."""
|
|
|
|
return Iq(None, *args, **kwargs)
|
|
|
|
|
|
|
|
def Presence(self, *args, **kwargs):
|
|
|
|
"""Create a presence stanza."""
|
|
|
|
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-03 22:31:22 +00:00
|
|
|
If use_values is False, the test using getValues() and
|
2010-06-27 21:33:43 +00:00
|
|
|
setValues() will not be used.
|
|
|
|
"""
|
|
|
|
|
2010-07-14 19:24:37 +00:00
|
|
|
self.fix_namespaces(msg.xml, 'jabber:client')
|
2010-06-27 21:33:43 +00:00
|
|
|
debug = "Given Stanza:\n%s\n" % ET.tostring(msg.xml)
|
|
|
|
|
|
|
|
xml = ET.fromstring(xml_string)
|
2010-07-14 19:24:37 +00:00
|
|
|
self.fix_namespaces(xml, 'jabber:client')
|
|
|
|
|
2010-06-27 21:33:43 +00:00
|
|
|
debug += "XML String:\n%s\n" % ET.tostring(xml)
|
2010-08-03 22:31:22 +00:00
|
|
|
|
2010-06-27 21:33:43 +00:00
|
|
|
msg2 = self.Message(xml)
|
|
|
|
debug += "Constructed Stanza:\n%s\n" % ET.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']
|
|
|
|
debug += "XML String:\n%s\n" % ET.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-06-27 21:33:43 +00:00
|
|
|
debug += "Second Constructed Stanza:\n%s\n" % ET.tostring(msg3.xml)
|
|
|
|
debug = "Three methods for creating stanza do not match:\n" + debug
|
2010-08-03 22:31:22 +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
|
|
|
|
self.failUnless(self.compare([xml, msg.xml, msg2.xml]), debug)
|
|
|
|
|
|
|
|
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-03 22:31:22 +00:00
|
|
|
If use_values is False, the test using getValues() and
|
2010-06-27 21:33:43 +00:00
|
|
|
setValues() will not be used.
|
|
|
|
"""
|
2010-07-14 19:24:37 +00:00
|
|
|
|
|
|
|
self.fix_namespaces(iq.xml, 'jabber:client')
|
2010-06-27 21:33:43 +00:00
|
|
|
debug = "Given Stanza:\n%s\n" % ET.tostring(iq.xml)
|
|
|
|
|
|
|
|
xml = ET.fromstring(xml_string)
|
2010-07-14 19:24:37 +00:00
|
|
|
self.fix_namespaces(xml, 'jabber:client')
|
2010-06-27 21:33:43 +00:00
|
|
|
debug += "XML String:\n%s\n" % ET.tostring(xml)
|
2010-08-03 22:31:22 +00:00
|
|
|
|
2010-06-27 21:33:43 +00:00
|
|
|
iq2 = self.Iq(xml)
|
|
|
|
debug += "Constructed Stanza:\n%s\n" % ET.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
|
|
|
|
|
|
|
debug += "Second Constructed Stanza:\n%s\n" % ET.tostring(iq3.xml)
|
|
|
|
debug = "Three methods for creating stanza do not match:\n" + debug
|
2010-08-03 22:31:22 +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
|
|
|
|
self.failUnless(self.compare([xml, iq.xml, iq2.xml]), debug)
|
|
|
|
|
|
|
|
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-03 22:31:22 +00:00
|
|
|
If use_values is False, the test using getValues() and
|
2010-06-27 21:33:43 +00:00
|
|
|
setValues() will not be used.
|
|
|
|
"""
|
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-03 22:31:22 +00:00
|
|
|
debug = "Given Stanza:\n%s\n" % ET.tostring(pres.xml)
|
|
|
|
debug += "XML String:\n%s\n" % ET.tostring(xml)
|
|
|
|
debug += "Constructed Stanza:\n%s\n" % ET.tostring(pres2.xml)
|
|
|
|
|
2010-07-20 03:58:33 +00:00
|
|
|
if use_values:
|
|
|
|
values = pres.getStanzaValues()
|
|
|
|
pres3 = self.Presence()
|
|
|
|
pres3.setStanzaValues(values)
|
|
|
|
|
|
|
|
debug += "Second Constructed Stanza:\n%s\n" % ET.tostring(pres3.xml)
|
|
|
|
debug = "Three methods for creating stanza do not match:\n" + debug
|
2010-08-03 22:31:22 +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
|
|
|
|
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):
|
|
|
|
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):
|
|
|
|
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-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-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-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):
|
|
|
|
if self.xmpp is not None:
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
if xml.tag.startswith('{'):
|
|
|
|
return
|
|
|
|
xml.tag = '{%s}%s' % (ns, xml.tag)
|
|
|
|
for child in xml.getchildren():
|
|
|
|
self.fix_namespaces(child, ns)
|
|
|
|
|
2010-06-27 21:33:43 +00:00
|
|
|
def compare(self, xml1, xml2=None):
|
|
|
|
"""
|
|
|
|
Compare XML objects.
|
|
|
|
|
|
|
|
If given a list of XML objects, then
|
|
|
|
all of the elements in the list will be
|
|
|
|
compared.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Compare multiple objects
|
|
|
|
if type(xml1) is list:
|
|
|
|
xmls = xml1
|
|
|
|
xml1 = xmls[0]
|
|
|
|
for xml in xmls[1:]:
|
|
|
|
xml2 = xml
|
|
|
|
if not self.compare(xml1, xml2):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
# Step 1: Check tags
|
|
|
|
if xml1.tag != xml2.tag:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Step 2: Check attributes
|
|
|
|
if xml1.attrib != xml2.attrib:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Step 3: Recursively check children
|
|
|
|
for child in xml1:
|
|
|
|
child2s = xml2.findall("%s" % child.tag)
|
|
|
|
if child2s is None:
|
|
|
|
return False
|
|
|
|
for child2 in child2s:
|
|
|
|
if self.compare(child, child2):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Everything matches
|
|
|
|
return True
|