1#!/bin/sh
2################################################################################
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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.
32#                            If LTPTMP and LTPBIN are not exported set their
33#                            values to /tmp and 'pwd' respectively.
34#                          - Added.   - exit status, if any test fails the test
35#                            exits with non-zero value if all tests pass test
36#                            exits with zero.
37#              Dec 18 2002 - Added.   - Code to read environment variable
38#                            LTPROOT and TMPBASE and set LTPTMP and LTPBIN
39#                            accordingly.
40
41
42#
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 binary executables
79# -------
80# 1) Use readelf to determine if the host is big- or little-endian
81#    and assign TEST_ARCH the string "MSB" or "LSB" respectively
82# 2) Write small C program to a known '.c' file
83# 3) Compile it using "cc"
84#    Ex: cc xyz xyz.c
85# 4) Use file command to get the type of the object file
86# 5) Grep for the string "ELF .*-bit $TEST_ARCH executable, .*"
87#    in the output of the 'file' command
88# 6) If the above command is successful, declare test as PASS
89#    else declare test as FAIL
90#
91# Test07: Test if file command can recognize tar files
92# -------
93# 1) Write text to three different files
94# 2) Archive the files using "tar" command
95#    Ex: tar -cf ...
96# 3) Use 'file' command to get the type of the archive file
97#    Ex: file xyz.tar
98# 4) Grep for the string "tar archive" from the output of
99#    the above 'file' command
100# 5) Declare test as PASS, if the above step is successfull else
101#    declare test as FAIL
102#
103# Test08: Test if file command can tar zip files
104# -------
105# 1) Write text to three different files
106# 2) Archive the files using "tar" command
107#    Ex: tar -cf ...
108# 3) Use 'gzip' command to zip tar files
109#    Ex: gzip -f xyz.tar
110# 4) Use 'file' command to get the type of the archive file
111#    Ex: file xyz.tar.gz
112# 5) Grep for the string "gzip compressed data, .*" from the above
113#    file commnand
114# 6) Declare test as PASS, if the above step is successfull else
115#    declare test as FAIL
116#
117
118
119export TST_TOTAL=10                # Number of tests in this testcase
120
121if [ -z "$LTPTMP" -a -z "$TMPBASE" ]
122then
123    LTPTMP=/tmp/
124else
125    LTPTMP=$TMPBASE
126fi
127
128# 'LTPBIN' where actual test cases (test binaries) reside
129# 'LTPROOT' where the actual LTP test suite resides
130if [ -z "$LTPBIN" -a -z "$LTPROOT" ]
131then
132    LTPBIN=./
133else
134	LTPBIN=$LTPROOT/testcases/bin/
135fi
136
137# set return code RC variable to 0, it will be set with a non-zero return code
138# in case of error. Set TFAILCNT to 0, increment if there occures a failure.
139
140TFAILCNT=0
141RC=0
142
143# TEST #1
144# Test if file command recognizes ASCII text files.
145
146export TCID=file01
147export TST_COUNT=1
148
149$LTPBIN/tst_resm TINFO "TEST #1: file command recogizes ASCII text files"
150
151cat > $LTPTMP/test_file.txt <<EOF
152This is a text file
153to test file command.
154EOF
155
156# Execute file command & check for output.
157# Expected out put is the string "ASCII English text"
158
159file $LTPTMP/test_file.txt > $LTPTMP/file.out 2>&1
160
161if [ $? -eq 0 ]
162then
163    grep -q "ASCII text" $LTPTMP/file.out
164    if [ $? -eq 0 ]
165    then
166        $LTPBIN/tst_resm TPASS "file: Recognised ASCII file correctly"
167        rm -f $LTPTMP/test_file.txt
168    else
169        $LTPBIN/tst_res TFAIL $LTPTMP/file.out \
170                "file: Failed to recognise ASCII file correctlyi. Reason:"
171        TFAILCNT=$(( $TFAILCNT+1 ))
172    fi
173else
174    $LTPBIN/tst_res TFAIL $LTPTMP/file.out  \
175                "file: failed to recognize ASCII file correctly\t\t"
176    TFAILCNT=$(( $TFAILCNT+1 ))
177fi
178
179# TEST #2
180# Test if file command can recognize bash shell script
181
182export TCID=file02
183export TST_COUNT=2
184
185$LTPBIN/tst_resm TINFO "TEST #2: file command recognizes bash shell scripts"
186
187cat > $LTPTMP/bash_script.sh <<EOF
188#! /bin/bash
189
190echo "this is a shell script"
191echo "used to test file command"
192
193EOF
194
195file $LTPTMP/bash_script.sh > $LTPTMP/file.out 2>&1
196
197if [ $? -eq 0 ]
198then
199    grep -q "Bourne-Again shell script" $LTPTMP/file.out
200    if [ $? -eq 0 ]
201    then
202        $LTPBIN/tst_resm TPASS "file: Recognised bash shell script correctly"
203        rm -f $LTPTMP/bash_script.sh
204    else
205        $LTPBIN/tst_res TFAIL $LTPTMP/file.out \
206            "file: Failed to recognise bash shell script. Reason"
207        TFAILCNT=$(( $TFAILCNT+1 ))
208    fi
209else
210    $LTPBIN/tst_resm TFAIL "file: Failed to recognize bash shell script"
211    TFAILCNT=$(( $TFAILCNT+1 ))
212fi
213
214# TEST #3
215# Test if file command can recognize korn shell script
216
217export TCID=file03
218export TST_COUNT=3
219
220$LTPBIN/tst_resm TINFO "TEST #3: file command recognizes korn shell scripts"
221
222cat > $LTPTMP/ksh_script.sh <<EOF
223#! /bin/ksh
224
225echo "this is a shell script"
226echo "used to test file command"
227
228EOF
229
230file $LTPTMP/ksh_script.sh > $LTPTMP/file.out 2>&1
231
232if [ $? -eq 0 ]
233then
234    grep -q "Korn shell script" $LTPTMP/file.out
235    if [ $? -eq 0 ]
236    then
237        $LTPBIN/tst_resm TPASS "file: recognised korn shell script"
238        rm -f $LTPTMP/ksh_script.sh
239    else
240        $LTPBIN/tst_res TFAIL $LTPTMP/file.out \
241            "file: Failed to recognise korn shell script. Reason:"
242        TFAILCNT=$(( $TFAILCNT+1 ))
243    fi
244else
245    $LTPBIN/tst_resm TFAIL "File: Failed to recognize korn shell script"
246    TFAILCNT=$(( $TFAILCNT+1 ))
247fi
248
249
250# TEST #4
251# Test if file command can recognize C shell script
252
253export TCID=file04
254export TST_COUNT=4
255
256$LTPBIN/tst_resm TINFO "TEST #4: file command recognizes C shell scripts"
257
258cat > $LTPTMP/C_script.sh <<EOF
259#! /bin/csh
260
261echo "this is a shell script"
262echo "used to test file command"
263
264EOF
265
266file $LTPTMP/C_script.sh > $LTPTMP/file.out 2>&1
267
268if [ $? -eq 0 ]
269then
270    grep -q "C shell script" $LTPTMP/file.out
271    if [ $? -eq 0 ]
272    then
273        $LTPBIN/tst_resm TPASS "file: Recognised C shell script correctly"
274        rm -f $LTPTMP/C_script.sh
275    else
276        $LTPBIN/tst_resm TFAIL $LTPTMP/file.out \
277            "file: Failed to recognise C shell script correctly. Reason:"
278        TFAILCNT=$(( $TFAILCNT+1 ))
279    fi
280else
281    $LTPBIN/tst_resm TFAIL "file: Failed to recognize C shell script"
282    TFAILCNT=$(( $TFAILCNT+1 ))
283fi
284
285
286# TEST #5
287# Test if file command can recognize C program text
288
289export TCID=file05
290export TST_COUNT=5
291
292$LTPBIN/tst_resm TINFO "TEST #5: file command recognizes C programs text"
293
294cat > $LTPTMP/cprog.c <<EOF
295#include <stdio.h>
296
297main()
298{
299    printf("Hello Hell\n");
300    exit(0);
301}
302EOF
303
304file $LTPTMP/cprog.c > $LTPTMP/file.out 2>&1
305
306if [ $? -eq 0 ]
307then
308    grep -q "ASCII C program text" $LTPTMP/file.out
309    if [ $? -eq 0 ]
310    then
311        $LTPBIN/tst_resm TPASS "file: Recognised C program text correctly"
312        rm -f $LTPTMP/cprog.c
313    else
314        grep -q "C source, ASCII text" $LTPTMP/file.out
315        if [ $? -eq 0 ]
316        then
317            $LTPBIN/tst_resm TPASS "file: Recognised C program text correctly"
318            rm -f $LTPTMP/cprog.c
319        else
320            $LTPBIN/tst_res TFAIL $LTPTMP/file.out \
321                 "file: Failed to Recognize C program text correctly. Reason:"
322            TFAILCNT=$(( $TFAILCNT+1 ))
323        fi
324    fi
325else
326    $LTPBIN/tst_resm TFAIL "file: Failed to recognize C programi text"
327    TFAILCNT=$(( $TFAILCNT+1 ))
328fi
329
330
331# TEST #6
332# Test if file command can recognize ELF binary executables
333
334export TCID=file06
335export TST_COUNT=6
336
337$LTPBIN/tst_resm TINFO \
338        "TEST #6: file command recognizes ELF executables"
339
340# check for CPU endianness
341case $(readelf -h /bin/sh) in
342    *Data:*"big endian"*)
343        TEST_ARCH=MSB
344        ;;
345    *Data:*"little endian"*)
346        TEST_ARCH=LSB
347        ;;
348    *)
349        TEST_ARCH=NULL
350        $LTPBIN/tst_resm TFAIL "file: Could not determine CPU endianness"
351        ;;
352esac
353
354cat > $LTPTMP/cprog.c <<EOF
355#include <stdio.h>
356
357main()
358{
359    printf("Hello Hell\n");
360    exit(0);
361}
362EOF
363
364cc -o $LTPTMP/cprog $LTPTMP/cprog.c > /dev/null 2>&1
365
366file $LTPTMP/cprog > $LTPTMP/file.out 2>&1
367
368if [ $? -eq 0 ]
369then
370    grep -q "ELF .*-bit $TEST_ARCH executable, .*" $LTPTMP/file.out
371    if [ $? -eq 0 ]
372    then
373        $LTPBIN/tst_resm TPASS "file: Recognized ELF binary executable"
374        rm -f $LTPTMP/cprog.c $LTPTMP/cprog
375    else
376        $LTPBIN/tst_res TFAIL $LTPTMP/file.out \
377             "file: Failed to Recognize ELF binary executable. Reason:"
378        TFAILCNT=$(( $TFAILCNT+1 ))
379    fi
380else
381    $LTPBIN/tst_resm TFAIL "file: Failed to recognize ELF binary executable"
382    TFAILCNT=$(( $TFAILCNT+1 ))
383fi
384
385
386# TEST #7
387# Test if file command can recognize tar files
388
389export TCID=file07
390export TST_COUNT=7
391
392$LTPBIN/tst_resm TINFO "TEST #7: file command recognizes tar files."
393
394cat > $LTPTMP/file1 <<EOF
395This is a simple test file
396EOF
397
398cat > $LTPTMP/file2 <<EOF
399This is a simple test file
400EOF
401
402cat > $LTPTMP/file3 <<EOF
403This is a simple test file
404EOF
405
406tar -cf $LTPTMP/files.tar $LTPTMP/file1 $LTPTMP/file2 $LTPTMP/file3 > /dev/null 2>&1
407
408file $LTPTMP/files.tar > $LTPTMP/file.out 2>&1
409
410if [ $? -eq 0 ]
411then
412    grep -q "tar archive" $LTPTMP/file.out
413    if [ $? -eq 0 ]
414    then
415        $LTPBIN/tst_resm TPASS "file: Recognised tar files"
416        rm -f $LTPTMP/files.tar # save $LTPTMP/file[123] for next test case
417    else
418        $LTPBIN/tst_res TFAIL $LTPTMP/file.out \
419             "file: Failed to Recognize tar files. Reason:"
420        TFAILCNT=$(( $TFAILCNT+1 ))
421    fi
422else
423    $LTPBIN/tst_resm TFAIL "file: Failed to recognize tar files."
424    TFAILCNT=$(( $TFAILCNT+1 ))
425fi
426
427
428# TEST #8
429# Test if file command can tar zip files
430
431export TCID=file08
432export TST_COUNT=8
433
434$LTPBIN/tst_resm TINFO "TEST #8: file command recognizes tar zip files"
435
436tar cf $LTPTMP/files.tar $LTPTMP/file1 $LTPTMP/file2 $LTPTMP/file3 \
437    > $LTPTMP/file.out 2>&1
438if [ $? -ne 0 ]
439then
440    $LTPBIN/tst_brk TBROK $LTPTMP/file.out NULL \
441        "file: tar failed unexpectedly. Reason:"
442fi
443
444gzip -f $LTPTMP/files.tar
445if [ $? -ne 0 ]
446then
447    $LTPBIN/tst_brk TBROK $LTPTMP/file.out NULL \
448        "file: gzip failed unexpectedly. Reason:"
449fi
450
451file $LTPTMP/files.tar.gz > $LTPTMP/file.out 2>&1
452if [ $? -eq 0 ]
453then
454    grep "gzip compressed data, .*" $LTPTMP/file.out > $LTPTMP/file1.out 2>&1
455    if [ $? -eq 0 ]
456    then
457        $LTPBIN/tst_resm TPASS "file: Recognised tar zip file"
458        rm -f $LTPTMP/files.tar.gz $LTPTMP/file1 $LTPTMP/file2 $LTPTMP/file3
459        rm -f $LTPTMP/file1.out
460    else
461        $LTPBIN/tst_brkm TBROK NULL \
462                "expected string: gzip compressed data, deflated,
463original filename, \`files.tar'"
464        $LTPBIN/tst_res TFAIL $LTPTMP/file.out \
465             "file: Failed to Recognize tar zip. Reason:"
466        TFAILCNT=$(( $TFAILCNT+1 ))
467    fi
468else
469    $LTPBIN/tst_brk TBROK $LTPTMP/file.out NULL \
470        "file: Failed to recognize tar zip file. Reason:"
471    TFAILCNT=$(( $TFAILCNT+1 ))
472fi
473
474
475# TEST #9
476# Test if file command can recognize RPM files.
477
478export TCID=file09
479export TST_COUNT=9
480
481$LTPBIN/tst_resm TINFO "TEST #9: file command recognizes RPM files"
482type rpm > /dev/null 2>&1
483if [ $? = 0 ]; then
484bDIR=$(rpm --eval "%{_topdir}")
485bCMD=rpmbuild
486
487rpmversion=`rpm --version | awk -F ' ' '{print $3}' | cut -d '.' -f1 `
488
489if [ "$rpmversion" -ge "4" ]; then
490 gpl="License: GPL"
491else
492 gpl="Copyright: GPL"
493fi
494
495
496cat > $LTPTMP/files.spec <<EOF
497
498Summary: Dummy package used to test file command
499Name: cprog
500Version: 0.0.7
501Release: 3
502$gpl
503Group: LillB test case
504Source: ./cprog.c
505BuildRoot: /var/tmp/%{name}-buildroot
506
507%description
508Dummy RPM package used for testing file command.
509
510%prep
511%setup -q
512
513%build
514make RPM_OPT_FLAGS="$RPM_OPT_FLAGS"
515
516%install
517rm -rf $RPM_BUILD_ROOT
518install -s -m 755 cprog $RPM_BUILD_ROOT/tmp
519
520%clean
521rm -rf $RPM_BUILD_ROOT
522
523%files -f ./cprog.c
524%defattr(-,root,root)
525%doc README TODO COPYING ChangeLog
526
527EOF
528
529RC=0
530if [ -d $bDIR/SOURCES ]
531then
532    echo "directory exists" > $LTPTMP/file.out 2>&1
533else
534    mkdir -p $bDIR/SOURCES/ > $LTPTMP/file.out 2>&1 || RC=$?
535fi
536
537if [ $RC -ne 0 ]
538then
539    $LTPBIN/tst_brk TBROK $LTPTMP/file.out NULL "mkdir: broke. Reason:"
540fi
541
542cat > $bDIR/SOURCES/cprog.c <<EOF
543#include <stdio.h>
544
545main()
546{
547    printf("Hello Hell\n");
548    exit(0);
549}
550EOF
551if [ $? -ne 0 ]
552then
553    $LTPBIN/tst_brkm TBROK NULL "cat: failed to create test file cprog.c"
554fi
555
556$bCMD --define "_topdir $bDIR" -bs  $LTPTMP/files.spec > $LTPTMP/file.out 2>&1
557if [ $? -ne 0 ]
558then
559    $LTPBIN/tst_brk TBROK $LTPTMP/file.out NULL "rpm command broke. Reason:"
560fi
561
562file $bDIR/SRPMS/cprog-0.0.7-3.src.rpm > $LTPTMP/file.out 2>&1
563
564if [ $? -eq 0 ]
565then
566    grep -qE "RPM v3(\.0)? src" $LTPTMP/file.out
567    if [ $? -eq 0 ]
568    then
569        $LTPBIN/tst_resm TPASS "file: Recognised RPM file correctly"
570        rm -f $LTPTMP/files.spec
571    else
572        $LTPBIN/tst_res TFAIL $LTPTMP/file.out \
573             "file: Failed to Recognize RPM file. Reason:"
574        TFAILCNT=$(( $TFAILCNT+1 ))
575
576    fi
577else
578    $LTPBIN/tst_resm TFAIL "file: Failed to recognize RPM file"
579    TFAILCNT=$(( $TFAILCNT+1 ))
580fi
581else
582    $LTPBIN/tst_resm TCONF "rpm not installed"
583fi
584
585
586# TEST #10
587# Test if file command can recognize kernel file
588
589export TCID=file10
590export TST_COUNT=10
591
592KERNEL=vmlinu
593
594$LTPBIN/tst_resm TINFO "TEST #10: file command recognizes $KERNEL file"
595
596# S390 Work around for vmlinuz file type
597# Applesoft BASIC:
598#
599# This is incredibly sloppy, but will be true if the program was
600# written at its usual memory location of 2048 and its first line
601# number is less than 256.  Yuck.
602#0       belong&0xff00ff 0x80000 Applesoft BASIC program data
603#>2      leshort         x       \b, first line number %d
604
605# Red Hat creates a user-mode-linux vmlinuz file (ends in .uml) - ignore it
606KERNFILE=$(find /boot ! -type l -name "$KERNEL*" | grep -v '.uml' | tail -n 1)
607file $KERNFILE > $LTPTMP/file.out 2>&1
608
609if [ $? -eq 0 ]
610then
611#####
612# There are lots of possible strings to look for, given the number
613# of different architectures...
614#####
615    MATCHED=""
616    grep -iq "$TEST_ARCH" $LTPTMP/file.out && MATCHED="y"
617    grep -iq "linux" $LTPTMP/file.out && MATCHED="y"
618    grep -iq "kernel" $LTPTMP/file.out && MATCHED="y"
619    grep -iq "compressed data" $LTPTMP/file.out && MATCHED="y"
620    grep -iq "x86 boot sector" $LTPTMP/file.out && MATCHED="y"
621    grep -iq "Applesoft BASIC" $LTPTMP/file.out && MATCHED="y"
622    if [ -n "$MATCHED" ]
623    then
624        $LTPBIN/tst_resm TPASS "file: Recognised $KERNEL file correctly"
625    else
626        $LTPBIN/tst_res TFAIL $LTPTMP/file.out \
627             "file: Failed to Recognize $KERNEL correctly. Reason:"
628        TFAILCNT=$(( $TFAILCNT+1 ))
629    fi
630else
631    $LTPBIN/tst_resm TFAIL "file: Failed to recognize $KERNEL file"
632    TFAILCNT=$(( $TFAILCNT+1 ))
633fi
634
635rm -f $LTPTMP/file.out
636exit $TFAILCNT
637