]> git.notmuchmail.org Git - notmuch/blob - test/crypto
test emacs: Add tests for hiding messages in notmuch-show view
[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 <../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  "Cc": "",
54  "Bcc": "",
55  "Date": "01 Jan 2000 12:00:00 -0000"},
56  "body": [{"id": 1,
57  "sigstatus": [{"status": "good",
58  "fingerprint": "'$FINGERPRINT'",
59  "created": 946728000}],
60  "content-type": "multipart/signed",
61  "content": [{"id": 2,
62  "content-type": "text/plain",
63  "content": "This is a test signed message.\n"},
64  {"id": 3,
65  "content-type": "application/pgp-signature"}]}]},
66  []]]]'
67 test_expect_equal \
68     "$output" \
69     "$expected"
70
71 test_begin_subtest "signature verification with full owner trust"
72 # give the key full owner trust
73 echo "${FINGERPRINT}:6:" | gpg --no-tty --import-ownertrust >>"$GNUPGHOME"/trust.log 2>&1
74 gpg --no-tty --check-trustdb >>"$GNUPGHOME"/trust.log 2>&1
75 output=$(notmuch show --format=json --verify subject:"test signed message 001" \
76     | notmuch_json_show_sanitize \
77     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
78 expected='[[[{"id": "XXXXX",
79  "match": true,
80  "filename": "YYYYY",
81  "timestamp": 946728000,
82  "date_relative": "2000-01-01",
83  "tags": ["inbox","signed"],
84  "headers": {"Subject": "test signed message 001",
85  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
86  "To": "test_suite@notmuchmail.org",
87  "Cc": "",
88  "Bcc": "",
89  "Date": "01 Jan 2000 12:00:00 -0000"},
90  "body": [{"id": 1,
91  "sigstatus": [{"status": "good",
92  "fingerprint": "'$FINGERPRINT'",
93  "created": 946728000,
94  "userid": " Notmuch Test Suite <test_suite@notmuchmail.org> (INSECURE!)"}],
95  "content-type": "multipart/signed",
96  "content": [{"id": 2,
97  "content-type": "text/plain",
98  "content": "This is a test signed message.\n"},
99  {"id": 3,
100  "content-type": "application/pgp-signature"}]}]},
101  []]]]'
102 test_expect_equal \
103     "$output" \
104     "$expected"
105
106 test_begin_subtest "signature verification with signer key unavailable"
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  "Cc": "",
122  "Bcc": "",
123  "Date": "01 Jan 2000 12:00:00 -0000"},
124  "body": [{"id": 1,
125  "sigstatus": [{"status": "error",
126  "keyid": "'$(echo $FINGERPRINT | cut -c 25-)'",
127  "errors": 2}],
128  "content-type": "multipart/signed",
129  "content": [{"id": 2,
130  "content-type": "text/plain",
131  "content": "This is a test signed message.\n"},
132  {"id": 3,
133  "content-type": "application/pgp-signature"}]}]},
134  []]]]'
135 test_expect_equal \
136     "$output" \
137     "$expected"
138 mv "${GNUPGHOME}"{.bak,}
139
140 # create a test encrypted message with attachment
141 cat <<EOF >TESTATTACHMENT
142 This is a test file.
143 EOF
144 test_expect_success 'emacs delivery of encrypted message with attachment' \
145 'emacs_deliver_message \
146     "test encrypted message 001" \
147     "This is a test encrypted message.\n" \
148     "(mml-attach-file \"TESTATTACHMENT\") (mml-secure-message-encrypt)"'
149
150 test_begin_subtest "decryption, --format=text"
151 output=$(notmuch show --format=text --decrypt subject:"test encrypted message 001" \
152     | notmuch_show_sanitize_all \
153     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
154 expected='\fmessage{ id:XXXXX depth:0 match:1 filename:XXXXX
155 \fheader{
156 Notmuch Test Suite <test_suite@notmuchmail.org> (2000-01-01) (encrypted inbox)
157 Subject: test encrypted message 001
158 From: Notmuch Test Suite <test_suite@notmuchmail.org>
159 To: test_suite@notmuchmail.org
160 Date: 01 Jan 2000 12:00:00 -0000
161 \fheader}
162 \fbody{
163 \fpart{ ID: 1, Content-type: multipart/encrypted
164 \fpart{ ID: 2, Content-type: application/pgp-encrypted
165 Non-text part: application/pgp-encrypted
166 \fpart}
167 \fpart{ ID: 3, Content-type: multipart/mixed
168 \fpart{ ID: 4, Content-type: text/plain
169 This is a test encrypted message.
170 \fpart}
171 \fattachment{ ID: 5, Content-type: application/octet-stream
172 Attachment: TESTATTACHMENT (application/octet-stream)
173 Non-text part: application/octet-stream
174 \fattachment}
175 \fpart}
176 \fpart}
177 \fbody}
178 \fmessage}'
179 test_expect_equal \
180     "$output" \
181     "$expected"
182
183 test_begin_subtest "decryption, --format=json"
184 output=$(notmuch show --format=json --decrypt subject:"test encrypted message 001" \
185     | notmuch_json_show_sanitize \
186     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
187 expected='[[[{"id": "XXXXX",
188  "match": true,
189  "filename": "YYYYY",
190  "timestamp": 946728000,
191  "date_relative": "2000-01-01",
192  "tags": ["encrypted","inbox"],
193  "headers": {"Subject": "test encrypted message 001",
194  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
195  "To": "test_suite@notmuchmail.org",
196  "Cc": "",
197  "Bcc": "",
198  "Date": "01 Jan 2000 12:00:00 -0000"},
199  "body": [{"id": 1,
200  "encstatus": [{"status": "good"}],
201  "sigstatus": [],
202  "content-type": "multipart/encrypted",
203  "content": [{"id": 2,
204  "content-type": "application/pgp-encrypted"},
205  {"id": 3,
206  "content-type": "multipart/mixed",
207  "content": [{"id": 4,
208  "content-type": "text/plain",
209  "content": "This is a test encrypted message.\n"},
210  {"id": 5,
211  "content-type": "application/octet-stream",
212  "filename": "TESTATTACHMENT"}]}]}]},
213  []]]]'
214 test_expect_equal \
215     "$output" \
216     "$expected"
217
218 test_begin_subtest "decryption, --format=json, --part=4"
219 output=$(notmuch show --format=json --part=4 --decrypt subject:"test encrypted message 001" \
220     | notmuch_json_show_sanitize \
221     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
222 expected='{"id": 4,
223  "content-type": "text/plain",
224  "content": "This is a test encrypted message.\n"}'
225 test_expect_equal \
226     "$output" \
227     "$expected"
228
229 test_begin_subtest "decrypt attachment (--part=5 --format=raw)"
230 notmuch show \
231     --format=raw \
232     --part=5 \
233     --decrypt \
234     subject:"test encrypted message 001" >OUTPUT
235 test_expect_equal_file OUTPUT TESTATTACHMENT
236
237 test_begin_subtest "decryption failure with missing key"
238 mv "${GNUPGHOME}"{,.bak}
239 output=$(notmuch show --format=json --decrypt subject:"test encrypted message 001" \
240     | notmuch_json_show_sanitize \
241     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
242 expected='[[[{"id": "XXXXX",
243  "match": true,
244  "filename": "YYYYY",
245  "timestamp": 946728000,
246  "date_relative": "2000-01-01",
247  "tags": ["encrypted","inbox"],
248  "headers": {"Subject": "test encrypted message 001",
249  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
250  "To": "test_suite@notmuchmail.org",
251  "Cc": "",
252  "Bcc": "",
253  "Date": "01 Jan 2000 12:00:00 -0000"},
254  "body": [{"id": 1,
255  "encstatus": [{"status": "bad"}],
256  "content-type": "multipart/encrypted",
257  "content": [{"id": 2,
258  "content-type": "application/pgp-encrypted"},
259  {"id": 3,
260  "content-type": "application/octet-stream"}]}]},
261  []]]]'
262 test_expect_equal \
263     "$output" \
264     "$expected"
265 mv "${GNUPGHOME}"{.bak,}
266
267 test_expect_success 'emacs delivery of encrypted + signed message' \
268 'emacs_deliver_message \
269     "test encrypted message 002" \
270     "This is another test encrypted message.\n" \
271     "(mml-secure-message-sign-encrypt)"'
272
273 test_begin_subtest "decryption + signature verification"
274 output=$(notmuch show --format=json --decrypt subject:"test encrypted message 002" \
275     | notmuch_json_show_sanitize \
276     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
277 expected='[[[{"id": "XXXXX",
278  "match": true,
279  "filename": "YYYYY",
280  "timestamp": 946728000,
281  "date_relative": "2000-01-01",
282  "tags": ["encrypted","inbox"],
283  "headers": {"Subject": "test encrypted message 002",
284  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
285  "To": "test_suite@notmuchmail.org",
286  "Cc": "",
287  "Bcc": "",
288  "Date": "01 Jan 2000 12:00:00 -0000"},
289  "body": [{"id": 1,
290  "encstatus": [{"status": "good"}],
291  "sigstatus": [{"status": "good",
292  "fingerprint": "'$FINGERPRINT'",
293  "created": 946728000,
294  "userid": " Notmuch Test Suite <test_suite@notmuchmail.org> (INSECURE!)"}],
295  "content-type": "multipart/encrypted",
296  "content": [{"id": 2,
297  "content-type": "application/pgp-encrypted"},
298  {"id": 3,
299  "content-type": "text/plain",
300  "content": "This is another test encrypted message.\n"}]}]},
301  []]]]'
302 test_expect_equal \
303     "$output" \
304     "$expected"
305
306 test_begin_subtest "reply to encrypted message"
307 output=$(notmuch reply --decrypt subject:"test encrypted message 002" \
308     | grep -v -e '^In-Reply-To:' -e '^References:')
309 expected='From: Notmuch Test Suite <test_suite@notmuchmail.org>
310 Subject: Re: test encrypted message 002
311
312 On 01 Jan 2000 12:00:00 -0000, Notmuch Test Suite <test_suite@notmuchmail.org> wrote:
313 Non-text part: multipart/encrypted
314 Non-text part: application/pgp-encrypted
315 > This is another test encrypted message.'
316 test_expect_equal \
317     "$output" \
318     "$expected"
319
320 test_begin_subtest "signature verification with revoked key"
321 # generate revokation certificate and load it to revoke key
322 echo "y
323 1
324 Notmuch Test Suite key revocation (automated) $(date '+%F_%T%z')
325
326 y
327
328 " \
329     | gpg --no-tty --quiet --command-fd 0 --armor --gen-revoke "0x${FINGERPRINT}!" 2>/dev/null \
330     | gpg --no-tty --quiet --import
331 output=$(notmuch show --format=json --verify subject:"test signed message 001" \
332     | notmuch_json_show_sanitize \
333     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
334 expected='[[[{"id": "XXXXX",
335  "match": true,
336  "filename": "YYYYY",
337  "timestamp": 946728000,
338  "date_relative": "2000-01-01",
339  "tags": ["inbox","signed"],
340  "headers": {"Subject": "test signed message 001",
341  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
342  "To": "test_suite@notmuchmail.org",
343  "Cc": "",
344  "Bcc": "",
345  "Date": "01 Jan 2000 12:00:00 -0000"},
346  "body": [{"id": 1,
347  "sigstatus": [{"status": "error",
348  "keyid": "6D92612D94E46381",
349  "errors": 8}],
350  "content-type": "multipart/signed",
351  "content": [{"id": 2,
352  "content-type": "text/plain",
353  "content": "This is a test signed message.\n"},
354  {"id": 3,
355  "content-type": "application/pgp-signature"}]}]},
356  []]]]'
357 test_expect_equal \
358     "$output" \
359     "$expected"
360
361 test_done