darkbio-wire 0.16.0

Encrypted protocol between Ark and host
Documentation
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
// wire-rs: encrypted protocol between Ark and host
// Copyright 2026 Dark Bio AG. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//! Client scenario tests driven by mock server scripts.

use super::*;
use crate::testing;

/// Runs a script with logging enabled.
fn run_logged(steps: &[Step]) -> Summary {
    testing::init_tracing();
    run(steps)
}

/// Tests a handshake followed by request and reply exchanges, including
/// requests sent before their replies are read.
#[test]
fn test_scripted_round_trip() {
    let summary = run_logged(&[
        // Establish a session and exchange one request and reply
        Step::Handshake,
        Step::Hello,
        Step::Send(1),
        Step::Reply(1),
        Step::Recv,
        // Send two requests before reading either reply
        Step::Send(2),
        Step::Send(3),
        Step::Reply(2),
        Step::Reply(3),
        Step::Recv,
        Step::Recv,
    ]);
    assert_eq!(
        summary,
        Summary {
            established: true,
            handshakes: 1,
            messages: 3,
            resets: 0,
            failures: 0,
            reads: 4,
        }
    );
}

/// Tests that a failed send ends receiving, even with a valid reply already
/// queued.
#[test]
fn test_scripted_send_failure_ends_receiving() {
    let summary = run_logged(&[
        // Queue a valid reply in a fresh session
        Step::Handshake,
        Step::Hello,
        Step::Reply(1),
        // Fail a send, which ends receiving that reply
        Step::Break,
        Step::Send(2),
        Step::Recv,
        // Healing the writer does not revive the session
        Step::Heal,
        Step::Send(3),
        // Reconnecting creates a session that sends and receives again
        Step::Handshake,
        Step::Hello,
        Step::Send(4),
        Step::Reply(4),
        Step::Recv,
    ]);
    assert!(summary.established);
    assert_eq!(summary.handshakes, 2);
    assert_eq!(summary.messages, 1);
    assert_eq!(summary.failures, 1);

    // With no reply waiting, EOF ends the receive side after a failed send
    let summary = run_logged(&[
        Step::Handshake,
        Step::Hello,
        Step::Break,
        Step::Send(1),
        Step::Recv,
    ]);
    assert!(!summary.established);
    assert_eq!(summary.failures, 1);
}

/// Tests that an oversized send keeps both encryption sequences, while a
/// corrupt reply ends both directions until a new handshake.
#[test]
fn test_scripted_send_refusal_and_receive_failure() {
    let summary = run_logged(&[
        // Refuse an oversized send without a session, then within one
        Step::SendOversized,
        Step::Handshake,
        Step::Hello,
        Step::SendOversized,
        // Both directions still work after the refusal
        Step::Send(1),
        Step::Reply(1),
        Step::Recv,
        // A corrupt reply ends receiving and sending
        Step::ReplyTampered,
        Step::Recv,
        Step::Send(2),
        // A new handshake restores both directions
        Step::Handshake,
        Step::Hello,
        Step::Send(3),
        Step::Reply(3),
        Step::Recv,
    ]);
    assert!(summary.established);
    assert_eq!(summary.handshakes, 2);
    assert_eq!(summary.messages, 2);
    assert_eq!(summary.failures, 1);
}

/// Tests that a retained sender works only in its own session and cannot
/// damage a later one.
#[test]
fn test_scripted_retained_sender() {
    let summary = run_logged(&[
        // Retaining the absent sender before any session is safe
        Step::Retain,
        Step::SendRetained(0),
        // A sender retained in a session sends in it
        Step::Handshake,
        Step::Hello,
        Step::Retain,
        Step::SendRetained(1),
        // After a reconnect it is refused, and the new session still works
        Step::Handshake,
        Step::Hello,
        Step::SendRetained(2),
        Step::Send(3),
        Step::Reply(3),
        Step::Recv,
        // Retaining the new sender makes the slot usable again
        Step::Retain,
        Step::SendRetained(4),
        Step::Reply(4),
        Step::Recv,
    ]);
    assert!(summary.established);
    assert_eq!(summary.handshakes, 2);
    assert_eq!(summary.messages, 2);
    assert_eq!(summary.failures, 0);
}

/// Tests that a failed reconnect and a failed retained send each retire the
/// old handle for good.
#[test]
fn test_scripted_retained_sender_failures() {
    let summary = run_logged(&[
        // Retain a sender, then fail a reconnect, which retires it
        Step::Handshake,
        Step::Hello,
        Step::Retain,
        Step::Handshake,
        Step::HelloTampered,
        Step::SendRetained(1),
        // It stays refused in the next session, which still sends
        Step::Handshake,
        Step::Hello,
        Step::SendRetained(2),
        Step::Send(3),
        // Retain the new sender and fail a send through it, retiring it too
        Step::Retain,
        Step::Break,
        Step::SendRetained(4),
        Step::Heal,
        Step::SendRetained(5),
        // Another session cannot revive it, and its refusal leaves the new
        // sender working
        Step::Handshake,
        Step::Hello,
        Step::SendRetained(6),
        Step::Send(7),
        Step::Reply(7),
        Step::Recv,
    ]);
    assert!(summary.established);
    assert_eq!(summary.handshakes, 3);
    assert_eq!(summary.messages, 1);
    assert_eq!(summary.failures, 1);
}

/// Tests that each flawed ArkHello fails the handshake with its own error and
/// leaves the client without a session.
#[test]
fn test_scripted_flawed_hellos() {
    for flaw in [
        Step::HelloTampered,
        Step::HelloBadAuth,
        Step::HelloBadSigner,
        Step::HelloBadPayload,
        Step::HelloBadKey,
        Step::HelloBadEncap,
        Step::HelloBadAttest,
    ] {
        // A flawed reply to the client's own hello fails its handshake
        let summary = run_logged(&[Step::Handshake, flaw.clone(), Step::Send(1)]);
        assert!(!summary.established, "{flaw:?}");
        assert_eq!(summary.failures, 1, "{flaw:?}");

        // Without a HostHello to answer, the mock sends junk instead
        let summary = run_logged(&[flaw.clone(), Step::Recv]);
        assert!(!summary.established, "{flaw:?}");
        assert_eq!(summary.failures, 1, "{flaw:?}");
    }
}

/// Tests that a handshake drains every frame not meant for it until its own
/// ArkHello arrives.
#[test]
fn test_scripted_stale_skipping() {
    let summary = run_logged(&[
        // Leave a signal and junk from before the handshake
        Step::Dropped,
        Step::Junk(vec![1, 2, 3]),
        // Mix a stale hello, a signal and an empty packet into the handshake
        Step::Handshake,
        Step::HelloStale,
        Step::Dropped,
        Step::Junk(vec![]),
        Step::Hello,
    ]);
    assert!(summary.established);
    assert_eq!(summary.failures, 0);

    // Reconnect drains replies left unread from the previous session
    let summary = run_logged(&[
        Step::Handshake,
        Step::Hello,
        Step::Send(1),
        Step::Reply(1),
        Step::Handshake,
        Step::Hello,
        Step::Send(2),
        Step::Reply(2),
        Step::Recv,
    ]);
    assert_eq!(summary.handshakes, 2);
    assert_eq!(summary.messages, 1);
    assert_eq!(summary.failures, 0);

    // Reconnect also drains ArkHellos left from an earlier handshake
    let summary = run_logged(&[
        Step::Handshake,
        Step::Hello,
        Step::Hello,
        Step::Handshake,
        Step::Hello,
    ]);
    assert_eq!(summary.handshakes, 2);
    assert!(summary.established);

    // Backlogs drain fully, up to the longest a script can queue
    for stale in [40, MAX_STEPS - 2] {
        let mut steps = vec![Step::Handshake];
        steps.extend(std::iter::repeat_n(Step::Dropped, stale));
        steps.push(Step::Hello);
        let summary = run_logged(&steps);
        assert!(summary.established);
        assert_eq!(summary.failures, 0);
    }

    // An undecodable frame left by an interrupted transfer is also drained
    let summary = run_logged(&[Step::Handshake, Step::Undecodable, Step::Hello]);
    assert!(summary.established);
    assert_eq!(summary.failures, 0);
}

/// Tests that a read error and EOF each abort the handshake with their own error.
#[test]
fn test_scripted_handshake_interrupted() {
    // A failed read aborts the handshake
    let summary = run_logged(&[Step::Handshake, Step::Yield, Step::Send(1)]);
    assert!(!summary.established);
    assert_eq!(summary.failures, 1);

    // So does the end of the script's input
    let summary = run_logged(&[Step::Handshake]);
    assert!(!summary.established);
    assert_eq!(summary.failures, 1);
}

/// Tests that the client delivers decrypted bytes without checking them as
/// protobuf, keeping the session for the next reply.
#[test]
fn test_scripted_garbage_keeps_session() {
    let summary = run_logged(&[
        Step::Handshake,
        Step::Hello,
        Step::Garbage,
        Step::Recv,
        Step::Reply(2),
        Step::Recv,
    ]);
    assert!(summary.established);
    assert_eq!(summary.messages, 2);
    assert_eq!(summary.failures, 0);
}

/// Tests that input a session cannot decode or decrypt ends the session.
#[test]
fn test_scripted_undecryptable_drops_session() {
    // A replayed reply ends the session
    let summary = run_logged(&[
        Step::Handshake,
        Step::Hello,
        Step::Reply(1),
        Step::ReplyReplay,
        Step::Recv,
        Step::Recv,
        Step::Send(1),
    ]);
    assert!(!summary.established);
    assert_eq!(summary.messages, 1);
    assert_eq!(summary.failures, 1);

    // So does a reply from the previous session
    let summary = run_logged(&[
        Step::Handshake,
        Step::Hello,
        Step::Reply(1),
        Step::Recv,
        Step::Handshake,
        Step::Hello,
        Step::ReplyReplay,
        Step::Recv,
    ]);
    assert!(!summary.established);
    assert_eq!(summary.handshakes, 2);
    assert_eq!(summary.messages, 1);
    assert_eq!(summary.failures, 1);

    // So does a tampered reply
    let summary = run_logged(&[
        Step::Handshake,
        Step::Hello,
        Step::ReplyTampered,
        Step::Recv,
    ]);
    assert!(!summary.established);

    // So does junk
    let summary = run_logged(&[
        Step::Handshake,
        Step::Hello,
        Step::Junk(vec![7]),
        Step::Recv,
    ]);
    assert!(!summary.established);

    // So does an unexpected ArkHello
    let summary = run_logged(&[Step::Handshake, Step::Hello, Step::Hello, Step::Recv]);
    assert!(!summary.established);

    // So does a frame failing COBS decoding
    let summary = run_logged(&[Step::Handshake, Step::Hello, Step::Undecodable, Step::Recv]);
    assert!(!summary.established);
}

/// Tests that reading the server's empty frame reports a session reset and ends
/// the session.
#[test]
fn test_scripted_dropped_signal() {
    // A read signal ends the session until a new handshake
    let summary = run_logged(&[
        Step::Handshake,
        Step::Hello,
        Step::Dropped,
        Step::Recv,
        Step::Send(1),
        Step::Handshake,
        Step::Hello,
        Step::Send(2),
    ]);
    assert!(summary.established);
    assert_eq!(summary.handshakes, 2);
    assert_eq!(summary.resets, 1);
    assert_eq!(summary.failures, 0);

    // Sending still works until the signal is read
    let summary = run_logged(&[
        Step::Handshake,
        Step::Hello,
        Step::Dropped,
        Step::Send(1),
        Step::Recv,
        Step::Send(2),
    ]);
    assert!(!summary.established);
    assert_eq!(summary.resets, 1);
    assert_eq!(summary.failures, 0);

    // Without a receive context, signals stay queued for the next handshake
    let summary = run_logged(&[Step::Dropped, Step::Recv]);
    assert_eq!(summary.resets, 0);
    assert_eq!(summary.failures, 1);
    assert_eq!(summary.reads, 0);

    // Once one signal ends the session, receiving fails before reading the next
    let summary = run_logged(&[
        Step::Handshake,
        Step::Hello,
        Step::Dropped,
        Step::Dropped,
        Step::Recv,
        Step::Recv,
    ]);
    assert!(!summary.established);
    assert_eq!(summary.resets, 1);
    assert_eq!(summary.failures, 1);
}

/// Tests that receiving without a receive context fails before reading,
/// whatever input is queued.
#[test]
fn test_scripted_recv_without_session() {
    // Receiving fails before reading junk
    let summary = run_logged(&[Step::Junk(vec![1]), Step::Recv]);
    assert_eq!(summary.failures, 1);
    assert_eq!(summary.reads, 0);

    // It fails before reading an undecodable frame too
    let summary = run_logged(&[Step::Undecodable, Step::Recv]);
    assert_eq!(summary.failures, 1);
    assert_eq!(summary.reads, 0);

    // It fails without any input too
    let summary = run_logged(&[Step::Recv]);
    assert_eq!(summary.failures, 1);
    assert_eq!(summary.reads, 0);

    // A send refused without a session does not count as a failure
    let summary = run_logged(&[Step::Send(1)]);
    assert_eq!(summary.failures, 0);

    // A failed receive releases its context. Further receives leave queued
    // input for reconnect to drain before accepting fresh session traffic.
    let summary = run_logged(&[
        Step::Handshake,
        Step::Hello,
        Step::Undecodable,
        Step::Recv,
        Step::Junk(vec![1]),
        Step::Recv,
        Step::Handshake,
        Step::Hello,
        Step::Reply(1),
        Step::Recv,
    ]);
    assert!(summary.established);
    assert_eq!(summary.failures, 2);
    assert_eq!(summary.handshakes, 2);
    assert_eq!(summary.messages, 1);
}

/// Tests that a read error or EOF ends an established session and refuses later
/// sends through its sender.
#[test]
fn test_scripted_read_failure_drops_session() {
    // A read failing before the next call ends the session
    let summary = run_logged(&[Step::Handshake, Step::Hello, Step::Recv, Step::Send(1)]);
    assert!(!summary.established);
    assert_eq!(summary.failures, 1);

    // So does the end of the script's input
    let summary = run_logged(&[Step::Handshake, Step::Hello, Step::Recv]);
    assert!(!summary.established);
    assert_eq!(summary.failures, 1);
}

/// Tests that persistent write failures fail a send, a reset and a HostAck,
/// until healed writes and a new handshake restore the session.
#[test]
fn test_scripted_broken_transport() {
    let summary = run_logged(&[
        // Break writes in a session, failing a send and a reconnect's reset
        Step::Handshake,
        Step::Hello,
        Step::Break,
        Step::Send(1),
        Step::Send(2),
        Step::Handshake,
        // Heal them and reconnect
        Step::Heal,
        Step::Handshake,
        Step::Hello,
        Step::Send(3),
    ]);
    assert!(summary.established);
    assert_eq!(summary.handshakes, 2);
    assert_eq!(summary.failures, 1);

    // Writes breaking during a handshake fail its HostAck
    let summary = run_logged(&[
        Step::Handshake,
        Step::Break,
        Step::Hello,
        Step::Heal,
        Step::Handshake,
        Step::Hello,
    ]);
    assert!(summary.established);
    assert_eq!(summary.handshakes, 1);
    assert_eq!(summary.failures, 1);
}

/// Tests that a cut in a request, reset, HostHello or HostAck fails the call,
/// and the next handshake's reset lets the stream recover.
#[test]
fn test_scripted_cut_sends() {
    for point in [
        CutPoint::Start,
        CutPoint::Middle(5),
        CutPoint::Delimiter,
        CutPoint::Flush,
    ] {
        let cut = Step::Cut {
            point,
            then_broken: false,
        };

        // A cut request fails its send, and a reconnect recovers
        let summary = run_logged(&[
            Step::Handshake,
            Step::Hello,
            cut.clone(),
            Step::Send(1),
            Step::Handshake,
            Step::Hello,
            Step::Send(2),
        ]);
        assert!(summary.established, "{point:?}");
        assert_eq!(summary.handshakes, 2, "{point:?}");
        assert_eq!(summary.failures, 0, "{point:?}");

        // A cut reset or HostHello fails the handshake, and a retry recovers
        let summary = run_logged(&[cut.clone(), Step::Handshake, Step::Handshake, Step::Hello]);
        assert!(summary.established, "{point:?}");
        assert_eq!(summary.handshakes, 1, "{point:?}");
        assert_eq!(summary.failures, 1, "{point:?}");

        // A cut HostAck fails the handshake, and a retry recovers
        let summary = run_logged(&[
            Step::Handshake,
            cut,
            Step::Hello,
            Step::Handshake,
            Step::Hello,
        ]);
        assert!(summary.established, "{point:?}");
        assert_eq!(summary.handshakes, 1, "{point:?}");
        assert_eq!(summary.failures, 1, "{point:?}");
    }

    // A partial frame survives further write failures until a successful reset
    // can terminate it
    let summary = run_logged(&[
        Step::Handshake,
        Step::Hello,
        Step::Cut {
            point: CutPoint::Middle(5),
            then_broken: true,
        },
        Step::Send(1),
        Step::Handshake,
        Step::Heal,
        Step::Handshake,
        Step::Hello,
    ]);
    assert!(summary.established);
    assert_eq!(summary.handshakes, 2);
    assert_eq!(summary.failures, 1);
}

/// Tests that consecutive failed handshakes, by cut or timeout, still allow a
/// successful exchange afterwards.
///
/// Each reset must retire the previous truncated hello before another output
/// failure leaves its own tail. The points include a failed reset that leaves
/// the earlier fragment untouched, and one that only terminates it.
#[test]
fn test_scripted_consecutive_handshake_failures() {
    for timeout in [false, true] {
        for point in [
            CutPoint::Middle(5),
            CutPoint::Start,
            CutPoint::Middle(0),
            CutPoint::Middle(u16::MAX),
            CutPoint::Delimiter,
            CutPoint::Flush,
        ] {
            // Fault the output with a cut or a timeout, as the outer loop picks
            let fault = |point| {
                if timeout {
                    Step::Timeout(point)
                } else {
                    Step::Cut {
                        point,
                        then_broken: false,
                    }
                }
            };

            let summary = run_logged(&[
                // Leave a truncated hello from a first failed handshake
                fault(CutPoint::Middle(5)),
                Step::Handshake,
                // Fail a second handshake at the point under test
                fault(point),
                Step::Handshake,
                // A third handshake recovers and exchanges a message
                Step::Handshake,
                Step::Hello,
                Step::Send(1),
                Step::Reply(1),
                Step::Recv,
            ]);
            assert!(summary.established, "{point:?}, timeout: {timeout}");
            assert_eq!(summary.handshakes, 1, "{point:?}, timeout: {timeout}");
            assert_eq!(summary.messages, 1, "{point:?}, timeout: {timeout}");
            assert_eq!(summary.failures, 2, "{point:?}, timeout: {timeout}");
        }
    }
}

/// Tests frame assembly across reads as small as one byte.
#[test]
fn test_scripted_chunked_reads() {
    for chunk in [1u8, 7, 254, 255] {
        let summary = run_logged(&[
            // Limit reads to the chunk, then exchange a message
            Step::Chunk(chunk),
            Step::Handshake,
            Step::Hello,
            Step::Send(1),
            Step::Reply(1),
            Step::Recv,
            // A long junk frame ends the session, and a reconnect recovers
            Step::Junk(vec![1; 300]),
            Step::Recv,
            Step::Handshake,
            Step::Hello,
        ]);
        assert!(summary.established, "chunk {chunk}");
        assert_eq!(summary.messages, 1, "chunk {chunk}");
        assert_eq!(summary.handshakes, 2, "chunk {chunk}");
    }
}

/// Tests that several frames can arrive in one read, each batch ending at the
/// frame that settles its call.
#[test]
fn test_scripted_batched_reads() {
    // A signal, junk and the hello arrive in one read
    let summary = run_logged(&[
        Step::Handshake,
        Step::Batch(3),
        Step::Dropped,
        Step::Junk(vec![1]),
        Step::Hello,
    ]);
    assert!(summary.established);
    assert_eq!(summary.reads, 1);

    // So do an undecodable frame and the hello
    let summary = run_logged(&[
        Step::Handshake,
        Step::Batch(2),
        Step::Undecodable,
        Step::Hello,
    ]);
    assert!(summary.established);
    assert_eq!(summary.reads, 1);

    // A batch of two replies stops after the first, which settles its receive
    let summary = run_logged(&[
        Step::Handshake,
        Step::Hello,
        Step::Send(1),
        Step::Send(2),
        Step::Batch(2),
        Step::Reply(1),
        Step::Reply(2),
        Step::Recv,
        Step::Recv,
    ]);
    assert!(summary.established);
    assert_eq!(summary.messages, 2);
    assert_eq!(summary.reads, 3);

    // Batches still form under small chunks
    for chunk in [1u8, 7] {
        let summary = run_logged(&[
            Step::Chunk(chunk),
            Step::Handshake,
            Step::Batch(3),
            Step::Dropped,
            Step::Junk(vec![1]),
            Step::Hello,
            Step::Send(1),
        ]);
        assert!(summary.established, "chunk {chunk}");
    }
}

/// Tests truncated and unterminated input, in a handshake and in a session.
#[test]
fn test_scripted_interrupted_frames() {
    for cut in [0u8, 1, 7, 255] {
        // A handshake drains a truncated frame
        let summary = run_logged(&[
            Step::Handshake,
            Step::Hello,
            Step::Handshake,
            Step::Truncated(cut),
            Step::Hello,
        ]);
        assert!(summary.established, "cut {cut}");
        assert_eq!(summary.handshakes, 2, "cut {cut}");
        assert_eq!(summary.failures, 0, "cut {cut}");

        // A truncated frame ends a session
        let summary = run_logged(&[
            Step::Handshake,
            Step::Hello,
            Step::Send(1),
            Step::Reply(1),
            Step::Recv,
            Step::Truncated(cut),
            Step::Recv,
            Step::Send(2),
        ]);
        assert!(!summary.established, "cut {cut}");
        assert_eq!(summary.messages, 1, "cut {cut}");
        assert_eq!(summary.failures, 1, "cut {cut}");
    }

    // A lone delimiter completes a partial ArkHello
    let summary = run_logged(&[Step::Handshake, Step::Partial, Step::Dropped]);
    assert!(summary.established);
    assert_eq!(summary.reads, 2);

    // Other bytes merge into it, and the handshake drains the result
    let summary = run_logged(&[
        Step::Handshake,
        Step::Partial,
        Step::Junk(vec![1]),
        Step::Hello,
    ]);
    assert!(summary.established);
    assert_eq!(summary.failures, 0);

    // So does a second partial hello
    let summary = run_logged(&[
        Step::Handshake,
        Step::Partial,
        Step::Partial,
        Step::Dropped,
        Step::Hello,
    ]);
    assert!(summary.established);
    assert_eq!(summary.failures, 0);

    // In a session, a partial frame merged into a reply ends it
    let summary = run_logged(&[
        Step::Handshake,
        Step::Hello,
        Step::Send(1),
        Step::Partial,
        Step::Reply(1),
        Step::Recv,
        Step::Send(2),
    ]);
    assert!(!summary.established);
    assert_eq!(summary.failures, 1);

    // So does a partial ArkHello the delimiter completes
    let summary = run_logged(&[
        Step::Handshake,
        Step::Hello,
        Step::Partial,
        Step::Dropped,
        Step::Recv,
    ]);
    assert!(!summary.established);
    assert_eq!(summary.failures, 1);

    // Small chunks do not change how a delimiter completes the hello
    for chunk in [1u8, 7] {
        let summary = run_logged(&[
            Step::Chunk(chunk),
            Step::Handshake,
            Step::Partial,
            Step::Dropped,
        ]);
        assert!(summary.established, "chunk {chunk}");
    }
}

/// Tests that the model completes a pending ArkHello with the empty packet only
/// when the hello's encoding ends in a full run.
#[test]
fn test_partial_hello_empty_packet_model() {
    // Encoding 254 nonzero bytes ends in a full run, encoding 253 does not
    for (len, completes) in [(254, true), (253, false)] {
        // Leave a pending hello of that length in an idle model
        let clock = crate::transport::testing::test_clock().clock();
        let mut server = Server::new(&[], Outbox::new(&clock), Recorder::default());
        let pending = frame(&vec![0x11; len]);
        server.partial = Partial::Hello(7, pending[..pending.len() - 1].to_vec());

        // Deliver the empty packet and check the predicted frame
        server.execute(Step::Junk(vec![]));
        let (bytes, frame) = server.queue.pop_back().unwrap();
        assert_eq!(bytes, [0x01, 0x00], "len {len}");
        assert_eq!(
            matches!(
                frame,
                Some(Frame::ArkHello {
                    generation: 7,
                    flaw: Flaw::None
                })
            ),
            completes,
            "len {len}"
        );
    }
}

/// Tests that interrupted reads are retried during a handshake or a receive
/// without ending the call or changing its result.
#[test]
fn test_scripted_interrupted_reads() {
    let summary = run_logged(&[
        // Interrupt a read during the handshake
        Step::Handshake,
        Step::Interrupt,
        Step::Hello,
        // Interrupt a read during a receive
        Step::Send(1),
        Step::Recv,
        Step::Interrupt,
        Step::Reply(1),
    ]);
    assert!(summary.established);
    assert_eq!(summary.messages, 1);
    assert_eq!(summary.failures, 0);
}

/// Tests that early read timeouts are retried in a handshake and a receive
/// without disturbing the session or a retained sender.
#[test]
fn test_scripted_read_timeouts() {
    let summary = run_logged(&[
        // A timeout during the handshake is retried
        Step::Handshake,
        Step::ReadTimeout,
        Step::Hello,
        // Retain the new sender and send through it
        Step::Retain,
        Step::SendRetained(1),
        // Timeouts during a receive are retried until the reply arrives
        Step::Recv,
        Step::ReadTimeout,
        Step::ReadTimeout,
        Step::Reply(1),
        // The retained sender still works
        Step::SendRetained(2),
    ]);
    assert!(summary.established);
    assert_eq!(summary.handshakes, 1);
    assert_eq!(summary.messages, 1);
    assert_eq!(summary.failures, 0);
}

/// Tests that a timed-out reset or HostHello fails the handshake, and a retry
/// succeeds on the same stream.
#[test]
fn test_scripted_handshake_timeouts() {
    for point in [
        CutPoint::Start,
        CutPoint::Middle(7),
        CutPoint::Delimiter,
        CutPoint::Flush,
    ] {
        let summary = run_logged(&[
            // Time out the reset or HostHello of a first handshake
            Step::Timeout(point),
            Step::Handshake,
            // A retry on the same stream exchanges a message
            Step::Handshake,
            Step::Hello,
            Step::Send(1),
            Step::Recv,
            Step::Reply(1),
        ]);
        assert!(summary.established, "{point:?}");
        assert_eq!(summary.handshakes, 1, "{point:?}");
        assert_eq!(summary.messages, 1, "{point:?}");
        assert_eq!(summary.failures, 1, "{point:?}");
    }
}

/// Tests that a send timeout ends its session and retires retained senders,
/// while a reconnect supplies a working one.
#[test]
fn test_scripted_send_timeouts() {
    for point in [
        CutPoint::Start,
        CutPoint::Middle(7),
        CutPoint::Delimiter,
        CutPoint::Flush,
    ] {
        let summary = run_logged(&[
            // Retain the sender, then time out a send, which ends the session
            Step::Handshake,
            Step::Hello,
            Step::Retain,
            Step::Timeout(point),
            Step::Send(1),
            // The retained sender stays refused, even after a reconnect
            // terminates any interrupted frame
            Step::SendRetained(2),
            Step::Handshake,
            Step::Hello,
            Step::SendRetained(3),
            // The new session sends and receives
            Step::Send(4),
            Step::Recv,
            Step::Reply(4),
        ]);
        assert!(summary.established, "{point:?}");
        assert_eq!(summary.handshakes, 2, "{point:?}");
        assert_eq!(summary.messages, 1, "{point:?}");
        assert_eq!(summary.failures, 0, "{point:?}");
    }
}

/// Tests that a handshake drains an oversized frame, while a session ends on
/// one along with its senders.
#[test]
fn test_scripted_oversized_frames() {
    let summary = run_logged(&[
        // A handshake drains an oversized frame
        Step::Handshake,
        Step::Oversized,
        Step::Hello,
        // In a session, one ends it before a queued reply is read
        Step::Retain,
        Step::Send(1),
        Step::Oversized,
        Step::Reply(1),
        Step::Recv,
        // The retained sender is refused, and receiving fails before reading
        Step::SendRetained(2),
        Step::Recv,
        // Only a new handshake supplies a working sender
        Step::Handshake,
        Step::Hello,
        Step::SendRetained(3),
        Step::Send(4),
        Step::Reply(4),
        Step::Recv,
    ]);
    assert!(summary.established);
    assert_eq!(summary.handshakes, 2);
    assert_eq!(summary.messages, 1);
    assert_eq!(summary.failures, 2);
}

/// Tests that oversized input after a partial hello is discarded through its
/// delimiter, with batched and chunked reads.
#[test]
fn test_scripted_oversized_fragments() {
    for chunk in [0, 255] {
        let summary = run_logged(&[
            // A handshake discards a partial hello grown oversized
            Step::Chunk(chunk),
            Step::Handshake,
            Step::Batch(3),
            Step::Partial,
            Step::Oversized,
            Step::Hello,
            // In a session, the same input ends it
            Step::Retain,
            Step::Partial,
            Step::Oversized,
            Step::Recv,
            // Later sends and receives fail at once
            Step::SendRetained(1),
            Step::Recv,
            // A reconnect drains the rest and a queued signal, and only its
            // sender works
            Step::Dropped,
            Step::Handshake,
            Step::Hello,
            Step::SendRetained(2),
            Step::Send(3),
            Step::Reply(3),
            Step::Recv,
        ]);
        assert!(summary.established, "chunk {chunk}");
        assert_eq!(summary.handshakes, 2, "chunk {chunk}");
        assert_eq!(summary.failures, 2, "chunk {chunk}");
        assert_eq!(summary.resets, 0, "chunk {chunk}");
        assert_eq!(summary.messages, 1, "chunk {chunk}");
    }
}

/// Tests that a handshake drains an oversized frame arriving one byte per read
/// after a partial hello.
///
/// The driver and vector replay must step through these reads without copying
/// the unread frame on every byte.
#[test]
fn test_scripted_bytewise_oversized_frame() {
    let summary = run_logged(&[
        Step::Chunk(1),
        Step::Handshake,
        Step::Batch(3),
        Step::Partial,
        Step::Oversized,
        Step::Hello,
    ]);
    assert!(summary.established);
    assert_eq!(summary.handshakes, 1);
    assert_eq!(summary.failures, 0);
    assert!(summary.reads > MAX_FRAME_SIZE);
}

/// Tests that a handshake drains an oversized stale frame and a long backlog
/// after it before its own ArkHello.
#[test]
fn test_scripted_oversized_stale_frames() {
    for stale in [40, MAX_STEPS - 4] {
        // Batch the oversized frame and the backlog ahead of the hello
        let mut steps = vec![Step::Handshake, Step::Batch(255), Step::Oversized];
        steps.extend(std::iter::repeat_n(Step::Dropped, stale));
        steps.push(Step::Hello);
        let summary = run_logged(&steps);
        assert!(summary.established, "stale {stale}");
        assert_eq!(summary.handshakes, 1, "stale {stale}");
        assert_eq!(summary.failures, 0, "stale {stale}");
    }
}

/// Tests that steps before the first client call either do nothing or queue
/// junk a later handshake drains.
#[test]
fn test_scripted_noops() {
    let summary = run_logged(&[
        // Read faults and unavailable replays or truncations do nothing
        Step::Yield,
        Step::Interrupt,
        Step::ReplyReplay,
        Step::Truncated(3),
        // Responses without a session or HostHello become junk
        Step::Reply(1),
        Step::Hello,
        // A handshake drains that junk
        Step::Handshake,
        Step::Hello,
    ]);
    assert!(summary.established);
    assert_eq!(summary.handshakes, 1);
}

/// Tests that a reset failing on broken writes leaves a middle cut armed for
/// the next HostHello.
///
/// A middle cut needs at least three bytes, so the model must not spend it on
/// the two-byte reset.
#[test]
fn test_scripted_cut_survives_broken_handshake() {
    let summary = run_logged(&[
        // Break writes with a middle cut armed, failing the reset
        Step::Cut {
            point: CutPoint::Middle(100),
            then_broken: true,
        },
        Step::Handshake,
        // Once healed, the cut fails the next HostHello
        Step::Heal,
        Step::Handshake,
    ]);
    assert_eq!(summary.handshakes, 0);
    assert_eq!(summary.failures, 2);
}