def test_contains_type(): tree = SignatureTree('h') assert signature_contains_type(tree, [0], 'h') assert not signature_contains_type(tree, [0], 'u') tree = SignatureTree('ah') assert signature_contains_type(tree, [[0]], 'h') assert signature_contains_type(tree, [[0]], 'a') assert not signature_contains_type(tree, [[0]], 'u') tree = SignatureTree('av') body = [[ Variant('u', 0), Variant('i', 0), Variant('x', 0), Variant('v', Variant('s', 'hi')) ]] assert signature_contains_type(tree, body, 'u') assert signature_contains_type(tree, body, 'x') assert signature_contains_type(tree, body, 'v') assert signature_contains_type(tree, body, 's') assert not signature_contains_type(tree, body, 'o') tree = SignatureTree('a{sv}') body = { 'foo': Variant('h', 0), 'bar': Variant('i', 0), 'bat': Variant('x', 0), 'baz': Variant('v', Variant('o', '/hi')) } for expected in 'hixvso': assert signature_contains_type(tree, [body], expected) assert not signature_contains_type(tree, [body], 'b')
def test_invalid_variants(): tree = SignatureTree('a{sa{sv}}') s_con = { 'type': '802-11-wireless', 'uuid': '1234', 'id': 'SSID', } s_wifi = { 'ssid': 'SSID', 'mode': 'infrastructure', 'hidden': True, } s_wsec = { 'key-mgmt': 'wpa-psk', 'auth-alg': 'open', 'psk': 'PASSWORD', } s_ip4 = {'method': 'auto'} s_ip6 = {'method': 'auto'} con = { 'connection': s_con, '802-11-wireless': s_wifi, '802-11-wireless-security': s_wsec, 'ipv4': s_ip4, 'ipv6': s_ip6 } with pytest.raises(SignatureBodyMismatchError): tree.verify([con])
def test_simple_struct(): tree = SignatureTree('(sss)') assert len(tree.types) == 1 child = tree.types[0] assert child.signature == '(sss)' assert len(child.children) == 3 for i in range(0, 3): assert_simple_type('s', child.children[i])
def test_array(): tree = SignatureTree('as') assert len(tree.types) == 1 child = tree.types[0] assert child.signature == 'as' assert child.token == 'a' assert len(child.children) == 1 assert_simple_type('s', child.children[0])
def test_struct_multiple(): tree = SignatureTree('(s)(s)(s)') assert len(tree.types) == 3 for i in range(0, 3): child = tree.types[0] assert child.token == '(' assert child.signature == '(s)' assert len(child.children) == 1 assert_simple_type('s', child.children[0])
def test_array_multiple(): tree = SignatureTree('asasass') assert len(tree.types) == 4 assert_simple_type('s', tree.types[3]) for i in range(0, 3): array_child = tree.types[i] assert array_child.token == 'a' assert array_child.signature == 'as' assert len(array_child.children) == 1 assert_simple_type('s', array_child.children[0])
async def test_methods(): bus1 = await MessageBus().connect() bus2 = await MessageBus().connect() interface = ExampleInterface('test.interface') export_path = '/test/path' async def call(member, signature='', body=[]): return await bus2.call( Message(destination=bus1.unique_name, path=export_path, interface=interface.name, member=member, signature=signature, body=body)) bus1.export(export_path, interface) body = ['hello world'] reply = await call('echo', 's', body) assert reply.message_type == MessageType.METHOD_RETURN, reply.body[0] assert reply.signature == 's' assert reply.body == body body = ['hello', 'world'] reply = await call('echo_multiple', 'ss', body) assert reply.message_type == MessageType.METHOD_RETURN, reply.body[0] assert reply.signature == 'ss' assert reply.body == body body = [['hello', 'world'], Variant('v', Variant('(ss)', ['hello', 'world'])), { 'foo': Variant('t', 100) }, ['one', ['two', [Variant('s', 'three')]]]] signature = 'asva{sv}(s(s(v)))' SignatureTree(signature).verify(body) reply = await call('echo_containers', signature, body) assert reply.message_type == MessageType.METHOD_RETURN, reply.body[0] assert reply.signature == signature assert reply.body == body reply = await call('ping') assert reply.message_type == MessageType.METHOD_RETURN, reply.body[0] assert reply.signature == '' assert reply.body == [] reply = await call('throws_unexpected_error') assert reply.message_type == MessageType.ERROR, reply.body[0] assert reply.error_name == ErrorType.SERVICE_ERROR.value, reply.body[0] reply = await call('throws_dbus_error') assert reply.message_type == MessageType.ERROR, reply.body[0] assert reply.error_name == 'test.error', reply.body[0] assert reply.body == ['an error ocurred']
def test_dict_simple(): tree = SignatureTree('a{ss}') assert len(tree.types) == 1 child = tree.types[0] assert child.signature == 'a{ss}' assert child.token == 'a' assert len(child.children) == 1 dict_child = child.children[0] assert dict_child.token == '{' assert dict_child.signature == '{ss}' assert len(dict_child.children) == 2 assert_simple_type('s', dict_child.children[0]) assert_simple_type('s', dict_child.children[1])
def test_array_of_structs(): tree = SignatureTree('a(ss)') assert len(tree.types) == 1 child = tree.types[0] assert child.token == 'a' assert child.signature == 'a(ss)' assert len(child.children) == 1 struct_child = child.children[0] assert struct_child.token == '(' assert struct_child.signature == '(ss)' assert len(struct_child.children) == 2 for i in range(0, 2): assert_simple_type('s', struct_child.children[i])
def test_nested_struct(): tree = SignatureTree('(s(s(s)))') assert len(tree.types) == 1 child = tree.types[0] assert child.signature == '(s(s(s)))' assert child.token == '(' assert len(child.children) == 2 assert_simple_type('s', child.children[0]) first_nested = child.children[1] assert first_nested.token == '(' assert first_nested.signature == '(s(s))' assert len(first_nested.children) == 2 assert_simple_type('s', first_nested.children[0]) second_nested = first_nested.children[1] assert second_nested.token == '(' assert second_nested.signature == '(s)' assert len(second_nested.children) == 1 assert_simple_type('s', second_nested.children[0])
def test_dict_of_structs(): tree = SignatureTree('a{s(ss)}') assert len(tree.types) == 1 child = tree.types[0] assert child.token == 'a' assert child.signature == 'a{s(ss)}' assert len(child.children) == 1 dict_child = child.children[0] assert dict_child.token == '{' assert dict_child.signature == '{s(ss)}' assert len(dict_child.children) == 2 assert_simple_type('s', dict_child.children[0]) struct_child = dict_child.children[1] assert struct_child.token == '(' assert struct_child.signature == '(ss)' assert len(struct_child.children) == 2 for i in range(0, 2): assert_simple_type('s', struct_child.children[i])
def replace_variants(type_, item): if type_.token == 'v' and type(item) is not Variant: item = Variant( item['signature'], replace_variants( SignatureTree(item['signature']).types[0], item['value'])) elif type_.token == 'a': for i, item_child in enumerate(item): if type_.children[0].token == '{': for k, v in item.items(): item[k] = replace_variants(type_.children[0].children[1], v) else: item[i] = replace_variants(type_.children[0], item_child) elif type_.token == '(': for i, item_child in enumerate(item): if type_.children[0].token == '{': assert False else: item[i] = replace_variants(type_.children[i], item_child) return item
def test_multiple_simple(): tree = SignatureTree('sss') assert len(tree.types) == 3 for i in range(0, 3): assert_simple_type('s', tree.types[i])
def test_simple(): tree = SignatureTree('s') assert len(tree.types) == 1 assert_simple_type('s', tree.types[0])