1import os
2
3import infra.basetest
4
5
6class TestUsbUtils(infra.basetest.BRTest):
7    # A specific configuration is needed for testing usbutils, to
8    # enable USB 2.0 support in the Kernel.
9    linux_fragment = \
10        infra.filepath("tests/package/test_usbutils/linux-usbutils.fragment")
11    config = \
12        f"""
13        BR2_aarch64=y
14        BR2_TOOLCHAIN_EXTERNAL=y
15        BR2_TARGET_GENERIC_GETTY_PORT="ttyAMA0"
16        BR2_LINUX_KERNEL=y
17        BR2_LINUX_KERNEL_CUSTOM_VERSION=y
18        BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="6.1.73"
19        BR2_LINUX_KERNEL_USE_CUSTOM_CONFIG=y
20        BR2_LINUX_KERNEL_CUSTOM_CONFIG_FILE="board/qemu/aarch64-virt/linux.config"
21        BR2_LINUX_KERNEL_CONFIG_FRAGMENT_FILES="{linux_fragment}"
22        BR2_LINUX_KERNEL_NEEDS_HOST_OPENSSL=y
23        BR2_PACKAGE_EUDEV=y
24        BR2_PACKAGE_USBUTILS=y
25        BR2_ROOTFS_DEVICE_CREATION_DYNAMIC_EUDEV=y
26        BR2_TARGET_ROOTFS_CPIO=y
27        BR2_TARGET_ROOTFS_CPIO_GZIP=y
28        # BR2_TARGET_ROOTFS_TAR is not set
29        """
30
31    def test_run(self):
32        img = os.path.join(self.builddir, "images", "rootfs.cpio.gz")
33        kern = os.path.join(self.builddir, "images", "Image")
34        # We add a USB keyboard and mouse devices for the test.
35        self.emulator.boot(arch="aarch64",
36                           kernel=kern,
37                           kernel_cmdline=["console=ttyAMA0"],
38                           options=["-M", "virt", "-cpu", "cortex-a57", "-m", "256M",
39                                    "-initrd", img,
40                                    "-device", "usb-ehci,id=ehci",
41                                    "-device", "usb-kbd,bus=ehci.0",
42                                    "-device", "usb-mouse,bus=ehci.0"])
43        self.emulator.login()
44
45        # Check the program can execute. We also check the version
46        # string to make sure we have the usbutils version. The
47        # BusyBox lsusb ignores arguments.
48        output, exit_code = self.emulator.run("lsusb --version")
49        self.assertEqual(exit_code, 0)
50        self.assertTrue(output[0].startswith("lsusb (usbutils)"))
51
52        # Test few simple and common invocations
53        self.assertRunOk("lsusb")
54        self.assertRunOk("lsusb --tree")
55        self.assertRunOk("lsusb --verbose")
56        # 1d6b:0002 is Linux Foundation 2.0 root hub
57        # it should be present. lsusb return an error if no device
58        # is found.
59        self.assertRunOk("lsusb -d 1d6b:0002")
60        # we emulate a USB keyboard and mouse, so usbhid-dump should find them
61        self.assertRunOk("usbhid-dump")
62