1
//! Functions to help test this crate.
2

            
3
use std::io;
4

            
5
/// The type of stream returned by `construct_socketpair`.
6
#[cfg(not(windows))]
7
pub(crate) type SocketpairStream = socketpair::SocketpairStream;
8
#[cfg(windows)]
9
pub(crate) type SocketpairStream = std::net::TcpStream;
10

            
11
/// Test helper: construct a socketpair.
12
12
pub(crate) fn construct_socketpair() -> io::Result<(SocketpairStream, SocketpairStream)> {
13
12
    #[cfg(not(windows))]
14
12
    {
15
12
        socketpair::socketpair_stream()
16
12
    }
17
12
    #[cfg(windows)]
18
12
    {
19
12
        // Alas, we can't use the socketpair crate on Windows!  It creates a Windows
20
12
        // "named pipe".  Windows "named pipe"s are not named pipes.  They are strange
21
12
        // things which a bit like an unholy cross between a Unix named pipe (aka a FIFO)
22
12
        // and an AF_UNIX socket.  This makes them bizarre and awkward.  They are best
23
12
        // avoided if possible.
24
12
        //
25
12
        // We have to use this nonsense instead.  It will cause these tests to fail on
26
12
        // some absurdly restrictive windows firewalls; that's a price we can afford.
27
12
        //
28
12
        // For details see
29
12
        // https://gitlab.torproject.org/tpo/core/arti/-/merge_requests/2758#note_3155460
30
12
        let listener = std::net::TcpListener::bind("127.0.0.1:0")?;
31
12
        let addr = listener.local_addr()?;
32
12
        let s1 = std::net::TcpStream::connect(addr)?;
33
12
        let (s2, s2_addr) = listener.accept()?;
34
12
        assert_eq!(s1.local_addr().unwrap(), s2_addr);
35
12
        Ok((s1, s2))
36
12
    }
37
12
}