# -*- encoding:utf-8 -*-
"""
Slixmpp: The Slick XMPP Library
Copyright (C) 2011 Nathanael C. Fritz, Dann Martens (TOMOTON).
This file is part of Slixmpp.
See the file LICENSE for copying permission.
"""
from __future__ import unicode_literals
import base64
import sys
from slixmpp.plugins.xep_0009.stanza.RPC import RPCQuery, MethodCall, \
MethodResponse
from slixmpp.plugins.xep_0009.binding import py2xml, xml2py, rpcbase64, \
rpctime
from slixmpp.stanza.iq import Iq
from slixmpp.test.slixtest import SlixTest
from slixmpp.xmlstream.stanzabase import register_stanza_plugin
from slixmpp.xmlstream.tostring import tostring
import unittest
class TestJabberRPC(SlixTest):
def setUp(self):
register_stanza_plugin(Iq, RPCQuery)
register_stanza_plugin(RPCQuery, MethodCall)
register_stanza_plugin(RPCQuery, MethodResponse)
def testMethodCall(self):
iq = self.Iq()
iq['rpc_query']['method_call']['method_name'] = 'system.exit'
iq['rpc_query']['method_call']['params'] = py2xml(*())
self.check(iq, """
system.exit
""", use_values=False)
def testMethodResponse(self):
iq = self.Iq()
iq['rpc_query']['method_response']['params'] = py2xml(*())
self.check(iq, """
""", use_values=False)
def testConvertNil(self):
params = [None]
params_xml = py2xml(*params)
expected_xml = self.parse_xml("""
""")
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 testConvertBoolean(self):
params = [True, False]
params_xml = py2xml(*params)
expected_xml = self.parse_xml("""
1
0
""")
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 testConvertString(self):
params = ["'This' & \"That\""]
params_xml = py2xml(*params)
expected_xml = self.parse_xml("""
'This' & "That"
""")
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 testConvertUnicodeString(self):
params = ["おはよう"]
params_xml = py2xml(*params)
expected_xml = self.parse_xml("""
おはよう
""")
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 testConvertInteger(self):
params = [32767, -32768]
params_xml = py2xml(*params)
expected_xml = self.parse_xml("""
32767
-32768
""")
alternate_xml = self.parse_xml("""
32767
-32768
""")
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 testConvertDouble(self):
params = [3.14159265]
params_xml = py2xml(*params)
expected_xml = self.parse_xml("""
3.14159265
""")
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 testConvertBase64(self):
params = [rpcbase64(base64.b64encode(b"Hello, world!"))]
params_xml = py2xml(*params)
expected_xml = self.parse_xml("""
SGVsbG8sIHdvcmxkIQ==
""")
alternate_xml = self.parse_xml("""
SGVsbG8sIHdvcmxkIQ==
""")
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 testConvertDateTime(self):
params = [rpctime("20111220T01:50:00")]
params_xml = py2xml(*params)
expected_xml = self.parse_xml("""
20111220T01:50:00
""")
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 testConvertArray(self):
params = [[1,2,3], ('a', 'b', 'c')]
params_xml = py2xml(*params)
expected_xml = self.parse_xml("""
1
2
3
a
b
c
""")
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("""
foo
bar
baz
0
""")
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")
suite = unittest.TestLoader().loadTestsFromTestCase(TestJabberRPC)