Skip to content

Commit 0c8f967

Browse files
committed
test: refine test_encrypted_no_autocrypt()
- Use TestContextManager - Actually run receive_imf rather than only mimeparser on "received" messages - Check that received message parts actually have a padlock
1 parent aca3437 commit 0c8f967

File tree

1 file changed

+23
-32
lines changed

1 file changed

+23
-32
lines changed

src/e2ee.rs

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,10 @@ pub async fn ensure_secret_key_exists(context: &Context) -> Result<()> {
169169
#[cfg(test)]
170170
mod tests {
171171
use super::*;
172-
use crate::chat;
173172
use crate::key::DcKey;
174173
use crate::message::{Message, Viewtype};
175174
use crate::param::Param;
176-
use crate::test_utils::{bob_keypair, TestContext};
175+
use crate::test_utils::{bob_keypair, TestContext, TestContextManager};
177176

178177
mod ensure_secret_key_exists {
179178
use super::*;
@@ -217,37 +216,35 @@ Sent with my Delta Chat Messenger: https://delta.chat";
217216

218217
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
219218
async fn test_encrypted_no_autocrypt() -> anyhow::Result<()> {
220-
let alice = TestContext::new_alice().await;
221-
let bob = TestContext::new_bob().await;
219+
let mut tcm = TestContextManager::new();
220+
let alice = tcm.alice().await;
221+
let bob = tcm.bob().await;
222222

223223
let chat_alice = alice.create_chat(&bob).await.id;
224224
let chat_bob = bob.create_chat(&alice).await.id;
225225

226226
// Alice sends unencrypted message to Bob
227227
let mut msg = Message::new(Viewtype::Text);
228-
chat::prepare_msg(&alice.ctx, chat_alice, &mut msg).await?;
229-
chat::send_msg(&alice.ctx, chat_alice, &mut msg).await?;
230-
let sent = alice.pop_sent_msg().await;
228+
let sent = alice.send_msg(chat_alice, &mut msg).await;
231229

232230
// Bob receives unencrypted message from Alice
233-
let msg = bob.parse_msg(&sent).await;
234-
assert!(!msg.was_encrypted());
231+
let msg = bob.recv_msg(&sent).await;
232+
assert!(!msg.get_showpadlock());
235233

236-
// Parsing a message is enough to update peerstate
237234
let peerstate_alice = Peerstate::from_addr(&bob.ctx, "alice@example.org")
238235
.await?
239236
.expect("no peerstate found in the database");
240237
assert_eq!(peerstate_alice.prefer_encrypt, EncryptPreference::Mutual);
241238

242-
// Bob sends encrypted message to Alice
239+
// Bob sends empty encrypted message to Alice
243240
let mut msg = Message::new(Viewtype::Text);
244-
chat::prepare_msg(&bob.ctx, chat_bob, &mut msg).await?;
245-
chat::send_msg(&bob.ctx, chat_bob, &mut msg).await?;
246-
let sent = bob.pop_sent_msg().await;
241+
let sent = bob.send_msg(chat_bob, &mut msg).await;
247242

248-
// Alice receives encrypted message from Bob
249-
let msg = alice.parse_msg(&sent).await;
250-
assert!(msg.was_encrypted());
243+
// Alice receives an empty encrypted message from Bob.
244+
// This is also a regression test for previously existing bug
245+
// that resulted in no padlock on encrypted empty messages.
246+
let msg = alice.recv_msg(&sent).await;
247+
assert!(msg.get_showpadlock());
251248

252249
let peerstate_bob = Peerstate::from_addr(&alice.ctx, "bob@example.net")
253250
.await?
@@ -259,12 +256,10 @@ Sent with my Delta Chat Messenger: https://delta.chat";
259256
// Alice sends encrypted message without Autocrypt header.
260257
let mut msg = Message::new(Viewtype::Text);
261258
msg.param.set_int(Param::SkipAutocrypt, 1);
262-
chat::prepare_msg(&alice.ctx, chat_alice, &mut msg).await?;
263-
chat::send_msg(&alice.ctx, chat_alice, &mut msg).await?;
264-
let sent = alice.pop_sent_msg().await;
259+
let sent = alice.send_msg(chat_alice, &mut msg).await;
265260

266-
let msg = bob.parse_msg(&sent).await;
267-
assert!(msg.was_encrypted());
261+
let msg = bob.recv_msg(&sent).await;
262+
assert!(msg.get_showpadlock());
268263
let peerstate_alice = Peerstate::from_addr(&bob.ctx, "alice@example.org")
269264
.await?
270265
.expect("no peerstate found in the database");
@@ -273,12 +268,10 @@ Sent with my Delta Chat Messenger: https://delta.chat";
273268
// Alice sends plaintext message with Autocrypt header.
274269
let mut msg = Message::new(Viewtype::Text);
275270
msg.force_plaintext();
276-
chat::prepare_msg(&alice.ctx, chat_alice, &mut msg).await?;
277-
chat::send_msg(&alice.ctx, chat_alice, &mut msg).await?;
278-
let sent = alice.pop_sent_msg().await;
271+
let sent = alice.send_msg(chat_alice, &mut msg).await;
279272

280-
let msg = bob.parse_msg(&sent).await;
281-
assert!(!msg.was_encrypted());
273+
let msg = bob.recv_msg(&sent).await;
274+
assert!(!msg.get_showpadlock());
282275
let peerstate_alice = Peerstate::from_addr(&bob.ctx, "alice@example.org")
283276
.await?
284277
.expect("no peerstate found in the database");
@@ -288,12 +281,10 @@ Sent with my Delta Chat Messenger: https://delta.chat";
288281
let mut msg = Message::new(Viewtype::Text);
289282
msg.force_plaintext();
290283
msg.param.set_int(Param::SkipAutocrypt, 1);
291-
chat::prepare_msg(&alice.ctx, chat_alice, &mut msg).await?;
292-
chat::send_msg(&alice.ctx, chat_alice, &mut msg).await?;
293-
let sent = alice.pop_sent_msg().await;
284+
let sent = alice.send_msg(chat_alice, &mut msg).await;
294285

295-
let msg = bob.parse_msg(&sent).await;
296-
assert!(!msg.was_encrypted());
286+
let msg = bob.recv_msg(&sent).await;
287+
assert!(!msg.get_showpadlock());
297288
let peerstate_alice = Peerstate::from_addr(&bob.ctx, "alice@example.org")
298289
.await?
299290
.expect("no peerstate found in the database");

0 commit comments

Comments
 (0)