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