midx: check both pack and index names for containment
[git/git.git] / t / t5319-multi-pack-index.sh
CommitLineData
a3407730
DS
1#!/bin/sh
2
3test_description='multi-pack-indexes'
4. ./test-lib.sh
5
c4d25228
DS
6objdir=.git/objects
7
4d80560c 8midx_read_expect () {
396f2570 9 NUM_PACKS=$1
d7cacf29 10 NUM_OBJECTS=$2
662148c4
DS
11 NUM_CHUNKS=$3
12 OBJECT_DIR=$4
13 EXTRA_CHUNKS="$5"
3227565c
DS
14 {
15 cat <<-EOF &&
662148c4
DS
16 header: 4d494458 1 $NUM_CHUNKS $NUM_PACKS
17 chunks: pack-names oid-fanout oid-lookup object-offsets$EXTRA_CHUNKS
d7cacf29 18 num_objects: $NUM_OBJECTS
3227565c
DS
19 packs:
20 EOF
21 if test $NUM_PACKS -ge 1
22 then
662148c4 23 ls $OBJECT_DIR/pack/ | grep idx | sort
3227565c 24 fi &&
662148c4 25 printf "object-dir: $OBJECT_DIR\n"
3227565c 26 } >expect &&
662148c4 27 test-tool read-midx $OBJECT_DIR >actual &&
4d80560c
DS
28 test_cmp expect actual
29}
30
a3407730 31test_expect_success 'write midx with no packs' '
fc59e748
DS
32 test_when_finished rm -f pack/multi-pack-index &&
33 git multi-pack-index --object-dir=. write &&
662148c4 34 midx_read_expect 0 0 4 .
a3407730
DS
35'
36
2c381335
DS
37generate_objects () {
38 i=$1
39 iii=$(printf '%03i' $i)
40 {
41 test-tool genrandom "bar" 200 &&
42 test-tool genrandom "baz $iii" 50
43 } >wide_delta_$iii &&
44 {
45 test-tool genrandom "foo"$i 100 &&
46 test-tool genrandom "foo"$(( $i + 1 )) 100 &&
47 test-tool genrandom "foo"$(( $i + 2 )) 100
48 } >deep_delta_$iii &&
49 {
50 echo $iii &&
51 test-tool genrandom "$iii" 8192
52 } >file_$iii &&
53 git update-index --add file_$iii deep_delta_$iii wide_delta_$iii
54}
55
56commit_and_list_objects () {
57 {
58 echo 101 &&
59 test-tool genrandom 100 8192;
60 } >file_101 &&
61 git update-index --add file_101 &&
62 tree=$(git write-tree) &&
63 commit=$(git commit-tree $tree -p HEAD</dev/null) &&
64 {
65 echo $tree &&
66 git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\) .*/\\1/"
67 } >obj-list &&
68 git reset --hard $commit
69}
70
71test_expect_success 'create objects' '
72 test_commit initial &&
73 for i in $(test_seq 1 5)
74 do
75 generate_objects $i
76 done &&
77 commit_and_list_objects
78'
79
80test_expect_success 'write midx with one v1 pack' '
c4d25228
DS
81 pack=$(git pack-objects --index-version=1 $objdir/pack/test <obj-list) &&
82 test_when_finished rm $objdir/pack/test-$pack.pack \
83 $objdir/pack/test-$pack.idx $objdir/pack/multi-pack-index &&
84 git multi-pack-index --object-dir=$objdir write &&
85 midx_read_expect 1 18 4 $objdir
2c381335
DS
86'
87
c4d25228 88midx_git_two_modes () {
b4a14394
JK
89 git -c core.multiPackIndex=false $1 >expect &&
90 git -c core.multiPackIndex=true $1 >actual &&
e9ab2ed7
DS
91 if [ "$2" = "sorted" ]
92 then
b4a14394
JK
93 sort <expect >expect.sorted &&
94 mv expect.sorted expect &&
95 sort <actual >actual.sorted &&
96 mv actual.sorted actual
e9ab2ed7 97 fi &&
c4d25228
DS
98 test_cmp expect actual
99}
100
101compare_results_with_midx () {
102 MSG=$1
103 test_expect_success "check normal git operations: $MSG" '
104 midx_git_two_modes "rev-list --objects --all" &&
e9ab2ed7
DS
105 midx_git_two_modes "log --raw" &&
106 midx_git_two_modes "count-objects --verbose" &&
5670ad98
JK
107 midx_git_two_modes "cat-file --batch-all-objects --batch-check" &&
108 midx_git_two_modes "cat-file --batch-all-objects --batch-check --unordered" sorted
c4d25228
DS
109 '
110}
111
2c381335 112test_expect_success 'write midx with one v2 pack' '
c4d25228
DS
113 git pack-objects --index-version=2,0x40 $objdir/pack/test <obj-list &&
114 git multi-pack-index --object-dir=$objdir write &&
115 midx_read_expect 1 18 4 $objdir
2c381335
DS
116'
117
c4d25228
DS
118compare_results_with_midx "one v2 pack"
119
2c381335
DS
120test_expect_success 'add more objects' '
121 for i in $(test_seq 6 10)
122 do
123 generate_objects $i
124 done &&
125 commit_and_list_objects
126'
127
128test_expect_success 'write midx with two packs' '
c4d25228
DS
129 git pack-objects --index-version=1 $objdir/pack/test-2 <obj-list &&
130 git multi-pack-index --object-dir=$objdir write &&
131 midx_read_expect 2 34 4 $objdir
2c381335
DS
132'
133
c4d25228
DS
134compare_results_with_midx "two packs"
135
2c381335
DS
136test_expect_success 'add more packs' '
137 for j in $(test_seq 11 20)
138 do
139 generate_objects $j &&
140 commit_and_list_objects &&
c4d25228 141 git pack-objects --index-version=2 $objdir/pack/test-pack <obj-list
2c381335
DS
142 done
143'
144
c4d25228
DS
145compare_results_with_midx "mixed mode (two packs + extra)"
146
2c381335 147test_expect_success 'write midx with twelve packs' '
c4d25228
DS
148 git multi-pack-index --object-dir=$objdir write &&
149 midx_read_expect 12 74 4 $objdir
662148c4
DS
150'
151
c4d25228
DS
152compare_results_with_midx "twelve packs"
153
56ee7ff1
DS
154test_expect_success 'verify multi-pack-index success' '
155 git multi-pack-index verify --object-dir=$objdir
156'
157
53ad0407
DS
158# usage: corrupt_midx_and_verify <pos> <data> <objdir> <string>
159corrupt_midx_and_verify() {
160 POS=$1 &&
161 DATA="${2:-\0}" &&
162 OBJDIR=$3 &&
163 GREPSTR="$4" &&
66ec0390
DS
164 COMMAND="$5" &&
165 if test -z "$COMMAND"
166 then
167 COMMAND="git multi-pack-index verify --object-dir=$OBJDIR"
168 fi &&
53ad0407
DS
169 FILE=$OBJDIR/pack/multi-pack-index &&
170 chmod a+w $FILE &&
171 test_when_finished mv midx-backup $FILE &&
172 cp $FILE midx-backup &&
173 printf "$DATA" | dd of="$FILE" bs=1 seek="$POS" conv=notrunc &&
66ec0390 174 test_must_fail $COMMAND 2>test_err &&
53ad0407
DS
175 grep -v "^+" test_err >err &&
176 test_i18ngrep "$GREPSTR" err
177}
178
179test_expect_success 'verify bad signature' '
180 corrupt_midx_and_verify 0 "\00" $objdir \
181 "multi-pack-index signature"
182'
183
55c5648d 184HASH_LEN=20
cc6af73c 185NUM_OBJECTS=74
53ad0407
DS
186MIDX_BYTE_VERSION=4
187MIDX_BYTE_OID_VERSION=5
188MIDX_BYTE_CHUNK_COUNT=6
d3f8e211
DS
189MIDX_HEADER_SIZE=12
190MIDX_BYTE_CHUNK_ID=$MIDX_HEADER_SIZE
191MIDX_BYTE_CHUNK_OFFSET=$(($MIDX_HEADER_SIZE + 4))
8e72a3c3
DS
192MIDX_NUM_CHUNKS=5
193MIDX_CHUNK_LOOKUP_WIDTH=12
194MIDX_OFFSET_PACKNAMES=$(($MIDX_HEADER_SIZE + \
195 $MIDX_NUM_CHUNKS * $MIDX_CHUNK_LOOKUP_WIDTH))
196MIDX_BYTE_PACKNAME_ORDER=$(($MIDX_OFFSET_PACKNAMES + 2))
2f23d3f3
DS
197MIDX_OFFSET_OID_FANOUT=$(($MIDX_OFFSET_PACKNAMES + 652))
198MIDX_OID_FANOUT_WIDTH=4
199MIDX_BYTE_OID_FANOUT_ORDER=$((MIDX_OFFSET_OID_FANOUT + 250 * $MIDX_OID_FANOUT_WIDTH + 1))
55c5648d
DS
200MIDX_OFFSET_OID_LOOKUP=$(($MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
201MIDX_BYTE_OID_LOOKUP=$(($MIDX_OFFSET_OID_LOOKUP + 16 * $HASH_LEN))
cc6af73c
DS
202MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
203MIDX_OFFSET_WIDTH=8
204MIDX_BYTE_PACK_INT_ID=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 2))
205MIDX_BYTE_OFFSET=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 6))
53ad0407
DS
206
207test_expect_success 'verify bad version' '
208 corrupt_midx_and_verify $MIDX_BYTE_VERSION "\00" $objdir \
209 "multi-pack-index version"
210'
211
212test_expect_success 'verify bad OID version' '
213 corrupt_midx_and_verify $MIDX_BYTE_OID_VERSION "\02" $objdir \
214 "hash version"
215'
216
217test_expect_success 'verify truncated chunk count' '
218 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\01" $objdir \
219 "missing required"
220'
221
222test_expect_success 'verify extended chunk count' '
223 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\07" $objdir \
224 "terminating multi-pack-index chunk id appears earlier than expected"
225'
226
d3f8e211
DS
227test_expect_success 'verify missing required chunk' '
228 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_ID "\01" $objdir \
229 "missing required"
230'
231
232test_expect_success 'verify invalid chunk offset' '
233 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_OFFSET "\01" $objdir \
234 "invalid chunk offset (too large)"
235'
236
8e72a3c3
DS
237test_expect_success 'verify packnames out of order' '
238 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "z" $objdir \
239 "pack names out of order"
240'
241
d4bf1d88
DS
242test_expect_success 'verify packnames out of order' '
243 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "a" $objdir \
244 "failed to load pack"
245'
246
2f23d3f3
DS
247test_expect_success 'verify oid fanout out of order' '
248 corrupt_midx_and_verify $MIDX_BYTE_OID_FANOUT_ORDER "\01" $objdir \
249 "oid fanout out of order"
250'
251
55c5648d
DS
252test_expect_success 'verify oid lookup out of order' '
253 corrupt_midx_and_verify $MIDX_BYTE_OID_LOOKUP "\00" $objdir \
254 "oid lookup out of order"
255'
256
cc6af73c
DS
257test_expect_success 'verify incorrect pack-int-id' '
258 corrupt_midx_and_verify $MIDX_BYTE_PACK_INT_ID "\07" $objdir \
259 "bad pack-int-id"
260'
261
262test_expect_success 'verify incorrect offset' '
263 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\07" $objdir \
264 "incorrect object offset"
265'
266
66ec0390
DS
267test_expect_success 'git-fsck incorrect offset' '
268 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\07" $objdir \
269 "incorrect object offset" \
270 "git -c core.multipackindex=true fsck"
271'
272
525e18c0
DS
273test_expect_success 'repack removes multi-pack-index' '
274 test_path_is_file $objdir/pack/multi-pack-index &&
0465a505 275 GIT_TEST_MULTI_PACK_INDEX=0 git repack -adf &&
525e18c0
DS
276 test_path_is_missing $objdir/pack/multi-pack-index
277'
278
279compare_results_with_midx "after repack"
280
e9ab2ed7
DS
281test_expect_success 'multi-pack-index and pack-bitmap' '
282 git -c repack.writeBitmaps=true repack -ad &&
283 git multi-pack-index write &&
284 git rev-list --test-bitmap HEAD
285'
286
29e2016b
DS
287test_expect_success 'multi-pack-index and alternates' '
288 git init --bare alt.git &&
289 echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
290 echo content1 >file1 &&
291 altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
292 git cat-file blob $altblob &&
293 git rev-list --all
294'
295
296compare_results_with_midx "with alternate (local midx)"
297
298test_expect_success 'multi-pack-index in an alternate' '
6a22d521
DS
299 mv .git/objects/pack/* alt.git/objects/pack &&
300 test_commit add_local_objects &&
301 git repack --local &&
302 git multi-pack-index write &&
303 midx_read_expect 1 3 4 $objdir &&
304 git reset --hard HEAD~1 &&
305 rm -f .git/objects/pack/*
29e2016b
DS
306'
307
308compare_results_with_midx "with alternate (remote midx)"
309
662148c4
DS
310# usage: corrupt_data <file> <pos> [<data>]
311corrupt_data () {
312 file=$1
313 pos=$2
314 data="${3:-\0}"
315 printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
316}
317
318# Force 64-bit offsets by manipulating the idx file.
319# This makes the IDX file _incorrect_ so be careful to clean up after!
320test_expect_success 'force some 64-bit offsets with pack-objects' '
321 mkdir objects64 &&
322 mkdir objects64/pack &&
323 for i in $(test_seq 1 11)
324 do
325 generate_objects 11
326 done &&
327 commit_and_list_objects &&
328 pack64=$(git pack-objects --index-version=2,0x40 objects64/pack/test-64 <obj-list) &&
329 idx64=objects64/pack/test-64-$pack64.idx &&
330 chmod u+w $idx64 &&
331 corrupt_data $idx64 2999 "\02" &&
332 midx64=$(git multi-pack-index --object-dir=objects64 write) &&
333 midx_read_expect 1 63 5 objects64 " large-offsets"
2c381335
DS
334'
335
56ee7ff1
DS
336test_expect_success 'verify multi-pack-index with 64-bit offsets' '
337 git multi-pack-index verify --object-dir=objects64
338'
339
cc6af73c
DS
340NUM_OBJECTS=63
341MIDX_OFFSET_OID_FANOUT=$((MIDX_OFFSET_PACKNAMES + 54))
342MIDX_OFFSET_OID_LOOKUP=$((MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
343MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
344MIDX_OFFSET_LARGE_OFFSETS=$(($MIDX_OFFSET_OBJECT_OFFSETS + $NUM_OBJECTS * $MIDX_OFFSET_WIDTH))
345MIDX_BYTE_LARGE_OFFSET=$(($MIDX_OFFSET_LARGE_OFFSETS + 3))
346
347test_expect_success 'verify incorrect 64-bit offset' '
348 corrupt_midx_and_verify $MIDX_BYTE_LARGE_OFFSET "\07" objects64 \
349 "incorrect object offset"
350'
351
a3407730 352test_done