t / t1060-object-corruption.shon commit Merge branch 'dl/config-alias-doc' (71221f2)
   1#!/bin/sh
   2
   3test_description='see how we handle various forms of corruption'
   4. ./test-lib.sh
   5
   6# convert "1234abcd" to ".git/objects/12/34abcd"
   7obj_to_file() {
   8        echo "$(git rev-parse --git-dir)/objects/$(git rev-parse "$1" | sed 's,..,&/,')"
   9}
  10
  11# Convert byte at offset "$2" of object "$1" into '\0'
  12corrupt_byte() {
  13        obj_file=$(obj_to_file "$1") &&
  14        chmod +w "$obj_file" &&
  15        printf '\0' | dd of="$obj_file" bs=1 seek="$2" conv=notrunc
  16}
  17
  18test_expect_success 'setup corrupt repo' '
  19        git init bit-error &&
  20        (
  21                cd bit-error &&
  22                test_commit content &&
  23                corrupt_byte HEAD:content.t 10
  24        ) &&
  25        git init no-bit-error &&
  26        (
  27                # distinct commit from bit-error, but containing a
  28                # non-corrupted version of the same blob
  29                cd no-bit-error &&
  30                test_tick &&
  31                test_commit content
  32        )
  33'
  34
  35test_expect_success 'setup repo with missing object' '
  36        git init missing &&
  37        (
  38                cd missing &&
  39                test_commit content &&
  40                rm -f "$(obj_to_file HEAD:content.t)"
  41        )
  42'
  43
  44test_expect_success 'setup repo with misnamed object' '
  45        git init misnamed &&
  46        (
  47                cd misnamed &&
  48                test_commit content &&
  49                good=$(obj_to_file HEAD:content.t) &&
  50                blob=$(echo corrupt | git hash-object -w --stdin) &&
  51                bad=$(obj_to_file $blob) &&
  52                rm -f "$good" &&
  53                mv "$bad" "$good"
  54        )
  55'
  56
  57test_expect_success 'streaming a corrupt blob fails' '
  58        (
  59                cd bit-error &&
  60                test_must_fail git cat-file blob HEAD:content.t
  61        )
  62'
  63
  64test_expect_success 'getting type of a corrupt blob fails' '
  65        (
  66                cd bit-error &&
  67                test_must_fail git cat-file -s HEAD:content.t
  68        )
  69'
  70
  71test_expect_success 'read-tree -u detects bit-errors in blobs' '
  72        (
  73                cd bit-error &&
  74                rm -f content.t &&
  75                test_must_fail git read-tree --reset -u HEAD
  76        )
  77'
  78
  79test_expect_success 'read-tree -u detects missing objects' '
  80        (
  81                cd missing &&
  82                rm -f content.t &&
  83                test_must_fail git read-tree --reset -u HEAD
  84        )
  85'
  86
  87# We use --bare to make sure that the transport detects it, not the checkout
  88# phase.
  89test_expect_success 'clone --no-local --bare detects corruption' '
  90        test_must_fail git clone --no-local --bare bit-error corrupt-transport
  91'
  92
  93test_expect_success 'clone --no-local --bare detects missing object' '
  94        test_must_fail git clone --no-local --bare missing missing-transport
  95'
  96
  97test_expect_success 'clone --no-local --bare detects misnamed object' '
  98        test_must_fail git clone --no-local --bare misnamed misnamed-transport
  99'
 100
 101# We do not expect --local to detect corruption at the transport layer,
 102# so we are really checking the checkout() code path.
 103test_expect_success 'clone --local detects corruption' '
 104        test_must_fail git clone --local bit-error corrupt-checkout
 105'
 106
 107test_expect_success 'error detected during checkout leaves repo intact' '
 108        test_path_is_dir corrupt-checkout/.git
 109'
 110
 111test_expect_success 'clone --local detects missing objects' '
 112        test_must_fail git clone --local missing missing-checkout
 113'
 114
 115test_expect_failure 'clone --local detects misnamed objects' '
 116        test_must_fail git clone --local misnamed misnamed-checkout
 117'
 118
 119test_expect_success 'fetch into corrupted repo with index-pack' '
 120        cp -R bit-error bit-error-cp &&
 121        test_when_finished "rm -rf bit-error-cp" &&
 122        (
 123                cd bit-error-cp &&
 124                test_must_fail git -c transfer.unpackLimit=1 \
 125                        fetch ../no-bit-error 2>stderr &&
 126                test_i18ngrep ! -i collision stderr
 127        )
 128'
 129
 130test_expect_success 'internal tree objects are not "missing"' '
 131        git init missing-empty &&
 132        (
 133                cd missing-empty &&
 134                empty_tree=$(git hash-object -t tree /dev/null) &&
 135                commit=$(echo foo | git commit-tree $empty_tree) &&
 136                git rev-list --objects $commit
 137        )
 138'
 139
 140test_done