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