iroh 0.98.0

p2p quic connections dialed by public key
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
//! In-memory test transport for testing.
//!
//! This module provides [`TestNetwork`] and [`TestTransport`] for testing
//! using in-memory channels instead of real network transports.

use std::{
    collections::BTreeMap,
    io,
    sync::{Arc, Mutex},
    task::Poll,
};

use bytes::Bytes;
use iroh_base::{CustomAddr, EndpointId, TransportAddr};
use tokio::sync::mpsc::{self, error::TrySendError};
use tracing::info;

use crate::{
    address_lookup::{AddressLookup, EndpointData, EndpointInfo, Item},
    endpoint::{
        Builder,
        presets::Preset,
        transports::{Addr, CustomEndpoint, CustomSender, CustomTransport, Transmit},
    },
};

/// The transport ID used by [`TestNetwork`].
///
/// See `TRANSPORTS.md` for the registry of transport IDs.
pub const TEST_TRANSPORT_ID: u64 = 0x20;

/// An outgoing packet that can be sent across channels.
#[derive(Debug, Clone)]
pub(crate) struct Packet {
    pub(crate) data: Bytes,
    pub(crate) from: CustomAddr,
}

/// A test transport for use with [`TestNetwork`].
///
/// Implements [`CustomTransport`] and [`CustomEndpoint`] for testing.
#[derive(Debug, Clone)]
pub struct TestTransport {
    id: EndpointId,
    id_watchable: n0_watcher::Watchable<Vec<CustomAddr>>,
    network: TestNetwork,
}

impl Preset for Arc<TestTransport> {
    /// Configures the builder with this transport and the network's address lookup.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let network = TestNetwork::new();
    /// let transport = network.create_transport(secret_key.public())?;
    /// let ep = Endpoint::builder()
    ///     .secret_key(secret_key)
    ///     .preset(transport)
    ///     .bind()
    ///     .await?;
    /// ```
    fn apply(self, builder: Builder) -> Builder {
        builder
            .add_custom_transport(self.clone())
            .address_lookup(self.network.address_lookup())
    }
}

/// A simulated network for testing custom transports.
///
/// This allows creating multiple [`TestTransport`] instances that can communicate
/// with each other through in-memory channels.
///
/// # Example
///
/// ```ignore
/// use iroh::test_utils::custom_transport::TestNetwork;
///
/// let network = TestNetwork::new();
/// let transport1 = network.create_transport(endpoint_id1)?;
/// let transport2 = network.create_transport(endpoint_id2)?;
/// // transport1 and transport2 can now communicate via the network
/// ```
#[derive(Debug, Clone, Default)]
pub struct TestNetwork {
    inner: Arc<Mutex<TestNetworkInner>>,
}

impl TestNetwork {
    /// Creates a new empty test network.
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates an address lookup service for this network.
    pub fn address_lookup(&self) -> impl AddressLookup {
        TestAddrLookup {
            network: self.clone(),
        }
    }

    /// Creates a new test transport for the given endpoint ID.
    ///
    /// Returns an error if the ID already exists in the network.
    pub fn create_transport(&self, id: EndpointId) -> io::Result<Arc<TestTransport>> {
        let id_custom = to_custom_addr(id);
        let mut guard = self.inner.lock().expect("poisoned");
        if guard.channels.contains_key(&id) {
            return Err(io::Error::other("endpoint ID already exists in network"));
        }
        guard.channels.insert(id, mpsc::channel(256));
        drop(guard);
        Ok(Arc::new(TestTransport {
            id_watchable: n0_watcher::Watchable::new(vec![id_custom]),
            network: self.clone(),
            id,
        }))
    }
}

#[derive(Debug)]
struct TestAddrLookup {
    network: TestNetwork,
}

#[derive(Debug, Default)]
struct TestNetworkInner {
    channels: BTreeMap<EndpointId, (mpsc::Sender<Packet>, mpsc::Receiver<Packet>)>,
}

impl AddressLookup for TestAddrLookup {
    fn publish(&self, _data: &EndpointData) {}

    fn resolve(
        &self,
        endpoint_id: EndpointId,
    ) -> Option<n0_future::stream::Boxed<Result<Item, crate::address_lookup::Error>>> {
        if self
            .network
            .inner
            .lock()
            .expect("poisoned")
            .channels
            .contains_key(&endpoint_id)
        {
            Some(Box::pin(n0_future::stream::once(Ok(Item::new(
                EndpointInfo {
                    endpoint_id,
                    data: EndpointData::from_iter([TransportAddr::Custom(CustomAddr::from_parts(
                        TEST_TRANSPORT_ID,
                        endpoint_id.as_bytes(),
                    ))]),
                },
                "test discovery",
                None,
            )))))
        } else {
            None
        }
    }
}

#[derive(Debug, Clone)]
struct TestSender {
    id: EndpointId,
    network: TestNetwork,
}

/// Converts an endpoint ID to a custom address for this test transport.
pub fn to_custom_addr(endpoint: EndpointId) -> CustomAddr {
    CustomAddr::from((TEST_TRANSPORT_ID, &endpoint.as_bytes()[..]))
}

fn try_parse_custom_addr(addr: &CustomAddr) -> io::Result<EndpointId> {
    if addr.id() != TEST_TRANSPORT_ID {
        return Err(io::Error::other("unexpected transport id"));
    }
    let key_bytes: &[u8; 32] = addr
        .data()
        .try_into()
        .map_err(|_| io::Error::other("wrong key length"))?;
    EndpointId::from_bytes(key_bytes).map_err(|_| io::Error::other("KeyParseError"))
}

impl TestSender {
    fn send_sync(&self, dst: &CustomAddr, packets: Vec<Packet>) -> io::Result<()> {
        let to_id = try_parse_custom_addr(dst)?;
        let guard = self.network.inner.lock().expect("poisoned");
        let (s, _) = guard
            .channels
            .get(&to_id)
            .ok_or_else(|| io::Error::other("Unknown endpoint"))?;
        for packet in packets {
            let len = packet.data.len();
            match s.try_send(packet) {
                Ok(_) => info!(
                    "send {} -> {}: sent {} bytes",
                    self.id.fmt_short(),
                    to_id.fmt_short(),
                    len
                ),
                Err(TrySendError::Full(_)) => info!(
                    "send {} -> {}: dropped {} bytes",
                    self.id.fmt_short(),
                    to_id.fmt_short(),
                    len
                ),
                Err(TrySendError::Closed(_)) => return Err(io::Error::other("channel closed")),
            }
        }
        Ok(())
    }

    fn split(&self, transmit: &Transmit) -> impl Iterator<Item = Packet> {
        let from = to_custom_addr(self.id);
        let segment_size = transmit.segment_size.unwrap_or(transmit.contents.len());
        transmit
            .contents
            .chunks(segment_size)
            .map(move |slice| Packet {
                from: from.clone(),
                data: Bytes::copy_from_slice(slice),
            })
    }
}

impl CustomSender for TestSender {
    fn is_valid_send_addr(&self, addr: &CustomAddr) -> bool {
        addr.id() == TEST_TRANSPORT_ID
    }

    fn poll_send(
        &self,
        _cx: &mut std::task::Context,
        dst: &CustomAddr,
        transmit: &Transmit<'_>,
    ) -> Poll<io::Result<()>> {
        let packets = self.split(transmit).collect();
        Poll::Ready(self.send_sync(dst, packets))
    }
}

impl CustomTransport for TestTransport {
    fn bind(&self) -> io::Result<Box<dyn CustomEndpoint>> {
        Ok(Box::new(self.clone()))
    }
}

impl CustomEndpoint for TestTransport {
    fn watch_local_addrs(&self) -> n0_watcher::Direct<Vec<CustomAddr>> {
        self.id_watchable.watch()
    }

    fn create_sender(&self) -> Arc<dyn CustomSender> {
        Arc::new(TestSender {
            id: self.id,
            network: self.network.clone(),
        })
    }

    fn poll_recv(
        &mut self,
        cx: &mut std::task::Context,
        bufs: &mut [io::IoSliceMut<'_>],
        metas: &mut [noq_udp::RecvMeta],
        source_addrs: &mut [Addr],
    ) -> Poll<io::Result<usize>> {
        let n = bufs.len();
        debug_assert_eq!(n, metas.len());
        debug_assert_eq!(n, source_addrs.len());
        if n == 0 {
            return Poll::Ready(Ok(0));
        }
        let mut guard = self.network.inner.lock().expect("poisoned");
        let Some((_, r)) = guard.channels.get_mut(&self.id) else {
            info!("me: {} not found in channels", self.id.fmt_short());
            return Poll::Ready(Ok(0));
        };
        let mut packets = Vec::new();
        match r.poll_recv_many(cx, &mut packets, n) {
            Poll::Pending => return Poll::Pending,
            Poll::Ready(0) => return Poll::Ready(Err(io::Error::other("channel closed"))),
            Poll::Ready(n) => n,
        };
        let mut count = 0;
        for (((packet, meta), buf), source_addr) in
            packets.into_iter().zip(metas).zip(bufs).zip(source_addrs)
        {
            if buf.len() < packet.data.len() {
                break;
            }
            let from = try_parse_custom_addr(&packet.from).expect("valid custom addr");
            info!(
                "recv {} -> {}: copying {} bytes",
                from.fmt_short(),
                self.id.fmt_short(),
                packet.data.len()
            );
            buf[..packet.data.len()].copy_from_slice(&packet.data);
            *source_addr = packet.from.into();
            meta.len = packet.data.len();
            meta.stride = packet.data.len();
            count += 1;
        }
        if count > 0 {
            info!("recv {}: filled {count} slots", self.id.fmt_short());
            Poll::Ready(Ok(count))
        } else {
            Poll::Pending
        }
    }
}

#[cfg(test)]
mod tests {
    use std::{sync::Arc, time::Duration};

    use iroh_relay::RelayMap;
    use n0_error::{Result, StdResultExt};
    use n0_tracing_test::traced_test;
    use n0_watcher::Watcher;

    use super::*;
    use crate::{
        Endpoint, EndpointAddr, RelayMode, SecretKey, TransportAddr,
        endpoint::{
            Builder, Connection, presets,
            transports::{AddrKind, TransportBias},
        },
        protocol::{AcceptError, ProtocolHandler, Router},
        test_utils::run_relay_server,
    };

    const ECHO_ALPN: &[u8] = b"test/echo";

    #[derive(Debug, Clone)]
    struct Echo;

    impl ProtocolHandler for Echo {
        async fn accept(&self, connection: Connection) -> Result<(), AcceptError> {
            let (mut send, mut recv) = connection.accept_bi().await?;
            tokio::io::copy(&mut recv, &mut send).await?;
            send.finish()?;
            connection.closed().await;
            Ok(())
        }
    }

    /// Configuration for endpoint builder.
    #[derive(Clone, Default)]
    struct EndpointConfig {
        custom_bias: Option<TransportBias>,
        keep_ip: bool,
        relay_map: Option<RelayMap>,
    }

    impl EndpointConfig {
        fn with_custom_bias(mut self, bias: TransportBias) -> Self {
            self.custom_bias = Some(bias);
            self
        }

        fn with_ip(mut self) -> Self {
            self.keep_ip = true;
            self
        }

        fn with_relay(mut self, relay_map: RelayMap) -> Self {
            self.relay_map = Some(relay_map);
            self
        }
    }

    /// Creates a basic endpoint builder with the given secret key and custom transport.
    fn endpoint_builder(
        secret_key: SecretKey,
        transport: Arc<TestTransport>,
        config: EndpointConfig,
    ) -> Builder {
        let relay_mode = match config.relay_map {
            Some(map) => RelayMode::Custom(map),
            None => RelayMode::Disabled,
        };
        let mut builder = Endpoint::builder(presets::N0)
            .secret_key(secret_key)
            .relay_mode(relay_mode)
            .ca_roots_config(crate::tls::CaRootsConfig::insecure_skip_verify())
            .add_custom_transport(transport);
        if let Some(bias) = config.custom_bias {
            builder = builder.transport_bias(AddrKind::Custom(TEST_TRANSPORT_ID), bias);
        }
        if !config.keep_ip {
            builder = builder.clear_ip_transports();
        }
        builder
    }

    /// Creates an address with both IP (from endpoint) and custom transport addresses.
    fn mixed_addr(ep: &Endpoint, endpoint_id: EndpointId) -> EndpointAddr {
        let ep_addr = ep.addr();
        let custom_addr = to_custom_addr(endpoint_id);
        EndpointAddr::from_parts(
            endpoint_id,
            ep_addr
                .addrs
                .iter()
                .cloned()
                .chain(std::iter::once(TransportAddr::Custom(custom_addr))),
        )
    }

    /// Creates an address with only the custom transport address.
    fn custom_only_addr(endpoint_id: EndpointId) -> EndpointAddr {
        EndpointAddr::from_parts(
            endpoint_id,
            std::iter::once(TransportAddr::Custom(to_custom_addr(endpoint_id))),
        )
    }

    /// Returns true if the selected path is the custom transport.
    fn is_custom_selected(conn: &crate::endpoint::Connection) -> bool {
        let paths = conn.paths().get();
        paths.iter().find(|p| p.is_selected()).is_some_and(
            |p| matches!(p.remote_addr(), TransportAddr::Custom(a) if a.id() == TEST_TRANSPORT_ID),
        )
    }

    /// Returns true if either
    /// - we have both IP and custom paths, and the selected path is IP.
    /// - we only have one path
    fn is_ip_selected_from_ip_and_custom(conn: &crate::endpoint::Connection) -> bool {
        let paths = conn.paths().get();
        let has_ip = paths.iter().any(|p| p.remote_addr().is_ip());
        let has_custom = paths.iter().any(|p| p.remote_addr().is_custom());
        if !has_ip || !has_custom {
            return true;
        }
        paths
            .iter()
            .any(|p| p.is_selected() && p.remote_addr().is_ip())
    }

    /// Returns true if the selected path is a relay transport.
    fn is_relay_selected(conn: &crate::endpoint::Connection) -> bool {
        let paths = conn.paths().get();
        paths
            .iter()
            .find(|p| p.is_selected())
            .is_some_and(|p| p.is_relay())
    }

    /// Verifies echo works over the connection.
    async fn verify_echo(conn: &crate::endpoint::Connection, msg: &[u8]) -> Result<()> {
        let (mut send, mut recv) = conn.open_bi().await.anyerr()?;
        send.write_all(msg).await.anyerr()?;
        send.finish().anyerr()?;
        let response = recv.read_to_end(100).await.anyerr()?;
        assert_eq!(response, msg);
        Ok(())
    }

    /// Test custom transport only - no IP, no relay, dial by custom address.
    #[tokio::test]
    #[traced_test]
    async fn test_custom_transport_only() -> Result<()> {
        let network = TestNetwork::new();
        let s1 = SecretKey::generate();
        let s2 = SecretKey::generate();

        let t1 = network.create_transport(s1.public())?;
        let t2 = network.create_transport(s2.public())?;

        let ep1 = endpoint_builder(s1, t1, EndpointConfig::default())
            .bind()
            .await?;
        let ep2 = endpoint_builder(s2.clone(), t2, EndpointConfig::default())
            .bind()
            .await?;
        let router = Router::builder(ep2).accept(ECHO_ALPN, Echo).spawn();

        let conn = ep1
            .connect(custom_only_addr(s2.public()), ECHO_ALPN)
            .await?;

        // Verify exactly one path exists and it's the custom transport
        let paths = conn.paths().get();
        assert_eq!(paths.len(), 1, "Expected exactly one path");
        assert!(
            is_custom_selected(&conn),
            "Custom transport should be selected"
        );

        verify_echo(&conn, b"custom only").await?;
        conn.close(0u32.into(), b"done");
        router.shutdown().await.anyerr()?;
        Ok(())
    }

    /// Test that custom transport is selected over IP when given an RTT advantage.
    #[tokio::test]
    #[traced_test]
    async fn test_custom_transport_wins_over_ip() -> Result<()> {
        let network = TestNetwork::new();
        let s1 = SecretKey::generate();
        let s2 = SecretKey::generate();

        let t1 = network.create_transport(s1.public())?;
        let t2 = network.create_transport(s2.public())?;

        // Strong RTT advantage for custom transport
        let custom_bias = TransportBias::primary().with_rtt_advantage(Duration::from_secs(10));
        let config = EndpointConfig::default()
            .with_ip()
            .with_custom_bias(custom_bias);

        let ep1 = endpoint_builder(s1, t1, config.clone()).bind().await?;
        let ep2 = endpoint_builder(s2.clone(), t2, config).bind().await?;
        let router = Router::builder(ep2.clone()).accept(ECHO_ALPN, Echo).spawn();

        let conn = ep1
            .connect(mixed_addr(&ep2, s2.public()), ECHO_ALPN)
            .await?;

        // Wait for paths to settle
        tokio::time::sleep(Duration::from_millis(100)).await;

        assert!(
            is_custom_selected(&conn),
            "Custom transport should be selected with RTT advantage"
        );

        verify_echo(&conn, b"custom wins").await?;
        conn.close(0u32.into(), b"done");
        router.shutdown().await.anyerr()?;
        Ok(())
    }

    /// Test that IP is selected over custom transport when custom has an RTT disadvantage.
    #[tokio::test]
    #[traced_test]
    async fn test_ip_wins_over_custom() -> Result<()> {
        let network = TestNetwork::new();
        let s1 = SecretKey::generate();
        let s2 = SecretKey::generate();

        let t1 = network.create_transport(s1.public())?;
        let t2 = network.create_transport(s2.public())?;

        // Strong RTT disadvantage for custom transport
        let custom_bias = TransportBias::primary().with_rtt_disadvantage(Duration::from_secs(10));
        let config = EndpointConfig::default()
            .with_ip()
            .with_custom_bias(custom_bias);

        let ep1 = endpoint_builder(s1, t1, config.clone()).bind().await?;
        let ep2 = endpoint_builder(s2.clone(), t2, config).bind().await?;
        let router = Router::builder(ep2.clone()).accept(ECHO_ALPN, Echo).spawn();

        let conn = ep1
            .connect(mixed_addr(&ep2, s2.public()), ECHO_ALPN)
            .await?;

        // Wait for paths to settle
        tokio::time::sleep(Duration::from_millis(200)).await;

        assert!(
            is_ip_selected_from_ip_and_custom(&conn),
            "IP transport should be selected when custom has RTT disadvantage"
        );

        verify_echo(&conn, b"ip wins").await?;
        conn.close(0u32.into(), b"done");
        router.shutdown().await.anyerr()?;
        Ok(())
    }

    /// Test that custom transport (primary) is selected over relay (backup).
    ///
    /// This test first connects using only the relay address, then reconnects with
    /// both relay and custom addresses to verify the custom transport (primary) wins
    /// over the relay (backup).
    #[tokio::test]
    #[traced_test]
    async fn test_custom_transport_wins_over_relay() -> Result<()> {
        let (relay_map, _relay_url, _guard) = run_relay_server().await?;
        let network = TestNetwork::new();
        let s1 = SecretKey::generate();
        let s2 = SecretKey::generate();

        let t1 = network.create_transport(s1.public())?;
        let t2 = network.create_transport(s2.public())?;

        // Custom transport is primary by default, relay is backup
        let config = EndpointConfig::default().with_relay(relay_map.clone());

        let ep1 = endpoint_builder(s1, t1, config.clone()).bind().await?;
        let ep2 = endpoint_builder(s2.clone(), t2, config).bind().await?;

        // Wait for relay connection to be established
        ep1.online().await;
        ep2.online().await;

        let router = Router::builder(ep2.clone()).accept(ECHO_ALPN, Echo).spawn();

        // Get all addresses including relay and custom
        let ep2_addr = ep2.addr();
        let custom_addr = to_custom_addr(s2.public());

        // Debug: print ep2 address to see what's available
        eprintln!("ep2 address: {:?}", ep2_addr);

        // Create address with both relay and custom
        let all_addrs = EndpointAddr::from_parts(
            s2.public(),
            ep2_addr
                .addrs
                .iter()
                .cloned()
                .chain(std::iter::once(TransportAddr::Custom(custom_addr))),
        );
        eprintln!("Connecting with all addresses: {:?}", all_addrs);

        // First, connect with relay-only to verify relay works
        let relay_addrs: Vec<_> = ep2_addr
            .addrs
            .iter()
            .filter(|a| matches!(a, TransportAddr::Relay(_)))
            .cloned()
            .collect();
        eprintln!("Relay addresses in ep2_addr: {:?}", relay_addrs);

        // If there are no relay addresses, skip the relay-first test
        if relay_addrs.is_empty() {
            eprintln!(
                "WARNING: No relay addresses found in ep2_addr, skipping relay-first connection test"
            );
        } else {
            // Connect with relay-only address first to verify relay works
            let relay_only_addr = EndpointAddr::from_parts(s2.public(), relay_addrs.into_iter());
            eprintln!("Connecting with relay-only address: {:?}", relay_only_addr);

            let conn = ep1.connect(relay_only_addr, ECHO_ALPN).await?;

            // Wait for relay path to be established
            tokio::time::sleep(Duration::from_millis(200)).await;

            // Debug: print paths after relay-only connect
            let paths = conn.paths().get();
            eprintln!("Paths after relay-only connect:");
            for path in paths.iter() {
                eprintln!(
                    "  {} selected={} rtt={:?}",
                    path.remote_addr(),
                    path.is_selected(),
                    path.rtt()
                );
            }

            // Verify relay is currently selected
            assert!(
                is_relay_selected(&conn),
                "Relay should be selected after connecting with relay-only address"
            );

            verify_echo(&conn, b"relay test").await?;
            conn.close(0u32.into(), b"done with relay test");
            tokio::time::sleep(Duration::from_millis(100)).await;
        }

        // Now connect with all addresses (relay + custom)
        let conn = ep1.connect(all_addrs, ECHO_ALPN).await?;

        // Wait for paths to settle
        tokio::time::sleep(Duration::from_millis(200)).await;

        // Debug: print all paths
        let paths = conn.paths().get();
        eprintln!("Paths after connecting with all addresses:");
        for path in paths.iter() {
            eprintln!(
                "  {} selected={} rtt={:?}",
                path.remote_addr(),
                path.is_selected(),
                path.rtt()
            );
        }

        // Custom (primary) should win over relay (backup)
        assert!(
            is_custom_selected(&conn),
            "Custom transport (primary) should be selected over relay (backup)"
        );

        verify_echo(&conn, b"custom wins over relay").await?;
        conn.close(0u32.into(), b"done");
        router.shutdown().await.anyerr()?;
        Ok(())
    }
}