|
Packit |
130fc8 |
#!/usr/bin/env python
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
"""Tests that don't need an active D-Bus connection to run, but can be
|
|
Packit |
130fc8 |
run in isolation.
|
|
Packit |
130fc8 |
"""
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
# Copyright (C) 2006 Collabora Ltd. <http://www.collabora.co.uk/>
|
|
Packit |
130fc8 |
#
|
|
Packit |
130fc8 |
# Permission is hereby granted, free of charge, to any person
|
|
Packit |
130fc8 |
# obtaining a copy of this software and associated documentation
|
|
Packit |
130fc8 |
# files (the "Software"), to deal in the Software without
|
|
Packit |
130fc8 |
# restriction, including without limitation the rights to use, copy,
|
|
Packit |
130fc8 |
# modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
Packit |
130fc8 |
# of the Software, and to permit persons to whom the Software is
|
|
Packit |
130fc8 |
# furnished to do so, subject to the following conditions:
|
|
Packit |
130fc8 |
#
|
|
Packit |
130fc8 |
# The above copyright notice and this permission notice shall be
|
|
Packit |
130fc8 |
# included in all copies or substantial portions of the Software.
|
|
Packit |
130fc8 |
#
|
|
Packit |
130fc8 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
Packit |
130fc8 |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
Packit |
130fc8 |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
Packit |
130fc8 |
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
Packit |
130fc8 |
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
Packit |
130fc8 |
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
Packit |
130fc8 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
Packit |
130fc8 |
# DEALINGS IN THE SOFTWARE.
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
from __future__ import unicode_literals
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
import struct
|
|
Packit |
130fc8 |
import sys
|
|
Packit |
130fc8 |
import os
|
|
Packit |
130fc8 |
import unittest
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
import _dbus_bindings
|
|
Packit |
130fc8 |
import dbus
|
|
Packit |
130fc8 |
import dbus.lowlevel as lowlevel
|
|
Packit |
130fc8 |
import dbus.types as types
|
|
Packit |
130fc8 |
from dbus._compat import is_py2, is_py3
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
if is_py3:
|
|
Packit |
130fc8 |
def make_long(n):
|
|
Packit |
130fc8 |
return n
|
|
Packit |
130fc8 |
else:
|
|
Packit |
130fc8 |
def make_long(n):
|
|
Packit |
130fc8 |
return long(n)
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
if 'DBUS_TEST_UNINSTALLED' in os.environ:
|
|
Packit |
130fc8 |
builddir = os.path.normpath(os.environ["DBUS_TOP_BUILDDIR"])
|
|
Packit |
130fc8 |
pydir = os.path.normpath(os.environ["DBUS_TOP_SRCDIR"])
|
|
Packit |
130fc8 |
pkg = dbus.__file__
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
if not pkg.startswith(pydir):
|
|
Packit |
130fc8 |
raise Exception("DBus modules (%s) are not being picked up from the "
|
|
Packit |
130fc8 |
"package" % pkg)
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
if not _dbus_bindings.__file__.startswith(builddir):
|
|
Packit |
130fc8 |
raise Exception("DBus modules (%s) are not being picked up from the "
|
|
Packit |
130fc8 |
"package" % _dbus_bindings.__file__)
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
assert _dbus_bindings.__version__ == os.environ['DBUS_PYTHON_VERSION'], \
|
|
Packit |
130fc8 |
'_dbus_bindings was compiled as version %s but Automake says '\
|
|
Packit |
130fc8 |
'we should be version %s' \
|
|
Packit |
130fc8 |
% (_dbus_bindings.__version__, os.environ['DBUS_PYTHON_VERSION'])
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
assert (_dbus_bindings._python_version & 0xffff0000
|
|
Packit |
130fc8 |
== sys.hexversion & 0xffff0000), \
|
|
Packit |
130fc8 |
'_dbus_bindings was compiled for Python %x but this is Python %x, '\
|
|
Packit |
130fc8 |
'a different major version'\
|
|
Packit |
130fc8 |
% (_dbus_bindings._python_version, sys.hexversion)
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def uni(x):
|
|
Packit |
130fc8 |
"""Return a Unicode string consisting of the single Unicode character
|
|
Packit |
130fc8 |
with the given codepoint (represented as a surrogate pair if this is
|
|
Packit |
130fc8 |
a "narrow" Python build). This is a generalization of unichr().
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
uni(0x0001f639) == u'\\U0001f639' in versions where that syntax is
|
|
Packit |
130fc8 |
supported.
|
|
Packit |
130fc8 |
"""
|
|
Packit |
130fc8 |
if x <= 0xFFFF:
|
|
Packit |
130fc8 |
if is_py3:
|
|
Packit |
130fc8 |
return chr(x)
|
|
Packit |
130fc8 |
else:
|
|
Packit |
130fc8 |
return unichr(x)
|
|
Packit |
130fc8 |
elif is_py3:
|
|
Packit |
130fc8 |
return struct.pack('>I', x).decode('utf_32_be')
|
|
Packit |
130fc8 |
else:
|
|
Packit |
130fc8 |
# Python 2.6 didn't accept unicode format strings
|
|
Packit |
130fc8 |
return struct.pack(b'>I', x).decode('utf_32_be')
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def utf8(*xs):
|
|
Packit |
130fc8 |
"""Return a bytestring containing the given UTF-8 bytes.
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
utf8(0xc2, 0xa3) == b'\\xc2\\xa3' on Python versions that
|
|
Packit |
130fc8 |
allow bytestring literals (but some Python 2 versions do not).
|
|
Packit |
130fc8 |
"""
|
|
Packit |
130fc8 |
if is_py3:
|
|
Packit |
130fc8 |
return bytes(xs)
|
|
Packit |
130fc8 |
else:
|
|
Packit |
130fc8 |
return str('').join(map(chr, xs))
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
class TestTypes(unittest.TestCase):
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_Dictionary(self):
|
|
Packit |
130fc8 |
self.assertEqual(types.Dictionary({'foo':'bar'}), {'foo':'bar'})
|
|
Packit |
130fc8 |
self.assertEqual(types.Dictionary({}, variant_level=2), {})
|
|
Packit |
130fc8 |
self.assertEqual(types.Dictionary({}, variant_level=2).variant_level, 2)
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_Array(self):
|
|
Packit |
130fc8 |
self.assertEqual(types.Array(['foo','bar']), ['foo','bar'])
|
|
Packit |
130fc8 |
self.assertEqual(types.Array([], variant_level=2), [])
|
|
Packit |
130fc8 |
self.assertEqual(types.Array([], variant_level=2).variant_level, 2)
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_Double(self):
|
|
Packit |
130fc8 |
self.assertEqual(types.Double(0.0), 0.0)
|
|
Packit |
130fc8 |
self.assertEqual(types.Double(0.125, variant_level=2), 0.125)
|
|
Packit |
130fc8 |
self.assertEqual(types.Double(0.125, variant_level=2).variant_level, 2)
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_Struct(self):
|
|
Packit |
130fc8 |
x = types.Struct(('',))
|
|
Packit |
130fc8 |
self.assertEqual(x.variant_level, 0)
|
|
Packit |
130fc8 |
self.assertEqual(x, ('',))
|
|
Packit |
130fc8 |
x = types.Struct('abc', variant_level=42)
|
|
Packit |
130fc8 |
self.assertEqual(x.variant_level, 42)
|
|
Packit |
130fc8 |
self.assertEqual(x, ('a','b','c'))
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_Byte(self):
|
|
Packit |
130fc8 |
self.assertEqual(types.Byte(b'x', variant_level=2),
|
|
Packit |
130fc8 |
types.Byte(ord('x')))
|
|
Packit |
130fc8 |
self.assertEqual(types.Byte(1), 1)
|
|
Packit |
130fc8 |
self.assertEqual(types.Byte(make_long(1)), 1)
|
|
Packit |
130fc8 |
self.assertRaises(Exception, lambda: types.Byte(b'ab'))
|
|
Packit |
130fc8 |
self.assertRaises(TypeError, types.Byte, '\x12xxxxxxxxxxxxx')
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
# Byte from a unicode object: what would that even mean?
|
|
Packit |
130fc8 |
self.assertRaises(Exception,
|
|
Packit |
130fc8 |
lambda: types.Byte(b'a'.decode('latin-1')))
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_ByteArray(self):
|
|
Packit |
130fc8 |
self.assertEqual(types.ByteArray(b''), b'')
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_object_path_attr(self):
|
|
Packit |
130fc8 |
class MyObject(object):
|
|
Packit |
130fc8 |
__dbus_object_path__ = '/foo'
|
|
Packit |
130fc8 |
from _dbus_bindings import SignalMessage
|
|
Packit |
130fc8 |
self.assertEqual(SignalMessage.guess_signature(MyObject()), 'o')
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_integers(self):
|
|
Packit |
130fc8 |
subclasses = [int]
|
|
Packit |
130fc8 |
if is_py2:
|
|
Packit |
130fc8 |
subclasses.append(long)
|
|
Packit |
130fc8 |
subclasses = tuple(subclasses)
|
|
Packit |
130fc8 |
# This is an API guarantee. Note that exactly which of these types
|
|
Packit |
130fc8 |
# are ints and which of them are longs is *not* guaranteed.
|
|
Packit |
130fc8 |
for cls in (types.Int16, types.UInt16, types.Int32, types.UInt32,
|
|
Packit |
130fc8 |
types.Int64, types.UInt64):
|
|
Packit |
130fc8 |
self.assertTrue(issubclass(cls, subclasses))
|
|
Packit |
130fc8 |
self.assertTrue(isinstance(cls(0), subclasses))
|
|
Packit |
130fc8 |
self.assertEqual(cls(0), 0)
|
|
Packit |
130fc8 |
self.assertEqual(cls(23, variant_level=1), 23)
|
|
Packit |
130fc8 |
self.assertEqual(cls(23, variant_level=1).variant_level, 1)
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_integer_limits_16(self):
|
|
Packit |
130fc8 |
self.assertEqual(types.Int16(0x7fff), 0x7fff)
|
|
Packit |
130fc8 |
self.assertEqual(types.Int16(-0x8000), -0x8000)
|
|
Packit |
130fc8 |
self.assertEqual(types.UInt16(0xffff), 0xffff)
|
|
Packit |
130fc8 |
self.assertRaises(Exception, types.Int16, 0x8000)
|
|
Packit |
130fc8 |
self.assertRaises(Exception, types.Int16, -0x8001)
|
|
Packit |
130fc8 |
self.assertRaises(Exception, types.UInt16, 0x10000)
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_integer_limits_32(self):
|
|
Packit |
130fc8 |
self.assertEqual(types.Int32(0x7fffffff), 0x7fffffff)
|
|
Packit |
130fc8 |
self.assertEqual(types.Int32(make_long(-0x80000000)),
|
|
Packit |
130fc8 |
make_long(-0x80000000))
|
|
Packit |
130fc8 |
self.assertEqual(types.UInt32(make_long(0xffffffff)),
|
|
Packit |
130fc8 |
make_long(0xffffffff))
|
|
Packit |
130fc8 |
self.assertRaises(Exception, types.Int32, make_long(0x80000000))
|
|
Packit |
130fc8 |
self.assertRaises(Exception, types.Int32, make_long(-0x80000001))
|
|
Packit |
130fc8 |
self.assertRaises(Exception, types.UInt32, make_long(0x100000000))
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_integer_limits_64(self):
|
|
Packit |
130fc8 |
self.assertEqual(types.Int64(make_long(0x7fffffffffffffff)),
|
|
Packit |
130fc8 |
make_long(0x7fffffffffffffff))
|
|
Packit |
130fc8 |
self.assertEqual(types.Int64(make_long(-0x8000000000000000)),
|
|
Packit |
130fc8 |
make_long(-0x8000000000000000))
|
|
Packit |
130fc8 |
self.assertEqual(types.UInt64(make_long(0xffffffffffffffff)),
|
|
Packit |
130fc8 |
make_long(0xffffffffffffffff))
|
|
Packit |
130fc8 |
self.assertRaises(Exception, types.Int16,
|
|
Packit |
130fc8 |
make_long(0x8000000000000000))
|
|
Packit |
130fc8 |
self.assertRaises(Exception, types.Int16,
|
|
Packit |
130fc8 |
make_long(-0x8000000000000001))
|
|
Packit |
130fc8 |
self.assertRaises(Exception, types.UInt16,
|
|
Packit |
130fc8 |
make_long(0x10000000000000000))
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_Signature(self):
|
|
Packit |
130fc8 |
self.assertRaises(Exception, types.Signature, 'a')
|
|
Packit |
130fc8 |
self.assertEqual(types.Signature('ab', variant_level=23), 'ab')
|
|
Packit |
130fc8 |
self.assertTrue(isinstance(types.Signature('ab'), str))
|
|
Packit |
130fc8 |
self.assertEqual(tuple(types.Signature('ab(xt)a{sv}')),
|
|
Packit |
130fc8 |
('ab', '(xt)', 'a{sv}'))
|
|
Packit |
130fc8 |
self.assertTrue(isinstance(tuple(types.Signature('ab'))[0],
|
|
Packit |
130fc8 |
types.Signature))
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
class TestMessageMarshalling(unittest.TestCase):
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_path(self):
|
|
Packit |
130fc8 |
s = lowlevel.SignalMessage('/a/b/c', 'foo.bar', 'baz')
|
|
Packit |
130fc8 |
self.assertEqual(s.get_path(), types.ObjectPath('/a/b/c'))
|
|
Packit |
130fc8 |
self.assertEqual(type(s.get_path()), types.ObjectPath)
|
|
Packit |
130fc8 |
self.assertEqual(s.get_path_decomposed(), ['a', 'b', 'c'])
|
|
Packit |
130fc8 |
# this is true in both major versions: it's a bytestring in
|
|
Packit |
130fc8 |
# Python 2 and a Unicode string in Python 3
|
|
Packit |
130fc8 |
self.assertEqual(type(s.get_path_decomposed()[0]), str)
|
|
Packit |
130fc8 |
self.assertTrue(s.has_path('/a/b/c'))
|
|
Packit |
130fc8 |
self.assertFalse(s.has_path('/a/b'))
|
|
Packit |
130fc8 |
self.assertFalse(s.has_path('/a/b/c/d'))
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
s = lowlevel.SignalMessage('/', 'foo.bar', 'baz')
|
|
Packit |
130fc8 |
self.assertEqual(s.get_path(), types.ObjectPath('/'))
|
|
Packit |
130fc8 |
self.assertEqual(s.get_path().__class__, types.ObjectPath)
|
|
Packit |
130fc8 |
self.assertEqual(s.get_path_decomposed(), [])
|
|
Packit |
130fc8 |
self.assertTrue(s.has_path('/'))
|
|
Packit |
130fc8 |
self.assertFalse(s.has_path(None))
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_sender(self):
|
|
Packit |
130fc8 |
s = lowlevel.SignalMessage('/a/b/c', 'foo.bar', 'baz')
|
|
Packit |
130fc8 |
self.assertEqual(s.get_sender(), None)
|
|
Packit |
130fc8 |
self.assertFalse(s.has_sender(':1.23'))
|
|
Packit |
130fc8 |
s.set_sender(':1.23')
|
|
Packit |
130fc8 |
self.assertEqual(s.get_sender(), ':1.23')
|
|
Packit |
130fc8 |
# bytestring in Python 2, Unicode string in Python 3
|
|
Packit |
130fc8 |
self.assertEqual(type(s.get_sender()), str)
|
|
Packit |
130fc8 |
self.assertTrue(s.has_sender(':1.23'))
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_destination(self):
|
|
Packit |
130fc8 |
s = lowlevel.SignalMessage('/a/b/c', 'foo.bar', 'baz')
|
|
Packit |
130fc8 |
self.assertEqual(s.get_destination(), None)
|
|
Packit |
130fc8 |
self.assertFalse(s.has_destination(':1.23'))
|
|
Packit |
130fc8 |
s.set_destination(':1.23')
|
|
Packit |
130fc8 |
self.assertEqual(s.get_destination(), ':1.23')
|
|
Packit |
130fc8 |
# bytestring in Python 2, Unicode string in Python 3
|
|
Packit |
130fc8 |
self.assertEqual(type(s.get_destination()), str)
|
|
Packit |
130fc8 |
self.assertTrue(s.has_destination(':1.23'))
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_interface(self):
|
|
Packit |
130fc8 |
s = lowlevel.SignalMessage('/a/b/c', 'foo.bar', 'baz')
|
|
Packit |
130fc8 |
self.assertEqual(s.get_interface(), 'foo.bar')
|
|
Packit |
130fc8 |
# bytestring in Python 2, Unicode string in Python 3
|
|
Packit |
130fc8 |
self.assertEqual(type(s.get_interface()), str)
|
|
Packit |
130fc8 |
self.assertTrue(s.has_interface('foo.bar'))
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_member(self):
|
|
Packit |
130fc8 |
s = lowlevel.SignalMessage('/a/b/c', 'foo.bar', 'baz')
|
|
Packit |
130fc8 |
self.assertEqual(s.get_member(), 'baz')
|
|
Packit |
130fc8 |
# bytestring in Python 2, Unicode string in Python 3
|
|
Packit |
130fc8 |
self.assertEqual(type(s.get_member()), str)
|
|
Packit |
130fc8 |
self.assertTrue(s.has_member('baz'))
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_count(self):
|
|
Packit |
130fc8 |
from _dbus_bindings import SignalMessage
|
|
Packit |
130fc8 |
s = SignalMessage('/', 'foo.bar', 'baz')
|
|
Packit |
130fc8 |
try:
|
|
Packit |
130fc8 |
s.append('a', signature='ss')
|
|
Packit |
130fc8 |
except TypeError:
|
|
Packit |
130fc8 |
pass
|
|
Packit |
130fc8 |
else:
|
|
Packit |
130fc8 |
raise AssertionError('Appending too few things in a message '
|
|
Packit |
130fc8 |
'should fail')
|
|
Packit |
130fc8 |
s = SignalMessage('/', 'foo.bar', 'baz')
|
|
Packit |
130fc8 |
try:
|
|
Packit |
130fc8 |
s.append('a','b','c', signature='ss')
|
|
Packit |
130fc8 |
except TypeError:
|
|
Packit |
130fc8 |
pass
|
|
Packit |
130fc8 |
else:
|
|
Packit |
130fc8 |
raise AssertionError('Appending too many things in a message '
|
|
Packit |
130fc8 |
'should fail')
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_append(self):
|
|
Packit |
130fc8 |
aeq = self.assertEqual
|
|
Packit |
130fc8 |
from _dbus_bindings import SignalMessage
|
|
Packit |
130fc8 |
s = SignalMessage('/', 'foo.bar', 'baz')
|
|
Packit |
130fc8 |
s.append([types.Byte(1)], signature='ay')
|
|
Packit |
130fc8 |
aeq(s.get_signature(), 'ay')
|
|
Packit |
130fc8 |
aeq(s.get_args_list(), [[types.Byte(1)]])
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
s = SignalMessage('/', 'foo.bar', 'baz')
|
|
Packit |
130fc8 |
s.append([], signature='ay')
|
|
Packit |
130fc8 |
aeq(s.get_args_list(), [[]])
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_append_Byte(self):
|
|
Packit |
130fc8 |
aeq = self.assertEqual
|
|
Packit |
130fc8 |
from _dbus_bindings import SignalMessage
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
s = SignalMessage('/', 'foo.bar', 'baz')
|
|
Packit |
130fc8 |
s.append(0xFE, signature='y')
|
|
Packit |
130fc8 |
aeq(s.get_args_list(), [types.Byte(0xFE)])
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
s = SignalMessage('/', 'foo.bar', 'baz')
|
|
Packit |
130fc8 |
s.append(b'\xfe', signature='y')
|
|
Packit |
130fc8 |
aeq(s.get_args_list(), [types.Byte(0xFE)])
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
# appending a unicode object (including str in Python 3)
|
|
Packit |
130fc8 |
# is not allowed
|
|
Packit |
130fc8 |
s = SignalMessage('/', 'foo.bar', 'baz')
|
|
Packit |
130fc8 |
self.assertRaises(Exception,
|
|
Packit |
130fc8 |
lambda: s.append('a'.decode('latin-1'), signature='y'))
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
s = SignalMessage('/', 'foo.bar', 'baz')
|
|
Packit |
130fc8 |
self.assertRaises(Exception,
|
|
Packit |
130fc8 |
lambda: s.append(b'ab', signature='y'))
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_append_ByteArray(self):
|
|
Packit |
130fc8 |
aeq = self.assertEqual
|
|
Packit |
130fc8 |
from _dbus_bindings import SignalMessage
|
|
Packit |
130fc8 |
s = SignalMessage('/', 'foo.bar', 'baz')
|
|
Packit |
130fc8 |
s.append(types.ByteArray(b'ab'), signature='ay')
|
|
Packit |
130fc8 |
aeq(s.get_args_list(), [[types.Byte(b'a'), types.Byte(b'b')]])
|
|
Packit |
130fc8 |
s = SignalMessage('/', 'foo.bar', 'baz')
|
|
Packit |
130fc8 |
s.append(types.ByteArray(b'ab'), signature='av')
|
|
Packit |
130fc8 |
aeq(s.get_args_list(), [[types.Byte(b'a'), types.Byte(b'b')]])
|
|
Packit |
130fc8 |
s = SignalMessage('/', 'foo.bar', 'baz')
|
|
Packit |
130fc8 |
s.append(types.ByteArray(b''), signature='ay')
|
|
Packit |
130fc8 |
aeq(s.get_args_list(), [[]])
|
|
Packit |
130fc8 |
aeq(s.get_args_list(byte_arrays=True), [types.ByteArray(b'')])
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_append_Variant(self):
|
|
Packit |
130fc8 |
aeq = self.assertEqual
|
|
Packit |
130fc8 |
from _dbus_bindings import SignalMessage
|
|
Packit |
130fc8 |
s = SignalMessage('/', 'foo.bar', 'baz')
|
|
Packit |
130fc8 |
s.append(types.Int32(1, variant_level=0),
|
|
Packit |
130fc8 |
types.String('a', variant_level=42),
|
|
Packit |
130fc8 |
types.Array([types.Byte(b'a', variant_level=1),
|
|
Packit |
130fc8 |
types.UInt32(123, variant_level=1)],
|
|
Packit |
130fc8 |
signature='v'),
|
|
Packit |
130fc8 |
signature='vvv')
|
|
Packit |
130fc8 |
aeq(s.get_signature(), 'vvv')
|
|
Packit |
130fc8 |
args = s.get_args_list()
|
|
Packit |
130fc8 |
aeq(args[0].__class__, types.Int32)
|
|
Packit |
130fc8 |
aeq(args[0].variant_level, 1)
|
|
Packit |
130fc8 |
aeq(args[1].__class__, types.String)
|
|
Packit |
130fc8 |
aeq(args[1].variant_level, 42)
|
|
Packit |
130fc8 |
aeq(args[2].__class__, types.Array)
|
|
Packit |
130fc8 |
aeq(args[2].variant_level, 1)
|
|
Packit |
130fc8 |
aeq(args[2].signature, 'v')
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_guess_signature(self):
|
|
Packit |
130fc8 |
aeq = self.assertEqual
|
|
Packit |
130fc8 |
from _dbus_bindings import Message
|
|
Packit |
130fc8 |
aeq(Message.guess_signature(('a','b')), '(ss)')
|
|
Packit |
130fc8 |
aeq(Message.guess_signature('a','b'), 'ss')
|
|
Packit |
130fc8 |
aeq(Message.guess_signature(['a','b']), 'as')
|
|
Packit |
130fc8 |
aeq(Message.guess_signature(('a',)), '(s)')
|
|
Packit |
130fc8 |
aeq(Message.guess_signature('abc'), 's')
|
|
Packit |
130fc8 |
aeq(Message.guess_signature(types.Int32(123)), 'i')
|
|
Packit |
130fc8 |
aeq(Message.guess_signature(types.ByteArray(b'abc')), 'ay')
|
|
Packit |
130fc8 |
aeq(Message.guess_signature(('a',)), '(s)')
|
|
Packit |
130fc8 |
aeq(Message.guess_signature(['a']), 'as')
|
|
Packit |
130fc8 |
aeq(Message.guess_signature({'a':'b'}), 'a{ss}')
|
|
Packit |
130fc8 |
aeq(Message.guess_signature(types.ObjectPath('/')), 'o')
|
|
Packit |
130fc8 |
aeq(Message.guess_signature(types.Signature('x')), 'g')
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_guess_signature_python_ints(self):
|
|
Packit |
130fc8 |
aeq = self.assertEqual
|
|
Packit |
130fc8 |
from _dbus_bindings import Message
|
|
Packit |
130fc8 |
aeq(Message.guess_signature(7), 'i')
|
|
Packit |
130fc8 |
if is_py2:
|
|
Packit |
130fc8 |
aeq(Message.guess_signature(make_long(7)), 'x')
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_guess_signature_dbus_types(self):
|
|
Packit |
130fc8 |
aeq = self.assertEqual
|
|
Packit |
130fc8 |
from _dbus_bindings import Message
|
|
Packit |
130fc8 |
gs = Message.guess_signature
|
|
Packit |
130fc8 |
aeq(gs(types.Dictionary({'a':'b'})), 'a{ss}')
|
|
Packit |
130fc8 |
aeq(gs(types.Dictionary({'a':'b'}, signature='sv')), 'a{sv}')
|
|
Packit |
130fc8 |
aeq(gs(types.Dictionary({}, signature='iu')), 'a{iu}')
|
|
Packit |
130fc8 |
aeq(gs(types.Array([types.Int32(1)])), 'ai')
|
|
Packit |
130fc8 |
aeq(gs(types.Array([types.Int32(1)], signature='u')), 'au')
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_get_args_options(self):
|
|
Packit |
130fc8 |
aeq = self.assertEqual
|
|
Packit |
130fc8 |
s = _dbus_bindings.SignalMessage('/', 'foo.bar', 'baz')
|
|
Packit |
130fc8 |
s.append(b'b', b'bytes', -1, 1, 'str', 'var', signature='yayiusv')
|
|
Packit |
130fc8 |
aeq(s.get_args_list(), [
|
|
Packit |
130fc8 |
ord('b'),
|
|
Packit |
130fc8 |
[ord('b'),ord('y'),ord('t'),ord('e'), ord('s')],
|
|
Packit |
130fc8 |
-1, 1, 'str', 'var'
|
|
Packit |
130fc8 |
])
|
|
Packit |
130fc8 |
byte, bytes, int32, uint32, string, variant = s.get_args_list()
|
|
Packit |
130fc8 |
aeq(byte.__class__, types.Byte)
|
|
Packit |
130fc8 |
aeq(bytes.__class__, types.Array)
|
|
Packit |
130fc8 |
aeq(bytes[0].__class__, types.Byte)
|
|
Packit |
130fc8 |
aeq(int32.__class__, types.Int32)
|
|
Packit |
130fc8 |
aeq(uint32.__class__, types.UInt32)
|
|
Packit |
130fc8 |
aeq(string.__class__, types.String)
|
|
Packit |
130fc8 |
aeq(string.variant_level, 0)
|
|
Packit |
130fc8 |
aeq(variant.__class__, types.String)
|
|
Packit |
130fc8 |
aeq(variant.variant_level, 1)
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
byte, bytes, int32, uint32, string, variant = s.get_args_list(
|
|
Packit |
130fc8 |
byte_arrays=True)
|
|
Packit |
130fc8 |
aeq(byte.__class__, types.Byte)
|
|
Packit |
130fc8 |
aeq(bytes.__class__, types.ByteArray)
|
|
Packit |
130fc8 |
aeq(bytes, b'bytes')
|
|
Packit |
130fc8 |
if is_py3:
|
|
Packit |
130fc8 |
aeq(bytes[0].__class__, int)
|
|
Packit |
130fc8 |
else:
|
|
Packit |
130fc8 |
aeq(bytes[0].__class__, str)
|
|
Packit |
130fc8 |
aeq(int32.__class__, types.Int32)
|
|
Packit |
130fc8 |
aeq(uint32.__class__, types.UInt32)
|
|
Packit |
130fc8 |
aeq(string.__class__, types.String)
|
|
Packit |
130fc8 |
aeq(variant.__class__, types.String)
|
|
Packit |
130fc8 |
aeq(variant.variant_level, 1)
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
kwargs = {}
|
|
Packit |
130fc8 |
if is_py2:
|
|
Packit |
130fc8 |
kwargs['utf8_strings'] = True
|
|
Packit |
130fc8 |
byte, bytes, int32, uint32, string, variant = s.get_args_list(
|
|
Packit |
130fc8 |
**kwargs)
|
|
Packit |
130fc8 |
aeq(byte.__class__, types.Byte)
|
|
Packit |
130fc8 |
aeq(bytes.__class__, types.Array)
|
|
Packit |
130fc8 |
aeq(bytes[0].__class__, types.Byte)
|
|
Packit |
130fc8 |
aeq(int32.__class__, types.Int32)
|
|
Packit |
130fc8 |
aeq(uint32.__class__, types.UInt32)
|
|
Packit |
130fc8 |
if is_py2:
|
|
Packit |
130fc8 |
aeq(string.__class__, types.UTF8String)
|
|
Packit |
130fc8 |
aeq(string, 'str')
|
|
Packit |
130fc8 |
if is_py2:
|
|
Packit |
130fc8 |
aeq(variant.__class__, types.UTF8String)
|
|
Packit |
130fc8 |
aeq(variant.variant_level, 1)
|
|
Packit |
130fc8 |
aeq(variant, 'var')
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_object_path_attr(self):
|
|
Packit |
130fc8 |
from _dbus_bindings import SignalMessage
|
|
Packit |
130fc8 |
class MyObject(object):
|
|
Packit |
130fc8 |
__dbus_object_path__ = '/foo'
|
|
Packit |
130fc8 |
s = SignalMessage('/', 'foo.bar', 'baz')
|
|
Packit |
130fc8 |
s.append(MyObject(), signature='o')
|
|
Packit |
130fc8 |
s.append(MyObject())
|
|
Packit |
130fc8 |
self.assertEqual(s.get_args_list(), ['/foo', '/foo'])
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_struct(self):
|
|
Packit |
130fc8 |
from _dbus_bindings import SignalMessage
|
|
Packit |
130fc8 |
s = SignalMessage('/', 'foo.bar', 'baz')
|
|
Packit |
130fc8 |
try:
|
|
Packit |
130fc8 |
s.append(('a',), signature='(ss)')
|
|
Packit |
130fc8 |
except TypeError:
|
|
Packit |
130fc8 |
pass
|
|
Packit |
130fc8 |
else:
|
|
Packit |
130fc8 |
raise AssertionError('Appending too few things in a struct '
|
|
Packit |
130fc8 |
'should fail')
|
|
Packit |
130fc8 |
s = SignalMessage('/', 'foo.bar', 'baz')
|
|
Packit |
130fc8 |
try:
|
|
Packit |
130fc8 |
s.append(('a','b','c'), signature='(ss)')
|
|
Packit |
130fc8 |
except TypeError:
|
|
Packit |
130fc8 |
pass
|
|
Packit |
130fc8 |
else:
|
|
Packit |
130fc8 |
raise AssertionError('Appending too many things in a struct '
|
|
Packit |
130fc8 |
'should fail')
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_utf8(self):
|
|
Packit |
130fc8 |
from _dbus_bindings import SignalMessage
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
for bad in [
|
|
Packit |
130fc8 |
uni(0xD800),
|
|
Packit |
130fc8 |
utf8(0xed, 0xa0, 0x80),
|
|
Packit |
130fc8 |
]:
|
|
Packit |
130fc8 |
s = SignalMessage('/', 'foo.bar', 'baz')
|
|
Packit |
130fc8 |
try:
|
|
Packit |
130fc8 |
s.append(bad, signature='s')
|
|
Packit |
130fc8 |
except UnicodeError:
|
|
Packit |
130fc8 |
pass
|
|
Packit |
130fc8 |
else:
|
|
Packit |
130fc8 |
raise AssertionError('Appending %r should fail' % bad)
|
|
Packit |
130fc8 |
for good in [
|
|
Packit |
130fc8 |
uni(0xfdcf),
|
|
Packit |
130fc8 |
uni(0xfdf0),
|
|
Packit |
130fc8 |
uni(0xfeff),
|
|
Packit |
130fc8 |
uni(0x0001feff),
|
|
Packit |
130fc8 |
uni(0x00020000),
|
|
Packit |
130fc8 |
uni(0x0007feff),
|
|
Packit |
130fc8 |
uni(0x00080000),
|
|
Packit |
130fc8 |
uni(0x0010feff),
|
|
Packit |
130fc8 |
]:
|
|
Packit |
130fc8 |
s = SignalMessage('/', 'foo.bar', 'baz')
|
|
Packit |
130fc8 |
s.append(good, signature='s')
|
|
Packit |
130fc8 |
s.append(good.encode('utf-8'), signature='s')
|
|
Packit |
130fc8 |
for noncharacter in [
|
|
Packit |
130fc8 |
uni(0xFDD0),
|
|
Packit |
130fc8 |
utf8(0xef, 0xb7, 0x90),
|
|
Packit |
130fc8 |
uni(0xFDD7),
|
|
Packit |
130fc8 |
utf8(0xef, 0xb7, 0x97),
|
|
Packit |
130fc8 |
uni(0xFDEF),
|
|
Packit |
130fc8 |
utf8(0xef, 0xb7, 0xaf),
|
|
Packit |
130fc8 |
uni(0xFFFE),
|
|
Packit |
130fc8 |
utf8(0xef, 0xbf, 0xbe),
|
|
Packit |
130fc8 |
uni(0xFFFF),
|
|
Packit |
130fc8 |
utf8(0xef, 0xbf, 0xbf),
|
|
Packit |
130fc8 |
uni(0x0001FFFE),
|
|
Packit |
130fc8 |
utf8(0xf0, 0x9f, 0xbf, 0xbe),
|
|
Packit |
130fc8 |
uni(0x0001FFFF),
|
|
Packit |
130fc8 |
utf8(0xf0, 0x9f, 0xbf, 0xbf),
|
|
Packit |
130fc8 |
uni(0x0007FFFE),
|
|
Packit |
130fc8 |
utf8(0xf1, 0xbf, 0xbf, 0xbe),
|
|
Packit |
130fc8 |
uni(0x0007FFFF),
|
|
Packit |
130fc8 |
utf8(0xf1, 0xbf, 0xbf, 0xbf),
|
|
Packit |
130fc8 |
uni(0x0010FFFE),
|
|
Packit |
130fc8 |
utf8(0xf4, 0x8f, 0xbf, 0xbe),
|
|
Packit |
130fc8 |
uni(0x0010FFFF),
|
|
Packit |
130fc8 |
utf8(0xf4, 0x8f, 0xbf, 0xbf),
|
|
Packit |
130fc8 |
]:
|
|
Packit |
130fc8 |
s = SignalMessage('/', 'foo.bar', 'baz')
|
|
Packit |
130fc8 |
try:
|
|
Packit |
130fc8 |
s.append(noncharacter, signature='s')
|
|
Packit |
130fc8 |
except UnicodeError:
|
|
Packit |
130fc8 |
pass # libdbus < 1.6.10 disallows noncharacters
|
|
Packit |
130fc8 |
else:
|
|
Packit |
130fc8 |
pass # libdbus >= 1.6.10 allows noncharacters
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
class TestMatching(unittest.TestCase):
|
|
Packit |
130fc8 |
def setUp(self):
|
|
Packit |
130fc8 |
from _dbus_bindings import SignalMessage
|
|
Packit |
130fc8 |
from dbus.connection import SignalMatch
|
|
Packit |
130fc8 |
self._message = SignalMessage('/', 'a.b', 'c')
|
|
Packit |
130fc8 |
class FakeConn(object): pass
|
|
Packit |
130fc8 |
def ignore_cb(*args, **kws): pass
|
|
Packit |
130fc8 |
self._match = SignalMatch(FakeConn(), None, '/', None, None,
|
|
Packit |
130fc8 |
ignore_cb, arg0='/')
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_string_match(self):
|
|
Packit |
130fc8 |
self._message.append('/', signature='s')
|
|
Packit |
130fc8 |
self.assertTrue(self._match.maybe_handle_message(self._message))
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_object_path_no_match(self):
|
|
Packit |
130fc8 |
self._message.append('/', signature='o')
|
|
Packit |
130fc8 |
self.assertFalse(self._match.maybe_handle_message(self._message))
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
class TestVersion(unittest.TestCase):
|
|
Packit |
130fc8 |
if sys.version_info[:2] < (2, 7):
|
|
Packit |
130fc8 |
def assertGreater(self, first, second):
|
|
Packit |
130fc8 |
self.assertTrue(first > second)
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def assertLess(self, first, second):
|
|
Packit |
130fc8 |
self.assertTrue(first < second)
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def test_version(self):
|
|
Packit |
130fc8 |
self.assertGreater(dbus.version, (0, 41))
|
|
Packit |
130fc8 |
self.assertLess(dbus.version, (23, 0))
|
|
Packit |
130fc8 |
self.assertGreater(dbus.__version__, '0')
|
|
Packit |
130fc8 |
self.assertLess(dbus.__version__, '9')
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
if __name__ == '__main__':
|
|
Packit |
130fc8 |
# Python 2.6 doesn't accept a `verbosity` keyword.
|
|
Packit |
130fc8 |
kwargs = {}
|
|
Packit |
130fc8 |
if sys.version_info[:2] >= (2, 7):
|
|
Packit |
130fc8 |
kwargs['verbosity'] = 2
|
|
Packit |
130fc8 |
unittest.main(**kwargs)
|