t1450: avoid use of "sed" on the index, which is a binary file

The previous step added a path zzzzzzzz to the index, and then used "sed" to replace this string to yyyyyyyy to create a test case where the checksum at the end of the file does not match the contents. Unfortunately, use of "sed" on a non-text file is not portable. Instead, use a Perl script that seeks to the end and modifies the last byte of the file (where we _know_ stores the trailing checksum). Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff Hostetler committed Apr 25, 2017 at 18:41 UTC 4d9bc37fbe9902fc033573cf7375b528fc6a434e
1 file changed +26 -8
t/t1450-fsck.sh
+26 -8
@@ -689,17 +689,35 @@ test_expect_success 'bogus head does not fallback to all heads' '
689 ! grep $blob out
690 '
691
692 +# Corrupt the checksum on the index.
693 +# Add 1 to the last byte in the SHA.
694 +corrupt_index_checksum () {
695 + perl -w -e '
696 + use Fcntl ":seek";
697 + open my $fh, "+<", ".git/index" or die "open: $!";
698 + binmode $fh;
699 + seek $fh, -1, SEEK_END or die "seek: $!";
700 + read $fh, my $in_byte, 1 or die "read: $!";
701 +
702 + $in_value = unpack("C", $in_byte);
703 + $out_value = ($in_value + 1) & 255;
704 +
705 + $out_byte = pack("C", $out_value);
706 +
707 + seek $fh, -1, SEEK_END or die "seek: $!";
708 + print $fh $out_byte;
709 + close $fh or die "close: $!";
710 + '
711 +}
712 +
713 +# Corrupt the checksum on the index and then
714 +# verify that only fsck notices.
715 test_expect_success 'detect corrupt index file in fsck' '
716 cp .git/index .git/index.backup &&
717 test_when_finished "mv .git/index.backup .git/index" &&
695 - echo zzzzzzzz >zzzzzzzz &&
696 - git add zzzzzzzz &&
697 - sed -e "s/zzzzzzzz/yyyyyyyy/" .git/index >.git/index.yyy &&
698 - mv .git/index.yyy .git/index &&
699 - # Confirm that fsck detects invalid checksum
700 - test_must_fail git fsck --cache &&
701 - # Confirm that status no longer complains about invalid checksum
702 - git status
718 + corrupt_index_checksum &&
719 + test_must_fail git fsck --cache 2>errors &&
720 + grep "bad index file" errors
721 '
722
723 test_done