Beispiel #1
0
def test_device_policy(coresys):
    """Test device cgroup policy."""
    device = Device(
        "ttyACM0",
        Path("/dev/ttyACM0"),
        Path("/sys/bus/usb/001"),
        "tty",
        [],
        {
            "MAJOR": "5",
            "MINOR": "10"
        },
    )

    assert coresys.hardware.policy.get_cgroups_rule(device) == "c 5:10 rwm"

    disk = Device(
        "sda0",
        Path("/dev/sda0"),
        Path("/sys/bus/usb/001"),
        "block",
        [],
        {
            "MAJOR": "5",
            "MINOR": "10"
        },
    )

    assert coresys.hardware.policy.get_cgroups_rule(disk) == "b 5:10 rwm"
Beispiel #2
0
def test_system_partition(coresys):
    """Test if it is a system partition."""
    disk = Device(
        "sda0",
        Path("/dev/sda0"),
        Path("/sys/bus/usb/001"),
        "block",
        [],
        {
            "MAJOR": "5",
            "MINOR": "10"
        },
    )

    assert not coresys.hardware.disk.is_system_partition(disk)

    disk = Device(
        "sda0",
        Path("/dev/sda0"),
        Path("/sys/bus/usb/001"),
        "block",
        [],
        {
            "MAJOR": "5",
            "MINOR": "10",
            "ID_FS_LABEL": "hassos-overlay"
        },
    )

    assert coresys.hardware.disk.is_system_partition(disk)
Beispiel #3
0
def test_allowed_access(coresys):
    """Test if is allow to access for device."""

    disk = Device(
        "sda0",
        Path("/dev/sda0"),
        Path("/sys/bus/usb/001"),
        "block",
        [],
        {
            "MAJOR": "5",
            "MINOR": "10",
            "ID_FS_LABEL": "hassos-overlay"
        },
    )

    assert not coresys.hardware.policy.allowed_for_access(disk)

    device = Device(
        "ttyACM0",
        Path("/dev/ttyACM0"),
        Path("/sys/bus/usb/001"),
        "tty",
        [],
        {
            "MAJOR": "204",
            "MINOR": "10"
        },
    )

    assert coresys.hardware.policy.allowed_for_access(device)
Beispiel #4
0
def test_ui_simple_device_schema_no_filter(coresys):
    """Test with simple schema without filter."""
    for device in (
            Device(
                "ttyACM0",
                Path("/dev/ttyACM0"),
                Path("/sys/bus/usb/002"),
                "tty",
                None,
                [],
                {"ID_VENDOR": "xy"},
                [],
            ),
            Device(
                "ttyUSB0",
                Path("/dev/ttyUSB0"),
                Path("/sys/bus/usb/001"),
                "tty",
                None,
                [Path("/dev/ttyS1"),
                 Path("/dev/serial/by-id/xyx")],
                {"ID_VENDOR": "xy"},
                [],
            ),
            Device(
                "ttyS0",
                Path("/dev/ttyS0"),
                Path("/sys/bus/usb/003"),
                "tty",
                None,
                [],
                {},
                [],
            ),
            Device(
                "video1",
                Path("/dev/video1"),
                Path("/sys/bus/usb/004"),
                "misc",
                None,
                [],
                {"ID_VENDOR": "xy"},
                [],
            ),
    ):
        coresys.hardware.update_device(device)

    data = UiOptions(coresys)({
        "name": "str",
        "password": "******",
        "fires": "bool",
        "alias": "str?",
        "input": "device",
    }, )

    assert sorted(data[-1]["options"]) == sorted(
        ["/dev/serial/by-id/xyx", "/dev/ttyACM0", "/dev/ttyS0", "/dev/video1"])
    assert data[-1]["type"] == "select"
Beispiel #5
0
def test_dynamic_group_alloc_minor(coresys):
    """Test dynamic cgroup generation based on minor."""
    for device in (
            Device(
                "ttyACM0",
                Path("/dev/ttyACM0"),
                Path("/sys/bus/usb/001"),
                "tty",
                [],
                {
                    "MAJOR": "204",
                    "MINOR": "10"
                },
            ),
            Device(
                "ttyUSB0",
                Path("/dev/ttyUSB0"),
                Path("/sys/bus/usb/000"),
                "tty",
                [Path("/dev/ttyS1"),
                 Path("/dev/serial/by-id/xyx")],
                {
                    "MAJOR": "188",
                    "MINOR": "10"
                },
            ),
            Device(
                "ttyS0",
                Path("/dev/ttyS0"),
                Path("/sys/bus/usb/002"),
                "tty",
                [],
                {
                    "MAJOR": "4",
                    "MINOR": "65"
                },
            ),
            Device(
                "video1",
                Path("/dev/video1"),
                Path("/sys/bus/usb/003"),
                "misc",
                [],
                {
                    "MAJOR": "38",
                    "MINOR": "10"
                },
            ),
    ):
        coresys.hardware.update_device(device)

    assert coresys.hardware.policy.get_cgroups_rules(PolicyGroup.UART) == [
        "c 204:* rwm",
        "c 188:* rwm",
        "c 166:* rwm",
        "c 4:65 rwm",
    ]
Beispiel #6
0
def test_device_path_lookup(coresys):
    """Test device lookup."""
    for device in (
            Device(
                "ttyACM0",
                Path("/dev/ttyACM0"),
                Path("/sys/bus/usb/001"),
                "tty",
                None,
                [],
                {"ID_VENDOR": "xy"},
                [],
            ),
            Device(
                "ttyUSB0",
                Path("/dev/ttyUSB0"),
                Path("/sys/bus/usb/000"),
                "tty",
                None,
                [Path("/dev/ttyS1"),
                 Path("/dev/serial/by-id/xyx")],
                {"ID_VENDOR": "xy"},
                [],
            ),
            Device(
                "ttyS0",
                Path("/dev/ttyS0"),
                Path("/sys/bus/usb/002"),
                "tty",
                None,
                [],
                {},
                [],
            ),
            Device(
                "video1",
                Path("/dev/video1"),
                Path("/sys/bus/usb/003"),
                "misc",
                None,
                [],
                {"ID_VENDOR": "xy"},
                [],
            ),
    ):
        coresys.hardware.update_device(device)

    assert coresys.hardware.exists_device_node(Path("/dev/ttyACM0"))
    assert coresys.hardware.exists_device_node(Path("/dev/ttyS1"))
    assert coresys.hardware.exists_device_node(Path("/dev/ttyS0"))
    assert coresys.hardware.exists_device_node(Path("/dev/serial/by-id/xyx"))
    assert coresys.hardware.exists_device_node(Path("/sys/bus/usb/001"))

    assert not coresys.hardware.exists_device_node(Path("/dev/ttyS2"))
    assert not coresys.hardware.exists_device_node(Path("/dev/ttyUSB1"))
Beispiel #7
0
def test_dynamic_group_alloc_major(coresys):
    """Test dynamic cgroup generation based on minor."""
    for device in (
            Device(
                "gpio16",
                Path("/dev/gpio16"),
                Path("/sys/bus/usb/001"),
                "gpio",
                [],
                {
                    "MAJOR": "254",
                    "MINOR": "10"
                },
            ),
            Device(
                "gpiomem",
                Path("/dev/gpiomem"),
                Path("/sys/bus/usb/000"),
                "gpiomem",
                [Path("/dev/ttyS1"),
                 Path("/dev/serial/by-id/xyx")],
                {
                    "MAJOR": "239",
                    "MINOR": "10"
                },
            ),
            Device(
                "ttyS0",
                Path("/dev/ttyS0"),
                Path("/sys/bus/usb/002"),
                "tty",
                [],
                {
                    "MAJOR": "4",
                    "MINOR": "65"
                },
            ),
            Device(
                "video1",
                Path("/dev/video1"),
                Path("/sys/bus/usb/003"),
                "misc",
                [],
                {
                    "MAJOR": "38",
                    "MINOR": "10"
                },
            ),
    ):
        coresys.hardware.update_device(device)

    assert coresys.hardware.policy.get_cgroups_rules(PolicyGroup.GPIO) == [
        "c 254:* rwm",
        "c 239:* rwm",
    ]
Beispiel #8
0
def test_device_filter(coresys):
    """Test device filter."""
    for device in (
            Device(
                "ttyACM0",
                Path("/dev/ttyACM0"),
                Path("/sys/bus/usb/000"),
                "tty",
                None,
                [],
                {"ID_VENDOR": "xy"},
                [],
            ),
            Device(
                "ttyUSB0",
                Path("/dev/ttyUSB0"),
                Path("/sys/bus/usb/001"),
                "tty",
                None,
                [Path("/dev/ttyS1"),
                 Path("/dev/serial/by-id/xyx")],
                {"ID_VENDOR": "xy"},
                [],
            ),
            Device(
                "ttyS0",
                Path("/dev/ttyS0"),
                Path("/sys/bus/usb/002"),
                "tty",
                None,
                [],
                {},
                [],
            ),
            Device(
                "video1",
                Path("/dev/video1"),
                Path("/sys/bus/usb/003"),
                "misc",
                None,
                [],
                {"ID_VENDOR": "xy"},
                [],
            ),
    ):
        coresys.hardware.update_device(device)

    assert sorted(device.path
                  for device in coresys.hardware.filter_devices()) == sorted(
                      device.path for device in coresys.hardware.devices)
    assert sorted(device.path for device in coresys.hardware.filter_devices(
        subsystem=UdevSubsystem.SERIAL)) == sorted(
            device.path for device in coresys.hardware.devices
            if device.subsystem == UdevSubsystem.SERIAL)
Beispiel #9
0
def test_system_partition_disk(coresys: CoreSys):
    """Test if it is a system disk/partition."""
    disk = Device(
        "sda1",
        Path("/dev/sda1"),
        Path("/sys/bus/usb/001"),
        "block",
        None,
        [],
        {
            "MAJOR": "5",
            "MINOR": "10"
        },
        [],
    )

    assert not coresys.hardware.disk.is_used_by_system(disk)

    disk = Device(
        "sda1",
        Path("/dev/sda1"),
        Path("/sys/bus/usb/001"),
        "block",
        None,
        [],
        {
            "MAJOR": "5",
            "MINOR": "10",
            "ID_FS_LABEL": "hassos-overlay"
        },
        [],
    )

    assert coresys.hardware.disk.is_used_by_system(disk)

    coresys.hardware.update_device(disk)
    disk_root = Device(
        "sda",
        Path("/dev/sda"),
        Path("/sys/bus/usb/001"),
        "block",
        None,
        [],
        {
            "MAJOR": "5",
            "MINOR": "0"
        },
        [Path("/dev/sda1")],
    )

    assert coresys.hardware.disk.is_used_by_system(disk_root)
Beispiel #10
0
async def test_api_os_datadisk_list(api_client, coresys: CoreSys):
    """Test datadisk list function."""
    await coresys.dbus.agent.connect()
    await coresys.dbus.agent.update()

    coresys.hardware.update_device(
        Device(
            "sda",
            Path("/dev/sda"),
            Path("/sys/bus/usb/000"),
            "block",
            None,
            [Path("/dev/serial/by-id/test")],
            {
                "ID_NAME": "xy",
                "MINOR": "0",
                "DEVTYPE": "disk"
            },
            [],
        ))
    coresys.hardware.update_device(
        Device(
            "sda1",
            Path("/dev/sda1"),
            Path("/sys/bus/usb/000/1"),
            "block",
            None,
            [Path("/dev/serial/by-id/test1")],
            {
                "ID_NAME": "xy",
                "MINOR": "1",
                "DEVTYPE": "partition"
            },
            [],
        ))

    resp = await api_client.get("/os/datadisk/list")
    result = await resp.json()

    assert result["data"]["devices"] == ["/dev/sda"]
Beispiel #11
0
async def test_datadisk_list(coresys: CoreSys):
    """Test docker info api."""
    await coresys.dbus.agent.connect()
    await coresys.dbus.agent.update()

    coresys.hardware.update_device(
        Device(
            "sda",
            Path("/dev/sda"),
            Path("/sys/bus/usb/000"),
            "block",
            None,
            [Path("/dev/serial/by-id/test")],
            {
                "ID_NAME": "xy",
                "MINOR": "0",
                "DEVTYPE": "disk"
            },
            [],
        ))
    coresys.hardware.update_device(
        Device(
            "sda1",
            Path("/dev/sda1"),
            Path("/sys/bus/usb/000/1"),
            "block",
            None,
            [Path("/dev/serial/by-id/test1")],
            {
                "ID_NAME": "xy",
                "MINOR": "1",
                "DEVTYPE": "partition"
            },
            [],
        ))

    assert coresys.os.datadisk.available_disks == [PosixPath("/dev/sda")]
Beispiel #12
0
def test_have_audio(coresys):
    """Test usb device filter."""
    assert not coresys.hardware.helper.support_audio

    coresys.hardware.update_device(
        Device(
            "sda",
            Path("/dev/sda"),
            Path("/sys/bus/usb/000"),
            "sound",
            [],
            {"ID_NAME": "xy"},
        ))

    assert coresys.hardware.helper.support_audio
Beispiel #13
0
def test_device_in_policy(coresys):
    """Test device cgroup policy."""
    device = Device(
        "ttyACM0",
        Path("/dev/ttyACM0"),
        Path("/sys/bus/usb/001"),
        "tty",
        None,
        [],
        {"MAJOR": "204", "MINOR": "10"},
        [],
    )

    assert coresys.hardware.policy.is_match_cgroup(PolicyGroup.UART, device)
    assert not coresys.hardware.policy.is_match_cgroup(PolicyGroup.GPIO, device)
Beispiel #14
0
def test_device_property(coresys):
    """Test device cgroup policy."""
    device = Device(
        "ttyACM0",
        Path("/dev/ttyACM0"),
        Path("/sys/bus/usb/001"),
        "tty",
        [Path("/dev/serial/by-id/fixed-device")],
        {
            "MAJOR": "5",
            "MINOR": "10"
        },
    )

    assert device.by_id == device.links[0]
    assert device.cgroups_major == 5
    assert device.cgroups_minor == 10
Beispiel #15
0
def test_have_usb(coresys):
    """Test usb device filter."""
    assert not coresys.hardware.helper.support_usb

    coresys.hardware.update_device(
        Device(
            "sda",
            Path("/dev/sda"),
            Path("/sys/bus/usb/000"),
            "usb",
            None,
            [],
            {"ID_NAME": "xy"},
            [],
        ))

    assert coresys.hardware.helper.support_usb
Beispiel #16
0
async def test_api_hardware_info_device(api_client, coresys):
    """Test docker info api."""
    coresys.hardware.update_device(
        Device(
            "sda",
            Path("/dev/sda"),
            Path("/sys/bus/usb/000"),
            "sound",
            [Path("/dev/serial/by-id/test")],
            {"ID_NAME": "xy"},
        ))

    resp = await api_client.get("/hardware/info")
    result = await resp.json()

    assert result["result"] == "ok"
    assert result["data"]["devices"][-1]["name"] == "sda"
    assert result["data"]["devices"][-1]["by_id"] == "/dev/serial/by-id/test"
Beispiel #17
0
def test_simple_device_schema(coresys):
    """Test with simple schema."""
    for device in (
            Device(
                "ttyACM0",
                Path("/dev/ttyACM0"),
                Path("/sys/bus/usb/002"),
                "tty",
                [],
                {"ID_VENDOR": "xy"},
            ),
            Device(
                "ttyUSB0",
                Path("/dev/ttyUSB0"),
                Path("/sys/bus/usb/001"),
                "tty",
                [Path("/dev/ttyS1"),
                 Path("/dev/serial/by-id/xyx")],
                {"ID_VENDOR": "xy"},
            ),
            Device("ttyS0", Path("/dev/ttyS0"), Path("/sys/bus/usb/003"),
                   "tty", [], {}),
            Device(
                "video1",
                Path("/dev/video1"),
                Path("/sys/bus/usb/004"),
                "misc",
                [],
                {"ID_VENDOR": "xy"},
            ),
    ):
        coresys.hardware.update_device(device)

    assert AddonOptions(
        coresys,
        {
            "name": "str",
            "password": "******",
            "input": "device"
        },
    )({
        "name": "Pascal",
        "password": "******",
        "input": "/dev/ttyUSB0"
    })

    data = AddonOptions(
        coresys,
        {
            "name": "str",
            "password": "******",
            "input": "device"
        },
    )({
        "name": "Pascal",
        "password": "******",
        "input": "/dev/serial/by-id/xyx"
    })
    assert data["input"] == "/dev/ttyUSB0"

    assert AddonOptions(
        coresys,
        {
            "name": "str",
            "password": "******",
            "input": "device(subsystem=tty)"
        },
    )({
        "name": "Pascal",
        "password": "******",
        "input": "/dev/ttyACM0"
    })

    with pytest.raises(vol.error.Invalid):
        assert AddonOptions(
            coresys,
            {
                "name": "str",
                "password": "******",
                "input": "device"
            },
        )({
            "name": "Pascal",
            "password": "******",
            "input": "/dev/not_exists"
        })

    with pytest.raises(vol.error.Invalid):
        assert AddonOptions(
            coresys,
            {
                "name": "str",
                "password": "******",
                "input": "device(subsystem=tty)"
            },
        )({
            "name": "Pascal",
            "password": "******",
            "input": "/dev/video1"
        })