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