]> 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
8 ##################################################
9
10 test_require_emacs
11 add_gnupg_home
12
13 # create a test encrypted message
14 test_begin_subtest 'emacs delivery of encrypted message'
15 test_expect_success \
16 'emacs_fcc_message \
17     "test encrypted message for cleartext index 001" \
18     "This is a test encrypted message with a wumpus.\n" \
19     "(mml-secure-message-encrypt)"'
20
21 test_begin_subtest "search for unindexed cleartext"
22 output=$(notmuch search wumpus)
23 expected=''
24 test_expect_equal \
25     "$output" \
26     "$expected"
27
28 # create a test encrypted message that is indexed in the clear
29 test_begin_subtest 'emacs delivery of encrypted message'
30 test_expect_success \
31 'emacs_fcc_message --decrypt=true \
32     "test encrypted message for cleartext index 002" \
33     "This is a test encrypted message with a wumpus.\n" \
34     "(mml-secure-message-encrypt)"'
35
36 test_begin_subtest "emacs delivery of encrypted message, indexed cleartext"
37 output=$(notmuch search wumpus)
38 expected='thread:0000000000000002   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 002 (encrypted inbox)'
39 test_expect_equal \
40     "$output" \
41     "$expected"
42
43 # and the same search, but by property ($expected is untouched):
44 test_begin_subtest "emacs search by property for one message"
45 output=$(notmuch search property:index.decryption=success)
46 test_expect_equal \
47     "$output" \
48     "$expected"
49
50 test_begin_subtest "show the message body of the encrypted message"
51 notmuch dump wumpus
52 output=$(notmuch show wumpus | notmuch_show_part 3)
53 expected='This is a test encrypted message with a wumpus.'
54 test_expect_equal \
55     "$output" \
56     "$expected"
57
58
59 test_begin_subtest "message should go away after deletion"
60 # cache the message in an env var and remove it:
61 fname=$(notmuch search --output=files wumpus)
62 contents="$(notmuch show --format=raw wumpus)"
63 rm -f "$fname"
64 notmuch new
65 output=$(notmuch search wumpus)
66 expected=''
67 test_expect_equal \
68     "$output" \
69     "$expected"
70
71 # try reinserting it without decryption, should stay the same:
72 test_begin_subtest "message cleartext not present after insert"
73 notmuch insert --folder=sent <<<"$contents"
74 output=$(notmuch search wumpus)
75 test_expect_equal \
76     "$output" \
77     "$expected"
78
79 # show the message using stashing decryption
80 test_begin_subtest "stash decryption during show"
81 output=$(notmuch show --decrypt=stash tag:encrypted subject:002 | notmuch_show_part 3)
82 expected='This is a test encrypted message with a wumpus.'
83 test_expect_equal \
84     "$output" \
85     "$expected"
86
87 test_begin_subtest "search should now find the contents"
88 output=$(notmuch search wumpus)
89 expected='thread:0000000000000003   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 002 (encrypted inbox unread)'
90 test_expect_equal \
91     "$output" \
92     "$expected"
93
94 # try reinserting it with decryption, should appear again, but now we
95 # have two copies of the message:
96 test_begin_subtest "message cleartext is present after reinserting with --decrypt=true"
97 notmuch insert --folder=sent --decrypt=true <<<"$contents"
98 output=$(notmuch search wumpus)
99 expected='thread:0000000000000003   2000-01-01 [1/1(2)] Notmuch Test Suite; test encrypted message for cleartext index 002 (encrypted inbox unread)'
100 test_expect_equal \
101     "$output" \
102     "$expected"
103
104 # remove all copies
105 test_begin_subtest "delete all copies of the message"
106 mid="$(notmuch search --output=messages wumpus)"
107 rm -f $(notmuch search --output=files wumpus)
108 notmuch new
109 output=$(notmuch search "id:$mid")
110 expected=''
111 test_expect_equal \
112     "$output" \
113     "$expected"
114
115 # try inserting it with decryption, should appear as a single copy
116 # (note: i think thread id skips 4 because of duplicate message-id
117 # insertion, above)
118 test_begin_subtest "message cleartext is present with insert --decrypt=true"
119 notmuch insert --folder=sent --decrypt=true <<<"$contents"
120 output=$(notmuch search wumpus)
121 expected='thread:0000000000000005   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 002 (encrypted inbox unread)'
122 test_expect_equal \
123     "$output" \
124     "$expected"
125
126
127 # add a tag to all messages to ensure that it stays after reindexing
128 test_begin_subtest 'tagging all messages'
129 test_expect_success 'notmuch tag +blarney "encrypted message"'
130 test_begin_subtest "verify that tags have not changed"
131 output=$(notmuch search tag:blarney)
132 expected='thread:0000000000000001   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 001 (blarney encrypted inbox)
133 thread:0000000000000005   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 002 (blarney encrypted inbox unread)'
134 test_expect_equal \
135     "$output" \
136     "$expected"
137
138 # see if first message shows up after reindexing with --decrypt=true (same $expected, untouched):
139 test_begin_subtest 'reindex old messages'
140 test_expect_success 'notmuch reindex --decrypt=true tag:encrypted and not property:index.decryption=success'
141 test_begin_subtest "reindexed encrypted message, including cleartext"
142 output=$(notmuch search wumpus)
143 test_expect_equal \
144     "$output" \
145     "$expected"
146
147 # and the same search, but by property ($expected is untouched):
148 test_begin_subtest "emacs search by property for both messages"
149 output=$(notmuch search property:index.decryption=success)
150 test_expect_equal \
151     "$output" \
152     "$expected"
153
154 # try a simple reindex
155 test_begin_subtest 'reindex in auto mode'
156 test_expect_success 'notmuch reindex tag:encrypted and property:index.decryption=success'
157 test_begin_subtest "reindexed encrypted messages, should not have changed"
158 output=$(notmuch search wumpus)
159 test_expect_equal \
160     "$output" \
161     "$expected"
162
163 # try to remove cleartext indexing
164 test_begin_subtest 'reindex without cleartext'
165 test_expect_success 'notmuch reindex --decrypt=false tag:encrypted and property:index.decryption=success'
166 test_begin_subtest "reindexed encrypted messages, without cleartext"
167 output=$(notmuch search wumpus)
168 expected=''
169 test_expect_equal \
170     "$output" \
171     "$expected"
172
173 # ensure no session keys are present:
174 test_begin_subtest 'reindex using only session keys'
175 test_expect_success 'notmuch reindex tag:encrypted and property:index.decryption=success'
176 test_begin_subtest "reindexed encrypted messages, decrypting only with session keys"
177 output=$(notmuch search wumpus)
178 expected=''
179 test_expect_equal \
180     "$output" \
181     "$expected"
182
183 # and the same search, but by property ($expected is untouched):
184 test_begin_subtest "emacs search by property with both messages unindexed"
185 output=$(notmuch search property:index.decryption=success)
186 test_expect_equal \
187     "$output" \
188     "$expected"
189
190 # ensure that the tags remain even when we are dropping the cleartext.
191 test_begin_subtest "verify that tags remain without cleartext"
192 output=$(notmuch search tag:blarney)
193 expected='thread:0000000000000001   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 001 (blarney encrypted inbox)
194 thread:0000000000000005   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 002 (blarney encrypted inbox unread)'
195 test_expect_equal \
196     "$output" \
197     "$expected"
198
199 test_begin_subtest "index cleartext without keeping session keys"
200 test_expect_success "notmuch reindex --decrypt=nostash tag:blarney"
201
202 test_begin_subtest "Ensure that the indexed terms are present"
203 output=$(notmuch search wumpus)
204 test_expect_equal \
205     "$output" \
206     "$expected"
207
208 test_begin_subtest "show one of the messages with --decrypt=true"
209 output=$(notmuch show --decrypt=true thread:0000000000000001 | notmuch_show_part 3)
210 expected='This is a test encrypted message with a wumpus.'
211 test_expect_equal \
212     "$output" \
213     "$expected"
214
215 test_begin_subtest "Ensure that we cannot show the message with --decrypt=auto"
216 output=$(notmuch show thread:0000000000000001 | notmuch_show_part 3)
217 expected='Non-text part: application/octet-stream'
218 test_expect_equal \
219     "$output" \
220     "$expected"
221
222 add_email_corpus crypto
223
224 test_begin_subtest "indexing message fails when secret key not available"
225 notmuch reindex --decrypt=true id:simple-encrypted@crypto.notmuchmail.org
226 output=$(notmuch dump | LC_ALL=C sort)
227 expected='#= simple-encrypted@crypto.notmuchmail.org index.decryption=failure
228 #notmuch-dump batch-tag:3 config,properties,tags
229 +encrypted +inbox +unread -- id:basic-encrypted@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