xref: /aosp_15_r20/external/toybox/tests/file.test (revision cf5a6c84e2b8763fc1a7db14496fd4742913b199)
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 | 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\ntext\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" "universal" \
41  "universal: Mach-O universal binary with 2 architectures: [x86_64] [arm64]\n" "" ""
42rm universal
43
44test_line "tar file" "tar/tar.tar" "POSIX tar archive (GNU)\n" "" ""
45testing "gzip data" "file \"$FILES\"/tar/tar.tgz | grep -o 'gzip compressed data'" \
46    "gzip compressed data\n" "" ""
47test_line "bzip2 data" "tar/tar.tbz2" \
48  "bzip2 compressed data, block size = 900k\n" "" ""
49test_line "7z file" "tar/tar.7z" "7-zip archive data, version 0.4\n" "" ""
50testing "zip file" \
51  "file $FILES/zip/example.zip | egrep -o '(Zip archive data|at least v1.0 to extract)'" \
52    "Zip archive data\nat least v1.0 to extract\n" "" ""
53
54echo R0lGODlhIAAgAMZHAAAAABYWFiYmJioqKi4uLjIy | base64 -d > gif
55testing "gif file" "file gif" "gif: GIF image data, version 89a, 32 x 32\n" "" ""
56rm -f gif
57
58# TODO: check in a genuine minimal .dex
59
60echo "6465780a3033350038ca8f6ce910f94e" | xxd -r -p > android.dex
61testing "Android .dex" "file android.dex | egrep -o '(dex file|version 035)'" \
62  "dex file\nversion 035\n" "" ""
63rm -f android.dex
64
65# These actually test a lot of the ELF code: 32-/64-bit, arm/arm64, PT_INTERP,
66# the two kinds of NDK ELF note, BuildID, and stripped/not stripped.
67toyonly test_line "Android NDK full ELF note" "elf/ndk-elf-note-full" \
68    "ELF shared object, 64-bit LSB arm64, dynamic (/system/bin/linker64), for Android 24, built by NDK r19b (5304403), BuildID=0c712b8af424d57041b85326f0000fadad38ee0a, not stripped\n" "" ""
69toyonly test_line "Android NDK short ELF note" "elf/ndk-elf-note-short" \
70    "ELF shared object, 32-bit LSB arm, EABI5, soft float, dynamic (/system/bin/linker), for Android 28, BuildID=da6a5f4ca8da163b9339326e626d8a3c, stripped\n" "" ""
71toyonly test_line "ELF static fdpic" "elf/fdstatic" \
72    "ELF executable (fdpic), 32-bit MSB sh, static, stripped\n" "" ""
73echo -ne '\x7fELF\00000000000000000000000000000000000000000000' > bad-bits
74testing "ELF bad bits" "file bad-bits" "bad-bits: ELF (bad type 12336), (bad class -1) (bad endian 48) unknown arch 12336\n" "" ""
75rm -f bad-bits
76
77testing "broken symlink" "file dangler" "dangler: broken symbolic link to $BROKEN\n" "" ""
78testing "symlink" "file symlink" "symlink: symbolic link to $LINK\n" "" ""
79testing "symlink -h" "file -h symlink" "symlink: symbolic link to $LINK\n" "" ""
80testing "symlink -L" \
81  "file -L symlink | egrep -o '(symlink:|Java class|version 53.0)'" \
82  "symlink:\nJava class\nversion 53.0\n" "" ""
83
84# Some host versions say "-" some "/dev/stdin"...
85testing "- pipe" "cat $FILES/java.class | file - | egrep -o '(Java class|version 53.0)'" \
86  "Java class\nversion 53.0\n" "" ""
87testing "- redirect" \
88  "file - <$FILES/java.class | egrep -o '(Java class|version 53.0)'" \
89  "Java class\nversion 53.0\n" "" ""
90
91zero_dev="1/5"
92[ "$(uname)" == "Darwin" ] && zero_dev="3/3"
93testing "/dev/zero" "file /dev/zero" "/dev/zero: character special ($zero_dev)\n" "" ""
94testing "- </dev/zero" "file - </dev/zero | grep -ow data" "data\n" "" ""
95
96testcmd 'ar' 'input | grep -o " ar archive"' ' ar archive\n' \
97  "$(echo -e '!<arch>\nhello/          0           0     0     644     6         `\nworld')\n" ""
98testcmd 'cpio' 'input' 'input: ASCII cpio archive (SVR4 with no CRC)\n' \
99  '07070103344745000081A4000003E800' ''
100
101#TODO block fifo socket
102#can't stat (unopenable)
103#file
104#  readerror
105#  elf
106#  png (grayscale, color RGB, indexed color, grayscale with alpha, color RGBA
107#    X x X x-bit/{non-}interlaced
108#  gif87
109#  jpeg
110#  xz
111#  Ogg (buncha types)
112#  wav audio (buncha types)
113#  truetype font/collection
114#  Opentype font
115#  LLVM IR bitcode
116#  PEM certificate
117#  pe32
118#  BMP
119#  Linux perf
120#  Android sparse image file
121#  Android boot image
122#  Android DTB/DTBO
123#  Android Binary XML
124#  #! shell script
125