Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests/lib: Update impl Arbitrary for Proto #65

Closed
mxinden opened this issue Nov 12, 2022 · 3 comments · Fixed by #76
Closed

tests/lib: Update impl Arbitrary for Proto #65

mxinden opened this issue Nov 12, 2022 · 3 comments · Fixed by #76

Comments

@mxinden
Copy link
Member

mxinden commented Nov 12, 2022

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"),
}
}
}

@mxinden
Copy link
Member Author

mxinden commented Dec 20, 2022

One option would be to use variant_count to make sure the above 26 doesn't become outdated. @jxs any other ideas?

@thomaseizinger
Copy link
Contributor

One option would be to use variant_count to make sure the above 26 doesn't become outdated. @jxs any other ideas?

That is nightly-only though, right?

@thomaseizinger
Copy link
Contributor

We could add a test that only runs on nightly which asserts a constant that we use in the stable tests and run tests on nightly in CI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants