class TestMatching(unittest.TestCase): def setUp(self): from _dbus_bindings import SignalMessage from dbus.connection import SignalMatch self._message = SignalMessage('/', 'a.b', 'c') class FakeConn(object): pass def ignore_cb(*args, **kws): pass self._match = SignalMatch(FakeConn(), None, '/', None, None, ignore_cb, arg0='/') def test_string_match(self): self._message.append('/', signature='s') self.assertTrue(self._match.maybe_handle_message(self._message)) def test_object_path_no_match(self): self._message.append('/', signature='o') self.assertFalse(self._match.maybe_handle_message(self._message))
def test_object_path_attr(self): from _dbus_bindings import SignalMessage class MyObject(object): __dbus_object_path__ = '/foo' s = SignalMessage('/', 'foo.bar', 'baz') s.append(MyObject(), signature='o') s.append(MyObject()) self.assertEqual(s.get_args_list(), ['/foo', '/foo'])
def test_append_ByteArray(self): aeq = self.assertEquals from _dbus_bindings import SignalMessage s = SignalMessage('/', 'foo.bar', 'baz') s.append(types.ByteArray('ab'), signature='ay') aeq(s.get_args_list(), [[types.Byte('a'), types.Byte('b')]]) s = SignalMessage('/', 'foo.bar', 'baz') s.append(types.ByteArray('ab'), signature='av') aeq(s.get_args_list(), [[types.Byte('a'), types.Byte('b')]])
def test_append(self): aeq = self.assertEqual from _dbus_bindings import SignalMessage s = SignalMessage('/', 'foo.bar', 'baz') s.append([types.Byte(1)], signature='ay') aeq(s.get_signature(), 'ay') aeq(s.get_args_list(), [[types.Byte(1)]]) s = SignalMessage('/', 'foo.bar', 'baz') s.append([], signature='ay') aeq(s.get_args_list(), [[]])
def test_utf8(self): from _dbus_bindings import SignalMessage for bad in [ uni(0xD800), b'\xed\xa0\x80', ]: s = SignalMessage('/', 'foo.bar', 'baz') try: s.append(bad, signature='s') except UnicodeError: pass else: raise AssertionError('Appending %r should fail' % bad) for good in [ uni(0xfdcf), uni(0xfdf0), uni(0xfeff), uni(0x0001feff), uni(0x00020000), uni(0x0007feff), uni(0x00080000), uni(0x0010feff), ]: s = SignalMessage('/', 'foo.bar', 'baz') s.append(good, signature='s') s.append(good.encode('utf-8'), signature='s') for noncharacter in [ uni(0xFDD0), b'\xef\xb7\x90', uni(0xFDD7), b'\xef\xb7\x97', uni(0xFDEF), b'\xef\xb7\xaf', uni(0xFFFE), b'\xef\xbf\xbe', uni(0xFFFF), b'\xef\xbf\xbf', uni(0x0001FFFE), b'\xf0\x9f\xbf\xbe', uni(0x0001FFFF), b'\xf0\x9f\xbf\xbf', uni(0x0007FFFE), b'\xf1\xbf\xbf\xbe', uni(0x0007FFFF), b'\xf1\xbf\xbf\xbf', uni(0x0010FFFE), b'\xf4\x8f\xbf\xbe', uni(0x0010FFFF), b'\xf4\x8f\xbf\xbf', ]: s = SignalMessage('/', 'foo.bar', 'baz') try: s.append(noncharacter, signature='s') except UnicodeError: raise AssertionError('Appending %r should succeed' % noncharacter) else: pass # libdbus >= 1.6.10 allows noncharacters
def test_utf8(self): from _dbus_bindings import SignalMessage for bad in [ uni(0xD800), b'\xed\xa0\x80', ]: s = SignalMessage('/', 'foo.bar', 'baz') try: s.append(bad, signature='s') except UnicodeError: pass else: raise AssertionError('Appending %r should fail' % bad) for good in [ uni(0xfdcf), uni(0xfdf0), uni(0xfeff), uni(0x0001feff), uni(0x00020000), uni(0x0007feff), uni(0x00080000), uni(0x0010feff), ]: s = SignalMessage('/', 'foo.bar', 'baz') s.append(good, signature='s') s.append(good.encode('utf-8'), signature='s') for noncharacter in [ uni(0xFDD0), b'\xef\xb7\x90', uni(0xFDD7), b'\xef\xb7\x97', uni(0xFDEF), b'\xef\xb7\xaf', uni(0xFFFE), b'\xef\xbf\xbe', uni(0xFFFF), b'\xef\xbf\xbf', uni(0x0001FFFE), b'\xf0\x9f\xbf\xbe', uni(0x0001FFFF), b'\xf0\x9f\xbf\xbf', uni(0x0007FFFE), b'\xf1\xbf\xbf\xbe', uni(0x0007FFFF), b'\xf1\xbf\xbf\xbf', uni(0x0010FFFE), b'\xf4\x8f\xbf\xbe', uni(0x0010FFFF), b'\xf4\x8f\xbf\xbf', ]: s = SignalMessage('/', 'foo.bar', 'baz') try: s.append(noncharacter, signature='s') except UnicodeError: pass # libdbus < 1.6.10 disallows noncharacters else: pass # libdbus >= 1.6.10 allows noncharacters
def test_struct(self): from _dbus_bindings import SignalMessage s = SignalMessage('/', 'foo.bar', 'baz') try: s.append(('a',), signature='(ss)') except TypeError: pass else: raise AssertionError('Appending too few things in a struct ' 'should fail') s = SignalMessage('/', 'foo.bar', 'baz') try: s.append(('a','b','c'), signature='(ss)') except TypeError: pass else: raise AssertionError('Appending too many things in a struct ' 'should fail')
def test_append_Variant(self): aeq = self.assertEqual from _dbus_bindings import SignalMessage s = SignalMessage('/', 'foo.bar', 'baz') s.append(types.Int32(1, variant_level=0), types.String('a', variant_level=42), types.Array([types.Byte(b'a', variant_level=1), types.UInt32(123, variant_level=1)], signature='v'), signature='vvv') aeq(s.get_signature(), 'vvv') args = s.get_args_list() aeq(args[0].__class__, types.Int32) aeq(args[0].variant_level, 1) aeq(args[1].__class__, types.String) aeq(args[1].variant_level, 42) aeq(args[2].__class__, types.Array) aeq(args[2].variant_level, 1) aeq(args[2].signature, 'v')
def test_append_ByteArray(self): aeq = self.assertEqual from _dbus_bindings import SignalMessage s = SignalMessage('/', 'foo.bar', 'baz') s.append(types.ByteArray(b'ab'), signature='ay') aeq(s.get_args_list(), [[types.Byte(b'a'), types.Byte(b'b')]]) s = SignalMessage('/', 'foo.bar', 'baz') s.append(types.ByteArray(b'ab'), signature='av') aeq(s.get_args_list(), [[types.Byte(b'a'), types.Byte(b'b')]]) s = SignalMessage('/', 'foo.bar', 'baz') s.append(types.ByteArray(b''), signature='ay') aeq(s.get_args_list(), [[]]) aeq(s.get_args_list(byte_arrays=True), [types.ByteArray(b'')])
def test_append_Byte(self): aeq = self.assertEqual from _dbus_bindings import SignalMessage s = SignalMessage('/', 'foo.bar', 'baz') s.append(0xFE, signature='y') aeq(s.get_args_list(), [types.Byte(0xFE)]) s = SignalMessage('/', 'foo.bar', 'baz') s.append(b'\xfe', signature='y') aeq(s.get_args_list(), [types.Byte(0xFE)]) # appending a unicode object (including str in Python 3) # is not allowed s = SignalMessage('/', 'foo.bar', 'baz') self.assertRaises( Exception, lambda: s.append('a'.decode('latin-1'), signature='y')) s = SignalMessage('/', 'foo.bar', 'baz') self.assertRaises(Exception, lambda: s.append(b'ab', signature='y'))
def test_append_Byte(self): aeq = self.assertEqual from _dbus_bindings import SignalMessage s = SignalMessage('/', 'foo.bar', 'baz') s.append(0xFE, signature='y') aeq(s.get_args_list(), [types.Byte(0xFE)]) s = SignalMessage('/', 'foo.bar', 'baz') s.append(b'\xfe', signature='y') aeq(s.get_args_list(), [types.Byte(0xFE)]) # appending a unicode object (including str in Python 3) # is not allowed s = SignalMessage('/', 'foo.bar', 'baz') self.assertRaises(Exception, lambda: s.append('a'.decode('latin-1'), signature='y')) s = SignalMessage('/', 'foo.bar', 'baz') self.assertRaises(Exception, lambda: s.append(b'ab', signature='y'))
def test_utf8(self): from _dbus_bindings import SignalMessage if is_py3: def utf8(*xs): return bytes(xs) def uni(x): return chr(x) else: def utf8(*xs): return str('').join(map(chr, xs)) def uni(x): return unichr(x) for bad in [ uni(0xD800), utf8(0xed, 0xa0, 0x80), ]: s = SignalMessage('/', 'foo.bar', 'baz') try: s.append(bad, signature='s') except UnicodeError: pass else: raise AssertionError('Appending %r should fail' % bad) for good in [ uni(0xfdcf), uni(0xfdf0), uni(0xfeff), uni(0x0001feff), uni(0x00020000), uni(0x0007feff), uni(0x00080000), uni(0x0010feff), ]: s = SignalMessage('/', 'foo.bar', 'baz') s.append(good, signature='s') s.append(good.encode('utf-8'), signature='s') for noncharacter in [ uni(0xFDD0), utf8(0xef, 0xb7, 0x90), uni(0xFDD7), utf8(0xef, 0xb7, 0x97), uni(0xFDEF), utf8(0xef, 0xb7, 0xaf), uni(0xFFFE), utf8(0xef, 0xbf, 0xbe), uni(0xFFFF), utf8(0xef, 0xbf, 0xbf), uni(0x0001FFFE), utf8(0xf0, 0x9f, 0xbf, 0xbe), uni(0x0001FFFF), utf8(0xf0, 0x9f, 0xbf, 0xbf), uni(0x0007FFFE), utf8(0xf1, 0xbf, 0xbf, 0xbe), uni(0x0007FFFF), utf8(0xf1, 0xbf, 0xbf, 0xbf), uni(0x0010FFFE), utf8(0xf4, 0x8f, 0xbf, 0xbe), uni(0x0010FFFF), utf8(0xf4, 0x8f, 0xbf, 0xbf), ]: s = SignalMessage('/', 'foo.bar', 'baz') try: s.append(noncharacter, signature='s') except UnicodeError: pass # libdbus < 1.6.10 disallows noncharacters else: pass # libdbus >= 1.6.10 allows noncharacters