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