Skip to content

tests/lib: Update impl Arbitrary for Proto #65

@mxinden

Description

@mxinden

Arbitrary implementation seems to be missing some enum variants.

Protocol has 30 variants. Arbitrary implementation generates 25.

Ideally this would be fixed by removing the need to list every Protocol variant. Though I can not think of a clean way of doing that. Simply updating the Arbitrary implementation seems fine for now.

impl Arbitrary for Proto {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
use Protocol::*;
match u8::arbitrary(g) % 26 {
// TODO: Add Protocol::Quic
0 => Proto(Dccp(Arbitrary::arbitrary(g))),
1 => Proto(Dns(Cow::Owned(SubString::arbitrary(g).0))),
2 => Proto(Dns4(Cow::Owned(SubString::arbitrary(g).0))),
3 => Proto(Dns6(Cow::Owned(SubString::arbitrary(g).0))),
4 => Proto(Http),
5 => Proto(Https),
6 => Proto(Ip4(Ipv4Addr::arbitrary(g))),
7 => Proto(Ip6(Ipv6Addr::arbitrary(g))),
8 => Proto(P2pWebRtcDirect),
9 => Proto(P2pWebRtcStar),
10 => Proto(P2pWebSocketStar),
11 => Proto(Memory(Arbitrary::arbitrary(g))),
// TODO: impl Arbitrary for Multihash:
12 => Proto(P2p(multihash(
"QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
))),
13 => Proto(P2pCircuit),
14 => Proto(Quic),
15 => Proto(Sctp(Arbitrary::arbitrary(g))),
16 => Proto(Tcp(Arbitrary::arbitrary(g))),
17 => Proto(Udp(Arbitrary::arbitrary(g))),
18 => Proto(Udt),
19 => Proto(Unix(Cow::Owned(SubString::arbitrary(g).0))),
20 => Proto(Utp),
21 => Proto(Ws("/".into())),
22 => Proto(Wss("/".into())),
23 => {
let a = iter::repeat_with(|| u8::arbitrary(g))
.take(10)
.collect::<Vec<_>>()
.try_into()
.unwrap();
Proto(Onion(Cow::Owned(a), std::cmp::max(1, u16::arbitrary(g))))
}
24 => {
let a: [u8; 35] = iter::repeat_with(|| u8::arbitrary(g))
.take(35)
.collect::<Vec<_>>()
.try_into()
.unwrap();
Proto(Onion3((a, std::cmp::max(1, u16::arbitrary(g))).into()))
}
25 => Proto(Tls),
_ => panic!("outside range"),
}
}
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions