test_file.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import os
  2. import infra.basetest
  3. class TestFile(infra.basetest.BRTest):
  4. config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
  5. f"""
  6. BR2_PACKAGE_FILE=y
  7. BR2_ROOTFS_OVERLAY="{infra.filepath("tests/package/test_file/rootfs-overlay")}"
  8. BR2_TARGET_ROOTFS_CPIO=y
  9. # BR2_TARGET_ROOTFS_TAR is not set
  10. """
  11. def test_run(self):
  12. cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
  13. self.emulator.boot(arch="armv5",
  14. kernel="builtin",
  15. options=["-initrd", cpio_file])
  16. self.emulator.login()
  17. self.assertRunOk("file --version")
  18. tests = [
  19. ("", "plain-text.txt", "ASCII text"),
  20. ("-i", "plain-text.txt", "text/plain"),
  21. ("", "plain-text.txt.gz", "gzip compressed data"),
  22. ("-i", "plain-text.txt.gz", "application/gzip"),
  23. ("-z", "plain-text.txt.gz", "ASCII text"),
  24. ("", "random-data.bin", "data"),
  25. ("-i", "random-data.bin", "application/octet-stream"),
  26. ("", "code.c", "C source"),
  27. ("-i", "code.c", "text/x-c"),
  28. ("", "script.sh", "POSIX shell script"),
  29. ("-i", "script.sh", "text/x-shellscript"),
  30. ("", "script.py", "Python script"),
  31. ("", "/usr/share/misc/magic.mgc", "magic binary file for file"),
  32. ("", "/usr/bin/file", "ELF"),
  33. ("", "/dev/zero", "character special"),
  34. ("", "/", "directory"),
  35. ("-h", "symlink-to-plain-text.txt", "symbolic link"),
  36. ("-L", "symlink-to-plain-text.txt", "ASCII text")
  37. ]
  38. for opt_str, path, pattern in tests:
  39. cmd = f"file {opt_str} '{path}'"
  40. out, ret = self.emulator.run(cmd)
  41. self.assertEqual(ret, 0, f"Failed to run '{cmd}'")
  42. self.assertIn(pattern, "\n".join(out))