File size: 50,419 Bytes
0220cd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
use std::time::Duration;

use deltachat_contact_tools::EmailAddress;

use super::*;
use crate::chat::{CantSendReason, add_contact_to_chat, remove_contact_from_chat};
use crate::chatlist::Chatlist;
use crate::constants::Chattype;
use crate::constants::DC_CHAT_ID_TRASH;
use crate::key::self_fingerprint;
use crate::mimeparser::{GossipedKey, SystemMessage};
use crate::qr::Qr;
use crate::receive_imf::receive_imf;
use crate::stock_str::{self, messages_e2e_encrypted};
use crate::test_utils::{
    AVATAR_64x64_BYTES, AVATAR_64x64_DEDUPLICATED, TestContext, TestContextManager,
    TimeShiftFalsePositiveNote, get_chat_msg,
};
use crate::tools::SystemTime;

#[derive(PartialEq)]
enum SetupContactCase {
    Normal,
    WrongAliceGossip,
    AliceIsBot,
    AliceHasName,
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_setup_contact() {
    test_setup_contact_ex(SetupContactCase::Normal).await
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_setup_contact_wrong_alice_gossip() {
    test_setup_contact_ex(SetupContactCase::WrongAliceGossip).await
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_setup_contact_alice_is_bot() {
    test_setup_contact_ex(SetupContactCase::AliceIsBot).await
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_setup_contact_alice_has_name() {
    test_setup_contact_ex(SetupContactCase::AliceHasName).await
}

async fn test_setup_contact_ex(case: SetupContactCase) {
    let _n = TimeShiftFalsePositiveNote;

    let mut tcm = TestContextManager::new();
    let alice = tcm.alice().await;
    let alice_addr = &alice.get_config(Config::Addr).await.unwrap().unwrap();
    if case == SetupContactCase::AliceHasName {
        alice
            .set_config(Config::Displayname, Some("Alice"))
            .await
            .unwrap();
    }
    let bob = tcm.bob().await;
    bob.set_config(Config::Displayname, Some("Bob Examplenet"))
        .await
        .unwrap();
    let alice_auto_submitted_hdr;
    match case {
        SetupContactCase::AliceIsBot => {
            alice.set_config_bool(Config::Bot, true).await.unwrap();
            alice_auto_submitted_hdr = "Auto-Submitted: auto-generated";
        }
        _ => alice_auto_submitted_hdr = "Auto-Submitted: auto-replied",
    };

    assert_eq!(
        Chatlist::try_load(&alice, 0, None, None)
            .await
            .unwrap()
            .len(),
        0
    );
    assert_eq!(
        Chatlist::try_load(&bob, 0, None, None).await.unwrap().len(),
        0
    );

    tcm.section("Step 1: Generate QR-code, ChatId(0) indicates setup-contact");
    let qr = get_securejoin_qr(&alice, None).await.unwrap();
    // We want Bob to learn Alice's name from their messages, not from the QR code.
    alice
        .set_config(Config::Displayname, Some("Alice Exampleorg"))
        .await
        .unwrap();

    tcm.section("Step 2: Bob scans QR-code, sends vc-request");
    let bob_chat_id = join_securejoin(&bob.ctx, &qr).await.unwrap();
    assert_eq!(
        Chatlist::try_load(&bob, 0, None, None).await.unwrap().len(),
        1
    );
    let bob_chat = Chat::load_from_db(&bob, bob_chat_id).await.unwrap();
    assert_eq!(
        bob_chat.why_cant_send(&bob).await.unwrap(),
        Some(CantSendReason::MissingKey)
    );

    // Check Bob's info messages.
    let msg_cnt = 2;
    let mut i = 0..msg_cnt;
    let msg = get_chat_msg(&bob, bob_chat.get_id(), i.next().unwrap(), msg_cnt).await;
    assert!(msg.is_info());
    assert_eq!(msg.get_text(), messages_e2e_encrypted(&bob).await);
    let msg = get_chat_msg(&bob, bob_chat.get_id(), i.next().unwrap(), msg_cnt).await;
    assert!(msg.is_info());
    assert_eq!(msg.get_text(), stock_str::securejoin_wait(&bob).await);

    let contact_alice_id = bob.add_or_lookup_contact_no_key(&alice).await.id;
    let sent = bob.pop_sent_msg().await;
    assert!(!sent.payload.contains("Bob Examplenet"));
    assert_eq!(sent.recipient(), EmailAddress::new(alice_addr).unwrap());
    let msg = alice.parse_msg(&sent).await;
    assert!(!msg.was_encrypted());
    assert_eq!(msg.get_header(HeaderDef::SecureJoin).unwrap(), "vc-request");
    assert!(msg.get_header(HeaderDef::SecureJoinInvitenumber).is_some());
    assert!(!msg.header_exists(HeaderDef::AutoSubmitted));

    tcm.section("Step 3: Alice receives vc-request, sends vc-auth-required");
    alice.recv_msg_trash(&sent).await;
    assert_eq!(
        Chatlist::try_load(&alice, 0, None, None)
            .await
            .unwrap()
            .len(),
        0
    );

    let sent = alice.pop_sent_msg().await;
    assert!(sent.payload.contains(alice_auto_submitted_hdr));
    assert!(!sent.payload.contains("Alice Exampleorg"));
    let msg = bob.parse_msg(&sent).await;
    assert!(msg.was_encrypted());
    assert_eq!(
        msg.get_header(HeaderDef::SecureJoin).unwrap(),
        "vc-auth-required"
    );

    let bob_chat = bob.get_chat(&alice).await;
    assert_eq!(bob_chat.can_send(&bob).await.unwrap(), true);

    tcm.section("Step 4: Bob receives vc-auth-required, sends vc-request-with-auth");
    bob.recv_msg_trash(&sent).await;
    let bob_chat = bob.get_chat(&alice).await;
    assert_eq!(bob_chat.why_cant_send(&bob).await.unwrap(), None);
    assert_eq!(bob_chat.can_send(&bob).await.unwrap(), true);

    // Check Bob emitted the JoinerProgress event.
    let event = bob
        .evtracker
        .get_matching(|evt| matches!(evt, EventType::SecurejoinJoinerProgress { .. }))
        .await;
    match event {
        EventType::SecurejoinJoinerProgress {
            contact_id,
            progress,
        } => {
            assert_eq!(contact_id, contact_alice_id);
            assert_eq!(progress, 400);
        }
        _ => unreachable!(),
    }

    // Check Bob sent the right message.
    let sent = bob.pop_sent_msg().await;
    assert!(sent.payload.contains("Auto-Submitted: auto-replied"));
    assert!(!sent.payload.contains("Bob Examplenet"));
    let mut msg = alice.parse_msg(&sent).await;
    assert!(msg.was_encrypted());
    assert_eq!(
        msg.get_header(HeaderDef::SecureJoin).unwrap(),
        "vc-request-with-auth"
    );
    assert!(msg.get_header(HeaderDef::SecureJoinAuth).is_some());
    let bob_fp = self_fingerprint(&bob).await.unwrap();
    assert_eq!(
        msg.get_header(HeaderDef::SecureJoinFingerprint).unwrap(),
        bob_fp
    );

    if case == SetupContactCase::WrongAliceGossip {
        let wrong_pubkey = GossipedKey {
            public_key: load_self_public_key(&bob).await.unwrap(),
            verified: false,
        };
        let alice_pubkey = msg
            .gossiped_keys
            .insert(alice_addr.to_string(), wrong_pubkey)
            .unwrap();
        let contact_bob = alice.add_or_lookup_contact_no_key(&bob).await;
        let handshake_msg = handle_securejoin_handshake(&alice, &mut msg, contact_bob.id)
            .await
            .unwrap();
        assert_eq!(handshake_msg, HandshakeMessage::Ignore);
        assert_eq!(contact_bob.is_verified(&alice).await.unwrap(), false);

        msg.gossiped_keys
            .insert(alice_addr.to_string(), alice_pubkey)
            .unwrap();
        let handshake_msg = handle_securejoin_handshake(&alice, &mut msg, contact_bob.id)
            .await
            .unwrap();
        assert_eq!(handshake_msg, HandshakeMessage::Ignore);
        assert!(contact_bob.is_verified(&alice).await.unwrap());
        return;
    }

    // Alice should not yet have Bob verified
    let contact_bob = alice.add_or_lookup_contact_no_key(&bob).await;
    let contact_bob_id = contact_bob.id;
    assert_eq!(contact_bob.is_key_contact(), true);
    assert_eq!(contact_bob.is_verified(&alice).await.unwrap(), false);
    assert_eq!(contact_bob.get_authname(), "");

    tcm.section("Step 5+6: Alice receives vc-request-with-auth, sends vc-contact-confirm");
    alice.recv_msg_trash(&sent).await;
    assert_eq!(contact_bob.is_verified(&alice).await.unwrap(), true);
    let contact_bob = Contact::get_by_id(&alice, contact_bob_id).await.unwrap();
    assert_eq!(contact_bob.get_authname(), "Bob Examplenet");
    assert!(contact_bob.get_name().is_empty());
    assert_eq!(contact_bob.is_bot(), false);

    // exactly one one-to-one chat should be visible for both now
    // (check this before calling alice.get_chat() explicitly below)
    assert_eq!(
        Chatlist::try_load(&alice, 0, None, None)
            .await
            .unwrap()
            .len(),
        1
    );
    assert_eq!(
        Chatlist::try_load(&bob, 0, None, None).await.unwrap().len(),
        1
    );

    // Check Alice got the verified message in her 1:1 chat.
    {
        let chat = alice.get_chat(&bob).await;
        let msg = get_chat_msg(&alice, chat.get_id(), 0, 1).await;
        assert!(msg.is_info());
        let expected_text = messages_e2e_encrypted(&alice).await;
        assert_eq!(msg.get_text(), expected_text);
    }

    // Make sure Alice hasn't yet sent their name to Bob.
    let contact_alice = Contact::get_by_id(&bob.ctx, contact_alice_id)
        .await
        .unwrap();
    match case {
        SetupContactCase::AliceHasName => assert_eq!(contact_alice.get_authname(), "Alice"),
        _ => assert_eq!(contact_alice.get_authname(), ""),
    };

    // Check Alice sent the right message to Bob.
    let sent = alice.pop_sent_msg().await;
    assert!(sent.payload.contains(alice_auto_submitted_hdr));
    assert!(!sent.payload.contains("Alice Exampleorg"));
    let msg = bob.parse_msg(&sent).await;
    assert!(msg.was_encrypted());
    assert_eq!(
        msg.get_header(HeaderDef::SecureJoin).unwrap(),
        "vc-contact-confirm"
    );

    // Bob has verified Alice already.
    //
    // Alice may not have verified Bob yet.
    assert_eq!(contact_alice.is_verified(&bob.ctx).await.unwrap(), true);

    // Step 7: Bob receives vc-contact-confirm
    bob.recv_msg_trash(&sent).await;
    assert_eq!(contact_alice.is_verified(&bob.ctx).await.unwrap(), true);
    let contact_alice = Contact::get_by_id(&bob.ctx, contact_alice_id)
        .await
        .unwrap();
    assert_eq!(contact_alice.get_authname(), "Alice Exampleorg");
    assert!(contact_alice.get_name().is_empty());
    assert_eq!(contact_alice.is_bot(), case == SetupContactCase::AliceIsBot);

    // The `SecurejoinWait` info message has been removed, but the e2ee notice remains.
    let msg = get_chat_msg(&bob, bob_chat.get_id(), 0, 1).await;
    assert!(msg.is_info());
    assert_eq!(msg.get_text(), messages_e2e_encrypted(&bob).await);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_setup_contact_bad_qr() {
    let bob = TestContext::new_bob().await;
    let ret = join_securejoin(&bob.ctx, "not a qr code").await;
    assert!(ret.is_err());
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_setup_contact_bob_knows_alice() -> Result<()> {
    let mut tcm = TestContextManager::new();
    let alice = &tcm.alice().await;
    let bob = &tcm.bob().await;

    // Ensure Bob knows Alice_FP
    let alice_contact_id = bob.add_or_lookup_contact_id(alice).await;

    tcm.section("Step 1: Generate QR-code");
    // `None` indicates setup-contact.
    let qr = get_securejoin_qr(alice, None).await?;

    tcm.section("Step 2+4: Bob scans QR-code, sends vc-request-with-auth, skipping vc-request");
    join_securejoin(bob, &qr).await.unwrap();

    // Check Bob emitted the JoinerProgress event.
    let event = bob
        .evtracker
        .get_matching(|evt| matches!(evt, EventType::SecurejoinJoinerProgress { .. }))
        .await;
    match event {
        EventType::SecurejoinJoinerProgress {
            contact_id,
            progress,
        } => {
            assert_eq!(contact_id, alice_contact_id);
            assert_eq!(progress, 400);
        }
        _ => unreachable!(),
    }

    // Check Bob sent the right handshake message.
    let sent = bob.pop_sent_msg().await;
    let msg = alice.parse_msg(&sent).await;
    assert!(msg.was_encrypted());
    assert_eq!(
        msg.get_header(HeaderDef::SecureJoin).unwrap(),
        "vc-request-with-auth"
    );
    assert!(msg.get_header(HeaderDef::SecureJoinAuth).is_some());
    let bob_fp = load_self_public_key(bob).await?.dc_fingerprint();
    assert_eq!(
        *msg.get_header(HeaderDef::SecureJoinFingerprint).unwrap(),
        bob_fp.hex()
    );

    // Alice should not yet have Bob verified
    let contact_bob = alice.add_or_lookup_contact_no_key(bob).await;
    assert_eq!(contact_bob.is_verified(alice).await?, false);

    tcm.section("Step 5+6: Alice receives vc-request-with-auth, sends vc-contact-confirm");
    alice.recv_msg_trash(&sent).await;
    assert_eq!(contact_bob.is_verified(alice).await?, true);

    // Check Alice signalled success via the SecurejoinInviterProgress event.
    let event = alice
        .evtracker
        .get_matching(|evt| {
            matches!(
                evt,
                EventType::SecurejoinInviterProgress { progress: 1000, .. }
            )
        })
        .await;
    match event {
        EventType::SecurejoinInviterProgress {
            contact_id,
            chat_type,
            progress,
            ..
        } => {
            assert_eq!(contact_id, contact_bob.id);
            assert_eq!(chat_type, Chattype::Single);
            assert_eq!(progress, 1000);
        }
        _ => unreachable!(),
    }

    let sent = alice.pop_sent_msg().await;
    let msg = bob.parse_msg(&sent).await;
    assert!(msg.was_encrypted());
    assert_eq!(
        msg.get_header(HeaderDef::SecureJoin).unwrap(),
        "vc-contact-confirm"
    );

    // Bob has verified Alice already.
    let contact_alice = bob.add_or_lookup_contact_no_key(alice).await;
    assert_eq!(contact_alice.is_verified(bob).await?, true);

    // Alice confirms that Bob is now verified.
    //
    // This does not change anything for Bob.
    tcm.section("Step 7: Bob receives vc-contact-confirm");
    bob.recv_msg_trash(&sent).await;
    assert_eq!(contact_alice.is_verified(bob).await?, true);

    Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_setup_contact_concurrent_calls() -> Result<()> {
    let mut tcm = TestContextManager::new();
    let alice = tcm.alice().await;
    let bob = tcm.bob().await;

    // do a scan that is not working as claire is never responding
    let qr_stale = "OPENPGP4FPR:1234567890123456789012345678901234567890#a=claire%40foo.de&n=&i=12345678901&s=23456789012";
    let claire_id = join_securejoin(&bob, qr_stale).await?;
    let chat = Chat::load_from_db(&bob, claire_id).await?;
    assert!(!claire_id.is_special());
    assert_eq!(chat.typ, Chattype::Single);
    assert!(bob.pop_sent_msg().await.payload().contains("claire@foo.de"));

    // subsequent scans shall abort existing ones or run concurrently -
    // but they must not fail as otherwise the whole qr scanning becomes unusable until restart.
    let qr = get_securejoin_qr(&alice, None).await?;
    let alice_id = join_securejoin(&bob, &qr).await?;
    let chat = Chat::load_from_db(&bob, alice_id).await?;
    assert!(!alice_id.is_special());
    assert_eq!(chat.typ, Chattype::Single);
    assert_ne!(claire_id, alice_id);
    assert!(
        bob.pop_sent_msg()
            .await
            .payload()
            .contains("alice@example.org")
    );

    Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_secure_join() -> Result<()> {
    let mut tcm = TestContextManager::new();
    let alice = tcm.alice().await;
    let bob = tcm.bob().await;

    // We start with empty chatlists.
    assert_eq!(Chatlist::try_load(&alice, 0, None, None).await?.len(), 0);
    assert_eq!(Chatlist::try_load(&bob, 0, None, None).await?.len(), 0);

    let alice_chatid = chat::create_group(&alice, "the chat").await?;

    tcm.section("Step 1: Generate QR-code, secure-join implied by chatid");
    let qr = get_securejoin_qr(&alice, Some(alice_chatid)).await.unwrap();

    tcm.section("Step 2: Bob scans QR-code, sends vg-request");
    let bob_chatid = join_securejoin(&bob, &qr).await?;
    assert_eq!(Chatlist::try_load(&bob, 0, None, None).await?.len(), 1);

    let sent = bob.pop_sent_msg().await;
    assert_eq!(
        sent.recipient(),
        EmailAddress::new("alice@example.org").unwrap()
    );
    let msg = alice.parse_msg(&sent).await;
    assert!(!msg.was_encrypted());
    assert_eq!(msg.get_header(HeaderDef::SecureJoin).unwrap(), "vg-request");
    assert!(msg.get_header(HeaderDef::SecureJoinInvitenumber).is_some());
    assert!(!msg.header_exists(HeaderDef::AutoSubmitted));

    // Old Delta Chat core sent `Secure-Join-Group` header in `vg-request`,
    // but it was only used by Alice in `vg-request-with-auth`.
    // New Delta Chat versions do not use `Secure-Join-Group` header at all
    // and it is deprecated.
    // Now `Secure-Join-Group` header
    // is only sent in `vg-request-with-auth` for compatibility.
    assert!(!msg.header_exists(HeaderDef::SecureJoinGroup));

    tcm.section("Step 3: Alice receives vg-request, sends vg-auth-required");
    alice.recv_msg_trash(&sent).await;

    let sent = alice.pop_sent_msg().await;
    assert!(sent.payload.contains("Auto-Submitted: auto-replied"));
    let msg = bob.parse_msg(&sent).await;
    assert!(msg.was_encrypted());
    assert_eq!(
        msg.get_header(HeaderDef::SecureJoin).unwrap(),
        "vg-auth-required"
    );

    tcm.section("Step 4: Bob receives vg-auth-required, sends vg-request-with-auth");
    bob.recv_msg_trash(&sent).await;
    let sent = bob.pop_sent_msg().await;

    // At this point Alice is still not part of the chat.
    // The final step of Alice adding Bob to the chat
    // may not work out and we don't want Bob
    // to implicitly add Alice if he manages to join the group
    // much later via another member.
    assert_eq!(chat::get_chat_contacts(&bob, bob_chatid).await?.len(), 0);

    let contact_alice_id = bob.add_or_lookup_contact_no_key(&alice).await.id;

    // Check Bob emitted the JoinerProgress event.
    let event = bob
        .evtracker
        .get_matching(|evt| matches!(evt, EventType::SecurejoinJoinerProgress { .. }))
        .await;
    match event {
        EventType::SecurejoinJoinerProgress {
            contact_id,
            progress,
        } => {
            assert_eq!(contact_id, contact_alice_id);
            assert_eq!(progress, 400);
        }
        _ => unreachable!(),
    }

    // Check Bob sent the right handshake message.
    assert!(sent.payload.contains("Auto-Submitted: auto-replied"));
    let msg = alice.parse_msg(&sent).await;
    assert!(msg.was_encrypted());
    assert_eq!(
        msg.get_header(HeaderDef::SecureJoin).unwrap(),
        "vg-request-with-auth"
    );
    assert!(msg.get_header(HeaderDef::SecureJoinAuth).is_some());
    let bob_fp = self_fingerprint(&bob).await?;
    assert_eq!(
        msg.get_header(HeaderDef::SecureJoinFingerprint).unwrap(),
        bob_fp
    );

    // Alice should not yet have Bob verified
    let contact_bob = alice.add_or_lookup_contact_no_key(&bob).await;
    assert_eq!(contact_bob.is_verified(&alice).await?, false);

    tcm.section("Step 5+6: Alice receives vg-request-with-auth, sends vg-member-added");
    alice.recv_msg_trash(&sent).await;
    assert_eq!(contact_bob.is_verified(&alice).await?, true);

    // Check Alice signalled success via the SecurejoinInviterProgress event.
    let event = alice
        .evtracker
        .get_matching(|evt| {
            matches!(
                evt,
                EventType::SecurejoinInviterProgress { progress: 1000, .. }
            )
        })
        .await;
    match event {
        EventType::SecurejoinInviterProgress {
            contact_id,
            chat_type,
            chat_id,
            progress,
        } => {
            assert_eq!(contact_id, contact_bob.id);
            assert_eq!(chat_type, Chattype::Group);
            assert_eq!(chat_id, alice_chatid);
            assert_eq!(progress, 1000);
        }
        _ => unreachable!(),
    }

    let sent = alice.pop_sent_msg().await;
    let msg = bob.parse_msg(&sent).await;
    assert!(msg.was_encrypted());
    assert_eq!(
        msg.get_header(HeaderDef::SecureJoin).unwrap(),
        "vg-member-added"
    );
    // Formally this message is auto-submitted, but as the member addition is a result of an
    // explicit user action, the Auto-Submitted header shouldn't be present. Otherwise it would
    // be strange to have it in "member-added" messages of verified groups only.
    assert!(!msg.header_exists(HeaderDef::AutoSubmitted));
    // This is a two-member group, but Alice must Autocrypt-gossip to her other devices.
    assert!(msg.get_header(HeaderDef::AutocryptGossip).is_some());

    {
        // Now Alice's chat with Bob should still be hidden, the verified message should
        // appear in the group chat.

        let chat = alice.get_chat(&bob).await;
        assert_eq!(
            chat.blocked,
            Blocked::Yes,
            "Alice's 1:1 chat with Bob is not hidden"
        );
        // There should be 2 messages in the chat:
        // - The ChatProtectionEnabled message
        // - You added member bob@example.net
        let msg = get_chat_msg(&alice, alice_chatid, 0, 2).await;
        assert!(msg.is_info());
        let expected_text = messages_e2e_encrypted(&alice).await;
        assert_eq!(msg.get_text(), expected_text);
    }

    // Bob has verified Alice already.
    //
    // Alice may not have verified Bob yet.
    let contact_alice = bob.add_or_lookup_contact_no_key(&alice).await;
    assert_eq!(contact_alice.is_verified(&bob).await?, true);

    tcm.section("Step 7: Bob receives vg-member-added");
    bob.recv_msg(&sent).await;
    {
        // Bob has Alice verified, message shows up in the group chat.
        assert_eq!(contact_alice.is_verified(&bob).await?, true);
        let chat = bob.get_chat(&alice).await;
        assert_eq!(
            chat.blocked,
            Blocked::Yes,
            "Bob's 1:1 chat with Alice is not hidden"
        );
        for item in chat::get_chat_msgs(&bob.ctx, bob_chatid).await.unwrap() {
            if let chat::ChatItem::Message { msg_id } = item {
                let msg = Message::load_from_db(&bob.ctx, msg_id).await.unwrap();
                let text = msg.get_text();
                println!("msg {msg_id} text: {text}");
            }
        }
    }

    let bob_chat = Chat::load_from_db(&bob.ctx, bob_chatid).await?;
    assert!(bob_chat.typ == Chattype::Group);

    // At the end of the protocol
    // Bob should have two members in the chat.
    assert_eq!(chat::get_chat_contacts(&bob, bob_chatid).await?.len(), 2);

    // On this "happy path", Alice and Bob get only a group-chat where all information are added to.
    // The one-to-one chats are used internally for the hidden handshake messages,
    // however, should not be visible in the UIs.
    assert_eq!(Chatlist::try_load(&alice, 0, None, None).await?.len(), 1);
    assert_eq!(Chatlist::try_load(&bob, 0, None, None).await?.len(), 1);

    // If Bob then sends a direct message to alice, however, the one-to-one with Alice should appear.
    let bobs_chat_with_alice = bob.create_chat(&alice).await;
    let sent = bob.send_text(bobs_chat_with_alice.id, "Hello").await;
    alice.recv_msg(&sent).await;
    assert_eq!(Chatlist::try_load(&alice, 0, None, None).await?.len(), 2);
    assert_eq!(Chatlist::try_load(&bob, 0, None, None).await?.len(), 2);

    Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_adhoc_group_no_qr() -> Result<()> {
    let alice = TestContext::new_alice().await;

    let mime = br#"Subject: First thread
Message-ID: first@example.org
To: Alice <alice@example.org>, Bob <bob@example.net>
From: Claire <claire@example.org>
Content-Type: text/plain; charset=utf-8; format=flowed; delsp=no

First thread."#;

    receive_imf(&alice, mime, false).await?;
    let msg = alice.get_last_msg().await;
    let chat_id = msg.chat_id;

    assert!(get_securejoin_qr(&alice, Some(chat_id)).await.is_err());
    Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_unknown_sender() -> Result<()> {
    let mut tcm = TestContextManager::new();
    let alice = tcm.alice().await;
    let bob = tcm.bob().await;

    tcm.execute_securejoin(&alice, &bob).await;

    let alice_chat_id = alice
        .create_group_with_members("Group with Bob", &[&bob])
        .await;

    let sent = alice.send_text(alice_chat_id, "Hi!").await;
    let bob_chat_id = bob.recv_msg(&sent).await.chat_id;

    let sent = bob.send_text(bob_chat_id, "Hi hi!").await;

    let alice_bob_contact_id = alice.add_or_lookup_contact_id(&bob).await;
    remove_contact_from_chat(&alice, alice_chat_id, alice_bob_contact_id).await?;
    alice.pop_sent_msg().await;

    // The message from Bob is delivered late, Bob is already removed.
    let msg = alice.recv_msg(&sent).await;
    assert_eq!(msg.text, "Hi hi!");
    assert_eq!(msg.get_override_sender_name().unwrap(), "bob@example.net");

    Ok(())
}

/// Tests that Bob gets Alice as verified
/// if `vc-contact-confirm` is lost.
/// Previously `vc-contact-confirm` was used
/// to confirm backward verification,
/// but backward verification is not tracked anymore.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_lost_contact_confirm() {
    let mut tcm = TestContextManager::new();
    let alice = tcm.alice().await;
    let bob = tcm.bob().await;

    let qr = get_securejoin_qr(&alice, None).await.unwrap();
    join_securejoin(&bob.ctx, &qr).await.unwrap();

    // vc-request
    let sent = bob.pop_sent_msg().await;
    alice.recv_msg_trash(&sent).await;

    // vc-auth-required
    let sent = alice.pop_sent_msg().await;
    bob.recv_msg_trash(&sent).await;

    // vc-request-with-auth
    let sent = bob.pop_sent_msg().await;
    alice.recv_msg_trash(&sent).await;

    // Alice has Bob verified now.
    let contact_bob = alice.add_or_lookup_contact_no_key(&bob).await;
    assert_eq!(contact_bob.is_verified(&alice).await.unwrap(), true);

    // Alice sends vc-contact-confirm, but it gets lost.
    let _sent_vc_contact_confirm = alice.pop_sent_msg().await;

    // Bob has alice as verified too, even though vc-contact-confirm is lost.
    let contact_alice = bob.add_or_lookup_contact_no_key(&alice).await;
    assert_eq!(contact_alice.is_verified(&bob).await.unwrap(), true);
}

/// Tests Bob joining two groups by scanning two QR codes
/// from the same Alice at the same time.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_parallel_securejoin() -> Result<()> {
    let mut tcm = TestContextManager::new();
    let alice = &tcm.alice().await;
    let bob = &tcm.bob().await;

    let alice_chat1_id = chat::create_group(alice, "First chat").await?;
    let alice_chat2_id = chat::create_group(alice, "Second chat").await?;

    let qr1 = get_securejoin_qr(alice, Some(alice_chat1_id)).await?;
    let qr2 = get_securejoin_qr(alice, Some(alice_chat2_id)).await?;

    // Bob scans both QR codes.
    let bob_chat1_id = join_securejoin(bob, &qr1).await?;
    let sent_vg_request1 = bob.pop_sent_msg().await;

    let bob_chat2_id = join_securejoin(bob, &qr2).await?;
    let sent_vg_request2 = bob.pop_sent_msg().await;

    // Alice receives two `vg-request` messages
    // and sends back two `vg-auth-required` messages.
    alice.recv_msg_trash(&sent_vg_request1).await;
    let sent_vg_auth_required1 = alice.pop_sent_msg().await;

    alice.recv_msg_trash(&sent_vg_request2).await;
    let _sent_vg_auth_required2 = alice.pop_sent_msg().await;

    // Bob receives first `vg-auth-required` message.
    // Bob has two securejoin processes started,
    // so he should send two `vg-request-with-auth` messages.
    bob.recv_msg_trash(&sent_vg_auth_required1).await;

    // Bob sends `vg-request-with-auth` messages.
    let sent_vg_request_with_auth2 = bob.pop_sent_msg().await;
    let sent_vg_request_with_auth1 = bob.pop_sent_msg().await;

    // Alice receives both `vg-request-with-auth` messages.
    alice.recv_msg_trash(&sent_vg_request_with_auth1).await;
    let sent_vg_member_added1 = alice.pop_sent_msg().await;
    let joined_chat_id1 = bob.recv_msg(&sent_vg_member_added1).await.chat_id;
    assert_eq!(joined_chat_id1, bob_chat1_id);

    alice.recv_msg_trash(&sent_vg_request_with_auth2).await;
    let sent_vg_member_added2 = alice.pop_sent_msg().await;
    let joined_chat_id2 = bob.recv_msg(&sent_vg_member_added2).await.chat_id;
    assert_eq!(joined_chat_id2, bob_chat2_id);

    Ok(())
}

/// Tests Bob scanning setup contact QR codes of Alice and Fiona
/// concurrently.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_parallel_setup_contact() -> Result<()> {
    let mut tcm = TestContextManager::new();
    let alice = &tcm.alice().await;
    let bob = &tcm.bob().await;
    let fiona = &tcm.fiona().await;

    // Bob scans Alice's QR code,
    // but Alice is offline and takes a while to respond.
    let alice_qr = get_securejoin_qr(alice, None).await?;
    join_securejoin(bob, &alice_qr).await?;
    let sent_alice_vc_request = bob.pop_sent_msg().await;

    // Bob scans Fiona's QR code while SecureJoin
    // process with Alice is not finished.
    let fiona_qr = get_securejoin_qr(fiona, None).await?;
    join_securejoin(bob, &fiona_qr).await?;
    let sent_fiona_vc_request = bob.pop_sent_msg().await;

    fiona.recv_msg_trash(&sent_fiona_vc_request).await;
    let sent_fiona_vc_auth_required = fiona.pop_sent_msg().await;

    bob.recv_msg_trash(&sent_fiona_vc_auth_required).await;
    let sent_fiona_vc_request_with_auth = bob.pop_sent_msg().await;

    fiona.recv_msg_trash(&sent_fiona_vc_request_with_auth).await;
    let sent_fiona_vc_contact_confirm = fiona.pop_sent_msg().await;

    bob.recv_msg_trash(&sent_fiona_vc_contact_confirm).await;
    let bob_fiona_contact_id = bob.add_or_lookup_contact_id(fiona).await;
    let bob_fiona_contact = Contact::get_by_id(bob, bob_fiona_contact_id).await.unwrap();
    assert_eq!(bob_fiona_contact.is_verified(bob).await.unwrap(), true);

    // Alice gets online and previously started SecureJoin process finishes.
    alice.recv_msg_trash(&sent_alice_vc_request).await;
    let sent_alice_vc_auth_required = alice.pop_sent_msg().await;

    bob.recv_msg_trash(&sent_alice_vc_auth_required).await;
    let sent_alice_vc_request_with_auth = bob.pop_sent_msg().await;

    alice.recv_msg_trash(&sent_alice_vc_request_with_auth).await;
    let sent_alice_vc_contact_confirm = alice.pop_sent_msg().await;

    bob.recv_msg_trash(&sent_alice_vc_contact_confirm).await;
    let bob_alice_contact_id = bob.add_or_lookup_contact_id(alice).await;
    let bob_alice_contact = Contact::get_by_id(bob, bob_alice_contact_id).await.unwrap();
    assert_eq!(bob_alice_contact.is_verified(bob).await.unwrap(), true);

    Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_wrong_auth_token() -> Result<()> {
    let mut tcm = TestContextManager::new();
    let alice = &tcm.alice().await;
    let bob = &tcm.bob().await;

    // Bob should already have Alice's key
    // so that he can directly send vc-request-with-auth
    tcm.send_recv(alice, bob, "hi").await;

    let alice_qr = get_securejoin_qr(alice, None).await?;
    println!("{}", &alice_qr);
    let invalid_alice_qr = alice_qr.replace("&s=", "&s=INVALIDAUTHTOKEN&someotherkey=");

    join_securejoin(bob, &invalid_alice_qr).await?;
    let sent = bob.pop_sent_msg().await;

    let msg = alice.parse_msg(&sent).await;
    assert_eq!(
        msg.get_header(HeaderDef::SecureJoin).unwrap(),
        "vc-request-with-auth"
    );

    alice.recv_msg_trash(&sent).await;

    let alice_bob_contact = alice.add_or_lookup_contact(bob).await;
    assert!(!alice_bob_contact.is_verified(alice).await?);

    Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_send_avatar_in_securejoin() -> Result<()> {
    async fn exec_securejoin_group(
        tcm: &TestContextManager,
        scanner: &TestContext,
        scanned: &TestContext,
    ) {
        let chat_id = chat::create_group(scanned, "group").await.unwrap();
        let qr = get_securejoin_qr(scanned, Some(chat_id)).await.unwrap();
        tcm.exec_securejoin_qr(scanner, scanned, &qr).await;
    }
    async fn exec_securejoin_broadcast(
        tcm: &TestContextManager,
        scanner: &TestContext,
        scanned: &TestContext,
    ) {
        let chat_id = chat::create_broadcast(scanned, "group".to_string())
            .await
            .unwrap();
        let qr = get_securejoin_qr(scanned, Some(chat_id)).await.unwrap();
        tcm.exec_securejoin_qr(scanner, scanned, &qr).await;
    }

    for round in 0..6 {
        let mut tcm = TestContextManager::new();
        let alice = &tcm.alice().await;
        let bob = &tcm.bob().await;

        let file = alice.dir.path().join("avatar.png");
        tokio::fs::write(&file, AVATAR_64x64_BYTES).await?;
        alice
            .set_config(Config::Selfavatar, Some(file.to_str().unwrap()))
            .await?;

        match round {
            0 => {
                tcm.execute_securejoin(alice, bob).await;
            }
            1 => {
                tcm.execute_securejoin(bob, alice).await;
            }
            2 => {
                exec_securejoin_group(&tcm, alice, bob).await;
            }
            3 => {
                exec_securejoin_group(&tcm, bob, alice).await;
            }
            4 => {
                exec_securejoin_broadcast(&tcm, alice, bob).await;
            }
            5 => {
                exec_securejoin_broadcast(&tcm, bob, alice).await;
            }
            _ => panic!(),
        }

        let alice_on_bob = bob.add_or_lookup_contact_no_key(alice).await;
        let avatar = alice_on_bob.get_profile_image(bob).await?.unwrap();
        assert_eq!(
            avatar.file_name().unwrap().to_str().unwrap(),
            AVATAR_64x64_DEDUPLICATED
        );
    }

    Ok(())
}

/// Tests that scanning a QR code week later
/// allows Bob to establish a contact with Alice,
/// but does not mark Bob as verified for Alice.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_expired_contact_auth_token() -> Result<()> {
    let mut tcm = TestContextManager::new();
    let alice = &tcm.alice().await;
    let bob = &tcm.bob().await;

    // Alice creates a QR code.
    let qr = get_securejoin_qr(alice, None).await?;

    // One week passes, QR code expires.
    SystemTime::shift(Duration::from_secs(7 * 24 * 3600));

    // Bob scans the QR code.
    join_securejoin(bob, &qr).await?;

    // vc-request
    alice.recv_msg_trash(&bob.pop_sent_msg().await).await;

    // vc-auth-requried
    bob.recv_msg_trash(&alice.pop_sent_msg().await).await;

    // vc-request-with-auth
    alice.recv_msg_trash(&bob.pop_sent_msg().await).await;

    // Bob should not be verified for Alice.
    let contact_bob = alice.add_or_lookup_contact_no_key(bob).await;
    assert_eq!(contact_bob.is_verified(alice).await.unwrap(), false);

    Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_expired_group_auth_token() -> Result<()> {
    let mut tcm = TestContextManager::new();
    let alice = &tcm.alice().await;
    let bob = &tcm.bob().await;

    let alice_chat_id = chat::create_group(alice, "Group").await?;

    // Alice creates a group QR code.
    let qr = get_securejoin_qr(alice, Some(alice_chat_id)).await.unwrap();

    // One week passes, QR code expires.
    SystemTime::shift(Duration::from_secs(7 * 24 * 3600));

    // Bob scans the QR code.
    join_securejoin(bob, &qr).await?;

    // vg-request
    alice.recv_msg_trash(&bob.pop_sent_msg().await).await;

    // vg-auth-requried
    bob.recv_msg_trash(&alice.pop_sent_msg().await).await;

    // vg-request-with-auth
    alice.recv_msg_trash(&bob.pop_sent_msg().await).await;

    // vg-member-added
    let bob_member_added_msg = bob.recv_msg(&alice.pop_sent_msg().await).await;
    assert!(bob_member_added_msg.is_info());
    assert_eq!(
        bob_member_added_msg.get_info_type(),
        SystemMessage::MemberAddedToGroup
    );

    // Bob should not be verified for Alice.
    let contact_bob = alice.add_or_lookup_contact_no_key(bob).await;
    assert_eq!(contact_bob.is_verified(alice).await.unwrap(), false);

    Ok(())
}

/// Tests that old token is considered expired
/// even if sync message just arrived.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_expired_synced_auth_token() -> Result<()> {
    let mut tcm = TestContextManager::new();
    let alice = &tcm.alice().await;
    let alice2 = &tcm.alice().await;
    let bob = &tcm.bob().await;

    alice.set_config_bool(Config::SyncMsgs, true).await?;
    alice2.set_config_bool(Config::SyncMsgs, true).await?;

    // Alice creates a QR code on the second device.
    let qr = get_securejoin_qr(alice2, None).await?;

    alice2.send_sync_msg().await.unwrap();
    let sync_msg = alice2.pop_sent_msg().await;

    // One week passes, QR code expires.
    SystemTime::shift(Duration::from_secs(7 * 24 * 3600));

    alice.recv_msg_trash(&sync_msg).await;

    // Bob scans the QR code.
    join_securejoin(bob, &qr).await?;

    // vc-request
    alice.recv_msg_trash(&bob.pop_sent_msg().await).await;

    // vc-auth-requried
    bob.recv_msg_trash(&alice.pop_sent_msg().await).await;

    // vc-request-with-auth
    alice.recv_msg_trash(&bob.pop_sent_msg().await).await;

    // Bob should not be verified for Alice.
    let contact_bob = alice.add_or_lookup_contact_no_key(bob).await;
    assert_eq!(contact_bob.is_verified(alice).await.unwrap(), false);

    Ok(())
}

/// Tests that attempting to join already joined group does nothing.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_rejoin_group() -> Result<()> {
    let mut tcm = TestContextManager::new();
    let alice = &tcm.alice().await;
    let bob = &tcm.bob().await;

    let alice_chat_id = chat::create_group(alice, "the chat").await?;

    let qr = get_securejoin_qr(alice, Some(alice_chat_id)).await?;
    tcm.exec_securejoin_qr(bob, alice, &qr).await;

    // Bob gets two progress events.
    for expected_progress in [400, 1000] {
        let EventType::SecurejoinJoinerProgress { progress, .. } = bob
            .evtracker
            .get_matching(|evt| matches!(evt, EventType::SecurejoinJoinerProgress { .. }))
            .await
        else {
            unreachable!();
        };
        assert_eq!(progress, expected_progress);
    }

    // Bob scans the QR code again.
    join_securejoin(bob, &qr).await?;

    // Bob immediately receives progress 1000 event.
    let EventType::SecurejoinJoinerProgress { progress, .. } = bob
        .evtracker
        .get_matching(|evt| matches!(evt, EventType::SecurejoinJoinerProgress { .. }))
        .await
    else {
        unreachable!();
    };
    assert_eq!(progress, 1000);

    // Bob does not send any more messages by scanning the QR code.
    assert!(bob.pop_sent_msg_opt(Duration::ZERO).await.is_none());

    Ok(())
}

/// To make invite links a little bit nicer, we put the readable part at the end and encode spaces by `+` instead of `%20`.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_get_securejoin_qr_name_is_last() -> Result<()> {
    let mut tcm = TestContextManager::new();
    let alice = &tcm.alice().await;
    alice
        .set_config(Config::Displayname, Some("Alice Axe"))
        .await?;
    let qr = get_securejoin_qr(alice, None).await?;
    assert!(qr.ends_with("Alice+Axe"));

    let alice_chat_id = chat::create_group(alice, "The Chat").await?;
    let qr = get_securejoin_qr(alice, Some(alice_chat_id)).await?;
    assert!(qr.ends_with("The+Chat"));

    Ok(())
}

/// QR codes should not get arbitrary big because of long names.
/// The truncation, however, should not let the url end with a `.`, which is a call for trouble in linkfiers.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_get_securejoin_qr_name_is_truncated() -> Result<()> {
    let mut tcm = TestContextManager::new();
    let alice = &tcm.alice().await;
    alice
        .set_config(
            Config::Displayname,
            Some("Alice Axe Has A Very Long Family Name Really Indeed"),
        )
        .await?;
    let qr = get_securejoin_qr(alice, None).await?;
    assert!(qr.ends_with("Alice+Axe+Has+A+Very+Lon_"));
    assert!(!qr.ends_with("."));

    let alice_chat_id =
        chat::create_group(alice, "The Chat With One Of The Longest Titles Around").await?;
    let qr = get_securejoin_qr(alice, Some(alice_chat_id)).await?;
    assert!(qr.ends_with("The+Chat+With+One+Of+The_"));
    assert!(!qr.ends_with("."));

    Ok(())
}

/// Regression test for the bug
/// that resulted in an info message
/// about Bob addition to the group on Fiona's device.
///
/// There should be no info messages about implicit
/// member changes when we are added to the group.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_two_group_securejoins() -> Result<()> {
    let mut tcm = TestContextManager::new();
    let alice = &tcm.alice().await;
    let bob = &tcm.bob().await;
    let fiona = &tcm.fiona().await;

    let group_id = chat::create_group(alice, "Group").await?;

    let qr = get_securejoin_qr(alice, Some(group_id)).await?;

    // Bob joins using QR code.
    tcm.exec_securejoin_qr(bob, alice, &qr).await;

    // Fiona joins using QR code.
    tcm.exec_securejoin_qr(fiona, alice, &qr).await;

    let fiona_chat_id = fiona.get_last_msg().await.chat_id;
    fiona
        .golden_test_chat(fiona_chat_id, "two_group_securejoins")
        .await;

    Ok(())
}

/// Tests that scanning an outdated QR code does not add the removed inviter back to the group.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_qr_no_implicit_inviter_addition() -> Result<()> {
    let mut tcm = TestContextManager::new();
    let alice = &tcm.alice().await;
    let bob = &tcm.bob().await;
    let charlie = &tcm.charlie().await;

    // Alice creates a group with Bob.
    let alice_chat_id = alice
        .create_group_with_members("Group with Bob", &[bob])
        .await;
    let alice_qr = get_securejoin_qr(alice, Some(alice_chat_id)).await?;

    // Bob joins the group via QR code.
    let bob_chat_id = tcm.exec_securejoin_qr(bob, alice, &alice_qr).await;

    // Bob creates a QR code for joining the group.
    let bob_qr = get_securejoin_qr(bob, Some(bob_chat_id)).await?;

    // Alice removes Bob from the group.
    let alice_bob_contact_id = alice.add_or_lookup_contact_id(bob).await;
    remove_contact_from_chat(alice, alice_chat_id, alice_bob_contact_id).await?;

    // Deliver the removal message to Bob.
    let removal_msg = alice.pop_sent_msg().await;
    bob.recv_msg(&removal_msg).await;

    // Charlie scans Bob's outdated QR code.
    let charlie_chat_id = join_securejoin(charlie, &bob_qr).await?;

    // Charlie sends vg-request to Bob.
    let sent = charlie.pop_sent_msg().await;
    bob.recv_msg_trash(&sent).await;

    // Bob sends vg-auth-required to Charlie.
    let sent = bob.pop_sent_msg().await;
    charlie.recv_msg_trash(&sent).await;

    // Bob receives vg-request-with-auth, but cannot add Charlie
    // because Bob himself is not in the group.
    let sent = charlie.pop_sent_msg().await;
    bob.recv_msg_trash(&sent).await;

    // Charlie still has no contacts in the list.
    let charlie_chat_contacts = chat::get_chat_contacts(charlie, charlie_chat_id).await?;
    assert_eq!(charlie_chat_contacts.len(), 0);

    // Alice adds Charlie to the group
    let alice_charlie_contact_id = alice.add_or_lookup_contact_id(charlie).await;
    add_contact_to_chat(alice, alice_chat_id, alice_charlie_contact_id).await?;

    let sent = alice.pop_sent_msg().await;
    assert_eq!(charlie.recv_msg(&sent).await.chat_id, charlie_chat_id);

    // Charlie has two contacts in the list: Alice and self.
    let charlie_chat_contacts = chat::get_chat_contacts(charlie, charlie_chat_id).await?;
    assert_eq!(charlie_chat_contacts.len(), 2);

    Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_user_deletes_chat_before_securejoin_completes() -> Result<()> {
    let mut tcm = TestContextManager::new();
    let alice = &tcm.alice().await;
    let bob = &tcm.bob().await;

    let qr = get_securejoin_qr(alice, None).await?;
    let bob_chat_id = join_securejoin(bob, &qr).await?;

    let bob_alice_chat = bob.get_chat(alice).await;
    // It's not possible yet to send to the chat, because Bob doesn't have Alice's key:
    assert_eq!(bob_alice_chat.can_send(bob).await?, false);
    assert_eq!(bob_alice_chat.id, bob_chat_id);

    let request = bob.pop_sent_msg().await;

    bob_chat_id.delete(bob).await?;

    alice.recv_msg_trash(&request).await;
    let auth_required = alice.pop_sent_msg().await;

    bob.recv_msg_trash(&auth_required).await;

    // The chat with Alice should be recreated,
    // and it should be sendable now:
    assert!(bob.get_chat(alice).await.can_send(bob).await?);

    Ok(())
}

/// Tests that vc-request is processed even if the server
/// encrypts incoming unencrypted messages with OpenPGP.
///
/// For an example of software that encrypts incoming messages
/// with OpenPGP, see <https://lacre.io/>.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_vc_request_encrypted_at_rest() -> Result<()> {
    let mut tcm = TestContextManager::new();
    let alice = &tcm.alice().await;
    let bob = &tcm.bob().await;

    let qr = get_securejoin_qr(alice, None).await?;

    let Qr::AskVerifyContact { invitenumber, .. } = check_qr(bob, &qr).await? else {
        panic!("Failed to parse QR code");
    };

    // Encrypted payload is this message encrypted to Alice's key:
    /*
    Content-Type: multipart/mixed;
        boundary="1888168c99b6020b_ed9b3571859a92d7_eeabe50050fc923b"

    --1888168c99b6020b_ed9b3571859a92d7_eeabe50050fc923b
    Content-Type: text/plain; charset="utf-8"
    Message-ID: <4f108a03-6c03-4f41-a6fb-fcd05b94c21f@localhost>
    Content-Transfer-Encoding: 7bit

    Secure-Join: vc-request
    --1888168c99b6020b_ed9b3571859a92d7_eeabe50050fc923b--
    */

    let payload = format!("To: <alice@example.org>
Subject: [...]
Date: Tue, 6 Jan 2026 08:20:47 +0000
References: <4f108a03-6c03-4f41-a6fb-fcd05b94c21f@localhost>
Chat-Version: 1.0
Autocrypt: addr=bob@example.net; prefer-encrypt=mutual; keydata=xsBNBF4wx1cBCADOwLS/xCd8iKDWUsyTfVzWby+ZGKPpamPTvdj0GFgnf0B1EBaA5//PjA
	 zbK5iKio6QNEmZagzJPkXPByJcAIRUm0T16tqDtCvxm+H93YEXpHi/XWOeJw9kohATSqUtsRO0pFJe
	 DvPiMTmQrEmHYoWDSQBfCrowZdvnMAlbJ9JjYOngcMeTxc0jxmPs5s17yFC+1OWu4fwWCyUM3wy1Jz
	 dKTcDWryrSkvmgFdUqJ7pJDk1HFTt+x9tvQlK3un9BXiRwv0u0zDSuI8eDH/dRLA4UL9Pq6vmJmBam
	 e1BPsE1PA7VzeTSJR2ooJXMT6o2AmH8PPUfRkv3OiWuh7LM5FSpHABEBAAHNETxib2JAZXhhbXBsZS
	 5uZXQ+wsCOBBMBCAA4BQJpXW0wFiEEzMtaqfbhFByUMWXx2xixjLz3BIcCGwMCHgAECwkIBwYVCAkK
	 CwIDFgIBAScCGQEACgkQ2xixjLz3BIcexwgAsOauz2xZ6QhVBQNX3Hg8OoYTn60w6b4XUOpI4UtqAT
	 bAmPr5VPhCUa5vzI19QCSekLZIVxUSl6C+7YF/mU8yJldKPgq1Kpr8tbnISNbUDpvuGz7NQdK0TEwx
	 RkPDw+ilqY7v2Rhhg0Sogi23Yrbyqvs72oorDjia6dBA7VLixpW1O9h/MfQ6WiXJcHSavoLqk2hFW1
	 y/AsVmArGiuT+kzRJWhTzojZLjQLHTKQii/AOQ3MhkJycyayI/igSEDWWwCcuqL8SF+PUJWGyU5PxU
	 2hTHDBsVItLLsqseM3WrwJRkBBdsdsR8+moHVyXJV9Gfl/4UFQCgltdEwWISgNTbAc7ATQReMMdXAQ
	 gAogeBLbIjaeJII3W2pxsu+SEusQkJVykbGYDtqyXV+XBZisY4GE0kTawK5mqSh+rDqquCxDgYWBRT
	 nGZwEKohnj2NG75pjfyVPhYMUdJt7+Ya1oiFvZlgrrOj1btjevq53yFtQolMN+X2oS8mlf9jSzIyPC
	 eDxJk1N1gxaAATg3ByAyB1Td0wDdFPp48ni8qzfyGZeYicvJlx74YnOaja2lnI/y+I9LsmmqiOgI8H
	 cbmH1od5qSnVjhcpBoTEA15YLIEkSE3C00Q5USlDS3EVg/IOu3FXnLl7v0hQ/jXyv88eycfpSfFcbM
	 Hot9VtJ4TIPIoSX7DQ+uU2SXJKiZNkVQARAQABwsB2BBgBCAAgBQJpXW0wAhsMFiEEzMtaqfbhFByU
	 MWXx2xixjLz3BIcACgkQ2xixjLz3BIcxvwf9G33yLtvekpNGeRbWkjRXSa083k/4CZpkF0Fb5led1O
	 hjRjw9KQdZj68qn1vVrDxygnYdHwZWy8WbwEkK+l3gyJ2v2D/Wsj+sHjQ1YMtPnx+EsRFWjKsolYfN
	 gJZ+IfMTasip8uJNxN5LIvI/svn43mzgeLUudddekmr/2eekhXfYz5covje3yf+u4rYDWQ78TLpJL5
	 bxTwiiUyV1ZIO1G2F4RNjv8wZHsGD3DasQck+OCV7jQNuyojcmq6J22lXN0clisI51mHzwkLAnI+Yh
	 jRj3nqwVSCxsmgfejap53JGHEL0CEy6KvepLqjFf8BiK4kwRBmI7/YWOyY2xBWdTew==
Secure-Join: vc-request
Secure-Join-Invitenumber: {invitenumber}
MIME-Version: 1.0
From: <bob@example.net>
Message-ID: <4f108a03-6c03-4f41-a6fb-fcd05b94c21f@localhost>
Content-Type: multipart/encrypted;
	protocol=\"application/pgp-encrypted\";
	boundary=\"1767687657/562150/131174/example.org\"

This is a MIME-encapsulated message.

--1767687657/562150/131174/example.org
Content-Type: application/pgp-encrypted
Content-Disposition: attachment

Version: 1

--1767687657/562150/131174/example.org
Content-Type: application/octet-stream
Content-Disposition: inline; filename=\"msg.asc\"

-----BEGIN PGP MESSAGE-----

wV4D5tq63hTeebASAQdAGKP3GqsznPOYBXOo8FYf5x8r0ZaJZUGkXIR+Oi32H1Mw
LdVEu0jcXmZ1fGZ7LLPtvfwllB2W9Bhiy/lCZZs+c98VYzSWaT0DHmp23WvTsBZn
0sDeAS0n2I6xPwYhUjqL8G1a9xdyvZRgHYUXhiARrXzx7G+aX8/KFjk6e2OZxiVy
PSHKh61T9wPYzPMlToPsI0GjXAzOgI9I3XQ8xPqihiUpnU6BSg+Tj26m9ZqdJe8v
aVAHxAu7aqz7eljp2H336Idgfvf1lXdA6gMA8HhHI729Ws/Mc8lcgefM9kzZWEIN
FOBYmf4JCdTghESpEJ5i+pF28ChqZ3+8lGzNmT66c2SQRTclwxFa4SS19D4mxkHq
iOMqj7LZ8R6IhnNVRD1V4mVapc7vFVZy8ViyZlmhJF0TBR/TEXlit6vtSkDKFiR9
Tb5zuFQVAwhun3i1tG3/Ii1ReflZNnteVlL1J1XnptOJ70vVUeO3qRqkbkiH8Mdm
Asvy24WKkz9jmEgdt0Hj8VUHtcOL5xmtsr0ZUCLUriylpzDh8E+0LTl2/DOgH+mI
D/gFLCzFHjI0jfXyLTGCYOPYAnvqJukLBT/X7JbCAI/62aq0K28Lp6beJhFs+bIz
gU6dGXsFMe/RpRHrIAkMAaM5xkxMDRuRJDxiUdS/X+Y8
=QrdS
-----END PGP MESSAGE-----

--1767687657/562150/131174/example.org--
");

    // Alice receives vc-request, but it is encrypted
    // by the server.
    let received = receive_imf(alice, payload.as_bytes(), false)
        .await?
        .unwrap();
    assert_eq!(received.chat_id, DC_CHAT_ID_TRASH);

    // Test that Alice sends vc-auth-required after processing vc-request.
    let sent = alice.pop_sent_msg().await;
    let msg = bob.parse_msg(&sent).await;
    assert_eq!(
        msg.get_header(HeaderDef::SecureJoin).unwrap(),
        "vc-auth-required"
    );

    Ok(())
}