Git fork
at reftables-rust 465 lines 14 kB view raw
1#!/bin/sh 2 3test_description='signed commit tests' 4GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main 5export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME 6 7. ./test-lib.sh 8GNUPGHOME_NOT_USED=$GNUPGHOME 9. "$TEST_DIRECTORY/lib-gpg.sh" 10 11test_expect_success GPG 'verify-commit does not crash with -h' ' 12 test_expect_code 129 git verify-commit -h >usage && 13 test_grep "[Uu]sage: git verify-commit " usage && 14 test_expect_code 129 nongit git verify-commit -h >usage && 15 test_grep "[Uu]sage: git verify-commit " usage 16' 17 18test_expect_success GPG 'create signed commits' ' 19 test_oid_cache <<-\EOF && 20 header sha1:gpgsig 21 header sha256:gpgsig-sha256 22 EOF 23 24 test_when_finished "test_unconfig commit.gpgsign" && 25 26 echo 1 >file && git add file && 27 test_tick && git commit -S -m initial && 28 git tag initial && 29 git branch side && 30 31 echo 2 >file && test_tick && git commit -a -S -m second && 32 git tag second && 33 34 git checkout side && 35 echo 3 >elif && git add elif && 36 test_tick && git commit -m "third on side" && 37 38 git checkout main && 39 test_tick && git merge -S side && 40 git tag merge && 41 42 echo 4 >file && test_tick && git commit -a -m "fourth unsigned" && 43 git tag fourth-unsigned && 44 45 test_tick && git commit --amend -S -m "fourth signed" && 46 git tag fourth-signed && 47 48 git config commit.gpgsign true && 49 echo 5 >file && test_tick && git commit -a -m "fifth signed" && 50 git tag fifth-signed && 51 52 git config commit.gpgsign false && 53 echo 6 >file && test_tick && git commit -a -m "sixth" && 54 git tag sixth-unsigned && 55 56 git config commit.gpgsign true && 57 echo 7 >file && test_tick && git commit -a -m "seventh" --no-gpg-sign && 58 git tag seventh-unsigned && 59 60 test_tick && git rebase -f HEAD^^ && git tag sixth-signed HEAD^ && 61 git tag seventh-signed && 62 63 echo 8 >file && test_tick && git commit -a -m eighth -SB7227189 && 64 git tag eighth-signed-alt && 65 66 # commit.gpgsign is still on but this must not be signed 67 echo 9 | git commit-tree HEAD^{tree} >oid && 68 test_line_count = 1 oid && 69 git tag ninth-unsigned $(cat oid) && 70 # explicit -S of course must sign. 71 echo 10 | git commit-tree -S HEAD^{tree} >oid && 72 test_line_count = 1 oid && 73 git tag tenth-signed $(cat oid) && 74 75 # --gpg-sign[=<key-id>] must sign. 76 echo 11 | git commit-tree --gpg-sign HEAD^{tree} >oid && 77 test_line_count = 1 oid && 78 git tag eleventh-signed $(cat oid) && 79 echo 12 | git commit-tree --gpg-sign=B7227189 HEAD^{tree} >oid && 80 test_line_count = 1 oid && 81 git tag twelfth-signed-alt $(cat oid) 82' 83 84test_expect_success GPG 'verify and show signatures' ' 85 ( 86 for commit in initial second merge fourth-signed \ 87 fifth-signed sixth-signed seventh-signed tenth-signed \ 88 eleventh-signed 89 do 90 git verify-commit $commit && 91 git show --pretty=short --show-signature $commit >actual && 92 grep "Good signature from" actual && 93 ! grep "BAD signature from" actual && 94 echo $commit OK || exit 1 95 done 96 ) && 97 ( 98 for commit in merge^2 fourth-unsigned sixth-unsigned \ 99 seventh-unsigned ninth-unsigned 100 do 101 test_must_fail git verify-commit $commit && 102 git show --pretty=short --show-signature $commit >actual && 103 ! grep "Good signature from" actual && 104 ! grep "BAD signature from" actual && 105 echo $commit OK || exit 1 106 done 107 ) && 108 ( 109 for commit in eighth-signed-alt twelfth-signed-alt 110 do 111 git show --pretty=short --show-signature $commit >actual && 112 grep "Good signature from" actual && 113 ! grep "BAD signature from" actual && 114 grep "not certified" actual && 115 echo $commit OK || exit 1 116 done 117 ) 118' 119 120test_expect_success GPG 'verify-commit exits failure on unknown signature' ' 121 test_must_fail env GNUPGHOME="$GNUPGHOME_NOT_USED" git verify-commit initial 2>actual && 122 ! grep "Good signature from" actual && 123 ! grep "BAD signature from" actual && 124 grep -q -F -e "No public key" -e "public key not found" actual 125' 126 127test_expect_success GPG 'verify-commit exits success on untrusted signature' ' 128 git verify-commit eighth-signed-alt 2>actual && 129 grep "Good signature from" actual && 130 ! grep "BAD signature from" actual && 131 grep "not certified" actual 132' 133 134test_expect_success GPG 'verify-commit exits success with matching minTrustLevel' ' 135 test_config gpg.minTrustLevel ultimate && 136 git verify-commit sixth-signed 137' 138 139test_expect_success GPG 'verify-commit exits success with low minTrustLevel' ' 140 test_config gpg.minTrustLevel fully && 141 git verify-commit sixth-signed 142' 143 144test_expect_success GPG 'verify-commit exits failure with high minTrustLevel' ' 145 test_config gpg.minTrustLevel ultimate && 146 test_must_fail git verify-commit eighth-signed-alt 147' 148 149test_expect_success GPG 'verify signatures with --raw' ' 150 ( 151 for commit in initial second merge fourth-signed fifth-signed sixth-signed seventh-signed 152 do 153 git verify-commit --raw $commit 2>actual && 154 grep "GOODSIG" actual && 155 ! grep "BADSIG" actual && 156 echo $commit OK || exit 1 157 done 158 ) && 159 ( 160 for commit in merge^2 fourth-unsigned sixth-unsigned seventh-unsigned 161 do 162 test_must_fail git verify-commit --raw $commit 2>actual && 163 ! grep "GOODSIG" actual && 164 ! grep "BADSIG" actual && 165 echo $commit OK || exit 1 166 done 167 ) && 168 ( 169 for commit in eighth-signed-alt 170 do 171 git verify-commit --raw $commit 2>actual && 172 grep "GOODSIG" actual && 173 ! grep "BADSIG" actual && 174 grep "TRUST_UNDEFINED" actual && 175 echo $commit OK || exit 1 176 done 177 ) 178' 179 180test_expect_success GPG 'proper header is used for hash algorithm' ' 181 git cat-file commit fourth-signed >output && 182 grep "^$(test_oid header) -----BEGIN PGP SIGNATURE-----" output 183' 184 185test_expect_success GPG 'show signed commit with signature' ' 186 git show -s initial >commit && 187 git show -s --show-signature initial >show && 188 git verify-commit -v initial >verify.1 2>verify.2 && 189 git cat-file commit initial >cat && 190 grep -v -e "gpg: " -e "Warning: " show >show.commit && 191 grep -e "gpg: " -e "Warning: " show >show.gpg && 192 grep -v "^ " cat | grep -v "^gpgsig.* " >cat.commit && 193 test_cmp show.commit commit && 194 test_cmp show.gpg verify.2 && 195 test_cmp cat.commit verify.1 196' 197 198test_expect_success GPG 'detect fudged signature' ' 199 git cat-file commit seventh-signed >raw && 200 sed -e "s/^seventh/7th forged/" raw >forged1 && 201 git hash-object -w -t commit forged1 >forged1.commit && 202 test_must_fail git verify-commit $(cat forged1.commit) && 203 git show --pretty=short --show-signature $(cat forged1.commit) >actual1 && 204 grep "BAD signature from" actual1 && 205 ! grep "Good signature from" actual1 206' 207 208test_expect_success GPG 'detect fudged signature with NUL' ' 209 git cat-file commit seventh-signed >raw && 210 cat raw >forged2 && 211 echo Qwik | tr "Q" "\000" >>forged2 && 212 git hash-object --literally -w -t commit forged2 >forged2.commit && 213 test_must_fail git verify-commit $(cat forged2.commit) && 214 git show --pretty=short --show-signature $(cat forged2.commit) >actual2 && 215 grep "BAD signature from" actual2 && 216 ! grep "Good signature from" actual2 217' 218 219test_expect_success GPG 'amending already signed commit' ' 220 git checkout -f fourth-signed^0 && 221 git commit --amend -S --no-edit && 222 git verify-commit HEAD && 223 git show -s --show-signature HEAD >actual && 224 grep "Good signature from" actual && 225 ! grep "BAD signature from" actual 226' 227 228test_expect_success GPG2 'bare signature' ' 229 git verify-commit fifth-signed 2>expect && 230 echo >>expect && 231 git log -1 --format="%GG" fifth-signed >actual && 232 test_cmp expect actual 233' 234 235test_expect_success GPG 'show good signature with custom format' ' 236 cat >expect <<-\EOF && 237 G 238 ultimate 239 13B6F51ECDDE430D 240 C O Mitter <committer@example.com> 241 73D758744BE721698EC54E8713B6F51ECDDE430D 242 73D758744BE721698EC54E8713B6F51ECDDE430D 243 EOF 244 git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" sixth-signed >actual && 245 test_cmp expect actual 246' 247 248test_expect_success GPG 'show bad signature with custom format' ' 249 cat >expect <<-\EOF && 250 B 251 undefined 252 13B6F51ECDDE430D 253 C O Mitter <committer@example.com> 254 255 256 EOF 257 git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" $(cat forged1.commit) >actual && 258 test_cmp expect actual 259' 260 261test_expect_success GPG 'show untrusted signature with custom format' ' 262 cat >expect <<-\EOF && 263 U 264 undefined 265 65A0EEA02E30CAD7 266 Eris Discordia <discord@example.net> 267 F8364A59E07FFE9F4D63005A65A0EEA02E30CAD7 268 D4BE22311AD3131E5EDA29A461092E85B7227189 269 EOF 270 git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" eighth-signed-alt >actual && 271 test_cmp expect actual 272' 273 274test_expect_success GPG 'show untrusted signature with undefined trust level' ' 275 cat >expect <<-\EOF && 276 U 277 undefined 278 65A0EEA02E30CAD7 279 Eris Discordia <discord@example.net> 280 F8364A59E07FFE9F4D63005A65A0EEA02E30CAD7 281 D4BE22311AD3131E5EDA29A461092E85B7227189 282 EOF 283 git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" eighth-signed-alt >actual && 284 test_cmp expect actual 285' 286 287test_expect_success GPG 'show untrusted signature with ultimate trust level' ' 288 cat >expect <<-\EOF && 289 G 290 ultimate 291 13B6F51ECDDE430D 292 C O Mitter <committer@example.com> 293 73D758744BE721698EC54E8713B6F51ECDDE430D 294 73D758744BE721698EC54E8713B6F51ECDDE430D 295 EOF 296 git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" sixth-signed >actual && 297 test_cmp expect actual 298' 299 300test_expect_success GPG 'show unknown signature with custom format' ' 301 cat >expect <<-\EOF && 302 E 303 undefined 304 65A0EEA02E30CAD7 305 306 307 308 EOF 309 GNUPGHOME="$GNUPGHOME_NOT_USED" git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" eighth-signed-alt >actual && 310 test_cmp expect actual 311' 312 313test_expect_success GPG 'show lack of signature with custom format' ' 314 cat >expect <<-\EOF && 315 N 316 undefined 317 318 319 320 321 EOF 322 git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" seventh-unsigned >actual && 323 test_cmp expect actual 324' 325 326test_expect_success GPG 'log.showsignature behaves like --show-signature' ' 327 test_config log.showsignature true && 328 git show initial >actual && 329 grep "gpg: Signature made" actual && 330 grep "gpg: Good signature" actual 331' 332 333test_expect_success GPG 'check config gpg.format values' ' 334 test_config gpg.format openpgp && 335 git commit -S --amend -m "success" && 336 test_config gpg.format OpEnPgP && 337 test_must_fail git commit -S --amend -m "fail" 338' 339 340test_expect_success GPG 'detect fudged commit with double signature' ' 341 sed -e "/gpgsig/,/END PGP/d" forged1 >double-base && 342 sed -n -e "/gpgsig/,/END PGP/p" forged1 | \ 343 sed -e "s/^$(test_oid header)//;s/^ //" | gpg --dearmor >double-sig1.sig && 344 gpg -o double-sig2.sig -u 29472784 --detach-sign double-base && 345 cat double-sig1.sig double-sig2.sig | gpg --enarmor >double-combined.asc && 346 sed -e "s/^\(-.*\)ARMORED FILE/\1SIGNATURE/;1s/^/$(test_oid header) /;2,\$s/^/ /" \ 347 double-combined.asc > double-gpgsig && 348 sed -e "/committer/r double-gpgsig" double-base >double-commit && 349 git hash-object -w -t commit double-commit >double-commit.commit && 350 test_must_fail git verify-commit $(cat double-commit.commit) && 351 git show --pretty=short --show-signature $(cat double-commit.commit) >double-actual && 352 grep "BAD signature from" double-actual && 353 grep "Good signature from" double-actual 354' 355 356test_expect_success GPG 'show double signature with custom format' ' 357 cat >expect <<-\EOF && 358 E 359 360 361 362 363 EOF 364 git log -1 --format="%G?%n%GK%n%GS%n%GF%n%GP" $(cat double-commit.commit) >actual && 365 test_cmp expect actual 366' 367 368 369# NEEDSWORK: This test relies on the test_tick commit/author dates from the first 370# 'create signed commits' test even though it creates its own 371test_expect_success GPG 'verify-commit verifies multiply signed commits' ' 372 git init multiply-signed && 373 cd multiply-signed && 374 test_commit first && 375 echo 1 >second && 376 git add second && 377 tree=$(git write-tree) && 378 parent=$(git rev-parse HEAD^{commit}) && 379 git commit --gpg-sign -m second && 380 git cat-file commit HEAD && 381 # Avoid trailing whitespace. 382 sed -e "s/^Q//" -e "s/^Z/ /" >commit <<-EOF && 383 Qtree $tree 384 Qparent $parent 385 Qauthor A U Thor <author@example.com> 1112912653 -0700 386 Qcommitter C O Mitter <committer@example.com> 1112912653 -0700 387 Qgpgsig -----BEGIN PGP SIGNATURE----- 388 QZ 389 Q iHQEABECADQWIQRz11h0S+chaY7FTocTtvUezd5DDQUCX/uBDRYcY29tbWl0dGVy 390 Q QGV4YW1wbGUuY29tAAoJEBO29R7N3kMNd+8AoK1I8mhLHviPH+q2I5fIVgPsEtYC 391 Q AKCTqBh+VabJceXcGIZuF0Ry+udbBQ== 392 Q =tQ0N 393 Q -----END PGP SIGNATURE----- 394 Qgpgsig-sha256 -----BEGIN PGP SIGNATURE----- 395 QZ 396 Q iHQEABECADQWIQRz11h0S+chaY7FTocTtvUezd5DDQUCX/uBIBYcY29tbWl0dGVy 397 Q QGV4YW1wbGUuY29tAAoJEBO29R7N3kMN/NEAn0XO9RYSBj2dFyozi0JKSbssYMtO 398 Q AJwKCQ1BQOtuwz//IjU8TiS+6S4iUw== 399 Q =pIwP 400 Q -----END PGP SIGNATURE----- 401 Q 402 Qsecond 403 EOF 404 head=$(git hash-object -t commit -w commit) && 405 git reset --hard $head && 406 git verify-commit $head 2>actual && 407 grep "Good signature from" actual && 408 ! grep "BAD signature from" actual 409' 410 411test_expect_success 'custom `gpg.program`' ' 412 write_script fake-gpg <<-\EOF && 413 args="$*" 414 415 # skip uninteresting options 416 while case "$1" in 417 --status-fd=*|--keyid-format=*) ;; # skip 418 *) break;; 419 esac; do shift; done 420 421 case "$1" in 422 -bsau) 423 test -z "$LET_GPG_PROGRAM_FAIL" || { 424 echo "zOMG signing failed!" >&2 425 exit 1 426 } 427 cat >sign.file 428 echo "[GNUPG:] SIG_CREATED $args" >&2 429 echo "-----BEGIN PGP MESSAGE-----" 430 echo "$args" 431 echo "-----END PGP MESSAGE-----" 432 ;; 433 --verify) 434 cat "$2" >verify.file 435 exit 0 436 ;; 437 *) 438 echo "Unhandled args: $*" >&2 439 exit 1 440 ;; 441 esac 442 EOF 443 444 test_config gpg.program "$(pwd)/fake-gpg" && 445 git commit -S --allow-empty -m signed-commit && 446 test_path_exists sign.file && 447 git show --show-signature && 448 test_path_exists verify.file && 449 450 test_must_fail env LET_GPG_PROGRAM_FAIL=1 \ 451 git commit -S --allow-empty -m must-fail 2>err && 452 grep zOMG err && 453 454 # `gpg.program` starts with `~`, the path should be interpreted to be relative to `$HOME` 455 test_config gpg.program "~/fake-gpg" && 456 env HOME="$(pwd)" \ 457 git commit -S --allow-empty -m signed-commit && 458 459 # `gpg.program` does not specify an absolute path, it should find a program in `$PATH` 460 test_config gpg.program "fake-gpg" && 461 env PATH="$PWD:$PATH" \ 462 git commit -S --allow-empty -m signed-commit 463' 464 465test_done