object-store: factor out odb_clear_loose_cache()
[git.git] / t / t7510-signed-commit.sh
blob86d3f93fa22e45ba9dda24c49ec02df14e22ebc3
1 #!/bin/sh
3 test_description='signed commit tests'
4 . ./test-lib.sh
5 GNUPGHOME_NOT_USED=$GNUPGHOME
6 . "$TEST_DIRECTORY/lib-gpg.sh"
8 test_expect_success GPG 'create signed commits' '
9 test_when_finished "test_unconfig commit.gpgsign" &&
11 echo 1 >file && git add file &&
12 test_tick && git commit -S -m initial &&
13 git tag initial &&
14 git branch side &&
16 echo 2 >file && test_tick && git commit -a -S -m second &&
17 git tag second &&
19 git checkout side &&
20 echo 3 >elif && git add elif &&
21 test_tick && git commit -m "third on side" &&
23 git checkout master &&
24 test_tick && git merge -S side &&
25 git tag merge &&
27 echo 4 >file && test_tick && git commit -a -m "fourth unsigned" &&
28 git tag fourth-unsigned &&
30 test_tick && git commit --amend -S -m "fourth signed" &&
31 git tag fourth-signed &&
33 git config commit.gpgsign true &&
34 echo 5 >file && test_tick && git commit -a -m "fifth signed" &&
35 git tag fifth-signed &&
37 git config commit.gpgsign false &&
38 echo 6 >file && test_tick && git commit -a -m "sixth" &&
39 git tag sixth-unsigned &&
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 &&
45 test_tick && git rebase -f HEAD^^ && git tag sixth-signed HEAD^ &&
46 git tag seventh-signed &&
48 echo 8 >file && test_tick && git commit -a -m eighth -SB7227189 &&
49 git tag eighth-signed-alt &&
51 # commit.gpgsign is still on but this must not be signed
52 git tag ninth-unsigned $(echo 9 | git commit-tree HEAD^{tree}) &&
53 # explicit -S of course must sign.
54 git tag tenth-signed $(echo 9 | git commit-tree -S HEAD^{tree})
57 test_expect_success GPG 'verify and show signatures' '
59 for commit in initial second merge fourth-signed \
60 fifth-signed sixth-signed seventh-signed tenth-signed
62 git verify-commit $commit &&
63 git show --pretty=short --show-signature $commit >actual &&
64 grep "Good signature from" actual &&
65 ! grep "BAD signature from" actual &&
66 echo $commit OK || exit 1
67 done
68 ) &&
70 for commit in merge^2 fourth-unsigned sixth-unsigned \
71 seventh-unsigned ninth-unsigned
73 test_must_fail git verify-commit $commit &&
74 git show --pretty=short --show-signature $commit >actual &&
75 ! grep "Good signature from" actual &&
76 ! grep "BAD signature from" actual &&
77 echo $commit OK || exit 1
78 done
79 ) &&
81 for commit in eighth-signed-alt
83 git show --pretty=short --show-signature $commit >actual &&
84 grep "Good signature from" actual &&
85 ! grep "BAD signature from" actual &&
86 grep "not certified" actual &&
87 echo $commit OK || exit 1
88 done
92 test_expect_success GPG 'verify-commit exits success on untrusted signature' '
93 git verify-commit eighth-signed-alt 2>actual &&
94 grep "Good signature from" actual &&
95 ! grep "BAD signature from" actual &&
96 grep "not certified" actual
99 test_expect_success GPG 'verify signatures with --raw' '
101 for commit in initial second merge fourth-signed fifth-signed sixth-signed seventh-signed
103 git verify-commit --raw $commit 2>actual &&
104 grep "GOODSIG" actual &&
105 ! grep "BADSIG" actual &&
106 echo $commit OK || exit 1
107 done
108 ) &&
110 for commit in merge^2 fourth-unsigned sixth-unsigned seventh-unsigned
112 test_must_fail git verify-commit --raw $commit 2>actual &&
113 ! grep "GOODSIG" actual &&
114 ! grep "BADSIG" actual &&
115 echo $commit OK || exit 1
116 done
117 ) &&
119 for commit in eighth-signed-alt
121 git verify-commit --raw $commit 2>actual &&
122 grep "GOODSIG" actual &&
123 ! grep "BADSIG" actual &&
124 grep "TRUST_UNDEFINED" actual &&
125 echo $commit OK || exit 1
126 done
130 test_expect_success GPG 'show signed commit with signature' '
131 git show -s initial >commit &&
132 git show -s --show-signature initial >show &&
133 git verify-commit -v initial >verify.1 2>verify.2 &&
134 git cat-file commit initial >cat &&
135 grep -v -e "gpg: " -e "Warning: " show >show.commit &&
136 grep -e "gpg: " -e "Warning: " show >show.gpg &&
137 grep -v "^ " cat | grep -v "^gpgsig " >cat.commit &&
138 test_cmp show.commit commit &&
139 test_cmp show.gpg verify.2 &&
140 test_cmp cat.commit verify.1
143 test_expect_success GPG 'detect fudged signature' '
144 git cat-file commit seventh-signed >raw &&
145 sed -e "s/^seventh/7th forged/" raw >forged1 &&
146 git hash-object -w -t commit forged1 >forged1.commit &&
147 test_must_fail git verify-commit $(cat forged1.commit) &&
148 git show --pretty=short --show-signature $(cat forged1.commit) >actual1 &&
149 grep "BAD signature from" actual1 &&
150 ! grep "Good signature from" actual1
153 test_expect_success GPG 'detect fudged signature with NUL' '
154 git cat-file commit seventh-signed >raw &&
155 cat raw >forged2 &&
156 echo Qwik | tr "Q" "\000" >>forged2 &&
157 git hash-object -w -t commit forged2 >forged2.commit &&
158 test_must_fail git verify-commit $(cat forged2.commit) &&
159 git show --pretty=short --show-signature $(cat forged2.commit) >actual2 &&
160 grep "BAD signature from" actual2 &&
161 ! grep "Good signature from" actual2
164 test_expect_success GPG 'amending already signed commit' '
165 git checkout fourth-signed^0 &&
166 git commit --amend -S --no-edit &&
167 git verify-commit HEAD &&
168 git show -s --show-signature HEAD >actual &&
169 grep "Good signature from" actual &&
170 ! grep "BAD signature from" actual
173 test_expect_success GPG 'show good signature with custom format' '
174 cat >expect <<-\EOF &&
176 13B6F51ECDDE430D
177 C O Mitter <committer@example.com>
178 73D758744BE721698EC54E8713B6F51ECDDE430D
179 73D758744BE721698EC54E8713B6F51ECDDE430D
181 git log -1 --format="%G?%n%GK%n%GS%n%GF%n%GP" sixth-signed >actual &&
182 test_cmp expect actual
185 test_expect_success GPG 'show bad signature with custom format' '
186 cat >expect <<-\EOF &&
188 13B6F51ECDDE430D
189 C O Mitter <committer@example.com>
193 git log -1 --format="%G?%n%GK%n%GS%n%GF%n%GP" $(cat forged1.commit) >actual &&
194 test_cmp expect actual
197 test_expect_success GPG 'show untrusted signature with custom format' '
198 cat >expect <<-\EOF &&
200 65A0EEA02E30CAD7
201 Eris Discordia <discord@example.net>
202 F8364A59E07FFE9F4D63005A65A0EEA02E30CAD7
203 D4BE22311AD3131E5EDA29A461092E85B7227189
205 git log -1 --format="%G?%n%GK%n%GS%n%GF%n%GP" eighth-signed-alt >actual &&
206 test_cmp expect actual
209 test_expect_success GPG 'show unknown signature with custom format' '
210 cat >expect <<-\EOF &&
212 65A0EEA02E30CAD7
217 GNUPGHOME="$GNUPGHOME_NOT_USED" git log -1 --format="%G?%n%GK%n%GS%n%GF%n%GP" eighth-signed-alt >actual &&
218 test_cmp expect actual
221 test_expect_success GPG 'show lack of signature with custom format' '
222 cat >expect <<-\EOF &&
229 git log -1 --format="%G?%n%GK%n%GS%n%GF%n%GP" seventh-unsigned >actual &&
230 test_cmp expect actual
233 test_expect_success GPG 'log.showsignature behaves like --show-signature' '
234 test_config log.showsignature true &&
235 git show initial >actual &&
236 grep "gpg: Signature made" actual &&
237 grep "gpg: Good signature" actual
240 test_expect_success GPG 'check config gpg.format values' '
241 test_config gpg.format openpgp &&
242 git commit -S --amend -m "success" &&
243 test_config gpg.format OpEnPgP &&
244 test_must_fail git commit -S --amend -m "fail"
247 test_expect_success GPG 'detect fudged commit with double signature' '
248 sed -e "/gpgsig/,/END PGP/d" forged1 >double-base &&
249 sed -n -e "/gpgsig/,/END PGP/p" forged1 | \
250 sed -e "s/^gpgsig//;s/^ //" | gpg --dearmor >double-sig1.sig &&
251 gpg -o double-sig2.sig -u 29472784 --detach-sign double-base &&
252 cat double-sig1.sig double-sig2.sig | gpg --enarmor >double-combined.asc &&
253 sed -e "s/^\(-.*\)ARMORED FILE/\1SIGNATURE/;1s/^/gpgsig /;2,\$s/^/ /" \
254 double-combined.asc > double-gpgsig &&
255 sed -e "/committer/r double-gpgsig" double-base >double-commit &&
256 git hash-object -w -t commit double-commit >double-commit.commit &&
257 test_must_fail git verify-commit $(cat double-commit.commit) &&
258 git show --pretty=short --show-signature $(cat double-commit.commit) >double-actual &&
259 grep "BAD signature from" double-actual &&
260 grep "Good signature from" double-actual
263 test_expect_success GPG 'show double signature with custom format' '
264 cat >expect <<-\EOF &&
271 git log -1 --format="%G?%n%GK%n%GS%n%GF%n%GP" $(cat double-commit.commit) >actual &&
272 test_cmp expect actual
275 test_done