3 test_description
='signed commit tests'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 GNUPGHOME_NOT_USED
=$GNUPGHOME
9 .
"$TEST_DIRECTORY/lib-gpg.sh"
11 test_expect_success GPG
'create signed commits' '
12 test_oid_cache <<-\EOF &&
14 header sha256:gpgsig-sha256
17 test_when_finished "test_unconfig commit.gpgsign" &&
19 echo 1 >file && git add file &&
20 test_tick && git commit -S -m initial &&
24 echo 2 >file && test_tick && git commit -a -S -m second &&
28 echo 3 >elif && git add elif &&
29 test_tick && git commit -m "third on side" &&
32 test_tick && git merge -S side &&
35 echo 4 >file && test_tick && git commit -a -m "fourth unsigned" &&
36 git tag fourth-unsigned &&
38 test_tick && git commit --amend -S -m "fourth signed" &&
39 git tag fourth-signed &&
41 git config commit.gpgsign true &&
42 echo 5 >file && test_tick && git commit -a -m "fifth signed" &&
43 git tag fifth-signed &&
45 git config commit.gpgsign false &&
46 echo 6 >file && test_tick && git commit -a -m "sixth" &&
47 git tag sixth-unsigned &&
49 git config commit.gpgsign true &&
50 echo 7 >file && test_tick && git commit -a -m "seventh" --no-gpg-sign &&
51 git tag seventh-unsigned &&
53 test_tick && git rebase -f HEAD^^ && git tag sixth-signed HEAD^ &&
54 git tag seventh-signed &&
56 echo 8 >file && test_tick && git commit -a -m eighth -SB7227189 &&
57 git tag eighth-signed-alt &&
59 # commit.gpgsign is still on but this must not be signed
60 echo 9 | git commit-tree HEAD^{tree} >oid &&
61 test_line_count = 1 oid &&
62 git tag ninth-unsigned $(cat oid) &&
63 # explicit -S of course must sign.
64 echo 10 | git commit-tree -S HEAD^{tree} >oid &&
65 test_line_count = 1 oid &&
66 git tag tenth-signed $(cat oid) &&
68 # --gpg-sign[=<key-id>] must sign.
69 echo 11 | git commit-tree --gpg-sign HEAD^{tree} >oid &&
70 test_line_count = 1 oid &&
71 git tag eleventh-signed $(cat oid) &&
72 echo 12 | git commit-tree --gpg-sign=B7227189 HEAD^{tree} >oid &&
73 test_line_count = 1 oid &&
74 git tag twelfth-signed-alt $(cat oid)
77 test_expect_success GPG
'verify and show signatures' '
79 for commit in initial second merge fourth-signed \
80 fifth-signed sixth-signed seventh-signed tenth-signed \
83 git verify-commit $commit &&
84 git show --pretty=short --show-signature $commit >actual &&
85 grep "Good signature from" actual &&
86 ! grep "BAD signature from" actual &&
87 echo $commit OK || exit 1
91 for commit in merge^2 fourth-unsigned sixth-unsigned \
92 seventh-unsigned ninth-unsigned
94 test_must_fail git verify-commit $commit &&
95 git show --pretty=short --show-signature $commit >actual &&
96 ! grep "Good signature from" actual &&
97 ! grep "BAD signature from" actual &&
98 echo $commit OK || exit 1
102 for commit in eighth-signed-alt twelfth-signed-alt
104 git show --pretty=short --show-signature $commit >actual &&
105 grep "Good signature from" actual &&
106 ! grep "BAD signature from" actual &&
107 grep "not certified" actual &&
108 echo $commit OK || exit 1
113 test_expect_success GPG
'verify-commit exits failure on unknown signature' '
114 test_must_fail env GNUPGHOME="$GNUPGHOME_NOT_USED" git verify-commit initial 2>actual &&
115 ! grep "Good signature from" actual &&
116 ! grep "BAD signature from" actual &&
117 grep -q -F -e "No public key" -e "public key not found" actual
120 test_expect_success GPG
'verify-commit exits success on untrusted signature' '
121 git verify-commit eighth-signed-alt 2>actual &&
122 grep "Good signature from" actual &&
123 ! grep "BAD signature from" actual &&
124 grep "not certified" actual
127 test_expect_success GPG
'verify-commit exits success with matching minTrustLevel' '
128 test_config gpg.minTrustLevel ultimate &&
129 git verify-commit sixth-signed
132 test_expect_success GPG
'verify-commit exits success with low minTrustLevel' '
133 test_config gpg.minTrustLevel fully &&
134 git verify-commit sixth-signed
137 test_expect_success GPG
'verify-commit exits failure with high minTrustLevel' '
138 test_config gpg.minTrustLevel ultimate &&
139 test_must_fail git verify-commit eighth-signed-alt
142 test_expect_success GPG
'verify signatures with --raw' '
144 for commit in initial second merge fourth-signed fifth-signed sixth-signed seventh-signed
146 git verify-commit --raw $commit 2>actual &&
147 grep "GOODSIG" actual &&
148 ! grep "BADSIG" actual &&
149 echo $commit OK || exit 1
153 for commit in merge^2 fourth-unsigned sixth-unsigned seventh-unsigned
155 test_must_fail git verify-commit --raw $commit 2>actual &&
156 ! grep "GOODSIG" actual &&
157 ! grep "BADSIG" actual &&
158 echo $commit OK || exit 1
162 for commit in eighth-signed-alt
164 git verify-commit --raw $commit 2>actual &&
165 grep "GOODSIG" actual &&
166 ! grep "BADSIG" actual &&
167 grep "TRUST_UNDEFINED" actual &&
168 echo $commit OK || exit 1
173 test_expect_success GPG
'proper header is used for hash algorithm' '
174 git cat-file commit fourth-signed >output &&
175 grep "^$(test_oid header) -----BEGIN PGP SIGNATURE-----" output
178 test_expect_success GPG
'show signed commit with signature' '
179 git show -s initial >commit &&
180 git show -s --show-signature initial >show &&
181 git verify-commit -v initial >verify.1 2>verify.2 &&
182 git cat-file commit initial >cat &&
183 grep -v -e "gpg: " -e "Warning: " show >show.commit &&
184 grep -e "gpg: " -e "Warning: " show >show.gpg &&
185 grep -v "^ " cat | grep -v "^gpgsig.* " >cat.commit &&
186 test_cmp show.commit commit &&
187 test_cmp show.gpg verify.2 &&
188 test_cmp cat.commit verify.1
191 test_expect_success GPG
'detect fudged signature' '
192 git cat-file commit seventh-signed >raw &&
193 sed -e "s/^seventh/7th forged/" raw >forged1 &&
194 git hash-object -w -t commit forged1 >forged1.commit &&
195 test_must_fail git verify-commit $(cat forged1.commit) &&
196 git show --pretty=short --show-signature $(cat forged1.commit) >actual1 &&
197 grep "BAD signature from" actual1 &&
198 ! grep "Good signature from" actual1
201 test_expect_success GPG
'detect fudged signature with NUL' '
202 git cat-file commit seventh-signed >raw &&
204 echo Qwik | tr "Q" "\000" >>forged2 &&
205 git hash-object --literally -w -t commit forged2 >forged2.commit &&
206 test_must_fail git verify-commit $(cat forged2.commit) &&
207 git show --pretty=short --show-signature $(cat forged2.commit) >actual2 &&
208 grep "BAD signature from" actual2 &&
209 ! grep "Good signature from" actual2
212 test_expect_success GPG
'amending already signed commit' '
213 git checkout -f fourth-signed^0 &&
214 git commit --amend -S --no-edit &&
215 git verify-commit HEAD &&
216 git show -s --show-signature HEAD >actual &&
217 grep "Good signature from" actual &&
218 ! grep "BAD signature from" actual
221 test_expect_success GPG
'show good signature with custom format' '
222 cat >expect <<-\EOF &&
226 C O Mitter <committer@example.com>
227 73D758744BE721698EC54E8713B6F51ECDDE430D
228 73D758744BE721698EC54E8713B6F51ECDDE430D
230 git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" sixth-signed >actual &&
231 test_cmp expect actual
234 test_expect_success GPG
'show bad signature with custom format' '
235 cat >expect <<-\EOF &&
239 C O Mitter <committer@example.com>
243 git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" $(cat forged1.commit) >actual &&
244 test_cmp expect actual
247 test_expect_success GPG
'show untrusted signature with custom format' '
248 cat >expect <<-\EOF &&
252 Eris Discordia <discord@example.net>
253 F8364A59E07FFE9F4D63005A65A0EEA02E30CAD7
254 D4BE22311AD3131E5EDA29A461092E85B7227189
256 git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" eighth-signed-alt >actual &&
257 test_cmp expect actual
260 test_expect_success GPG
'show untrusted signature with undefined trust level' '
261 cat >expect <<-\EOF &&
265 Eris Discordia <discord@example.net>
266 F8364A59E07FFE9F4D63005A65A0EEA02E30CAD7
267 D4BE22311AD3131E5EDA29A461092E85B7227189
269 git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" eighth-signed-alt >actual &&
270 test_cmp expect actual
273 test_expect_success GPG
'show untrusted signature with ultimate trust level' '
274 cat >expect <<-\EOF &&
278 C O Mitter <committer@example.com>
279 73D758744BE721698EC54E8713B6F51ECDDE430D
280 73D758744BE721698EC54E8713B6F51ECDDE430D
282 git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" sixth-signed >actual &&
283 test_cmp expect actual
286 test_expect_success GPG
'show unknown signature with custom format' '
287 cat >expect <<-\EOF &&
295 GNUPGHOME="$GNUPGHOME_NOT_USED" git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" eighth-signed-alt >actual &&
296 test_cmp expect actual
299 test_expect_success GPG
'show lack of signature with custom format' '
300 cat >expect <<-\EOF &&
308 git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" seventh-unsigned >actual &&
309 test_cmp expect actual
312 test_expect_success GPG
'log.showsignature behaves like --show-signature' '
313 test_config log.showsignature true &&
314 git show initial >actual &&
315 grep "gpg: Signature made" actual &&
316 grep "gpg: Good signature" actual
319 test_expect_success GPG
'check config gpg.format values' '
320 test_config gpg.format openpgp &&
321 git commit -S --amend -m "success" &&
322 test_config gpg.format OpEnPgP &&
323 test_must_fail git commit -S --amend -m "fail"
326 test_expect_success GPG
'detect fudged commit with double signature' '
327 sed -e "/gpgsig/,/END PGP/d" forged1 >double-base &&
328 sed -n -e "/gpgsig/,/END PGP/p" forged1 | \
329 sed -e "s/^$(test_oid header)//;s/^ //" | gpg --dearmor >double-sig1.sig &&
330 gpg -o double-sig2.sig -u 29472784 --detach-sign double-base &&
331 cat double-sig1.sig double-sig2.sig | gpg --enarmor >double-combined.asc &&
332 sed -e "s/^\(-.*\)ARMORED FILE/\1SIGNATURE/;1s/^/$(test_oid header) /;2,\$s/^/ /" \
333 double-combined.asc > double-gpgsig &&
334 sed -e "/committer/r double-gpgsig" double-base >double-commit &&
335 git hash-object -w -t commit double-commit >double-commit.commit &&
336 test_must_fail git verify-commit $(cat double-commit.commit) &&
337 git show --pretty=short --show-signature $(cat double-commit.commit) >double-actual &&
338 grep "BAD signature from" double-actual &&
339 grep "Good signature from" double-actual
342 test_expect_success GPG
'show double signature with custom format' '
343 cat >expect <<-\EOF &&
350 git log -1 --format="%G?%n%GK%n%GS%n%GF%n%GP" $(cat double-commit.commit) >actual &&
351 test_cmp expect actual
355 # NEEDSWORK: This test relies on the test_tick commit/author dates from the first
356 # 'create signed commits' test even though it creates its own
357 test_expect_success GPG
'verify-commit verifies multiply signed commits' '
358 git init multiply-signed &&
359 cd multiply-signed &&
363 tree=$(git write-tree) &&
364 parent=$(git rev-parse HEAD^{commit}) &&
365 git commit --gpg-sign -m second &&
366 git cat-file commit HEAD &&
367 # Avoid trailing whitespace.
368 sed -e "s/^Q//" -e "s/^Z/ /" >commit <<-EOF &&
371 Qauthor A U Thor <author@example.com> 1112912653 -0700
372 Qcommitter C O Mitter <committer@example.com> 1112912653 -0700
373 Qgpgsig -----BEGIN PGP SIGNATURE-----
375 Q iHQEABECADQWIQRz11h0S+chaY7FTocTtvUezd5DDQUCX/uBDRYcY29tbWl0dGVy
376 Q QGV4YW1wbGUuY29tAAoJEBO29R7N3kMNd+8AoK1I8mhLHviPH+q2I5fIVgPsEtYC
377 Q AKCTqBh+VabJceXcGIZuF0Ry+udbBQ==
379 Q -----END PGP SIGNATURE-----
380 Qgpgsig-sha256 -----BEGIN PGP SIGNATURE-----
382 Q iHQEABECADQWIQRz11h0S+chaY7FTocTtvUezd5DDQUCX/uBIBYcY29tbWl0dGVy
383 Q QGV4YW1wbGUuY29tAAoJEBO29R7N3kMN/NEAn0XO9RYSBj2dFyozi0JKSbssYMtO
384 Q AJwKCQ1BQOtuwz//IjU8TiS+6S4iUw==
386 Q -----END PGP SIGNATURE-----
390 head=$(git hash-object -t commit -w commit) &&
391 git reset --hard $head &&
392 git verify-commit $head 2>actual &&
393 grep "Good signature from" actual &&
394 ! grep "BAD signature from" actual
397 test_expect_success
'custom `gpg.program`' '
398 write_script fake-gpg <<-\EOF &&
401 # skip uninteresting options
403 --status-fd=*|--keyid-format=*) ;; # skip
409 test -z "$LET_GPG_PROGRAM_FAIL" || {
410 echo "zOMG signing failed!" >&2
414 echo "[GNUPG:] SIG_CREATED $args" >&2
415 echo "-----BEGIN PGP MESSAGE-----"
417 echo "-----END PGP MESSAGE-----"
420 cat "$2" >verify.file
424 echo "Unhandled args: $*" >&2
430 test_config gpg.program "$(pwd)/fake-gpg" &&
431 git commit -S --allow-empty -m signed-commit &&
432 test_path_exists sign.file &&
433 git show --show-signature &&
434 test_path_exists verify.file &&
436 test_must_fail env LET_GPG_PROGRAM_FAIL=1 \
437 git commit -S --allow-empty -m must-fail 2>err &&