1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
from dbus_next.service import ServiceInterface, dbus_property, PropertyAccess
from dbus_next.signature import Variant
from dbus_next.aio import MessageBus
from dbus_next import Message, MessageType, introspection as intr
from dbus_next.constants import ErrorType
import pytest
standard_interfaces_count = len(intr.Node.default().interfaces)
class ExampleInterface(ServiceInterface):
def __init__(self, name):
super().__init__(name)
class ExampleComplexInterface(ServiceInterface):
def __init__(self, name):
self._foo = 42
self._bar = 'str'
self._async_prop = 'async'
super().__init__(name)
@dbus_property(access=PropertyAccess.READ)
def Foo(self) -> 'y':
return self._foo
@dbus_property(access=PropertyAccess.READ)
def Bar(self) -> 's':
return self._bar
@dbus_property(access=PropertyAccess.READ)
async def AsyncProp(self) -> 's':
return self._async_prop
@pytest.mark.asyncio
async def test_introspectable_interface():
bus1 = await MessageBus().connect()
bus2 = await MessageBus().connect()
interface = ExampleInterface('test.interface')
interface2 = ExampleInterface('test.interface2')
export_path = '/test/path'
bus1.export(export_path, interface)
bus1.export(export_path, interface2)
reply = await bus2.call(
Message(destination=bus1.unique_name,
path=export_path,
interface='org.freedesktop.DBus.Introspectable',
member='Introspect'))
assert reply.message_type == MessageType.METHOD_RETURN, reply.body[0]
assert reply.signature == 's'
node = intr.Node.parse(reply.body[0])
assert len(node.interfaces) == standard_interfaces_count + 2
assert node.interfaces[-1].name == 'test.interface2'
assert node.interfaces[-2].name == 'test.interface'
assert not node.nodes
# introspect works on every path
reply = await bus2.call(
Message(destination=bus1.unique_name,
path='/path/doesnt/exist',
interface='org.freedesktop.DBus.Introspectable',
member='Introspect'))
assert reply.message_type == MessageType.METHOD_RETURN, reply.body[0]
assert reply.signature == 's'
node = intr.Node.parse(reply.body[0])
assert not node.interfaces
assert not node.nodes
@pytest.mark.asyncio
async def test_peer_interface():
bus1 = await MessageBus().connect()
bus2 = await MessageBus().connect()
reply = await bus2.call(
Message(destination=bus1.unique_name,
path='/path/doesnt/exist',
interface='org.freedesktop.DBus.Peer',
member='Ping'))
assert reply.message_type == MessageType.METHOD_RETURN, reply.body[0]
assert reply.signature == ''
reply = await bus2.call(
Message(destination=bus1.unique_name,
path='/path/doesnt/exist',
interface='org.freedesktop.DBus.Peer',
member='GetMachineId',
signature=''))
assert reply.message_type == MessageType.METHOD_RETURN, reply.body[0]
assert reply.signature == 's'
@pytest.mark.asyncio
async def test_object_manager():
expected_reply = {
'/test/path/deeper': {
'test.interface2': {
'Bar': Variant('s', 'str'),
'Foo': Variant('y', 42),
'AsyncProp': Variant('s', 'async'),
}
}
}
reply_ext = {
'/test/path': {
'test.interface1': {},
'test.interface2': {
'Bar': Variant('s', 'str'),
'Foo': Variant('y', 42),
'AsyncProp': Variant('s', 'async'),
}
}
}
bus1 = await MessageBus().connect()
bus2 = await MessageBus().connect()
interface = ExampleInterface('test.interface1')
interface2 = ExampleComplexInterface('test.interface2')
export_path = '/test/path'
bus1.export(export_path, interface)
bus1.export(export_path, interface2)
bus1.export(export_path + '/deeper', interface2)
reply_root = await bus2.call(
Message(destination=bus1.unique_name,
path='/',
interface='org.freedesktop.DBus.ObjectManager',
member='GetManagedObjects'))
reply_level1 = await bus2.call(
Message(destination=bus1.unique_name,
path=export_path,
interface='org.freedesktop.DBus.ObjectManager',
member='GetManagedObjects'))
reply_level2 = await bus2.call(
Message(destination=bus1.unique_name,
path=export_path + '/deeper',
interface='org.freedesktop.DBus.ObjectManager',
member='GetManagedObjects'))
assert reply_root.signature == 'a{oa{sa{sv}}}'
assert reply_level1.signature == 'a{oa{sa{sv}}}'
assert reply_level2.signature == 'a{oa{sa{sv}}}'
assert reply_level2.body == [{}]
assert reply_level1.body == [expected_reply]
expected_reply.update(reply_ext)
assert reply_root.body == [expected_reply]
@pytest.mark.asyncio
async def test_standard_interface_properties():
# standard interfaces have no properties, but should still behave correctly
# when you try to call the methods anyway (#49)
bus1 = await MessageBus().connect()
bus2 = await MessageBus().connect()
interface = ExampleInterface('test.interface1')
export_path = '/test/path'
bus1.export(export_path, interface)
for iface in [
'org.freedesktop.DBus.Properties', 'org.freedesktop.DBus.Introspectable',
'org.freedesktop.DBus.Peer', 'org.freedesktop.DBus.ObjectManager'
]:
result = await bus2.call(
Message(destination=bus1.unique_name,
path=export_path,
interface='org.freedesktop.DBus.Properties',
member='Get',
signature='ss',
body=[iface, 'anything']))
assert result.message_type is MessageType.ERROR
assert result.error_name == ErrorType.UNKNOWN_PROPERTY.value
result = await bus2.call(
Message(destination=bus1.unique_name,
path=export_path,
interface='org.freedesktop.DBus.Properties',
member='Set',
signature='ssv',
body=[iface, 'anything', Variant('s', 'new thing')]))
assert result.message_type is MessageType.ERROR
assert result.error_name == ErrorType.UNKNOWN_PROPERTY.value
result = await bus2.call(
Message(destination=bus1.unique_name,
path=export_path,
interface='org.freedesktop.DBus.Properties',
member='GetAll',
signature='s',
body=[iface]))
assert result.message_type is MessageType.METHOD_RETURN
assert result.body == [{}]
|