public inbox for gentoo-dev@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Michał Górny" <mgorny@gentoo.org>
To: gentoo-dev@lists.gentoo.org
Cc: "Michał Górny" <mgorny@gentoo.org>
Subject: [gentoo-dev] [PATCH 01/15] eclass/tests: Add tests for unpacker.eclass
Date: Sun, 25 Sep 2022 20:23:03 +0200	[thread overview]
Message-ID: <20220925182317.1559529-2-mgorny@gentoo.org> (raw)
In-Reply-To: <20220925182317.1559529-1-mgorny@gentoo.org>

Signed-off-by: Michał Górny <mgorny@gentoo.org>
---
 eclass/tests/tests-common.sh |   7 ++
 eclass/tests/unpacker.sh     | 233 +++++++++++++++++++++++++++++++++++
 2 files changed, 240 insertions(+)
 create mode 100755 eclass/tests/unpacker.sh

diff --git a/eclass/tests/tests-common.sh b/eclass/tests/tests-common.sh
index a677842b6ac5..45b1e20b933a 100644
--- a/eclass/tests/tests-common.sh
+++ b/eclass/tests/tests-common.sh
@@ -60,6 +60,13 @@ die() {
 	exit 1
 }
 
+assert() {
+	local x pipestatus=${PIPESTATUS[*]}
+	for x in ${pipestatus} ; do
+		[[ ${x} -eq 0 ]] || die "$@"
+	done
+}
+
 has_version() {
 	while [[ $1 == -* ]]; do
 		shift
diff --git a/eclass/tests/unpacker.sh b/eclass/tests/unpacker.sh
new file mode 100755
index 000000000000..af979b0e2995
--- /dev/null
+++ b/eclass/tests/unpacker.sh
@@ -0,0 +1,233 @@
+#!/bin/bash
+# Copyright 2022 Gentoo Authors
+# Distributed under the terms of the GNU General Public License v2
+
+EAPI=8
+
+source tests-common.sh || exit
+
+inherit unpacker
+
+# silence the output
+unpack_banner() { :; }
+
+TESTFILE=test.in
+TESTDIR=$(mktemp -d || die)
+trap 'cd - >/dev/null && rm -r "${TESTDIR}"' EXIT
+
+# prepare some test data
+# NB: we need something "compressible", as compress(1) will return
+# an error if the file "is larger than before compression"
+cp ../unpacker.eclass "${TESTDIR}/${TESTFILE}" || die
+cd "${TESTDIR}" || die
+
+test_unpack() {
+	local archive=${1}
+	local unpacked=${2}
+	local deps=${3}
+	local packcmd=${4}
+
+	local x
+	for x in ${deps}; do
+		if ! type "${x}" &>/dev/null; then
+			ewarn "Skipping ${archive}, tool ${x} not found"
+			return
+		fi
+	done
+
+	rm -rf testdir || die
+	mkdir -p testdir || die
+
+	tbegin "unpacking ${archive}"
+	eval "${packcmd}"
+	assert "packing ${archive} failed"
+	cd testdir || die
+	local out
+	out=$(
+		_unpacker "../${archive}" 2>&1
+	)
+	ret=$?
+	if [[ ${ret} -eq 0 ]]; then
+		if [[ ! -f ${unpacked} ]]; then
+			eerror "${unpacked} not found after unpacking"
+			ret=1
+		elif ! diff -u "${unpacked}" "../${TESTFILE}"; then
+			eerror "${unpacked} different than input"
+			ret=1
+		fi
+	fi
+	[[ ${ret} -ne 0 ]] && echo "${out}" >&2
+	tend ${ret}
+
+	cd .. || die
+	rm -f "${archive}" || die
+}
+
+test_compressed_file() {
+	local suffix=${1}
+	local tool=${2}
+
+	test_unpack "test${suffix}" test "${tool}" \
+		"${tool} -c \${TESTFILE} > \${archive}"
+}
+
+test_compressed_file_multistream() {
+	local suffix=${1}
+	local tool=${2}
+
+	test_unpack "test+multistream${suffix}" "test+multistream" "${tool}" \
+		"head -n 300 \${TESTFILE} | ${tool} -c > \${archive} &&
+		tail -n +301 \${TESTFILE} | ${tool} -c >> \${archive}"
+}
+
+test_compressed_file_with_junk() {
+	local suffix=${1}
+	local tool=${2}
+	local flag=${3}
+
+	test_unpack "test+junk${suffix}" "test+junk" "${tool}" \
+		"${tool} -c \${TESTFILE} > \${archive} && cat test.in >> \${archive}"
+}
+
+test_compressed_tar() {
+	local suffix=${1}
+	local tool=${2}
+
+	test_unpack "test${suffix}" test.in "tar ${tool}" \
+		"tar -c \${TESTFILE} | ${tool} -c > \${archive}"
+}
+
+test_compressed_cpio() {
+	local suffix=${1}
+	local tool=${2}
+
+	test_unpack "test${suffix}" test.in "cpio ${tool}" \
+		"cpio -o --quiet <<<\${TESTFILE} | ${tool} -c > \${archive}"
+}
+
+create_deb() {
+	local suffix=${1}
+	local tool=${2}
+	local archive=${3}
+	local infile=${4}
+
+	echo 2.0 > debian-binary || die
+	: > control || die
+	tar -cf control.tar control || die
+	tar -c "${infile}" | ${tool} > "data.tar${suffix}"
+	assert "packing data.tar${suffix} failed"
+	ar r "${archive}" debian-binary control.tar "data.tar${suffix}" \
+		2>/dev/null || die
+	rm -f control control.tar "data.tar${suffix}" debian-binary || die
+}
+
+test_deb() {
+	local suffix=${1}
+	local tool=${2}
+	local tool_cmd
+
+	if [[ -n ${tool} ]]; then
+		tool_cmd="${tool} -c"
+	else
+		tool_cmd=cat
+	fi
+
+	test_unpack "test-${tool}_1.2.3_noarch.deb" test.in "ar tar ${tool}" \
+		"create_deb '${suffix}' '${tool_cmd}' \${archive} \${TESTFILE}"
+}
+
+test_reject_junk() {
+	local suffix=${1}
+	local archive=test${1}
+
+	rm -rf testdir || die
+	mkdir -p testdir || die
+
+	tbegin "rejecting junk named ${archive}"
+	cat test.in >> "${archive}" || die
+	cd testdir || die
+	(
+		# some decompressors (e.g. cpio) are very verbose about junk
+		_unpacker "../${archive}" &>/dev/null
+	)
+	[[ $? -ne 0 ]]
+	ret=$?
+	tend ${ret}
+
+	cd .. || die
+	rm -f "${archive}" || die
+}
+
+test_compressed_file .bz2 bzip2
+test_compressed_file .Z compress
+test_compressed_file .gz gzip
+test_compressed_file .lzma lzma
+test_compressed_file .xz xz
+test_compressed_file .lz lzip
+test_compressed_file .zst zstd
+
+test_compressed_file_multistream .bz2 bzip2
+test_compressed_file_multistream .gz gzip
+test_compressed_file_multistream .xz xz
+test_compressed_file_multistream .lz lzip
+test_compressed_file_multistream .zst zstd
+
+test_compressed_file_with_junk .bz2 bzip2
+test_compressed_file_with_junk .lz lzip
+
+test_unpack test.tar test.in tar 'tar -cf ${archive} ${TESTFILE}'
+test_compressed_tar .tar.bz2 bzip2
+test_compressed_tar .tbz bzip2
+test_compressed_tar .tbz2 bzip2
+test_compressed_tar .tar.Z compress
+test_compressed_tar .tar.gz gzip
+test_compressed_tar .tgz gzip
+test_compressed_tar .tar.lzma lzma
+test_compressed_tar .tar.xz xz
+test_compressed_tar .txz xz
+test_compressed_tar .tar.lz lzip
+test_compressed_tar .tar.zst zstd
+
+test_unpack test.cpio test.in cpio 'cpio -o --quiet <<<${TESTFILE} > ${archive}'
+test_compressed_cpio .cpio.bz2 bzip2
+test_compressed_cpio .cpio.Z compress
+test_compressed_cpio .cpio.gz gzip
+test_compressed_cpio .cpio.lzma lzma
+test_compressed_cpio .cpio.xz xz
+test_compressed_cpio .cpio.lz lzip
+test_compressed_cpio .cpio.zst zstd
+
+test_deb
+test_deb .gz gzip
+test_deb .xz xz
+test_deb .bz2 bzip2
+test_deb .lzma lzma
+
+test_unpack test.zip test.in zip 'zip -q ${archive} ${TESTFILE}'
+# test handling non-adjusted zip with junk prepended
+test_unpack test.zip test.in zip \
+	'zip -q testdir/tmp.zip ${TESTFILE} && cat test.in testdir/tmp.zip > ${archive}'
+test_unpack test.7z test.in 7z '7z -bso0 a ${archive} ${TESTFILE}'
+test_unpack test.lha test.in lha 'lha a -q ${archive} ${TESTFILE}'
+test_unpack test.lzh test.in lha 'lha a -q ${archive} ${TESTFILE}'
+test_unpack test.rar test.in rar 'rar -idq a ${archive} ${TESTFILE}'
+
+# TODO: .run/.sh/.bin
+
+test_reject_junk .bz2
+test_reject_junk .Z
+test_reject_junk .gz
+test_reject_junk .lzma
+test_reject_junk .xz
+test_reject_junk .lz
+test_reject_junk .zst
+test_reject_junk .tar
+test_reject_junk .cpio
+test_reject_junk .deb
+test_reject_junk .zip
+test_reject_junk .7z
+test_reject_junk .rar
+test_reject_junk .lha
+test_reject_junk .lzh
+
+texit
-- 
2.37.3



  reply	other threads:[~2022-09-25 18:23 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-25 18:23 [gentoo-dev] [PATCH 00/15] unpacker.eclass: Tests, bugfixes and GPKG support Michał Górny
2022-09-25 18:23 ` Michał Górny [this message]
2022-09-25 18:23 ` [gentoo-dev] [PATCH 02/15] unpacker.eclass: Remove `-f` from zstd arguments Michał Górny
2022-09-25 18:23 ` [gentoo-dev] [PATCH 03/15] unpacker.eclass: Fix unpack_7z to respect the exit status Michał Górny
2022-09-25 18:23 ` [gentoo-dev] [PATCH 04/15] unpacker.eclass: Remove support for EAPI 5 Michał Górny
2022-09-25 18:23 ` [gentoo-dev] [PATCH 05/15] unpacker.eclass: Use bash substitution instead of tr for lowercase Michał Górny
2022-09-25 18:23 ` [gentoo-dev] [PATCH 06/15] unpacker.eclass: Use lowercase in unpacker_src_uri_depends Michał Górny
2022-09-25 21:04   ` John Helmert III
2022-09-25 21:06     ` John Helmert III
2022-09-25 18:23 ` [gentoo-dev] [PATCH 07/15] unpacker.eclass: Remove uppercase RAR/LHA variants Michał Górny
2022-09-25 18:23 ` [gentoo-dev] [PATCH 08/15] unpacker.eclass: Move decompressor recognition into a function Michał Górny
2022-09-25 18:23 ` [gentoo-dev] [PATCH 09/15] unpacker.eclass: Add support for .lz4 and .lzo compression Michał Górny
2022-09-25 18:23 ` [gentoo-dev] [PATCH 10/15] unpacker.eclass: Add on-the-fly .gpkg.tar unpacking support Michał Górny
2022-09-25 18:23 ` [gentoo-dev] [PATCH 11/15] sys-kernel/gentoo-kernel-bin: Use unpacker.eclass for .gpkg.tar Michał Górny
2022-09-25 18:23 ` [gentoo-dev] [PATCH 12/15] unpacker.eclass: decompress xz in parallel Michał Górny
2022-09-25 18:23 ` [gentoo-dev] [PATCH 13/15] unpacker.eclass: Support lbzip2 as parallel bz2 decompressor Michał Górny
2022-09-25 18:23 ` [gentoo-dev] [PATCH 14/15] unpacker.eclass: Fix handling GNU ar archives in hand-weaved impl Michał Górny
2022-09-25 18:23 ` [gentoo-dev] [PATCH 15/15] unpacker.eclass: Unpack .deb packages on-the-fly as well Michał Górny

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220925182317.1559529-2-mgorny@gentoo.org \
    --to=mgorny@gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox