blob: fe82025d4a0dbae0cee3d0c3943617ede5b5c7a5 [file] [log] [blame]
Nicolas Pitre6e541772007-04-10 16:26:10 -04001#!/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' \
Jonathan Niedera48fcd82010-10-30 20:46:54 -050011 'rm -rf .git &&
Junio C Hamano5be60072007-07-02 22:52:14 -070012 git init &&
Johannes Schindelin1415be82008-12-15 20:44:30 +010013 git config pack.threads 1 &&
Shawn O. Pearceb3431bc2007-05-02 12:59:55 -040014 i=1 &&
Nicolas Pitre2b5c2082008-10-22 20:59:22 -040015 while test $i -le 100
Nicolas Pitre6e541772007-04-10 16:26:10 -040016 do
Nicolas Pitre2b5c2082008-10-22 20:59:22 -040017 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
Nicolas Pitre6e541772007-04-10 16:26:10 -040027 done &&
Junio C Hamanobd4b0ae2007-04-23 22:05:22 -070028 { echo 101 && test-genrandom 100 8192; } >file_101 &&
Junio C Hamano5be60072007-07-02 22:52:14 -070029 git update-index --add file_101 &&
30 tree=`git write-tree` &&
31 commit=`git commit-tree $tree </dev/null` && {
Nicolas Pitre6e541772007-04-10 16:26:10 -040032 echo $tree &&
Junio C Hamano5be60072007-07-02 22:52:14 -070033 git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\) .*/\\1/"
Nicolas Pitre6e541772007-04-10 16:26:10 -040034 } >obj-list &&
Junio C Hamano5be60072007-07-02 22:52:14 -070035 git update-ref HEAD $commit'
Nicolas Pitre6e541772007-04-10 16:26:10 -040036
37test_expect_success \
38 'pack-objects with index version 1' \
Junio C Hamano5be60072007-07-02 22:52:14 -070039 'pack1=$(git pack-objects --index-version=1 test-1 <obj-list) &&
40 git verify-pack -v "test-1-${pack1}.pack"'
Nicolas Pitre6e541772007-04-10 16:26:10 -040041
42test_expect_success \
43 'pack-objects with index version 2' \
Junio C Hamano5be60072007-07-02 22:52:14 -070044 'pack2=$(git pack-objects --index-version=2 test-2 <obj-list) &&
45 git verify-pack -v "test-2-${pack2}.pack"'
Nicolas Pitre6e541772007-04-10 16:26:10 -040046
47test_expect_success \
48 'both packs should be identical' \
49 'cmp "test-1-${pack1}.pack" "test-2-${pack2}.pack"'
50
Junio C Hamano41ac4142008-02-01 01:50:53 -080051test_expect_success \
Nicolas Pitre6e541772007-04-10 16:26:10 -040052 'index v1 and index v2 should be different' \
Junio C Hamano41ac4142008-02-01 01:50:53 -080053 '! cmp "test-1-${pack1}.idx" "test-2-${pack2}.idx"'
Nicolas Pitre6e541772007-04-10 16:26:10 -040054
55test_expect_success \
56 'index-pack with index version 1' \
Nanako Shiraishi3604e7c2008-09-03 17:59:29 +090057 'git index-pack --index-version=1 -o 1.idx "test-1-${pack1}.pack"'
Nicolas Pitre6e541772007-04-10 16:26:10 -040058
59test_expect_success \
60 'index-pack with index version 2' \
Nanako Shiraishi3604e7c2008-09-03 17:59:29 +090061 'git index-pack --index-version=2 -o 2.idx "test-1-${pack1}.pack"'
Nicolas Pitre6e541772007-04-10 16:26:10 -040062
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
Junio C Hamanoe337a042011-02-02 17:29:01 -080068test_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
Nicolas Pitre6e541772007-04-10 16:26:10 -040076test_expect_success \
Nguyễn Thái Ngọc Duy6a301342012-02-01 22:17:18 +070077 'pack-objects --index-version=2, is not accepted' \
78 'test_must_fail git pack-objects --index-version=2, test-3 <obj-list'
79
80test_expect_success \
Nicolas Pitre6e541772007-04-10 16:26:10 -040081 'index v2: force some 64-bit offsets with pack-objects' \
Johannes Sixt8ed2fca2007-11-13 21:04:58 +010082 'pack3=$(git pack-objects --index-version=2,0x40000 test-3 <obj-list)'
Nicolas Pitre6e541772007-04-10 16:26:10 -040083
Johannes Sixt8ed2fca2007-11-13 21:04:58 +010084if msg=$(git verify-pack -v "test-3-${pack3}.pack" 2>&1) ||
Jeff Kingbbf08122008-05-14 00:01:22 -040085 ! (echo "$msg" | grep "pack too large .* off_t")
Johannes Sixt8ed2fca2007-11-13 21:04:58 +010086then
Johannes Sixt18bf8792009-02-25 22:34:34 +010087 test_set_prereq OFF64_T
Johannes Sixt8ed2fca2007-11-13 21:04:58 +010088else
Ævar Arnfjörð Bjarmasonfadb5152010-06-24 17:44:48 +000089 say "# skipping tests concerning 64-bit offsets"
Johannes Sixt8ed2fca2007-11-13 21:04:58 +010090fi
91
Johannes Sixt18bf8792009-02-25 22:34:34 +010092test_expect_success OFF64_T \
Johannes Sixt8ed2fca2007-11-13 21:04:58 +010093 'index v2: verify a pack with some 64-bit offsets' \
94 'git verify-pack -v "test-3-${pack3}.pack"'
95
Johannes Sixt18bf8792009-02-25 22:34:34 +010096test_expect_success OFF64_T \
Nicolas Pitre6e541772007-04-10 16:26:10 -040097 '64-bit offsets: should be different from previous index v2 results' \
Junio C Hamano41ac4142008-02-01 01:50:53 -080098 '! cmp "test-2-${pack2}.idx" "test-3-${pack3}.idx"'
Nicolas Pitre6e541772007-04-10 16:26:10 -040099
Johannes Sixt18bf8792009-02-25 22:34:34 +0100100test_expect_success OFF64_T \
Nicolas Pitre6e541772007-04-10 16:26:10 -0400101 'index v2: force some 64-bit offsets with index-pack' \
Nanako Shiraishi3604e7c2008-09-03 17:59:29 +0900102 'git index-pack --index-version=2,0x40000 -o 3.idx "test-1-${pack1}.pack"'
Nicolas Pitre6e541772007-04-10 16:26:10 -0400103
Johannes Sixt18bf8792009-02-25 22:34:34 +0100104test_expect_success OFF64_T \
Nicolas Pitre6e541772007-04-10 16:26:10 -0400105 '64-bit offsets: index-pack result should match pack-objects one' \
106 'cmp "test-3-${pack3}.idx" "3.idx"'
107
Junio C Hamanoe337a042011-02-02 17:29:01 -0800108test_expect_success OFF64_T 'index-pack --verify on 64-bit offset v2 (cheat)' '
109 # This cheats by knowing which lower offset should still be encoded
110 # in 64-bit representation.
111 git index-pack --verify --index-version=2,0x40000 "test-3-${pack3}.pack"
112'
113
Junio C Hamano3c9fc072011-02-25 16:55:26 -0800114test_expect_success OFF64_T 'index-pack --verify on 64-bit offset v2' '
Junio C Hamanoe337a042011-02-02 17:29:01 -0800115 git index-pack --verify "test-3-${pack3}.pack"
116'
117
Nicolas Pitre2b5c2082008-10-22 20:59:22 -0400118# returns the object number for given object in given pack index
119index_obj_nr()
120{
121 idx_file=$1
122 object_sha1=$2
123 nr=0
124 git show-index < $idx_file |
125 while read offs sha1 extra
126 do
127 nr=$(($nr + 1))
128 test "$sha1" = "$object_sha1" || continue
129 echo "$(($nr - 1))"
130 break
131 done
132}
133
134# returns the pack offset for given object as found in given pack index
135index_obj_offset()
136{
137 idx_file=$1
138 object_sha1=$2
139 git show-index < $idx_file | grep $object_sha1 |
140 ( read offs extra && echo "$offs" )
141}
142
Nicolas Pitre6e541772007-04-10 16:26:10 -0400143test_expect_success \
144 '[index v1] 1) stream pack to repository' \
Nanako Shiraishi3604e7c2008-09-03 17:59:29 +0900145 'git index-pack --index-version=1 --stdin < "test-1-${pack1}.pack" &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700146 git prune-packed &&
147 git count-objects | ( read nr rest && test "$nr" -eq 1 ) &&
Nicolas Pitre6e541772007-04-10 16:26:10 -0400148 cmp "test-1-${pack1}.pack" ".git/objects/pack/pack-${pack1}.pack" &&
149 cmp "test-1-${pack1}.idx" ".git/objects/pack/pack-${pack1}.idx"'
150
151test_expect_success \
152 '[index v1] 2) create a stealth corruption in a delta base reference' \
Nicolas Pitre2b5c2082008-10-22 20:59:22 -0400153 '# This test assumes file_101 is a delta smaller than 16 bytes.
154 # It should be against file_100 but we substitute its base for file_099
155 sha1_101=`git hash-object file_101` &&
156 sha1_099=`git hash-object file_099` &&
157 offs_101=`index_obj_offset 1.idx $sha1_101` &&
158 nr_099=`index_obj_nr 1.idx $sha1_099` &&
159 chmod +w ".git/objects/pack/pack-${pack1}.pack" &&
160 dd of=".git/objects/pack/pack-${pack1}.pack" seek=$(($offs_101 + 1)) \
161 if=".git/objects/pack/pack-${pack1}.idx" \
162 skip=$((4 + 256 * 4 + $nr_099 * 24)) \
163 bs=1 count=20 conv=notrunc &&
164 git cat-file blob $sha1_101 > file_101_foo1'
Nicolas Pitre6e541772007-04-10 16:26:10 -0400165
Junio C Hamano41ac4142008-02-01 01:50:53 -0800166test_expect_success \
Nicolas Pitre6e541772007-04-10 16:26:10 -0400167 '[index v1] 3) corrupted delta happily returned wrong data' \
Nicolas Pitre2b5c2082008-10-22 20:59:22 -0400168 'test -f file_101_foo1 && ! cmp file_101 file_101_foo1'
Nicolas Pitre6e541772007-04-10 16:26:10 -0400169
Junio C Hamano41ac4142008-02-01 01:50:53 -0800170test_expect_success \
Nicolas Pitre6e541772007-04-10 16:26:10 -0400171 '[index v1] 4) confirm that the pack is actually corrupted' \
Stephan Beyerd492b312008-07-12 17:47:52 +0200172 'test_must_fail git fsck --full $commit'
Nicolas Pitre6e541772007-04-10 16:26:10 -0400173
174test_expect_success \
175 '[index v1] 5) pack-objects happily reuses corrupted data' \
Junio C Hamano5be60072007-07-02 22:52:14 -0700176 'pack4=$(git pack-objects test-4 <obj-list) &&
Nicolas Pitre6e541772007-04-10 16:26:10 -0400177 test -f "test-4-${pack1}.pack"'
178
Junio C Hamano41ac4142008-02-01 01:50:53 -0800179test_expect_success \
Nicolas Pitre6e541772007-04-10 16:26:10 -0400180 '[index v1] 6) newly created pack is BAD !' \
Stephan Beyerd492b312008-07-12 17:47:52 +0200181 'test_must_fail git verify-pack -v "test-4-${pack1}.pack"'
Nicolas Pitre6e541772007-04-10 16:26:10 -0400182
183test_expect_success \
184 '[index v2] 1) stream pack to repository' \
185 'rm -f .git/objects/pack/* &&
Nanako Shiraishi3604e7c2008-09-03 17:59:29 +0900186 git index-pack --index-version=2 --stdin < "test-1-${pack1}.pack" &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700187 git prune-packed &&
188 git count-objects | ( read nr rest && test "$nr" -eq 1 ) &&
Nicolas Pitre6e541772007-04-10 16:26:10 -0400189 cmp "test-1-${pack1}.pack" ".git/objects/pack/pack-${pack1}.pack" &&
Nicolas Pitre5f9ffff2007-11-15 12:24:17 -0500190 cmp "test-2-${pack1}.idx" ".git/objects/pack/pack-${pack1}.idx"'
Nicolas Pitre6e541772007-04-10 16:26:10 -0400191
192test_expect_success \
193 '[index v2] 2) create a stealth corruption in a delta base reference' \
Nicolas Pitre2b5c2082008-10-22 20:59:22 -0400194 '# This test assumes file_101 is a delta smaller than 16 bytes.
195 # It should be against file_100 but we substitute its base for file_099
196 sha1_101=`git hash-object file_101` &&
197 sha1_099=`git hash-object file_099` &&
198 offs_101=`index_obj_offset 1.idx $sha1_101` &&
199 nr_099=`index_obj_nr 1.idx $sha1_099` &&
200 chmod +w ".git/objects/pack/pack-${pack1}.pack" &&
201 dd of=".git/objects/pack/pack-${pack1}.pack" seek=$(($offs_101 + 1)) \
202 if=".git/objects/pack/pack-${pack1}.idx" \
203 skip=$((8 + 256 * 4 + $nr_099 * 20)) \
204 bs=1 count=20 conv=notrunc &&
205 git cat-file blob $sha1_101 > file_101_foo2'
Nicolas Pitre6e541772007-04-10 16:26:10 -0400206
Junio C Hamano41ac4142008-02-01 01:50:53 -0800207test_expect_success \
Nicolas Pitre6e541772007-04-10 16:26:10 -0400208 '[index v2] 3) corrupted delta happily returned wrong data' \
Nicolas Pitre2b5c2082008-10-22 20:59:22 -0400209 'test -f file_101_foo2 && ! cmp file_101 file_101_foo2'
Nicolas Pitre6e541772007-04-10 16:26:10 -0400210
Junio C Hamano41ac4142008-02-01 01:50:53 -0800211test_expect_success \
Nicolas Pitre6e541772007-04-10 16:26:10 -0400212 '[index v2] 4) confirm that the pack is actually corrupted' \
Stephan Beyerd492b312008-07-12 17:47:52 +0200213 'test_must_fail git fsck --full $commit'
Nicolas Pitre6e541772007-04-10 16:26:10 -0400214
Junio C Hamano41ac4142008-02-01 01:50:53 -0800215test_expect_success \
Nicolas Pitre6e541772007-04-10 16:26:10 -0400216 '[index v2] 5) pack-objects refuses to reuse corrupted data' \
Nicolas Pitre0e8189e2008-10-31 11:31:08 -0400217 'test_must_fail git pack-objects test-5 <obj-list &&
218 test_must_fail git pack-objects --no-reuse-object test-6 <obj-list'
Nicolas Pitre6e541772007-04-10 16:26:10 -0400219
Nicolas Pitre85fe23e2008-06-24 23:19:44 -0400220test_expect_success \
221 '[index v2] 6) verify-pack detects CRC mismatch' \
222 'rm -f .git/objects/pack/* &&
Nanako Shiraishi3604e7c2008-09-03 17:59:29 +0900223 git index-pack --index-version=2 --stdin < "test-1-${pack1}.pack" &&
Nicolas Pitre85fe23e2008-06-24 23:19:44 -0400224 git verify-pack ".git/objects/pack/pack-${pack1}.pack" &&
Nicolas Pitre2b5c2082008-10-22 20:59:22 -0400225 obj=`git hash-object file_001` &&
226 nr=`index_obj_nr ".git/objects/pack/pack-${pack1}.idx" $obj` &&
Nicolas Pitre85fe23e2008-06-24 23:19:44 -0400227 chmod +w ".git/objects/pack/pack-${pack1}.idx" &&
Johannes Sixtb689ccf2008-11-17 09:21:30 +0100228 printf xxxx | dd of=".git/objects/pack/pack-${pack1}.idx" conv=notrunc \
Nicolas Pitre2b5c2082008-10-22 20:59:22 -0400229 bs=1 count=4 seek=$((8 + 256 * 4 + `wc -l <obj-list` * 20 + $nr * 4)) &&
Nicolas Pitre85fe23e2008-06-24 23:19:44 -0400230 ( while read obj
231 do git cat-file -p $obj >/dev/null || exit 1
232 done <obj-list ) &&
Junio C Hamano3de89c92011-06-03 15:32:17 -0700233 test_must_fail git verify-pack ".git/objects/pack/pack-${pack1}.pack"
234'
Nicolas Pitre85fe23e2008-06-24 23:19:44 -0400235
Nicolas Pitrea672ea62008-10-20 21:17:07 -0400236test_expect_success 'running index-pack in the object store' '
237 rm -f .git/objects/pack/* &&
238 cp test-1-${pack1}.pack .git/objects/pack/pack-${pack1}.pack &&
239 (
240 cd .git/objects/pack
241 git index-pack pack-${pack1}.pack
242 ) &&
243 test -f .git/objects/pack/pack-${pack1}.idx
244'
245
Nicolas Pitre6e541772007-04-10 16:26:10 -0400246test_done