async def test_export_unexport():
    interface = ExampleInterface('test.interface')
    interface2 = ExampleInterface('test.interface2')

    export_path = '/test/path'
    export_path2 = '/test/path/child'

    bus = await MessageBus().connect()
    bus.export(export_path, interface)
    assert export_path in bus._path_exports
    assert len(bus._path_exports[export_path]) == 1
    assert bus._path_exports[export_path][0] is interface
    assert len(ServiceInterface._get_buses(interface)) == 1

    bus.export(export_path2, interface2)

    node = bus._introspect_export_path(export_path)
    assert len(node.interfaces) == standard_interfaces_count + 1
    assert len(node.nodes) == 1
    # relative path
    assert node.nodes[0].name == 'child'

    bus.unexport(export_path, interface)
    assert export_path not in bus._path_exports
    assert len(ServiceInterface._get_buses(interface)) == 0

    bus.export(export_path2, interface)
    assert len(bus._path_exports[export_path2]) == 2

    # test unexporting the whole path
    bus.unexport(export_path2)
    assert not bus._path_exports
    assert not ServiceInterface._get_buses(interface)
    assert not ServiceInterface._get_buses(interface2)

    # test unexporting by name
    bus.export(export_path, interface)
    bus.unexport(export_path, interface.name)
    assert not bus._path_exports
    assert not ServiceInterface._get_buses(interface)

    node = bus._introspect_export_path('/path/doesnt/exist')
    assert type(node) is intr.Node
    assert not node.interfaces
    assert not node.nodes
Esempio n. 2
0
async def test_fn_result_to_body(result, out_signature, expected):
    out_signature_tree = SignatureTree(out_signature)
    assert ServiceInterface._fn_result_to_body(result, out_signature_tree) == expected
Esempio n. 3
0
async def test_property_methods(interface_class):
    bus1 = await MessageBus().connect()
    bus2 = await MessageBus().connect()

    interface = interface_class('test.interface')
    export_path = '/test/path'
    bus1.export(export_path, interface)

    async def call_properties(member, signature, body):
        return await bus2.call(
            Message(destination=bus1.unique_name,
                    path=export_path,
                    interface='org.freedesktop.DBus.Properties',
                    member=member,
                    signature=signature,
                    body=body))

    result = await call_properties('GetAll', 's', [interface.name])

    assert result.message_type == MessageType.METHOD_RETURN, result.body[0]
    assert result.signature == 'a{sv}'
    assert result.body == [{
        'string_prop': Variant('s', interface._string_prop),
        'readonly_prop': Variant('t', interface._readonly_prop),
        'container_prop': Variant('a(ss)', interface._container_prop),
        'renamed_prop': Variant('s', interface._renamed_prop)
    }]

    result = await call_properties('Get', 'ss', [interface.name, 'string_prop'])
    assert result.message_type == MessageType.METHOD_RETURN, result.body[0]
    assert result.signature == 'v'
    assert result.body == [Variant('s', 'hi')]

    result = await call_properties(
        'Set', 'ssv',
        [interface.name, 'string_prop', Variant('s', 'ho')])
    assert result.message_type == MessageType.METHOD_RETURN, result.body[0]
    assert interface._string_prop == 'ho'
    if interface_class is AsyncInterface:
        assert 'ho', await interface.string_prop()
    else:
        assert 'ho', interface.string_prop

    result = await call_properties(
        'Set', 'ssv',
        [interface.name, 'readonly_prop', Variant('t', 100)])
    assert result.message_type == MessageType.ERROR, result.body[0]
    assert result.error_name == ErrorType.PROPERTY_READ_ONLY.value, result.body[0]

    result = await call_properties(
        'Set', 'ssv',
        [interface.name, 'disabled_prop', Variant('s', 'asdf')])
    assert result.message_type == MessageType.ERROR, result.body[0]
    assert result.error_name == ErrorType.UNKNOWN_PROPERTY.value

    result = await call_properties(
        'Set', 'ssv',
        [interface.name, 'not_a_prop', Variant('s', 'asdf')])
    assert result.message_type == MessageType.ERROR, result.body[0]
    assert result.error_name == ErrorType.UNKNOWN_PROPERTY.value

    # wrong type
    result = await call_properties('Set', 'ssv', [interface.name, 'string_prop', Variant('t', 100)])
    assert result.message_type == MessageType.ERROR
    assert result.error_name == ErrorType.INVALID_SIGNATURE.value

    # enable the erroring properties so we can test them
    for prop in ServiceInterface._get_properties(interface):
        if prop.name in ['throws_error', 'returns_wrong_type']:
            prop.disabled = False

    result = await call_properties('Get', 'ss', [interface.name, 'returns_wrong_type'])
    assert result.message_type == MessageType.ERROR, result.body[0]
    assert result.error_name == ErrorType.SERVICE_ERROR.value

    result = await call_properties(
        'Set', 'ssv',
        [interface.name, 'throws_error', Variant('s', 'ho')])
    assert result.message_type == MessageType.ERROR, result.body[0]
    assert result.error_name == 'test.error'
    assert result.body == ['told you so']

    result = await call_properties('Get', 'ss', [interface.name, 'throws_error'])
    assert result.message_type == MessageType.ERROR, result.body[0]
    assert result.error_name == 'test.error'
    assert result.body == ['told you so']

    result = await call_properties('GetAll', 's', [interface.name])
    assert result.message_type == MessageType.ERROR, result.body[0]
    assert result.error_name == 'test.error'
    assert result.body == ['told you so']
Esempio n. 4
0
def test_method_decorator():
    interface = ExampleInterface()
    assert interface.name == 'test.interface'

    properties = ServiceInterface._get_properties(interface)
    methods = ServiceInterface._get_methods(interface)
    signals = ServiceInterface._get_signals(interface)

    assert len(methods) == 2

    method = methods[0]
    assert method.name == 'renamed_method'
    assert method.in_signature == 'ot'
    assert method.out_signature == ''
    assert method.disabled
    assert type(method.introspection) is intr.Method

    method = methods[1]
    assert method.name == 'some_method'
    assert method.in_signature == 'ss'
    assert method.out_signature == 's'
    assert not method.disabled
    assert type(method.introspection) is intr.Method

    assert len(signals) == 2

    signal = signals[0]
    assert signal.name == 'renamed_signal'
    assert signal.signature == '(dodo)'
    assert signal.disabled
    assert type(signal.introspection) is intr.Signal

    signal = signals[1]
    assert signal.name == 'some_signal'
    assert signal.signature == 'as'
    assert not signal.disabled
    assert type(signal.introspection) is intr.Signal

    assert len(properties) == 3

    renamed_readonly_prop = properties[0]
    assert renamed_readonly_prop.name == 'renamed_readonly_property'
    assert renamed_readonly_prop.signature == 't'
    assert renamed_readonly_prop.access == PropertyAccess.READ
    assert renamed_readonly_prop.disabled
    assert type(renamed_readonly_prop.introspection) is intr.Property

    weird_prop = properties[1]
    assert weird_prop.name == 'weird_prop'
    assert weird_prop.access == PropertyAccess.READWRITE
    assert weird_prop.signature == 't'
    assert not weird_prop.disabled
    assert weird_prop.prop_getter is not None
    assert weird_prop.prop_getter.__name__ == 'weird_prop'
    assert weird_prop.prop_setter is not None
    assert weird_prop.prop_setter.__name__ == 'setter_for_weird_prop'
    assert type(weird_prop.introspection) is intr.Property

    prop = properties[2]
    assert prop.name == 'some_prop'
    assert prop.access == PropertyAccess.READWRITE
    assert prop.signature == 'u'
    assert not prop.disabled
    assert prop.prop_getter is not None
    assert prop.prop_setter is not None
    assert type(prop.introspection) is intr.Property

    # make sure the getter and setter actually work
    assert interface._some_prop == 55
    interface._some_prop = 555
    assert interface.some_prop == 555

    assert interface._weird_prop == 500
    assert weird_prop.prop_getter(interface) == 500
    interface._weird_prop = 1001
    assert interface._weird_prop == 1001
    weird_prop.prop_setter(interface, 600)
    assert interface._weird_prop == 600