1#!/bin/sh
2#
3# Copyright (c) 2007 Nicolas Pitre
4#
5
6test_description='pack index with 64-bit offsets and object CRC'
7. ./test-lib.sh
8
9test_expect_success \
10 'setup' \
11 'rm -rf .git &&
12 git init &&
13 git config pack.threads 1 &&
14 i=1 &&
15 while test $i -le 100
16 do
17 iii=`printf '%03i' $i`
18 test-genrandom "bar" 200 > wide_delta_$iii &&
19 test-genrandom "baz $iii" 50 >> wide_delta_$iii &&
20 test-genrandom "foo"$i 100 > deep_delta_$iii &&
21 test-genrandom "foo"`expr $i + 1` 100 >> deep_delta_$iii &&
22 test-genrandom "foo"`expr $i + 2` 100 >> deep_delta_$iii &&
23 echo $iii >file_$iii &&
24 test-genrandom "$iii" 8192 >>file_$iii &&
25 git update-index --add file_$iii deep_delta_$iii wide_delta_$iii &&
26 i=`expr $i + 1` || return 1
27 done &&
28 { echo 101 && test-genrandom 100 8192; } >file_101 &&
29 git update-index --add file_101 &&
30 tree=`git write-tree` &&
31 commit=`git commit-tree $tree </dev/null` && {
32 echo $tree &&
33 git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\) .*/\\1/"
34 } >obj-list &&
35 git update-ref HEAD $commit'
36
37test_expect_success \
38 'pack-objects with index version 1' \
39 'pack1=$(git pack-objects --index-version=1 test-1 <obj-list) &&
40 git verify-pack -v "test-1-${pack1}.pack"'
41
42test_expect_success \
43 'pack-objects with index version 2' \
44 'pack2=$(git pack-objects --index-version=2 test-2 <obj-list) &&
45 git verify-pack -v "test-2-${pack2}.pack"'
46
47test_expect_success \
48 'both packs should be identical' \
49 'cmp "test-1-${pack1}.pack" "test-2-${pack2}.pack"'
50
51test_expect_success \
52 'index v1 and index v2 should be different' \
53 '! cmp "test-1-${pack1}.idx" "test-2-${pack2}.idx"'
54
55test_expect_success \
56 'index-pack with index version 1' \
57 'git index-pack --index-version=1 -o 1.idx "test-1-${pack1}.pack"'
58
59test_expect_success \
60 'index-pack with index version 2' \
61 'git index-pack --index-version=2 -o 2.idx "test-1-${pack1}.pack"'
62
63test_expect_success \
64 'index-pack results should match pack-objects ones' \
65 'cmp "test-1-${pack1}.idx" "1.idx" &&
66 cmp "test-2-${pack2}.idx" "2.idx"'
67
68test_expect_success 'index-pack --verify on index version 1' '
69 git index-pack --verify "test-1-${pack1}.pack"
70'
71
72test_expect_success 'index-pack --verify on index version 2' '
73 git index-pack --verify "test-2-${pack2}.pack"
74'
75
76test_expect_success \
77 'index v2: force some 64-bit offsets with pack-objects' \
78 'pack3=$(git pack-objects --index-version=2,0x40000 test-3 <obj-list)'
79
80if msg=$(git verify-pack -v "test-3-${pack3}.pack" 2>&1) ||
81 ! (echo "$msg" | grep "pack too large .* off_t")
82then
83 test_set_prereq OFF64_T
84else
85 say "# skipping tests concerning 64-bit offsets"
86fi
87
88test_expect_success OFF64_T \
89 'index v2: verify a pack with some 64-bit offsets' \
90 'git verify-pack -v "test-3-${pack3}.pack"'
91
92test_expect_success OFF64_T \
93 '64-bit offsets: should be different from previous index v2 results' \
94 '! cmp "test-2-${pack2}.idx" "test-3-${pack3}.idx"'
95
96test_expect_success OFF64_T \
97 'index v2: force some 64-bit offsets with index-pack' \
98 'git index-pack --index-version=2,0x40000 -o 3.idx "test-1-${pack1}.pack"'
99
100test_expect_success OFF64_T \
101 '64-bit offsets: index-pack result should match pack-objects one' \
102 'cmp "test-3-${pack3}.idx" "3.idx"'
103
104test_expect_success OFF64_T 'index-pack --verify on 64-bit offset v2 (cheat)' '
105 # This cheats by knowing which lower offset should still be encoded
106 # in 64-bit representation.
107 git index-pack --verify --index-version=2,0x40000 "test-3-${pack3}.pack"
108'
109
110test_expect_success OFF64_T 'index-pack --verify on 64-bit offset v2' '
111 git index-pack --verify "test-3-${pack3}.pack"
112'
113
114# returns the object number for given object in given pack index
115index_obj_nr()
116{
117 idx_file=$1
118 object_sha1=$2
119 nr=0
120 git show-index < $idx_file |
121 while read offs sha1 extra
122 do
123 nr=$(($nr + 1))
124 test "$sha1" = "$object_sha1" || continue
125 echo "$(($nr - 1))"
126 break
127 done
128}
129
130# returns the pack offset for given object as found in given pack index
131index_obj_offset()
132{
133 idx_file=$1
134 object_sha1=$2
135 git show-index < $idx_file | grep $object_sha1 |
136 ( read offs extra && echo "$offs" )
137}
138
139test_expect_success \
140 '[index v1] 1) stream pack to repository' \
141 'git index-pack --index-version=1 --stdin < "test-1-${pack1}.pack" &&
142 git prune-packed &&
143 git count-objects | ( read nr rest && test "$nr" -eq 1 ) &&
144 cmp "test-1-${pack1}.pack" ".git/objects/pack/pack-${pack1}.pack" &&
145 cmp "test-1-${pack1}.idx" ".git/objects/pack/pack-${pack1}.idx"'
146
147test_expect_success \
148 '[index v1] 2) create a stealth corruption in a delta base reference' \
149 '# This test assumes file_101 is a delta smaller than 16 bytes.
150 # It should be against file_100 but we substitute its base for file_099
151 sha1_101=`git hash-object file_101` &&
152 sha1_099=`git hash-object file_099` &&
153 offs_101=`index_obj_offset 1.idx $sha1_101` &&
154 nr_099=`index_obj_nr 1.idx $sha1_099` &&
155 chmod +w ".git/objects/pack/pack-${pack1}.pack" &&
156 dd of=".git/objects/pack/pack-${pack1}.pack" seek=$(($offs_101 + 1)) \
157 if=".git/objects/pack/pack-${pack1}.idx" \
158 skip=$((4 + 256 * 4 + $nr_099 * 24)) \
159 bs=1 count=20 conv=notrunc &&
160 git cat-file blob $sha1_101 > file_101_foo1'
161
162test_expect_success \
163 '[index v1] 3) corrupted delta happily returned wrong data' \
164 'test -f file_101_foo1 && ! cmp file_101 file_101_foo1'
165
166test_expect_success \
167 '[index v1] 4) confirm that the pack is actually corrupted' \
168 'test_must_fail git fsck --full $commit'
169
170test_expect_success \
171 '[index v1] 5) pack-objects happily reuses corrupted data' \
172 'pack4=$(git pack-objects test-4 <obj-list) &&
173 test -f "test-4-${pack1}.pack"'
174
175test_expect_success \
176 '[index v1] 6) newly created pack is BAD !' \
177 'test_must_fail git verify-pack -v "test-4-${pack1}.pack"'
178
179test_expect_success \
180 '[index v2] 1) stream pack to repository' \
181 'rm -f .git/objects/pack/* &&
182 git index-pack --index-version=2 --stdin < "test-1-${pack1}.pack" &&
183 git prune-packed &&
184 git count-objects | ( read nr rest && test "$nr" -eq 1 ) &&
185 cmp "test-1-${pack1}.pack" ".git/objects/pack/pack-${pack1}.pack" &&
186 cmp "test-2-${pack1}.idx" ".git/objects/pack/pack-${pack1}.idx"'
187
188test_expect_success \
189 '[index v2] 2) create a stealth corruption in a delta base reference' \
190 '# This test assumes file_101 is a delta smaller than 16 bytes.
191 # It should be against file_100 but we substitute its base for file_099
192 sha1_101=`git hash-object file_101` &&
193 sha1_099=`git hash-object file_099` &&
194 offs_101=`index_obj_offset 1.idx $sha1_101` &&
195 nr_099=`index_obj_nr 1.idx $sha1_099` &&
196 chmod +w ".git/objects/pack/pack-${pack1}.pack" &&
197 dd of=".git/objects/pack/pack-${pack1}.pack" seek=$(($offs_101 + 1)) \
198 if=".git/objects/pack/pack-${pack1}.idx" \
199 skip=$((8 + 256 * 4 + $nr_099 * 20)) \
200 bs=1 count=20 conv=notrunc &&
201 git cat-file blob $sha1_101 > file_101_foo2'
202
203test_expect_success \
204 '[index v2] 3) corrupted delta happily returned wrong data' \
205 'test -f file_101_foo2 && ! cmp file_101 file_101_foo2'
206
207test_expect_success \
208 '[index v2] 4) confirm that the pack is actually corrupted' \
209 'test_must_fail git fsck --full $commit'
210
211test_expect_success \
212 '[index v2] 5) pack-objects refuses to reuse corrupted data' \
213 'test_must_fail git pack-objects test-5 <obj-list &&
214 test_must_fail git pack-objects --no-reuse-object test-6 <obj-list'
215
216test_expect_success \
217 '[index v2] 6) verify-pack detects CRC mismatch' \
218 'rm -f .git/objects/pack/* &&
219 git index-pack --index-version=2 --stdin < "test-1-${pack1}.pack" &&
220 git verify-pack ".git/objects/pack/pack-${pack1}.pack" &&
221 obj=`git hash-object file_001` &&
222 nr=`index_obj_nr ".git/objects/pack/pack-${pack1}.idx" $obj` &&
223 chmod +w ".git/objects/pack/pack-${pack1}.idx" &&
224 printf xxxx | dd of=".git/objects/pack/pack-${pack1}.idx" conv=notrunc \
225 bs=1 count=4 seek=$((8 + 256 * 4 + `wc -l <obj-list` * 20 + $nr * 4)) &&
226 ( while read obj
227 do git cat-file -p $obj >/dev/null || exit 1
228 done <obj-list ) &&
229 test_must_fail git verify-pack ".git/objects/pack/pack-${pack1}.pack"
230'
231
232test_expect_success 'running index-pack in the object store' '
233 rm -f .git/objects/pack/* &&
234 cp test-1-${pack1}.pack .git/objects/pack/pack-${pack1}.pack &&
235 (
236 cd .git/objects/pack
237 git index-pack pack-${pack1}.pack
238 ) &&
239 test -f .git/objects/pack/pack-${pack1}.idx
240'
241
242test_done