toybox/tests/file.test
<<
>>
Prefs
   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, 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" "" ""
  73
  74testing "broken symlink" "file dangler" "dangler: broken symbolic link to $BROKEN\n" "" ""
  75testing "symlink" "file symlink" "symlink: symbolic link to $LINK\n" "" ""
  76testing "symlink -h" "file -h symlink" "symlink: symbolic link to $LINK\n" "" ""
  77testing "symlink -L" \
  78  "file -L symlink | egrep -o '(symlink:|Java class|version 53.0)'" \
  79  "symlink:\nJava class\nversion 53.0\n" "" ""
  80
  81# Some host versions say "-" some "/dev/stdin"...
  82testing "- pipe" "cat $FILES/java.class | file - | egrep -o '(Java class|version 53.0)'" \
  83  "Java class\nversion 53.0\n" "" ""
  84testing "- redirect" \
  85  "file - <$FILES/java.class | egrep -o '(Java class|version 53.0)'" \
  86  "Java class\nversion 53.0\n" "" ""
  87
  88zero_dev="1/5"
  89[ "$(uname)" == "Darwin" ] && zero_dev="3/3"
  90testing "/dev/zero" "file /dev/zero" "/dev/zero: character special ($zero_dev)\n" "" ""
  91testing "- </dev/zero" "file - </dev/zero | grep -ow data" "data\n" "" ""
  92
  93testcmd 'ar' 'input | grep -o " ar archive"' ' ar archive\n' \
  94  "$(echo -e '!<arch>\nhello/          0           0     0     644     6         `\nworld')\n" ""
  95testcmd 'cpio' 'input' 'input: ASCII cpio archive (SVR4 with no CRC)\n' \
  96  '07070103344745000081A4000003E800' ''
  97
  98#TODO block fifo socket
  99#can't stat (unopenable)
 100#file
 101#  readerror
 102#  elf
 103#  png (grayscale, color RGB, indexed color, grayscale with alpha, color RGBA
 104#    X x X x-bit/{non-}interlaced
 105#  gif87
 106#  jpeg
 107#  xz
 108#  Ogg (buncha types)
 109#  wav audio (buncha types)
 110#  truetype font/collection
 111#  Opentype font
 112#  LLVM IR bitcode
 113#  PEM certificate
 114#  pe32
 115#  BMP
 116#  Linux perf
 117#  Android sparse image file
 118#  Android boot image
 119#  Android DTB/DTBO
 120#  Android Binary XML
 121#  #! shell script
 122