blob: 14277748d19c42b65f53eb8ce914294ef62df9d7 [file] [log] [blame]
subrata_modakc8649602008-08-27 11:29:55 +00001#!/bin/sh
iyermanojde6f3982002-12-17 16:02:52 +00002################################################################################
3## ##
4## Copyright (c) International Business Machines Corp., 2001 ##
5## ##
6## This program is free software; you can redistribute it and#or modify ##
7## it under the terms of the GNU General Public License as published by ##
8## the Free Software Foundation; either version 2 of the License, or ##
9## (at your option) any later version. ##
10## ##
11## This program is distributed in the hope that it will be useful, but ##
12## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
13## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ##
14## for more details. ##
15## ##
16## You should have received a copy of the GNU General Public License ##
17## along with this program; if not, write to the Free Software ##
18## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ##
19## ##
20################################################################################
21#
22# File: file_test.sh
23#
24# Description: This program tests the file command. The tests are aimed at
25# testing if the file command can recognize some of the commonly
26# used file formats like, tar, tar.gz, rpm, C, ASCII, ELF etc.
27#
28# Author: Manoj Iyer, manjo@mail.utexas.edu
29#
30# History: Dec 16 2002 - Created. - Manoj Iyer manjo@austin.ibm.com.
31# Dec 17 2002 - Added. - GPL header and minor doc changes.
iyermanoj4706bad2002-12-17 16:11:20 +000032# If LTPTMP and LTPBIN are not exported set their
33# values to /tmp and 'pwd' respectively.
iyermanoj2a8f2092002-12-17 16:42:06 +000034# - Added. - exit status, if any test fails the test
35# exits with non-zero value if all tests pass test
36# exits with zero.
iyermanoja06f8782002-12-18 20:21:05 +000037# Dec 18 2002 - Added. - Code to read environment variable
38# LTPROOT and TMPBASE and set LTPTMP and LTPBIN
39# accordingly.
iyermanojfce31412002-12-17 15:37:29 +000040
41
subrata_modak1ae05952009-07-10 10:31:18 +000042#
43# Description of individual test cases
44# ------------------------------------
45#
46# Test01: Test if file command recognizes ASCII text files
47# -------
48# 1) Write text to a known file
49# 2) Use 'file' command to get the type of the known file
50# Ex: file xyz.txt
51# 3) Grep for the keyword "ASCII text" in the output of the
52# 'file' command
53# 4) Declare test as PASS if above step is successful else
54# declare test as FAIL
55#
56# Test02: Test if file command can recognize bash shell script
57# -------
58# 1) Write a small bash shell script to a known file
59# 2) Use 'file' command to get the type of the known file
60# Ex: file xyz.sh
61# 3) Grep for the keyword "Bourne-Again shell script" in
62# the output of the 'file' command
63# 4) Declare test as PASS if above step is successful else
64# declare test as FAIL
65#
66# Test03: Test if file command can recognize bash shell script
67# -------
68# Similar test(as Test02) is performed with Korn shell script
69#
70# Test04: Test if file command can recognize C shell script
71# -------
72# Similar test(as Test02) is performed with C shell script
73#
74# Test05: Test if file command can recognize C program text
75# -------
76# Similar test(as Test02) is performed with C program text
77#
78# Test06: Test if file command can recognize ELF binay executables
79# -------
80# 1) Grep for 'm68k' or 'sparc' or 'mips' or 'mipseb' or 'sh.eb'
81# or 'powerpc' or 'ppc' or 's390' from the output of the command
82# 'uname -m'
83# 2) If the above step is successful, assign string 'MSB' to variable
84# TARGET_ARCH else assign string 'LSB'
85# 3) Write small C program to a known '.c' file
86# 4) Compile it using "cc"
87# Ex: cc xyz xyz.c
88# 5) Use file command to get the type of the object file
89# 6) Grep for the string "ELF .*-bit $TEST_ARCH executable, .*"
90# in the output of the 'file' command
91# 7) If the above command is successful, declare test as PASS
92# else declare test as FAIL
93#
94# Test07: Test if file command can recognize tar files
95# -------
96# 1) Write text to three different files
97# 2) Archive the files using "tar" command
98# Ex: tar -cf ...
99# 3) Use 'file' command to get the type of the archive file
100# Ex: file xyz.tar
101# 4) Grep for the string "tar archive" from the output of
102# the above 'file' command
103# 5) Declare test as PASS, if the above step is successfull else
104# declare test as FAIL
105#
106# Test08: Test if file command can tar zip files
107# -------
108# 1) Write text to three different files
109# 2) Archive the files using "tar" command
110# Ex: tar -cf ...
111# 3) Use 'gzip' command to zip tar files
112# Ex: gzip -f xyz.tar
113# 4) Use 'file' command to get the type of the archive file
114# Ex: file xyz.tar.gz
115# 5) Grep for the string "gzip compressed data, .*" from the above
116# file commnand
117# 6) Declare test as PASS, if the above step is successfull else
118# declare test as FAIL
119#
iyermanojfce31412002-12-17 15:37:29 +0000120
subrata_modak1ae05952009-07-10 10:31:18 +0000121
122export TST_TOTAL=10 # Number of tests in this testcase
123
robbiew87ea61f2003-12-08 16:07:38 +0000124if [ -z "$LTPTMP" -a -z "$TMPBASE" ]
iyermanoj4706bad2002-12-17 16:11:20 +0000125then
iyermanoja06f8782002-12-18 20:21:05 +0000126 LTPTMP=/tmp/
127else
subrata_modak1ae05952009-07-10 10:31:18 +0000128 LTPTMP=$TMPBASE
iyermanoj4706bad2002-12-17 16:11:20 +0000129fi
130
subrata_modak1ae05952009-07-10 10:31:18 +0000131# 'LTPBIN' where actual test cases (test binaries) reside
132# 'LTPROOT' where the actual LTP test suite resides
robbiew87ea61f2003-12-08 16:07:38 +0000133if [ -z "$LTPBIN" -a -z "$LTPROOT" ]
iyermanoj4706bad2002-12-17 16:11:20 +0000134then
135 LTPBIN=./
iyermanoja06f8782002-12-18 20:21:05 +0000136else
137 LTPBIN=$LTPROOT/testcases/bin/
iyermanoj4706bad2002-12-17 16:11:20 +0000138fi
iyermanojfce31412002-12-17 15:37:29 +0000139
140# set return code RC variable to 0, it will be set with a non-zero return code
iyermanoj2a8f2092002-12-17 16:42:06 +0000141# in case of error. Set TFAILCNT to 0, increment if there occures a failure.
iyermanojfce31412002-12-17 15:37:29 +0000142
iyermanoj2a8f2092002-12-17 16:42:06 +0000143TFAILCNT=0
iyermanojfce31412002-12-17 15:37:29 +0000144RC=0
145
146# TEST #1
147# Test if file command recognizes ASCII text files.
148
iyermanojde6f3982002-12-17 16:02:52 +0000149export TCID=file01
subrata_modak1ae05952009-07-10 10:31:18 +0000150export TST_COUNT=1
iyermanojfce31412002-12-17 15:37:29 +0000151
subrata_modak29e13752009-08-23 06:30:42 +0000152$LTPBIN/tst_resm TINFO "TEST #1: file command recogizes ASCII text files"
iyermanojfce31412002-12-17 15:37:29 +0000153
154cat > $LTPTMP/test_file.txt <<EOF
155This is a text file
156to test file command.
157EOF
158
159# Execute file command & check for output.
160# Expected out put is the string "ASCII English text"
161
subrata_modak39552a02008-10-21 06:54:15 +0000162file $LTPTMP/test_file.txt > $LTPTMP/file.out 2>&1
iyermanojfce31412002-12-17 15:37:29 +0000163
robbiewc6f5e762003-12-09 19:03:08 +0000164if [ $? -eq 0 ]
iyermanojfce31412002-12-17 15:37:29 +0000165then
subrata_modak39552a02008-10-21 06:54:15 +0000166 grep "ASCII text" $LTPTMP/file.out > /dev/null 2>&1
iyermanojde6f3982002-12-17 16:02:52 +0000167 if [ $? -eq 0 ]
168 then
169 $LTPBIN/tst_resm TPASS "file: Recognised ASCII file correctly"
robbiewc6f5e762003-12-09 19:03:08 +0000170 rm -f $LTPTMP/test_file.txt
iyermanojde6f3982002-12-17 16:02:52 +0000171 else
172 $LTPBIN/tst_res TFAIL $LTPTMP/file.out \
173 "file: Failed to recognise ASCII file correctlyi. Reason:"
robbiew46d75562003-04-07 21:30:25 +0000174 TFAILCNT=$(( $TFAILCNT+1 ))
iyermanojde6f3982002-12-17 16:02:52 +0000175 fi
iyermanojfce31412002-12-17 15:37:29 +0000176else
iyermanojde6f3982002-12-17 16:02:52 +0000177 $LTPBIN/tst_res TFAIL $LTPTMP/file.out \
178 "file: failed to recognize ASCII file correctly\t\t"
robbiew46d75562003-04-07 21:30:25 +0000179 TFAILCNT=$(( $TFAILCNT+1 ))
iyermanojfce31412002-12-17 15:37:29 +0000180fi
181
182# TEST #2
183# Test if file command can recognize bash shell script
184
iyermanojde6f3982002-12-17 16:02:52 +0000185export TCID=file02
iyermanojfce31412002-12-17 15:37:29 +0000186export TST_COUNT=2
187
188$LTPBIN/tst_resm TINFO "TEST #2: file command recognizes bash shell scripts"
189
190cat > $LTPTMP/bash_script.sh <<EOF
191#! /bin/bash
192
193echo "this is a shell script"
subrata_modak29e13752009-08-23 06:30:42 +0000194echo "used to test file command"
iyermanojfce31412002-12-17 15:37:29 +0000195
196EOF
197
subrata_modak39552a02008-10-21 06:54:15 +0000198file $LTPTMP/bash_script.sh > $LTPTMP/file.out 2>&1
iyermanojfce31412002-12-17 15:37:29 +0000199
robbiewc6f5e762003-12-09 19:03:08 +0000200if [ $? -eq 0 ]
iyermanojfce31412002-12-17 15:37:29 +0000201then
subrata_modak39552a02008-10-21 06:54:15 +0000202 grep "Bourne-Again shell script" $LTPTMP/file.out > /dev/null 2>&1
iyermanojde6f3982002-12-17 16:02:52 +0000203 if [ $? -eq 0 ]
204 then
205 $LTPBIN/tst_resm TPASS "file: Recognised bash shell script correctly"
robbiewc6f5e762003-12-09 19:03:08 +0000206 rm -f $LTPTMP/bash_script.sh
iyermanojde6f3982002-12-17 16:02:52 +0000207 else
208 $LTPBIN/tst_res TFAIL $LTPTMP/file.out \
209 "file: Failed to recognise bash shell script. Reason"
robbiew46d75562003-04-07 21:30:25 +0000210 TFAILCNT=$(( $TFAILCNT+1 ))
iyermanojde6f3982002-12-17 16:02:52 +0000211 fi
iyermanojfce31412002-12-17 15:37:29 +0000212else
iyermanojde6f3982002-12-17 16:02:52 +0000213 $LTPBIN/tst_resm TFAIL "file: Failed to recognize bash shell script"
robbiew46d75562003-04-07 21:30:25 +0000214 TFAILCNT=$(( $TFAILCNT+1 ))
iyermanojfce31412002-12-17 15:37:29 +0000215fi
216
217# TEST #3
218# Test if file command can recognize korn shell script
219
iyermanojde6f3982002-12-17 16:02:52 +0000220export TCID=file03
iyermanojfce31412002-12-17 15:37:29 +0000221export TST_COUNT=3
222
223$LTPBIN/tst_resm TINFO "TEST #3: file command recognizes korn shell scripts"
224
225cat > $LTPTMP/ksh_script.sh <<EOF
226#! /bin/ksh
227
228echo "this is a shell script"
subrata_modak29e13752009-08-23 06:30:42 +0000229echo "used to test file command"
iyermanojfce31412002-12-17 15:37:29 +0000230
231EOF
232
subrata_modak39552a02008-10-21 06:54:15 +0000233file $LTPTMP/ksh_script.sh > $LTPTMP/file.out 2>&1
iyermanojfce31412002-12-17 15:37:29 +0000234
robbiewc6f5e762003-12-09 19:03:08 +0000235if [ $? -eq 0 ]
iyermanojfce31412002-12-17 15:37:29 +0000236then
iyermanojde6f3982002-12-17 16:02:52 +0000237 grep "Korn shell script" $LTPTMP/file.out 2>&1 1>/dev/null
238 if [ $? -eq 0 ]
239 then
240 $LTPBIN/tst_resm TPASS "file: recognised korn shell script"
robbiewc6f5e762003-12-09 19:03:08 +0000241 rm -f $LTPTMP/ksh_script.sh
iyermanojde6f3982002-12-17 16:02:52 +0000242 else
243 $LTPBIN/tst_res TFAIL $LTPTMP/file.out \
244 "file: Failed to recognise korn shell script. Reason:"
robbiew46d75562003-04-07 21:30:25 +0000245 TFAILCNT=$(( $TFAILCNT+1 ))
iyermanojde6f3982002-12-17 16:02:52 +0000246 fi
iyermanojfce31412002-12-17 15:37:29 +0000247else
iyermanojde6f3982002-12-17 16:02:52 +0000248 $LTPBIN/tst_resm TFAIL "File: Failed to recognize korn shell script"
robbiew46d75562003-04-07 21:30:25 +0000249 TFAILCNT=$(( $TFAILCNT+1 ))
iyermanojfce31412002-12-17 15:37:29 +0000250fi
251
252
253# TEST #4
254# Test if file command can recognize C shell script
255
iyermanojde6f3982002-12-17 16:02:52 +0000256export TCID=file04
iyermanojfce31412002-12-17 15:37:29 +0000257export TST_COUNT=4
258
259$LTPBIN/tst_resm TINFO "TEST #4: file command recognizes C shell scripts"
260
261cat > $LTPTMP/C_script.sh <<EOF
262#! /bin/csh
263
264echo "this is a shell script"
subrata_modak29e13752009-08-23 06:30:42 +0000265echo "used to test file command"
iyermanojfce31412002-12-17 15:37:29 +0000266
267EOF
268
subrata_modak39552a02008-10-21 06:54:15 +0000269file $LTPTMP/C_script.sh > $LTPTMP/file.out 2>&1
iyermanojfce31412002-12-17 15:37:29 +0000270
robbiewc6f5e762003-12-09 19:03:08 +0000271if [ $? -eq 0 ]
iyermanojfce31412002-12-17 15:37:29 +0000272then
subrata_modak39552a02008-10-21 06:54:15 +0000273 grep "C shell script" $LTPTMP/file.out > /dev/null 2>&1
iyermanojde6f3982002-12-17 16:02:52 +0000274 if [ $? -eq 0 ]
275 then
276 $LTPBIN/tst_resm TPASS "file: Recognised C shell script correctly"
robbiewc6f5e762003-12-09 19:03:08 +0000277 rm -f $LTPTMP/C_script.sh
iyermanojde6f3982002-12-17 16:02:52 +0000278 else
279 $LTPBIN/tst_resm TFAIL $LTPTMP/file.out \
280 "file: Failed to recognise C shell script correctly. Reason:"
robbiew46d75562003-04-07 21:30:25 +0000281 TFAILCNT=$(( $TFAILCNT+1 ))
iyermanojde6f3982002-12-17 16:02:52 +0000282 fi
iyermanojfce31412002-12-17 15:37:29 +0000283else
iyermanojde6f3982002-12-17 16:02:52 +0000284 $LTPBIN/tst_resm TFAIL "file: Failed to recognize C shell script"
robbiew46d75562003-04-07 21:30:25 +0000285 TFAILCNT=$(( $TFAILCNT+1 ))
iyermanojfce31412002-12-17 15:37:29 +0000286fi
287
288
289# TEST #5
290# Test if file command can recognize C program text
291
iyermanojde6f3982002-12-17 16:02:52 +0000292export TCID=file05
iyermanojfce31412002-12-17 15:37:29 +0000293export TST_COUNT=5
294
295$LTPBIN/tst_resm TINFO "TEST #5: file command recognizes C programs text"
296
297cat > $LTPTMP/cprog.c <<EOF
298#include <stdio.h>
299
300main()
301{
iyermanojde6f3982002-12-17 16:02:52 +0000302 printf("Hello Hell\n");
303 exit(0);
iyermanojfce31412002-12-17 15:37:29 +0000304}
305EOF
306
subrata_modak39552a02008-10-21 06:54:15 +0000307file $LTPTMP/cprog.c > $LTPTMP/file.out 2>&1
iyermanojfce31412002-12-17 15:37:29 +0000308
robbiewc6f5e762003-12-09 19:03:08 +0000309if [ $? -eq 0 ]
iyermanojfce31412002-12-17 15:37:29 +0000310then
subrata_modak39552a02008-10-21 06:54:15 +0000311 grep "ASCII C program text" $LTPTMP/file.out > /dev/null 2>&1
iyermanojde6f3982002-12-17 16:02:52 +0000312 if [ $? -eq 0 ]
313 then
314 $LTPBIN/tst_resm TPASS "file: Recognised C program text correctly"
robbiewc6f5e762003-12-09 19:03:08 +0000315 rm -f $LTPTMP/cprog.c
iyermanojde6f3982002-12-17 16:02:52 +0000316 else
317 $LTPBIN/tst_res TFAIL $LTPTMP/file.out \
318 "file: Failed to Recognize C program text correctly. Reason:"
robbiew46d75562003-04-07 21:30:25 +0000319 TFAILCNT=$(( $TFAILCNT+1 ))
iyermanojde6f3982002-12-17 16:02:52 +0000320
321 fi
iyermanojfce31412002-12-17 15:37:29 +0000322else
iyermanojde6f3982002-12-17 16:02:52 +0000323 $LTPBIN/tst_resm TFAIL "file: Failed to recognize C programi text"
robbiew46d75562003-04-07 21:30:25 +0000324 TFAILCNT=$(( $TFAILCNT+1 ))
iyermanojfce31412002-12-17 15:37:29 +0000325fi
326
327
328# TEST #6
329# Test if file command can recognize ELF binay executables
330
mreed1083088662007-02-28 18:01:32 +0000331# Check ppc architecture
332 TEST_ARCH=LSB # Assume the architecture is Intel
mreed1083088662007-02-28 18:01:32 +0000333
subrata_modakf8ee2c52008-10-21 09:01:48 +0000334 if uname -m |
335 grep -qe '\(m68k\)\|\(sparc\)\|\(mips\b\)\|\(mipseb\)\|\(sh.eb\)' \
336 -e '\(powerpc\)\|\(ppc\)\|\(s390\)'; then
mreed1083088662007-02-28 18:01:32 +0000337 TEST_ARCH=MSB
338 fi
339
iyermanojde6f3982002-12-17 16:02:52 +0000340export TCID=file06
iyermanojfce31412002-12-17 15:37:29 +0000341export TST_COUNT=6
342
343$LTPBIN/tst_resm TINFO \
iyermanojde6f3982002-12-17 16:02:52 +0000344 "TEST #6: file command recognizes ELF executables"
iyermanojfce31412002-12-17 15:37:29 +0000345
346
347cat > $LTPTMP/cprog.c <<EOF
348#include <stdio.h>
349
350main()
351{
iyermanojde6f3982002-12-17 16:02:52 +0000352 printf("Hello Hell\n");
353 exit(0);
iyermanojfce31412002-12-17 15:37:29 +0000354}
355EOF
356
subrata_modak39552a02008-10-21 06:54:15 +0000357cc -o $LTPTMP/cprog $LTPTMP/cprog.c > /dev/null 2>&1
iyermanojfce31412002-12-17 15:37:29 +0000358
subrata_modak39552a02008-10-21 06:54:15 +0000359file $LTPTMP/cprog > $LTPTMP/file.out 2>&1
iyermanojfce31412002-12-17 15:37:29 +0000360
robbiewc6f5e762003-12-09 19:03:08 +0000361if [ $? -eq 0 ]
iyermanojfce31412002-12-17 15:37:29 +0000362then
subrata_modak39552a02008-10-21 06:54:15 +0000363 grep "ELF .*-bit $TEST_ARCH executable, .*" $LTPTMP/file.out > /dev/null 2>&1
iyermanojde6f3982002-12-17 16:02:52 +0000364 if [ $? -eq 0 ]
365 then
366 $LTPBIN/tst_resm TPASS "file: Recognized ELF binary executable"
robbiewc6f5e762003-12-09 19:03:08 +0000367 rm -f $LTPTMP/cprog.c $LTPTMP/cprog
iyermanojde6f3982002-12-17 16:02:52 +0000368 else
369 $LTPBIN/tst_res TFAIL $LTPTMP/file.out \
370 "file: Failed to Recognize ELF binary executable. Reason:"
robbiew46d75562003-04-07 21:30:25 +0000371 TFAILCNT=$(( $TFAILCNT+1 ))
iyermanojde6f3982002-12-17 16:02:52 +0000372 fi
iyermanojfce31412002-12-17 15:37:29 +0000373else
subrata_modakc60b4822008-10-21 06:51:42 +0000374 $LTPBIN/tst_resm TFAIL "file: Failed to recognize ELF binary executable"
robbiew46d75562003-04-07 21:30:25 +0000375 TFAILCNT=$(( $TFAILCNT+1 ))
iyermanojfce31412002-12-17 15:37:29 +0000376fi
377
378
379# TEST #7
380# Test if file command can recognize tar files
381
iyermanojde6f3982002-12-17 16:02:52 +0000382export TCID=file07
robbiew87ea61f2003-12-08 16:07:38 +0000383export TST_COUNT=7
iyermanojfce31412002-12-17 15:37:29 +0000384
385$LTPBIN/tst_resm TINFO "TEST #7: file command recognizes tar files."
386
387cat > $LTPTMP/file1 <<EOF
388This is a simple test file
389EOF
390
391cat > $LTPTMP/file2 <<EOF
392This is a simple test file
393EOF
394
395cat > $LTPTMP/file3 <<EOF
396This is a simple test file
397EOF
398
subrata_modak39552a02008-10-21 06:54:15 +0000399tar -cf $LTPTMP/files.tar $LTPTMP/file1 $LTPTMP/file2 $LTPTMP/file3 > /dev/null 2>&1
iyermanojfce31412002-12-17 15:37:29 +0000400
subrata_modak39552a02008-10-21 06:54:15 +0000401file $LTPTMP/files.tar > $LTPTMP/file.out 2>&1
iyermanojfce31412002-12-17 15:37:29 +0000402
robbiewc6f5e762003-12-09 19:03:08 +0000403if [ $? -eq 0 ]
iyermanojfce31412002-12-17 15:37:29 +0000404then
subrata_modak39552a02008-10-21 06:54:15 +0000405 grep "tar archive" $LTPTMP/file.out > /dev/null 2>&1
iyermanojde6f3982002-12-17 16:02:52 +0000406 if [ $? -eq 0 ]
407 then
408 $LTPBIN/tst_resm TPASS "file: Recognised tar files"
robbiewc6f5e762003-12-09 19:03:08 +0000409 rm -f $LTPTMP/files.tar # save $LTPTMP/file[123] for next test case
iyermanojde6f3982002-12-17 16:02:52 +0000410 else
411 $LTPBIN/tst_res TFAIL $LTPTMP/file.out \
412 "file: Failed to Recognize tar files. Reason:"
robbiew46d75562003-04-07 21:30:25 +0000413 TFAILCNT=$(( $TFAILCNT+1 ))
iyermanojde6f3982002-12-17 16:02:52 +0000414 fi
iyermanojfce31412002-12-17 15:37:29 +0000415else
iyermanojde6f3982002-12-17 16:02:52 +0000416 $LTPBIN/tst_resm TFAIL "file: Failed to recognize tar files."
robbiew46d75562003-04-07 21:30:25 +0000417 TFAILCNT=$(( $TFAILCNT+1 ))
iyermanojfce31412002-12-17 15:37:29 +0000418fi
419
420
421# TEST #8
422# Test if file command can tar zip files
423
iyermanojde6f3982002-12-17 16:02:52 +0000424export TCID=file08
iyermanojfce31412002-12-17 15:37:29 +0000425export TST_COUNT=8
426
427$LTPBIN/tst_resm TINFO "TEST #8: file command recognizes tar zip files"
428
robbiew65839342003-11-26 19:30:28 +0000429tar cf $LTPTMP/files.tar $LTPTMP/file1 $LTPTMP/file2 $LTPTMP/file3 \
subrata_modak39552a02008-10-21 06:54:15 +0000430 > $LTPTMP/file.out 2>&1
iyermanojfce31412002-12-17 15:37:29 +0000431if [ $? -ne 0 ]
432then
iyermanojde6f3982002-12-17 16:02:52 +0000433 $LTPBIN/tst_brk TBROK $LTPTMP/file.out NULL \
434 "file: tar failed unexpectedly. Reason:"
iyermanojfce31412002-12-17 15:37:29 +0000435fi
iyermanojde6f3982002-12-17 16:02:52 +0000436
iyermanojfce31412002-12-17 15:37:29 +0000437gzip -f $LTPTMP/files.tar
438if [ $? -ne 0 ]
439then
iyermanojde6f3982002-12-17 16:02:52 +0000440 $LTPBIN/tst_brk TBROK $LTPTMP/file.out NULL \
441 "file: gzip failed unexpectedly. Reason:"
iyermanojfce31412002-12-17 15:37:29 +0000442fi
iyermanojde6f3982002-12-17 16:02:52 +0000443
subrata_modak39552a02008-10-21 06:54:15 +0000444file $LTPTMP/files.tar.gz > $LTPTMP/file.out 2>&1
robbiewc6f5e762003-12-09 19:03:08 +0000445if [ $? -eq 0 ]
iyermanojfce31412002-12-17 15:37:29 +0000446then
subrata_modak39552a02008-10-21 06:54:15 +0000447 grep "gzip compressed data, .*" $LTPTMP/file.out > $LTPTMP/file1.out 2>&1
iyermanojde6f3982002-12-17 16:02:52 +0000448 if [ $? -eq 0 ]
449 then
450 $LTPBIN/tst_resm TPASS "file: Recognised tar zip file"
robbiewc6f5e762003-12-09 19:03:08 +0000451 rm -f $LTPTMP/files.tar.gz $LTPTMP/file1 $LTPTMP/file2 $LTPTMP/file3
452 rm -f $LTPTMP/file1.out
iyermanojde6f3982002-12-17 16:02:52 +0000453 else
454 $LTPBIN/tst_brkm TBROK NULL \
455 "expected string: gzip compressed data, deflated,
iyermanojfce31412002-12-17 15:37:29 +0000456original filename, \`files.tar'"
iyermanojde6f3982002-12-17 16:02:52 +0000457 $LTPBIN/tst_res TFAIL $LTPTMP/file.out \
458 "file: Failed to Recognize tar zip. Reason:"
robbiew46d75562003-04-07 21:30:25 +0000459 TFAILCNT=$(( $TFAILCNT+1 ))
iyermanojde6f3982002-12-17 16:02:52 +0000460 fi
iyermanojfce31412002-12-17 15:37:29 +0000461else
iyermanojde6f3982002-12-17 16:02:52 +0000462 $LTPBIN/tst_brk TBROK $LTPTMP/file.out NULL \
463 "file: Failed to recognize tar zip file. Reason:"
robbiew46d75562003-04-07 21:30:25 +0000464 TFAILCNT=$(( $TFAILCNT+1 ))
iyermanojfce31412002-12-17 15:37:29 +0000465fi
466
467
468# TEST #9
469# Test if file command can recognize RPM files.
470
iyermanojde6f3982002-12-17 16:02:52 +0000471export TCID=file09
iyermanojfce31412002-12-17 15:37:29 +0000472export TST_COUNT=9
473
474$LTPBIN/tst_resm TINFO "TEST #9: file command recognizes RPM files"
subrata_modak97544a12009-10-26 15:07:57 +0000475type rpm > /dev/null 2>&1
subrata_modak2d01bce2009-08-23 05:57:38 +0000476if [ $? = 0 ]; then
subrata_modak33463f82009-08-28 06:00:20 +0000477bDIR=$(rpm --eval "%{_topdir}")
478bCMD=rpmbuild
iyermanojfce31412002-12-17 15:37:29 +0000479
mreed1016c180b2006-05-01 23:01:24 +0000480rpmversion=`rpm --version | awk -F ' ' '{print $3}' | cut -d '.' -f1 `
481
482if [ "$rpmversion" -ge "4" ]; then
483 gpl="License: GPL"
484else
485 gpl="Copyright: GPL"
486fi
487
488
iyermanojfce31412002-12-17 15:37:29 +0000489cat > $LTPTMP/files.spec <<EOF
490
491Summary: Dummy package used to test file command
492Name: cprog
493Version: 0.0.7
494Release: 3
mreed1016c180b2006-05-01 23:01:24 +0000495$gpl
iyermanojfce31412002-12-17 15:37:29 +0000496Group: LillB test case
497Source: ./cprog.c
498BuildRoot: /var/tmp/%{name}-buildroot
499
500%description
501Dummy RPM package used for testing file command.
502
503%prep
504%setup -q
505
506%build
507make RPM_OPT_FLAGS="$RPM_OPT_FLAGS"
508
509%install
510rm -rf $RPM_BUILD_ROOT
511install -s -m 755 cprog $RPM_BUILD_ROOT/tmp
512
513%clean
514rm -rf $RPM_BUILD_ROOT
515
516%files -f ./cprog.c
517%defattr(-,root,root)
518%doc README TODO COPYING ChangeLog
519
520EOF
521
robbiewc6f5e762003-12-09 19:03:08 +0000522RC=0
robbiew65839342003-11-26 19:30:28 +0000523if [ -d $bDIR/SOURCES ]
iyermanojfce31412002-12-17 15:37:29 +0000524then
subrata_modak39552a02008-10-21 06:54:15 +0000525 echo "directory exists" > $LTPTMP/file.out 2>&1
iyermanojfce31412002-12-17 15:37:29 +0000526else
subrata_modak39552a02008-10-21 06:54:15 +0000527 mkdir -p $bDIR/SOURCES/ > $LTPTMP/file.out 2>&1 || RC=$?
iyermanojfce31412002-12-17 15:37:29 +0000528fi
529
530if [ $RC -ne 0 ]
531then
subrata_modak89050332009-08-23 06:32:43 +0000532 $LTPBIN/tst_brk TBROK $LTPTMP/file.out NULL "mkdir: broke. Reason:"
iyermanojfce31412002-12-17 15:37:29 +0000533fi
534
robbiewc6f5e762003-12-09 19:03:08 +0000535cat > $bDIR/SOURCES/cprog.c <<EOF
iyermanojfce31412002-12-17 15:37:29 +0000536#include <stdio.h>
537
538main()
539{
540 printf("Hello Hell\n");
541 exit(0);
542}
543EOF
robbiewc6f5e762003-12-09 19:03:08 +0000544if [ $? -ne 0 ]
iyermanojfce31412002-12-17 15:37:29 +0000545then
iyermanojde6f3982002-12-17 16:02:52 +0000546 $LTPBIN/tst_brkm TBROK NULL "cat: failed to create test file cprog.c"
iyermanojfce31412002-12-17 15:37:29 +0000547fi
548
subrata_modak54127302009-05-22 06:30:17 +0000549$bCMD --define "_topdir $bDIR" -bs $LTPTMP/files.spec > $LTPTMP/file.out 2>&1
iyermanojfce31412002-12-17 15:37:29 +0000550if [ $? -ne 0 ]
551then
subrata_modak89050332009-08-23 06:32:43 +0000552 $LTPBIN/tst_brk TBROK $LTPTMP/file.out NULL "rpm command broke. Reason:"
iyermanojfce31412002-12-17 15:37:29 +0000553fi
554
subrata_modak39552a02008-10-21 06:54:15 +0000555file $bDIR/SRPMS/cprog-0.0.7-3.src.rpm > $LTPTMP/file.out 2>&1
iyermanojfce31412002-12-17 15:37:29 +0000556
robbiewc6f5e762003-12-09 19:03:08 +0000557if [ $? -eq 0 ]
iyermanojfce31412002-12-17 15:37:29 +0000558then
subrata_modak39552a02008-10-21 06:54:15 +0000559 grep "RPM v3 src" $LTPTMP/file.out > /dev/null 2>&1
iyermanojde6f3982002-12-17 16:02:52 +0000560 if [ $? -eq 0 ]
561 then
562 $LTPBIN/tst_resm TPASS "file: Recognised RPM file correctly"
robbiewc6f5e762003-12-09 19:03:08 +0000563 rm -f $LTPTMP/files.spec
iyermanojde6f3982002-12-17 16:02:52 +0000564 else
565 $LTPBIN/tst_res TFAIL $LTPTMP/file.out \
566 "file: Failed to Recognize RPM file. Reason:"
robbiew46d75562003-04-07 21:30:25 +0000567 TFAILCNT=$(( $TFAILCNT+1 ))
iyermanojde6f3982002-12-17 16:02:52 +0000568
569 fi
iyermanojfce31412002-12-17 15:37:29 +0000570else
iyermanojde6f3982002-12-17 16:02:52 +0000571 $LTPBIN/tst_resm TFAIL "file: Failed to recognize RPM file"
robbiew46d75562003-04-07 21:30:25 +0000572 TFAILCNT=$(( $TFAILCNT+1 ))
iyermanojde6f3982002-12-17 16:02:52 +0000573fi
subrata_modak2d01bce2009-08-23 05:57:38 +0000574else
575 $LTPBIN/tst_resm TCONF "rpm not installed"
576fi
iyermanojde6f3982002-12-17 16:02:52 +0000577
578
579# TEST #10
subrata_modak7c4ec402008-02-27 14:38:32 +0000580# Test if file command can recognize kernel file
iyermanojde6f3982002-12-17 16:02:52 +0000581
582export TCID=file10
robbiew87ea61f2003-12-08 16:07:38 +0000583export TST_COUNT=10
iyermanojde6f3982002-12-17 16:02:52 +0000584
subrata_modak2d01bce2009-08-23 05:57:38 +0000585KERNEL=vmlinu
subrata_modak7c4ec402008-02-27 14:38:32 +0000586
587$LTPBIN/tst_resm TINFO "TEST #10: file command recognizes $KERNEL file"
iyermanojde6f3982002-12-17 16:02:52 +0000588
subrata_modakf8ee2c52008-10-21 09:01:48 +0000589# S390 Work around for vmlinuz file type
590# Applesoft BASIC:
591#
592# This is incredibly sloppy, but will be true if the program was
593# written at its usual memory location of 2048 and its first line
594# number is less than 256. Yuck.
595#0 belong&0xff00ff 0x80000 Applesoft BASIC program data
596#>2 leshort x \b, first line number %d
597
robbiewc6f5e762003-12-09 19:03:08 +0000598# Red Hat creates a user-mode-linux vmlinuz file (ends in .uml) - ignore it
subrata_modak7c4ec402008-02-27 14:38:32 +0000599KERNFILE=$(find /boot ! -type l -name "$KERNEL*" | grep -v '.uml' | tail -1)
subrata_modak39552a02008-10-21 06:54:15 +0000600file $KERNFILE > $LTPTMP/file.out 2>&1
iyermanojde6f3982002-12-17 16:02:52 +0000601
robbiew87ea61f2003-12-08 16:07:38 +0000602if [ $? -eq 0 ]
iyermanojde6f3982002-12-17 16:02:52 +0000603then
robbiew87ea61f2003-12-08 16:07:38 +0000604#####
605# There are lots of possible strings to look for, given the number
606# of different architectures...
607#####
608 MATCHED=""
subrata_modak7c4ec402008-02-27 14:38:32 +0000609 grep -iq "$TEST_ARCH" $LTPTMP/file.out && MATCHED="y"
610 grep -iq "kernel" $LTPTMP/file.out && MATCHED="y"
611 grep -iq "compressed data" $LTPTMP/file.out && MATCHED="y"
612 grep -iq "x86 boot sector" $LTPTMP/file.out && MATCHED="y"
subrata_modakf8ee2c52008-10-21 09:01:48 +0000613 grep -iq "Applesoft BASIC" $LTPTMP/file.out && MATCHED="y"
robbiew87ea61f2003-12-08 16:07:38 +0000614 if [ -n "$MATCHED" ]
iyermanojde6f3982002-12-17 16:02:52 +0000615 then
subrata_modak7c4ec402008-02-27 14:38:32 +0000616 $LTPBIN/tst_resm TPASS "file: Recognised $KERNEL file correctly"
iyermanojde6f3982002-12-17 16:02:52 +0000617 else
618 $LTPBIN/tst_res TFAIL $LTPTMP/file.out \
subrata_modak7c4ec402008-02-27 14:38:32 +0000619 "file: Failed to Recognize $KERNEL correctly. Reason:"
robbiew46d75562003-04-07 21:30:25 +0000620 TFAILCNT=$(( $TFAILCNT+1 ))
iyermanojde6f3982002-12-17 16:02:52 +0000621 fi
622else
subrata_modak7c4ec402008-02-27 14:38:32 +0000623 $LTPBIN/tst_resm TFAIL "file: Failed to recognize $KERNEL file"
robbiew46d75562003-04-07 21:30:25 +0000624 TFAILCNT=$(( $TFAILCNT+1 ))
iyermanojfce31412002-12-17 15:37:29 +0000625fi
iyermanoj2a8f2092002-12-17 16:42:06 +0000626
robbiewc6f5e762003-12-09 19:03:08 +0000627rm -f $LTPTMP/file.out
iyermanoj2a8f2092002-12-17 16:42:06 +0000628exit $TFAILCNT