def testConvertStruct(self): params = [{"foo": "bar", "baz": False}] params_xml = py2xml(*params) expected_xml = self.parse_xml(""" <params xmlns="jabber:iq:rpc"> <param> <value> <struct> <member> <name>foo</name> <value><string>bar</string></value> </member> <member> <name>baz</name> <value><boolean>0</boolean></value> </member> </struct> </value> </param> </params> """) self.assertTrue(self.compare(expected_xml, params_xml), "Struct to XML conversion\nExpected: %s\nGot: %s" % ( tostring(expected_xml), tostring(params_xml))) self.assertEqual(params, xml2py(expected_xml), "XML to struct conversion")
def testConvertBase64(self): params = [rpcbase64(base64.b64encode(b"Hello, world!"))] params_xml = py2xml(*params) expected_xml = self.parse_xml(""" <params xmlns="jabber:iq:rpc"> <param> <value> <base64>SGVsbG8sIHdvcmxkIQ==</base64> </value> </param> </params> """) alternate_xml = self.parse_xml(""" <params xmlns="jabber:iq:rpc"> <param> <value> <Base64>SGVsbG8sIHdvcmxkIQ==</Base64> </value> </param> </params> """) self.assertTrue(self.compare(expected_xml, params_xml), "Base64 to XML conversion\nExpected: %s\nGot: %s" % ( tostring(expected_xml), tostring(params_xml))) self.assertEqual(list(map(lambda x: x.decode(), params)), list(map(lambda x: x.decode(), xml2py(expected_xml))), "XML to base64 conversion") self.assertEqual(list(map(lambda x: x.decode(), params)), list(map(lambda x: x.decode(), xml2py(alternate_xml))), "Alternate XML to base64 conversion")
def testConvertArray(self): params = [[1,2,3], ('a', 'b', 'c')] params_xml = py2xml(*params) expected_xml = self.parse_xml(""" <params xmlns="jabber:iq:rpc"> <param> <value> <array> <data> <value><i4>1</i4></value> <value><i4>2</i4></value> <value><i4>3</i4></value> </data> </array> </value> </param> <param> <value> <array> <data> <value><string>a</string></value> <value><string>b</string></value> <value><string>c</string></value> </data> </array> </value> </param> </params> """) self.assertTrue(self.compare(expected_xml, params_xml), "Array to XML conversion\nExpected: %s\nGot: %s" % ( tostring(expected_xml), tostring(params_xml))) self.assertEqual(list(map(list, params)), xml2py(expected_xml), "XML to array conversion")
def testConvertDouble(self): params = [3.14159265] params_xml = py2xml(*params) expected_xml = self.parse_xml(""" <params xmlns="jabber:iq:rpc"> <param> <value> <double>3.14159265</double> </value> </param> </params> """) self.assertTrue(self.compare(expected_xml, params_xml), "Double to XML conversion\nExpected: %s\nGot: %s" % ( tostring(expected_xml), tostring(params_xml))) self.assertEqual(params, xml2py(expected_xml), "XML to double conversion")
def testConvertUnicodeString(self): params = ["おはよう"] params_xml = py2xml(*params) expected_xml = self.parse_xml(""" <params xmlns="jabber:iq:rpc"> <param> <value> <string>おはよう</string> </value> </param> </params> """) self.assertTrue(self.compare(expected_xml, params_xml), "String to XML conversion\nExpected: %s\nGot: %s" % ( tostring(expected_xml), tostring(params_xml))) self.assertEqual(params, xml2py(expected_xml), "XML to string conversion")
def testConvertNil(self): params = [None] params_xml = py2xml(*params) expected_xml = self.parse_xml(""" <params xmlns="jabber:iq:rpc"> <param> <value> <nil /> </value> </param> </params> """) self.assertTrue(self.compare(expected_xml, params_xml), "Nil to XML conversion\nExpected: %s\nGot: %s" % ( tostring(expected_xml), tostring(params_xml))) self.assertEqual(params, xml2py(expected_xml), "XML to nil conversion")
def testConvertString(self): params = ["'This' & \"That\""] params_xml = py2xml(*params) expected_xml = self.parse_xml(""" <params xmlns="jabber:iq:rpc"> <param> <value> <string>'This' & "That"</string> </value> </param> </params> """) self.assertTrue(self.compare(expected_xml, params_xml), "String to XML conversion\nExpected: %s\nGot: %s" % ( tostring(expected_xml), tostring(params_xml))) self.assertEqual(params, xml2py(expected_xml), "XML to string conversion")
def testConvertDateTime(self): params = [rpctime("20111220T01:50:00")] params_xml = py2xml(*params) expected_xml = self.parse_xml(""" <params xmlns="jabber:iq:rpc"> <param> <value> <dateTime.iso8601>20111220T01:50:00</dateTime.iso8601> </value> </param> </params> """) self.assertTrue(self.compare(expected_xml, params_xml), "DateTime to XML conversion\nExpected: %s\nGot: %s" % ( tostring(expected_xml), tostring(params_xml))) self.assertEqual(list(map(lambda x: x.iso8601(), params)), list(map(lambda x: x.iso8601(), xml2py(expected_xml))), None)
def send_feature(self, data, method='mask', use_values=True, timeout=1): """ """ sent_data = self.xmpp.socket.next_sent(timeout) xml = self.parse_xml(data) sent_xml = self.parse_xml(sent_data) if sent_data is None: self.fail("No stanza was sent.") if method == 'exact': self.assertTrue(self.compare(xml, sent_xml), "Features do not match.\nDesired:\n%s\nReceived:\n%s" % ( highlight(tostring(xml)), highlight(tostring(sent_xml)))) elif method == 'mask': matcher = MatchXMLMask(xml) self.assertTrue(matcher.match(sent_xml), "Stanza did not match using %s method:\n" % method + \ "Criteria:\n%s\n" % highlight(tostring(xml)) + \ "Stanza:\n%s" % highlight(tostring(sent_xml))) else: raise ValueError("Uknown matching method: %s" % method)
def tryTostring(self, original='', expected=None, message='', **kwargs): """ Compare the result of calling tostring against an expected result. """ if not expected: expected=original if isinstance(original, str): xml = ET.fromstring(original) else: xml=original result = tostring(xml, **kwargs) self.assertTrue(result == expected, "%s: %s" % (message, result))
def tryTostring(self, original='', expected=None, message='', **kwargs): """ Compare the result of calling tostring against an expected result. """ if not expected: expected = original if isinstance(original, str): xml = ET.fromstring(original) else: xml = original result = tostring(xml, **kwargs) self.failUnless(result == expected, "%s: %s" % (message, result))
def testConvertBoolean(self): params = [True, False] params_xml = py2xml(*params) expected_xml = self.parse_xml(""" <params xmlns="jabber:iq:rpc"> <param> <value> <boolean>1</boolean> </value> </param> <param> <value> <boolean>0</boolean> </value> </param> </params> """) self.assertTrue(self.compare(expected_xml, params_xml), "Boolean to XML conversion\nExpected: %s\nGot: %s" % ( tostring(expected_xml), tostring(params_xml))) self.assertEqual(params, xml2py(expected_xml), "XML to boolean conversion")
def testConvertInteger(self): params = [32767, -32768] params_xml = py2xml(*params) expected_xml = self.parse_xml(""" <params xmlns="jabber:iq:rpc"> <param> <value> <i4>32767</i4> </value> </param> <param> <value> <i4>-32768</i4> </value> </param> </params> """) alternate_xml = self.parse_xml(""" <params xmlns="jabber:iq:rpc"> <param> <value> <int>32767</int> </value> </param> <param> <value> <int>-32768</int> </value> </param> </params> """) self.assertTrue(self.compare(expected_xml, params_xml), "Integer to XML conversion\nExpected: %s\nGot: %s" % ( tostring(expected_xml), tostring(params_xml))) self.assertEqual(params, xml2py(expected_xml), "XML to boolean conversion") self.assertEqual(params, xml2py(alternate_xml), "Alternate XML to boolean conversion")
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): """ 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. """ header = self.make_header(sto, sfrom, sid, stream_ns=stream_ns, default_ns=default_ns, version=version, xml_header=xml_header) 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] if list(recv_xml): # We received more than just the header for xml in recv_xml: self.xmpp.data_received(tostring(xml)) attrib = recv_xml.attrib recv_xml.clear() recv_xml.attrib = attrib self.assertTrue( 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)))
def check(self, stanza, criteria, method='exact', defaults=None, use_values=True): """ Create and compare several stanza objects to a correct XML string. If use_values is False, tests using stanza.values will not be used. 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. criteria -- An expression the stanza must match against. method -- The type of matching to use; one of: 'exact', 'mask', 'id', 'xpath', and 'stanzapath'. Defaults to the value of self.match_method. 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. use_values -- Indicates if testing using stanza.values should be used. Defaults to True. """ if method is None and hasattr(self, 'match_method'): method = getattr(self, 'match_method') if method != 'exact': matchers = { 'stanzapath': StanzaPath, 'xpath': MatchXPath, 'mask': MatchXMLMask, 'idsender': MatchIDSender, 'id': MatcherId } Matcher = matchers.get(method, None) if Matcher is None: raise ValueError("Unknown matching method.") test = Matcher(criteria) self.assertTrue(test.match(stanza), "Stanza did not match using %s method:\n" % method + \ "Criteria:\n%s\n" % str(criteria) + \ "Stanza:\n%s" % str(stanza)) else: stanza_class = stanza.__class__ if not isinstance(criteria, ElementBase): xml = self.parse_xml(criteria) else: xml = criteria.xml # 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: # 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. 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 values = stanza2.values stanza3 = stanza_class() stanza3.values = values debug = "Three methods for creating stanzas do not match.\n" debug += "Given XML:\n%s\n" % highlight(tostring(xml)) debug += "Given stanza:\n%s\n" % highlight(tostring( stanza.xml)) debug += "Generated stanza:\n%s\n" % highlight( tostring(stanza2.xml)) debug += "Second generated stanza:\n%s\n" % highlight( 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" % highlight(tostring(xml)) debug += "Given stanza:\n%s\n" % highlight(tostring( stanza.xml)) debug += "Generated stanza:\n%s\n" % highlight( tostring(stanza2.xml)) result = self.compare(xml, stanza.xml, stanza2.xml) self.assertTrue(result, debug)
from_ = from_.bare to_ = to_.bare if self.dest == 'from': return from_ == self.jid elif self.dest == 'to': return to_ == self.jid return self.jid in (from_, to_) def __repr__(self): return '%s%s%s' % (self.dest, ': ' if self.dest else '', self.jid) MATCHERS_MAPPINGS = { MatchJID: ('JID', lambda obj: repr(obj)), matcher.MatcherId: ('ID', lambda obj: obj._criteria), matcher.MatchXMLMask: ('XMLMask', lambda obj: tostring(obj._criteria)), matcher.MatchXPath: ('XPath', lambda obj: obj._criteria) } class XMLTab(Tab): def __init__(self): Tab.__init__(self) self.state = 'normal' self.name = 'XMLTab' self.filters = [] self.core_buffer = self.core.xml_buffer self.filtered_buffer = text_buffer.TextBuffer() self.info_header = windows.XMLInfoWin() self.text_win = windows.XMLTextWin()
to_ = to_.bare if self.dest == 'from': return from_ == self.jid elif self.dest == 'to': return to_ == self.jid return self.jid in (from_, to_) def __repr__(self): return '%s%s%s' % (self.dest, ': ' if self.dest else '', self.jid) MATCHERS_MAPPINGS = { MatchJID: ('JID', repr), matcher.MatcherId: ('ID', lambda obj: obj._criteria), matcher.MatchXMLMask: ('XMLMask', lambda obj: tostring(obj._criteria)), matcher.MatchXPath: ('XPath', lambda obj: obj._criteria) } class XMLTab(Tab): def __init__(self, core): Tab.__init__(self, core) self.state = 'normal' self.name = 'XMLTab' self.filters = [] self.core_buffer = self.core.xml_buffer self.filtered_buffer = text_buffer.TextBuffer() self.info_header = windows.XMLInfoWin()
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): """ 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. """ header = self.make_header(sto, sfrom, sid, stream_ns=stream_ns, default_ns=default_ns, version=version, xml_header=xml_header) 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] if list(recv_xml): # We received more than just the header for xml in recv_xml: self.xmpp.data_received(tostring(xml)) attrib = recv_xml.attrib recv_xml.clear() recv_xml.attrib = attrib self.assertTrue( 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)))
def check(self, stanza, criteria, method='exact', defaults=None, use_values=True): """ Create and compare several stanza objects to a correct XML string. If use_values is False, tests using stanza.values will not be used. 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. criteria -- An expression the stanza must match against. method -- The type of matching to use; one of: 'exact', 'mask', 'id', 'xpath', and 'stanzapath'. Defaults to the value of self.match_method. 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. use_values -- Indicates if testing using stanza.values should be used. Defaults to True. """ if method is None and hasattr(self, 'match_method'): method = getattr(self, 'match_method') if method != 'exact': matchers = {'stanzapath': StanzaPath, 'xpath': MatchXPath, 'mask': MatchXMLMask, 'idsender': MatchIDSender, 'id': MatcherId} Matcher = matchers.get(method, None) if Matcher is None: raise ValueError("Unknown matching method.") test = Matcher(criteria) self.assertTrue(test.match(stanza), "Stanza did not match using %s method:\n" % method + \ "Criteria:\n%s\n" % str(criteria) + \ "Stanza:\n%s" % str(stanza)) else: stanza_class = stanza.__class__ if not isinstance(criteria, ElementBase): xml = self.parse_xml(criteria) else: xml = criteria.xml # 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: # 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. 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 values = stanza2.values stanza3 = stanza_class() stanza3.values = values debug = "Three methods for creating stanzas do not match.\n" debug += "Given XML:\n%s\n" % highlight(tostring(xml)) debug += "Given stanza:\n%s\n" % highlight(tostring(stanza.xml)) debug += "Generated stanza:\n%s\n" % highlight(tostring(stanza2.xml)) debug += "Second generated stanza:\n%s\n" % highlight(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" % highlight(tostring(xml)) debug += "Given stanza:\n%s\n" % highlight(tostring(stanza.xml)) debug += "Generated stanza:\n%s\n" % highlight(tostring(stanza2.xml)) result = self.compare(xml, stanza.xml, stanza2.xml) self.assertTrue(result, debug)