-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_e2e.rs
More file actions
1211 lines (1120 loc) · 41.8 KB
/
Copy pathnode_e2e.rs
File metadata and controls
1211 lines (1120 loc) · 41.8 KB
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
//! End-to-end tests for the `kult-node` runtime: the delivery engine,
//! transport scheduler, receipts, fragmentation, retry/backoff, and
//! out-of-order arrival — all over real or mock transports, with real
//! encrypted stores and process "restarts" (node drop + reopen).
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use std::sync::{Arc, Mutex};
use async_trait::async_trait;
use rand::rngs::StdRng;
use rand::SeedableRng;
use kult_crypto::{
AuthorityPairingBundle, ConnectCode, Identity, KdfProfile, OneTimePrekeySecret, PqPrekeySecret,
PrekeyBundle, SignedPrekeySecret,
};
use kult_node::{ContentStatus, Event, Node};
use kult_protocol::{
decode_content, encode_text, fragment, DecodedContent, Envelope, EnvelopeKind, CONTENT_MAGIC,
};
use kult_store::{DeliveryState, Store, MAX_PENDING_ENVELOPES};
use kult_transport::{
CostClass, DeliveryHint, LatencyClass, LinkProfile, Reachability, SendReceipt,
SneakernetTransport, Transport, TransportError,
};
const NOW: u64 = 1_800_000_000;
/// Fast Argon2id profile for tests only.
const TEST_KDF: KdfProfile = KdfProfile {
m_cost_kib: 8,
t_cost: 1,
p_cost: 1,
};
fn count_received(events: &[Event]) -> usize {
events
.iter()
.filter(|e| matches!(e, Event::MessageReceived { .. }))
.count()
}
fn delivered_ids(events: &[Event]) -> Vec<[u8; 16]> {
events
.iter()
.filter_map(|e| match e {
Event::DeliveryUpdated {
id,
state: DeliveryState::Delivered,
} => Some(*id),
_ => None,
})
.collect()
}
#[test]
fn pairing_bundle_carries_signed_first_message_routes() {
let mut rng = StdRng::seed_from_u64(700);
let dir = tempfile::tempdir().unwrap();
let mut node = Node::create(&dir.path().join("node.db"), b"pass", TEST_KDF, &mut rng).unwrap();
let hints = vec![
DeliveryHint::Multiaddr("/ip4/192.0.2.7/udp/4242/quic-v1/p2p/12D3KooWExample".to_owned()),
DeliveryHint::Relay("/ip4/198.51.100.4/tcp/443".to_owned()),
];
let encoded = node
.handshake_bundle_with_hints(&hints, NOW, &mut rng)
.unwrap();
let pairing = AuthorityPairingBundle::decode(&encoded).unwrap();
pairing.verify(NOW).unwrap();
let code = ConnectCode::parse(&node.connect_code().unwrap()).unwrap();
assert_eq!(pairing.discovery_capability, code.capability());
assert!(pairing.discovery_generation > 0);
let mut wrong_capability = pairing.clone();
wrong_capability.discovery_capability[0] ^= 0x80;
assert!(wrong_capability.verify(NOW).is_err());
let mut trailing = encoded.clone();
trailing.push(0);
assert!(AuthorityPairingBundle::decode(&trailing).is_err());
let bundle = &pairing.device_bundle;
assert!(
bundle.prekey.relay_hints.len() > hints.len(),
"the signed bundle also carries its bounded admission extension"
);
let decoded = bundle
.prekey
.transport_hints()
.iter()
.map(|bytes| postcard::from_bytes::<DeliveryHint>(bytes).unwrap())
.collect::<Vec<_>>();
assert_eq!(decoded, hints);
bundle.verify(NOW).unwrap();
let mut receiver =
Node::create(&dir.path().join("receiver.db"), b"pass", TEST_KDF, &mut rng).unwrap();
receiver
.add_contact("sender", &encoded, &[], NOW, &mut rng)
.unwrap();
let stored = receiver.contacts().unwrap().pop().unwrap();
let imported = stored
.hints
.iter()
.map(|bytes| postcard::from_bytes::<DeliveryHint>(bytes).unwrap())
.collect::<Vec<_>>();
assert_eq!(imported, hints);
}
#[tokio::test]
async fn rescanning_a_fresh_bundle_rekeys_and_retries_unconfirmed_messages() {
let mut rng = StdRng::seed_from_u64(701);
let dir = tempfile::tempdir().unwrap();
let sender_inbox = dir.path().join("sender-spool");
let stale_receiver_inbox = dir.path().join("stale-receiver-spool");
let fresh_receiver_inbox = dir.path().join("fresh-receiver-spool");
let mut sender =
Node::create(&dir.path().join("sender.db"), b"sender", TEST_KDF, &mut rng).unwrap();
let mut receiver = Node::create(
&dir.path().join("receiver.db"),
b"receiver",
TEST_KDF,
&mut rng,
)
.unwrap();
let _stale_receiver = SneakernetTransport::new(&stale_receiver_inbox).unwrap();
sender.add_transport(Arc::new(SneakernetTransport::new(&sender_inbox).unwrap()));
receiver.add_transport(Arc::new(
SneakernetTransport::new(&fresh_receiver_inbox).unwrap(),
));
let stale_bundle = receiver.handshake_bundle(NOW, &mut rng).unwrap();
let receiver_id = sender
.add_contact(
"receiver",
&stale_bundle,
&[DeliveryHint::Spool(stale_receiver_inbox)],
NOW,
&mut rng,
)
.unwrap();
sender
.send_message(&receiver_id, b"first attempt", NOW, &mut rng)
.unwrap();
sender
.send_message(&receiver_id, b"follow-up", NOW + 1, &mut rng)
.unwrap();
sender.tick(NOW + 2, &mut rng).await.unwrap();
assert!(sender
.messages_with(&receiver_id)
.unwrap()
.iter()
.all(|message| message.state == DeliveryState::Sent));
// Both first-flight envelopes were handed to a transport but never
// reached the recipient. A new scan must abandon that unconfirmed
// ratchet and encrypt the pending messages against the fresh bundle.
let fresh_bundle = receiver.handshake_bundle(NOW + 3, &mut rng).unwrap();
sender
.add_contact(
"receiver",
&fresh_bundle,
&[DeliveryHint::Spool(fresh_receiver_inbox)],
NOW + 3,
&mut rng,
)
.unwrap();
assert!(sender
.messages_with(&receiver_id)
.unwrap()
.iter()
.all(|message| { message.state == DeliveryState::Queued && message.wire_id.is_none() }));
sender.tick(NOW + 4, &mut rng).await.unwrap();
let events = receiver.tick(NOW + 5, &mut rng).await.unwrap();
assert_eq!(count_received(&events), 0);
assert!(events
.iter()
.any(|event| matches!(event, Event::MessageRequestReceived { .. })));
let request = receiver.message_requests().unwrap().remove(0);
assert_eq!(request.preview, "first attempt");
receiver
.accept_message_request(&request.id, "sender", NOW + 6, &mut rng)
.unwrap();
let events = receiver.tick(NOW + 7, &mut rng).await.unwrap();
assert_eq!(count_received(&events), 2);
let history = receiver.messages_with(&request.account).unwrap();
assert_eq!(history.len(), 2);
assert!(history
.iter()
.any(|message| message.body == b"first attempt"));
assert!(history.iter().any(|message| message.body == b"follow-up"));
}
#[tokio::test]
async fn one_way_pairing_imports_the_initiators_signed_return_route() {
let mut rng = StdRng::seed_from_u64(702);
let dir = tempfile::tempdir().unwrap();
let phone_inbox = dir.path().join("phone-spool");
let desktop_inbox = dir.path().join("desktop-spool");
let mut phone =
Node::create(&dir.path().join("phone.db"), b"phone", TEST_KDF, &mut rng).unwrap();
let mut desktop = Node::create(
&dir.path().join("desktop.db"),
b"desktop",
TEST_KDF,
&mut rng,
)
.unwrap();
phone.add_transport(Arc::new(SneakernetTransport::new(&phone_inbox).unwrap()));
desktop.add_transport(Arc::new(SneakernetTransport::new(&desktop_inbox).unwrap()));
// Runtime startup records the phone's current signed return route even
// though only the phone scans the desktop during pairing.
phone
.handshake_bundle_with_hints(&[DeliveryHint::Spool(phone_inbox.clone())], NOW, &mut rng)
.unwrap();
let desktop_bundle = desktop.handshake_bundle(NOW, &mut rng).unwrap();
let desktop_id = phone
.add_contact(
"desktop",
&desktop_bundle,
&[DeliveryHint::Spool(desktop_inbox)],
NOW,
&mut rng,
)
.unwrap();
let message = phone
.send_message(&desktop_id, b"one scan is bidirectional", NOW, &mut rng)
.unwrap();
phone.tick(NOW + 1, &mut rng).await.unwrap();
let events = desktop.tick(NOW + 2, &mut rng).await.unwrap();
assert_eq!(count_received(&events), 0);
assert!(events
.iter()
.any(|event| matches!(event, Event::MessageRequestReceived { .. })));
assert!(desktop.contacts().unwrap().is_empty());
let request = desktop.message_requests().unwrap().remove(0);
assert_eq!(request.preview, "one scan is bidirectional");
desktop
.accept_message_request(&request.id, "phone", NOW + 2, &mut rng)
.unwrap();
assert_eq!(desktop.contacts().unwrap().len(), 1);
assert_eq!(
desktop.messages_with(&request.account).unwrap()[0].body,
b"one scan is bidirectional"
);
desktop.tick(NOW + 3, &mut rng).await.unwrap();
let events = phone.tick(NOW + 4, &mut rng).await.unwrap();
assert!(delivered_ids(&events).contains(&message));
}
// ---------------------------------------------------------------------------
// 1. Full round trip over sneakernet spools: handshake, messages, receipts,
// restart persistence, reply on the established session.
// ---------------------------------------------------------------------------
#[tokio::test]
async fn sneakernet_round_trip_with_receipts_and_restart() {
let mut rng = StdRng::seed_from_u64(1);
let dir = tempfile::tempdir().unwrap();
let alice_db = dir.path().join("alice.db");
let bob_db = dir.path().join("bob.db");
let alice_inbox = dir.path().join("alice-spool");
let bob_inbox = dir.path().join("bob-spool");
let mut alice = Node::create(&alice_db, b"alice-pass", TEST_KDF, &mut rng).unwrap();
let mut bob = Node::create(&bob_db, b"bob-pass", TEST_KDF, &mut rng).unwrap();
alice.add_transport(Arc::new(SneakernetTransport::new(&alice_inbox).unwrap()));
bob.add_transport(Arc::new(SneakernetTransport::new(&bob_inbox).unwrap()));
// Mutual out-of-band exchange (QR codes at a kitchen table): each side
// gets the other's signed bundle and spool hint.
let alice_bundle = alice.handshake_bundle(NOW, &mut rng).unwrap();
let bob_bundle = bob.handshake_bundle(NOW, &mut rng).unwrap();
let bob_id = alice
.add_contact(
"bob",
&bob_bundle,
&[DeliveryHint::Spool(bob_inbox.clone())],
NOW,
&mut rng,
)
.unwrap();
let alice_id = bob
.add_contact(
"alice",
&alice_bundle,
&[DeliveryHint::Spool(alice_inbox.clone())],
NOW,
&mut rng,
)
.unwrap();
assert_eq!(bob_id, bob.peer_id());
assert_eq!(alice_id, alice.peer_id());
// Alice queues two messages; the first rides the handshake flight.
let m1 = alice
.send_message(&bob_id, b"hello over a usb stick", NOW, &mut rng)
.unwrap();
let m2 = alice
.send_message(&bob_id, b"second, same courier", NOW, &mut rng)
.unwrap();
// Flush: envelopes land in Bob's spool; records advance Queued -> Sent.
let events = alice.tick(NOW + 1, &mut rng).await.unwrap();
assert_eq!(
events
.iter()
.filter(|e| matches!(
e,
Event::DeliveryUpdated {
state: DeliveryState::Sent,
..
}
))
.count(),
2
);
assert_eq!(alice.queued().unwrap(), 0);
// Bob "receives the stick": session established, both messages decrypt,
// an encrypted receipt is queued and flushed back in the same tick
// (Bob already has Alice's hints).
let events = bob.tick(NOW + 60, &mut rng).await.unwrap();
assert!(events
.iter()
.any(|e| matches!(e, Event::SessionEstablished { peer } if *peer == alice_id)));
assert_eq!(count_received(&events), 2);
assert_eq!(bob.queued().unwrap(), 0, "receipt flushed to alice's spool");
// Alice reads the return courier: both records advance to Delivered.
let events = alice.tick(NOW + 120, &mut rng).await.unwrap();
let delivered = delivered_ids(&events);
assert!(delivered.contains(&m1) && delivered.contains(&m2));
let history = alice.messages_with(&bob_id).unwrap();
assert!(history.iter().all(|r| r.state == DeliveryState::Delivered));
assert!(history
.iter()
.all(|record| matches!(decode_content(&record.body), DecodedContent::LegacyText(_))));
// ---- Both devices restart; everything must survive. ----
drop(alice);
drop(bob);
let mut alice = Node::open(&alice_db, b"alice-pass").unwrap();
let mut bob = Node::open(&bob_db, b"bob-pass").unwrap();
alice.add_transport(Arc::new(SneakernetTransport::new(&alice_inbox).unwrap()));
bob.add_transport(Arc::new(SneakernetTransport::new(&bob_inbox).unwrap()));
assert_eq!(
alice.messages_with(&bob_id).unwrap().len(),
2,
"history survives restart"
);
// Bob replies on the established (persisted) session — no new handshake.
let r1 = bob
.send_message(&alice_id, b"got both, replying", NOW + 200, &mut rng)
.unwrap();
let reply_history = bob.messages_with(&alice_id).unwrap();
assert!(matches!(
decode_content(&reply_history.last().unwrap().body),
DecodedContent::Text { id, text: "got both, replying" } if id == r1
));
bob.tick(NOW + 201, &mut rng).await.unwrap();
let events = alice.tick(NOW + 260, &mut rng).await.unwrap();
assert_eq!(count_received(&events), 1);
assert!(events.iter().any(|event| matches!(
event,
Event::MessageReceived {
id,
content: ContentStatus::Text { id: content_id },
body,
..
} if *id == r1 && content_id == id && body == b"got both, replying"
)));
// Alice's receipt makes it back to Bob.
let events = bob.tick(NOW + 320, &mut rng).await.unwrap();
assert!(delivered_ids(&events).contains(&r1));
// Authenticated unsupported and malformed content is retained exactly,
// acknowledged normally, and never exposed as raw application text.
let mut unsupported = CONTENT_MAGIC.to_vec();
unsupported.push(2); // unknown framing version
let mut malformed = CONTENT_MAGIC.to_vec();
malformed.push(1); // truncated v1 header
let unsupported_id = bob
.send_message(&alice_id, &unsupported, NOW + 400, &mut rng)
.unwrap();
let malformed_id = bob
.send_message(&alice_id, &malformed, NOW + 400, &mut rng)
.unwrap();
bob.tick(NOW + 401, &mut rng).await.unwrap();
let events = alice.tick(NOW + 460, &mut rng).await.unwrap();
assert!(events.iter().any(|event| matches!(
event,
Event::MessageReceived {
body,
content: ContentStatus::Unsupported { format_version: Some(2), kind: None },
..
} if body.is_empty()
)));
assert!(events.iter().any(|event| matches!(
event,
Event::MessageReceived {
body,
content: ContentStatus::Malformed,
..
} if body.is_empty()
)));
let retained = alice.messages_with(&bob_id).unwrap();
assert!(retained.iter().any(|record| record.body == unsupported));
assert!(retained.iter().any(|record| record.body == malformed));
let events = bob.tick(NOW + 520, &mut rng).await.unwrap();
let delivered = delivered_ids(&events);
assert!(delivered.contains(&unsupported_id) && delivered.contains(&malformed_id));
// Re-encrypting the same logical event under two transport envelopes is
// still one message inside this conversation and author scope. Both
// envelopes are acknowledged so the sender's delivery ladder completes.
let repeated = encode_text([0x42; 16], "once").unwrap();
let copy_one = bob
.send_message(&alice_id, &repeated, NOW + 600, &mut rng)
.unwrap();
let copy_two = bob
.send_message(&alice_id, &repeated, NOW + 600, &mut rng)
.unwrap();
bob.tick(NOW + 601, &mut rng).await.unwrap();
let events = alice.tick(NOW + 660, &mut rng).await.unwrap();
assert_eq!(
events
.iter()
.filter(|event| matches!(
event,
Event::MessageReceived {
content: ContentStatus::Text { id },
..
} if *id == [0x42; 16]
))
.count(),
1
);
assert_eq!(
alice
.messages_with(&bob_id)
.unwrap()
.iter()
.filter(|record| matches!(
decode_content(&record.body),
DecodedContent::Text { id, .. } if id == [0x42; 16]
))
.count(),
1
);
let events = bob.tick(NOW + 720, &mut rng).await.unwrap();
let delivered = delivered_ids(&events);
assert!(delivered.contains(©_one) && delivered.contains(©_two));
// Wrong passphrase still fails closed.
assert!(Node::open(&alice_db, b"wrong").is_err());
}
// ---------------------------------------------------------------------------
// Mock mesh transport: in-memory network keyed by MeshNode number, small MTU,
// optional duplicate delivery (multipath is normal).
// ---------------------------------------------------------------------------
type Net = Arc<Mutex<HashMap<u32, Vec<Envelope>>>>;
struct MockMesh {
net: Net,
me: u32,
mtu: usize,
duplicate: bool,
}
#[async_trait]
impl Transport for MockMesh {
fn profile(&self) -> LinkProfile {
LinkProfile {
mtu: self.mtu,
latency: LatencyClass::Seconds,
cost: CostClass::Airtime,
broadcast: false,
}
}
async fn reachable(&self, peer: &DeliveryHint) -> Reachability {
match peer {
DeliveryHint::MeshNode(_) => Reachability::Now,
_ => Reachability::Unreachable,
}
}
async fn send(
&self,
peer: &DeliveryHint,
envelope: &Envelope,
) -> kult_transport::Result<SendReceipt> {
let DeliveryHint::MeshNode(n) = peer else {
return Err(TransportError::UnsupportedHint);
};
let mut net = self.net.lock().unwrap();
let queue = net.entry(*n).or_default();
queue.push(envelope.clone());
if self.duplicate {
queue.push(envelope.clone());
}
Ok(SendReceipt::HandedToLink)
}
async fn recv(&self) -> kult_transport::Result<Vec<Envelope>> {
Ok(std::mem::take(
self.net.lock().unwrap().entry(self.me).or_default(),
))
}
}
// ---------------------------------------------------------------------------
// 2. 180-byte MTU with duplicate delivery: envelopes fragment on send,
// reassemble on receive, and multipath duplicates dedup to exactly one
// message and one receipt.
// ---------------------------------------------------------------------------
#[tokio::test]
async fn small_mtu_fragmentation_and_duplicate_dedup() {
let mut rng = StdRng::seed_from_u64(2);
let dir = tempfile::tempdir().unwrap();
let net: Net = Arc::new(Mutex::new(HashMap::new()));
let mut alice = Node::create(&dir.path().join("a.db"), b"a", TEST_KDF, &mut rng).unwrap();
let mut bob = Node::create(&dir.path().join("b.db"), b"b", TEST_KDF, &mut rng).unwrap();
alice.add_transport(Arc::new(MockMesh {
net: net.clone(),
me: 1,
mtu: 180,
duplicate: true,
}));
bob.add_transport(Arc::new(MockMesh {
net: net.clone(),
me: 2,
mtu: 180,
duplicate: true,
}));
let bob_bundle = bob.handshake_bundle(NOW, &mut rng).unwrap();
let alice_bundle = alice.handshake_bundle(NOW, &mut rng).unwrap();
let bob_id = alice
.add_contact(
"bob",
&bob_bundle,
&[DeliveryHint::MeshNode(2)],
NOW,
&mut rng,
)
.unwrap();
let alice_id = bob
.add_contact(
"alice",
&alice_bundle,
&[DeliveryHint::MeshNode(1)],
NOW,
&mut rng,
)
.unwrap();
assert_eq!(alice_id, alice.peer_id());
// 600 bytes of body pads to the 1024 bucket — far over one 180 B frame.
let big = vec![0x42u8; 600];
let m1 = alice.send_message(&bob_id, &big, NOW, &mut rng).unwrap();
alice.tick(NOW + 1, &mut rng).await.unwrap();
// Everything on the wire is a fragment within the MTU (and duplicated).
{
let net = net.lock().unwrap();
let frames = net.get(&2).unwrap();
assert!(frames.len() >= 4, "large envelope must fragment");
assert!(frames.iter().all(|f| f.encode().len() <= 180));
}
let events = bob.tick(NOW + 5, &mut rng).await.unwrap();
assert_eq!(
count_received(&events),
1,
"duplicates dedup to one message"
);
let received = events.iter().find_map(|e| match e {
Event::MessageReceived { body, .. } => Some(body.clone()),
_ => None,
});
assert_eq!(received.unwrap(), big);
// Receipt returns (also fragmented, also duplicated) → exactly one
// Delivered transition.
let events = alice.tick(NOW + 10, &mut rng).await.unwrap();
assert_eq!(delivered_ids(&events), vec![m1]);
let events = alice.tick(NOW + 15, &mut rng).await.unwrap();
assert!(delivered_ids(&events).is_empty(), "no double delivery");
}
#[tokio::test]
async fn passive_retry_replays_a_lost_end_to_end_receipt() {
let mut rng = StdRng::seed_from_u64(202);
let dir = tempfile::tempdir().unwrap();
let net: Net = Arc::new(Mutex::new(HashMap::new()));
let mut alice = Node::create(&dir.path().join("a.db"), b"a", TEST_KDF, &mut rng).unwrap();
let mut bob = Node::create(&dir.path().join("b.db"), b"b", TEST_KDF, &mut rng).unwrap();
alice.add_transport(Arc::new(MockMesh {
net: net.clone(),
me: 1,
mtu: 64 * 1024,
duplicate: false,
}));
bob.add_transport(Arc::new(MockMesh {
net: net.clone(),
me: 2,
mtu: 64 * 1024,
duplicate: false,
}));
let bob_bundle = bob.handshake_bundle(NOW, &mut rng).unwrap();
let alice_bundle = alice.handshake_bundle(NOW, &mut rng).unwrap();
let bob_id = alice
.add_contact(
"bob",
&bob_bundle,
&[DeliveryHint::MeshNode(2)],
NOW,
&mut rng,
)
.unwrap();
bob.add_contact(
"alice",
&alice_bundle,
&[DeliveryHint::MeshNode(1)],
NOW,
&mut rng,
)
.unwrap();
let message = alice
.send_message(&bob_id, b"receipt may be lost", NOW, &mut rng)
.unwrap();
alice.tick(NOW + 1, &mut rng).await.unwrap();
assert_eq!(
count_received(&bob.tick(NOW + 2, &mut rng).await.unwrap()),
1
);
// Simulate a carrier losing Bob's first receipt after Bob handed it off.
net.lock().unwrap().entry(1).or_default().clear();
assert_eq!(
alice
.messages_with(&bob_id)
.unwrap()
.iter()
.find(|record| record.id == message)
.unwrap()
.state,
DeliveryState::Sent
);
// The retained ciphertext retries in the passive lane. Bob recognizes
// the exact duplicate and replays the receipt without storing it twice.
alice.tick(NOW + 901, &mut rng).await.unwrap();
let replay_events = bob.tick(NOW + 902, &mut rng).await.unwrap();
assert_eq!(count_received(&replay_events), 0);
let alice_events = alice.tick(NOW + 903, &mut rng).await.unwrap();
assert!(delivered_ids(&alice_events).contains(&message));
assert_eq!(alice.queued().unwrap(), 0);
}
// ---------------------------------------------------------------------------
// 3. A failing link: sends error, the item stays queued with exponential
// backoff, and goes out once the link recovers.
// ---------------------------------------------------------------------------
struct FlakyLink {
healthy: Arc<AtomicBool>,
attempts: Arc<AtomicU32>,
net: Net,
}
#[async_trait]
impl Transport for FlakyLink {
fn profile(&self) -> LinkProfile {
LinkProfile {
mtu: 64 * 1024,
latency: LatencyClass::Millis,
cost: CostClass::Metered,
broadcast: false,
}
}
async fn reachable(&self, peer: &DeliveryHint) -> Reachability {
match peer {
DeliveryHint::MeshNode(_) => Reachability::Now,
_ => Reachability::Unreachable,
}
}
async fn send(
&self,
peer: &DeliveryHint,
envelope: &Envelope,
) -> kult_transport::Result<SendReceipt> {
self.attempts.fetch_add(1, Ordering::SeqCst);
if !self.healthy.load(Ordering::SeqCst) {
return Err(TransportError::Io(std::io::Error::other("link down")));
}
let DeliveryHint::MeshNode(n) = peer else {
return Err(TransportError::UnsupportedHint);
};
self.net
.lock()
.unwrap()
.entry(*n)
.or_default()
.push(envelope.clone());
Ok(SendReceipt::HandedToLink)
}
async fn recv(&self) -> kult_transport::Result<Vec<Envelope>> {
Ok(Vec::new())
}
}
#[tokio::test]
async fn retry_with_backoff_until_link_recovers() {
let mut rng = StdRng::seed_from_u64(3);
let dir = tempfile::tempdir().unwrap();
let net: Net = Arc::new(Mutex::new(HashMap::new()));
let healthy = Arc::new(AtomicBool::new(false));
let attempts = Arc::new(AtomicU32::new(0));
let mut alice = Node::create(&dir.path().join("a.db"), b"a", TEST_KDF, &mut rng).unwrap();
alice.add_transport(Arc::new(FlakyLink {
healthy: healthy.clone(),
attempts: attempts.clone(),
net: net.clone(),
}));
// A standalone signed bundle is enough to add a contact.
let peer_identity = Identity::generate(&mut rng);
let spk = SignedPrekeySecret::generate(&mut rng, 1);
let pqspk = PqPrekeySecret::generate(&mut rng, 1);
let opk = OneTimePrekeySecret::generate(&mut rng, 1);
let bundle = PrekeyBundle::build(
&peer_identity,
&spk,
&pqspk,
Some(&opk),
NOW + 86_400,
vec![],
)
.encode();
let peer = alice
.add_contact("peer", &bundle, &[DeliveryHint::MeshNode(9)], NOW, &mut rng)
.unwrap();
let msg = alice
.send_message(&peer, b"stubborn", NOW, &mut rng)
.unwrap();
// Link down: the message plus terminal content- and discovery-capability
// controls all stay queued.
alice.tick(NOW, &mut rng).await.unwrap();
assert_eq!(attempts.load(Ordering::SeqCst), 3);
assert_eq!(alice.queued().unwrap(), 3);
// Inside the backoff window nothing is attempted.
alice.tick(NOW + 5, &mut rng).await.unwrap();
assert_eq!(
attempts.load(Ordering::SeqCst),
3,
"backoff suppresses retry"
);
// Link recovers; after the backoff expires the send succeeds.
healthy.store(true, Ordering::SeqCst);
alice.tick(NOW + 31, &mut rng).await.unwrap();
assert_eq!(attempts.load(Ordering::SeqCst), 6);
assert_eq!(alice.queued().unwrap(), 0);
let record = alice
.messages_with(&peer)
.unwrap()
.into_iter()
.find(|r| r.id == msg)
.unwrap();
assert_eq!(record.state, DeliveryState::Sent);
assert_eq!(net.lock().unwrap().get(&9).unwrap().len(), 3);
}
#[tokio::test]
async fn fresh_user_message_bypasses_passive_unreachable_retry() {
let mut rng = StdRng::seed_from_u64(303);
let dir = tempfile::tempdir().unwrap();
let net: Net = Arc::new(Mutex::new(HashMap::new()));
let healthy = Arc::new(AtomicBool::new(false));
let attempts = Arc::new(AtomicU32::new(0));
let mut alice = Node::create(&dir.path().join("a.db"), b"a", TEST_KDF, &mut rng).unwrap();
alice.add_transport(Arc::new(FlakyLink {
healthy: healthy.clone(),
attempts,
net: net.clone(),
}));
let peer_identity = Identity::generate(&mut rng);
let spk = SignedPrekeySecret::generate(&mut rng, 1);
let pqspk = PqPrekeySecret::generate(&mut rng, 1);
let opk = OneTimePrekeySecret::generate(&mut rng, 1);
let bundle = PrekeyBundle::build(
&peer_identity,
&spk,
&pqspk,
Some(&opk),
NOW + 86_400,
vec![],
)
.encode();
let peer = alice
.add_contact("peer", &bundle, &[DeliveryHint::MeshNode(9)], NOW, &mut rng)
.unwrap();
let old = alice
.send_message(&peer, b"old unreachable", NOW, &mut rng)
.unwrap();
alice.tick(NOW, &mut rng).await.unwrap();
alice.tick(NOW + 31, &mut rng).await.unwrap();
alice.tick(NOW + 92, &mut rng).await.unwrap();
// Three failed rounds demote the old envelope to the passive 15-minute
// lane. A new tap of Send still gets one immediate foreground attempt.
let fresh = alice
.send_message(&peer, b"fresh foreground", NOW + 100, &mut rng)
.unwrap();
healthy.store(true, Ordering::SeqCst);
alice.tick(NOW + 100, &mut rng).await.unwrap();
let history = alice.messages_with(&peer).unwrap();
assert_eq!(
history
.iter()
.find(|message| message.id == fresh)
.unwrap()
.state,
DeliveryState::Sent
);
assert_eq!(
history
.iter()
.find(|message| message.id == old)
.unwrap()
.state,
DeliveryState::Queued,
"the passive item remains paced instead of blocking the new action"
);
assert_eq!(net.lock().unwrap().get(&9).unwrap().len(), 1);
// Once its passive deadline arrives, the old item resumes automatically.
alice.tick(NOW + 992, &mut rng).await.unwrap();
assert_eq!(
alice
.messages_with(&peer)
.unwrap()
.iter()
.find(|message| message.id == old)
.unwrap()
.state,
DeliveryState::Sent
);
}
#[tokio::test]
async fn undelivered_message_fails_and_leaves_queue_after_thirty_days() {
let mut rng = StdRng::seed_from_u64(304);
let dir = tempfile::tempdir().unwrap();
let net: Net = Arc::new(Mutex::new(HashMap::new()));
let healthy = Arc::new(AtomicBool::new(false));
let attempts = Arc::new(AtomicU32::new(0));
let mut alice = Node::create(&dir.path().join("a.db"), b"a", TEST_KDF, &mut rng).unwrap();
alice.add_transport(Arc::new(FlakyLink {
healthy,
attempts,
net,
}));
let peer_identity = Identity::generate(&mut rng);
let spk = SignedPrekeySecret::generate(&mut rng, 1);
let pqspk = PqPrekeySecret::generate(&mut rng, 1);
let opk = OneTimePrekeySecret::generate(&mut rng, 1);
let bundle = PrekeyBundle::build(
&peer_identity,
&spk,
&pqspk,
Some(&opk),
NOW + 86_400,
vec![],
)
.encode();
let peer = alice
.add_contact("peer", &bundle, &[DeliveryHint::MeshNode(9)], NOW, &mut rng)
.unwrap();
let message = alice
.send_message(&peer, b"bounded delivery", NOW, &mut rng)
.unwrap();
alice.tick(NOW, &mut rng).await.unwrap();
let events = alice.tick(NOW + 30 * 86_400, &mut rng).await.unwrap();
assert!(events.iter().any(|event| {
matches!(
event,
Event::DeliveryUpdated {
id,
state: DeliveryState::Failed
} if *id == message
)
}));
assert_eq!(alice.queued().unwrap(), 0);
assert_eq!(
alice
.messages_with(&peer)
.unwrap()
.iter()
.find(|record| record.id == message)
.unwrap()
.state,
DeliveryState::Failed
);
assert!(alice
.message_device_deliveries(&message)
.unwrap()
.iter()
.all(|delivery| delivery.state == DeliveryState::Failed));
}
// ---------------------------------------------------------------------------
// 4. Courier reordering: a session message arrives before the handshake that
// creates the session — and the receiver restarts in between. The stashed
// envelope survives and both messages decrypt once the handshake lands.
// ---------------------------------------------------------------------------
#[tokio::test]
async fn out_of_order_arrival_survives_restart() {
let mut rng = StdRng::seed_from_u64(4);
let dir = tempfile::tempdir().unwrap();
let bob_db = dir.path().join("b.db");
let net: Net = Arc::new(Mutex::new(HashMap::new()));
let mut alice = Node::create(&dir.path().join("a.db"), b"a", TEST_KDF, &mut rng).unwrap();
let mut bob = Node::create(&bob_db, b"b", TEST_KDF, &mut rng).unwrap();
let mesh = |me| MockMesh {
net: net.clone(),
me,
mtu: 64 * 1024,
duplicate: false,
};
alice.add_transport(Arc::new(mesh(1)));
bob.add_transport(Arc::new(mesh(2)));
let bob_bundle = bob.handshake_bundle(NOW, &mut rng).unwrap();
let bob_id = alice
.add_contact(
"bob",
&bob_bundle,
&[DeliveryHint::MeshNode(2)],
NOW,
&mut rng,
)
.unwrap();
alice
.send_message(&bob_id, b"first (handshake)", NOW, &mut rng)
.unwrap();
alice
.send_message(&bob_id, b"second (session)", NOW, &mut rng)
.unwrap();
alice.tick(NOW + 1, &mut rng).await.unwrap();
// Intercept the two message envelopes and deliver the session message
// first. The terminal capability controls are irrelevant to this test.
// (Picked by kind: priority flushing sends the text-class envelope
// before the handshake, so wire order is not handshake-first.)
let (handshake, session_msg) = {
let mut locked = net.lock().unwrap();
let queue = locked.get_mut(&2).unwrap();
assert_eq!(queue.len(), 4);
let hs_at = queue
.iter()
.position(|e| e.kind == EnvelopeKind::Handshake)
.unwrap();
let hs = queue.remove(hs_at);
let msg_at = queue
.iter()
.position(|e| e.kind == EnvelopeKind::Message)
.unwrap();
let sm = queue.remove(msg_at);
assert_eq!(queue.len(), 2);
assert!(queue
.drain(..)
.all(|envelope| envelope.kind == EnvelopeKind::Receipt));
(hs, sm)
};
// Session message first: nothing can read it yet → stashed, no events.