]> git.notmuchmail.org Git - notmuch/blob - test/T357-index-decryption.sh
emacs: Add new option notmuch-search-hide-excluded
[notmuch] / test / T357-index-decryption.sh
1 #!/usr/bin/env bash
2
3 # TODO: test index.decryption=failed
4
5 test_description='indexing decrypted mail'
6 . $(dirname "$0")/test-lib.sh || exit 1
7 . $NOTMUCH_SRCDIR/test/test-lib-emacs.sh || exit 1
8
9 ##################################################
10
11 test_require_emacs
12 add_gnupg_home
13
14 # create a test encrypted message
15 test_begin_subtest 'emacs delivery of encrypted message'
16 test_expect_success \
17 'emacs_fcc_message \
18     "test encrypted message for cleartext index 001" \
19     "This is a test encrypted message with a wumpus.\n" \
20     "(mml-secure-message-encrypt)"'
21
22 test_begin_subtest "search for unindexed cleartext"
23 output=$(notmuch search wumpus)
24 expected=''
25 test_expect_equal \
26     "$output" \
27     "$expected"
28
29 # create a test encrypted message that is indexed in the clear
30 test_begin_subtest 'emacs delivery of encrypted message'
31 test_expect_success \
32 'emacs_fcc_message --decrypt=true \
33     "test encrypted message for cleartext index 002" \
34     "This is a test encrypted message with a wumpus.\n" \
35     "(mml-secure-message-encrypt)"'
36
37 test_begin_subtest "emacs delivery of encrypted message, indexed cleartext"
38 output=$(notmuch search wumpus)
39 expected='thread:0000000000000002   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 002 (encrypted inbox)'
40 test_expect_equal \
41     "$output" \
42     "$expected"
43
44 # and the same search, but by property ($expected is untouched):
45 test_begin_subtest "emacs search by property for one message"
46 output=$(notmuch search property:index.decryption=success)
47 test_expect_equal \
48     "$output" \
49     "$expected"
50
51 test_begin_subtest "show the message body of the encrypted message"
52 notmuch dump wumpus
53 output=$(notmuch show wumpus | notmuch_show_part 3)
54 expected='This is a test encrypted message with a wumpus.'
55 test_expect_equal \
56     "$output" \
57     "$expected"
58
59
60 test_begin_subtest "message should go away after deletion"
61 # cache the message in an env var and remove it:
62 fname=$(notmuch search --output=files wumpus)
63 contents="$(notmuch show --format=raw wumpus)"
64 rm -f "$fname"
65 notmuch new
66 output=$(notmuch search wumpus)
67 expected=''
68 test_expect_equal \
69     "$output" \
70     "$expected"
71
72 # try reinserting it without decryption, should stay the same:
73 test_begin_subtest "message cleartext not present after insert"
74 notmuch insert --folder=sent <<<"$contents"
75 output=$(notmuch search wumpus)
76 test_expect_equal \
77     "$output" \
78     "$expected"
79
80 # show the message using stashing decryption
81 test_begin_subtest "stash decryption during show"
82 output=$(notmuch show --decrypt=stash tag:encrypted subject:002 | notmuch_show_part 3)
83 expected='This is a test encrypted message with a wumpus.'
84 test_expect_equal \
85     "$output" \
86     "$expected"
87
88 test_begin_subtest "search should now find the contents"
89 output=$(notmuch search wumpus)
90 expected='thread:0000000000000003   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 002 (encrypted inbox unread)'
91 test_expect_equal \
92     "$output" \
93     "$expected"
94
95 # try reinserting it with decryption, should appear again, but now we
96 # have two copies of the message:
97 test_begin_subtest "message cleartext is present after reinserting with --decrypt=true"
98 notmuch insert --folder=sent --decrypt=true <<<"$contents"
99 output=$(notmuch search wumpus)
100 expected='thread:0000000000000003   2000-01-01 [1/1(2)] Notmuch Test Suite; test encrypted message for cleartext index 002 (encrypted inbox unread)'
101 test_expect_equal \
102     "$output" \
103     "$expected"
104
105 # remove all copies
106 test_begin_subtest "delete all copies of the message"
107 mid="$(notmuch search --output=messages wumpus)"
108 rm -f $(notmuch search --output=files wumpus)
109 notmuch new
110 output=$(notmuch search "id:$mid")
111 expected=''
112 test_expect_equal \
113     "$output" \
114     "$expected"
115
116 # try inserting it with decryption, should appear as a single copy
117 test_begin_subtest "message cleartext is present with insert --decrypt=true"
118 notmuch insert --folder=sent --decrypt=true <<<"$contents"
119 output=$(notmuch search wumpus | notmuch_search_sanitize)
120 expected='thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 002 (encrypted inbox unread)'
121 test_expect_equal \
122     "$output" \
123     "$expected"
124
125
126 # add a tag to all messages to ensure that it stays after reindexing
127 test_begin_subtest 'tagging all messages'
128 test_expect_success 'notmuch tag +blarney "encrypted message"'
129 test_begin_subtest "verify that tags have not changed"
130 output=$(notmuch search tag:blarney | notmuch_search_sanitize)
131 expected='thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 001 (blarney encrypted inbox)
132 thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 002 (blarney encrypted inbox unread)'
133 test_expect_equal \
134     "$output" \
135     "$expected"
136
137 # see if first message shows up after reindexing with --decrypt=true (same $expected, untouched):
138 test_begin_subtest 'reindex old messages'
139 test_expect_success 'notmuch reindex --decrypt=true tag:encrypted and not property:index.decryption=success'
140 test_begin_subtest "reindexed encrypted message, including cleartext"
141 output=$(notmuch search wumpus | notmuch_search_sanitize)
142 test_expect_equal \
143     "$output" \
144     "$expected"
145
146 # and the same search, but by property ($expected is untouched):
147 test_begin_subtest "emacs search by property for both messages"
148 output=$(notmuch search property:index.decryption=success | notmuch_search_sanitize)
149 test_expect_equal \
150     "$output" \
151     "$expected"
152
153 # try a simple reindex
154 test_begin_subtest 'reindex in auto mode'
155 test_expect_success 'notmuch reindex tag:encrypted and property:index.decryption=success'
156 test_begin_subtest "reindexed encrypted messages, should not have changed"
157 output=$(notmuch search wumpus | notmuch_search_sanitize)
158 test_expect_equal \
159     "$output" \
160     "$expected"
161
162 # try to remove cleartext indexing
163 test_begin_subtest 'reindex without cleartext'
164 test_expect_success 'notmuch reindex --decrypt=false tag:encrypted and property:index.decryption=success'
165 test_begin_subtest "reindexed encrypted messages, without cleartext"
166 output=$(notmuch search wumpus)
167 expected=''
168 test_expect_equal \
169     "$output" \
170     "$expected"
171
172 # ensure no session keys are present:
173 test_begin_subtest 'reindex using only session keys'
174 test_expect_success 'notmuch reindex tag:encrypted and property:index.decryption=success'
175 test_begin_subtest "reindexed encrypted messages, decrypting only with session keys"
176 output=$(notmuch search wumpus)
177 expected=''
178 test_expect_equal \
179     "$output" \
180     "$expected"
181
182 # and the same search, but by property ($expected is untouched):
183 test_begin_subtest "emacs search by property with both messages unindexed"
184 output=$(notmuch search property:index.decryption=success)
185 test_expect_equal \
186     "$output" \
187     "$expected"
188
189 # ensure that the tags remain even when we are dropping the cleartext.
190 test_begin_subtest "verify that tags remain without cleartext"
191 output=$(notmuch search tag:blarney | notmuch_search_sanitize)
192 expected='thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 001 (blarney encrypted inbox)
193 thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 002 (blarney encrypted inbox unread)'
194 test_expect_equal \
195     "$output" \
196     "$expected"
197
198 test_begin_subtest "index cleartext without keeping session keys"
199 test_expect_success "notmuch reindex --decrypt=nostash tag:blarney"
200
201 test_begin_subtest "Ensure that the indexed terms are present"
202 output=$(notmuch search wumpus | notmuch_search_sanitize)
203 test_expect_equal \
204     "$output" \
205     "$expected"
206
207 test_begin_subtest "show one of the messages with --decrypt=true"
208 output=$(notmuch show --decrypt=true thread:0000000000000001 | notmuch_show_part 3)
209 expected='This is a test encrypted message with a wumpus.'
210 test_expect_equal \
211     "$output" \
212     "$expected"
213
214 test_begin_subtest "Ensure that we cannot show the message with --decrypt=auto"
215 output=$(notmuch show thread:0000000000000001 | notmuch_show_part 3)
216 expected='Non-text part: application/octet-stream'
217 test_expect_equal \
218     "$output" \
219     "$expected"
220
221 add_email_corpus crypto
222
223 test_begin_subtest "indexing message fails when secret key not available"
224 notmuch reindex --decrypt=true id:simple-encrypted@crypto.notmuchmail.org
225 output=$(notmuch dump | LC_ALL=C sort)
226 expected='#= simple-encrypted@crypto.notmuchmail.org index.decryption=failure
227 #notmuch-dump batch-tag:3 config,properties,tags
228 +encrypted +inbox +unread -- id:basic-encrypted@crypto.notmuchmail.org
229 +encrypted +inbox +unread -- id:encrypted-rfc822-attachment@crypto.notmuchmail.org
230 +encrypted +inbox +unread -- id:encrypted-signed@crypto.notmuchmail.org
231 +encrypted +inbox +unread -- id:simple-encrypted@crypto.notmuchmail.org'
232 test_expect_equal \
233     "$output" \
234     "$expected"
235
236 test_begin_subtest "cannot find cleartext index"
237 output=$(notmuch search sekrit)
238 expected=''
239 test_expect_equal \
240     "$output" \
241     "$expected"
242
243 test_begin_subtest "cleartext index recovery on reindexing with stashed session keys"
244 notmuch restore <<EOF
245 #notmuch-dump batch-tag:3 config,properties,tags
246 #= simple-encrypted@crypto.notmuchmail.org session-key=9%3AFC09987F5F927CC0CC0EE80A96E4C5BBF4A499818FB591207705DFDDD6112CF9
247 EOF
248 notmuch reindex id:simple-encrypted@crypto.notmuchmail.org
249 output=$(notmuch search sekrit | notmuch_search_sanitize)
250 expected='thread:XXX   2016-12-22 [1/1] Daniel Kahn Gillmor; encrypted message (encrypted inbox unread)'
251 test_expect_equal \
252     "$output" \
253     "$expected"
254
255 test_begin_subtest "notmuch reply should show cleartext if session key is present"
256 output=$(notmuch reply id:simple-encrypted@crypto.notmuchmail.org | grep '^>')
257 expected='> This is a top sekrit message.'
258 test_expect_equal \
259     "$output" \
260     "$expected"
261
262 test_begin_subtest "notmuch show should show cleartext if session key is present"
263 output=$(notmuch show id:simple-encrypted@crypto.notmuchmail.org | notmuch_show_part 3)
264 expected='This is a top sekrit message.'
265 test_expect_equal \
266     "$output" \
267     "$expected"
268
269 test_begin_subtest "notmuch show should show nothing if decryption is explicitly disallowed"
270 output=$(notmuch show --decrypt=false id:simple-encrypted@crypto.notmuchmail.org | notmuch_show_part 3)
271 expected='Non-text part: application/octet-stream'
272 test_expect_equal \
273     "$output" \
274     "$expected"
275
276 test_begin_subtest "purging stashed session keys should lose access to the cleartext"
277 notmuch reindex --decrypt=false id:simple-encrypted@crypto.notmuchmail.org
278 output=$(notmuch search sekrit)
279 expected=''
280 test_expect_equal \
281     "$output" \
282     "$expected"
283
284 test_begin_subtest "and cleartext should be unrecoverable now that there are no stashed session keys"
285 notmuch dump
286 notmuch reindex --decrypt=true id:simple-encrypted@crypto.notmuchmail.org
287 output=$(notmuch search sekrit)
288 expected=''
289 test_expect_equal \
290     "$output" \
291     "$expected"
292
293 goodsig='good_sig:[0][0][0]["crypto"]["signed"]["status"][0]["status"]="good"'
294 nosig='no_sig:[0][0][0]["crypto"]!"signed"'
295
296 test_begin_subtest "verify signature without a session key stashed when --decrypt=true"
297 output=$(notmuch show --format=json --decrypt=true id:encrypted-signed@crypto.notmuchmail.org)
298 test_json_nodes <<<"$output" "$goodsig"
299
300 test_begin_subtest "do not verify sig without a session key stashed if --decrypt=auto"
301 output=$(notmuch show --format=json id:encrypted-signed@crypto.notmuchmail.org)
302 test_json_nodes <<<"$output" "$nosig"
303
304 test_begin_subtest "verify signature when --decrypt=stash"
305 output=$(notmuch show --format=json --decrypt=stash id:encrypted-signed@crypto.notmuchmail.org)
306 test_json_nodes <<<"$output" "$goodsig"
307
308 test_begin_subtest "verify signature with stashed session key"
309 output=$(notmuch show --format=json id:encrypted-signed@crypto.notmuchmail.org)
310 if [ $NOTMUCH_GMIME_VERIFY_WITH_SESSION_KEY -ne 1 ]; then
311     test_subtest_known_broken
312 fi
313 test_json_nodes <<<"$output" "$goodsig"
314
315 # TODO: test removal of a message from the message store between
316 # indexing and reindexing.
317
318 # TODO: insert the same message into the message store twice, index,
319 # remove one of them from the message store, and then reindex.
320 # reindexing should return a failure but the message should still be
321 # present? -- or what should the semantics be if you ask to reindex a
322 # message whose underlying files have been renamed or moved or
323 # removed?
324
325 test_done