1#! /bin/sh
2# check script for Plzip - Massively parallel implementation of lzip
3# Copyright (C) 2009-2021 Antonio Diaz Diaz.
4#
5# This script is free software: you have unlimited permission
6# to copy, distribute, and modify it.
7
8LC_ALL=C
9export LC_ALL
10objdir=`pwd`
11testdir=`cd "$1" ; pwd`
12LZIP="${objdir}"/plzip
13framework_failure() { echo "failure in testing framework" ; exit 1 ; }
14
15if [ ! -f "${LZIP}" ] || [ ! -x "${LZIP}" ] ; then
16	echo "${LZIP}: cannot execute"
17	exit 1
18fi
19
20[ -e "${LZIP}" ] 2> /dev/null ||
21	{
22	echo "$0: a POSIX shell is required to run the tests"
23	echo "Try bash -c \"$0 $1 $2\""
24	exit 1
25	}
26
27if [ -d tmp ] ; then rm -rf tmp ; fi
28mkdir tmp
29cd "${objdir}"/tmp || framework_failure
30
31cat "${testdir}"/test.txt > in || framework_failure
32in_lz="${testdir}"/test.txt.lz
33in_em="${testdir}"/test_em.txt.lz
34fail=0
35lwarn8=0
36lwarn10=0
37test_failed() { fail=1 ; printf " $1" ; [ -z "$2" ] || printf "($2)" ; }
38lzlib_1_8() { [ ${lwarn8} = 0 ] &&
39	printf "\nwarning: header truncation detection requires lzlib 1.8 or newer"
40	lwarn8=1 ; }
41lzlib_1_10() { [ ${lwarn10} = 0 ] &&
42	printf "\nwarning: header HD=3 detection requires lzlib 1.10 or newer"
43	lwarn10=1 ; }
44
45"${LZIP}" --check-lib				# just print warning
46printf "testing plzip-%s..." "$2"
47
48"${LZIP}" -fkqm4 in
49[ $? = 1 ] || test_failed $LINENO
50[ ! -e in.lz ] || test_failed $LINENO
51"${LZIP}" -fkqm274 in
52[ $? = 1 ] || test_failed $LINENO
53[ ! -e in.lz ] || test_failed $LINENO
54for i in bad_size -1 0 4095 513MiB 1G 1T 1P 1E 1Z 1Y 10KB ; do
55	"${LZIP}" -fkqs $i in
56	[ $? = 1 ] || test_failed $LINENO $i
57	[ ! -e in.lz ] || test_failed $LINENO $i
58done
59"${LZIP}" -lq in
60[ $? = 2 ] || test_failed $LINENO
61"${LZIP}" -tq in
62[ $? = 2 ] || test_failed $LINENO
63"${LZIP}" -tq < in
64[ $? = 2 ] || test_failed $LINENO
65"${LZIP}" -cdq in
66[ $? = 2 ] || test_failed $LINENO
67"${LZIP}" -cdq < in
68[ $? = 2 ] || test_failed $LINENO
69"${LZIP}" -dq -o in < "${in_lz}"
70[ $? = 1 ] || test_failed $LINENO
71"${LZIP}" -dq -o in "${in_lz}"
72[ $? = 1 ] || test_failed $LINENO
73"${LZIP}" -dq -o out nx_file.lz
74[ $? = 1 ] || test_failed $LINENO
75[ ! -e out ] || test_failed $LINENO
76"${LZIP}" -q -o out.lz nx_file
77[ $? = 1 ] || test_failed $LINENO
78[ ! -e out.lz ] || test_failed $LINENO
79# these are for code coverage
80"${LZIP}" -lt "${in_lz}" 2> /dev/null
81[ $? = 1 ] || test_failed $LINENO
82"${LZIP}" -cdl "${in_lz}" > out 2> /dev/null
83[ $? = 1 ] || test_failed $LINENO
84"${LZIP}" -cdt "${in_lz}" > out 2> /dev/null
85[ $? = 1 ] || test_failed $LINENO
86"${LZIP}" -t -- nx_file.lz 2> /dev/null
87[ $? = 1 ] || test_failed $LINENO
88"${LZIP}" -t "" < /dev/null 2> /dev/null
89[ $? = 1 ] || test_failed $LINENO
90"${LZIP}" --help > /dev/null || test_failed $LINENO
91"${LZIP}" -n1 -V > /dev/null || test_failed $LINENO
92"${LZIP}" -m 2> /dev/null
93[ $? = 1 ] || test_failed $LINENO
94"${LZIP}" -z 2> /dev/null
95[ $? = 1 ] || test_failed $LINENO
96"${LZIP}" --bad_option 2> /dev/null
97[ $? = 1 ] || test_failed $LINENO
98"${LZIP}" --t 2> /dev/null
99[ $? = 1 ] || test_failed $LINENO
100"${LZIP}" --test=2 2> /dev/null
101[ $? = 1 ] || test_failed $LINENO
102"${LZIP}" --output= 2> /dev/null
103[ $? = 1 ] || test_failed $LINENO
104"${LZIP}" --output 2> /dev/null
105[ $? = 1 ] || test_failed $LINENO
106printf "LZIP\001-.............................." | "${LZIP}" -t 2> /dev/null
107printf "LZIP\002-.............................." | "${LZIP}" -t 2> /dev/null
108printf "LZIP\001+.............................." | "${LZIP}" -t 2> /dev/null
109
110printf "\ntesting decompression..."
111
112for i in "${in_lz}" "${in_em}" ; do
113	"${LZIP}" -lq "$i" || test_failed $LINENO "$i"
114	"${LZIP}" -t "$i" || test_failed $LINENO "$i"
115	"${LZIP}" -d "$i" -o copy || test_failed $LINENO "$i"
116	cmp in copy || test_failed $LINENO "$i"
117	"${LZIP}" -cd "$i" > copy || test_failed $LINENO "$i"
118	cmp in copy || test_failed $LINENO "$i"
119	"${LZIP}" -d "$i" -o - > copy || test_failed $LINENO "$i"
120	cmp in copy || test_failed $LINENO "$i"
121	"${LZIP}" -d < "$i" > copy || test_failed $LINENO "$i"
122	cmp in copy || test_failed $LINENO "$i"
123	rm -f copy || framework_failure
124done
125
126lines=$("${LZIP}" -tvv "${in_em}" 2>&1 | wc -l) || test_failed $LINENO
127[ "${lines}" -eq 1 ] || test_failed $LINENO "${lines}"
128
129lines=$("${LZIP}" -lvv "${in_em}" | wc -l) || test_failed $LINENO
130[ "${lines}" -eq 11 ] || test_failed $LINENO "${lines}"
131
132cat "${in_lz}" > copy.lz || framework_failure
133"${LZIP}" -dk copy.lz || test_failed $LINENO
134cmp in copy || test_failed $LINENO
135printf "to be overwritten" > copy || framework_failure
136"${LZIP}" -d copy.lz 2> /dev/null
137[ $? = 1 ] || test_failed $LINENO
138"${LZIP}" -df copy.lz || test_failed $LINENO
139[ ! -e copy.lz ] || test_failed $LINENO
140cmp in copy || test_failed $LINENO
141
142printf "to be overwritten" > copy || framework_failure
143"${LZIP}" -df -o copy < "${in_lz}" || test_failed $LINENO
144cmp in copy || test_failed $LINENO
145rm -f out copy || framework_failure
146"${LZIP}" -d -o ./- "${in_lz}" || test_failed $LINENO
147cmp in ./- || test_failed $LINENO
148rm -f ./- || framework_failure
149"${LZIP}" -d -o ./- < "${in_lz}" || test_failed $LINENO
150cmp in ./- || test_failed $LINENO
151rm -f ./- || framework_failure
152
153cat "${in_lz}" > anyothername || framework_failure
154"${LZIP}" -dv - anyothername - < "${in_lz}" > copy 2> /dev/null ||
155	test_failed $LINENO
156cmp in copy || test_failed $LINENO
157cmp in anyothername.out || test_failed $LINENO
158rm -f copy anyothername.out || framework_failure
159
160"${LZIP}" -lq in "${in_lz}"
161[ $? = 2 ] || test_failed $LINENO
162"${LZIP}" -lq nx_file.lz "${in_lz}"
163[ $? = 1 ] || test_failed $LINENO
164"${LZIP}" -tq in "${in_lz}"
165[ $? = 2 ] || test_failed $LINENO
166"${LZIP}" -tq nx_file.lz "${in_lz}"
167[ $? = 1 ] || test_failed $LINENO
168"${LZIP}" -cdq in "${in_lz}" > copy
169[ $? = 2 ] || test_failed $LINENO
170cat copy in | cmp in - || test_failed $LINENO
171"${LZIP}" -cdq nx_file.lz "${in_lz}" > copy
172[ $? = 1 ] || test_failed $LINENO
173cmp in copy || test_failed $LINENO
174rm -f copy || framework_failure
175cat "${in_lz}" > copy.lz || framework_failure
176for i in 1 2 3 4 5 6 7 ; do
177	printf "g" >> copy.lz || framework_failure
178	"${LZIP}" -alvv copy.lz "${in_lz}" > /dev/null 2>&1
179	[ $? = 2 ] || test_failed $LINENO $i
180	"${LZIP}" -atvvvv copy.lz "${in_lz}" 2> /dev/null
181	[ $? = 2 ] || test_failed $LINENO $i
182done
183"${LZIP}" -dq in copy.lz
184[ $? = 2 ] || test_failed $LINENO
185[ -e copy.lz ] || test_failed $LINENO
186[ ! -e copy ] || test_failed $LINENO
187[ ! -e in.out ] || test_failed $LINENO
188"${LZIP}" -dq nx_file.lz copy.lz
189[ $? = 1 ] || test_failed $LINENO
190[ ! -e copy.lz ] || test_failed $LINENO
191[ ! -e nx_file ] || test_failed $LINENO
192cmp in copy || test_failed $LINENO
193
194cat in in > in2 || framework_failure
195"${LZIP}" -lq "${in_lz}" "${in_lz}" || test_failed $LINENO
196"${LZIP}" -t "${in_lz}" "${in_lz}" || test_failed $LINENO
197"${LZIP}" -cd "${in_lz}" "${in_lz}" -o out > copy2 || test_failed $LINENO
198[ ! -e out ] || test_failed $LINENO			# override -o
199cmp in2 copy2 || test_failed $LINENO
200rm -f copy2 || framework_failure
201"${LZIP}" -d "${in_lz}" "${in_lz}" -o copy2 || test_failed $LINENO
202cmp in2 copy2 || test_failed $LINENO
203rm -f copy2 || framework_failure
204
205cat "${in_lz}" "${in_lz}" > copy2.lz || framework_failure
206printf "\ngarbage" >> copy2.lz || framework_failure
207"${LZIP}" -tvvvv copy2.lz 2> /dev/null || test_failed $LINENO
208"${LZIP}" -alq copy2.lz
209[ $? = 2 ] || test_failed $LINENO
210"${LZIP}" -atq copy2.lz
211[ $? = 2 ] || test_failed $LINENO
212"${LZIP}" -atq < copy2.lz
213[ $? = 2 ] || test_failed $LINENO
214"${LZIP}" -adkq copy2.lz
215[ $? = 2 ] || test_failed $LINENO
216[ ! -e copy2 ] || test_failed $LINENO
217"${LZIP}" -adkq -o copy2 < copy2.lz
218[ $? = 2 ] || test_failed $LINENO
219[ ! -e copy2 ] || test_failed $LINENO
220printf "to be overwritten" > copy2 || framework_failure
221"${LZIP}" -df copy2.lz || test_failed $LINENO
222cmp in2 copy2 || test_failed $LINENO
223rm -f in2 copy2 || framework_failure
224
225printf "\ntesting   compression..."
226
227"${LZIP}" -cf "${in_lz}" > out 2> /dev/null	# /dev/null is a tty on OS/2
228[ $? = 1 ] || test_failed $LINENO
229"${LZIP}" -Fvvm36 -o - "${in_lz}" > out 2> /dev/null || test_failed $LINENO
230"${LZIP}" -cd out | "${LZIP}" -d > copy || test_failed $LINENO
231cmp in copy || test_failed $LINENO
232
233"${LZIP}" -0 -o ./- in || test_failed $LINENO
234"${LZIP}" -cd ./- | cmp in - || test_failed $LINENO
235rm -f ./- || framework_failure
236"${LZIP}" -0 -o ./- < in || test_failed $LINENO		# add .lz
237[ ! -e ./- ] || test_failed $LINENO
238"${LZIP}" -cd -- -.lz | cmp in - || test_failed $LINENO
239rm -f ./-.lz || framework_failure
240
241for i in s4Ki 0 1 2 3 4 5 6 7 8 9 ; do
242	"${LZIP}" -k -$i in || test_failed $LINENO $i
243	mv -f in.lz copy.lz || test_failed $LINENO $i
244	printf "garbage" >> copy.lz || framework_failure
245	"${LZIP}" -df copy.lz || test_failed $LINENO $i
246	cmp in copy || test_failed $LINENO $i
247
248	"${LZIP}" -$i in -c > out || test_failed $LINENO $i
249	"${LZIP}" -$i in -o o_out || test_failed $LINENO $i	# don't add .lz
250	[ ! -e o_out.lz ] || test_failed $LINENO
251	cmp out o_out || test_failed $LINENO $i
252	rm -f o_out || framework_failure
253	printf "g" >> out || framework_failure
254	"${LZIP}" -cd out > copy || test_failed $LINENO $i
255	cmp in copy || test_failed $LINENO $i
256
257	"${LZIP}" -$i < in > out || test_failed $LINENO $i
258	"${LZIP}" -d < out > copy || test_failed $LINENO $i
259	cmp in copy || test_failed $LINENO $i
260
261	rm -f out || framework_failure
262	printf "to be overwritten" > out.lz || framework_failure
263	"${LZIP}" -f -$i -o out < in || test_failed $LINENO $i	# add .lz
264	[ ! -e out ] || test_failed $LINENO
265	"${LZIP}" -df -o copy < out.lz || test_failed $LINENO $i
266	cmp in copy || test_failed $LINENO $i
267done
268rm -f out out.lz || framework_failure
269
270cat in in in in > in4 || framework_failure
271for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ; do
272	"${LZIP}" -s4Ki -B8Ki -n$i < in4 > out4.lz || test_failed $LINENO $i
273	printf "g" >> out4.lz || framework_failure
274	"${LZIP}" -d -n$i < out4.lz > out4 || test_failed $LINENO $i
275	cmp in4 out4 || test_failed $LINENO $i
276	"${LZIP}" -d --in-slots=$i < out4.lz > out4 || test_failed $LINENO $i
277	cmp in4 out4 || test_failed $LINENO $i
278	"${LZIP}" -d --out-slots=$i < out4.lz > out4 || test_failed $LINENO $i
279	cmp in4 out4 || test_failed $LINENO $i
280
281	"${LZIP}" -c -s4Ki -B8Ki -n$i in4 > out4.lz || test_failed $LINENO $i
282	printf "g" >> out4.lz || framework_failure
283	"${LZIP}" -cd -n$i out4.lz > out4 || test_failed $LINENO $i
284	cmp in4 out4 || test_failed $LINENO $i
285	"${LZIP}" -cd --out-slots=$i out4.lz > out4 || test_failed $LINENO $i
286	cmp in4 out4 || test_failed $LINENO $i
287	rm -f out4 || framework_failure
288	"${LZIP}" -d -n$i out4.lz || test_failed $LINENO $i
289	cmp in4 out4 || test_failed $LINENO $i
290done
291rm -f out4 || framework_failure
292
293cat in in in in in in in in | "${LZIP}" -1s4Ki | "${LZIP}" -t ||
294	test_failed $LINENO
295
296printf "\ntesting bad input..."
297
298headers='LZIp LZiP LZip LzIP LzIp LziP lZIP lZIp lZiP lzIP'
299body='\001\014\000\203\377\373\377\377\300\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000$\000\000\000\000\000\000\000'
300cat "${in_lz}" > int.lz
301printf "LZIP${body}" >> int.lz
302if "${LZIP}" -tq int.lz ; then
303	for header in ${headers} ; do
304		printf "${header}${body}" > int.lz	# first member
305		"${LZIP}" -lq int.lz
306		[ $? = 2 ] || test_failed $LINENO ${header}
307		"${LZIP}" -tq int.lz
308		[ $? = 2 ] || test_failed $LINENO ${header}
309		"${LZIP}" -tq < int.lz
310		[ $? = 2 ] || test_failed $LINENO ${header}
311		"${LZIP}" -cdq int.lz > /dev/null
312		[ $? = 2 ] || test_failed $LINENO ${header}
313		"${LZIP}" -lq --loose-trailing int.lz
314		[ $? = 2 ] || test_failed $LINENO ${header}
315		"${LZIP}" -tq --loose-trailing int.lz
316		[ $? = 2 ] || test_failed $LINENO ${header}
317		"${LZIP}" -tq --loose-trailing < int.lz
318		[ $? = 2 ] || test_failed $LINENO ${header}
319		"${LZIP}" -cdq --loose-trailing int.lz > /dev/null
320		[ $? = 2 ] || test_failed $LINENO ${header}
321		cat "${in_lz}" > int.lz
322		printf "${header}${body}" >> int.lz	# trailing data
323		"${LZIP}" -lq int.lz
324		[ $? = 2 ] || test_failed $LINENO ${header}
325		"${LZIP}" -tq int.lz
326		[ $? = 2 ] || test_failed $LINENO ${header}
327		"${LZIP}" -tq < int.lz
328		[ $? = 2 ] || lzlib_1_10		# requires lzlib 1.10
329		"${LZIP}" -cdq int.lz > /dev/null
330		[ $? = 2 ] || test_failed $LINENO ${header}
331		"${LZIP}" -lq --loose-trailing int.lz ||
332			test_failed $LINENO ${header}
333		"${LZIP}" -t --loose-trailing int.lz ||
334			test_failed $LINENO ${header}
335		"${LZIP}" -t --loose-trailing < int.lz ||
336			test_failed $LINENO ${header}
337		"${LZIP}" -cd --loose-trailing int.lz > /dev/null ||
338			test_failed $LINENO ${header}
339		"${LZIP}" -lq --loose-trailing --trailing-error int.lz
340		[ $? = 2 ] || test_failed $LINENO ${header}
341		"${LZIP}" -tq --loose-trailing --trailing-error int.lz
342		[ $? = 2 ] || test_failed $LINENO ${header}
343		"${LZIP}" -tq --loose-trailing --trailing-error < int.lz
344		[ $? = 2 ] || test_failed $LINENO ${header}
345		"${LZIP}" -cdq --loose-trailing --trailing-error int.lz > /dev/null
346		[ $? = 2 ] || test_failed $LINENO ${header}
347	done
348else
349	printf "\nwarning: skipping header test: 'printf' does not work on your system."
350fi
351rm -f int.lz || framework_failure
352
353for i in fox_v2.lz fox_s11.lz fox_de20.lz \
354         fox_bcrc.lz fox_crc0.lz fox_das46.lz fox_mes81.lz ; do
355	"${LZIP}" -tq "${testdir}"/$i
356	[ $? = 2 ] || test_failed $LINENO $i
357done
358
359cat "${in_lz}" "${in_lz}" > in2.lz || framework_failure
360cat "${in_lz}" "${in_lz}" "${in_lz}" > in3.lz || framework_failure
361if dd if=in3.lz of=trunc.lz bs=14752 count=1 2> /dev/null &&
362   [ -e trunc.lz ] && cmp in2.lz trunc.lz > /dev/null 2>&1 ; then
363	for i in 6 20 14734 14753 14754 14755 14756 14757 14758 ; do
364		dd if=in3.lz of=trunc.lz bs=$i count=1 2> /dev/null
365		"${LZIP}" -lq trunc.lz
366		[ $? = 2 ] || test_failed $LINENO $i
367		"${LZIP}" -tq trunc.lz
368		[ $? = 2 ] || test_failed $LINENO $i
369		"${LZIP}" -tq < trunc.lz
370		[ $? = 2 ] || lzlib_1_8			# requires lzlib 1.8
371		"${LZIP}" -cdq trunc.lz > out
372		[ $? = 2 ] || test_failed $LINENO $i
373		"${LZIP}" -dq < trunc.lz > out
374		[ $? = 2 ] || lzlib_1_8			# requires lzlib 1.8
375	done
376else
377	printf "\nwarning: skipping truncation test: 'dd' does not work on your system."
378fi
379rm -f in2.lz in3.lz trunc.lz out || framework_failure
380
381cat "${in_lz}" > ingin.lz || framework_failure
382printf "g" >> ingin.lz || framework_failure
383cat "${in_lz}" >> ingin.lz || framework_failure
384"${LZIP}" -lq ingin.lz
385[ $? = 2 ] || test_failed $LINENO
386"${LZIP}" -atq ingin.lz
387[ $? = 2 ] || test_failed $LINENO
388"${LZIP}" -atq < ingin.lz
389[ $? = 2 ] || test_failed $LINENO
390"${LZIP}" -acdq ingin.lz > out
391[ $? = 2 ] || test_failed $LINENO
392"${LZIP}" -adq < ingin.lz > out
393[ $? = 2 ] || test_failed $LINENO
394"${LZIP}" -tq ingin.lz
395[ $? = 2 ] || test_failed $LINENO
396"${LZIP}" -t < ingin.lz || test_failed $LINENO
397"${LZIP}" -cdq ingin.lz > copy
398[ $? = 2 ] || test_failed $LINENO
399"${LZIP}" -d < ingin.lz > copy || test_failed $LINENO
400cmp in copy || test_failed $LINENO
401rm -f copy ingin.lz out || framework_failure
402
403echo
404if [ ${fail} = 0 ] ; then
405	echo "tests completed successfully."
406	cd "${objdir}" && rm -r tmp
407else
408	echo "tests failed."
409fi
410exit ${fail}
411