Windows: set gitexecdir = $(bindir)
[git/mingw/4msysgit/wingit-dll.git] / t / t5303-pack-corruption-resilience.sh
blob874c6fe705caebbae1a2b9ba186eb81dfa74d93c
1 #!/bin/sh
3 # Copyright (c) 2008 Nicolas Pitre
6 test_description='resilience to pack corruptions with redundant objects'
7 . ./test-lib.sh
9 # Note: the test objects are created with knowledge of their pack encoding
10 # to ensure good code path coverage, and to facilitate direct alteration
11 # later on. The assumed characteristics are:
13 # 1) blob_2 is a delta with blob_1 for base and blob_3 is a delta with blob2
14 # for base, such that blob_3 delta depth is 2;
16 # 2) the bulk of object data is uncompressible so the text part remains
17 # visible;
19 # 3) object header is always 2 bytes.
21 create_test_files() {
22 test-genrandom "foo" 2000 > file_1 &&
23 test-genrandom "foo" 1800 > file_2 &&
24 test-genrandom "foo" 1800 > file_3 &&
25 echo " base " >> file_1 &&
26 echo " delta1 " >> file_2 &&
27 echo " delta delta2 " >> file_3 &&
28 test-genrandom "bar" 150 >> file_2 &&
29 test-genrandom "baz" 100 >> file_3
32 create_new_pack() {
33 rm -rf .git &&
34 git init &&
35 blob_1=`git hash-object -t blob -w file_1` &&
36 blob_2=`git hash-object -t blob -w file_2` &&
37 blob_3=`git hash-object -t blob -w file_3` &&
38 pack=`printf "$blob_1\n$blob_2\n$blob_3\n" |
39 git pack-objects $@ .git/objects/pack/pack` &&
40 pack=".git/objects/pack/pack-${pack}" &&
41 git verify-pack -v ${pack}.pack
44 zeros () {
45 while :; do
46 echo -n xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
47 done | tr x '\0'
50 do_corrupt_object() {
51 ofs=`git show-index < ${pack}.idx | grep $1 | cut -f1 -d" "` &&
52 ofs=$(($ofs + $2)) &&
53 chmod +w ${pack}.pack &&
54 zeros | dd of=${pack}.pack count=1 bs=1 conv=notrunc seek=$ofs &&
55 test_must_fail git verify-pack ${pack}.pack
58 test_expect_success \
59 'initial setup validation' \
60 'create_test_files &&
61 create_new_pack &&
62 git prune-packed &&
63 git cat-file blob $blob_1 > /dev/null &&
64 git cat-file blob $blob_2 > /dev/null &&
65 git cat-file blob $blob_3 > /dev/null'
67 test_expect_success \
68 'create corruption in header of first object' \
69 'do_corrupt_object $blob_1 0 &&
70 test_must_fail git cat-file blob $blob_1 > /dev/null &&
71 test_must_fail git cat-file blob $blob_2 > /dev/null &&
72 test_must_fail git cat-file blob $blob_3 > /dev/null'
74 test_expect_success \
75 '... but having a loose copy allows for full recovery' \
76 'mv ${pack}.idx tmp &&
77 git hash-object -t blob -w file_1 &&
78 mv tmp ${pack}.idx &&
79 git cat-file blob $blob_1 > /dev/null &&
80 git cat-file blob $blob_2 > /dev/null &&
81 git cat-file blob $blob_3 > /dev/null'
83 test_expect_success \
84 '... and loose copy of first delta allows for partial recovery' \
85 'git prune-packed &&
86 test_must_fail git cat-file blob $blob_2 > /dev/null &&
87 mv ${pack}.idx tmp &&
88 git hash-object -t blob -w file_2 &&
89 mv tmp ${pack}.idx &&
90 test_must_fail git cat-file blob $blob_1 > /dev/null &&
91 git cat-file blob $blob_2 > /dev/null &&
92 git cat-file blob $blob_3 > /dev/null'
94 test_expect_success \
95 'create corruption in data of first object' \
96 'create_new_pack &&
97 git prune-packed &&
98 chmod +w ${pack}.pack &&
99 perl -i.bak -pe "s/ base /abcdef/" ${pack}.pack &&
100 test_must_fail git cat-file blob $blob_1 > /dev/null &&
101 test_must_fail git cat-file blob $blob_2 > /dev/null &&
102 test_must_fail git cat-file blob $blob_3 > /dev/null'
104 test_expect_success \
105 '... but having a loose copy allows for full recovery' \
106 'mv ${pack}.idx tmp &&
107 git hash-object -t blob -w file_1 &&
108 mv tmp ${pack}.idx &&
109 git cat-file blob $blob_1 > /dev/null &&
110 git cat-file blob $blob_2 > /dev/null &&
111 git cat-file blob $blob_3 > /dev/null'
113 test_expect_success \
114 '... and loose copy of second object allows for partial recovery' \
115 'git prune-packed &&
116 test_must_fail git cat-file blob $blob_2 > /dev/null &&
117 mv ${pack}.idx tmp &&
118 git hash-object -t blob -w file_2 &&
119 mv tmp ${pack}.idx &&
120 test_must_fail git cat-file blob $blob_1 > /dev/null &&
121 git cat-file blob $blob_2 > /dev/null &&
122 git cat-file blob $blob_3 > /dev/null'
124 test_expect_success \
125 'create corruption in header of first delta' \
126 'create_new_pack &&
127 git prune-packed &&
128 do_corrupt_object $blob_2 0 &&
129 git cat-file blob $blob_1 > /dev/null &&
130 test_must_fail git cat-file blob $blob_2 > /dev/null &&
131 test_must_fail git cat-file blob $blob_3 > /dev/null'
133 test_expect_success \
134 '... but having a loose copy allows for full recovery' \
135 'mv ${pack}.idx tmp &&
136 git hash-object -t blob -w file_2 &&
137 mv tmp ${pack}.idx &&
138 git cat-file blob $blob_1 > /dev/null &&
139 git cat-file blob $blob_2 > /dev/null &&
140 git cat-file blob $blob_3 > /dev/null'
142 test_expect_success \
143 'create corruption in data of first delta' \
144 'create_new_pack &&
145 git prune-packed &&
146 chmod +w ${pack}.pack &&
147 perl -i.bak -pe "s/ delta1 /abcdefgh/" ${pack}.pack &&
148 git cat-file blob $blob_1 > /dev/null &&
149 test_must_fail git cat-file blob $blob_2 > /dev/null &&
150 test_must_fail git cat-file blob $blob_3 > /dev/null'
152 test_expect_success \
153 '... but having a loose copy allows for full recovery' \
154 'mv ${pack}.idx tmp &&
155 git hash-object -t blob -w file_2 &&
156 mv tmp ${pack}.idx &&
157 git cat-file blob $blob_1 > /dev/null &&
158 git cat-file blob $blob_2 > /dev/null &&
159 git cat-file blob $blob_3 > /dev/null'
161 test_expect_success \
162 'corruption in delta base reference of first delta (OBJ_REF_DELTA)' \
163 'create_new_pack &&
164 git prune-packed &&
165 do_corrupt_object $blob_2 2 &&
166 git cat-file blob $blob_1 > /dev/null &&
167 test_must_fail git cat-file blob $blob_2 > /dev/null &&
168 test_must_fail git cat-file blob $blob_3 > /dev/null'
170 test_expect_success \
171 '... but having a loose copy allows for full recovery' \
172 'mv ${pack}.idx tmp &&
173 git hash-object -t blob -w file_2 &&
174 mv tmp ${pack}.idx &&
175 git cat-file blob $blob_1 > /dev/null &&
176 git cat-file blob $blob_2 > /dev/null &&
177 git cat-file blob $blob_3 > /dev/null'
179 test_expect_success \
180 'corruption in delta base reference of first delta (OBJ_OFS_DELTA)' \
181 'create_new_pack --delta-base-offset &&
182 git prune-packed &&
183 do_corrupt_object $blob_2 2 &&
184 git cat-file blob $blob_1 > /dev/null &&
185 test_must_fail git cat-file blob $blob_2 > /dev/null &&
186 test_must_fail git cat-file blob $blob_3 > /dev/null'
188 test_expect_success \
189 '... and a redundant pack allows for full recovery too' \
190 'mv ${pack}.idx tmp &&
191 git hash-object -t blob -w file_1 &&
192 git hash-object -t blob -w file_2 &&
193 printf "$blob_1\n$blob_2\n" | git pack-objects .git/objects/pack/pack &&
194 git prune-packed &&
195 mv tmp ${pack}.idx &&
196 git cat-file blob $blob_1 > /dev/null &&
197 git cat-file blob $blob_2 > /dev/null &&
198 git cat-file blob $blob_3 > /dev/null'
200 test_done