Merge branch 'js/reset-usage'
[git] / t / t7510-signed-commit.sh
1 #!/bin/sh
2
3 test_description='signed commit tests'
4 . ./test-lib.sh
5 . "$TEST_DIRECTORY/lib-gpg.sh"
6
7 test_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         # commit.gpgsign is still on but this must not be signed
51         git tag ninth-unsigned $(echo 9 | git commit-tree HEAD^{tree}) &&
52         # explicit -S of course must sign.
53         git tag tenth-signed $(echo 9 | git commit-tree -S HEAD^{tree})
54 '
55
56 test_expect_success GPG 'verify and show signatures' '
57         (
58                 for commit in initial second merge fourth-signed \
59                         fifth-signed sixth-signed seventh-signed tenth-signed
60                 do
61                         git verify-commit $commit &&
62                         git show --pretty=short --show-signature $commit >actual &&
63                         grep "Good signature from" actual &&
64                         ! grep "BAD signature from" actual &&
65                         echo $commit OK || exit 1
66                 done
67         ) &&
68         (
69                 for commit in merge^2 fourth-unsigned sixth-unsigned \
70                         seventh-unsigned ninth-unsigned
71                 do
72                         test_must_fail git verify-commit $commit &&
73                         git show --pretty=short --show-signature $commit >actual &&
74                         ! grep "Good signature from" actual &&
75                         ! grep "BAD signature from" actual &&
76                         echo $commit OK || exit 1
77                 done
78         ) &&
79         (
80                 for commit in eighth-signed-alt
81                 do
82                         git show --pretty=short --show-signature $commit >actual &&
83                         grep "Good signature from" actual &&
84                         ! grep "BAD signature from" actual &&
85                         grep "not certified" actual &&
86                         echo $commit OK || exit 1
87                 done
88         )
89 '
90
91 test_expect_success GPG 'verify-commit exits success on untrusted signature' '
92         git verify-commit eighth-signed-alt 2>actual &&
93         grep "Good signature from" actual &&
94         ! grep "BAD signature from" actual &&
95         grep "not certified" actual
96 '
97
98 test_expect_success GPG 'verify signatures with --raw' '
99         (
100                 for commit in initial second merge fourth-signed fifth-signed sixth-signed seventh-signed
101                 do
102                         git verify-commit --raw $commit 2>actual &&
103                         grep "GOODSIG" actual &&
104                         ! grep "BADSIG" actual &&
105                         echo $commit OK || exit 1
106                 done
107         ) &&
108         (
109                 for commit in merge^2 fourth-unsigned sixth-unsigned seventh-unsigned
110                 do
111                         test_must_fail git verify-commit --raw $commit 2>actual &&
112                         ! grep "GOODSIG" actual &&
113                         ! grep "BADSIG" actual &&
114                         echo $commit OK || exit 1
115                 done
116         ) &&
117         (
118                 for commit in eighth-signed-alt
119                 do
120                         git verify-commit --raw $commit 2>actual &&
121                         grep "GOODSIG" actual &&
122                         ! grep "BADSIG" actual &&
123                         grep "TRUST_UNDEFINED" actual &&
124                         echo $commit OK || exit 1
125                 done
126         )
127 '
128
129 test_expect_success GPG 'show signed commit with signature' '
130         git show -s initial >commit &&
131         git show -s --show-signature initial >show &&
132         git verify-commit -v initial >verify.1 2>verify.2 &&
133         git cat-file commit initial >cat &&
134         grep -v -e "gpg: " -e "Warning: " show >show.commit &&
135         grep -e "gpg: " -e "Warning: " show >show.gpg &&
136         grep -v "^ " cat | grep -v "^gpgsig " >cat.commit &&
137         test_cmp show.commit commit &&
138         test_cmp show.gpg verify.2 &&
139         test_cmp cat.commit verify.1
140 '
141
142 test_expect_success GPG 'detect fudged signature' '
143         git cat-file commit seventh-signed >raw &&
144
145         sed -e "s/seventh/7th forged/" raw >forged1 &&
146         git hash-object -w -t commit forged1 >forged1.commit &&
147         ! 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
151 '
152
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         ! 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
162 '
163
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
171 '
172
173 test_expect_success GPG 'show good signature with custom format' '
174         cat >expect <<-\EOF &&
175         G
176         13B6F51ECDDE430D
177         C O Mitter <committer@example.com>
178         EOF
179         git log -1 --format="%G?%n%GK%n%GS" sixth-signed >actual &&
180         test_cmp expect actual
181 '
182
183 test_expect_success GPG 'show bad signature with custom format' '
184         cat >expect <<-\EOF &&
185         B
186         13B6F51ECDDE430D
187         C O Mitter <committer@example.com>
188         EOF
189         git log -1 --format="%G?%n%GK%n%GS" $(cat forged1.commit) >actual &&
190         test_cmp expect actual
191 '
192
193 test_expect_success GPG 'show unknown signature with custom format' '
194         cat >expect <<-\EOF &&
195         U
196         61092E85B7227189
197         Eris Discordia <discord@example.net>
198         EOF
199         git log -1 --format="%G?%n%GK%n%GS" eighth-signed-alt >actual &&
200         test_cmp expect actual
201 '
202
203 test_expect_success GPG 'show lack of signature with custom format' '
204         cat >expect <<-\EOF &&
205         N
206
207
208         EOF
209         git log -1 --format="%G?%n%GK%n%GS" seventh-unsigned >actual &&
210         test_cmp expect actual
211 '
212
213 test_expect_success GPG 'log.showsignature behaves like --show-signature' '
214         test_config log.showsignature true &&
215         git show initial >actual &&
216         grep "gpg: Signature made" actual &&
217         grep "gpg: Good signature" actual
218 '
219
220 test_done