]> git.notmuchmail.org Git - notmuch/blob - test/crypto
show: Unify JSON header output for messages and message parts
[notmuch] / test / crypto
1 #!/usr/bin/env bash
2
3 # TODO:
4 # - decryption/verification with signer key not available
5 # - verification of signatures from expired/revoked keys
6
7 test_description='PGP/MIME signature verification and decryption'
8 . ./test-lib.sh
9
10 add_gnupg_home ()
11 {
12     local output
13     [ -d ${GNUPGHOME} ] && return
14     mkdir -m 0700 "$GNUPGHOME"
15     gpg --no-tty --import <$TEST_DIRECTORY/gnupg-secret-key.asc >"$GNUPGHOME"/import.log 2>&1
16     test_debug "cat $GNUPGHOME/import.log"
17     if (gpg --quick-random --version >/dev/null 2>&1) ; then
18         echo quick-random >> "$GNUPGHOME"/gpg.conf
19     elif (gpg --debug-quick-random --version >/dev/null 2>&1) ; then
20         echo debug-quick-random >> "$GNUPGHOME"/gpg.conf
21     fi
22 }
23
24 ##################################################
25
26 add_gnupg_home
27 # get key fingerprint
28 FINGERPRINT=$(gpg --no-tty --list-secret-keys --with-colons --fingerprint | grep '^fpr:' | cut -d: -f10)
29
30 # for some reason this is needed for emacs_deliver_message to work,
31 # although I can't figure out why
32 add_email_corpus
33
34 test_expect_success 'emacs delivery of signed message' \
35 'emacs_deliver_message \
36     "test signed message 001" \
37     "This is a test signed message." \
38     "(mml-secure-message-sign)"'
39
40 test_begin_subtest "signature verification"
41 output=$(notmuch show --format=json --verify subject:"test signed message 001" \
42     | notmuch_json_show_sanitize \
43     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
44 expected='[[[{"id": "XXXXX",
45  "match": true,
46  "filename": "YYYYY",
47  "timestamp": 946728000,
48  "date_relative": "2000-01-01",
49  "tags": ["inbox","signed"],
50  "headers": {"Subject": "test signed message 001",
51  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
52  "To": "test_suite@notmuchmail.org",
53  "Date": "Sat,
54  01 Jan 2000 12:00:00 +0000"},
55  "body": [{"id": 1,
56  "sigstatus": [{"status": "good",
57  "fingerprint": "'$FINGERPRINT'",
58  "created": 946728000}],
59  "content-type": "multipart/signed",
60  "content": [{"id": 2,
61  "content-type": "text/plain",
62  "content": "This is a test signed message.\n"},
63  {"id": 3,
64  "content-type": "application/pgp-signature"}]}]},
65  []]]]'
66 test_expect_equal \
67     "$output" \
68     "$expected"
69
70 test_begin_subtest "signature verification with full owner trust"
71 # give the key full owner trust
72 echo "${FINGERPRINT}:6:" | gpg --no-tty --import-ownertrust >>"$GNUPGHOME"/trust.log 2>&1
73 gpg --no-tty --check-trustdb >>"$GNUPGHOME"/trust.log 2>&1
74 output=$(notmuch show --format=json --verify subject:"test signed message 001" \
75     | notmuch_json_show_sanitize \
76     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
77 expected='[[[{"id": "XXXXX",
78  "match": true,
79  "filename": "YYYYY",
80  "timestamp": 946728000,
81  "date_relative": "2000-01-01",
82  "tags": ["inbox","signed"],
83  "headers": {"Subject": "test signed message 001",
84  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
85  "To": "test_suite@notmuchmail.org",
86  "Date": "Sat,
87  01 Jan 2000 12:00:00 +0000"},
88  "body": [{"id": 1,
89  "sigstatus": [{"status": "good",
90  "fingerprint": "'$FINGERPRINT'",
91  "created": 946728000,
92  "userid": " Notmuch Test Suite <test_suite@notmuchmail.org> (INSECURE!)"}],
93  "content-type": "multipart/signed",
94  "content": [{"id": 2,
95  "content-type": "text/plain",
96  "content": "This is a test signed message.\n"},
97  {"id": 3,
98  "content-type": "application/pgp-signature"}]}]},
99  []]]]'
100 test_expect_equal \
101     "$output" \
102     "$expected"
103
104 test_begin_subtest "signature verification with signer key unavailable"
105 # this is broken with current versions of gmime-2.6
106 (ldd $(which notmuch) | grep -Fq gmime-2.6) && test_subtest_known_broken
107 # move the gnupghome temporarily out of the way
108 mv "${GNUPGHOME}"{,.bak}
109 output=$(notmuch show --format=json --verify subject:"test signed message 001" \
110     | notmuch_json_show_sanitize \
111     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
112 expected='[[[{"id": "XXXXX",
113  "match": true,
114  "filename": "YYYYY",
115  "timestamp": 946728000,
116  "date_relative": "2000-01-01",
117  "tags": ["inbox","signed"],
118  "headers": {"Subject": "test signed message 001",
119  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
120  "To": "test_suite@notmuchmail.org",
121  "Date": "Sat,
122  01 Jan 2000 12:00:00 +0000"},
123  "body": [{"id": 1,
124  "sigstatus": [{"status": "error",
125  "keyid": "'$(echo $FINGERPRINT | cut -c 25-)'",
126  "errors": 2}],
127  "content-type": "multipart/signed",
128  "content": [{"id": 2,
129  "content-type": "text/plain",
130  "content": "This is a test signed message.\n"},
131  {"id": 3,
132  "content-type": "application/pgp-signature"}]}]},
133  []]]]'
134 test_expect_equal \
135     "$output" \
136     "$expected"
137 mv "${GNUPGHOME}"{.bak,}
138
139 # create a test encrypted message with attachment
140 cat <<EOF >TESTATTACHMENT
141 This is a test file.
142 EOF
143 test_expect_success 'emacs delivery of encrypted message with attachment' \
144 'emacs_deliver_message \
145     "test encrypted message 001" \
146     "This is a test encrypted message.\n" \
147     "(mml-attach-file \"TESTATTACHMENT\") (mml-secure-message-encrypt)"'
148
149 test_begin_subtest "decryption, --format=text"
150 output=$(notmuch show --format=text --decrypt subject:"test encrypted message 001" \
151     | notmuch_show_sanitize_all \
152     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
153 expected='\fmessage{ id:XXXXX depth:0 match:1 filename:XXXXX
154 \fheader{
155 Notmuch Test Suite <test_suite@notmuchmail.org> (2000-01-01) (encrypted inbox)
156 Subject: test encrypted message 001
157 From: Notmuch Test Suite <test_suite@notmuchmail.org>
158 To: test_suite@notmuchmail.org
159 Date: Sat, 01 Jan 2000 12:00:00 +0000
160 \fheader}
161 \fbody{
162 \fpart{ ID: 1, Content-type: multipart/encrypted
163 \fpart{ ID: 2, Content-type: application/pgp-encrypted
164 Non-text part: application/pgp-encrypted
165 \fpart}
166 \fpart{ ID: 3, Content-type: multipart/mixed
167 \fpart{ ID: 4, Content-type: text/plain
168 This is a test encrypted message.
169 \fpart}
170 \fattachment{ ID: 5, Filename: TESTATTACHMENT, Content-type: application/octet-stream
171 Non-text part: application/octet-stream
172 \fattachment}
173 \fpart}
174 \fpart}
175 \fbody}
176 \fmessage}'
177 test_expect_equal \
178     "$output" \
179     "$expected"
180
181 test_begin_subtest "decryption, --format=json"
182 output=$(notmuch show --format=json --decrypt subject:"test encrypted message 001" \
183     | notmuch_json_show_sanitize \
184     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
185 expected='[[[{"id": "XXXXX",
186  "match": true,
187  "filename": "YYYYY",
188  "timestamp": 946728000,
189  "date_relative": "2000-01-01",
190  "tags": ["encrypted","inbox"],
191  "headers": {"Subject": "test encrypted message 001",
192  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
193  "To": "test_suite@notmuchmail.org",
194  "Date": "Sat,
195  01 Jan 2000 12:00:00 +0000"},
196  "body": [{"id": 1,
197  "encstatus": [{"status": "good"}],
198  "sigstatus": [],
199  "content-type": "multipart/encrypted",
200  "content": [{"id": 2,
201  "content-type": "application/pgp-encrypted"},
202  {"id": 3,
203  "content-type": "multipart/mixed",
204  "content": [{"id": 4,
205  "content-type": "text/plain",
206  "content": "This is a test encrypted message.\n"},
207  {"id": 5,
208  "content-type": "application/octet-stream",
209  "filename": "TESTATTACHMENT"}]}]}]},
210  []]]]'
211 test_expect_equal \
212     "$output" \
213     "$expected"
214
215 test_begin_subtest "decryption, --format=json, --part=4"
216 output=$(notmuch show --format=json --part=4 --decrypt subject:"test encrypted message 001" \
217     | notmuch_json_show_sanitize \
218     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
219 expected='{"id": 4,
220  "content-type": "text/plain",
221  "content": "This is a test encrypted message.\n"}'
222 test_expect_equal \
223     "$output" \
224     "$expected"
225
226 test_begin_subtest "decrypt attachment (--part=5 --format=raw)"
227 notmuch show \
228     --format=raw \
229     --part=5 \
230     --decrypt \
231     subject:"test encrypted message 001" >OUTPUT
232 test_expect_equal_file OUTPUT TESTATTACHMENT
233
234 test_begin_subtest "decryption failure with missing key"
235 mv "${GNUPGHOME}"{,.bak}
236 output=$(notmuch show --format=json --decrypt subject:"test encrypted message 001" \
237     | notmuch_json_show_sanitize \
238     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
239 expected='[[[{"id": "XXXXX",
240  "match": true,
241  "filename": "YYYYY",
242  "timestamp": 946728000,
243  "date_relative": "2000-01-01",
244  "tags": ["encrypted","inbox"],
245  "headers": {"Subject": "test encrypted message 001",
246  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
247  "To": "test_suite@notmuchmail.org",
248  "Date": "Sat,
249  01 Jan 2000 12:00:00 +0000"},
250  "body": [{"id": 1,
251  "encstatus": [{"status": "bad"}],
252  "content-type": "multipart/encrypted",
253  "content": [{"id": 2,
254  "content-type": "application/pgp-encrypted"},
255  {"id": 3,
256  "content-type": "application/octet-stream"}]}]},
257  []]]]'
258 test_expect_equal \
259     "$output" \
260     "$expected"
261 mv "${GNUPGHOME}"{.bak,}
262
263 test_expect_success 'emacs delivery of encrypted + signed message' \
264 'emacs_deliver_message \
265     "test encrypted message 002" \
266     "This is another test encrypted message.\n" \
267     "(mml-secure-message-sign-encrypt)"'
268
269 test_begin_subtest "decryption + signature verification"
270 output=$(notmuch show --format=json --decrypt subject:"test encrypted message 002" \
271     | notmuch_json_show_sanitize \
272     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
273 expected='[[[{"id": "XXXXX",
274  "match": true,
275  "filename": "YYYYY",
276  "timestamp": 946728000,
277  "date_relative": "2000-01-01",
278  "tags": ["encrypted","inbox"],
279  "headers": {"Subject": "test encrypted message 002",
280  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
281  "To": "test_suite@notmuchmail.org",
282  "Date": "Sat,
283  01 Jan 2000 12:00:00 +0000"},
284  "body": [{"id": 1,
285  "encstatus": [{"status": "good"}],
286  "sigstatus": [{"status": "good",
287  "fingerprint": "'$FINGERPRINT'",
288  "created": 946728000,
289  "userid": " Notmuch Test Suite <test_suite@notmuchmail.org> (INSECURE!)"}],
290  "content-type": "multipart/encrypted",
291  "content": [{"id": 2,
292  "content-type": "application/pgp-encrypted"},
293  {"id": 3,
294  "content-type": "text/plain",
295  "content": "This is another test encrypted message.\n"}]}]},
296  []]]]'
297 test_expect_equal \
298     "$output" \
299     "$expected"
300
301 test_begin_subtest "reply to encrypted message"
302 output=$(notmuch reply --decrypt subject:"test encrypted message 002" \
303     | grep -v -e '^In-Reply-To:' -e '^References:')
304 expected='From: Notmuch Test Suite <test_suite@notmuchmail.org>
305 Subject: Re: test encrypted message 002
306
307 On 01 Jan 2000 12:00:00 -0000, Notmuch Test Suite <test_suite@notmuchmail.org> wrote:
308 > This is another test encrypted message.'
309 test_expect_equal \
310     "$output" \
311     "$expected"
312
313 test_begin_subtest "signature verification with revoked key"
314 # generate revocation certificate and load it to revoke key
315 echo "y
316 1
317 Notmuch Test Suite key revocation (automated) $(date '+%F_%T%z')
318
319 y
320
321 " \
322     | gpg --no-tty --quiet --command-fd 0 --armor --gen-revoke "0x${FINGERPRINT}!" 2>/dev/null \
323     | gpg --no-tty --quiet --import
324 output=$(notmuch show --format=json --verify subject:"test signed message 001" \
325     | notmuch_json_show_sanitize \
326     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
327 expected='[[[{"id": "XXXXX",
328  "match": true,
329  "filename": "YYYYY",
330  "timestamp": 946728000,
331  "date_relative": "2000-01-01",
332  "tags": ["inbox","signed"],
333  "headers": {"Subject": "test signed message 001",
334  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
335  "To": "test_suite@notmuchmail.org",
336  "Date": "Sat,
337  01 Jan 2000 12:00:00 +0000"},
338  "body": [{"id": 1,
339  "sigstatus": [{"status": "error",
340  "keyid": "6D92612D94E46381",
341  "errors": 8}],
342  "content-type": "multipart/signed",
343  "content": [{"id": 2,
344  "content-type": "text/plain",
345  "content": "This is a test signed message.\n"},
346  {"id": 3,
347  "content-type": "application/pgp-signature"}]}]},
348  []]]]'
349 test_expect_equal \
350     "$output" \
351     "$expected"
352
353 test_done