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