1 import ./make-test-python.nix ({ pkgs, ... }: {
3 meta = with pkgs.lib.maintainers; {
4 maintainers = [ tnias ];
12 IPCAllowedUsers = [ "alice" "root" ];
14 # As virtual USB devices get attached to the "QEMU USB Hub" we need to
15 # allow Hubs. Otherwise we would have to explicitly allow them too.
17 allow with-interface equals { 09:00:00 }
20 imports = [ ./common/user-account.nix ];
24 # create a blank disk image for our fake USB stick
25 with open(machine.state_dir / "usbstick.img", "wb") as stick:
26 stick.write(b"\x00" * (1024 * 1024))
28 # wait for machine to have started and the usbguard service to be up
29 machine.wait_for_unit("usbguard.service")
31 with subtest("IPC access control"):
32 # User "alice" is allowed to access the IPC interface
33 machine.succeed("su alice -c 'usbguard list-devices'")
35 # User "bob" is not allowed to access the IPC interface
36 machine.fail("su bob -c 'usbguard list-devices'")
38 with subtest("check basic functionality"):
39 # at this point we expect that no USB HDD is connected
40 machine.fail("usbguard list-devices | grep -E 'QEMU USB HARDDRIVE'")
43 machine.send_monitor_command(
44 f"drive_add 0 id=stick,if=none,file={stick.name},format=raw"
46 machine.send_monitor_command("device_add usb-storage,id=stick,drive=stick")
48 # the attached USB HDD should show up after a short while
49 machine.wait_until_succeeds("usbguard list-devices | grep -E 'QEMU USB HARDDRIVE'")
51 # at this point there should be a **blocked** USB HDD
52 machine.succeed("usbguard list-devices | grep -E 'block.*QEMU USB HARDDRIVE'")
53 machine.fail("usbguard list-devices | grep -E ' allow .*QEMU USB HARDDRIVE'")
55 # allow storage devices
56 machine.succeed("usbguard allow-device 'with-interface { 08:*:* }'")
58 # at this point there should be an **allowed** USB HDD
59 machine.succeed("usbguard list-devices | grep -E ' allow .*QEMU USB HARDDRIVE'")
60 machine.fail("usbguard list-devices | grep -E ' block .*QEMU USB HARDDRIVE'")