test_dosfstools.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import os
  2. import subprocess
  3. import infra.basetest
  4. class TestDosFsTools(infra.basetest.BRTest):
  5. # This test needs a Kernel with vfat and NLS support. The vfat
  6. # filesystem also needs character set conversion libraries, since
  7. # its default encoding is CP850.
  8. kern_frag = \
  9. infra.filepath("tests/package/test_dosfstools/linux-vfat.fragment")
  10. config = \
  11. f"""
  12. BR2_aarch64=y
  13. BR2_TOOLCHAIN_EXTERNAL=y
  14. BR2_TARGET_GENERIC_GETTY_PORT="ttyAMA0"
  15. BR2_LINUX_KERNEL=y
  16. BR2_LINUX_KERNEL_CUSTOM_VERSION=y
  17. BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="6.6.47"
  18. BR2_LINUX_KERNEL_USE_CUSTOM_CONFIG=y
  19. BR2_LINUX_KERNEL_CUSTOM_CONFIG_FILE="board/qemu/aarch64-virt/linux.config"
  20. BR2_LINUX_KERNEL_CONFIG_FRAGMENT_FILES="{kern_frag}"
  21. BR2_LINUX_KERNEL_NEEDS_HOST_OPENSSL=y
  22. BR2_PACKAGE_DOSFSTOOLS=y
  23. BR2_PACKAGE_DOSFSTOOLS_FATLABEL=y
  24. BR2_PACKAGE_DOSFSTOOLS_FSCK_FAT=y
  25. BR2_PACKAGE_DOSFSTOOLS_MKFS_FAT=y
  26. BR2_SYSTEM_ENABLE_NLS=y
  27. BR2_TARGET_ROOTFS_CPIO=y
  28. BR2_TARGET_ROOTFS_CPIO_GZIP=y
  29. # BR2_TARGET_ROOTFS_TAR is not set
  30. BR2_TOOLCHAIN_GLIBC_GCONV_LIBS_COPY=y
  31. """
  32. def test_run(self):
  33. # Prepare the disk image.
  34. disk_file = os.path.join(self.builddir, "images", "disk.img")
  35. self.emulator.logfile.write(f"Creating disk image: {disk_file}\n")
  36. self.emulator.logfile.flush()
  37. subprocess.check_call(
  38. ["dd", "if=/dev/zero", f"of={disk_file}", "bs=1M", "count=256"],
  39. stdout=self.emulator.logfile,
  40. stderr=self.emulator.logfile)
  41. # Run the emulator with a blank drive.
  42. img = os.path.join(self.builddir, "images", "rootfs.cpio.gz")
  43. kern = os.path.join(self.builddir, "images", "Image")
  44. bootargs = ["console=ttyAMA0"]
  45. qemu_opts = ["-M", "virt", "-cpu", "cortex-a57", "-m", "256M",
  46. "-initrd", img,
  47. "-drive", f"file={disk_file},if=virtio,format=raw"]
  48. self.emulator.boot(arch="aarch64",
  49. kernel=kern,
  50. kernel_cmdline=bootargs,
  51. options=qemu_opts)
  52. self.emulator.login()
  53. # Variables for this test.
  54. dev = "/dev/vda"
  55. label = "BR_TEST"
  56. mnt_pt = "/tmp/vfat"
  57. data_file = f"{mnt_pt}/data.bin"
  58. # We create the vfat filesystem on our device.
  59. self.assertRunOk(f"mkfs.vfat {dev}")
  60. # We set a label on this filesystem.
  61. self.assertRunOk(f"fatlabel {dev} '{label}'")
  62. # We create a mount point and mount this filesystem.
  63. self.assertRunOk(f"mkdir -p {mnt_pt}")
  64. self.assertRunOk(f"mount {dev} {mnt_pt}")
  65. # We create a file with random data, to use this new
  66. # filesystem a bit.
  67. self.assertRunOk(f"dd if=/dev/urandom of={data_file} bs=1M count=10")
  68. # We compute the sha256 hash and save it for later.
  69. hash_cmd = f"sha256sum {data_file}"
  70. out, ret = self.emulator.run(hash_cmd)
  71. self.assertEqual(ret, 0)
  72. data_sha256 = out[0]
  73. # We unmount the filesystem.
  74. self.assertRunOk(f"umount {mnt_pt}")
  75. # We run a filesystem check. Since we cleanly unmounted the
  76. # filesystem, we are not expecting any repair. This is just to
  77. # test the program works correctly.
  78. self.assertRunOk(f"fsck.vfat -v {dev}")
  79. # We query the label and check it is the one we set at the
  80. # beginning.
  81. out, ret = self.emulator.run(f"fatlabel {dev}")
  82. self.assertEqual(ret, 0)
  83. self.assertEqual(out[0], label)
  84. # We remount our filesystem.
  85. self.assertRunOk(f"mount {dev} {mnt_pt}")
  86. # We should recompute the same sha256 hash as before, on the
  87. # first data file we created at the beginning.
  88. out, ret = self.emulator.run(hash_cmd)
  89. self.assertEqual(ret, 0)
  90. self.assertEqual(out[0], data_sha256)