aboutsummaryrefslogtreecommitdiff
path: root/testcases/commands/gzip/gzip_tests.sh
blob: 3262c555b376bdb21b77333c06117686f976e699 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (c) International Business Machines Corp., 2001
# Author: Manoj Iyer <manjo@mail.utexas.edu>
#
# Test basic functionality of gzip and gunzip command
# Test #1: Test that gzip -r will travel directories and
#  compress all the files available.
#
# Test #2: Test that gunzip -r will travel directories and
# uncompress all the files available.

TST_CNT=2
TST_TESTFUNC=test
TST_NEEDS_TMPDIR=1
TST_NEEDS_CMDS="gzip gunzip"

creat_dirnfiles()
{
	local numdirs=$2
	local numfiles=$3
	local dirname=$4
	local dircnt=0
	local fcnt=0

	tst_res TINFO "Test #$1: Creating $numdirs directories"
	tst_res TINFO "Test #$1: filling each dir with $numfiles files"
	while [ $dircnt -lt $numdirs ]; do
		dirname=$dirname/d.$dircnt
		ROD_SILENT mkdir -p $dirname

		fcnt=0
		while [ $fcnt -lt $numfiles ]; do
			ROD_SILENT touch $dirname/f.$fcnt
			fcnt=$(($fcnt+1))
		done
		dircnt=$(($dircnt+1))
	done
}

creat_expout()
{
	local numdir=$1
	local numfile=$2
	local dirname=$3
	local ext=$4
	local dircnt=0
	local fcnt=0

	echo "$dirname:"  1> tst_gzip.exp
	echo "d.$dircnt"  1>> tst_gzip.exp
	while [ $dircnt -lt $numdirs ]; do
		dirname=$dirname/d.$dircnt
		dircnt=$(($dircnt+1))
		echo "$dirname:"  1>> tst_gzip.exp
		if [ $dircnt -lt $numdirs ]; then
			echo "d.$dircnt"  1>> tst_gzip.exp
		fi
		fcnt=0
		while [ $fcnt -lt $numfiles ]; do
			echo "f.$fcnt$ext " 1>> tst_gzip.exp
			fcnt=$(($fcnt+1))
		done
		printf "\n\n" 1>> tst_gzip.exp
	done
}

test1()
{
	numdirs=10
	numfiles=10
	dircnt=0
	fcnt=0

	ROD_SILENT mkdir tst_gzip.tmp

	tst_res TINFO "Test #1: gzip -r will recursively compress contents" \
			"of directory"

	creat_dirnfiles 1 $numdirs $numfiles tst_gzip.tmp

	gzip -r tst_gzip.tmp > tst_gzip.err 2>&1
	if [ $? -ne 0 ]; then
		cat tst_gzip.err
		tst_res TFAIL "Test #1: gzip -r failed"
		return
	fi

	tst_res TINFO "Test #1: creating output file"
	ls -R tst_gzip.tmp > tst_gzip.out 2>&1

	tst_res TINFO "Test #1: creating expected output file"
	creat_expout $numdirs $numfiles tst_gzip.tmp .gz

	diff -w -B tst_gzip.out tst_gzip.exp > tst_gzip.err 2>&1
	if [ $? -ne 0 ]; then
		cat tst_gzip.err
		tst_res TFAIL "Test #1: gzip failed"
	else
		tst_res TPASS "Test #1: gzip -r success"
	fi

	ROD_SILENT rm -rf tst_gzip.tmp/
}

test2()
{
	numdirs=10
	numfiles=10
	dircnt=0
	fcnt=0

	ROD_SILENT mkdir tst_gzip.tmp

	tst_res TINFO "Test #2: gunzip -r will recursively uncompress" \
			"contents of directory"

	creat_dirnfiles 2 $numdirs $numfiles tst_gzip.tmp

	gzip -r tst_gzip.tmp > tst_gzip.err 2>&1
	if [ $? -ne 0 ]; then
		cat tst_gzip.err
		tst_brk TBROK "Test #2: compressing directory tst_gzip.tmp" \
				"failed"
	fi

	gunzip -r tst_gzip.tmp > tst_gzip.err 2>&1
	if [ $? -ne 0 ]; then
		cat tst_gzip.err
		tst_brk TBROK "Test #2: uncompressing directory" \
				" tst_gzip.tmp failed"
	fi

	tst_res TINFO "Test #2: creating output file"
	ls -R tst_gzip.tmp > tst_gzip.out 2>&1

	tst_res TINFO "Test #2: creating expected output file"
	creat_expout $numdirs $numfiles tst_gzip.tmp

	tst_res TINFO "Test #2: comparing expected out and actual output file"
	diff -w -B tst_gzip.out tst_gzip.exp > tst_gzip.err 2>&1
	if [ $? -ne 0 ]; then
		cat tst_gzip.err
		tst_res TFAIL "Test #2: gunzip failed"
	else
		tst_res TPASS "Test #2: gunzip -r success"
	fi

	ROD_SILENT rm -rf tst_gzip.tmp/
}

. tst_test.sh
tst_run