blob: b67bcf187391ecd0958cd77ce6d87ba99b5b20bc [file] [log] [blame]
subrata_modakc8649602008-08-27 11:29:55 +00001#!/bin/sh
iyermanoj8117f0c2002-12-18 00:07:37 +00002################################################################################
3## ##
4## Copyright (c) International Business Machines Corp., 2001 ##
Cyril Hrubiscb008792016-10-24 15:23:52 +02005## Author: Manoj Iyer, manjo@mail.utexas.edu ##
6## Copyright (c) 2016 Cyril Hrubis <chrubis@suse.cz> ##
iyermanoj8117f0c2002-12-18 00:07:37 +00007## ##
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 ##
Cyril Hrubiscb008792016-10-24 15:23:52 +020019## along with this program; if not, write to the Free Software Foundation, ##
20## Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ##
subrata_modaka1fd64b2007-04-11 11:24:49 +000021## ##
iyermanoj8117f0c2002-12-18 00:07:37 +000022################################################################################
23#
Cyril Hrubiscb008792016-10-24 15:23:52 +020024# Creates, lists and extracts an plain, gzip and bzip tar archive.
iyermanoj8117f0c2002-12-18 00:07:37 +000025#
iyermanoj59df78432002-12-18 20:16:28 +000026
Cyril Hrubiscb008792016-10-24 15:23:52 +020027TST_ID="tar01"
28TST_CNT=6
29TST_TESTFUNC=do_test
30TST_NEEDS_TMPDIR=1
31. tst_test.sh
iyermanoj8117f0c2002-12-18 00:07:37 +000032
Cyril Hrubiscb008792016-10-24 15:23:52 +020033TAR_FILES="file1 file2 file3"
iyermanoj8117f0c2002-12-18 00:07:37 +000034
Cyril Hrubiscb008792016-10-24 15:23:52 +020035check_listing()
36{
37 local i
38 local verbose=$1
39 shift
iyermanoj8117f0c2002-12-18 00:07:37 +000040
Cyril Hrubiscb008792016-10-24 15:23:52 +020041 if [ -z "$verbose" ]; then
42 if [ -s tar.out ]; then
43 tst_res TFAIL "Tar produced unexpected output"
44 cat tar.out
iyermanoj8117f0c2002-12-18 00:07:37 +000045 else
Cyril Hrubiscb008792016-10-24 15:23:52 +020046 tst_res TPASS "Tar produced no output"
iyermanoj8117f0c2002-12-18 00:07:37 +000047 fi
Cyril Hrubiscb008792016-10-24 15:23:52 +020048
49 return
iyermanoj8117f0c2002-12-18 00:07:37 +000050 fi
iyermanoj8117f0c2002-12-18 00:07:37 +000051
Cyril Hrubiscb008792016-10-24 15:23:52 +020052 if [ $(wc -l < tar.out) != $# ]; then
53 tst_res TFAIL "Unexpected number of lines in tar.out"
54 cat tar.out
55 return
56 fi
iyermanoj8117f0c2002-12-18 00:07:37 +000057
Cyril Hrubiscb008792016-10-24 15:23:52 +020058 for i in $@; do
59 if ! grep -q $i tar.out; then
60 tst_res TFAIL "File $i missing in listing"
61 return
62 fi
63 done
iyermanoj8117f0c2002-12-18 00:07:37 +000064
Cyril Hrubiscb008792016-10-24 15:23:52 +020065 tst_res TPASS "Listing in tar.out is correct"
66}
iyermanojfcac63f2002-12-18 00:22:29 +000067
Cyril Hrubiscb008792016-10-24 15:23:52 +020068check_content()
69{
70 local fname="$1"
71 local verbose="$2"
72 shift 2
73
74 EXPECT_PASS tar t${verbose}f "$fname" \> tar.out
75 check_listing v $@
76}
77
78check_files()
79{
80 for i in $@; do
81 if ! [ -f $i ]; then
82 tst_res TFAIL "Missing file $i in extracted archive"
83 cat tar.out
84 return
85 fi
86 done
87
88 tst_res TPASS "Files were uncompressed correctly"
89}
90
91check_extraction()
92{
93 local fname="$1"
94 local verbose="$2"
95 shift 2
96
97 EXPECT_PASS tar x${verbose}f $fname \> tar.out
98 check_listing "${verbose}" $@
99 check_files $@
100 ROD rm $@
101}
102
103test_tar()
104{
105 local comp="$1"
106 local verbose="$2"
107 local fname="$3"
108 local i
109
110 # Create archive
111 ROD touch $TAR_FILES
112 EXPECT_PASS tar c${verbose}f$comp $fname $TAR_FILES \> tar.out
113 check_listing "$verbose" $TAR_FILES
114
115 # Diff filesystem against the archive, should be the same at this point
116 EXPECT_PASS tar d${verbose}f $fname \> tar.out
117 check_listing "$verbose" $TAR_FILES
118
119 ROD rm $TAR_FILES
120
121 # Check content listing
122 check_content $fname "$verbose" $TAR_FILES
123
124 # Check decompression
125 check_extraction $fname "$verbose" $TAR_FILES
126
127 # Append to an archive, only possible for uncompressed archive
128 if [ -z "$comp" ]; then
129 ROD touch file4
130 EXPECT_PASS tar r${verbose}f $fname file4 \> tar.out
131 check_listing "$verbose" file4
132 check_content $fname "$verbose" $TAR_FILES file4
133 ROD rm file4
134
135 check_extraction $fname "$verbose" $TAR_FILES file4
136 fi
137
138 ROD rm $fname
139}
140
141do_test()
142{
143 case $1 in
144 1) test_tar "" "v" "test.tar";;
145 2) test_tar "z" "v" "test.tar.gz";;
146 3) test_tar "j" "v" "test.tar.bz2";;
147 4) test_tar "" "" "test.tar";;
148 5) test_tar "z" "" "test.tar.gz";;
149 6) test_tar "j" "" "test.tar.bz2";;
150 esac
151}
152
153tst_run