Skip to content

Commit 59fac54

Browse files
authored
test: Add unique offsets to ids generated by TestContext to increase test correctness (#7297)
and fix the mistakes in tests that get discovered by this. closes #6799
1 parent 65b61ef commit 59fac54

14 files changed

+137
-60
lines changed

src/chat/chat_tests.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ async fn test_msg_with_implicit_member_removed() -> Result<()> {
483483
// If Bob sends a message to Alice now, Fiona is removed.
484484
assert_eq!(get_chat_contacts(&alice, alice_chat_id).await?.len(), 3);
485485
let sent_msg = bob
486-
.send_text(alice_chat_id, "I have removed Fiona some time ago.")
486+
.send_text(bob_chat_id, "I have removed Fiona some time ago.")
487487
.await;
488488
alice.recv_msg(&sent_msg).await;
489489
assert_eq!(get_chat_contacts(&alice, alice_chat_id).await?.len(), 2);
@@ -2412,14 +2412,14 @@ async fn test_forward_from_saved_to_saved() -> Result<()> {
24122412
let bob = TestContext::new_bob().await;
24132413
let sent = alice.send_text(alice.create_chat(&bob).await.id, "k").await;
24142414

2415-
bob.recv_msg(&sent).await;
2415+
let received_message = bob.recv_msg(&sent).await;
24162416
let orig = bob.get_last_msg().await;
24172417
let self_chat = bob.get_self_chat().await;
24182418
save_msgs(&bob, &[orig.id]).await?;
24192419
let saved1 = bob.get_last_msg().await;
24202420
assert_eq!(
24212421
saved1.get_original_msg_id(&bob).await?.unwrap(),
2422-
sent.sender_msg_id
2422+
received_message.id
24232423
);
24242424
assert_ne!(saved1.from_id, ContactId::SELF);
24252425

@@ -2644,7 +2644,7 @@ async fn test_broadcast() -> Result<()> {
26442644
add_contact_to_chat(
26452645
&alice,
26462646
broadcast_id,
2647-
get_chat_contacts(&alice, chat_bob.id).await?.pop().unwrap(),
2647+
get_chat_contacts(&alice, msg.chat_id).await?.pop().unwrap(),
26482648
)
26492649
.await?;
26502650
let fiona_contact_id = alice.add_or_lookup_contact_id(&fiona).await;
@@ -4013,38 +4013,40 @@ async fn test_info_contact_id() -> Result<()> {
40134013
)
40144014
.await?;
40154015

4016-
let fiona_id = alice.add_or_lookup_contact_id(&tcm.fiona().await).await; // contexts are in sync, fiona_id is same everywhere
4017-
add_contact_to_chat(alice, alice_chat_id, fiona_id).await?;
4016+
let alice_fiona_id = alice.add_or_lookup_contact_id(&tcm.fiona().await).await;
4017+
let bob_fiona_id = bob.add_or_lookup_contact_id(&tcm.fiona().await).await;
4018+
add_contact_to_chat(alice, alice_chat_id, alice_fiona_id).await?;
40184019
pop_recv_and_check(
40194020
alice,
40204021
alice2,
40214022
bob,
40224023
SystemMessage::MemberAddedToGroup,
4023-
fiona_id,
4024-
fiona_id,
4024+
alice_fiona_id,
4025+
bob_fiona_id,
40254026
)
40264027
.await?;
40274028

4028-
remove_contact_from_chat(alice, alice_chat_id, fiona_id).await?;
4029+
remove_contact_from_chat(alice, alice_chat_id, alice_fiona_id).await?;
40294030
pop_recv_and_check(
40304031
alice,
40314032
alice2,
40324033
bob,
40334034
SystemMessage::MemberRemovedFromGroup,
4034-
fiona_id,
4035-
fiona_id,
4035+
alice_fiona_id,
4036+
bob_fiona_id,
40364037
)
40374038
.await?;
40384039

40394040
// When fiona_id is deleted, get_info_contact_id() returns None.
40404041
// We raw delete in db as Contact::delete() leaves a tombstone (which is great as the tap works longer then)
40414042
alice
40424043
.sql
4043-
.execute("DELETE FROM contacts WHERE id=?", (fiona_id,))
4044+
.execute("DELETE FROM contacts WHERE id=?", (alice_fiona_id,))
40444045
.await?;
40454046
let msg = alice.get_last_msg().await;
40464047
assert_eq!(msg.get_info_type(), SystemMessage::MemberRemovedFromGroup);
40474048
assert!(msg.get_info_contact_id(alice).await?.is_none());
4049+
assert!(msg.get_info_contact_id(bob).await?.is_none());
40484050

40494051
Ok(())
40504052
}

src/peer_channels.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ mod tests {
616616
loop {
617617
let event = bob.evtracker.recv().await.unwrap();
618618
if let EventType::WebxdcRealtimeAdvertisementReceived { msg_id } = event.typ {
619-
assert!(msg_id == alice_webxdc.id);
619+
assert!(msg_id == bob_webxdc.id);
620620
break;
621621
}
622622
}

src/receive_imf/receive_imf_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2901,9 +2901,9 @@ async fn test_accept_outgoing() -> Result<()> {
29012901
let bob1_chat = bob1.create_chat(&alice1).await;
29022902
let sent = bob1.send_text(bob1_chat.id, "Hello!").await;
29032903

2904-
alice1.recv_msg(&sent).await;
2904+
let alice1_msg = alice1.recv_msg(&sent).await;
29052905
alice2.recv_msg(&sent).await;
2906-
let alice1_msg = bob2.recv_msg(&sent).await;
2906+
bob2.recv_msg(&sent).await;
29072907
assert_eq!(alice1_msg.text, "Hello!");
29082908
let alice1_chat = chat::Chat::load_from_db(&alice1, alice1_msg.chat_id).await?;
29092909
assert!(alice1_chat.is_contact_request());

src/sql/migrations/migrations_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ async fn test_key_contacts_migration_email2() -> Result<()> {
122122
.await?
123123
.is_empty()
124124
);
125-
let pgp_bob = Contact::get_by_id(&t, ContactId::new(11)).await?;
125+
let pgp_bob = Contact::get_by_id(&t, ContactId::new(11001)).await?;
126126
assert_eq!(pgp_bob.is_key_contact(), true);
127127
assert_eq!(pgp_bob.origin, Origin::Hidden);
128128

src/test_utils.rs

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ impl TestContextManager {
8484
pub async fn alice(&mut self) -> TestContext {
8585
TestContext::builder()
8686
.configure_alice()
87+
.with_id_offset(1000)
8788
.with_log_sink(self.log_sink.clone())
8889
.build(Some(&mut self.used_names))
8990
.await
@@ -92,6 +93,7 @@ impl TestContextManager {
9293
pub async fn bob(&mut self) -> TestContext {
9394
TestContext::builder()
9495
.configure_bob()
96+
.with_id_offset(2000)
9597
.with_log_sink(self.log_sink.clone())
9698
.build(Some(&mut self.used_names))
9799
.await
@@ -100,6 +102,7 @@ impl TestContextManager {
100102
pub async fn charlie(&mut self) -> TestContext {
101103
TestContext::builder()
102104
.configure_charlie()
105+
.with_id_offset(3000)
103106
.with_log_sink(self.log_sink.clone())
104107
.build(Some(&mut self.used_names))
105108
.await
@@ -108,6 +111,7 @@ impl TestContextManager {
108111
pub async fn dom(&mut self) -> TestContext {
109112
TestContext::builder()
110113
.configure_dom()
114+
.with_id_offset(4000)
111115
.with_log_sink(self.log_sink.clone())
112116
.build(Some(&mut self.used_names))
113117
.await
@@ -116,6 +120,7 @@ impl TestContextManager {
116120
pub async fn elena(&mut self) -> TestContext {
117121
TestContext::builder()
118122
.configure_elena()
123+
.with_id_offset(5000)
119124
.with_log_sink(self.log_sink.clone())
120125
.build(Some(&mut self.used_names))
121126
.await
@@ -124,6 +129,7 @@ impl TestContextManager {
124129
pub async fn fiona(&mut self) -> TestContext {
125130
TestContext::builder()
126131
.configure_fiona()
132+
.with_id_offset(6000)
127133
.with_log_sink(self.log_sink.clone())
128134
.build(Some(&mut self.used_names))
129135
.await
@@ -263,6 +269,11 @@ pub struct TestContextBuilder {
263269
/// so the caller should store the LogSink elsewhere to
264270
/// prevent it from being dropped immediately.
265271
log_sink: Option<LogSink>,
272+
273+
/// Offset for chat-,message-,contact ids.
274+
///
275+
/// This makes tests fail where ids from different accounts were mixed up.
276+
id_offset: Option<u32>,
266277
}
267278

268279
impl TestContextBuilder {
@@ -328,6 +339,14 @@ impl TestContextBuilder {
328339
self
329340
}
330341

342+
/// Adds an offset for chat-, message-, contact IDs.
343+
///
344+
/// This makes it harder to accidentally mix up IDs from different accounts.
345+
pub fn with_id_offset(mut self, offset: u32) -> Self {
346+
self.id_offset = Some(offset);
347+
self
348+
}
349+
331350
/// Builds the [`TestContext`].
332351
pub async fn build(self, used_names: Option<&mut BTreeSet<String>>) -> TestContext {
333352
if let Some(key_pair) = self.key_pair {
@@ -360,6 +379,22 @@ impl TestContextBuilder {
360379
key::store_self_keypair(&test_context, &key_pair)
361380
.await
362381
.expect("Failed to save key");
382+
383+
if let Some(offset) = self.id_offset {
384+
test_context
385+
.ctx
386+
.sql
387+
.execute(
388+
"UPDATE sqlite_sequence SET seq = ?
389+
WHERE name = 'contacts'
390+
OR name = 'chats'
391+
OR name = 'msgs'",
392+
(offset,),
393+
)
394+
.await
395+
.expect("Failed set id offset");
396+
}
397+
363398
test_context
364399
} else {
365400
TestContext::new_internal(None, self.log_sink).await
@@ -409,21 +444,33 @@ impl TestContext {
409444
///
410445
/// This is a shortcut which configures alice@example.org with a fixed key.
411446
pub async fn new_alice() -> Self {
412-
Self::builder().configure_alice().build(None).await
447+
Self::builder()
448+
.configure_alice()
449+
.with_id_offset(11000)
450+
.build(None)
451+
.await
413452
}
414453

415454
/// Creates a new configured [`TestContext`].
416455
///
417456
/// This is a shortcut which configures bob@example.net with a fixed key.
418457
pub async fn new_bob() -> Self {
419-
Self::builder().configure_bob().build(None).await
458+
Self::builder()
459+
.configure_bob()
460+
.with_id_offset(12000)
461+
.build(None)
462+
.await
420463
}
421464

422465
/// Creates a new configured [`TestContext`].
423466
///
424467
/// This is a shortcut which configures fiona@example.net with a fixed key.
425468
pub async fn new_fiona() -> Self {
426-
Self::builder().configure_fiona().build(None).await
469+
Self::builder()
470+
.configure_fiona()
471+
.with_id_offset(13000)
472+
.build(None)
473+
.await
427474
}
428475

429476
/// Print current chat state.
@@ -1624,4 +1671,32 @@ mod tests {
16241671
let runtime = tokio::runtime::Runtime::new().expect("unable to create tokio runtime");
16251672
runtime.block_on(TestContext::new());
16261673
}
1674+
1675+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1676+
async fn test_id_offset() {
1677+
let mut tcm = TestContextManager::new();
1678+
let alice = tcm.alice().await;
1679+
let bob = tcm.bob().await;
1680+
let fiona = tcm.fiona().await;
1681+
1682+
// chat ids
1683+
let alice_bob_chat = alice.create_chat(&bob).await;
1684+
let bob_alice_chat = bob.create_chat(&alice).await;
1685+
assert_ne!(alice_bob_chat.id, bob_alice_chat.id);
1686+
1687+
// contact ids
1688+
let alice_fiona_contact_id = alice.add_or_lookup_contact_id(&fiona).await;
1689+
let fiona_fiona_contact_id = bob.add_or_lookup_contact_id(&fiona).await;
1690+
assert_ne!(alice_fiona_contact_id, fiona_fiona_contact_id);
1691+
1692+
// message ids
1693+
let alice_group_id = alice
1694+
.create_group_with_members("test group", &[&bob, &fiona])
1695+
.await;
1696+
let alice_sent_msg = alice.send_text(alice_group_id, "testing").await;
1697+
let bob_received_id = bob.recv_msg(&alice_sent_msg).await;
1698+
assert_ne!(alice_sent_msg.sender_msg_id, bob_received_id.id);
1699+
let fiona_received_id = fiona.recv_msg(&alice_sent_msg).await;
1700+
assert_ne!(bob_received_id.id, fiona_received_id.id);
1701+
}
16271702
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
Group#Chat#10: Group chat [3 member(s)]
1+
Group#Chat#2001: Group chat [3 member(s)]
22
--------------------------------------------------------------------------------
3-
Msg#10: info (Contact#Contact#Info): Messages are end-to-end encrypted. [NOTICED][INFO]
4-
Msg#11🔒: (Contact#Contact#10): Hi! I created a group. [FRESH]
5-
Msg#12🔒: Me (Contact#Contact#Self): You left the group. [INFO] √
6-
Msg#13🔒: (Contact#Contact#10): Member charlie@example.net added by alice@example.org. [FRESH][INFO]
7-
Msg#14🔒: (Contact#Contact#10): What a silence! [FRESH]
3+
Msg#2001: info (Contact#Contact#Info): Messages are end-to-end encrypted. [NOTICED][INFO]
4+
Msg#2002🔒: (Contact#Contact#2001): Hi! I created a group. [FRESH]
5+
Msg#2003🔒: Me (Contact#Contact#Self): You left the group. [INFO] √
6+
Msg#2004🔒: (Contact#Contact#2001): Member charlie@example.net added by alice@example.org. [FRESH][INFO]
7+
Msg#2005🔒: (Contact#Contact#2001): What a silence! [FRESH]
88
--------------------------------------------------------------------------------
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
Group#Chat#10: Group [5 member(s)]
1+
Group#Chat#1001: Group [5 member(s)]
22
--------------------------------------------------------------------------------
3-
Msg#10: info (Contact#Contact#Info): Messages are end-to-end encrypted. [NOTICED][INFO]
4-
Msg#11🔒: Me (Contact#Contact#Self): populate √
5-
Msg#12: info (Contact#Contact#Info): Member dom@example.net added. [NOTICED][INFO]
6-
Msg#13: info (Contact#Contact#Info): Member fiona@example.net removed. [NOTICED][INFO]
7-
Msg#14🔒: (Contact#Contact#10): Member elena@example.net added by bob@example.net. [FRESH][INFO]
8-
Msg#15🔒: Me (Contact#Contact#Self): You added member fiona@example.net. [INFO] o
9-
Msg#16🔒: (Contact#Contact#10): Member fiona@example.net removed by bob@example.net. [FRESH][INFO]
3+
Msg#1001: info (Contact#Contact#Info): Messages are end-to-end encrypted. [NOTICED][INFO]
4+
Msg#1002🔒: Me (Contact#Contact#Self): populate √
5+
Msg#1003: info (Contact#Contact#Info): Member dom@example.net added. [NOTICED][INFO]
6+
Msg#1004: info (Contact#Contact#Info): Member fiona@example.net removed. [NOTICED][INFO]
7+
Msg#1005🔒: (Contact#Contact#1001): Member elena@example.net added by bob@example.net. [FRESH][INFO]
8+
Msg#1006🔒: Me (Contact#Contact#Self): You added member fiona@example.net. [INFO] o
9+
Msg#1007🔒: (Contact#Contact#1001): Member fiona@example.net removed by bob@example.net. [FRESH][INFO]
1010
--------------------------------------------------------------------------------
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Single#Chat#10: bob@example.net [bob@example.net] Icon: 4138c52e5bc1c576cda7dd44d088c07.png
1+
Single#Chat#1001: bob@example.net [bob@example.net] Icon: 4138c52e5bc1c576cda7dd44d088c07.png
22
--------------------------------------------------------------------------------
3-
Msg#10: Me (Contact#Contact#Self): We share this account √
4-
Msg#11: Me (Contact#Contact#Self): I'm Alice too √
3+
Msg#1001: Me (Contact#Contact#Self): We share this account √
4+
Msg#1002: Me (Contact#Contact#Self): I'm Alice too √
55
--------------------------------------------------------------------------------
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Single#Chat#10: Bob [bob@example.net] Icon: 4138c52e5bc1c576cda7dd44d088c07.png
1+
Single#Chat#11001: Bob [bob@example.net] Icon: 4138c52e5bc1c576cda7dd44d088c07.png
22
--------------------------------------------------------------------------------
3-
Msg#10: Me (Contact#Contact#Self): Happy birthday, Bob! √
4-
Msg#11: (Contact#Contact#10): Happy birthday to me, Alice! [FRESH]
3+
Msg#11001: Me (Contact#Contact#Self): Happy birthday, Bob! √
4+
Msg#11002: (Contact#Contact#11001): Happy birthday to me, Alice! [FRESH]
55
--------------------------------------------------------------------------------
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Single#Chat#10: bob@example.net [KEY bob@example.net]
1+
Single#Chat#1001: bob@example.net [KEY bob@example.net]
22
--------------------------------------------------------------------------------
3-
Msg#10: info (Contact#Contact#Info): Messages are end-to-end encrypted. [NOTICED][INFO]
4-
Msg#11🔒: Me (Contact#Contact#Self): Test – This is encrypted, signed, and has an Autocrypt Header without prefer-encrypt=mutual. √
3+
Msg#1001: info (Contact#Contact#Info): Messages are end-to-end encrypted. [NOTICED][INFO]
4+
Msg#1002🔒: Me (Contact#Contact#Self): Test – This is encrypted, signed, and has an Autocrypt Header without prefer-encrypt=mutual. √
55
--------------------------------------------------------------------------------

0 commit comments

Comments
 (0)