58f528b98faef1e1823d9ba423ff309c572b8f91
   1#!/bin/sh
   2
   3test_description='signed commit tests'
   4. ./test-lib.sh
   5GNUPGHOME_NOT_USED=$GNUPGHOME
   6. "$TEST_DIRECTORY/lib-gpg.sh"
   7
   8test_expect_success GPG 'create signed commits' '
   9        test_when_finished "test_unconfig commit.gpgsign" &&
  10
  11        echo 1 >file && git add file &&
  12        test_tick && git commit -S -m initial &&
  13        git tag initial &&
  14        git branch side &&
  15
  16        echo 2 >file && test_tick && git commit -a -S -m second &&
  17        git tag second &&
  18
  19        git checkout side &&
  20        echo 3 >elif && git add elif &&
  21        test_tick && git commit -m "third on side" &&
  22
  23        git checkout master &&
  24        test_tick && git merge -S side &&
  25        git tag merge &&
  26
  27        echo 4 >file && test_tick && git commit -a -m "fourth unsigned" &&
  28        git tag fourth-unsigned &&
  29
  30        test_tick && git commit --amend -S -m "fourth signed" &&
  31        git tag fourth-signed &&
  32
  33        git config commit.gpgsign true &&
  34        echo 5 >file && test_tick && git commit -a -m "fifth signed" &&
  35        git tag fifth-signed &&
  36
  37        git config commit.gpgsign false &&
  38        echo 6 >file && test_tick && git commit -a -m "sixth" &&
  39        git tag sixth-unsigned &&
  40
  41        git config commit.gpgsign true &&
  42        echo 7 >file && test_tick && git commit -a -m "seventh" --no-gpg-sign &&
  43        git tag seventh-unsigned &&
  44
  45        test_tick && git rebase -f HEAD^^ && git tag sixth-signed HEAD^ &&
  46        git tag seventh-signed &&
  47
  48        echo 8 >file && test_tick && git commit -a -m eighth -SB7227189 &&
  49        git tag eighth-signed-alt &&
  50
  51        # commit.gpgsign is still on but this must not be signed
  52        echo 9 | git commit-tree HEAD^{tree} >oid &&
  53        test_line_count = 1 oid &&
  54        git tag ninth-unsigned $(cat oid) &&
  55        # explicit -S of course must sign.
  56        echo 10 | git commit-tree -S HEAD^{tree} >oid &&
  57        test_line_count = 1 oid &&
  58        git tag tenth-signed $(cat oid)
  59'
  60
  61test_expect_success GPG 'verify and show signatures' '
  62        (
  63                for commit in initial second merge fourth-signed \
  64                        fifth-signed sixth-signed seventh-signed tenth-signed
  65                do
  66                        git verify-commit $commit &&
  67                        git show --pretty=short --show-signature $commit >actual &&
  68                        grep "Good signature from" actual &&
  69                        ! grep "BAD signature from" actual &&
  70                        echo $commit OK || exit 1
  71                done
  72        ) &&
  73        (
  74                for commit in merge^2 fourth-unsigned sixth-unsigned \
  75                        seventh-unsigned ninth-unsigned
  76                do
  77                        test_must_fail git verify-commit $commit &&
  78                        git show --pretty=short --show-signature $commit >actual &&
  79                        ! grep "Good signature from" actual &&
  80                        ! grep "BAD signature from" actual &&
  81                        echo $commit OK || exit 1
  82                done
  83        ) &&
  84        (
  85                for commit in eighth-signed-alt
  86                do
  87                        git show --pretty=short --show-signature $commit >actual &&
  88                        grep "Good signature from" actual &&
  89                        ! grep "BAD signature from" actual &&
  90                        grep "not certified" actual &&
  91                        echo $commit OK || exit 1
  92                done
  93        )
  94'
  95
  96test_expect_success GPG 'verify-commit exits success on untrusted signature' '
  97        git verify-commit eighth-signed-alt 2>actual &&
  98        grep "Good signature from" actual &&
  99        ! grep "BAD signature from" actual &&
 100        grep "not certified" actual
 101'
 102
 103test_expect_success GPG 'verify signatures with --raw' '
 104        (
 105                for commit in initial second merge fourth-signed fifth-signed sixth-signed seventh-signed
 106                do
 107                        git verify-commit --raw $commit 2>actual &&
 108                        grep "GOODSIG" actual &&
 109                        ! grep "BADSIG" actual &&
 110                        echo $commit OK || exit 1
 111                done
 112        ) &&
 113        (
 114                for commit in merge^2 fourth-unsigned sixth-unsigned seventh-unsigned
 115                do
 116                        test_must_fail git verify-commit --raw $commit 2>actual &&
 117                        ! grep "GOODSIG" actual &&
 118                        ! grep "BADSIG" actual &&
 119                        echo $commit OK || exit 1
 120                done
 121        ) &&
 122        (
 123                for commit in eighth-signed-alt
 124                do
 125                        git verify-commit --raw $commit 2>actual &&
 126                        grep "GOODSIG" actual &&
 127                        ! grep "BADSIG" actual &&
 128                        grep "TRUST_UNDEFINED" actual &&
 129                        echo $commit OK || exit 1
 130                done
 131        )
 132'
 133
 134test_expect_success GPG 'show signed commit with signature' '
 135        git show -s initial >commit &&
 136        git show -s --show-signature initial >show &&
 137        git verify-commit -v initial >verify.1 2>verify.2 &&
 138        git cat-file commit initial >cat &&
 139        grep -v -e "gpg: " -e "Warning: " show >show.commit &&
 140        grep -e "gpg: " -e "Warning: " show >show.gpg &&
 141        grep -v "^ " cat | grep -v "^gpgsig " >cat.commit &&
 142        test_cmp show.commit commit &&
 143        test_cmp show.gpg verify.2 &&
 144        test_cmp cat.commit verify.1
 145'
 146
 147test_expect_success GPG 'detect fudged signature' '
 148        git cat-file commit seventh-signed >raw &&
 149        sed -e "s/^seventh/7th forged/" raw >forged1 &&
 150        git hash-object -w -t commit forged1 >forged1.commit &&
 151        test_must_fail git verify-commit $(cat forged1.commit) &&
 152        git show --pretty=short --show-signature $(cat forged1.commit) >actual1 &&
 153        grep "BAD signature from" actual1 &&
 154        ! grep "Good signature from" actual1
 155'
 156
 157test_expect_success GPG 'detect fudged signature with NUL' '
 158        git cat-file commit seventh-signed >raw &&
 159        cat raw >forged2 &&
 160        echo Qwik | tr "Q" "\000" >>forged2 &&
 161        git hash-object -w -t commit forged2 >forged2.commit &&
 162        test_must_fail git verify-commit $(cat forged2.commit) &&
 163        git show --pretty=short --show-signature $(cat forged2.commit) >actual2 &&
 164        grep "BAD signature from" actual2 &&
 165        ! grep "Good signature from" actual2
 166'
 167
 168test_expect_success GPG 'amending already signed commit' '
 169        git checkout fourth-signed^0 &&
 170        git commit --amend -S --no-edit &&
 171        git verify-commit HEAD &&
 172        git show -s --show-signature HEAD >actual &&
 173        grep "Good signature from" actual &&
 174        ! grep "BAD signature from" actual
 175'
 176
 177test_expect_success GPG 'show good signature with custom format' '
 178        cat >expect <<-\EOF &&
 179        G
 180        13B6F51ECDDE430D
 181        C O Mitter <committer@example.com>
 182        73D758744BE721698EC54E8713B6F51ECDDE430D
 183        73D758744BE721698EC54E8713B6F51ECDDE430D
 184        EOF
 185        git log -1 --format="%G?%n%GK%n%GS%n%GF%n%GP" sixth-signed >actual &&
 186        test_cmp expect actual
 187'
 188
 189test_expect_success GPG 'show bad signature with custom format' '
 190        cat >expect <<-\EOF &&
 191        B
 192        13B6F51ECDDE430D
 193        C O Mitter <committer@example.com>
 194
 195
 196        EOF
 197        git log -1 --format="%G?%n%GK%n%GS%n%GF%n%GP" $(cat forged1.commit) >actual &&
 198        test_cmp expect actual
 199'
 200
 201test_expect_success GPG 'show untrusted signature with custom format' '
 202        cat >expect <<-\EOF &&
 203        U
 204        65A0EEA02E30CAD7
 205        Eris Discordia <discord@example.net>
 206        F8364A59E07FFE9F4D63005A65A0EEA02E30CAD7
 207        D4BE22311AD3131E5EDA29A461092E85B7227189
 208        EOF
 209        git log -1 --format="%G?%n%GK%n%GS%n%GF%n%GP" eighth-signed-alt >actual &&
 210        test_cmp expect actual
 211'
 212
 213test_expect_success GPG 'show unknown signature with custom format' '
 214        cat >expect <<-\EOF &&
 215        E
 216        65A0EEA02E30CAD7
 217
 218
 219
 220        EOF
 221        GNUPGHOME="$GNUPGHOME_NOT_USED" git log -1 --format="%G?%n%GK%n%GS%n%GF%n%GP" eighth-signed-alt >actual &&
 222        test_cmp expect actual
 223'
 224
 225test_expect_success GPG 'show lack of signature with custom format' '
 226        cat >expect <<-\EOF &&
 227        N
 228
 229
 230
 231
 232        EOF
 233        git log -1 --format="%G?%n%GK%n%GS%n%GF%n%GP" seventh-unsigned >actual &&
 234        test_cmp expect actual
 235'
 236
 237test_expect_success GPG 'log.showsignature behaves like --show-signature' '
 238        test_config log.showsignature true &&
 239        git show initial >actual &&
 240        grep "gpg: Signature made" actual &&
 241        grep "gpg: Good signature" actual
 242'
 243
 244test_expect_success GPG 'check config gpg.format values' '
 245        test_config gpg.format openpgp &&
 246        git commit -S --amend -m "success" &&
 247        test_config gpg.format OpEnPgP &&
 248        test_must_fail git commit -S --amend -m "fail"
 249'
 250
 251test_expect_success GPG 'detect fudged commit with double signature' '
 252        sed -e "/gpgsig/,/END PGP/d" forged1 >double-base &&
 253        sed -n -e "/gpgsig/,/END PGP/p" forged1 | \
 254                sed -e "s/^gpgsig//;s/^ //" | gpg --dearmor >double-sig1.sig &&
 255        gpg -o double-sig2.sig -u 29472784 --detach-sign double-base &&
 256        cat double-sig1.sig double-sig2.sig | gpg --enarmor >double-combined.asc &&
 257        sed -e "s/^\(-.*\)ARMORED FILE/\1SIGNATURE/;1s/^/gpgsig /;2,\$s/^/ /" \
 258                double-combined.asc > double-gpgsig &&
 259        sed -e "/committer/r double-gpgsig" double-base >double-commit &&
 260        git hash-object -w -t commit double-commit >double-commit.commit &&
 261        test_must_fail git verify-commit $(cat double-commit.commit) &&
 262        git show --pretty=short --show-signature $(cat double-commit.commit) >double-actual &&
 263        grep "BAD signature from" double-actual &&
 264        grep "Good signature from" double-actual
 265'
 266
 267test_expect_success GPG 'show double signature with custom format' '
 268        cat >expect <<-\EOF &&
 269        E
 270
 271
 272
 273
 274        EOF
 275        git log -1 --format="%G?%n%GK%n%GS%n%GF%n%GP" $(cat double-commit.commit) >actual &&
 276        test_cmp expect actual
 277'
 278
 279test_done