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