diff options
| author | Sebastian Holmin <sebastian.holmin@mullvad.net> | 2024-02-06 14:56:06 +0100 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2024-02-08 15:53:31 +0100 |
| commit | 058a3e99dedcdccfe71b45b75ad060b8968cbc63 (patch) | |
| tree | f5c31a1e1d54254c4dbe264f6c487b0a2aa0de96 | |
| parent | 9963325e5779774b1da08b2720b50dff8280be81 (diff) | |
| download | mullvadvpn-058a3e99dedcdccfe71b45b75ad060b8968cbc63.tar.xz mullvadvpn-058a3e99dedcdccfe71b45b75ad060b8968cbc63.zip | |
Add `proptest` dependency
| -rw-r--r-- | Cargo.lock | 89 | ||||
| -rw-r--r-- | talpid-wireguard/Cargo.toml | 3 | ||||
| -rw-r--r-- | talpid-wireguard/src/lib.rs | 32 |
3 files changed, 117 insertions, 7 deletions
diff --git a/Cargo.lock b/Cargo.lock index 070d7c9c1a..cb8c92e08b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -290,6 +290,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" [[package]] +name = "bit-set" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0700ddab506f33b20a03b13996eccd309a48e5ff77d0d95926aa0210fb4e95f1" +dependencies = [ + "bit-vec", +] + +[[package]] +name = "bit-vec" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb" + +[[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1636,6 +1651,12 @@ dependencies = [ ] [[package]] +name = "libm" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" + +[[package]] name = "linked-hash-map" version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2226,6 +2247,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2" dependencies = [ "autocfg", + "libm", ] [[package]] @@ -2671,6 +2693,26 @@ dependencies = [ ] [[package]] +name = "proptest" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31b476131c3c86cb68032fdc5cb6d5a1045e3e42d96b69fa599fd77701e1f5bf" +dependencies = [ + "bit-set", + "bit-vec", + "bitflags 2.4.0", + "lazy_static", + "num-traits", + "rand 0.8.5", + "rand_chacha 0.3.1", + "rand_xorshift", + "regex-syntax 0.8.2", + "rusty-fork", + "tempfile", + "unarray", +] + +[[package]] name = "prost" version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2841,6 +2883,15 @@ dependencies = [ ] [[package]] +name = "rand_xorshift" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d25bf25ec5ae4a3f1b92f929810509a2f53d7dca2f50b794ff57e3face536c8f" +dependencies = [ + "rand_core 0.6.4", +] + +[[package]] name = "redox_syscall" version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2878,7 +2929,7 @@ dependencies = [ "aho-corasick", "memchr", "regex-automata", - "regex-syntax", + "regex-syntax 0.7.5", ] [[package]] @@ -2889,7 +2940,7 @@ checksum = "c2f401f4955220693b56f8ec66ee9c78abffd8d1c4f23dc41a23839eb88f0795" dependencies = [ "aho-corasick", "memchr", - "regex-syntax", + "regex-syntax 0.7.5", ] [[package]] @@ -2899,6 +2950,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da" [[package]] +name = "regex-syntax" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" + +[[package]] name = "resolv-conf" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -3029,6 +3086,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ffc183a10b4478d04cbbbfc96d0873219d962dd5accaff2ffbd4ceb7df837f4" [[package]] +name = "rusty-fork" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb3dcc6e454c328bb824492db107ab7c0ae8fcffe4ad210136ef014458c1bc4f" +dependencies = [ + "fnv", + "quick-error", + "tempfile", + "wait-timeout", +] + +[[package]] name = "ryu" version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -3726,6 +3795,7 @@ dependencies = [ "nix 0.23.2", "once_cell", "parking_lot", + "proptest", "rand 0.8.5", "rtnetlink", "socket2 0.5.3", @@ -4223,6 +4293,12 @@ dependencies = [ ] [[package]] +name = "unarray" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eaea85b334db583fe3274d12b4cd1880032beab409c0d774be044d4480ab9a94" + +[[package]] name = "unicode-bidi" version = "0.3.13" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -4300,6 +4376,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] +name = "wait-timeout" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6" +dependencies = [ + "libc", +] + +[[package]] name = "walkdir" version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/talpid-wireguard/Cargo.toml b/talpid-wireguard/Cargo.toml index c6f3669f8b..d28ccca2ae 100644 --- a/talpid-wireguard/Cargo.toml +++ b/talpid-wireguard/Cargo.toml @@ -79,3 +79,6 @@ features = [ "Win32_UI_Shell", "Win32_UI_WindowsAndMessaging", ] + +[dev-dependencies] +proptest = "1.4" diff --git a/talpid-wireguard/src/lib.rs b/talpid-wireguard/src/lib.rs index 0875ad0a49..09a0fc929a 100644 --- a/talpid-wireguard/src/lib.rs +++ b/talpid-wireguard/src/lib.rs @@ -1070,18 +1070,40 @@ async fn auto_mtu_detection( /// points. #[cfg(target_os = "linux")] fn mtu_spacing(mtu_min: u16, mtu_max: u16, step_size: u16) -> Vec<u16> { - if mtu_min > mtu_max { - panic!("Invalid MTU detection range: `mtu_min`={mtu_min}, `mtu_max`={mtu_max}."); - } - let second_mtu = mtu_min.next_multiple_of(step_size); + assert!(mtu_min < mtu_max); + assert!(step_size < mtu_max); + assert_ne!(step_size, 0); + + let second_mtu = (mtu_min + 1).next_multiple_of(step_size); let in_between = (second_mtu..mtu_max).step_by(step_size as usize); - let mut ret = Vec::with_capacity(((mtu_max - second_mtu).div_ceil(step_size) + 2) as usize); + + let mut ret = Vec::with_capacity(in_between.clone().count() + 2); ret.push(mtu_min); ret.extend(in_between); ret.push(mtu_max); ret } +#[cfg(all(test, target_os = "linux"))] +mod tests { + use crate::mtu_spacing; + use proptest::prelude::*; + + proptest! { + #[test] + fn test_mtu_spacing(mtu_min in 0..800u16, mtu_max in 800..2000u16, step_size in 1..800u16) { + let mtu_spacing = mtu_spacing(mtu_min, mtu_max, step_size); + + prop_assert_eq!(mtu_spacing.iter().filter(|mtu| mtu == &&mtu_min).count(), 1); + prop_assert_eq!(mtu_spacing.iter().filter(|mtu| mtu == &&mtu_max).count(), 1); + prop_assert_eq!(mtu_spacing.capacity(), mtu_spacing.len()); + let mut diffs = mtu_spacing.windows(2).map(|win| win[1]-win[0]); + prop_assert!(diffs.all(|diff| diff <= step_size)); + + } + } +} + #[derive(Debug)] enum CloseMsg { Stop, |
