1#!/bin/sh 2################################################################################ 3## ## 4## Copyright (c) International Business Machines Corp., 2001 ## 5## Copyright (c) 2016 Cyril Hrubis <chrubis@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# 27TST_CNT=20 28TST_SETUP=setup 29TST_TESTFUNC=do_test 30TST_NEEDS_TMPDIR=1 31TST_NEEDS_CMDS="readelf" 32 33. tst_test.sh 34 35setup() 36{ 37 case $(readelf -h /bin/sh) in 38 *Data:*"big endian"*) TEST_ARCH=MSB;; 39 *Data:*"little endian"*) TEST_ARCH=LSB;; 40 *) tst_brk TBROK "Failed to determine CPU endianess";; 41 esac 42} 43 44file_test() 45{ 46 local fname="$1" 47 local fpath 48 local i 49 shift 50 51 if ! [ -f "$fname" ]; then 52 fpath="$TST_DATAROOT/$fname" 53 else 54 fpath="$fname" 55 fi 56 57 EXPECT_PASS file "$fpath" \> file.out 58 59 while [ $# -gt 0 ]; do 60 if grep -q "$1" file.out; then 61 break 62 fi 63 shift 64 done 65 66 if [ $# -gt 0 ]; then 67 tst_res TPASS "$fname: recognized as $1" 68 else 69 tst_res TFAIL "$fname: was not recognized" 70 cat file.out 71 fi 72} 73 74do_test() 75{ 76 case $1 in 77 1) file_test in.txt "ASCII text";; 78 2) file_test in.bash "Bourne-Again shell script";; 79 3) file_test in.sh "POSIX shell script, ASCII text executable" \ 80 "POSIX shell script text executable" \ 81 "POSIX shell script text" \ 82 "Bourne shell script text executable";; 83 4) file_test in.ksh "Korn shell script";; 84 5) file_test in.csh "C shell script";; 85 6) file_test in.c "ASCII C program text" "C source, ASCII text";; 86 7) file_test in.pl "[pP]erl script, ASCII text executable" \ 87 "[pP]erl script text executable" \ 88 "a /usr/bin/perl script text";; 89 8) file_test in.py "[pP]ython3\{0,1\} script, ASCII text executable" \ 90 "[pP]ython3\{0,1\} script text executable";; 91 9) file_test in.m4 "M4 macro processor script, ASCII text" \ 92 "ASCII M4 macro language pre-processor text";; 93 10) file_test in "ELF .*-bit $TEST_ARCH executable, .*" \ 94 "ELF .*-bit $TEST_ARCH shared object, .*";; 95 11) file_test in.ar "current ar archive";; 96 12) file_test in.tar "tar archive";; 97 13) file_test in.tar.gz "gzip compressed data, .*";; 98 14) file_test in.tar.bz2 "bzip2 compressed data, .*";; 99 15) file_test in.src.rpm "RPM v3 src" "RPM v3.0 src";; 100 16) file_test in.jpg "JPEG image data";; 101 17) file_test in.png "PNG image data";; 102 18) file_test in.wav "RIFF (little-endian) data, WAVE audio, Microsoft PCM";; 103 19) file_test in.mp3 "MPEG ADTS, layer III";; 104 20) file_test in.zip "Zip archive data";; 105 esac 106} 107 108tst_run 109