rtci-ice 0.2.0

Sans-I/O ICE agent (RFC 8445) with trickle ICE, ICE-TCP and consent freshness, for WebRTC
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
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
//! Rows of the RFC 8445, RFC 8838, RFC 6544, RFC 7675 and RFC 6762 audits that were open:
//! two lite agents, Ta agreed with the peer, IPv6 link-local pairing, what the transport
//! reports (ICMP, TCP connections), candidates after the nomination, consent over TCP, and
//! the mDNS options. Read from the wire in virtual time, as the other agent tests.

use std::sync::Arc;
use std::time::{Duration, Instant};

use sansio::Protocol;
use stun::attributes::ATTR_USERNAME;

use super::agent_rfc8445_test::*;
use super::agent_selector::{MAX_TCP_CONNECTION_ATTEMPTS_PER_ADDRESS, TransportError};
use super::*;
use crate::candidate::candidate_host::CandidateHostConfig;

const MS: Duration = Duration::from_millis(1);

fn candidate(
    network: &str,
    address: &str,
    port: u16,
    priority: u32,
    tcp_type: TcpType,
) -> Candidate {
    CandidateHostConfig {
        base_config: CandidateConfig {
            network: network.to_owned(),
            address: address.to_owned(),
            port,
            component: 1,
            priority,
            ..Default::default()
        },
        tcp_type,
    }
    .new_candidate_host()
    .unwrap()
}

fn udp(address: &str, port: u16, priority: u32) -> Candidate {
    candidate("udp", address, port, priority, TcpType::Unspecified)
}

fn path(local: &str, remote: &str, transport_protocol: TransportProtocol) -> TransportContext {
    TransportContext {
        local_addr: addr(local),
        peer_addr: addr(remote),
        ecn: None,
        transport_protocol,
    }
}

fn events(a: &mut Agent) -> Vec<Event> {
    std::iter::from_fn(|| a.poll_event())
        .map(|e| e.event)
        .collect()
}

fn selected(a: &Agent) -> Option<SocketAddr> {
    a.get_selected_candidate_pair().map(|(_, r)| r.addr())
}

// --- RFC 8445 Sections 6.1.1, 6.2, 8.2: two lite agents ---

fn lite_agent(now: Instant, controlling: bool, remotes: &[Candidate]) -> Agent {
    let cfg = AgentConfig {
        lite: true,
        candidate_types: vec![CandidateType::Host],
        ..config(controlling)
    };
    let mut a = bare_agent(now, cfg);
    a.set_remote_lite(true);
    for remote in remotes {
        a.add_remote_candidate(remote.clone()).unwrap();
    }
    a.start_connectivity_checks(
        now,
        controlling,
        REMOTE_UFRAG.to_owned(),
        REMOTE_PWD.to_owned(),
    )
    .unwrap();
    a
}

/// Section 6.1.1, both lite: "no connectivity checks are ever sent. Rather, once the
/// candidates are exchanged, each agent performs the processing described in Section 8
/// without connectivity checks." Section 8.2: "if there is only one candidate pair, that pair
/// is added to the valid list. [...] If all of the components for all data streams had one
/// pair, the state of ICE processing is Completed." Either role; the only STUN either sends
/// is the Binding indication that keeps the path alive (Section 11).
#[test]
fn test_two_lite_agents_select_the_one_pair_without_checks() {
    for controlling in [true, false] {
        let now = Instant::now();
        let mut a = lite_agent(now, controlling, &[udp("192.168.0.3", 2000, 0)]);
        a.set_local_gathering_complete();
        a.set_remote_end_of_candidates();
        let out = run(&mut a, now, now + Duration::from_secs(60));
        assert!(
            requests(&out).is_empty(),
            "a lite agent sent a Binding request (controlling: {controlling})"
        );
        assert!(!out.is_empty(), "keepalives");
        assert!(
            out.iter()
                .all(|(m, to)| m.typ.class == CLASS_INDICATION && *to == addr(REMOTE_A))
        );
        assert_eq!(selected(&a), Some(addr(REMOTE_A)));
        assert_eq!(a.media_path().unwrap().peer_addr, addr(REMOTE_A));
        // No Binding request ever arrives either: the state does not decay.
        assert_eq!(a.state(), ConnectionState::Completed);
        assert!(
            !events(&mut a)
                .iter()
                .any(|e| matches!(e, Event::LiteCandidateUpdate(..))),
            "one pair needs no updated candidate exchange"
        );
    }
}

/// Section 8.2, more than one pair: "it is RECOMMENDED that an agent follow the procedures of
/// RFC 6724 to select a pair", and "the controlling agent MUST send an updated candidate list
/// to reconcile different agents selecting different candidate pairs". The destination on the
/// local candidate's own network wins over one elsewhere (longest matching prefix), whatever
/// the candidate priorities say; the controlling agent asks for the updated exchange, once;
/// the controlled agent takes the update and uses the pair it names.
#[test]
fn test_two_lite_agents_with_several_pairs_select_by_rfc6724_and_update() {
    let now = Instant::now();
    let elsewhere = udp("10.0.0.1", 5000, 3000);
    let same_network = udp("192.168.0.9", 5000, 1000);
    let mut a = lite_agent(now, true, &[elsewhere.clone(), same_network.clone()]);
    let out = run(&mut a, now, now + Duration::from_secs(5));
    assert!(requests(&out).is_empty());
    assert_eq!(selected(&a), Some(addr("192.168.0.9:5000")));
    let updates: Vec<(SocketAddr, SocketAddr)> = events(&mut a)
        .into_iter()
        .filter_map(|e| match e {
            Event::LiteCandidateUpdate(local, remote) => Some((local.addr(), remote.addr())),
            _ => None,
        })
        .collect();
    assert_eq!(updates, vec![(addr(LOCAL), addr("192.168.0.9:5000"))]);
    run(
        &mut a,
        now + Duration::from_secs(5),
        now + Duration::from_secs(30),
    );
    assert!(events(&mut a).is_empty(), "the update is asked for once");

    // The controlled agent chose by the same rule; the controlling agent's update names
    // another candidate, and that is the pair from then on.
    let mut b = lite_agent(now, false, &[elsewhere.clone(), same_network]);
    run(&mut b, now, now + Duration::from_secs(1));
    assert_eq!(selected(&b), Some(addr("192.168.0.9:5000")));
    assert!(
        events(&mut b)
            .iter()
            .all(|e| !matches!(e, Event::LiteCandidateUpdate(..)))
    );
    let later = now + Duration::from_secs(1);
    assert!(b.replace_remote_candidates(later, vec![elsewhere]));
    run(&mut b, later, later + Duration::from_secs(1));
    assert_eq!(selected(&b), Some(addr("10.0.0.1:5000")));
    assert_eq!(b.get_remote_candidates().len(), 1);

    // It is the procedure of two lite agents only.
    let mut full = started_agent(now, config(true), &[REMOTE_A]);
    assert!(!full.replace_remote_candidates(now, vec![]));
    assert_eq!(full.get_remote_candidates().len(), 1);
}

/// RFC 8839 Section 4.4.2.2: a lite answerer offered `a=remote-candidates` forms its pair
/// with the local candidate the attribute names, even where the RFC 6724 preference would
/// take another local candidate (here the one on the remote's own network).
#[test]
fn test_a_lite_answerer_takes_the_local_candidate_remote_candidates_names() {
    let now = Instant::now();
    let remote = udp("192.168.0.9", 5000, 1000);
    let mut b = lite_agent(now, false, std::slice::from_ref(&remote));
    b.add_local_candidate(udp("10.0.0.2", 1000, 500)).unwrap();
    run(&mut b, now, now + Duration::from_secs(1));
    let local = |a: &Agent| a.get_selected_candidate_pair().map(|(l, _)| l.addr());
    assert_eq!(
        local(&b),
        Some(addr(LOCAL)),
        "the preference: the same network"
    );

    let later = now + Duration::from_secs(1);
    assert!(b.replace_remote_candidates_pinning(
        later,
        vec![remote.clone()],
        Some(addr("10.0.0.2:1000"))
    ));
    run(&mut b, later, later + Duration::from_secs(1));
    assert_eq!(
        local(&b),
        Some(addr("10.0.0.2:1000")),
        "the named local candidate"
    );
    assert_eq!(selected(&b), Some(addr("192.168.0.9:5000")));

    // Without a name, the preference decides again.
    let later = later + Duration::from_secs(1);
    assert!(b.replace_remote_candidates(later, vec![remote]));
    run(&mut b, later, later + Duration::from_secs(1));
    assert_eq!(local(&b), Some(addr(LOCAL)));
}

/// A lite agent whose peer is full still waits for the nomination (Section 8.2): the pair is
/// not selected from the candidate exchange.
#[test]
fn test_lite_agent_with_a_full_peer_waits_for_the_nomination() {
    let now = Instant::now();
    let cfg = AgentConfig {
        lite: true,
        candidate_types: vec![CandidateType::Host],
        ..config(false)
    };
    let mut a = started_agent(now, cfg, &[REMOTE_A]);
    run(&mut a, now, now + Duration::from_secs(2));
    assert_eq!(selected(&a), None);
}

// --- Section 14.2: Ta is agreed with the peer ---

/// "If an agent wants to use a Ta value other than the default value, the agent MUST indicate
/// the proposed value to its peer [...] Both agents MUST use the higher out of the two
/// proposed values."
#[test]
fn test_ta_is_the_higher_of_the_two_proposals() {
    let now = Instant::now();
    // Nothing configured: nothing to propose, and 50 ms.
    let mut a = started_agent(now, config(true), &[REMOTE_A]);
    assert_eq!(a.proposed_pacing(), None);
    assert_eq!(a.pacing(), 50 * MS);
    // The peer proposes more: its value.
    a.set_remote_pacing(Some(120 * MS));
    assert_eq!(a.pacing(), 120 * MS);
    // The peer proposes less than the default this agent uses: the default stays.
    a.set_remote_pacing(Some(10 * MS));
    assert_eq!(a.pacing(), 50 * MS);

    // 20 ms configured: proposed, and used only if the peer proposes no more.
    let cfg = AgentConfig {
        ta: Some(20 * MS),
        ..config(true)
    };
    let mut a = started_agent(now, cfg, &[REMOTE_A]);
    assert_eq!(a.proposed_pacing(), Some(20 * MS));
    a.set_remote_pacing(None);
    assert_eq!(
        a.pacing(),
        50 * MS,
        "a peer that proposes nothing uses 50 ms"
    );
    a.set_remote_pacing(Some(30 * MS));
    assert_eq!(a.pacing(), 30 * MS);
    a.set_remote_pacing(Some(20 * MS));
    assert_eq!(a.pacing(), 20 * MS);
    a.set_remote_pacing(Some(MS));
    assert_eq!(a.pacing(), 20 * MS);

    // The default written out is no proposal; under 5 ms is 5 ms (Section 14.2).
    let cfg = AgentConfig {
        ta: Some(50 * MS),
        ..config(true)
    };
    assert_eq!(started_agent(now, cfg, &[]).proposed_pacing(), None);
    let cfg = AgentConfig {
        ta: Some(MS),
        ..config(true)
    };
    assert_eq!(started_agent(now, cfg, &[]).proposed_pacing(), Some(5 * MS));
}

/// The agreed Ta is the one the checks are paced by.
#[test]
fn test_checks_are_paced_by_the_agreed_ta() {
    let now = Instant::now();
    let mut a = bare_agent(now, config(true));
    for n in 1..=3 {
        a.add_remote_candidate(udp(&format!("192.168.1.{n}"), 5000, 1000 - n))
            .unwrap();
    }
    a.set_remote_pacing(Some(100 * MS));
    a.start_connectivity_checks(now, true, REMOTE_UFRAG.to_owned(), REMOTE_PWD.to_owned())
        .unwrap();
    let mut started = vec![];
    let mut at = now;
    while at <= now + 450 * MS {
        a.handle_timeout(at).unwrap();
        while let Some(t) = a.poll_write() {
            started.push(t.now.duration_since(now));
        }
        at = match a.poll_timeout() {
            Some(next) => next.max(at + MS),
            None => break,
        };
    }
    assert_eq!(started, vec![Duration::ZERO, 100 * MS, 200 * MS]);
}

// --- Section 6.1.2.2: IPv6 link-local addresses ---

/// "IPv6 link-local addresses MUST NOT be paired with other than link-local addresses", in
/// either direction, and no setting lifts it.
#[test]
fn test_ipv6_link_local_pairs_only_with_link_local() {
    let now = Instant::now();
    let cfg = AgentConfig {
        allow_link_local_remote_candidates: true,
        ..config(true)
    };
    let mut a = Agent::new(now, Arc::new(cfg), provider()).unwrap();
    a.add_local_candidate(udp("fe80::1", 1000, 0)).unwrap();
    a.add_local_candidate(udp("2001:db8::1", 1000, 0)).unwrap();
    a.add_remote_candidate(udp("fe80::2", 2000, 0)).unwrap();
    a.add_remote_candidate(udp("2001:db8::2", 2000, 0)).unwrap();
    let mut pairs: Vec<(String, String)> = a
        .candidate_pairs
        .iter()
        .map(|p| {
            (
                a.local_candidates[p.local_index].addr().ip().to_string(),
                a.remote_candidates[p.remote_index].addr().ip().to_string(),
            )
        })
        .collect();
    pairs.sort();
    assert_eq!(
        pairs,
        vec![
            ("2001:db8::1".to_owned(), "2001:db8::2".to_owned()),
            ("fe80::1".to_owned(), "fe80::2".to_owned()),
        ]
    );
}

// --- Section 7.2.5.2.2 and RFC 6544 Section 7.1: what the transport reports ---

/// "If the agent supports processing of ICMP errors, and if a Binding request generates a
/// hard ICMP error, the agent SHOULD set the state of the candidate pair to Failed." The
/// check stops being retransmitted. An ICMP error is not authenticated: it fails neither a
/// pair that no check is in flight on nor one a check has validated.
#[test]
fn test_hard_icmp_error_fails_the_pair_whose_check_is_in_flight() {
    let now = Instant::now();
    let cfg = AgentConfig {
        failed_timeout: Some(Duration::ZERO),
        ..config(false)
    };
    let mut a = started_agent(now, cfg, &[REMOTE_A, REMOTE_B]);
    // Only A's check has started (Ta).
    let out = run(&mut a, now, now + 10 * MS);
    assert_eq!(requests(&out).len(), 1);
    let udp_path = |remote: &str| path(LOCAL, remote, TransportProtocol::UDP);
    assert!(
        !a.handle_transport_error(
            now + 10 * MS,
            udp_path(REMOTE_B),
            TransportError::IcmpHardError
        ),
        "no check is in flight on that pair"
    );
    assert!(!a.handle_transport_error(
        now + 10 * MS,
        udp_path("192.168.0.77:1"),
        TransportError::IcmpHardError
    ));
    assert!(a.handle_transport_error(
        now + 10 * MS,
        udp_path(REMOTE_A),
        TransportError::IcmpHardError
    ));
    let pair_to = |a: &Agent, remote: &str| {
        a.candidate_pairs[a
            .find_pair(0, a.find_remote_candidate(addr(remote)).unwrap())
            .unwrap()]
    };
    assert_eq!(pair_to(&a, REMOTE_A).state, CandidatePairState::Failed);
    // B is checked and answers; nothing more goes to A.
    let out = run_answering(
        &mut a,
        now + 10 * MS,
        now + Duration::from_secs(10),
        |_, _| true,
    );
    assert!(requests(&out).iter().all(|(_, to)| *to == addr(REMOTE_B)));
    assert!(pair_to(&a, REMOTE_B).valid);
    // A valid pair is not failed by an ICMP error.
    assert!(!a.handle_transport_error(
        now + Duration::from_secs(10),
        udp_path(REMOTE_B),
        TransportError::IcmpHardError
    ));
    assert_eq!(pair_to(&a, REMOTE_B).state, CandidatePairState::Succeeded);
}

/// A controlling agent with one active TCP candidate and `n` remote passive candidates on one
/// IP address (ports 5001...), each with a foundation of its own: candidates of one type on one
/// address would share a foundation, and RFC 8445 Section 6.1.2.6 then leaves all but the best
/// pair Frozen until it succeeds, so only one connection would ever be attempted.
fn tcp_agent(now: Instant, n: u16) -> Agent {
    let cfg = AgentConfig {
        failed_timeout: Some(Duration::ZERO),
        ..config(true)
    };
    let mut a = Agent::new(now, Arc::new(cfg), provider()).unwrap();
    a.add_local_candidate(candidate("tcp", "192.168.0.2", 9, 0, TcpType::Active))
        .unwrap();
    for i in 1..=n {
        a.add_remote_candidate(
            CandidateHostConfig {
                base_config: CandidateConfig {
                    network: "tcp".to_owned(),
                    address: "192.168.0.3".to_owned(),
                    port: 5000 + i,
                    component: 1,
                    priority: 10_000 - u32::from(i),
                    foundation: format!("t{i}"),
                    ..Default::default()
                },
                tcp_type: TcpType::Passive,
            }
            .new_candidate_host()
            .unwrap(),
        )
        .unwrap();
    }
    a.start_connectivity_checks(now, true, REMOTE_UFRAG.to_owned(), REMOTE_PWD.to_owned())
        .unwrap();
    a
}

fn tcp_path(remote_port: u16) -> TransportContext {
    path(
        "192.168.0.2:40000",
        &format!("192.168.0.3:{remote_port}"),
        TransportProtocol::TCP,
    )
}

fn tcp_state(a: &Agent, remote_port: u16) -> CandidatePairState {
    let remote = addr(&format!("192.168.0.3:{remote_port}"));
    a.candidate_pairs[a
        .find_pair(0, a.find_remote_candidate(remote).unwrap())
        .unwrap()]
    .state
}

/// RFC 6544 Section 12: "an ICE agent SHOULD NOT have more than 5 outstanding TCP connection
/// attempts with the same peer to the same IP address." Section 7.1: "If the TCP connection
/// cannot be established, the check is considered to have failed, and a full-mode agent MUST
/// update the pair state to Failed": the transport says so, the pair fails at once (not 39.5
/// seconds later), and the next attempt takes its place.
#[test]
fn test_tcp_connection_attempts_are_limited_and_a_failed_connection_fails_the_pair() {
    let now = Instant::now();
    let mut a = tcp_agent(now, 8);
    let out = run(&mut a, now, now + Duration::from_secs(2));
    let attempted: Vec<u16> = requests(&out).iter().map(|(_, to)| to.port()).collect();
    assert_eq!(attempted, vec![5001, 5002, 5003, 5004, 5005]);
    assert_eq!(attempted.len(), MAX_TCP_CONNECTION_ATTEMPTS_PER_ADDRESS);
    assert_eq!(tcp_state(&a, 5006), CandidatePairState::Waiting);

    let later = now + Duration::from_secs(2);
    assert!(a.handle_transport_error(later, tcp_path(5002), TransportError::ConnectionFailed));
    assert_eq!(tcp_state(&a, 5002), CandidatePairState::Failed);
    let out = run(&mut a, later, later + Duration::from_secs(2));
    let attempted: Vec<u16> = requests(&out).iter().map(|(_, to)| to.port()).collect();
    assert_eq!(attempted, vec![5006], "one attempt ended, one more starts");

    // A connection that fails before its check started fails its pair as well.
    let later = later + Duration::from_secs(2);
    assert_eq!(tcp_state(&a, 5008), CandidatePairState::Waiting);
    assert!(a.handle_transport_error(later, tcp_path(5008), TransportError::ConnectionFailed));
    assert_eq!(tcp_state(&a, 5008), CandidatePairState::Failed);
    // And it is a report about TCP: the same call for a UDP path changes nothing.
    assert!(!a.handle_transport_error(
        later,
        path(
            "192.168.0.2:40000",
            "192.168.0.3:5007",
            TransportProtocol::UDP
        ),
        TransportError::ConnectionFailed
    ));
}

/// RFC 6544 Section 7.1: "If the response to the first connectivity check on the established
/// TCP connection is something other than a STUN message, the remote candidate address
/// apparently was not one of the peer's addresses, and the agent SHOULD close the connection
/// and consider all pairs with that remote candidate as failed."
#[test]
fn test_first_answer_that_is_not_stun_fails_the_pairs_of_that_remote_candidate() {
    let now = Instant::now();
    let mut a = tcp_agent(now, 2);
    run(&mut a, now, now + 10 * MS);
    assert_eq!(tcp_state(&a, 5001), CandidatePairState::InProgress);
    // No check is in flight to 5002 yet: nothing is concluded about it.
    assert!(!a.handle_transport_error(now + 10 * MS, tcp_path(5002), TransportError::NotStun));
    assert!(a.handle_transport_error(now + 10 * MS, tcp_path(5001), TransportError::NotStun));
    assert_eq!(tcp_state(&a, 5001), CandidatePairState::Failed);
    assert_ne!(tcp_state(&a, 5002), CandidatePairState::Failed);
    assert!(a.pending_binding_requests.is_empty());
}

/// The same, as the peer connection reports it (`Agent::handle_non_stun_before_validation`):
/// non-STUN data on a TCP connection this agent opened, from its active candidate, while the
/// check is in flight. Over UDP it concludes nothing.
#[test]
fn test_non_stun_data_on_a_connection_this_agent_opened_fails_the_pairs() {
    let now = Instant::now();
    let mut a = tcp_agent(now, 2);
    run(&mut a, now, now + 10 * MS);
    assert_eq!(tcp_state(&a, 5001), CandidatePairState::InProgress);
    let mut udp = tcp_path(5001);
    udp.transport_protocol = TransportProtocol::UDP;
    assert!(!a.handle_non_stun_before_validation(now + 10 * MS, udp));
    assert_eq!(tcp_state(&a, 5001), CandidatePairState::InProgress);
    assert!(a.handle_non_stun_before_validation(now + 10 * MS, tcp_path(5001)));
    assert_eq!(tcp_state(&a, 5001), CandidatePairState::Failed);
}

// --- RFC 6544 Sections 4.1, 4.2, 4.5: relayed TCP candidates (RFC 6062 allocations) ---

const RELAYED: &str = "203.0.113.5";

/// A relayed TCP candidate of a TURN TCP allocation (RFC 6062) at `RELAYED:port`.
fn relayed_tcp(port: u16, tcp_type: TcpType) -> Candidate {
    let mut c = crate::candidate::candidate_relay::CandidateRelayConfig {
        base_config: CandidateConfig {
            network: "tcp".to_owned(),
            address: RELAYED.to_owned(),
            port,
            component: 1,
            ..Default::default()
        },
        rel_addr: "192.168.0.2".to_owned(),
        rel_port: 40000,
        url: None,
    }
    .new_candidate_relay()
    .unwrap();
    c.set_tcp_type(tcp_type);
    c
}

/// RFC 6544 Section 4.1: "the agent SHOULD obtain [...] passive relayed candidates [...] it
/// SHOULD also include an active candidate" (of the relayed address, port 9: Section 4.5); the
/// application holds the TURN TCP allocation (`rtci_turn` `Relay::connect`, `DataConnection`)
/// and hands the agent the relayed candidates. Section 6.2: they pair by `tcptype` like host
/// ones. The active relayed candidate's check goes to the peer's passive candidate on the
/// relayed path (the application opens it with a Connect and a data connection); a check from
/// the peer's active side that arrives on the passive relayed candidate (an accepted
/// ConnectionAttempt's data connection) is answered on that path.
#[test]
fn test_relayed_tcp_candidates_pair_by_tcptype_and_check_on_the_relayed_path() {
    let now = Instant::now();
    let cfg = AgentConfig {
        failed_timeout: Some(Duration::ZERO),
        ..config(true)
    };
    let mut a = Agent::new(now, Arc::new(cfg), provider()).unwrap();
    let passive = relayed_tcp(5000, TcpType::Passive);
    let active = relayed_tcp(9, TcpType::Active);
    // Section 4.2: direction-pref 6 (active) and 4 (passive) for relayed candidates, as for
    // host ones.
    assert!(active.priority() > passive.priority());
    a.add_local_candidate(passive).unwrap();
    a.add_local_candidate(active).unwrap();
    a.add_remote_candidate(candidate("tcp", "192.168.0.3", 6000, 0, TcpType::Passive))
        .unwrap();
    // A signalled active candidate (port 9) is reached as the peer-reflexive candidate its
    // checks make, as for host candidates.
    assert!(
        !a.add_remote_candidate(candidate("tcp", "192.168.0.3", 9, 0, TcpType::Active))
            .unwrap()
    );
    a.start_connectivity_checks(now, true, REMOTE_UFRAG.to_owned(), REMOTE_PWD.to_owned())
        .unwrap();

    let local = |port: u16| {
        a.find_local_candidate(addr(&format!("{RELAYED}:{port}")), TransportProtocol::TCP)
    };
    let remote =
        |a: &Agent, port: u16| a.find_remote_candidate(addr(&format!("192.168.0.3:{port}")));
    let (lp, la) = (local(5000).unwrap(), local(9).unwrap());
    let rp = remote(&a, 6000).unwrap();
    assert!(
        a.find_pair(la, rp).is_some(),
        "active relayed with remote passive"
    );
    assert!(a.find_pair(lp, rp).is_none(), "passive with passive");

    // The active relayed candidate's check: to the peer's passive address, from the relayed
    // address, over TCP.
    a.handle_timeout(now + 10 * MS).unwrap();
    let out: Vec<TaggedBytesMut> = std::iter::from_fn(|| a.poll_write()).collect();
    let check = out
        .iter()
        .find(|t| t.transport.peer_addr == addr("192.168.0.3:6000"))
        .expect("a check to the passive remote candidate");
    assert_eq!(check.transport.transport_protocol, TransportProtocol::TCP);
    assert_eq!(
        check.transport.local_addr.ip(),
        addr(&format!("{RELAYED}:9")).ip()
    );
    assert!(
        out.iter()
            .all(|t| t.transport.peer_addr != addr("192.168.0.3:9")),
        "nothing goes to an active candidate's port 9"
    );

    // The peer's check over a data connection the TURN server accepted for the passive
    // relayed candidate: from the peer's ephemeral port, answered on the same path.
    let request = peer_request(
        &a,
        vec![
            Box::new(crate::attributes::control::AttrControlled(1)),
            Box::new(crate::attributes::priority::PriorityAttr(1)),
        ],
    );
    a.handle_read(TaggedBytesMut {
        now: now + 20 * MS,
        transport: TransportContext {
            local_addr: addr(&format!("{RELAYED}:5000")),
            peer_addr: addr("192.168.0.3:51000"),
            ecn: None,
            transport_protocol: TransportProtocol::TCP,
        },
        message: BytesMut::from(&request.raw[..]),
    })
    .unwrap();
    let answers: Vec<TaggedBytesMut> = std::iter::from_fn(|| a.poll_write()).collect();
    let answer = answers
        .iter()
        .find(|t| t.transport.peer_addr == addr("192.168.0.3:51000"))
        .expect("the check is answered");
    assert_eq!(
        answer.transport.local_addr,
        addr(&format!("{RELAYED}:5000"))
    );
    assert_eq!(answer.transport.transport_protocol, TransportProtocol::TCP);
    let mut m = Message {
        raw: answer.message.to_vec(),
        ..Message::default()
    };
    m.decode().unwrap();
    assert_eq!(m.typ, BINDING_SUCCESS);
    // The peer's active side is now a peer-reflexive candidate, paired with the passive
    // relayed one.
    let learned = remote(&a, 51000).expect("a peer-reflexive candidate for the peer's connection");
    assert_eq!(a.remote_candidates[learned].tcp_type(), TcpType::Active);
    assert!(
        a.find_pair(lp, learned).is_some(),
        "passive relayed with the peer's active side"
    );
}

/// RFC 6544 Section 4.2: for server-reflexive candidates the direction-pref order is
/// simultaneous-open (6), then active (4), then passive (2): an active or S-O srflx candidate
/// can work through a NAT that a passive one cannot.
#[test]
fn test_server_reflexive_tcp_direction_preferences() {
    let srflx = |tcp_type: TcpType| {
        let mut c = crate::candidate::candidate_server_reflexive::CandidateServerReflexiveConfig {
            base_config: CandidateConfig {
                network: "tcp".to_owned(),
                address: "198.51.100.7".to_owned(),
                port: 7000,
                component: 1,
                ..Default::default()
            },
            rel_addr: "192.168.0.2".to_owned(),
            rel_port: 40000,
            url: None,
        }
        .new_candidate_server_reflexive()
        .unwrap();
        c.set_tcp_type(tcp_type);
        c.priority()
    };
    let (so, active, passive) = (
        srflx(TcpType::SimultaneousOpen),
        srflx(TcpType::Active),
        srflx(TcpType::Passive),
    );
    assert!(so > active && active > passive, "{so} {active} {passive}");
}

// --- RFC 8838 Section 13: no candidate after the nomination ---

/// "In all cases, an agent MUST NOT trickle any new candidates within an ICE session after
/// nomination of a candidate pair": a local candidate gathered after it is not taken (and so
/// not announced); an ICE restart takes candidates again.
#[test]
fn test_local_candidates_are_refused_after_the_nomination() {
    let now = Instant::now();
    let mut a = started_agent(now, config(true), &[REMOTE_A]);
    assert!(a.add_local_candidate(udp("192.168.0.20", 1000, 0)).unwrap());
    run_answering(&mut a, now, now + Duration::from_secs(1), |_, _| true);
    assert!(a.get_selected_candidate_pair().is_some());
    let before = a.get_local_candidates().len();
    assert!(!a.add_local_candidate(udp("192.168.0.21", 1000, 0)).unwrap());
    assert_eq!(a.get_local_candidates().len(), before);

    a.restart(
        now + Duration::from_secs(2),
        String::new(),
        String::new(),
        true,
    )
    .unwrap();
    assert!(a.add_local_candidate(udp("192.168.0.21", 1000, 0)).unwrap());
}

/// The same rule binds the session the nomination was made in, not the one a staged restart
/// begins: a candidate gathered while the restart is staged is taken, but not paired with the
/// current session's remote candidates, and the restart keeps it even when it discards the
/// previous generation's local candidates. A candidate held already that is gathered again in
/// that window is kept too; one that is not is discarded.
#[test]
fn test_a_candidate_gathered_for_a_staged_restart_is_kept_and_not_paired_before_it() {
    let now = Instant::now();
    let mut a = started_agent(now, config(true), &[REMOTE_A]);
    assert!(a.add_local_candidate(udp("192.168.0.20", 1000, 0)).unwrap());
    assert!(a.add_local_candidate(udp("192.168.0.22", 1000, 0)).unwrap());
    run_answering(&mut a, now, now + Duration::from_secs(1), |_, _| true);
    assert!(a.get_selected_candidate_pair().is_some());
    let pairs = a.get_candidate_pairs().len();

    a.generate_restart_credentials(String::new(), String::new())
        .unwrap();
    assert!(a.add_local_candidate(udp("192.168.0.21", 1000, 0)).unwrap());
    assert!(!a.add_local_candidate(udp("192.168.0.20", 1000, 0)).unwrap());
    assert_eq!(
        a.get_candidate_pairs().len(),
        pairs,
        "not paired in the session that nominated"
    );
    let staged: Vec<String> = a
        .get_restart_generation_local_candidates()
        .iter()
        .map(|c| c.address().to_owned())
        .collect();
    assert_eq!(staged, ["192.168.0.20", "192.168.0.21"]);

    a.apply_restart(now + Duration::from_secs(2), false)
        .unwrap();
    let kept: Vec<String> = a
        .get_local_candidates()
        .iter()
        .map(|c| c.address().to_owned())
        .collect();
    assert_eq!(kept, ["192.168.0.20", "192.168.0.21"]);
    assert!(a.get_restart_generation_local_candidates().is_empty());
}

// --- RFC 7675 Section 5.1 ---

/// "Consent checks run over every transport": on a TCP pair they are Binding requests on the
/// connection, 4 to 6 seconds apart, each a transaction of its own sent once.
#[test]
fn test_consent_checks_run_on_a_tcp_pair() {
    let now = Instant::now();
    let mut a = tcp_agent(now, 1);
    let answer = |a: &mut Agent, from: Instant, until: Instant| {
        let mut out = vec![];
        let mut at = from;
        loop {
            a.handle_timeout(at).unwrap();
            while let Some(t) = a.poll_write() {
                assert_eq!(t.transport.transport_protocol, TransportProtocol::TCP);
                let mut m = Message {
                    raw: t.message.to_vec(),
                    ..Message::default()
                };
                m.decode().unwrap();
                let response = peer_response(
                    BINDING_SUCCESS,
                    m.transaction_id,
                    addr("192.168.0.2:40000"),
                    None,
                );
                a.handle_read(TaggedBytesMut {
                    now: at,
                    transport: tcp_path(5001),
                    message: BytesMut::from(&response.raw[..]),
                })
                .unwrap();
                out.push((t.now, m));
            }
            match a.poll_timeout() {
                Some(next) if next <= until => at = next.max(at + MS),
                _ => return out,
            }
        }
    };
    answer(&mut a, now, now + Duration::from_secs(1));
    assert_eq!(a.state(), ConnectionState::Connected);
    let consent = answer(
        &mut a,
        now + Duration::from_secs(1),
        now + Duration::from_secs(31),
    );
    assert!(
        consent.len() >= 5,
        "{} consent checks in 30 s",
        consent.len()
    );
    let mut ids: Vec<TransactionId> = consent.iter().map(|(_, m)| m.transaction_id).collect();
    ids.dedup();
    assert_eq!(ids.len(), consent.len(), "each one is sent once");
    for pair in consent.windows(2) {
        let gap = pair[1].0.duration_since(pair[0].0);
        assert!(
            (Duration::from_secs(4)..=Duration::from_secs(6)).contains(&gap),
            "{gap:?}"
        );
    }
    assert_eq!(a.state(), ConnectionState::Connected);
}

/// Consent on a TCP pair whose round trip is long (RFC 7675 Section 5.1: "consent checks MUST
/// be performed over all transport connections, including TCP"): each consent request is
/// answered 800 ms later, or 6 s later, more than the initial RTO, and each answer renews
/// consent. The pair is never given up.
#[test]
fn test_consent_is_kept_on_a_tcp_pair_whose_round_trip_is_long() {
    for round_trip in [800 * MS, 6000 * MS] {
        let now = Instant::now();
        let mut a = tcp_agent(now, 1);
        // Responses on their way: when each arrives.
        let mut in_flight: Vec<(Instant, Message)> = vec![];
        let mut states = vec![];
        let mut consent_requests = 0;
        let until = now + Duration::from_secs(120);
        let mut at = now;
        loop {
            in_flight.retain(|(arrival, response)| {
                if *arrival > at {
                    return true;
                }
                a.handle_read(TaggedBytesMut {
                    now: at,
                    transport: tcp_path(5001),
                    message: BytesMut::from(&response.raw[..]),
                })
                .unwrap();
                false
            });
            a.handle_timeout(at).unwrap();
            while let Some(t) = a.poll_write() {
                assert_eq!(t.transport.transport_protocol, TransportProtocol::TCP);
                let mut m = Message {
                    raw: t.message.to_vec(),
                    ..Message::default()
                };
                m.decode().unwrap();
                let response = peer_response(
                    BINDING_SUCCESS,
                    m.transaction_id,
                    addr("192.168.0.2:40000"),
                    None,
                );
                // The checks are answered at once; the consent requests after the round
                // trip.
                if a.get_selected_candidate_pair().is_some() {
                    consent_requests += 1;
                    in_flight.push((at + round_trip, response));
                } else {
                    in_flight.push((at, response));
                }
            }
            states.extend(events(&mut a).into_iter().filter_map(|e| match e {
                Event::ConnectionStateChange(s) => Some(s),
                _ => None,
            }));
            let arrival = in_flight.iter().map(|(arrival, _)| *arrival).min();
            let next = match (a.poll_timeout(), arrival) {
                (Some(timer), Some(arrival)) => Some(timer.min(arrival)),
                (timer, arrival) => timer.or(arrival),
            };
            match next {
                Some(next) if next <= until => at = next.max(at + Duration::from_micros(1)),
                _ => break,
            }
        }
        assert!(
            consent_requests >= 15,
            "{consent_requests} consent requests"
        );
        assert!(
            !states.contains(&ConnectionState::Failed),
            "{round_trip:?}: {states:?}"
        );
        if round_trip < Duration::from_secs(5) {
            assert!(
                !states.contains(&ConnectionState::Disconnected),
                "{round_trip:?}: {states:?}"
            );
        }
        let (_, remote) = a.get_selected_candidate_pair().unwrap();
        assert!(
            until - remote.last_consent().unwrap() < Duration::from_secs(7),
            "{round_trip:?}"
        );
    }
}

/// "After consent is lost, the same ICE credentials MUST NOT be used on the affected 5-tuple
/// again. That means that a new session, or an ICE restart, is needed": the agent that lost
/// consent is `Failed` and silent, and what a restart sends on the same 5-tuple carries
/// another username, also when the application pinned its credentials.
#[test]
fn test_after_consent_is_lost_the_same_credentials_are_not_used_again() {
    let now = Instant::now();
    let cfg = AgentConfig {
        local_ufrag: "pinnedufrag".to_owned(),
        local_pwd: "pinnedpasswordpinnedpassword".to_owned(),
        ..config(true)
    };
    let mut a = started_agent(now, cfg, &[REMOTE_A]);
    let out = run_answering(&mut a, now, now + Duration::from_secs(1), |_, _| true);
    let username = |m: &Message| String::from_utf8(m.get(ATTR_USERNAME).unwrap()).unwrap();
    let before = username(&requests(&out)[0].0);
    assert_eq!(before, format!("{REMOTE_UFRAG}:pinnedufrag"));

    // The peer falls silent: consent expires.
    run(
        &mut a,
        now + Duration::from_secs(1),
        now + Duration::from_secs(40),
    );
    assert_eq!(a.state(), ConnectionState::Failed);
    let silent = run(
        &mut a,
        now + Duration::from_secs(40),
        now + Duration::from_secs(80),
    );
    assert!(silent.is_empty(), "nothing is sent without consent");

    // An ICE restart, the application still asking for its pinned credentials.
    let later = now + Duration::from_secs(80);
    a.restart(
        later,
        "pinnedufrag".to_owned(),
        "pinnedpasswordpinnedpassword".to_owned(),
        false,
    )
    .unwrap();
    a.add_local_candidate(host(addr(LOCAL))).unwrap();
    a.add_remote_candidate(host(addr(REMOTE_A))).unwrap();
    a.start_connectivity_checks(later, true, REMOTE_UFRAG.to_owned(), REMOTE_PWD.to_owned())
        .unwrap();
    let out = run(&mut a, later, later + Duration::from_secs(1));
    let after = username(&requests(&out)[0].0);
    assert_ne!(after, before, "the same credentials on the same 5-tuple");
}

// --- mDNS (RFC 6762, draft-ietf-mmusic-mdns-ice-candidates) ---

const MDNS_GROUP: &str = "224.0.0.251:5353";
const NAME: &str = "61b445d2-6503-41ac-96ce-ee3edac00e9f.local";

fn mdns_candidate() -> Candidate {
    unmarshal_candidate(&format!(
        "1114572465 1 udp 2113939711 {NAME} 61163 typ host"
    ))
    .unwrap()
}

/// When the agent's first mDNS datagram leaves, after `from`.
fn first_mdns_write(a: &mut Agent, from: Instant) -> Option<Duration> {
    let mut at = from;
    for _ in 0..1000 {
        a.handle_timeout(at).unwrap();
        while let Some(t) = a.poll_write() {
            if t.transport.peer_addr == addr(MDNS_GROUP) {
                return Some(t.now.duration_since(from));
            }
        }
        at = a.poll_timeout()?.max(at + MS);
        if at > from + Duration::from_secs(2) {
            return None;
        }
    }
    None
}

/// RFC 6762 Section 5.2: "the interval between the first two queries MUST be at least one
/// second [...] a Multicast DNS querier SHOULD also delay the first query of the series by a
/// randomly chosen amount in the range 20-120 ms." It is the agent's default;
/// `multicast_dns_first_query_delay: Some(false)` asks at once.
#[test]
fn test_first_mdns_query_waits_20_to_120_ms_by_default() {
    let now = Instant::now();
    let mut a = Agent::new(now, Arc::new(AgentConfig::default()), provider()).unwrap();
    assert!(!a.add_remote_candidate(mdns_candidate()).unwrap());
    let delay = first_mdns_write(&mut a, now).expect("a query");
    assert!((20 * MS..=120 * MS).contains(&delay), "{delay:?}");
    // The unresolved candidate is submitted, not held.
    assert_eq!(a.pending_mdns_candidates().count(), 1);
    assert!(a.get_remote_candidates().is_empty());

    let cfg = AgentConfig {
        multicast_dns_first_query_delay: Some(false),
        ..AgentConfig::default()
    };
    let mut a = Agent::new(now, Arc::new(cfg), provider()).unwrap();
    a.add_remote_candidate(mdns_candidate()).unwrap();
    assert_eq!(first_mdns_write(&mut a, now), Some(Duration::ZERO));
}

/// draft-ietf-mmusic-mdns-ice-candidates Section 7.1: the rate limit is the
/// implementation's, not one agent's. Two agents given clones of one `MdnsRateLimit` spend one
/// budget: with a budget of one message, the second agent's query waits for the first one's.
#[test]
fn test_agents_share_one_mdns_rate_limit() {
    let now = Instant::now();
    let limit = mdns::MdnsRateLimit::new(1, 1);
    let agent = |limit: Option<mdns::MdnsRateLimit>| {
        let cfg = AgentConfig {
            multicast_dns_first_query_delay: Some(false),
            multicast_dns_rate_limit: limit,
            ..AgentConfig::default()
        };
        let mut a = Agent::new(now, Arc::new(cfg), provider()).unwrap();
        a.add_remote_candidate(mdns_candidate()).unwrap();
        a
    };
    let (mut first, mut second) = (agent(Some(limit.clone())), agent(Some(limit)));
    assert_eq!(first_mdns_write(&mut first, now), Some(Duration::ZERO));
    let waited = first_mdns_write(&mut second, now).expect("the second agent's query");
    assert!(waited >= Duration::from_millis(900), "{waited:?}");

    // With limits of their own neither waits for the other.
    let (mut first, mut second) = (agent(None), agent(None));
    assert_eq!(first_mdns_write(&mut first, now), Some(Duration::ZERO));
    assert_eq!(first_mdns_write(&mut second, now), Some(Duration::ZERO));
}

/// RFC 6762 Section 6.7: a query from a source port other than 5353 is a legacy unicast
/// query and gets a conventional unicast reply. It reaches the mDNS connection because what
/// arrives on the mDNS socket (local port 5353) is mDNS whatever its source port; the reply
/// goes back to that port.
#[test]
fn test_legacy_unicast_query_is_answered_by_unicast() {
    let now = Instant::now();
    let cfg = AgentConfig {
        multicast_dns_mode: MulticastDnsMode::QueryAndGather,
        ..AgentConfig::default()
    };
    let mut a = Agent::new(now, Arc::new(cfg), provider()).unwrap();
    a.add_local_candidate(host(addr(LOCAL))).unwrap();
    let name = a.get_local_candidates()[0].address().to_owned();
    assert!(name.ends_with(".local"));
    // Past the announcements.
    let mut at = now;
    while at < now + Duration::from_secs(5) {
        a.handle_timeout(at).unwrap();
        while a.poll_write().is_some() {}
        at = a
            .poll_timeout()
            .map_or(at + Duration::from_secs(1), |t| t.max(at + MS));
    }

    // A one-question query for the name, as a conventional DNS client sends it.
    let mut query = vec![0x12, 0x34, 0x00, 0x00, 0, 1, 0, 0, 0, 0, 0, 0];
    for label in name.split('.') {
        query.push(u8::try_from(label.len()).unwrap());
        query.extend_from_slice(label.as_bytes());
    }
    query.extend_from_slice(&[0, 0, 1, 0, 1]);
    a.handle_read(TaggedBytesMut {
        now: at,
        transport: path("0.0.0.0:5353", "192.168.0.50:40123", TransportProtocol::UDP),
        message: BytesMut::from(&query[..]),
    })
    .unwrap();
    a.handle_timeout(at).unwrap();
    let replies: Vec<TaggedBytesMut> = std::iter::from_fn(|| a.poll_write()).collect();
    assert_eq!(replies.len(), 1, "one reply");
    assert_eq!(replies[0].transport.peer_addr, addr("192.168.0.50:40123"));
    assert_eq!(&replies[0].message[..2], &[0x12, 0x34], "the query's ID");
    assert_ne!(replies[0].message[2] & 0x80, 0, "a response");
}

/// RFC 6762 Section 9: a host that finds its name in use by another "MUST [...] choose a new
/// name". The agent's probe for the name it was configured with meets a defender: the
/// candidate gets a new `<uuid>.local` name, which the responder answers for, and is reported
/// for the application to convey again.
#[test]
fn test_lost_mdns_name_is_replaced_and_the_candidate_announced_again() {
    use mdns::{Mdns, MdnsConfig, MdnsProbePolicy};

    let now = Instant::now();
    let lost = "ice-agent-host.local";
    let cfg = AgentConfig {
        multicast_dns_mode: MulticastDnsMode::QueryAndGather,
        multicast_dns_local_name: lost.to_owned(),
        multicast_dns_local_ip: Some("192.168.0.2".parse().unwrap()),
        ..AgentConfig::default()
    };
    let mut a = Agent::new(now, Arc::new(cfg), provider()).unwrap();
    a.add_local_candidate(host(addr(LOCAL))).unwrap();
    assert_eq!(a.get_local_candidates()[0].address(), lost);
    let id = a.get_local_candidates()[0].id().to_owned();
    // The first probe for the name goes out.
    let probed = first_mdns_write(&mut a, now).expect("a probe");
    let at = now + probed;

    // Another host owns the name and says so.
    let mut owner = Mdns::new(
        now,
        MdnsConfig::new()
            .with_local_names(vec![lost.to_owned()])
            .with_local_ip("192.168.0.77".parse().unwrap())
            .with_probe_policy(MdnsProbePolicy::Never),
    );
    owner.handle_timeout(at).unwrap();
    let announcement = owner.poll_write().expect("the owner announces its name");
    a.handle_read(TaggedBytesMut {
        now: at,
        transport: path(MDNS_GROUP, "192.168.0.77:5353", TransportProtocol::UDP),
        message: announcement.message,
    })
    .unwrap();

    let renamed: Vec<Candidate> = events(&mut a)
        .into_iter()
        .filter_map(|e| match e {
            Event::LocalCandidateRenamed(c) => Some(*c),
            _ => None,
        })
        .collect();
    assert_eq!(renamed.len(), 1);
    let name = renamed[0].address().to_owned();
    assert_ne!(name, lost);
    assert!(crate::mdns::is_ice_mdns_name(&name), "{name}");
    assert_eq!(renamed[0].id(), id, "the same candidate");
    assert_eq!(a.get_local_candidates()[0].address(), name);
    assert_eq!(
        a.get_local_candidates_to_signal()[0].marshal(),
        renamed[0].marshal()
    );
    assert!(renamed[0].marshal().contains(&name));
    // A candidate added later on that address is published under the new name too.
    a.add_local_candidate(udp("192.168.0.2", 1001, 0)).unwrap();
    assert_eq!(a.get_local_candidates()[1].address(), name);
}

/// RFC 8839 section 5.4: `ice-ufrag` is 4 to 256 `ice-char`s (letters, digits, `+`, `/`) and
/// "MUST NOT be longer than 32 characters when sending"; `ice-pwd` is 22 to 256. Credentials
/// the application supplies outside that are refused, when the agent is made and when it
/// restarts with them; the bounds themselves are taken.
#[test]
fn test_supplied_credentials_are_held_to_the_rfc_8839_grammar() {
    let now = Instant::now();
    let made = |ufrag: &str, pwd: &str| {
        Agent::new(
            now,
            Arc::new(AgentConfig {
                local_ufrag: ufrag.to_owned(),
                local_pwd: pwd.to_owned(),
                ..Default::default()
            }),
            provider(),
        )
        .map(|_| ())
    };
    let pwd = "p".repeat(22);
    assert_eq!(made("abc", &pwd), Err(Error::ErrLocalUfragInsufficientBits));
    assert_eq!(
        made("abcd", &"p".repeat(21)),
        Err(Error::ErrLocalPwdInsufficientBits)
    );
    assert_eq!(
        made(&"u".repeat(33), &pwd),
        Err(Error::ErrLocalUfragInvalid)
    );
    assert_eq!(made("ab-d", &pwd), Err(Error::ErrLocalUfragInvalid));
    assert_eq!(made("ab d", &pwd), Err(Error::ErrLocalUfragInvalid));
    assert_eq!(
        made("abcd", &"p".repeat(257)),
        Err(Error::ErrLocalPwdInvalid)
    );
    assert_eq!(
        made("abcd", &format!("{}:", "p".repeat(22))),
        Err(Error::ErrLocalPwdInvalid)
    );
    assert_eq!(made("abcd", &pwd), Ok(()));
    assert_eq!(made(&"u".repeat(32), &"p".repeat(256)), Ok(()));
    assert_eq!(made("a+/9", &format!("{}+/", "p".repeat(20))), Ok(()));

    let mut a = Agent::new(now, Arc::new(AgentConfig::default()), provider()).unwrap();
    assert_eq!(
        a.restart(now, "ab-d".to_owned(), pwd.clone(), false),
        Err(Error::ErrLocalUfragInvalid)
    );
    a.restart(now, "abcd".to_owned(), pwd, false).unwrap();
    assert_eq!(a.get_local_credentials().ufrag, "abcd");
}

/// RFC 8839 section 5.1: `raddr` and `rport` "MUST be present for server reflexive,
/// peer-reflexive and relayed candidates". A local server-reflexive or relayed candidate
/// without a related address would be written `raddr  rport 0`: the agent refuses it; with
/// one (or `0.0.0.0 0`, how a hidden base is written) it is taken.
#[test]
fn test_a_local_reflexive_candidate_needs_its_related_address() {
    use crate::candidate::candidate_relay::CandidateRelayConfig;
    use crate::candidate::candidate_server_reflexive::CandidateServerReflexiveConfig;
    let now = Instant::now();
    let mut a = Agent::new(now, Arc::new(AgentConfig::default()), provider()).unwrap();
    let base = |port: u16| CandidateConfig {
        network: "udp".to_owned(),
        address: "198.51.100.7".to_owned(),
        port,
        component: 1,
        ..Default::default()
    };
    let srflx = |rel_addr: &str, port: u16| {
        CandidateServerReflexiveConfig {
            base_config: base(port),
            rel_addr: rel_addr.to_owned(),
            rel_port: 5000,
            url: None,
        }
        .new_candidate_server_reflexive()
        .unwrap()
    };
    let relay = |rel_addr: &str, port: u16| {
        CandidateRelayConfig {
            base_config: base(port),
            rel_addr: rel_addr.to_owned(),
            rel_port: 5000,
            url: None,
        }
        .new_candidate_relay()
        .unwrap()
    };
    assert_eq!(
        a.add_local_candidate(srflx("", 6000)),
        Err(Error::ErrLocalCandidateWithoutRelatedAddress)
    );
    assert_eq!(
        a.add_local_candidate(relay("", 6001)),
        Err(Error::ErrLocalCandidateWithoutRelatedAddress)
    );
    assert_eq!(a.add_local_candidate(srflx("192.0.2.1", 6002)), Ok(true));
    assert_eq!(a.add_local_candidate(relay("0.0.0.0", 6003)), Ok(true));
}