t / t7510-signed-commit.shon commit Merge branch 'es/worktree-add' (4d9f744)
   1#!/bin/sh
   2
   3test_description='signed commit tests'
   4. ./test-lib.sh
   5. "$TEST_DIRECTORY/lib-gpg.sh"
   6
   7test_expect_success GPG 'create signed commits' '
   8        test_when_finished "test_unconfig commit.gpgsign" &&
   9
  10        echo 1 >file && git add file &&
  11        test_tick && git commit -S -m initial &&
  12        git tag initial &&
  13        git branch side &&
  14
  15        echo 2 >file && test_tick && git commit -a -S -m second &&
  16        git tag second &&
  17
  18        git checkout side &&
  19        echo 3 >elif && git add elif &&
  20        test_tick && git commit -m "third on side" &&
  21
  22        git checkout master &&
  23        test_tick && git merge -S side &&
  24        git tag merge &&
  25
  26        echo 4 >file && test_tick && git commit -a -m "fourth unsigned" &&
  27        git tag fourth-unsigned &&
  28
  29        test_tick && git commit --amend -S -m "fourth signed" &&
  30        git tag fourth-signed &&
  31
  32        git config commit.gpgsign true &&
  33        echo 5 >file && test_tick && git commit -a -m "fifth signed" &&
  34        git tag fifth-signed &&
  35
  36        git config commit.gpgsign false &&
  37        echo 6 >file && test_tick && git commit -a -m "sixth" &&
  38        git tag sixth-unsigned &&
  39
  40        git config commit.gpgsign true &&
  41        echo 7 >file && test_tick && git commit -a -m "seventh" --no-gpg-sign &&
  42        git tag seventh-unsigned &&
  43
  44        test_tick && git rebase -f HEAD^^ && git tag sixth-signed HEAD^ &&
  45        git tag seventh-signed &&
  46
  47        echo 8 >file && test_tick && git commit -a -m eighth -SB7227189 &&
  48        git tag eighth-signed-alt
  49'
  50
  51test_expect_success GPG 'verify and show signatures' '
  52        (
  53                for commit in initial second merge fourth-signed fifth-signed sixth-signed seventh-signed
  54                do
  55                        git verify-commit $commit &&
  56                        git show --pretty=short --show-signature $commit >actual &&
  57                        grep "Good signature from" actual &&
  58                        ! grep "BAD signature from" actual &&
  59                        echo $commit OK || exit 1
  60                done
  61        ) &&
  62        (
  63                for commit in merge^2 fourth-unsigned sixth-unsigned seventh-unsigned
  64                do
  65                        test_must_fail git verify-commit $commit &&
  66                        git show --pretty=short --show-signature $commit >actual &&
  67                        ! grep "Good signature from" actual &&
  68                        ! grep "BAD signature from" actual &&
  69                        echo $commit OK || exit 1
  70                done
  71        ) &&
  72        (
  73                for commit in eighth-signed-alt
  74                do
  75                        git show --pretty=short --show-signature $commit >actual &&
  76                        grep "Good signature from" actual &&
  77                        ! grep "BAD signature from" actual &&
  78                        grep "not certified" actual &&
  79                        echo $commit OK || exit 1
  80                done
  81        )
  82'
  83
  84test_expect_success GPG 'show signed commit with signature' '
  85        git show -s initial >commit &&
  86        git show -s --show-signature initial >show &&
  87        git verify-commit -v initial >verify.1 2>verify.2 &&
  88        git cat-file commit initial >cat &&
  89        grep -v -e "gpg: " -e "Warning: " show >show.commit &&
  90        grep -e "gpg: " -e "Warning: " show >show.gpg &&
  91        grep -v "^ " cat | grep -v "^gpgsig " >cat.commit &&
  92        test_cmp show.commit commit &&
  93        test_cmp show.gpg verify.2 &&
  94        test_cmp cat.commit verify.1
  95'
  96
  97test_expect_success GPG 'detect fudged signature' '
  98        git cat-file commit seventh-signed >raw &&
  99
 100        sed -e "s/seventh/7th forged/" raw >forged1 &&
 101        git hash-object -w -t commit forged1 >forged1.commit &&
 102        ! git verify-commit $(cat forged1.commit) &&
 103        git show --pretty=short --show-signature $(cat forged1.commit) >actual1 &&
 104        grep "BAD signature from" actual1 &&
 105        ! grep "Good signature from" actual1
 106'
 107
 108test_expect_success GPG 'detect fudged signature with NUL' '
 109        git cat-file commit seventh-signed >raw &&
 110        cat raw >forged2 &&
 111        echo Qwik | tr "Q" "\000" >>forged2 &&
 112        git hash-object -w -t commit forged2 >forged2.commit &&
 113        ! git verify-commit $(cat forged2.commit) &&
 114        git show --pretty=short --show-signature $(cat forged2.commit) >actual2 &&
 115        grep "BAD signature from" actual2 &&
 116        ! grep "Good signature from" actual2
 117'
 118
 119test_expect_success GPG 'amending already signed commit' '
 120        git checkout fourth-signed^0 &&
 121        git commit --amend -S --no-edit &&
 122        git verify-commit HEAD &&
 123        git show -s --show-signature HEAD >actual &&
 124        grep "Good signature from" actual &&
 125        ! grep "BAD signature from" actual
 126'
 127
 128test_expect_success GPG 'show good signature with custom format' '
 129        cat >expect <<-\EOF &&
 130        G
 131        13B6F51ECDDE430D
 132        C O Mitter <committer@example.com>
 133        EOF
 134        git log -1 --format="%G?%n%GK%n%GS" sixth-signed >actual &&
 135        test_cmp expect actual
 136'
 137
 138test_expect_success GPG 'show bad signature with custom format' '
 139        cat >expect <<-\EOF &&
 140        B
 141        13B6F51ECDDE430D
 142        C O Mitter <committer@example.com>
 143        EOF
 144        git log -1 --format="%G?%n%GK%n%GS" $(cat forged1.commit) >actual &&
 145        test_cmp expect actual
 146'
 147
 148test_expect_success GPG 'show unknown signature with custom format' '
 149        cat >expect <<-\EOF &&
 150        U
 151        61092E85B7227189
 152        Eris Discordia <discord@example.net>
 153        EOF
 154        git log -1 --format="%G?%n%GK%n%GS" eighth-signed-alt >actual &&
 155        test_cmp expect actual
 156'
 157
 158test_expect_success GPG 'show lack of signature with custom format' '
 159        cat >expect <<-\EOF &&
 160        N
 161
 162
 163        EOF
 164        git log -1 --format="%G?%n%GK%n%GS" seventh-unsigned >actual &&
 165        test_cmp expect actual
 166'
 167
 168test_done