The fifth batch
[git.git] / t / t1060-object-corruption.sh
blobbc89371f53436dbf3427f231f560ef1b1f8683f6
1 #!/bin/sh
3 test_description='see how we handle various forms of corruption'
4 . ./test-lib.sh
6 # convert "1234abcd" to ".git/objects/12/34abcd"
7 obj_to_file() {
8 echo "$(git rev-parse --git-dir)/objects/$(git rev-parse "$1" | sed 's,..,&/,')"
11 # Convert byte at offset "$2" of object "$1" into '\0'
12 corrupt_byte() {
13 obj_file=$(obj_to_file "$1") &&
14 chmod +w "$obj_file" &&
15 printf '\0' | dd of="$obj_file" bs=1 seek="$2" conv=notrunc
18 test_expect_success 'setup corrupt repo' '
19 git init bit-error &&
21 cd bit-error &&
22 test_commit content &&
23 corrupt_byte HEAD:content.t 10
24 ) &&
25 git init no-bit-error &&
27 # distinct commit from bit-error, but containing a
28 # non-corrupted version of the same blob
29 cd no-bit-error &&
30 test_tick &&
31 test_commit content
35 test_expect_success 'setup repo with missing object' '
36 git init missing &&
38 cd missing &&
39 test_commit content &&
40 rm -f "$(obj_to_file HEAD:content.t)"
44 test_expect_success 'setup repo with misnamed object' '
45 git init misnamed &&
47 cd misnamed &&
48 test_commit content &&
49 good=$(obj_to_file HEAD:content.t) &&
50 blob=$(echo corrupt | git hash-object -w --stdin) &&
51 bad=$(obj_to_file $blob) &&
52 rm -f "$good" &&
53 mv "$bad" "$good"
57 test_expect_success 'streaming a corrupt blob fails' '
59 cd bit-error &&
60 test_must_fail git cat-file blob HEAD:content.t
64 test_expect_success 'getting type of a corrupt blob fails' '
66 cd bit-error &&
67 test_must_fail git cat-file -s HEAD:content.t
71 test_expect_success 'read-tree -u detects bit-errors in blobs' '
73 cd bit-error &&
74 rm -f content.t &&
75 test_must_fail git read-tree --reset -u HEAD
79 test_expect_success 'read-tree -u detects missing objects' '
81 cd missing &&
82 rm -f content.t &&
83 test_must_fail git read-tree --reset -u HEAD
87 # We use --bare to make sure that the transport detects it, not the checkout
88 # phase.
89 test_expect_success 'clone --no-local --bare detects corruption' '
90 test_must_fail git clone --no-local --bare bit-error corrupt-transport
93 test_expect_success 'clone --no-local --bare detects missing object' '
94 test_must_fail git clone --no-local --bare missing missing-transport
97 test_expect_success 'clone --no-local --bare detects misnamed object' '
98 test_must_fail git clone --no-local --bare misnamed misnamed-transport
101 # We do not expect --local to detect corruption at the transport layer,
102 # so we are really checking the checkout() code path.
103 test_expect_success 'clone --local detects corruption' '
104 test_must_fail git clone --local bit-error corrupt-checkout
107 test_expect_success 'error detected during checkout leaves repo intact' '
108 test_path_is_dir corrupt-checkout/.git
111 test_expect_success 'clone --local detects missing objects' '
112 test_must_fail git clone --local missing missing-checkout
115 test_expect_failure 'clone --local detects misnamed objects' '
116 test_must_fail git clone --local misnamed misnamed-checkout
119 test_expect_success 'fetch into corrupted repo with index-pack' '
120 cp -R bit-error bit-error-cp &&
121 test_when_finished "rm -rf bit-error-cp" &&
123 cd bit-error-cp &&
124 test_must_fail git -c transfer.unpackLimit=1 \
125 fetch ../no-bit-error 2>stderr &&
126 test_i18ngrep ! -i collision stderr
130 test_expect_success 'internal tree objects are not "missing"' '
131 git init missing-empty &&
133 cd missing-empty &&
134 empty_tree=$(git hash-object -t tree /dev/null) &&
135 commit=$(echo foo | git commit-tree $empty_tree) &&
136 git rev-list --objects $commit
140 test_done