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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
|
# SPDX-License-Identifier: LGPL-2.1-or-later
# Copyright (C) 2020-2022 igo95862
# This file is part of python-sdbus
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
from __future__ import annotations
from sdbus.exceptions import DbusUnknownObjectError
from sdbus.unittest import IsolatedDbusTestCase
from sdbus.utils import (
parse_get_managed_objects,
parse_interfaces_added,
parse_interfaces_removed,
)
from sdbus import (
DbusInterfaceCommonAsync,
DbusObjectManagerInterfaceAsync,
dbus_method_async,
dbus_property_async,
)
HELLO_WORLD = 'Hello World!'
TEST_NUMBER = 1000
class ObjectManagerTestInterface(
DbusObjectManagerInterfaceAsync,
interface_name='org.test.objectmanager',
):
@dbus_method_async(
result_signature='s',
)
async def get_hello_world(self) -> str:
return HELLO_WORLD
OBJECT_MANAGER_PATH = '/object_manager'
CONNECTION_NAME = 'org.example.test'
MANAGED_INTERFACE_NAME = 'org.test.testing'
class ManagedInterface(
DbusInterfaceCommonAsync,
interface_name=MANAGED_INTERFACE_NAME,
):
@dbus_property_async('x')
def test_int(self) -> int:
return TEST_NUMBER
MANAGED_PATH = '/object_manager/test'
class TestObjectManager(IsolatedDbusTestCase):
async def test_object_manager(self) -> None:
await self.bus.request_name_async(CONNECTION_NAME, 0)
object_manager = ObjectManagerTestInterface()
object_manager.export_to_dbus(OBJECT_MANAGER_PATH)
object_manager_connection = ObjectManagerTestInterface.new_proxy(
CONNECTION_NAME, OBJECT_MANAGER_PATH)
self.assertEqual(
await object_manager_connection.get_hello_world(),
HELLO_WORLD)
managed_object = ManagedInterface()
async with self.assertDbusSignalEmits(
object_manager_connection.interfaces_added
) as added_interfaces_catch:
object_manager.export_with_manager(MANAGED_PATH, managed_object)
caught_added = added_interfaces_catch.output[0]
added_path, added_attributes = caught_added
self.assertEqual(added_path, MANAGED_PATH)
self.assertEqual(
added_attributes[
MANAGED_INTERFACE_NAME][
'TestInt'][1],
TEST_NUMBER,
)
with self.subTest("Test interfaces added parser"):
parse_interfaces_added(ManagedInterface, caught_added)
async with self.assertDbusSignalEmits(
object_manager_connection.interfaces_removed
) as removed_interfaces_catch:
object_manager.remove_managed_object(managed_object)
path_removed, interfaces_removed = removed_interfaces_catch.output[0]
self.assertEqual(path_removed, MANAGED_PATH)
self.assertIn(MANAGED_INTERFACE_NAME, interfaces_removed)
def test_expot_with_no_manager(self) -> None:
object_manager = ObjectManagerTestInterface()
managed_object = ManagedInterface()
self.assertRaises(
RuntimeError,
object_manager.export_with_manager,
MANAGED_PATH,
managed_object,
)
async def test_parse_interfaces_added_removed(self) -> None:
MANAGED_TWO_INTERFACE_NAME = MANAGED_INTERFACE_NAME + 'Two'
class ManagedTwoInterface(
ManagedInterface,
interface_name=MANAGED_TWO_INTERFACE_NAME,
):
@dbus_property_async('s')
def test_str(self) -> str:
return 'test'
await self.bus.request_name_async(CONNECTION_NAME, 0)
object_manager = DbusObjectManagerInterfaceAsync()
object_manager.export_to_dbus(OBJECT_MANAGER_PATH)
object_manager_connection = DbusObjectManagerInterfaceAsync.new_proxy(
CONNECTION_NAME, OBJECT_MANAGER_PATH)
managed_object = ManagedTwoInterface()
async with self.assertDbusSignalEmits(
object_manager_connection.interfaces_added
) as added_interfaces_catch:
object_manager.export_with_manager(MANAGED_PATH, managed_object)
caught_added = added_interfaces_catch.output[0]
with self.subTest('Parse added class'):
path, python_class, python_properties = (
parse_interfaces_added(ManagedTwoInterface, caught_added)
)
self.assertEqual(path, MANAGED_PATH)
self.assertEqual(python_class, ManagedTwoInterface)
self.assertIn('test_str', python_properties)
self.assertIn('test_int', python_properties)
with self.subTest('Parse added object'):
path, python_class, python_properties = (
parse_interfaces_added(managed_object, caught_added)
)
self.assertEqual(path, MANAGED_PATH)
self.assertEqual(python_class, ManagedTwoInterface)
self.assertIn('test_str', python_properties)
self.assertIn('test_int', python_properties)
with self.subTest('Parse added iterable'):
path, python_class, python_properties = (
parse_interfaces_added(
(ManagedInterface, ManagedTwoInterface),
caught_added)
)
self.assertEqual(path, MANAGED_PATH)
self.assertEqual(python_class, ManagedTwoInterface)
self.assertIn('test_str', python_properties)
self.assertIn('test_int', python_properties)
with self.subTest('Parse added unknown'):
with self.assertRaises(KeyError):
path, python_class, python_properties = (
parse_interfaces_added(
ManagedInterface,
caught_added)
)
with self.assertRaises(KeyError):
path, python_class, python_properties = (
parse_interfaces_added(
ManagedInterface,
caught_added,
on_unknown_interface='none',
)
)
path, python_class, python_properties = (
parse_interfaces_added(
ManagedInterface,
caught_added,
on_unknown_interface='none',
on_unknown_member='reuse',
)
)
self.assertEqual(path, MANAGED_PATH)
self.assertIsNone(python_class)
self.assertIn('TestStr', python_properties)
self.assertIn('TestInt', python_properties)
get_managed_data = (
await object_manager_connection.get_managed_objects()
)
with self.subTest('Parse get managed objects class'):
managed_dict = (
parse_get_managed_objects(
ManagedTwoInterface,
get_managed_data,
)
)
self.assertIn(MANAGED_PATH, managed_dict)
managed_class, managed_properties = (
managed_dict[MANAGED_PATH]
)
self.assertEqual(managed_class, ManagedTwoInterface)
self.assertIn('test_str', managed_properties)
self.assertIn('test_int', managed_properties)
with self.subTest('Parse get managed objects object'):
managed_dict = (
parse_get_managed_objects(
managed_object,
get_managed_data,
)
)
self.assertIn(MANAGED_PATH, managed_dict)
managed_class, managed_properties = (
managed_dict[MANAGED_PATH]
)
self.assertEqual(managed_class, ManagedTwoInterface)
self.assertIn('test_str', managed_properties)
self.assertIn('test_int', managed_properties)
with self.subTest('Parse get managed objects iterable'):
managed_dict = (
parse_get_managed_objects(
(ManagedInterface, ManagedTwoInterface),
get_managed_data,
)
)
self.assertIn(MANAGED_PATH, managed_dict)
managed_class, managed_properties = (
managed_dict[MANAGED_PATH]
)
self.assertEqual(managed_class, ManagedTwoInterface)
self.assertIn('test_str', managed_properties)
self.assertIn('test_int', managed_properties)
with self.subTest('Parse get managed objects unknown'):
with self.assertRaises(KeyError):
managed_dict = (
parse_get_managed_objects(
ManagedInterface,
get_managed_data,
)
)
with self.assertRaises(KeyError):
managed_dict = (
parse_get_managed_objects(
ManagedInterface,
get_managed_data,
on_unknown_interface='none',
)
)
managed_dict = (
parse_get_managed_objects(
ManagedInterface,
get_managed_data,
on_unknown_interface='none',
on_unknown_member='reuse',
)
)
path, python_class, python_properties = (
parse_interfaces_added(
ManagedInterface,
caught_added,
on_unknown_interface='none',
on_unknown_member='reuse',
)
)
self.assertIn(MANAGED_PATH, managed_dict)
managed_class, managed_properties = (
managed_dict[MANAGED_PATH]
)
self.assertIsNone(managed_class)
self.assertIn('TestStr', managed_properties)
self.assertIn('TestInt', managed_properties)
async with self.assertDbusSignalEmits(
object_manager_connection.interfaces_removed
) as removed_interfaces_catch:
object_manager.remove_managed_object(managed_object)
interfaces_removed_data = removed_interfaces_catch.output[0]
with self.subTest('Parse removed class'):
path, python_class = (
parse_interfaces_removed(
ManagedTwoInterface,
interfaces_removed_data,
)
)
self.assertEqual(path, MANAGED_PATH)
self.assertEqual(python_class, ManagedTwoInterface)
with self.subTest('Parse removed unknown'):
with self.assertRaises(KeyError):
path, python_class = (
parse_interfaces_removed(
ManagedInterface,
interfaces_removed_data,
)
)
path, python_class = (
parse_interfaces_removed(
ManagedInterface,
interfaces_removed_data,
on_unknown_interface='none',
)
)
self.assertEqual(path, MANAGED_PATH)
self.assertIsNone(python_class)
async def test_main_export_handle(self) -> None:
await self.bus.request_name_async(CONNECTION_NAME, 0)
object_manager = ObjectManagerTestInterface()
object_manager_connection = ObjectManagerTestInterface.new_proxy(
CONNECTION_NAME, OBJECT_MANAGER_PATH)
with object_manager.export_to_dbus(OBJECT_MANAGER_PATH):
self.assertIsInstance(
await object_manager_connection.get_managed_objects(),
dict,
)
with self.assertRaises(DbusUnknownObjectError):
self.assertIsInstance(
await object_manager_connection.get_managed_objects(),
dict,
)
async def test_secondary_export_handle(self) -> None:
await self.bus.request_name_async(CONNECTION_NAME, 0)
object_manager = ObjectManagerTestInterface()
object_manager_connection = ObjectManagerTestInterface.new_proxy(
CONNECTION_NAME, OBJECT_MANAGER_PATH)
object_manager.export_to_dbus(OBJECT_MANAGER_PATH)
managed_object = ManagedInterface()
managed_proxy = ManagedInterface.new_proxy(
CONNECTION_NAME, MANAGED_PATH,
)
async with self.assertDbusSignalEmits(
object_manager_connection.interfaces_added
) as added, self.assertDbusSignalEmits(
object_manager_connection.interfaces_removed
) as removed, object_manager.export_with_manager(
MANAGED_PATH, managed_object,
):
self.assertEqual(
await managed_proxy.test_int,
TEST_NUMBER,
)
self.assertEqual(added.output[0][0], MANAGED_PATH)
removed_path, removed_interfaces = removed.output[0]
self.assertEqual(removed_path, MANAGED_PATH)
self.assertIn(MANAGED_INTERFACE_NAME, removed_interfaces)
with self.assertRaises(DbusUnknownObjectError):
await managed_proxy.test_int
|