Gentoo Archives: gentoo-dev

From: "Michał Górny" <mgorny@g.o>
To: gentoo-dev@l.g.o
Cc: "Michał Górny" <mgorny@g.o>
Subject: [gentoo-dev] [PATCH 01/15] eclass/tests: Add tests for unpacker.eclass
Date: Sun, 25 Sep 2022 18:23:47
Message-Id: 20220925182317.1559529-2-mgorny@gentoo.org
In Reply to: [gentoo-dev] [PATCH 00/15] unpacker.eclass: Tests, bugfixes and GPKG support by "Michał Górny"
1 Signed-off-by: Michał Górny <mgorny@g.o>
2 ---
3 eclass/tests/tests-common.sh | 7 ++
4 eclass/tests/unpacker.sh | 233 +++++++++++++++++++++++++++++++++++
5 2 files changed, 240 insertions(+)
6 create mode 100755 eclass/tests/unpacker.sh
7
8 diff --git a/eclass/tests/tests-common.sh b/eclass/tests/tests-common.sh
9 index a677842b6ac5..45b1e20b933a 100644
10 --- a/eclass/tests/tests-common.sh
11 +++ b/eclass/tests/tests-common.sh
12 @@ -60,6 +60,13 @@ die() {
13 exit 1
14 }
15
16 +assert() {
17 + local x pipestatus=${PIPESTATUS[*]}
18 + for x in ${pipestatus} ; do
19 + [[ ${x} -eq 0 ]] || die "$@"
20 + done
21 +}
22 +
23 has_version() {
24 while [[ $1 == -* ]]; do
25 shift
26 diff --git a/eclass/tests/unpacker.sh b/eclass/tests/unpacker.sh
27 new file mode 100755
28 index 000000000000..af979b0e2995
29 --- /dev/null
30 +++ b/eclass/tests/unpacker.sh
31 @@ -0,0 +1,233 @@
32 +#!/bin/bash
33 +# Copyright 2022 Gentoo Authors
34 +# Distributed under the terms of the GNU General Public License v2
35 +
36 +EAPI=8
37 +
38 +source tests-common.sh || exit
39 +
40 +inherit unpacker
41 +
42 +# silence the output
43 +unpack_banner() { :; }
44 +
45 +TESTFILE=test.in
46 +TESTDIR=$(mktemp -d || die)
47 +trap 'cd - >/dev/null && rm -r "${TESTDIR}"' EXIT
48 +
49 +# prepare some test data
50 +# NB: we need something "compressible", as compress(1) will return
51 +# an error if the file "is larger than before compression"
52 +cp ../unpacker.eclass "${TESTDIR}/${TESTFILE}" || die
53 +cd "${TESTDIR}" || die
54 +
55 +test_unpack() {
56 + local archive=${1}
57 + local unpacked=${2}
58 + local deps=${3}
59 + local packcmd=${4}
60 +
61 + local x
62 + for x in ${deps}; do
63 + if ! type "${x}" &>/dev/null; then
64 + ewarn "Skipping ${archive}, tool ${x} not found"
65 + return
66 + fi
67 + done
68 +
69 + rm -rf testdir || die
70 + mkdir -p testdir || die
71 +
72 + tbegin "unpacking ${archive}"
73 + eval "${packcmd}"
74 + assert "packing ${archive} failed"
75 + cd testdir || die
76 + local out
77 + out=$(
78 + _unpacker "../${archive}" 2>&1
79 + )
80 + ret=$?
81 + if [[ ${ret} -eq 0 ]]; then
82 + if [[ ! -f ${unpacked} ]]; then
83 + eerror "${unpacked} not found after unpacking"
84 + ret=1
85 + elif ! diff -u "${unpacked}" "../${TESTFILE}"; then
86 + eerror "${unpacked} different than input"
87 + ret=1
88 + fi
89 + fi
90 + [[ ${ret} -ne 0 ]] && echo "${out}" >&2
91 + tend ${ret}
92 +
93 + cd .. || die
94 + rm -f "${archive}" || die
95 +}
96 +
97 +test_compressed_file() {
98 + local suffix=${1}
99 + local tool=${2}
100 +
101 + test_unpack "test${suffix}" test "${tool}" \
102 + "${tool} -c \${TESTFILE} > \${archive}"
103 +}
104 +
105 +test_compressed_file_multistream() {
106 + local suffix=${1}
107 + local tool=${2}
108 +
109 + test_unpack "test+multistream${suffix}" "test+multistream" "${tool}" \
110 + "head -n 300 \${TESTFILE} | ${tool} -c > \${archive} &&
111 + tail -n +301 \${TESTFILE} | ${tool} -c >> \${archive}"
112 +}
113 +
114 +test_compressed_file_with_junk() {
115 + local suffix=${1}
116 + local tool=${2}
117 + local flag=${3}
118 +
119 + test_unpack "test+junk${suffix}" "test+junk" "${tool}" \
120 + "${tool} -c \${TESTFILE} > \${archive} && cat test.in >> \${archive}"
121 +}
122 +
123 +test_compressed_tar() {
124 + local suffix=${1}
125 + local tool=${2}
126 +
127 + test_unpack "test${suffix}" test.in "tar ${tool}" \
128 + "tar -c \${TESTFILE} | ${tool} -c > \${archive}"
129 +}
130 +
131 +test_compressed_cpio() {
132 + local suffix=${1}
133 + local tool=${2}
134 +
135 + test_unpack "test${suffix}" test.in "cpio ${tool}" \
136 + "cpio -o --quiet <<<\${TESTFILE} | ${tool} -c > \${archive}"
137 +}
138 +
139 +create_deb() {
140 + local suffix=${1}
141 + local tool=${2}
142 + local archive=${3}
143 + local infile=${4}
144 +
145 + echo 2.0 > debian-binary || die
146 + : > control || die
147 + tar -cf control.tar control || die
148 + tar -c "${infile}" | ${tool} > "data.tar${suffix}"
149 + assert "packing data.tar${suffix} failed"
150 + ar r "${archive}" debian-binary control.tar "data.tar${suffix}" \
151 + 2>/dev/null || die
152 + rm -f control control.tar "data.tar${suffix}" debian-binary || die
153 +}
154 +
155 +test_deb() {
156 + local suffix=${1}
157 + local tool=${2}
158 + local tool_cmd
159 +
160 + if [[ -n ${tool} ]]; then
161 + tool_cmd="${tool} -c"
162 + else
163 + tool_cmd=cat
164 + fi
165 +
166 + test_unpack "test-${tool}_1.2.3_noarch.deb" test.in "ar tar ${tool}" \
167 + "create_deb '${suffix}' '${tool_cmd}' \${archive} \${TESTFILE}"
168 +}
169 +
170 +test_reject_junk() {
171 + local suffix=${1}
172 + local archive=test${1}
173 +
174 + rm -rf testdir || die
175 + mkdir -p testdir || die
176 +
177 + tbegin "rejecting junk named ${archive}"
178 + cat test.in >> "${archive}" || die
179 + cd testdir || die
180 + (
181 + # some decompressors (e.g. cpio) are very verbose about junk
182 + _unpacker "../${archive}" &>/dev/null
183 + )
184 + [[ $? -ne 0 ]]
185 + ret=$?
186 + tend ${ret}
187 +
188 + cd .. || die
189 + rm -f "${archive}" || die
190 +}
191 +
192 +test_compressed_file .bz2 bzip2
193 +test_compressed_file .Z compress
194 +test_compressed_file .gz gzip
195 +test_compressed_file .lzma lzma
196 +test_compressed_file .xz xz
197 +test_compressed_file .lz lzip
198 +test_compressed_file .zst zstd
199 +
200 +test_compressed_file_multistream .bz2 bzip2
201 +test_compressed_file_multistream .gz gzip
202 +test_compressed_file_multistream .xz xz
203 +test_compressed_file_multistream .lz lzip
204 +test_compressed_file_multistream .zst zstd
205 +
206 +test_compressed_file_with_junk .bz2 bzip2
207 +test_compressed_file_with_junk .lz lzip
208 +
209 +test_unpack test.tar test.in tar 'tar -cf ${archive} ${TESTFILE}'
210 +test_compressed_tar .tar.bz2 bzip2
211 +test_compressed_tar .tbz bzip2
212 +test_compressed_tar .tbz2 bzip2
213 +test_compressed_tar .tar.Z compress
214 +test_compressed_tar .tar.gz gzip
215 +test_compressed_tar .tgz gzip
216 +test_compressed_tar .tar.lzma lzma
217 +test_compressed_tar .tar.xz xz
218 +test_compressed_tar .txz xz
219 +test_compressed_tar .tar.lz lzip
220 +test_compressed_tar .tar.zst zstd
221 +
222 +test_unpack test.cpio test.in cpio 'cpio -o --quiet <<<${TESTFILE} > ${archive}'
223 +test_compressed_cpio .cpio.bz2 bzip2
224 +test_compressed_cpio .cpio.Z compress
225 +test_compressed_cpio .cpio.gz gzip
226 +test_compressed_cpio .cpio.lzma lzma
227 +test_compressed_cpio .cpio.xz xz
228 +test_compressed_cpio .cpio.lz lzip
229 +test_compressed_cpio .cpio.zst zstd
230 +
231 +test_deb
232 +test_deb .gz gzip
233 +test_deb .xz xz
234 +test_deb .bz2 bzip2
235 +test_deb .lzma lzma
236 +
237 +test_unpack test.zip test.in zip 'zip -q ${archive} ${TESTFILE}'
238 +# test handling non-adjusted zip with junk prepended
239 +test_unpack test.zip test.in zip \
240 + 'zip -q testdir/tmp.zip ${TESTFILE} && cat test.in testdir/tmp.zip > ${archive}'
241 +test_unpack test.7z test.in 7z '7z -bso0 a ${archive} ${TESTFILE}'
242 +test_unpack test.lha test.in lha 'lha a -q ${archive} ${TESTFILE}'
243 +test_unpack test.lzh test.in lha 'lha a -q ${archive} ${TESTFILE}'
244 +test_unpack test.rar test.in rar 'rar -idq a ${archive} ${TESTFILE}'
245 +
246 +# TODO: .run/.sh/.bin
247 +
248 +test_reject_junk .bz2
249 +test_reject_junk .Z
250 +test_reject_junk .gz
251 +test_reject_junk .lzma
252 +test_reject_junk .xz
253 +test_reject_junk .lz
254 +test_reject_junk .zst
255 +test_reject_junk .tar
256 +test_reject_junk .cpio
257 +test_reject_junk .deb
258 +test_reject_junk .zip
259 +test_reject_junk .7z
260 +test_reject_junk .rar
261 +test_reject_junk .lha
262 +test_reject_junk .lzh
263 +
264 +texit
265 --
266 2.37.3