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