[PATCH bzip2-tests] Test bzip2recover works on valid .bz2 and doesn't crash on bad files

Mark Wielaard mark@klomp.org
Wed Jun 3 23:04:24 GMT 2026


From: Mark Wielaard <mjw@redhat.com>

Run bzip2recover on all good .bz2 files (except empty.bz2). For all
bad files check that bzip2recover exits normally and doesn't crash.
---
 README       |  6 +++++
 run-tests.sh | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 80 insertions(+), 2 deletions(-)

diff --git a/README b/README
index f684c24d93cf..1cff8a1bfe16 100644
--- a/README
+++ b/README
@@ -25,6 +25,8 @@ They can then be checked again with:
   md5sum --check file.md5 < file
 
 Note do NOT name a file ending in .testfilecopy or .testfilecopy.bz2.
+Also do NOT name a file starting with rec0[0-9]+ and ending in .bz2,
+as created by the bzip2recover utility.
 Those will automatically be cleaned by up the testframework.
 
 There is a simple bash script to run the tests:
@@ -46,6 +48,10 @@ default mode and small mode. The bzip2 binary is expected to
 return either 1 or 2 as exit status. Any other exit code is
 interpreted as failure.
 
+The bzip2recover utility is run on all .bz2 files to check the
+blocks can be recovered. For all .bz2.bad files bzip2recover
+should try to recover any blocks and should exit gracefully.
+
 If you just want to check a directory (and any subdirectories)
 full of (known good) .bz2 files you can invoke the script as:
 
diff --git a/run-tests.sh b/run-tests.sh
index 3b80fdd90900..e5b1aa26ac9b 100755
--- a/run-tests.sh
+++ b/run-tests.sh
@@ -1,7 +1,7 @@
 #!/bin/bash
 # A simple testframe runner for the bzip2 test suite.
 #
-# Copyright (C) 2019, Mark J. Wielaard  <mark@klomp.org>
+# Copyright (C) 2019, 2026 Mark J. Wielaard  <mark@klomp.org>
 #
 # This file is free software.  You can redistribute it and/or modify
 # it under the terms of the GNU General Public License (GPL); either
@@ -10,6 +10,7 @@
 VALGRIND="valgrind"
 VALGRIND_ARGS="-q --error-exitcode=9"
 BZIP2="bzip2"
+BZIP2RECOVER="bzip2recover"
 TESTS_DIR="."
 IGNORE_MD5=0
 
@@ -18,6 +19,7 @@ do
 case $i in
     --bzip2=*)
     BZIP2="${i#*=}"
+    BZIP2RECOVER="${BZIP2}recover"
     shift
     ;;
     --without-valgrind)
@@ -60,6 +62,10 @@ fi
 # Remove any left over tesfilecopies from previous runs first.
 find ${TESTS_DIR} -type f -name \*\.testfilecopy\.bz2 -exec rm \{\} \;
 
+# Remove any left over recovered files from previous runs (start with rec0)
+find ${TESTS_DIR} -type f -name rec0\[0-9\]\[0-9\]\*\.bz2 -exec rm \{\} \;
+
+
 # First test the good bz2 files.
 echo "Testing decompression and recompression..."
 echo
@@ -229,7 +235,73 @@ else
 fi
 echo
 
-results=$[${nogood}+${badresults}]
+# Test good bz2 files can be recovered
+echo "Testing recovering bzip2 files..."
+echo
+
+badrecover=()
+
+norecover=0
+while IFS= read -r -d '' bzfile; do
+  # Recover (good files, should work)
+  # bzip2recover should have a -q option
+  ${VALGRIND} ${VALGRIND_ARGS} ${BZIP2RECOVER} ${bzfile} > /dev/null 2>&1 \
+    && { echo "PASS: ${bzfile} Recover"; } \
+    || { echo "FAIL: ${bzfile} Recover";
+         badrecover=("${badrecover[@]}" $"${bzfile} bad recover result")
+         norecover=$[${norecover}+1]; continue; }
+  dn=$(dirname ${bzfile})
+  bn=$(basename ${bzfile})
+  rm -f "${dn}"/rec0*"${bn}"
+done < <(find ${TESTS_DIR} -type f -name \*\.bz2 -print0 \
+	 | grep -z -v empty.bz2) # bzip2recover doesn't handle valid empty.bz2
+echo
+
+# Try to recover bad inputs.
+# Might not work, but should not crash (or give valgrind errors)
+echo "Testing recovering of bad input data..."
+echo
+
+badrec=0
+badrecs=()
+while IFS= read -r -d '' badfile; do
+
+  ${VALGRIND} ${VALGRIND_ARGS} ${BZIP2RECOVER} ${badfile} > /dev/null 2>&1
+  ret=$?
+
+  # Assume we can recover (exit zero) or not (exit one).
+  # A crash or valgrind issue will be reported with something else.
+  if [[ ${ret} != 0 ]] && [[ ${ret} != 1 ]]; then
+    echo "FAIL: Bad recover for ${badfile}"
+    badrec=$[${badrec}+1]
+    badrecs=("${badrecs[@]}" $"${badfile} bad recover")
+    continue
+  else
+    echo "PASS: No crash recovering ${badfile}"
+  fi
+  # note .bad doesn't end in bz2 so bzip2recover adds .bz2
+  dn=$(dirname ${badfile})
+  bn=$(basename ${badfile})
+  rm -f "${dn}"/rec0*"${bn}".bz2
+done < <(find ${TESTS_DIR} -type f -name \*\.bz2.bad -print0)
+
+badrecover=$[${norecover}+${badrec}]
+echo
+
+if [[ ${badrecover} -eq 0 ]]; then
+  echo "bzip2recover recovered what it could."
+else
+  if [[ ${norecover} -ne 0 ]]; then
+    echo "!!! ${norecover} good bz2 files couldn't be recovered."
+  fi
+  if [[ ${badrec} -ne 0 ]]; then
+    echo "!!! ${badrec} bad bz2 files caused baddness during recovering."
+  fi
+fi
+
+echo
+
+results=$[${nogood}+${badresults}+${badrecover}]
 if [[ ${results} -eq 0 ]]; then
   echo "All tests passed"
   exit 0
-- 
2.54.0



More information about the Bzip2-devel mailing list