Add test cases to t5318-commit-graph.sh that corrupt the commit-graph
file and check that the 'git commit-graph verify' command fails. These
tests verify the header and chunk information is checked carefully.

Helped-by: Martin Ågren <martin.ag...@gmail.com>
Signed-off-by: Derrick Stolee <dsto...@microsoft.com>
---
 t/t5318-commit-graph.sh | 53 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)

diff --git a/t/t5318-commit-graph.sh b/t/t5318-commit-graph.sh
index 6ca451dfd2..0cb88232fa 100755
--- a/t/t5318-commit-graph.sh
+++ b/t/t5318-commit-graph.sh
@@ -240,4 +240,57 @@ test_expect_success 'git commit-graph verify' '
        git commit-graph verify >output
 '
 
+# usage: corrupt_data <file> <pos> [<data>]
+corrupt_data() {
+       file=$1
+       pos=$2
+       data="${3:-\0}"
+       printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
+}
+
+test_expect_success 'detect bad signature' '
+       cd "$TRASH_DIRECTORY/full" &&
+       cp $objdir/info/commit-graph commit-graph-backup &&
+       test_when_finished mv commit-graph-backup $objdir/info/commit-graph &&
+       corrupt_data $objdir/info/commit-graph 0 "\0" &&
+       test_must_fail git commit-graph verify 2>err &&
+       grep -v "^\+" err > verify-errors &&
+       test_line_count = 1 verify-errors &&
+       grep "graph signature" verify-errors
+'
+
+test_expect_success 'detect bad version number' '
+       cd "$TRASH_DIRECTORY/full" &&
+       cp $objdir/info/commit-graph commit-graph-backup &&
+       test_when_finished mv commit-graph-backup $objdir/info/commit-graph &&
+       corrupt_data $objdir/info/commit-graph 4 "\02" &&
+       test_must_fail git commit-graph verify 2>err &&
+       grep -v "^\+" err > verify-errors &&
+       test_line_count = 1 verify-errors &&
+       grep "graph version" verify-errors
+'
+
+test_expect_success 'detect bad hash version' '
+       cd "$TRASH_DIRECTORY/full" &&
+       cp $objdir/info/commit-graph commit-graph-backup &&
+       test_when_finished mv commit-graph-backup $objdir/info/commit-graph &&
+       corrupt_data $objdir/info/commit-graph 5 "\02" &&
+       test_must_fail git commit-graph verify 2>err &&
+       grep -v "^\+" err > verify-errors &&
+       test_line_count = 1 verify-errors &&
+       grep "hash version" verify-errors
+'
+
+test_expect_success 'detect too small chunk-count' '
+       cd "$TRASH_DIRECTORY/full" &&
+       cp $objdir/info/commit-graph commit-graph-backup &&
+       test_when_finished mv commit-graph-backup $objdir/info/commit-graph &&
+       corrupt_data $objdir/info/commit-graph 6 "\01" &&
+       test_must_fail git commit-graph verify 2>err &&
+       grep -v "^\+" err > verify-errors &&
+       test_line_count = 2 verify-errors &&
+       grep "missing the OID Lookup chunk" verify-errors &&
+       grep "missing the Commit Data chunk" verify-errors
+'
+
 test_done
-- 
2.16.2.329.gfb62395de6

Reply via email to