test_usbip.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import os
  2. import infra.basetest
  3. class TestUsbIp(infra.basetest.BRTest):
  4. # A specific configuration is needed for testing usbip, to
  5. # enable USB 2.0 and USBIP support in the Kernel.
  6. linux_fragment = \
  7. infra.filepath("tests/package/test_usbip/linux-usbip.fragment")
  8. config = \
  9. f"""
  10. BR2_aarch64=y
  11. BR2_TOOLCHAIN_EXTERNAL=y
  12. BR2_TARGET_GENERIC_GETTY_PORT="ttyAMA0"
  13. BR2_LINUX_KERNEL=y
  14. BR2_LINUX_KERNEL_CUSTOM_VERSION=y
  15. BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="6.12.6"
  16. BR2_LINUX_KERNEL_USE_CUSTOM_CONFIG=y
  17. BR2_LINUX_KERNEL_CUSTOM_CONFIG_FILE="board/qemu/aarch64-virt/linux.config"
  18. BR2_LINUX_KERNEL_CONFIG_FRAGMENT_FILES="{linux_fragment}"
  19. BR2_LINUX_KERNEL_NEEDS_HOST_OPENSSL=y
  20. BR2_PACKAGE_EUDEV=y
  21. BR2_PACKAGE_HWDATA=y
  22. BR2_PACKAGE_HWDATA_USB_IDS=y
  23. BR2_PACKAGE_USBIP=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. def test_run(self):
  31. img = os.path.join(self.builddir, "images", "rootfs.cpio.gz")
  32. kern = os.path.join(self.builddir, "images", "Image")
  33. # We add a USB keyboard and mouse devices for the test.
  34. self.emulator.boot(arch="aarch64",
  35. kernel=kern,
  36. kernel_cmdline=["console=ttyAMA0"],
  37. options=["-M", "virt", "-cpu", "cortex-a57", "-m", "256M",
  38. "-initrd", img,
  39. "-device", "usb-ehci,id=ehci",
  40. "-device", "usb-kbd,bus=ehci.0",
  41. "-device", "usb-mouse,bus=ehci.0"])
  42. self.emulator.login()
  43. # We check the program can execute.
  44. self.assertRunOk("usbipd --version")
  45. # We check "lsusb" sees exactly one QEMU USB Keyboard.
  46. out, ret = self.emulator.run("lsusb")
  47. self.assertEqual(ret, 0)
  48. kbd_count = "\n".join(out).count("QEMU USB Keyboard")
  49. self.assertEqual(kbd_count, 1)
  50. # The daemon is not running yet. Listing remote devices is
  51. # expected to fail.
  52. _, ret = self.emulator.run("usbip list --remote=127.0.0.1")
  53. self.assertNotEqual(ret, 0)
  54. # We start the USBIP daemon.
  55. self.assertRunOk("usbipd -D")
  56. # The daemon is started. Listing remote devices is now
  57. # expected to succeed, but with an empty list (since we did
  58. # not exported any device yet).
  59. out, ret = self.emulator.run("usbip list --remote=127.0.0.1")
  60. self.assertEqual(ret, 0)
  61. self.assertIn("no exportable devices found", "\n".join(out))
  62. # We list the local devices seen by usbip. We check we can see
  63. # our local USB keyboard device in it.
  64. out, ret = self.emulator.run("usbip list --local")
  65. self.assertEqual(ret, 0)
  66. self.assertIn("busid 1-1", "\n".join(out))
  67. # We bind the first device (USB Keyboard)
  68. self.assertRunOk("usbip bind --busid=1-1")
  69. # We list the remote devices. We should see our exported
  70. # keyboard: we check we have the list header, and the device
  71. # ID in the output.
  72. out, ret = self.emulator.run("usbip list --remote=127.0.0.1")
  73. self.assertEqual(ret, 0)
  74. out_str = "\n".join(out)
  75. self.assertNotIn("no exportable devices found", out_str)
  76. self.assertIn("Exportable USB devices", out_str)
  77. self.assertIn("(0627:0001)", out_str)
  78. # We attach the keyboard. This should create a second USB
  79. # keyboard.
  80. self.assertRunOk("usbip attach --remote=127.0.0.1 --busid=1-1")
  81. # We check "lsusb" now sees exactly two QEMU USB Keyboards
  82. # (the original one, and a second one created by usbip).
  83. out, ret = self.emulator.run("lsusb")
  84. self.assertEqual(ret, 0)
  85. kbd_count = "\n".join(out).count("QEMU USB Keyboard")
  86. self.assertEqual(kbd_count, 2)