file.test 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/bin/bash
  2. [ -f testing.sh ] && . testing.sh
  3. #testing "name" "command" "result" "infile" "stdin"
  4. test_line() {
  5. ONE="$1"; shift; TWO="\"$FILES\"/$1 | sed 's/^.*: //'"; shift
  6. testcmd "$ONE" "$TWO" "$@"
  7. }
  8. ln -s "$FILES"/java.class symlink
  9. LINK=$(readlink symlink)
  10. ln -s "$FILES"/java.klass dangler
  11. BROKEN=$(readlink dangler)
  12. mkdir directory
  13. testing "directory" "file ." ".: directory\n" "" ""
  14. rmdir directory
  15. touch empty
  16. testing "empty" "file empty" "empty: empty\n" "" ""
  17. rm -f empty
  18. testing "script" "file input | grep -o ' script'" " script\n" "#!/bin/bash\n" ""
  19. testing "script with spaces" "file input | grep -o ' script'" " script\n" \
  20. "#! /bin/bash\n" ""
  21. testing "env script" "file input | egrep -o '(python|script)' | sort" \
  22. "python\nscript\n" "#! /usr/bin/env python\n" ""
  23. testing "ascii" "file input" "input: ASCII text\n" "Hello, world!\n" ""
  24. testing "utf-8" \
  25. "file \"$FILES\"/utf8/japan.txt | egrep -o '(UTF-8|text)' | LANG=c sort" \
  26. "UTF-8\ntext\n" "" ""
  27. # TODO each of these has multiple options we could test
  28. testing "java class" \
  29. "file \"$FILES\"/java.class | egrep -o '(Java class|version 53.0)'"\
  30. "Java class\nversion 53.0\n" "" ""
  31. echo "cafebabe000000020100000700000003000040000000d9300000000e0100000c8000000200014000000098500000000e" | xxd -r -p > universal
  32. testcmd "mach-o universal" "universal" \
  33. "universal: Mach-O universal binary with 2 architectures: [x86_64] [arm64]\n" "" ""
  34. rm universal
  35. test_line "tar file" "tar/tar.tar" "POSIX tar archive (GNU)\n" "" ""
  36. testing "gzip data" "file \"$FILES\"/tar/tar.tgz | grep -o 'gzip compressed data'" \
  37. "gzip compressed data\n" "" ""
  38. test_line "bzip2 data" "tar/tar.tbz2" \
  39. "bzip2 compressed data, block size = 900k\n" "" ""
  40. test_line "7z file" "tar/tar.7z" "7-zip archive data, version 0.4\n" "" ""
  41. testing "zip file" \
  42. "file $FILES/zip/example.zip | egrep -o '(Zip archive data|at least v1.0 to extract)'" \
  43. "Zip archive data\nat least v1.0 to extract\n" "" ""
  44. echo R0lGODlhIAAgAMZHAAAAABYWFiYmJioqKi4uLjIy | base64 -d > gif
  45. testing "gif file" "file gif" "gif: GIF image data, version 89a, 32 x 32\n" "" ""
  46. rm -f gif
  47. # TODO: check in a genuine minimal .dex
  48. echo "6465780a3033350038ca8f6ce910f94e" | xxd -r -p > android.dex
  49. testing "Android .dex" "file android.dex | egrep -o '(dex file|version 035)'" \
  50. "dex file\nversion 035\n" "" ""
  51. rm -f android.dex
  52. # These actually test a lot of the ELF code: 32-/64-bit, arm/arm64, PT_INTERP,
  53. # the two kinds of NDK ELF note, BuildID, and stripped/not stripped.
  54. toyonly test_line "Android NDK full ELF note" "elf/ndk-elf-note-full" \
  55. "ELF shared object, 64-bit LSB arm64, dynamic (/system/bin/linker64), for Android 24, built by NDK r19b (5304403), BuildID=0c712b8af424d57041b85326f0000fadad38ee0a, not stripped\n" "" ""
  56. toyonly test_line "Android NDK short ELF note" "elf/ndk-elf-note-short" \
  57. "ELF shared object, 32-bit LSB arm, dynamic (/system/bin/linker), for Android 28, BuildID=da6a5f4ca8da163b9339326e626d8a3c, stripped\n" "" ""
  58. toyonly test_line "ELF static fdpic" "elf/fdstatic" \
  59. "ELF executable (fdpic), 32-bit MSB sh, static, stripped\n" "" ""
  60. testing "broken symlink" "file dangler" "dangler: broken symbolic link to $BROKEN\n" "" ""
  61. testing "symlink" "file symlink" "symlink: symbolic link to $LINK\n" "" ""
  62. testing "symlink -h" "file -h symlink" "symlink: symbolic link to $LINK\n" "" ""
  63. testing "symlink -L" \
  64. "file -L symlink | egrep -o '(symlink:|Java class|version 53.0)'" \
  65. "symlink:\nJava class\nversion 53.0\n" "" ""
  66. # Some host versions say "-" some "/dev/stdin"...
  67. testing "- pipe" "cat $FILES/java.class | file - | egrep -o '(Java class|version 53.0)'" \
  68. "Java class\nversion 53.0\n" "" ""
  69. testing "- redirect" \
  70. "file - <$FILES/java.class | egrep -o '(Java class|version 53.0)'" \
  71. "Java class\nversion 53.0\n" "" ""
  72. zero_dev="1/5"
  73. [ "$(uname)" == "Darwin" ] && zero_dev="3/3"
  74. testing "/dev/zero" "file /dev/zero" "/dev/zero: character special ($zero_dev)\n" "" ""
  75. testing "- </dev/zero" "file - </dev/zero | grep -ow data" "data\n" "" ""
  76. testcmd 'ar' 'input | grep -o " ar archive"' ' ar archive\n' \
  77. "$(echo -e '!<arch>\nhello/ 0 0 0 644 6 `\nworld')\n" ""
  78. testcmd 'cpio' 'input' 'input: ASCII cpio archive (SVR4 with no CRC)\n' \
  79. '07070103344745000081A4000003E800' ''
  80. #TODO block fifo socket
  81. #can't stat (unopenable)
  82. #file
  83. # readerror
  84. # elf
  85. # png (grayscale, color RGB, indexed color, grayscale with alpha, color RGBA
  86. # X x X x-bit/{non-}interlaced
  87. # gif87
  88. # jpeg
  89. # xz
  90. # Ogg (buncha types)
  91. # wav audio (buncha types)
  92. # truetype font/collection
  93. # Opentype font
  94. # LLVM IR bitcode
  95. # PEM certificate
  96. # pe32
  97. # BMP
  98. # Linux perf
  99. # Android sparse image file
  100. # Android boot image
  101. # Android DTB/DTBO
  102. # Android Binary XML
  103. # #! shell script