Skip to content

Commit fb55bed

Browse files
committed
tests: overwrite bytes in files using a perl script instead of dd
The dd in my build environment on Windows crashes unpredictably. Work it around by rewriting most instances with a helper function that uses perl behind the scenes. Signed-off-by: Johannes Sixt <j6t@kdbg.org>
1 parent 8d86dd9 commit fb55bed

6 files changed

Lines changed: 26 additions & 9 deletions

t/t1060-object-corruption.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ obj_to_file() {
1313
corrupt_byte() {
1414
obj_file=$(obj_to_file "$1") &&
1515
chmod +w "$obj_file" &&
16-
printf '\0' | dd of="$obj_file" bs=1 seek="$2" conv=notrunc
16+
printf '\0' | test_overwrite_bytes "$obj_file" "$2"
1717
}
1818

1919
test_expect_success 'setup corrupt repo' '

t/t5300-pack-object.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ test_expect_success 'verify-pack catches mismatched .idx and .pack files' '
290290

291291
test_expect_success 'verify-pack catches a corrupted pack signature' '
292292
cat test-1-${packname_1}.pack >test-3.pack &&
293-
echo | dd of=test-3.pack count=1 bs=1 conv=notrunc seek=2 &&
293+
echo | test_overwrite_bytes test-3.pack 2 &&
294294
if git verify-pack test-3.idx
295295
then false
296296
else :;
@@ -299,7 +299,7 @@ test_expect_success 'verify-pack catches a corrupted pack signature' '
299299

300300
test_expect_success 'verify-pack catches a corrupted pack version' '
301301
cat test-1-${packname_1}.pack >test-3.pack &&
302-
echo | dd of=test-3.pack count=1 bs=1 conv=notrunc seek=7 &&
302+
echo | test_overwrite_bytes test-3.pack 7 &&
303303
if git verify-pack test-3.idx
304304
then false
305305
else :;
@@ -308,7 +308,7 @@ test_expect_success 'verify-pack catches a corrupted pack version' '
308308

309309
test_expect_success 'verify-pack catches a corrupted type/size of the 1st packed object data' '
310310
cat test-1-${packname_1}.pack >test-3.pack &&
311-
echo | dd of=test-3.pack count=1 bs=1 conv=notrunc seek=12 &&
311+
echo | test_overwrite_bytes test-3.pack 12 &&
312312
if git verify-pack test-3.idx
313313
then false
314314
else :;
@@ -319,7 +319,7 @@ test_expect_success 'verify-pack catches a corrupted sum of the index file itsel
319319
l=$(wc -c <test-3.idx) &&
320320
l=$(expr $l - 20) &&
321321
cat test-1-${packname_1}.pack >test-3.pack &&
322-
printf "%20s" "" | dd of=test-3.idx count=20 bs=1 conv=notrunc seek=$l &&
322+
printf "%20s" "" | test_overwrite_bytes test-3.idx $l &&
323323
if git verify-pack test-3.pack
324324
then false
325325
else :;

t/t5302-pack-index.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,9 @@ test_expect_success \
236236
obj=$(git hash-object file_001) &&
237237
nr=$(index_obj_nr ".git/objects/pack/pack-${pack1}.idx" $obj) &&
238238
chmod +w ".git/objects/pack/pack-${pack1}.idx" &&
239-
printf xxxx | dd of=".git/objects/pack/pack-${pack1}.idx" conv=notrunc \
240-
bs=1 count=4 seek=$((8 + 256 * 4 + $(wc -l <obj-list) * rawsz + $nr * 4)) &&
239+
printf xxxx |
240+
test_overwrite_bytes ".git/objects/pack/pack-${pack1}.idx" \
241+
$((8 + 256 * 4 + $(wc -l <obj-list) * rawsz + $nr * 4)) &&
241242
( while read obj
242243
do git cat-file -p $obj >/dev/null || exit 1
243244
done <obj-list ) &&

t/t5303-pack-corruption-resilience.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ do_corrupt_object() {
5757
ofs=$(git show-index < ${pack}.idx | grep $1 | cut -f1 -d" ") &&
5858
ofs=$(($ofs + $2)) &&
5959
chmod +w ${pack}.pack &&
60-
dd of=${pack}.pack bs=1 conv=notrunc seek=$ofs &&
60+
test_overwrite_bytes "${pack}.pack" "$ofs" &&
6161
test_must_fail git verify-pack ${pack}.pack
6262
}
6363

t/t5313-pack-bounds-checks.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ do_pack () {
2828
}
2929

3030
munge () {
31-
printf "$3" | dd of="$1" bs=1 conv=notrunc seek=$2
31+
printf "$3" | test_overwrite_bytes "$1" "$2"
3232
}
3333

3434
# Offset in a v2 .idx to its initial and extended offset tables. For an index

t/test-lib-functions.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,22 @@ test_skip_or_die () {
16061606
error "$2"
16071607
}
16081608

1609+
# Overwrite bytes at an offset in a file
1610+
# $1 ... the file to modify
1611+
# $2 ... byte offset into file
1612+
# stdin ... new bytes
1613+
test_overwrite_bytes () {
1614+
perl -e '
1615+
$fname = shift @ARGV;
1616+
$offset = shift @ARGV;
1617+
$bytes = <>;
1618+
open my $fh, "+<", $fname or die "open $fname: $!\n";
1619+
seek($fh, $offset, 0) or die "seek $fname: $!\n";
1620+
syswrite($fh, $bytes) or die "write $fname: $!\n";
1621+
close $fh or die "close $fname: $!\n";
1622+
' "$@"
1623+
}
1624+
16091625
# Like "env FOO=BAR some-program", but run inside a subshell, which means
16101626
# it also works for shell functions (though those functions cannot impact
16111627
# the environment outside of the test_env invocation).

0 commit comments

Comments
 (0)