diff --git a/tests/chroot-extractors b/tests/chroot-extractors index 628819495..42ec93c9c 100755 --- a/tests/chroot-extractors +++ b/tests/chroot-extractors @@ -6,8 +6,12 @@ # stand-in unzip reads it with Python's zipfile, and a stand-in debugfs # fails the copy of build.prop as real debugfs does on a full work # directory (an error message, an empty file, exit 0). Checked: each tool -# then says build.prop can't be read, not that the image has none. -# XVC=path and XFP=path run other copies of the tools (a negative control). +# then says build.prop can't be read, not that the image has none, and +# passes on debugfs's own last line, the reason; a failed copy where +# debugfs said nothing but its banner gets no reason. With mkfs.ext4 and +# debugfs on the host, the same on real ext4 images: one without +# build.prop, and one cut short. XVC=path and XFP=path run other copies of +# the tools (a negative control). set -euo pipefail cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." XVC=$(realpath -- "${XVC:-tools/extract-vendor-chroot}") @@ -18,7 +22,7 @@ pass() { printf 'ok - %s\n' "$1"; } t=$(mktemp -d) trap 'rm -rf "$t"' EXIT -mkdir -p "$t/bin" +mkdir -p "$t/bin" "$t/fake" cat > "$t/bin/unzip" <<'EOF' #!/bin/bash # unzip -Z1 ZIP (list) or unzip -p ZIP MEMBER (to stdout) @@ -31,38 +35,75 @@ with zipfile.ZipFile(sys.argv[2]) as z: sys.stdout.buffer.write(z.read(sys.argv[3])) ' "$@" EOF -cat > "$t/bin/debugfs" <<'EOF' +cat > "$t/fake/debugfs" <<'EOF' #!/bin/bash # debugfs -R "dump /build.prop OUT" IMG on a full disk: real debugfs's -# messages, an empty OUT, exit 0 +# messages, an empty OUT, exit 0; DFS_REASON="" prints only the banner read -r _ _ out <<<"$2" out=${out#\"}; out=${out%\"}; out=${out//\"\"/\"} # debugfs's quoting : > "$out" echo "debugfs 1.47.2 (stand-in)" >&2 -echo "dump: No space left on device while writing file" >&2 +[[ -z ${DFS_REASON-x} ]] || echo "${DFS_REASON-dump: No space left on device while writing file}" >&2 exit 0 EOF -chmod +x "$t/bin/unzip" "$t/bin/debugfs" +chmod +x "$t/bin/unzip" "$t/fake/debugfs" head -c 4096 /dev/zero > "$t/ext4.img" printf '\x53\xef' | dd of="$t/ext4.img" bs=1 seek=1080 conv=notrunc status=none -python3 -I -c ' +# zipimg ZIP VENDOR SYSTEM: an image zip with those two images in it +zipimg() { + python3 -I -c ' import sys, zipfile with zipfile.ZipFile(sys.argv[1], "w") as z: - for p in ("vendor.img", "system.img"): - z.write(sys.argv[2], p) -' "$t/image-cheetah-fictional.zip" "$t/ext4.img" + z.write(sys.argv[2], "vendor.img") + z.write(sys.argv[3], "system.img") +' "$@" +} +zipimg "$t/image-cheetah-fictional.zip" "$t/ext4.img" "$t/ext4.img" + +# refused NAME ZIP LINE: both tools refuse ZIP, and their error line is LINE +# Compare the error bytes, including any trailing blanks from debugfs. +refused() { + local tool name args + for tool in "$XVC" "$XFP"; do + name=${tool##*/} + args=() + [[ $tool != "$XVC" ]] || args=(-p gnss) + rm -rf "$t/work" "$t/out" + if PATH=$t/bin:$PATH "$tool" "${args[@]}" -w "$t/work" -o "$t/out" "$2" > "$t/log" 2>&1; then + fail "$name: $1: refused" + elif [[ $(sed -n '/^error: /p' "$t/log") != "$3" ]]; then + cat "$t/log"; fail "$name: $1: says $3" + else + pass "$name: $1: refused, says $3" + fi + done +} +msg="error: build.prop can't be read from vendor.img" +PATH=$t/fake:$PATH refused "a build.prop whose copy fails" "$t/image-cheetah-fictional.zip" \ + "$msg: dump: No space left on device while writing file" +DFS_REASON="" PATH=$t/fake:$PATH refused "a failed copy with only debugfs's banner" \ + "$t/image-cheetah-fictional.zip" "$msg" -for tool in "$XVC" "$XFP"; do - name=${tool##*/} - args=() - [[ $tool != "$XVC" ]] || args=(-p gnss) - if PATH=$t/bin:$PATH "$tool" "${args[@]}" -w "$t/work" -o "$t/out" "$t/image-cheetah-fictional.zip" > "$t/log" 2>&1; then - fail "$name: a build.prop whose copy fails is refused" - elif ! grep -qF "error: build.prop can't be read from vendor.img" "$t/log"; then - cat "$t/log"; fail "$name: a build.prop whose copy fails: says it can't be read" - else - pass "$name: a build.prop whose copy fails: refused, says it can't be read" - fi -done +# --- Real ext4 images (made-up files), when the host can make them ------------ +mkdir -p "$t/v" "$t/s/system" +printf 'ro.product.vendor.device=cheetah\n' > "$t/v/build.prop" +if command -v debugfs >/dev/null && mkfs.ext4 -q -F -d "$t/s" "$t/system.img" 4M >/dev/null 2>&1 && + mkfs.ext4 -q -F -d "$t/v" "$t/vendor.img" 8M >/dev/null 2>&1 && + mkfs.ext4 -q -F -d "$t/s" "$t/noprop.img" 8M >/dev/null 2>&1; then + cp "$t/vendor.img" "$t/short.img"; truncate -s 1M "$t/short.img" + zipimg "$t/image-noprop.zip" "$t/noprop.img" "$t/system.img" + zipimg "$t/image-short.zip" "$t/short.img" "$t/system.img" + for image in noprop short; do + err=$(debugfs -R "dump /build.prop \"$t/probe.prop\"" "$t/$image.img" 2>&1 >/dev/null) + reason=${err##*$'\n'} + if [[ -z $reason || $reason == "debugfs "[0-9]* || -s $t/probe.prop ]]; then + fail "real ext4 $image: direct debugfs must fail to copy with a reason" + continue + fi + refused "real ext4 $image" "$t/image-$image.zip" "$msg: $reason" + done +else + echo "ok - # skip: no mkfs.ext4 -d and debugfs for the real ext4 checks" +fi ((failed == 0)) || { echo "$failed check(s) failed" >&2; exit 1; } diff --git a/tools/extract-fingerprint-vendor b/tools/extract-fingerprint-vendor index 09f498e99..a71862ec4 100755 --- a/tools/extract-fingerprint-vendor +++ b/tools/extract-fingerprint-vendor @@ -199,8 +199,12 @@ done # The vendor partition says which device it was built for. mkdir -p "$WORK/v" -debugfs -R "dump /build.prop $(dq "$WORK/v/build.prop")" "$WORK/vendor.img" 2>/dev/null -[[ -s "$WORK/v/build.prop" ]] || die "build.prop can't be read from vendor.img" +# debugfs exits 0 when the copy fails, and the last line it writes to stderr +# says why: kept in a variable, not a file in $WORK, which may be full +{ err=$(debugfs -R "dump /build.prop $(dq "$WORK/v/build.prop")" "$WORK/vendor.img" 2>&1 >&3); } 3>&1 +reason=${err##*$'\n'} +[[ $reason != "debugfs "[0-9]* ]] || reason="" # only its banner: no reason +[[ -s "$WORK/v/build.prop" ]] || die "build.prop can't be read from vendor.img${reason:+: $reason}" dev=$(sed -n 's/^ro.product.vendor.device=//p' "$WORK/v/build.prop" | head -1) [[ "$dev" == "$DEVICE" || -n "$FORCE" ]] || die "vendor partition is for '${dev:-unknown}', not $DEVICE; use -f to override" diff --git a/tools/extract-vendor-chroot b/tools/extract-vendor-chroot index 89f5d4695..b2b3f777d 100755 --- a/tools/extract-vendor-chroot +++ b/tools/extract-vendor-chroot @@ -295,8 +295,12 @@ done # The vendor partition says which device it was built for. mkdir -p "$WORK/v" -debugfs -R "dump /build.prop $(dq "$WORK/v/build.prop")" "$WORK/vendor.img" 2>/dev/null -[[ -s "$WORK/v/build.prop" ]] || die "build.prop can't be read from vendor.img" +# debugfs exits 0 when the copy fails, and the last line it writes to stderr +# says why: kept in a variable, not a file in $WORK, which may be full +{ err=$(debugfs -R "dump /build.prop $(dq "$WORK/v/build.prop")" "$WORK/vendor.img" 2>&1 >&3); } 3>&1 +reason=${err##*$'\n'} +[[ $reason != "debugfs "[0-9]* ]] || reason="" # only its banner: no reason +[[ -s "$WORK/v/build.prop" ]] || die "build.prop can't be read from vendor.img${reason:+: $reason}" dev=$(sed -n 's/^ro.product.vendor.device=//p' "$WORK/v/build.prop" | head -1) [[ "$dev" == "$DEVICE" || -n "$FORCE" ]] || die "vendor partition is for '${dev:-unknown}', not $DEVICE; use -f to override"