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 recv_feature(self, data, method='mask', use_values=True, timeout=1): """ """ if method is None and hasattr(self, 'match_method'): method = getattr(self, 'match_method') 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) if recv_data is None: self.fail("No stanza was received.") 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) self.failUnless(matcher.match(recv_xml), "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) else: # place the data in the dummy socket receiving queue. data = str(data) self.xmpp.socket.recv_data(data)
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 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 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 recv_feature(self, data, method='mask', use_values=True, timeout=1): """ """ if method is None and hasattr(self, 'match_method'): method = getattr(self, 'match_method') 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) if recv_data is None: self.fail("No stanza was received.") 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) self.failUnless(matcher.match(recv_xml), "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) else: # place the data in the dummy socket receiving queue. data = str(data) self.xmpp.socket.recv_data(data)
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 send_feature(self, data, use_values=True, timeout=1): """ """ sent_data = self.xmpp.socket.next_sent(timeout) if sent_data is None: return False xml = self.parse_xml(data) sent_xml = self.parse_xml(sent_data) self.failUnless(self.compare(xml, sent_xml), "Features do not match.\nDesired:\n%s\nSent:\n%s" % ( tostring(xml), tostring(sent_xml)))
def send_feature(self, data, use_values=True, timeout=1): """ """ sent_data = self.xmpp.socket.next_sent(timeout) if sent_data is None: return False xml = self.parse_xml(data) sent_xml = self.parse_xml(sent_data) self.failUnless( self.compare(xml, sent_xml), "Features do not match.\nDesired:\n%s\nSent:\n%s" % (tostring(xml), tostring(sent_xml)))
def __init__(self, xmpp_msg): self.title = re.sub('([\n\r]+)', '. ', xmpp_msg['body']) self.pubdate = datetime.utcnow() self.project_id = None self.story_id = None self.categories = [] self.text = None self.tags = None self.status = None self.color = None self.creator = None self.creator_mail = None self.content = None self.content_plain = None source = tostring(xmpp_msg['html']['body']) self._load_project_and_story(source) self.link = 'https://agilezen.com' \ + '/project/' + str(self.project_id) \ + '/story/' + str(self.story_id) self.guid = self.link + '#' + xmpp_msg['id'] match = re.search(CAUSER_RE, source, re.UNICODE) if match: self.causer = match.group(1) self._load_additional_data()
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 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 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 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 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 recv_feature(self, data, use_values=True, timeout=1): """ """ 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: return False xml = self.parse_xml(data) recv_xml = self.parse_xml(recv_data) self.failUnless(self.compare(xml, recv_xml), "Features do not match.\nDesired:\n%s\nReceived:\n%s" % ( tostring(xml), tostring(recv_xml))) else: # place the data in the dummy socket receiving queue. data = str(data) self.xmpp.socket.recv_data(data)
def __str__(self, top_level_ns=True): """ Return a string serialization of the underlying XML object. Arguments: top_level_ns -- Display the top-most namespace. Defaults to True. """ stanza_ns = '' if top_level_ns else self.namespace return tostring(self.xml, xmlns='', stanza_ns=stanza_ns)
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.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) self.failUnless(matcher.match(sent_xml), "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)
def __str__(self, top_level_ns=False): """Serialize the stanza's XML to a string. :param bool top_level_ns: Display the top-most namespace. Defaults to ``False``. """ stanza_ns = '' if top_level_ns else self.namespace return tostring(self.xml, xmlns='', stanza_ns=stanza_ns, stream=self.stream, top_level=not top_level_ns)
def recv_feature(self, data, use_values=True, timeout=1): """ """ 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: return False xml = self.parse_xml(data) recv_xml = self.parse_xml(recv_data) self.failUnless( self.compare(xml, recv_xml), "Features do not match.\nDesired:\n%s\nReceived:\n%s" % (tostring(xml), tostring(recv_xml))) else: # place the data in the dummy socket receiving queue. data = str(data) self.xmpp.socket.recv_data(data)
def __str__(self, top_level_ns=False): """ Serialize the stanza's XML to a string. Arguments: top_level_ns -- Display the top-most namespace. Defaults to False. """ stanza_ns = '' if top_level_ns else self.namespace return tostring(self.xml, xmlns='', stanza_ns=stanza_ns, stream=self.stream)
def __str__(self, top_level_ns=True): """Return a string serialization of the underlying XML object. .. seealso:: :ref:`tostring` :param bool top_level_ns: Display the top-most namespace. Defaults to True. """ stanza_ns = '' if top_level_ns else self.namespace return tostring(self.xml, xmlns='', stanza_ns=stanza_ns, top_level=not top_level_ns)
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 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 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 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 rsync(self, project, files=None): """ Starts a rsync transaction, rsync and stop the transaction. Raises a BaboonException if there's a problem. """ # Verify if the connection is established. Otherwise, wait... if not self.connected.is_set(): self.connected.wait() # Set the rsync flags. self.rsync_running.set() self.rsync_finished.clear() #TODO: make this an int while checking config file max_stanza_size = int(config['server']['max_stanza_size']) # Build first stanza iq = self._build_iq(project, files) try: # Get the size of the stanza to_xml = tostring(iq.xml) size = sys.getsizeof(to_xml) # If it's bigger than the max_stanza_size, split it ! if size >= max_stanza_size: iqs = self._split_iq(size, project, files) self.logger.warning( 'The xml stanza has been split %s stanzas.' % len(iqs)) else: # Else the original iq will be the only element to send iqs = [iq] # Send elements in list for iq in iqs: iq.send() self.logger.debug('Sent (%d/%d)!' % (iqs.index(iq) + 1, len(iqs))) except IqError as e: self.rsync_error(e.iq['error']['text']) except Exception as e: self.rsync_error(e)
def rsync(self, project, files=None): """ Starts a rsync transaction, rsync and stop the transaction. Raises a BaboonException if there's a problem. """ # Verify if the connection is established. Otherwise, wait... if not self.connected.is_set(): self.connected.wait() # Set the rsync flags. self.rsync_running.set() self.rsync_finished.clear() #TODO: make this an int while checking config file max_stanza_size = int(config['server']['max_stanza_size']) # Build first stanza iq = self._build_iq(project, files) try: # Get the size of the stanza to_xml = tostring(iq.xml) size = sys.getsizeof(to_xml) # If it's bigger than the max_stanza_size, split it ! if size >= max_stanza_size: iqs = self._split_iq(size, project, files) self.logger.warning('The xml stanza has been split %s stanzas.' % len(iqs)) else: # Else the original iq will be the only element to send iqs = [iq] # Send elements in list for iq in iqs: iq.send() self.logger.debug('Sent (%d/%d)!' % (iqs.index(iq) + 1, len(iqs))) except IqError as e: self.rsync_error(e.iq['error']['text']) except Exception as e: self.rsync_error(e)
def check(self, stanza, xml_string, defaults=None, use_values=True): """ Create and compare several stanza objects to a correct XML string. If use_values is False, test using getStanzaValues() and setStanzaValues() 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. xml_string -- A string version of the correct XML expected. 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 getStanzaValues() and setStanzaValues() should be used. Defaults to True. """ stanza_class = stanza.__class__ xml = self.parse_xml(xml_string) # 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 getStanzaValues() and setStanzaValues() 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.getStanzaValues() stanza3 = stanza_class() stanza3.setStanzaValues(values) 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)
def __str__(self): """ Return a string serialization of the underlying XML object. """ return tostring(self.xml, xmlns='', stanza_ns=self.namespace)
def __str__(self): """Serialize the stanza's XML to a string.""" return tostring(self.xml, xmlns='', stanza_ns=self.namespace, stream=self.stream)
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, '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)) 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" % 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)
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 recv_xml.getchildren: # We received more than just the header for xml in recv_xml.getchildren(): 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)))
def check(self, stanza, xml_string, defaults=None, use_values=True): """ Create and compare several stanza objects to a correct XML string. If use_values is False, test using getStanzaValues() and setStanzaValues() 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. xml_string -- A string version of the correct XML expected. 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 getStanzaValues() and setStanzaValues() should be used. Defaults to True. """ stanza_class = stanza.__class__ xml = self.parse_xml(xml_string) # 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 getStanzaValues() and setStanzaValues() 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.getStanzaValues() stanza3 = stanza_class() stanza3.setStanzaValues(values) 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)
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, '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)) 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" % 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)
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.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)))