Home | History | Annotate | Download | only in tar
      1 #!/bin/sh
      2 ################################################################################
      3 ##                                                                            ##
      4 ## Copyright (c) International Business Machines  Corp., 2001                 ##
      5 ##  Author: Manoj Iyer, manjo (at] mail.utexas.edu                                 ##
      6 ## Copyright (c) 2016 Cyril Hrubis <chrubis (at] suse.cz>                          ##
      7 ##                                                                            ##
      8 ## This program is free software;  you can redistribute it and#or modify      ##
      9 ## it under the terms of the GNU General Public License as published by       ##
     10 ## the Free Software Foundation; either version 2 of the License, or          ##
     11 ## (at your option) any later version.                                        ##
     12 ##                                                                            ##
     13 ## This program is distributed in the hope that it will be useful, but        ##
     14 ## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
     15 ## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
     16 ## for more details.                                                          ##
     17 ##                                                                            ##
     18 ## You should have received a copy of the GNU General Public License          ##
     19 ## along with this program;  if not, write to the Free Software Foundation,   ##
     20 ## Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA           ##
     21 ##                                                                            ##
     22 ################################################################################
     23 #
     24 # Creates, lists and extracts an plain, gzip and bzip tar archive.
     25 #
     26 
     27 TST_CNT=6
     28 TST_TESTFUNC=do_test
     29 TST_NEEDS_TMPDIR=1
     30 TST_NEEDS_CMDS="gzip bzip2"
     31 
     32 . tst_test.sh
     33 
     34 TAR_FILES="file1 file2 file3"
     35 
     36 check_listing()
     37 {
     38 	local i
     39 	local verbose=$1
     40 	shift
     41 
     42 	if [ -z "$verbose" ]; then
     43 		if [ -s tar.out ]; then
     44 			tst_res TFAIL "Tar produced unexpected output"
     45 			cat tar.out
     46 		else
     47 			tst_res TPASS "Tar produced no output"
     48 		fi
     49 
     50 		return
     51 	fi
     52 
     53 	if [ $(wc -l < tar.out) != $# ]; then
     54 		tst_res TFAIL "Unexpected number of lines in tar.out"
     55 		cat tar.out
     56 		return
     57 	fi
     58 
     59 	for i in $@; do
     60 		if ! grep -q $i tar.out; then
     61 			tst_res TFAIL "File $i missing in listing"
     62 			return
     63 		fi
     64 	done
     65 
     66 	tst_res TPASS "Listing in tar.out is correct"
     67 }
     68 
     69 check_content()
     70 {
     71 	local fname="$1"
     72 	local verbose="$2"
     73 	shift 2
     74 
     75 	EXPECT_PASS tar t${verbose}f "$fname" \> tar.out
     76 	check_listing v $@
     77 }
     78 
     79 check_files()
     80 {
     81 	for i in $@; do
     82 		if ! [ -f $i ]; then
     83 			tst_res TFAIL "Missing file $i in extracted archive"
     84 			cat tar.out
     85 			return
     86 		fi
     87 	done
     88 
     89 	tst_res TPASS "Files were uncompressed correctly"
     90 }
     91 
     92 check_extraction()
     93 {
     94 	local fname="$1"
     95 	local verbose="$2"
     96 	shift 2
     97 
     98 	EXPECT_PASS tar x${verbose}f $fname \> tar.out
     99 	check_listing "${verbose}" $@
    100 	check_files $@
    101 	ROD rm $@
    102 }
    103 
    104 test_tar()
    105 {
    106 	local comp="$1"
    107 	local verbose="$2"
    108 	local fname="$3"
    109 	local i
    110 
    111 	# Create archive
    112 	ROD touch $TAR_FILES
    113 	EXPECT_PASS tar c${verbose}f$comp $fname $TAR_FILES \> tar.out
    114 	check_listing "$verbose" $TAR_FILES
    115 
    116 	# Diff filesystem against the archive, should be the same at this point
    117 	EXPECT_PASS tar d${verbose}f $fname \> tar.out
    118 	check_listing "$verbose" $TAR_FILES
    119 
    120 	ROD rm $TAR_FILES
    121 
    122 	# Check content listing
    123 	check_content $fname "$verbose" $TAR_FILES
    124 
    125 	# Check decompression
    126 	check_extraction $fname "$verbose" $TAR_FILES
    127 
    128 	# Append to an archive, only possible for uncompressed archive
    129 	if [ -z "$comp" ]; then
    130 		ROD touch file4
    131 		EXPECT_PASS tar r${verbose}f $fname file4 \> tar.out
    132 		check_listing "$verbose" file4
    133 		check_content $fname "$verbose" $TAR_FILES file4
    134 		ROD rm file4
    135 
    136 		check_extraction $fname "$verbose" $TAR_FILES file4
    137 	fi
    138 
    139 	ROD rm $fname
    140 }
    141 
    142 do_test()
    143 {
    144 	case $1 in
    145 	1) test_tar ""  "v" "test.tar";;
    146 	2) test_tar "z" "v" "test.tar.gz";;
    147 	3) test_tar "j" "v" "test.tar.bz2";;
    148 	4) test_tar ""  ""  "test.tar";;
    149 	5) test_tar "z" ""  "test.tar.gz";;
    150 	6) test_tar "j" ""  "test.tar.bz2";;
    151 	esac
    152 }
    153 
    154 tst_run
    155