]> git.notmuchmail.org Git - notmuch/blob - test/T357-index-decryption.sh
Merge tag 0.28.4
[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 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 # (note: i think thread id skips 4 because of duplicate message-id
118 # insertion, above)
119 test_begin_subtest "message cleartext is present with insert --decrypt=true"
120 notmuch insert --folder=sent --decrypt=true <<<"$contents"
121 output=$(notmuch search wumpus)
122 expected='thread:0000000000000005   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 002 (encrypted inbox unread)'
123 test_expect_equal \
124     "$output" \
125     "$expected"
126
127
128 # add a tag to all messages to ensure that it stays after reindexing
129 test_begin_subtest 'tagging all messages'
130 test_expect_success 'notmuch tag +blarney "encrypted message"'
131 test_begin_subtest "verify that tags have not changed"
132 output=$(notmuch search tag:blarney)
133 expected='thread:0000000000000001   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 001 (blarney encrypted inbox)
134 thread:0000000000000005   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 002 (blarney encrypted inbox unread)'
135 test_expect_equal \
136     "$output" \
137     "$expected"
138
139 # see if first message shows up after reindexing with --decrypt=true (same $expected, untouched):
140 test_begin_subtest 'reindex old messages'
141 test_expect_success 'notmuch reindex --decrypt=true tag:encrypted and not property:index.decryption=success'
142 test_begin_subtest "reindexed encrypted message, including cleartext"
143 output=$(notmuch search wumpus)
144 test_expect_equal \
145     "$output" \
146     "$expected"
147
148 # and the same search, but by property ($expected is untouched):
149 test_begin_subtest "emacs search by property for both messages"
150 output=$(notmuch search property:index.decryption=success)
151 test_expect_equal \
152     "$output" \
153     "$expected"
154
155 # try a simple reindex
156 test_begin_subtest 'reindex in auto mode'
157 test_expect_success 'notmuch reindex tag:encrypted and property:index.decryption=success'
158 test_begin_subtest "reindexed encrypted messages, should not have changed"
159 output=$(notmuch search wumpus)
160 test_expect_equal \
161     "$output" \
162     "$expected"
163
164 # try to remove cleartext indexing
165 test_begin_subtest 'reindex without cleartext'
166 test_expect_success 'notmuch reindex --decrypt=false tag:encrypted and property:index.decryption=success'
167 test_begin_subtest "reindexed encrypted messages, without cleartext"
168 output=$(notmuch search wumpus)
169 expected=''
170 test_expect_equal \
171     "$output" \
172     "$expected"
173
174 # ensure no session keys are present:
175 test_begin_subtest 'reindex using only session keys'
176 test_expect_success 'notmuch reindex tag:encrypted and property:index.decryption=success'
177 test_begin_subtest "reindexed encrypted messages, decrypting only with session keys"
178 output=$(notmuch search wumpus)
179 expected=''
180 test_expect_equal \
181     "$output" \
182     "$expected"
183
184 # and the same search, but by property ($expected is untouched):
185 test_begin_subtest "emacs search by property with both messages unindexed"
186 output=$(notmuch search property:index.decryption=success)
187 test_expect_equal \
188     "$output" \
189     "$expected"
190
191 # ensure that the tags remain even when we are dropping the cleartext.
192 test_begin_subtest "verify that tags remain without cleartext"
193 output=$(notmuch search tag:blarney)
194 expected='thread:0000000000000001   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 001 (blarney encrypted inbox)
195 thread:0000000000000005   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 002 (blarney encrypted inbox unread)'
196 test_expect_equal \
197     "$output" \
198     "$expected"
199
200 test_begin_subtest "index cleartext without keeping session keys"
201 test_expect_success "notmuch reindex --decrypt=nostash tag:blarney"
202
203 test_begin_subtest "Ensure that the indexed terms are present"
204 output=$(notmuch search wumpus)
205 test_expect_equal \
206     "$output" \
207     "$expected"
208
209 test_begin_subtest "show one of the messages with --decrypt=true"
210 output=$(notmuch show --decrypt=true thread:0000000000000001 | notmuch_show_part 3)
211 expected='This is a test encrypted message with a wumpus.'
212 test_expect_equal \
213     "$output" \
214     "$expected"
215
216 test_begin_subtest "Ensure that we cannot show the message with --decrypt=auto"
217 output=$(notmuch show thread:0000000000000001 | notmuch_show_part 3)
218 expected='Non-text part: application/octet-stream'
219 test_expect_equal \
220     "$output" \
221     "$expected"
222
223 add_email_corpus crypto
224
225 test_begin_subtest "indexing message fails when secret key not available"
226 notmuch reindex --decrypt=true id:simple-encrypted@crypto.notmuchmail.org
227 output=$(notmuch dump )
228 expected='#notmuch-dump batch-tag:3 config,properties,tags
229 +encrypted +inbox +unread -- id:simple-encrypted@crypto.notmuchmail.org
230 #= simple-encrypted@crypto.notmuchmail.org index.decryption=failure'
231 test_expect_equal \
232     "$output" \
233     "$expected"
234
235 test_begin_subtest "cannot find cleartext index"
236 output=$(notmuch search sekrit)
237 expected=''
238 test_expect_equal \
239     "$output" \
240     "$expected"
241
242 test_begin_subtest "cleartext index recovery on reindexing with stashed session keys"
243 notmuch restore <<EOF
244 #notmuch-dump batch-tag:3 config,properties,tags
245 #= simple-encrypted@crypto.notmuchmail.org session-key=9%3AFC09987F5F927CC0CC0EE80A96E4C5BBF4A499818FB591207705DFDDD6112CF9
246 EOF
247 notmuch reindex id:simple-encrypted@crypto.notmuchmail.org
248 output=$(notmuch search sekrit)
249 expected='thread:0000000000000001   2016-12-22 [1/1] Daniel Kahn Gillmor; encrypted message (encrypted inbox unread)'
250 test_expect_equal \
251     "$output" \
252     "$expected"
253
254 test_begin_subtest "notmuch reply should show cleartext if session key is present"
255 output=$(notmuch reply id:simple-encrypted@crypto.notmuchmail.org | grep '^>')
256 expected='> This is a top sekrit message.'
257 test_expect_equal \
258     "$output" \
259     "$expected"
260
261 test_begin_subtest "notmuch show should show cleartext if session key is present"
262 output=$(notmuch show id:simple-encrypted@crypto.notmuchmail.org | notmuch_show_part 3)
263 expected='This is a top sekrit message.'
264 test_expect_equal \
265     "$output" \
266     "$expected"
267
268 test_begin_subtest "notmuch show should show nothing if decryption is explicitly disallowed"
269 output=$(notmuch show --decrypt=false id:simple-encrypted@crypto.notmuchmail.org | notmuch_show_part 3)
270 expected='Non-text part: application/octet-stream'
271 test_expect_equal \
272     "$output" \
273     "$expected"
274
275 test_begin_subtest "purging stashed session keys should lose access to the cleartext"
276 notmuch reindex --decrypt=false id:simple-encrypted@crypto.notmuchmail.org
277 output=$(notmuch search sekrit)
278 expected=''
279 test_expect_equal \
280     "$output" \
281     "$expected"
282
283 test_begin_subtest "and cleartext should be unrecoverable now that there are no stashed session keys"
284 notmuch dump
285 notmuch reindex --decrypt=true id:simple-encrypted@crypto.notmuchmail.org
286 output=$(notmuch search sekrit)
287 expected=''
288 test_expect_equal \
289     "$output" \
290     "$expected"
291
292
293 # TODO: test removal of a message from the message store between
294 # indexing and reindexing.
295
296 # TODO: insert the same message into the message store twice, index,
297 # remove one of them from the message store, and then reindex.
298 # reindexing should return a failure but the message should still be
299 # present? -- or what should the semantics be if you ask to reindex a
300 # message whose underlying files have been renamed or moved or
301 # removed?
302
303 test_done