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