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