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# Author: Manoj Iyer <manjo@mail.utexas.edu> 6# 7# Creates, lists and extracts an plain, gzip and bzip tar archive. 8 9TST_CNT=6 10TST_TESTFUNC=do_test 11TST_NEEDS_TMPDIR=1 12TST_NEEDS_CMDS="gzip bzip2" 13 14. tst_test.sh 15 16TAR_FILES="file1 file2 file3" 17 18check_listing() 19{ 20 local i 21 local verbose=$1 22 shift 23 24 if [ -z "$verbose" ]; then 25 if [ -s tar.out ]; then 26 tst_res TFAIL "Tar produced unexpected output" 27 cat tar.out 28 else 29 tst_res TPASS "Tar produced no output" 30 fi 31 32 return 33 fi 34 35 if [ $(wc -l < tar.out) != $# ]; then 36 tst_res TFAIL "Unexpected number of lines in tar.out" 37 cat tar.out 38 return 39 fi 40 41 for i in $@; do 42 if ! grep -q $i tar.out; then 43 tst_res TFAIL "File $i missing in listing" 44 return 45 fi 46 done 47 48 tst_res TPASS "Listing in tar.out is correct" 49} 50 51check_content() 52{ 53 local fname="$1" 54 local verbose="$2" 55 shift 2 56 57 EXPECT_PASS tar t${verbose}f "$fname" \> tar.out 58 check_listing v $@ 59} 60 61check_files() 62{ 63 for i in $@; do 64 if ! [ -f $i ]; then 65 tst_res TFAIL "Missing file $i in extracted archive" 66 cat tar.out 67 return 68 fi 69 done 70 71 tst_res TPASS "Files were uncompressed correctly" 72} 73 74check_extraction() 75{ 76 local fname="$1" 77 local verbose="$2" 78 shift 2 79 80 EXPECT_PASS tar x${verbose}f $fname \> tar.out 81 check_listing "${verbose}" $@ 82 check_files $@ 83 ROD rm $@ 84} 85 86test_tar() 87{ 88 local comp="$1" 89 local verbose="$2" 90 local fname="$3" 91 local i 92 93 # Create archive 94 ROD touch $TAR_FILES 95 EXPECT_PASS tar c${verbose}f$comp $fname $TAR_FILES \> tar.out 96 check_listing "$verbose" $TAR_FILES 97 98 # Diff filesystem against the archive, should be the same at this point 99 EXPECT_PASS tar d${verbose}f $fname \> tar.out 100 check_listing "$verbose" $TAR_FILES 101 102 ROD rm $TAR_FILES 103 104 # Check content listing 105 check_content $fname "$verbose" $TAR_FILES 106 107 # Check decompression 108 check_extraction $fname "$verbose" $TAR_FILES 109 110 # Append to an archive, only possible for uncompressed archive 111 if [ -z "$comp" ]; then 112 ROD touch file4 113 EXPECT_PASS tar r${verbose}f $fname file4 \> tar.out 114 check_listing "$verbose" file4 115 check_content $fname "$verbose" $TAR_FILES file4 116 ROD rm file4 117 118 check_extraction $fname "$verbose" $TAR_FILES file4 119 fi 120 121 ROD rm $fname 122} 123 124do_test() 125{ 126 case $1 in 127 1) test_tar "" "v" "test.tar";; 128 2) test_tar "z" "v" "test.tar.gz";; 129 3) test_tar "j" "v" "test.tar.bz2";; 130 4) test_tar "" "" "test.tar";; 131 5) test_tar "z" "" "test.tar.gz";; 132 6) test_tar "j" "" "test.tar.bz2";; 133 esac 134} 135 136tst_run 137