| 1 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
# |
| 3 |
# This file is formatted with Python Black |
| 4 |
|
| 5 |
import tests.xdp_utils as xdp |
| 6 |
|
| 7 |
import pytest |
| 8 |
import os |
| 9 |
import gi |
| 10 |
import subprocess |
| 11 |
import re |
| 12 |
from typing import assert_never |
| 13 |
|
| 14 |
gi.require_version("UMockdev", "1.0") |
| 15 |
from gi.repository import GLib, UMockdev # noqa E402 |
| 16 |
|
| 17 |
|
| 18 |
@pytest.fixture |
| 19 |
def required_templates(): |
| 20 |
return {"usb": {}} |
| 21 |
|
| 22 |
|
| 23 |
@pytest.fixture |
| 24 |
def usb_queries() -> str | None: |
| 25 |
return None |
| 26 |
|
| 27 |
|
| 28 |
# Supported on host and flatpak |
| 29 |
# Host apps get access to all usb devices. The query has no effect. |
| 30 |
@pytest.fixture(params=["AppInfoHost", "AppInfoFlatpak"]) |
| 31 |
def xdp_app_info(request, usb_queries) -> xdp.AppInfo: |
| 32 |
app_info_kind = request.param |
| 33 |
|
| 34 |
if app_info_kind == "AppInfoHost": |
| 35 |
return xdp.AppInfoHost() |
| 36 |
|
| 37 |
if app_info_kind == "AppInfoFlatpak": |
| 38 |
return xdp.AppInfoFlatpak(usb_queries=usb_queries) |
| 39 |
|
| 40 |
assert_never(app_info_kind) |
| 41 |
|
| 42 |
|
| 43 |
@pytest.fixture |
| 44 |
def umockdev(): |
| 45 |
return UMockdev.Testbed.new() |
| 46 |
|
| 47 |
|
| 48 |
def umockdev_has_working_remove(): |
| 49 |
# umockdev only generates remove events since version 0.18.4 |
| 50 |
# https://github.com/martinpitt/umockdev/releases/tag/0.18.4 |
| 51 |
required = (0, 18, 4) |
| 52 |
|
| 53 |
result = subprocess.run(["umockdev-run", "--version"], stdout=subprocess.PIPE) |
| 54 |
if result.returncode != 0: |
| 55 |
return False |
| 56 |
match = re.match(r"^(\d+)\.(\d+)\.(\d+)", result.stdout.decode("UTF-8").strip()) |
| 57 |
if not match: |
| 58 |
return False |
| 59 |
version = tuple(map(int, match.groups())) |
| 60 |
return version >= required |
| 61 |
|
| 62 |
|
| 63 |
class TestUsb: |
| 64 |
_num_devices = 0 |
| 65 |
|
| 66 |
def generate_device( |
| 67 |
self, testbed, vendor, vendor_name, product, product_name, serial |
| 68 |
): |
| 69 |
n = self._num_devices |
| 70 |
self._num_devices += 1 |
| 71 |
|
| 72 |
testbed.add_from_string(f"""P: /devices/usb{n} |
| 73 |
N: bus/usb/001/{n:03d} |
| 74 |
E: BUSNUM=001 |
| 75 |
E: DEVNUM={n:03d} |
| 76 |
E: DEVNAME=/dev/bus/usb/001/{n:03d} |
| 77 |
E: DEVTYPE=usb_device |
| 78 |
E: DRIVER=usb |
| 79 |
E: ID_BUS=usb |
| 80 |
E: ID_MODEL={product_name} |
| 81 |
E: ID_MODEL_ID={product} |
| 82 |
E: ID_REVISION=0002 |
| 83 |
E: ID_SERIAL={vendor_name}_{product_name}_{serial} |
| 84 |
E: ID_SERIAL_SHORT={serial} |
| 85 |
E: ID_VENDOR={vendor_name} |
| 86 |
E: ID_VENDOR_ID={vendor} |
| 87 |
E: SUBSYSTEM=usb |
| 88 |
A: idProduct={product} |
| 89 |
A: idVendor={vendor} |
| 90 |
""") |
| 91 |
|
| 92 |
return f"/sys/devices/usb{n}" |
| 93 |
|
| 94 |
def test_version(self, portals, dbus_con): |
| 95 |
xdp.check_version(dbus_con, "Usb", 1) |
| 96 |
|
| 97 |
def test_create_close_session(self, portals, dbus_con): |
| 98 |
usb_intf = xdp.get_portal_iface(dbus_con, "Usb") |
| 99 |
|
| 100 |
session = xdp.Session( |
| 101 |
dbus_con, |
| 102 |
usb_intf.CreateSession({"session_handle_token": "session_token0"}), |
| 103 |
) |
| 104 |
session.close() |
| 105 |
|
| 106 |
def test_empty_initial_devices(self, portals, dbus_con): |
| 107 |
usb_intf = xdp.get_portal_iface(dbus_con, "Usb") |
| 108 |
|
| 109 |
xdp.Session( |
| 110 |
dbus_con, |
| 111 |
usb_intf.CreateSession({"session_handle_token": "session_token0"}), |
| 112 |
) |
| 113 |
|
| 114 |
device_events_signal_received = False |
| 115 |
|
| 116 |
def cb_device_events(session_handle, events): |
| 117 |
nonlocal device_events_signal_received |
| 118 |
device_events_signal_received = True |
| 119 |
|
| 120 |
usb_intf.connect_to_signal("DeviceEvents", cb_device_events) |
| 121 |
xdp.wait(300) |
| 122 |
assert not device_events_signal_received |
| 123 |
|
| 124 |
@pytest.mark.parametrize("usb_queries", ["vnd:04a9", None]) |
| 125 |
def test_initial_devices( |
| 126 |
self, portals, dbus_con, xdp_app_info, usb_queries, umockdev |
| 127 |
): |
| 128 |
usb_intf = xdp.get_portal_iface(dbus_con, "Usb") |
| 129 |
|
| 130 |
self.generate_device( |
| 131 |
umockdev, |
| 132 |
"04a9", |
| 133 |
"Canon_Inc.", |
| 134 |
"31c0", |
| 135 |
"Canon_Digital_Camera", |
| 136 |
"C767F1C714174C309255F70E4A7B2EE2", |
| 137 |
) |
| 138 |
|
| 139 |
# TODO: to make this more robust, we should find a way to wait for |
| 140 |
# the portal to pick up the device |
| 141 |
xdp.wait(300) |
| 142 |
|
| 143 |
session = xdp.Session( |
| 144 |
dbus_con, |
| 145 |
usb_intf.CreateSession({"session_handle_token": "session_token0"}), |
| 146 |
) |
| 147 |
|
| 148 |
device_events_signal_received = False |
| 149 |
devices_received = 0 |
| 150 |
|
| 151 |
def cb_device_events(session_handle, events): |
| 152 |
nonlocal device_events_signal_received |
| 153 |
nonlocal devices_received |
| 154 |
assert session.handle == session_handle |
| 155 |
|
| 156 |
for action, id, device in events: |
| 157 |
assert action == "add" |
| 158 |
devices_received += 1 |
| 159 |
|
| 160 |
device_events_signal_received = True |
| 161 |
|
| 162 |
usb_intf.connect_to_signal("DeviceEvents", cb_device_events) |
| 163 |
|
| 164 |
# If the app is not running on the host, make sure an empty usb_query |
| 165 |
# results in no devices being available |
| 166 |
if usb_queries is None and not isinstance(xdp_app_info, xdp.AppInfoHost): |
| 167 |
xdp.wait(300) |
| 168 |
assert not device_events_signal_received |
| 169 |
assert devices_received == 0 |
| 170 |
else: |
| 171 |
xdp.wait_for(lambda: device_events_signal_received) |
| 172 |
assert devices_received == 1 |
| 173 |
|
| 174 |
@pytest.mark.parametrize("usb_queries", ["vnd:04a9", None]) |
| 175 |
def test_device_add(self, portals, xdp_app_info, dbus_con, usb_queries, umockdev): |
| 176 |
usb_intf = xdp.get_portal_iface(dbus_con, "Usb") |
| 177 |
|
| 178 |
session = xdp.Session( |
| 179 |
dbus_con, |
| 180 |
usb_intf.CreateSession({"session_handle_token": "session_token0"}), |
| 181 |
) |
| 182 |
|
| 183 |
device_events_signal_received = False |
| 184 |
devices_received = 0 |
| 185 |
device = None |
| 186 |
|
| 187 |
def cb_device_events(session_handle, events): |
| 188 |
nonlocal device_events_signal_received |
| 189 |
nonlocal devices_received |
| 190 |
nonlocal device |
| 191 |
assert session.handle == session_handle |
| 192 |
|
| 193 |
for action, _, dev in events: |
| 194 |
assert action == "add" |
| 195 |
device = dev |
| 196 |
devices_received += 1 |
| 197 |
|
| 198 |
device_events_signal_received = True |
| 199 |
|
| 200 |
usb_intf.connect_to_signal("DeviceEvents", cb_device_events) |
| 201 |
xdp.wait(300) |
| 202 |
assert not device_events_signal_received |
| 203 |
|
| 204 |
self.generate_device( |
| 205 |
umockdev, |
| 206 |
"04a9", |
| 207 |
"Canon_Inc.", |
| 208 |
"31c0", |
| 209 |
"Canon_Digital_Camera", |
| 210 |
"C767F1C714174C309255F70E4A7B2EE2", |
| 211 |
) |
| 212 |
|
| 213 |
if usb_queries is None and not isinstance(xdp_app_info, xdp.AppInfoHost): |
| 214 |
xdp.wait(300) |
| 215 |
assert not device_events_signal_received |
| 216 |
assert devices_received == 0 |
| 217 |
else: |
| 218 |
xdp.wait_for(lambda: device_events_signal_received) |
| 219 |
assert devices_received == 1 |
| 220 |
|
| 221 |
assert device |
| 222 |
assert device["readable"] |
| 223 |
assert device["writable"] |
| 224 |
assert device["device-file"] == "/dev/bus/usb/001/000" |
| 225 |
assert device["properties"]["ID_VENDOR_ID"] == "04a9" |
| 226 |
assert device["properties"]["ID_MODEL_ID"] == "31c0" |
| 227 |
assert ( |
| 228 |
device["properties"]["ID_SERIAL"] |
| 229 |
== "Canon_Inc._Canon_Digital_Camera_C767F1C714174C309255F70E4A7B2EE2" |
| 230 |
) |
| 231 |
|
| 232 |
@pytest.mark.parametrize("usb_queries", ["vnd:04a9", None]) |
| 233 |
@pytest.mark.skipif( |
| 234 |
not umockdev_has_working_remove(), reason="UMockdev version 0.18.4 required" |
| 235 |
) |
| 236 |
def test_device_remove( |
| 237 |
self, portals, dbus_con, xdp_app_info, usb_queries, umockdev |
| 238 |
): |
| 239 |
usb_intf = xdp.get_portal_iface(dbus_con, "Usb") |
| 240 |
|
| 241 |
dev_path = self.generate_device( |
| 242 |
umockdev, |
| 243 |
"04a9", |
| 244 |
"Canon_Inc.", |
| 245 |
"31c0", |
| 246 |
"Canon_Digital_Camera", |
| 247 |
"C767F1C714174C309255F70E4A7B2EE2", |
| 248 |
) |
| 249 |
|
| 250 |
session = xdp.Session( |
| 251 |
dbus_con, |
| 252 |
usb_intf.CreateSession({"session_handle_token": "session_token0"}), |
| 253 |
) |
| 254 |
|
| 255 |
device_events_signal_count = 0 |
| 256 |
devices_received = 0 |
| 257 |
devices_removed = 0 |
| 258 |
|
| 259 |
def cb_device_events(session_handle, events): |
| 260 |
nonlocal device_events_signal_count |
| 261 |
nonlocal devices_received |
| 262 |
nonlocal devices_removed |
| 263 |
|
| 264 |
assert session.handle == session_handle |
| 265 |
|
| 266 |
for action, id, device in events: |
| 267 |
if action == "add": |
| 268 |
devices_received += 1 |
| 269 |
elif action == "remove": |
| 270 |
devices_removed += 1 |
| 271 |
else: |
| 272 |
assert False |
| 273 |
|
| 274 |
device_events_signal_count += 1 |
| 275 |
|
| 276 |
usb_intf.connect_to_signal("DeviceEvents", cb_device_events) |
| 277 |
|
| 278 |
if usb_queries is None and not isinstance(xdp_app_info, xdp.AppInfoHost): |
| 279 |
xdp.wait(300) |
| 280 |
assert device_events_signal_count == 0 |
| 281 |
assert devices_received == 0 |
| 282 |
assert devices_removed == 0 |
| 283 |
else: |
| 284 |
xdp.wait_for(lambda: device_events_signal_count == 1) |
| 285 |
assert devices_received == 1 |
| 286 |
assert devices_removed == 0 |
| 287 |
|
| 288 |
umockdev.remove_device(dev_path) |
| 289 |
|
| 290 |
if usb_queries is None and not isinstance(xdp_app_info, xdp.AppInfoHost): |
| 291 |
xdp.wait(300) |
| 292 |
assert device_events_signal_count == 0 |
| 293 |
assert devices_received == 0 |
| 294 |
assert devices_removed == 0 |
| 295 |
else: |
| 296 |
xdp.wait_for(lambda: device_events_signal_count == 2) |
| 297 |
assert devices_received == 1 |
| 298 |
assert devices_removed == 1 |
| 299 |
|
| 300 |
@pytest.mark.parametrize("usb_queries", ["vnd:04a9;vnd:04aa"]) |
| 301 |
@pytest.mark.parametrize( |
| 302 |
"template_params", [{"usb": {"filters": {"vendor": "04a9"}}}] |
| 303 |
) |
| 304 |
def test_acquire(self, portals, dbus_con, umockdev): |
| 305 |
usb_intf = xdp.get_portal_iface(dbus_con, "Usb") |
| 306 |
|
| 307 |
self.generate_device( |
| 308 |
umockdev, |
| 309 |
"04a9", |
| 310 |
"Canon_Inc.", |
| 311 |
"31c0", |
| 312 |
"Canon_Digital_Camera", |
| 313 |
"C767F1C714174C309255F70E4A7B2EE2", |
| 314 |
) |
| 315 |
|
| 316 |
self.generate_device( |
| 317 |
umockdev, |
| 318 |
"04aa", |
| 319 |
"Someone Else.", |
| 320 |
"31c0", |
| 321 |
"SomeProduct", |
| 322 |
"00001", |
| 323 |
) |
| 324 |
|
| 325 |
possible_vendors = ["04a9", "04aa"] |
| 326 |
|
| 327 |
devices = usb_intf.EnumerateDevices({}) |
| 328 |
assert len(devices) == 2 |
| 329 |
(id1, dev_info1) = devices[0] |
| 330 |
assert id1 |
| 331 |
assert dev_info1 |
| 332 |
vendor_id = dev_info1["properties"]["ID_VENDOR_ID"] |
| 333 |
assert vendor_id in possible_vendors |
| 334 |
possible_vendors.remove(vendor_id) |
| 335 |
(id2, dev_info2) = devices[1] |
| 336 |
assert id2 |
| 337 |
assert dev_info2 |
| 338 |
vendor_id = dev_info2["properties"]["ID_VENDOR_ID"] |
| 339 |
assert vendor_id in possible_vendors |
| 340 |
possible_vendors.remove(vendor_id) |
| 341 |
|
| 342 |
request = xdp.Request(dbus_con, usb_intf) |
| 343 |
response = request.call( |
| 344 |
"AcquireDevices", |
| 345 |
parent_window="", |
| 346 |
devices=[ |
| 347 |
(id1, {"writable": True}), |
| 348 |
(id2, {"writable": True}), |
| 349 |
], |
| 350 |
options={}, |
| 351 |
) |
| 352 |
assert response |
| 353 |
assert response.response == 0 |
| 354 |
|
| 355 |
(results, finished) = usb_intf.FinishAcquireDevices(request.handle, {}) |
| 356 |
assert finished |
| 357 |
assert len(results) == 1 |
| 358 |
(res_id, device) = results[0] |
| 359 |
assert res_id == id1 or res_id == id2 |
| 360 |
assert device["success"] |
| 361 |
fd = device["fd"].take() |
| 362 |
assert fd > 0 |
| 363 |
with os.fdopen(fd, "r") as f: |
| 364 |
assert f |
| 365 |
assert "error" not in device |
| 366 |
|
| 367 |
usb_intf.ReleaseDevices([res_id], {}) |
| 368 |
|
| 369 |
@pytest.mark.parametrize("usb_queries", ["vnd:0001"]) |
| 370 |
@pytest.mark.parametrize( |
| 371 |
"expected,template_params", |
| 372 |
[ |
| 373 |
(1, {"usb": {"filters": {"model": "0000"}}}), |
| 374 |
(1, {"usb": {"filters": {"model": "0001"}}}), |
| 375 |
(0, {"usb": {"filters": {"model": "0002"}}}), |
| 376 |
(2, {"usb": {"filters": {"vendor": "0001"}}}), |
| 377 |
(0, {"usb": {"filters": {"vendor": "0002"}}}), |
| 378 |
(1, {"usb": {"filters": {"vendor": "0001", "model": "0000"}}}), |
| 379 |
(0, {"usb": {"filters": {"vendor": "0002", "model": "0000"}}}), |
| 380 |
], |
| 381 |
) |
| 382 |
def test_queries(self, portals, dbus_con, expected, usb_queries, umockdev): |
| 383 |
usb_intf = xdp.get_portal_iface(dbus_con, "Usb") |
| 384 |
|
| 385 |
for i in range(2): |
| 386 |
self.generate_device( |
| 387 |
umockdev, |
| 388 |
"0001", |
| 389 |
"example_org", |
| 390 |
f"000{i}", |
| 391 |
f"model{i}", |
| 392 |
"0001", |
| 393 |
) |
| 394 |
|
| 395 |
devices = usb_intf.EnumerateDevices({}) |
| 396 |
assert len(devices) == 2 |
| 397 |
acquire_devices = [(id, {"writable": True}) for (id, _) in devices] |
| 398 |
|
| 399 |
request = xdp.Request(dbus_con, usb_intf) |
| 400 |
response = request.call( |
| 401 |
"AcquireDevices", |
| 402 |
parent_window="", |
| 403 |
devices=acquire_devices, |
| 404 |
options={}, |
| 405 |
) |
| 406 |
assert response |
| 407 |
assert response.response == 0 |
| 408 |
|
| 409 |
(results, finished) = usb_intf.FinishAcquireDevices(request.handle, {}) |
| 410 |
assert finished |
| 411 |
assert len(results) == expected |