]> git.notmuchmail.org Git - notmuch/blob - test/T357-index-decryption.sh
31991e22be98e158cc8d5ac73f6a12832cf96b9a
[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
52 test_begin_subtest "message should go away after deletion"
53 # cache the message in an env var and remove it:
54 fname=$(notmuch search --output=files wumpus)
55 contents="$(notmuch show --format=raw wumpus)"
56 rm -f "$fname"
57 notmuch new
58 output=$(notmuch search wumpus)
59 expected=''
60 test_expect_equal \
61     "$output" \
62     "$expected"
63
64 # try reinserting it without decryption, should stay the same:
65 test_begin_subtest "message cleartext not present after insert"
66 notmuch insert --folder=sent <<<"$contents"
67 output=$(notmuch search wumpus)
68 test_expect_equal \
69     "$output" \
70     "$expected"
71
72 # try reinserting it with decryption, should appear again, but now we
73 # have two copies of the message:
74 test_begin_subtest "message cleartext is present after reinserting with --decrypt=true"
75 notmuch insert --folder=sent --decrypt=true <<<"$contents"
76 output=$(notmuch search wumpus)
77 expected='thread:0000000000000003   2000-01-01 [1/1(2)] Notmuch Test Suite; test encrypted message for cleartext index 002 (encrypted inbox unread)'
78 test_expect_equal \
79     "$output" \
80     "$expected"
81
82 # remove all copies
83 test_begin_subtest "delete all copies of the message"
84 mid="$(notmuch search --output=messages wumpus)"
85 rm -f $(notmuch search --output=files wumpus)
86 notmuch new
87 output=$(notmuch search "id:$mid")
88 expected=''
89 test_expect_equal \
90     "$output" \
91     "$expected"
92
93 # try inserting it with decryption, should appear as a single copy
94 # (note: i think thread id skips 4 because of duplicate message-id
95 # insertion, above)
96 test_begin_subtest "message cleartext is present with insert --decrypt=true"
97 notmuch insert --folder=sent --decrypt=true <<<"$contents"
98 output=$(notmuch search wumpus)
99 expected='thread:0000000000000005   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 002 (encrypted inbox unread)'
100 test_expect_equal \
101     "$output" \
102     "$expected"
103
104
105 # add a tag to all messages to ensure that it stays after reindexing
106 test_begin_subtest 'tagging all messages'
107 test_expect_success 'notmuch tag +blarney "encrypted message"'
108 test_begin_subtest "verify that tags have not changed"
109 output=$(notmuch search tag:blarney)
110 expected='thread:0000000000000001   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 001 (blarney encrypted inbox)
111 thread:0000000000000005   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 002 (blarney encrypted inbox unread)'
112 test_expect_equal \
113     "$output" \
114     "$expected"
115
116 # see if first message shows up after reindexing with --decrypt=true (same $expected, untouched):
117 test_begin_subtest 'reindex old messages'
118 test_expect_success 'notmuch reindex --decrypt=true tag:encrypted and not property:index.decryption=success'
119 test_begin_subtest "reindexed encrypted message, including cleartext"
120 output=$(notmuch search wumpus)
121 test_expect_equal \
122     "$output" \
123     "$expected"
124
125 # and the same search, but by property ($expected is untouched):
126 test_begin_subtest "emacs search by property for both messages"
127 output=$(notmuch search property:index.decryption=success)
128 test_expect_equal \
129     "$output" \
130     "$expected"
131
132
133 # try to remove cleartext indexing
134 test_begin_subtest 'reindex without cleartext'
135 test_expect_success 'notmuch reindex tag:encrypted and property:index.decryption=success'
136 test_begin_subtest "reindexed encrypted messages, without cleartext"
137 output=$(notmuch search wumpus)
138 expected=''
139 test_expect_equal \
140     "$output" \
141     "$expected"
142
143 # ensure no session keys are present:
144 test_begin_subtest 'reindex using only session keys'
145 test_expect_success 'notmuch reindex --decrypt=auto tag:encrypted and property:index.decryption=success'
146 test_begin_subtest "reindexed encrypted messages, decrypting only with session keys"
147 output=$(notmuch search wumpus)
148 expected=''
149 test_expect_equal \
150     "$output" \
151     "$expected"
152
153 # and the same search, but by property ($expected is untouched):
154 test_begin_subtest "emacs search by property with both messages unindexed"
155 output=$(notmuch search property:index.decryption=success)
156 test_expect_equal \
157     "$output" \
158     "$expected"
159
160 # ensure that the tags remain even when we are dropping the cleartext.
161 test_begin_subtest "verify that tags remain without cleartext"
162 output=$(notmuch search tag:blarney)
163 expected='thread:0000000000000001   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 001 (blarney encrypted inbox)
164 thread:0000000000000005   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 002 (blarney encrypted inbox unread)'
165 test_expect_equal \
166     "$output" \
167     "$expected"
168
169 add_email_corpus crypto
170
171 test_begin_subtest "indexing message fails when secret key not available"
172 notmuch reindex --decrypt=true id:simple-encrypted@crypto.notmuchmail.org
173 output=$(notmuch dump )
174 expected='#notmuch-dump batch-tag:3 config,properties,tags
175 +encrypted +inbox +unread -- id:simple-encrypted@crypto.notmuchmail.org
176 #= simple-encrypted@crypto.notmuchmail.org index.decryption=failure'
177 test_expect_equal \
178     "$output" \
179     "$expected"
180
181 test_begin_subtest "cannot find cleartext index"
182 output=$(notmuch search sekrit)
183 expected=''
184 test_expect_equal \
185     "$output" \
186     "$expected"
187
188 test_begin_subtest "cleartext index recovery on reindexing with stashed session keys"
189 notmuch restore <<EOF
190 #notmuch-dump batch-tag:3 config,properties,tags
191 #= simple-encrypted@crypto.notmuchmail.org session-key=9%3AFC09987F5F927CC0CC0EE80A96E4C5BBF4A499818FB591207705DFDDD6112CF9
192 EOF
193 notmuch reindex --decrypt=auto id:simple-encrypted@crypto.notmuchmail.org
194 output=$(notmuch search sekrit)
195 expected='thread:0000000000000001   2016-12-22 [1/1] Daniel Kahn Gillmor; encrypted message (encrypted inbox unread)'
196 if [ $NOTMUCH_HAVE_GMIME_SESSION_KEYS -eq 0 ]; then
197     test_subtest_known_broken
198 fi
199 test_expect_equal \
200     "$output" \
201     "$expected"
202
203 test_begin_subtest "notmuch reply should show cleartext if session key is present"
204 output=$(notmuch reply id:simple-encrypted@crypto.notmuchmail.org | grep '^>')
205 expected='> This is a top sekrit message.'
206 if [ $NOTMUCH_HAVE_GMIME_SESSION_KEYS -eq 0 ]; then
207     test_subtest_known_broken
208 fi
209 test_expect_equal \
210     "$output" \
211     "$expected"
212
213
214 # TODO: test removal of a message from the message store between
215 # indexing and reindexing.
216
217 # TODO: insert the same message into the message store twice, index,
218 # remove one of them from the message store, and then reindex.
219 # reindexing should return a failure but the message should still be
220 # present? -- or what should the semantics be if you ask to reindex a
221 # message whose underlying files have been renamed or moved or
222 # removed?
223
224 test_done