proptest_arbitrary/_std/
net.rs

1//! Arbitrary implementations for `std::net`.
2
3use super::*;
4use std::net::*;
5
6// TODO: Can we design a workable semantic for PBT wrt. actual networking
7// connections?
8
9arbitrary!(AddrParseError; "".parse::<Ipv4Addr>().unwrap_err());
10
11arbitrary!(Ipv4Addr,
12    TupleUnion<(
13        W<Just<Self>>,
14        W<Just<Self>>,
15        W<FMapped<'a, u32, Self>>,
16    )>;
17    prop_oneof![
18        1  => Just(Self::new(0, 0, 0, 0)),
19        4  => Just(Self::new(127, 0, 0, 1)),
20        10 => any_sinto::<u32, _>()
21    ]
22);
23
24arbitrary!(Ipv6Addr,
25    TupleUnion<(W<SMapped<'a, Ipv4Addr, Self>>, W<FMapped<'a, [u16; 8], Self>>)>;
26    prop_oneof![
27        2 => any_with_smap((), |ip| ip.to_ipv6_mapped()),
28        1 => any_sinto::<[u16; 8], _>()
29    ]
30);
31
32arbitrary!(SocketAddrV4, SMapped<'a, (Ipv4Addr, u16), Self>;
33    static_map(any::<(Ipv4Addr, u16)>(), |(a, b)| Self::new(a, b))
34);
35
36arbitrary!(SocketAddrV6, SMapped<'a, (Ipv6Addr, u16, u32, u32), Self>;
37    static_map(any::<(Ipv6Addr, u16, u32, u32)>(),
38        |(a, b, c, d)| Self::new(a, b, c, d))
39);
40
41arbitrary!(IpAddr,
42    TupleUnion<(W<FMapped<'a, Ipv4Addr, Self>>,
43                W<FMapped<'a, Ipv6Addr, Self>>)>;
44    prop_oneof![any_sinto::<Ipv4Addr, _>(), any_sinto::<Ipv6Addr, _>()]
45);
46
47arbitrary!(Shutdown,
48    TupleUnion<(W<Just<Self>>, W<Just<Self>>, W<Just<Self>>)>;
49    {
50        use std::net::Shutdown::*;
51        prop_oneof![Just(Both), Just(Read), Just(Write)]
52    }
53);
54arbitrary!(SocketAddr,
55    TupleUnion<(W<FMapped<'a, SocketAddrV4, Self>>,
56                W<FMapped<'a, SocketAddrV6, Self>>)>;
57    prop_oneof![any_sinto::<SocketAddrV4, _>(), any_sinto::<SocketAddrV6, _>()]
58);
59
60#[cfg(feature = "unstable")]
61arbitrary!(Ipv6MulticastScope,
62    TupleUnion<( W<Just<Self>>, W<Just<Self>>, W<Just<Self>>
63               , W<Just<Self>>, W<Just<Self>>, W<Just<Self>>, W<Just<Self>>)>;
64    {
65        use std::net::Ipv6MulticastScope::*;
66        prop_oneof![
67            Just(InterfaceLocal),
68            Just(LinkLocal),
69            Just(RealmLocal),
70            Just(AdminLocal),
71            Just(SiteLocal),
72            Just(OrganizationLocal),
73            Just(Global),
74        ]
75    }
76);
77
78#[cfg(test)]
79mod test {
80    no_panic_test!(
81        addr_parse_error => AddrParseError,
82        ipv4_addr => Ipv4Addr,
83        ipv6_addr => Ipv6Addr,
84        socket_addr_v4 => SocketAddrV4,
85        socket_addr_v6 => SocketAddrV6,
86        ip_addr => IpAddr,
87        shutdown => Shutdown,
88        socket_addr => SocketAddr
89    );
90
91    #[cfg(feature = "unstable")]
92    no_panic_test!(
93        ipv6_multicast_scope => Ipv6MulticastScope
94    );
95}