]> git.notmuchmail.org Git - notmuch/blob - test/T357-index-decryption.sh
test: redirect STDIN from /dev/null
[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
12 # create a test encrypted message
13 test_begin_subtest 'emacs delivery of encrypted message'
14 test_expect_success \
15 'emacs_fcc_message \
16     "test encrypted message for cleartext index 001" \
17     "This is a test encrypted message with a wumpus.\n" \
18     "(mml-secure-message-encrypt)"'
19
20 test_begin_subtest "search for unindexed cleartext"
21 output=$(notmuch search wumpus)
22 expected=''
23 test_expect_equal \
24     "$output" \
25     "$expected"
26
27 # create a test encrypted message that is indexed in the clear
28 test_begin_subtest 'emacs delivery of encrypted message'
29 test_expect_success \
30 'emacs_fcc_message --decrypt=true \
31     "test encrypted message for cleartext index 002" \
32     "This is a test encrypted message with a wumpus.\n" \
33     "(mml-secure-message-encrypt)"'
34
35 test_begin_subtest "emacs delivery of encrypted message, indexed cleartext"
36 output=$(notmuch search wumpus)
37 expected='thread:0000000000000002   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 002 (encrypted inbox)'
38 test_expect_equal \
39     "$output" \
40     "$expected"
41
42 # and the same search, but by property ($expected is untouched):
43 test_begin_subtest "emacs search by property for one message"
44 output=$(notmuch search property:index.decryption=success)
45 test_expect_equal \
46     "$output" \
47     "$expected"
48
49 test_begin_subtest "show the message body of the encrypted message"
50 notmuch dump wumpus
51 output=$(notmuch show wumpus | notmuch_show_part 3)
52 expected='This is a test encrypted message with a wumpus.'
53 test_expect_equal \
54     "$output" \
55     "$expected"
56
57
58 test_begin_subtest "message should go away after deletion"
59 # cache the message in an env var and remove it:
60 fname=$(notmuch search --output=files wumpus)
61 contents="$(notmuch show --format=raw wumpus)"
62 rm -f "$fname"
63 notmuch new
64 output=$(notmuch search wumpus)
65 expected=''
66 test_expect_equal \
67     "$output" \
68     "$expected"
69
70 # try reinserting it without decryption, should stay the same:
71 test_begin_subtest "message cleartext not present after insert"
72 notmuch insert --folder=sent <<<"$contents"
73 output=$(notmuch search wumpus)
74 test_expect_equal \
75     "$output" \
76     "$expected"
77
78 # show the message using stashing decryption
79 test_begin_subtest "stash decryption during show"
80 output=$(notmuch show --decrypt=stash tag:encrypted subject:002 | notmuch_show_part 3)
81 expected='This is a test encrypted message with a wumpus.'
82 test_expect_equal \
83     "$output" \
84     "$expected"
85
86 test_begin_subtest "search should now find the contents"
87 output=$(notmuch search wumpus)
88 expected='thread:0000000000000003   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 002 (encrypted inbox unread)'
89 test_expect_equal \
90     "$output" \
91     "$expected"
92
93 # try reinserting it with decryption, should appear again, but now we
94 # have two copies of the message:
95 test_begin_subtest "message cleartext is present after reinserting with --decrypt=true"
96 notmuch insert --folder=sent --decrypt=true <<<"$contents"
97 output=$(notmuch search wumpus)
98 expected='thread:0000000000000003   2000-01-01 [1/1(2)] Notmuch Test Suite; test encrypted message for cleartext index 002 (encrypted inbox unread)'
99 test_expect_equal \
100     "$output" \
101     "$expected"
102
103 # remove all copies
104 test_begin_subtest "delete all copies of the message"
105 mid="$(notmuch search --output=messages wumpus)"
106 rm -f $(notmuch search --output=files wumpus)
107 notmuch new
108 output=$(notmuch search "id:$mid")
109 expected=''
110 test_expect_equal \
111     "$output" \
112     "$expected"
113
114 # try inserting it with decryption, should appear as a single copy
115 # (note: i think thread id skips 4 because of duplicate message-id
116 # insertion, above)
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)
120 expected='thread:0000000000000005   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)
131 expected='thread:0000000000000001   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 001 (blarney encrypted inbox)
132 thread:0000000000000005   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)
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)
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)
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)
192 expected='thread:0000000000000001   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 001 (blarney encrypted inbox)
193 thread:0000000000000005   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)
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:simple-encrypted@crypto.notmuchmail.org'
230 test_expect_equal \
231     "$output" \
232     "$expected"
233
234 test_begin_subtest "cannot find cleartext index"
235 output=$(notmuch search sekrit)
236 expected=''
237 test_expect_equal \
238     "$output" \
239     "$expected"
240
241 test_begin_subtest "cleartext index recovery on reindexing with stashed session keys"
242 notmuch restore <<EOF
243 #notmuch-dump batch-tag:3 config,properties,tags
244 #= simple-encrypted@crypto.notmuchmail.org session-key=9%3AFC09987F5F927CC0CC0EE80A96E4C5BBF4A499818FB591207705DFDDD6112CF9
245 EOF
246 notmuch reindex id:simple-encrypted@crypto.notmuchmail.org
247 output=$(notmuch search sekrit | notmuch_search_sanitize)
248 expected='thread:XXX   2016-12-22 [1/1] Daniel Kahn Gillmor; encrypted message (encrypted inbox unread)'
249 test_expect_equal \
250     "$output" \
251     "$expected"
252
253 test_begin_subtest "notmuch reply should show cleartext if session key is present"
254 output=$(notmuch reply id:simple-encrypted@crypto.notmuchmail.org | grep '^>')
255 expected='> This is a top sekrit message.'
256 test_expect_equal \
257     "$output" \
258     "$expected"
259
260 test_begin_subtest "notmuch show should show cleartext if session key is present"
261 output=$(notmuch show id:simple-encrypted@crypto.notmuchmail.org | notmuch_show_part 3)
262 expected='This is a top sekrit message.'
263 test_expect_equal \
264     "$output" \
265     "$expected"
266
267 test_begin_subtest "notmuch show should show nothing if decryption is explicitly disallowed"
268 output=$(notmuch show --decrypt=false id:simple-encrypted@crypto.notmuchmail.org | notmuch_show_part 3)
269 expected='Non-text part: application/octet-stream'
270 test_expect_equal \
271     "$output" \
272     "$expected"
273
274 test_begin_subtest "purging stashed session keys should lose access to the cleartext"
275 notmuch reindex --decrypt=false id:simple-encrypted@crypto.notmuchmail.org
276 output=$(notmuch search sekrit)
277 expected=''
278 test_expect_equal \
279     "$output" \
280     "$expected"
281
282 test_begin_subtest "and cleartext should be unrecoverable now that there are no stashed session keys"
283 notmuch dump
284 notmuch reindex --decrypt=true id:simple-encrypted@crypto.notmuchmail.org
285 output=$(notmuch search sekrit)
286 expected=''
287 test_expect_equal \
288     "$output" \
289     "$expected"
290
291
292 # TODO: test removal of a message from the message store between
293 # indexing and reindexing.
294
295 # TODO: insert the same message into the message store twice, index,
296 # remove one of them from the message store, and then reindex.
297 # reindexing should return a failure but the message should still be
298 # present? -- or what should the semantics be if you ask to reindex a
299 # message whose underlying files have been renamed or moved or
300 # removed?
301
302 test_done