Home | History | Annotate | Download | only in file
      1 #!/bin/sh
      2 ################################################################################
      3 ##                                                                            ##
      4 ## Copyright (c) International Business Machines  Corp., 2001                 ##
      5 ## Copyright (c) 2016 Cyril Hrubis <chrubis (at] suse.cz>                          ##
      6 ##                                                                            ##
      7 ## This program is free software;  you can redistribute it and#or modify      ##
      8 ## it under the terms of the GNU General Public License as published by       ##
      9 ## the Free Software Foundation; either version 2 of the License, or          ##
     10 ## (at your option) any later version.                                        ##
     11 ##                                                                            ##
     12 ## This program is distributed in the hope that it will be useful, but        ##
     13 ## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
     14 ## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
     15 ## for more details.                                                          ##
     16 ##                                                                            ##
     17 ## You should have received a copy of the GNU General Public License          ##
     18 ## along with this program;  if not, write to the Free Software Foundation,   ##
     19 ## Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA           ##
     20 ##                                                                            ##
     21 ################################################################################
     22 #
     23 # This program tests the file command. The tests are aimed at
     24 # testing if the file command can recognize some of the commonly
     25 # used file formats like, tar, tar.gz, rpm, C, ASCII, ELF etc.
     26 #
     27 TST_ID="file01"
     28 TST_CNT=20
     29 TST_SETUP=setup
     30 TST_TESTFUNC=do_test
     31 TST_NEEDS_TMPDIR=1
     32 . tst_test.sh
     33 
     34 setup()
     35 {
     36 	case $(readelf -h /bin/sh) in
     37 	*Data:*"big endian"*) TEST_ARCH=MSB;;
     38 	*Data:*"little endian"*) TEST_ARCH=LSB;;
     39         *) tst_brk TBROK "Failed to determine CPU endianess";;
     40 	esac
     41 }
     42 
     43 file_test()
     44 {
     45 	local fname="$1"
     46 	local fpath
     47 	local i
     48 	shift
     49 
     50 	if ! [ -f "$fname" ]; then
     51 		fpath="$TST_DATAROOT/$fname"
     52 	else
     53 		fpath="$fname"
     54 	fi
     55 
     56 	EXPECT_PASS file "$fpath" \> file.out
     57 
     58 	while [ $# -gt 0 ]; do
     59 		if grep -q "$1" file.out; then
     60 			break
     61 		fi
     62 		shift
     63 	done
     64 
     65 	if [ $# -gt 0 ]; then
     66 		tst_res TPASS "$fname: recognized as $1"
     67 	else
     68 		tst_res TFAIL "$fname: was not recognized"
     69 		cat file.out
     70 	fi
     71 }
     72 
     73 do_test()
     74 {
     75 	case $1 in
     76 	 1) file_test in.txt "ASCII text";;
     77 	 2) file_test in.bash "Bourne-Again shell script";;
     78 	 3) file_test in.sh "POSIX shell script, ASCII text executable" \
     79 			    "POSIX shell script text executable" \
     80 			    "POSIX shell script text" \
     81 			    "Bourne shell script text executable";;
     82 	 4) file_test in.ksh "Korn shell script";;
     83 	 5) file_test in.csh "C shell script";;
     84 	 6) file_test in.c "ASCII C program text" "C source, ASCII text";;
     85 	 7) file_test in.pl "[pP]erl script, ASCII text executable" \
     86 			    "[pP]erl script text executable" \
     87 			    "a /usr/bin/perl script text";;
     88 	 8) file_test in.py "[pP]ython script, ASCII text executable" \
     89 			    "[pP]ython script text executable";;
     90 	 9) file_test in.m4 "M4 macro processor script, ASCII text" \
     91 			    "ASCII M4 macro language pre-processor text";;
     92 	10) file_test in "ELF .*-bit $TEST_ARCH executable, .*";;
     93 	11) file_test in.ar "current ar archive";;
     94 	12) file_test in.tar "tar archive";;
     95     	13) file_test in.tar.gz "gzip compressed data, .*";;
     96     	14) file_test in.tar.bz2 "bzip2 compressed data, .*";;
     97 	15) file_test in.src.rpm "RPM v3 src" "RPM v3.0 src";;
     98 	16) file_test in.jpg "JPEG image data";;
     99 	17) file_test in.png "PNG image data";;
    100 	18) file_test in.wav "RIFF (little-endian) data, WAVE audio, Microsoft PCM";;
    101 	19) file_test in.mp3 "MPEG ADTS, layer III";;
    102 	20) file_test in.zip "Zip archive data";;
    103 	esac
    104 }
    105 
    106 tst_run
    107