file01.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0-or-later
  3. # Copyright (c) International Business Machines Corp., 2001
  4. # Copyright (c) 2016 Cyril Hrubis <chrubis@suse.cz>
  5. #
  6. # This program tests the file command. The tests are aimed at
  7. # testing if the file command can recognize some of the commonly
  8. # used file formats like, tar, tar.gz, rpm, C, ASCII, ELF etc.
  9. TST_CNT=20
  10. TST_SETUP=setup
  11. TST_TESTFUNC=do_test
  12. TST_NEEDS_TMPDIR=1
  13. TST_NEEDS_CMDS="readelf"
  14. . tst_test.sh
  15. setup()
  16. {
  17. case $(readelf -h /bin/sh) in
  18. *Data:*"big endian"*) TEST_ARCH=MSB;;
  19. *Data:*"little endian"*) TEST_ARCH=LSB;;
  20. *) tst_brk TBROK "Failed to determine CPU endianess";;
  21. esac
  22. }
  23. file_test()
  24. {
  25. local fname="$1"
  26. local fpath
  27. local i
  28. shift
  29. if ! [ -f "$fname" ]; then
  30. fpath="$TST_DATAROOT/$fname"
  31. else
  32. fpath="$fname"
  33. fi
  34. EXPECT_PASS file "$fpath" \> file.out
  35. while [ $# -gt 0 ]; do
  36. if grep -q "$1" file.out; then
  37. break
  38. fi
  39. shift
  40. done
  41. if [ $# -gt 0 ]; then
  42. tst_res TPASS "$fname: recognized as $1"
  43. else
  44. tst_res TFAIL "$fname: was not recognized"
  45. cat file.out
  46. fi
  47. }
  48. do_test()
  49. {
  50. case $1 in
  51. 1) file_test in.txt "ASCII text";;
  52. 2) file_test in.bash "Bourne-Again shell script";;
  53. 3) file_test in.sh "POSIX shell script, ASCII text executable" \
  54. "POSIX shell script text executable" \
  55. "POSIX shell script text" \
  56. "Bourne shell script text executable";;
  57. 4) file_test in.ksh "Korn shell script";;
  58. 5) file_test in.csh "C shell script";;
  59. 6) file_test in.c "ASCII C program text" "C source, ASCII text";;
  60. 7) file_test in.pl "[pP]erl script, ASCII text executable" \
  61. "[pP]erl script text executable" \
  62. "a /usr/bin/perl script text";;
  63. 8) file_test in.py "[pP]ython3\{0,1\} script, ASCII text executable" \
  64. "[pP]ython3\{0,1\} script text executable";;
  65. 9) file_test in.m4 "M4 macro processor script, ASCII text" \
  66. "ASCII M4 macro language pre-processor text";;
  67. 10) file_test in "ELF .*-bit $TEST_ARCH executable, .*" \
  68. "ELF .*-bit $TEST_ARCH shared object, .*" \
  69. "ELF .*-bit $TEST_ARCH pie executable, .*" \
  70. "ELF .*-bit $TEST_ARCH pie shared object, .*";;
  71. 11) file_test in.ar "current ar archive";;
  72. 12) file_test in.tar "tar archive";;
  73. 13) file_test in.tar.gz "gzip compressed data, .*";;
  74. 14) file_test in.tar.bz2 "bzip2 compressed data, .*";;
  75. 15) file_test in.src.rpm "RPM v3 src" "RPM v3.0 src";;
  76. 16) file_test in.jpg "JPEG image data";;
  77. 17) file_test in.png "PNG image data";;
  78. 18) file_test in.wav "RIFF (little-endian) data, WAVE audio, Microsoft PCM";;
  79. 19) file_test in.mp3 "MPEG ADTS, layer III";;
  80. 20) file_test in.zip "Zip archive data";;
  81. esac
  82. }
  83. tst_run