Gentoo Archives: gentoo-commits

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