summaryrefslogtreecommitdiffstats
path: root/t/t5303-pack-corruption-resilience.sh
blob: 2926e8dfc41223cc0030685beec8f6612db25a4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/bin/sh
#
# Copyright (c) 2008 Nicolas Pitre
#

test_description='resilience to pack corruptions with redundant objects'

TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh

# Note: the test objects are created with knowledge of their pack encoding
# to ensure good code path coverage, and to facilitate direct alteration
# later on.  The assumed characteristics are:
#
# 1) blob_2 is a delta with blob_1 for base and blob_3 is a delta with blob2
#    for base, such that blob_3 delta depth is 2;
#
# 2) the bulk of object data is uncompressible so the text part remains
#    visible;
#
# 3) object header is always 2 bytes.

create_test_files() {
    test-tool genrandom "foo" 2000 > file_1 &&
    test-tool genrandom "foo" 1800 > file_2 &&
    test-tool genrandom "foo" 1800 > file_3 &&
    echo " base " >> file_1 &&
    echo " delta1 " >> file_2 &&
    echo " delta delta2 " >> file_3 &&
    test-tool genrandom "bar" 150 >> file_2 &&
    test-tool genrandom "baz" 100 >> file_3
}

create_new_pack() {
    rm -rf .git &&
    git init &&
    blob_1=$(git hash-object -t blob -w file_1) &&
    blob_2=$(git hash-object -t blob -w file_2) &&
    blob_3=$(git hash-object -t blob -w file_3) &&
    pack=$(printf "$blob_1\n$blob_2\n$blob_3\n" |
          git pack-objects $@ .git/objects/pack/pack) &&
    pack=".git/objects/pack/pack-${pack}" &&
    git verify-pack -v ${pack}.pack
}

do_repack() {
    pack=$(printf "$blob_1\n$blob_2\n$blob_3\n" |
          git pack-objects $@ .git/objects/pack/pack) &&
    pack=".git/objects/pack/pack-${pack}"
}

do_corrupt_object() {
    ofs=$(git show-index < ${pack}.idx | grep $1 | cut -f1 -d" ") &&
    ofs=$(($ofs + $2)) &&
    chmod +w ${pack}.pack &&
    dd of=${pack}.pack bs=1 conv=notrunc seek=$ofs &&
    test_must_fail git verify-pack ${pack}.pack
}

printf '\0' > zero

test_expect_success \
    'initial setup validation' \
    'create_test_files &&
     create_new_pack &&
     git prune-packed &&
     git cat-file blob $blob_1 > /dev/null &&
     git cat-file blob $blob_2 > /dev/null &&
     git cat-file blob $blob_3 > /dev/null'

test_expect_success \
    'create corruption in header of first object' \
    'do_corrupt_object $blob_1 0 < zero &&
     test_must_fail git cat-file blob $blob_1 > /dev/null &&
     test_must_fail git cat-file blob $blob_2 > /dev/null &&
     test_must_fail git cat-file blob $blob_3 > /dev/null'

test_expect_success \
    '... but having a loose copy allows for full recovery' \
    'mv ${pack}.idx tmp &&
     git hash-object -t blob -w file_1 &&
     mv tmp ${pack}.idx &&
     git cat-file blob $blob_1 > /dev/null &&
     git cat-file blob $blob_2 > /dev/null &&
     git cat-file blob $blob_3 > /dev/null'

test_expect_success \
    '... and loose copy of first delta allows for partial recovery' \
    'git prune-packed &&
     test_must_fail git cat-file blob $blob_2 > /dev/null &&
     mv ${pack}.idx tmp &&
     git hash-object -t blob -w file_2 &&
     mv tmp ${pack}.idx &&
     test_must_fail git cat-file blob $blob_1 > /dev/null &&
     git cat-file blob $blob_2 > /dev/null &&
     git cat-file blob $blob_3 > /dev/null'

test_expect_success \
    'create corruption in data of first object' \
    'create_new_pack &&
     git prune-packed &&
     chmod +w ${pack}.pack &&
     perl -i.bak -pe "s/ base /abcdef/" ${pack}.pack &&
     test_must_fail git cat-file blob $blob_1 > /dev/null &&
     test_must_fail git cat-file blob $blob_2 > /dev/null &&
     test_must_fail git cat-file blob $blob_3 > /dev/null'

test_expect_success \
    '... but having a loose copy allows for full recovery' \
    'mv ${pack}.idx tmp &&
     git hash-object -t blob -w file_1 &&
     mv tmp ${pack}.idx &&
     git cat-file blob $blob_1 > /dev/null &&
     git cat-file blob $blob_2 > /dev/null &&
     git cat-file blob $blob_3 > /dev/null'

test_expect_success \
    '... and loose copy of second object allows for partial recovery' \
    'git prune-packed &&
     test_must_fail git cat-file blob $blob_2 > /dev/null &&
     mv ${pack}.idx tmp &&
     git hash-object -t blob -w file_2 &&
     mv tmp ${pack}.idx &&
     test_must_fail git cat-file blob $blob_1 > /dev/null &&
     git cat-file blob $blob_2 > /dev/null &&
     git cat-file blob $blob_3 > /dev/null'

test_expect_success \
    'create corruption in header of first delta' \
    'create_new_pack &&
     git prune-packed &&
     do_corrupt_object $blob_2 0 < zero &&
     git cat-file blob $blob_1 > /dev/null &&
     test_must_fail git cat-file blob $blob_2 > /dev/null &&
     test_must_fail git cat-file blob $blob_3 > /dev/null'

test_expect_success \
    '... but having a loose copy allows for full recovery' \
    'mv ${pack}.idx tmp &&
     git hash-object -t blob -w file_2 &&
     mv tmp ${pack}.idx &&
     git cat-file blob $blob_1 > /dev/null &&
     git cat-file blob $blob_2 > /dev/null &&
     git cat-file blob $blob_3 > /dev/null'

test_expect_success \
    '... and then a repack "clears" the corruption' \
    'do_repack &&
     git prune-packed &&
     git verify-pack ${pack}.pack &&
     git cat-file blob $blob_1 > /dev/null &&
     git cat-file blob $blob_2 > /dev/null &&
     git cat-file blob $blob_3 > /dev/null'

test_expect_success \
    'create corruption in data of first delta' \
    'create_new_pack &&
     git prune-packed &&
     chmod +w ${pack}.pack &&
     perl -i.bak -pe "s/ delta1 /abcdefgh/" ${pack}.pack &&
     git cat-file blob $blob_1 > /dev/null &&
     test_must_fail git cat-file blob $blob_2 > /dev/null &&
     test_must_fail git cat-file blob $blob_3 > /dev/null'

test_expect_success \
    '... but having a loose copy allows for full recovery' \
    'mv ${pack}.idx tmp &&
     git hash-object -t blob -w file_2 &&
     mv tmp ${pack}.idx &&
     git cat-file blob $blob_1 > /dev/null &&
     git cat-file blob $blob_2 > /dev/null &&
     git cat-file blob $blob_3 > /dev/null'

test_expect_success \
    '... and then a repack "clears" the corruption' \
    'do_repack &&
     git prune-packed &&
     git verify-pack ${pack}.pack &&
     git cat-file blob $blob_1 > /dev/null &&
     git cat-file blob $blob_2 > /dev/null &&
     git cat-file blob $blob_3 > /dev/null'

test_expect_success \
    'corruption in delta base reference of first delta (OBJ_REF_DELTA)' \
    'create_new_pack &&
     git prune-packed &&
     do_corrupt_object $blob_2 2 < zero &&
     git cat-file blob $blob_1 > /dev/null &&
     test_must_fail git cat-file blob $blob_2 > /dev/null &&
     test_must_fail git cat-file blob $blob_3 > /dev/null'

test_expect_success \
    '... but having a loose copy allows for full recovery' \
    'mv ${pack}.idx tmp &&
     git hash-object -t blob -w file_2 &&
     mv tmp ${pack}.idx &&
     git cat-file blob $blob_1 > /dev/null &&
     git cat-file blob $blob_2 > /dev/null &&
     git cat-file blob $blob_3 > /dev/null'

test_expect_success \
    '... and then a repack "clears" the corruption' \
    'do_repack &&
     git prune-packed &&
     git verify-pack ${pack}.pack &&
     git cat-file blob $blob_1 > /dev/null &&
     git cat-file blob $blob_2 > /dev/null &&
     git cat-file blob $blob_3 > /dev/null'

test_expect_success \
    'corruption #0 in delta base reference of first delta (OBJ_OFS_DELTA)' \
    'create_new_pack --delta-base-offset &&
     git prune-packed &&
     do_corrupt_object $blob_2 2 < zero &&
     git cat-file blob $blob_1 > /dev/null &&
     test_must_fail git cat-file blob $blob_2 > /dev/null &&
     test_must_fail git cat-file blob $blob_3 > /dev/null'

test_expect_success \
    '... but having a loose copy allows for full recovery' \
    'mv ${pack}.idx tmp &&
     git hash-object -t blob -w file_2 &&
     mv tmp ${pack}.idx &&
     git cat-file blob $blob_1 > /dev/null &&
     git cat-file blob $blob_2 > /dev/null &&
     git cat-file blob $blob_3 > /dev/null'

test_expect_success \
    '... and then a repack "clears" the corruption' \
    'do_repack --delta-base-offset &&
     git prune-packed &&
     git verify-pack ${pack}.pack &&
     git cat-file blob $blob_1 > /dev/null &&
     git cat-file blob $blob_2 > /dev/null &&
     git cat-file blob $blob_3 > /dev/null'

test_expect_success \
    'corruption #1 in delta base reference of first delta (OBJ_OFS_DELTA)' \
    'create_new_pack --delta-base-offset &&
     git prune-packed &&
     printf "\001" | do_corrupt_object $blob_2 2 &&
     git cat-file blob $blob_1 > /dev/null &&
     test_must_fail git cat-file blob $blob_2 > /dev/null &&
     test_must_fail git cat-file blob $blob_3 > /dev/null'

test_expect_success \
    '... but having a loose copy allows for full recovery' \
    'mv ${pack}.idx tmp &&
     git hash-object -t blob -w file_2 &&
     mv tmp ${pack}.idx &&
     git cat-file blob $blob_1 > /dev/null &&
     git cat-file blob $blob_2 > /dev/null &&
     git cat-file blob $blob_3 > /dev/null'

test_expect_success \
    '... and then a repack "clears" the corruption' \
    'do_repack --delta-base-offset &&
     git prune-packed &&
     git verify-pack ${pack}.pack &&
     git cat-file blob $blob_1 > /dev/null &&
     git cat-file blob $blob_2 > /dev/null &&
     git cat-file blob $blob_3 > /dev/null'

test_expect_success \
    '... and a redundant pack allows for full recovery too' \
    'do_corrupt_object $blob_2 2 < zero &&
     git cat-file blob $blob_1 > /dev/null &&
     test_must_fail git cat-file blob $blob_2 > /dev/null &&
     test_must_fail git cat-file blob $blob_3 > /dev/null &&
     mv ${pack}.idx tmp &&
     git hash-object -t blob -w file_1 &&
     git hash-object -t blob -w file_2 &&
     printf "$blob_1\n$blob_2\n" | git pack-objects .git/objects/pack/pack &&
     git prune-packed &&
     mv tmp ${pack}.idx &&
     git cat-file blob $blob_1 > /dev/null &&
     git cat-file blob $blob_2 > /dev/null &&
     git cat-file blob $blob_3 > /dev/null'

test_expect_success \
    'corruption of delta base reference pointing to wrong object' \
    'create_new_pack --delta-base-offset &&
     git prune-packed &&
     printf "\220\033" | do_corrupt_object $blob_3 2 &&
     git cat-file blob $blob_1 >/dev/null &&
     git cat-file blob $blob_2 >/dev/null &&
     test_must_fail git cat-file blob $blob_3 >/dev/null'

test_expect_success \
    '... but having a loose copy allows for full recovery' \
    'mv ${pack}.idx tmp &&
     git hash-object -t blob -w file_3 &&
     mv tmp ${pack}.idx &&
     git cat-file blob $blob_1 > /dev/null &&
     git cat-file blob $blob_2 > /dev/null &&
     git cat-file blob $blob_3 > /dev/null'

test_expect_success \
    '... and then a repack "clears" the corruption' \
    'do_repack --delta-base-offset --no-reuse-delta &&
     git prune-packed &&
     git verify-pack ${pack}.pack &&
     git cat-file blob $blob_1 > /dev/null &&
     git cat-file blob $blob_2 > /dev/null &&
     git cat-file blob $blob_3 > /dev/null'

test_expect_success \
    'corrupting header to have too small output buffer fails unpack' \
    'create_new_pack &&
     git prune-packed &&
     printf "\262\001" | do_corrupt_object $blob_1 0 &&
     test_must_fail git cat-file blob $blob_1 > /dev/null &&
     test_must_fail git cat-file blob $blob_2 > /dev/null &&
     test_must_fail git cat-file blob $blob_3 > /dev/null'

# \0 - empty base
# \1 - one byte in result
# \1 - one literal byte (X)
test_expect_success \
    'apply good minimal delta' \
    'printf "\0\1\1X" > minimal_delta &&
     test-tool delta -p /dev/null minimal_delta /dev/null'

# \0 - empty base
# \1 - 1 byte in result
# \2 - two literal bytes (one too many)
test_expect_success \
    'apply delta with too many literal bytes' \
    'printf "\0\1\2XX" > too_big_literal &&
     test_must_fail test-tool delta -p /dev/null too_big_literal /dev/null'

# \4 - four bytes in base
# \1 - one byte in result
# \221 - copy, one byte offset, one byte size
#   \0 - copy from offset 0
#   \2 - copy two bytes (one too many)
test_expect_success \
    'apply delta with too many copied bytes' \
    'printf "\4\1\221\0\2" > too_big_copy &&
     printf base >base &&
     test_must_fail test-tool delta -p base too_big_copy /dev/null'

# \0 - empty base
# \2 - two bytes in result
# \2 - two literal bytes (we are short one)
test_expect_success \
    'apply delta with too few literal bytes' \
    'printf "\0\2\2X" > truncated_delta &&
     test_must_fail test-tool delta -p /dev/null truncated_delta /dev/null'

# \0 - empty base
# \1 - one byte in result
# \221 - copy, one byte offset, one byte size
#   \0 - copy from offset 0
#   \1 - copy one byte (we are short one)
test_expect_success \
    'apply delta with too few bytes in base' \
    'printf "\0\1\221\0\1" > truncated_base &&
     test_must_fail test-tool delta -p /dev/null truncated_base /dev/null'

# \4 - four bytes in base
# \2 - two bytes in result
# \1 - one literal byte (X)
# \221 - copy, one byte offset, one byte size
#        (offset/size missing)
#
# Note that the literal byte is necessary to get past the uninteresting minimum
# delta size check.
test_expect_success \
    'apply delta with truncated copy parameters' \
    'printf "\4\2\1X\221" > truncated_copy_delta &&
     printf base >base &&
     test_must_fail test-tool delta -p base truncated_copy_delta /dev/null'

# \0 - empty base
# \1 - one byte in result
# \1 - one literal byte (X)
# \1 - trailing garbage command
test_expect_success \
    'apply delta with trailing garbage literal' \
    'printf "\0\1\1X\1" > tail_garbage_literal &&
     test_must_fail test-tool delta -p /dev/null tail_garbage_literal /dev/null'

# \4 - four bytes in base
# \1 - one byte in result
# \1 - one literal byte (X)
# \221 - copy, one byte offset, one byte size
#   \0 - copy from offset 0
#   \1 - copy 1 byte
test_expect_success \
    'apply delta with trailing garbage copy' \
    'printf "\4\1\1X\221\0\1" > tail_garbage_copy &&
     printf base >base &&
     test_must_fail test-tool delta -p /dev/null tail_garbage_copy /dev/null'

# \0 - empty base
# \1 - one byte in result
# \1 - one literal byte (X)
# \0 - bogus opcode
test_expect_success \
    'apply delta with trailing garbage opcode' \
    'printf "\0\1\1X\0" > tail_garbage_opcode &&
     test_must_fail test-tool delta -p /dev/null tail_garbage_opcode /dev/null'

test_done