diff options
author | cyqsimon <28627918+cyqsimon@users.noreply.github.com> | 2023-10-07 22:13:14 +0800 |
---|---|---|
committer | cyqsimon <28627918+cyqsimon@users.noreply.github.com> | 2023-10-07 22:35:35 +0800 |
commit | 72b04c82aec4a21fabe1bf694d56f44eb5cae193 (patch) | |
tree | c6e953f47d323d268959cfedcfa136778234af62 | |
parent | f1b49254c2c07efd4720beb1c742b6e94a0c94ee (diff) |
Compact tests using `rstest`
16 files changed, 393 insertions, 765 deletions
@@ -162,6 +162,7 @@ dependencies = [ "ratatui", "regex", "resolv-conf", + "rstest", "sysinfo", "thiserror", "tokio", @@ -568,12 +569,28 @@ dependencies = [ ] [[package]] +name = "futures" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23342abe12aba583913b2e62f22225ff9c950774065e4bfb61a19cd9770fec40" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] name = "futures-channel" version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "955518d47e09b25bbebc7a18df10b81f0c766eaf4c4f1cccef2fca5f2a4fb5f2" dependencies = [ "futures-core", + "futures-sink", ] [[package]] @@ -583,25 +600,64 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c" [[package]] +name = "futures-executor" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccecee823288125bd88b4d7f565c9e58e41858e47ab72e8ea2d64e93624386e0" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] name = "futures-io" version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fff74096e71ed47f8e023204cfd0aa1289cd54ae5430a9523be060cdb849964" [[package]] +name = "futures-macro" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.38", +] + +[[package]] +name = "futures-sink" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f43be4fe21a13b9781a69afa4985b0f6ee0e1afab2c6f454a8cf30e2b2237b6e" + +[[package]] name = "futures-task" version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "76d3d132be6c0e6aa1534069c705a74a5997a356c0dc2f86a47765e5617c5b65" [[package]] +name = "futures-timer" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e64b03909df88034c26dc1547e8970b91f98bdb65165d6a4e9110d94263dbb2c" + +[[package]] name = "futures-util" version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533" dependencies = [ + "futures-channel", "futures-core", + "futures-io", + "futures-macro", + "futures-sink", "futures-task", + "memchr", "pin-project-lite", "pin-utils", "slab", @@ -1376,6 +1432,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da" [[package]] +name = "relative-path" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c707298afce11da2efef2f600116fa93ffa7a032b5d7b628aa17711ec81383ca" + +[[package]] name = "resolv-conf" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1386,12 +1448,50 @@ dependencies = [ ] [[package]] +name = "rstest" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97eeab2f3c0a199bc4be135c36c924b6590b88c377d416494288c14f2db30199" +dependencies = [ + "futures", + "futures-timer", + "rstest_macros", + "rustc_version", +] + +[[package]] +name = "rstest_macros" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d428f8247852f894ee1be110b375111b586d4fa431f6c46e64ba5a0dcccbe605" +dependencies = [ + "cfg-if", + "glob", + "proc-macro2", + "quote", + "regex", + "relative-path", + "rustc_version", + "syn 2.0.38", + "unicode-ident", +] + +[[package]] name = "rustc-demangle" version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" [[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver", +] + +[[package]] name = "rustix" version = "0.36.15" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1463,6 +1563,12 @@ dependencies = [ ] [[package]] +name = "semver" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad977052201c6de01a8ef2aa3378c4bd23217a056337d1d6da40468d267a4fb0" + +[[package]] name = "serde" version = "1.0.188" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -48,6 +48,7 @@ procfs = "0.15.1" insta = "1.33.0" pnet_base = "0.34.0" packet-builder = { version = "0.7.0", git = "https://github.com/cyqsimon/packet_builder.git", branch = "patch-update" } +rstest = "0.18.2" [target.'cfg(target_os="windows")'.build-dependencies] anyhow = "1.0.75" diff --git a/src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout_full_width_under_30_height.snap b/src/tests/cases/snapshots/bandwhich__tests__cases__ui__full-width-under-30-height-0.snap index 1e26e22..1e26e22 100644 --- a/src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout_full_width_under_30_height.snap +++ b/src/tests/cases/snapshots/bandwhich__tests__cases__ui__full-width-under-30-height-0.snap diff --git a/src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout_full_width_under_30_height-2.snap b/src/tests/cases/snapshots/bandwhich__tests__cases__ui__full-width-under-30-height-1.snap index 9c60b32..9c60b32 100644 --- a/src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout_full_width_under_30_height-2.snap +++ b/src/tests/cases/snapshots/bandwhich__tests__cases__ui__full-width-under-30-height-1.snap diff --git a/src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout_under_50_width_under_50_height-2.snap b/src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout_under_50_width_under_50_height-2.snap deleted file mode 100644 index b7493b4..0000000 --- a/src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout_under_50_width_under_50_height-2.snap +++ /dev/null @@ -1,55 +0,0 @@ ---- -source: src/tests/cases/ui.rs -assertion_line: 1580 -expression: "&terminal_draw_events_mirror[1]" ---- - 98Bps - - - 5 0Bps / 28Bps - 4 0Bps / 26Bps - 1 0Bps / 22Bps - 2 0Bps / 21Bps - - - - - - - - - - - - - - - - - - - - - 3.3.3.3 0Bps / 28Bps - 2.2.2.2 0Bps / 26Bps - 1.1.1.1 0Bps / 22Bps - 4.4.4.4 0Bps / 21Bps - - - - - - - - - - - - - - - - - - - diff --git a/src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout_under_50_width_under_50_height.snap b/src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout_under_50_width_under_50_height.snap deleted file mode 100644 index 15cfa04..0000000 --- a/src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout_under_50_width_under_50_height.snap +++ /dev/null @@ -1,55 +0,0 @@ ---- -source: src/tests/cases/ui.rs -assertion_line: 1579 -expression: "&terminal_draw_events_mirror[0]" ---- - Total Up / Down: 0Bps / 0Bps -┌Utilization by process name────────────────────┐ -│Process Up / Down │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -└───────────────────────────────────────────────┘ -┌Utilization by remote address──────────────────┐ -│Remote Address Up / Down │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -└───────────────────────────────────────────────┘ - Press <SPACE> to pause. - diff --git a/src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout_under_120_width_full_height.snap b/src/tests/cases/snapshots/bandwhich__tests__cases__ui__under-120-width-full-height-0.snap index 53043a7..53043a7 100644 --- a/src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout_under_120_width_full_height.snap +++ b/src/tests/cases/snapshots/bandwhich__tests__cases__ui__under-120-width-full-height-0.snap diff --git a/src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout_under_120_width_full_height-2.snap b/src/tests/cases/snapshots/bandwhich__tests__cases__ui__under-120-width-full-height-1.snap index 8230e66..8230e66 100644 --- a/src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout_under_120_width_full_height-2.snap +++ b/src/tests/cases/snapshots/bandwhich__tests__cases__ui__under-120-width-full-height-1.snap diff --git a/src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout_under_120_width_under_30_height.snap b/src/tests/cases/snapshots/bandwhich__tests__cases__ui__under-120-width-under-30-height-0.snap index 0bbc796..0bbc796 100644 --- a/src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout_under_120_width_under_30_height.snap +++ b/src/tests/cases/snapshots/bandwhich__tests__cases__ui__under-120-width-under-30-height-0.snap diff --git a/src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout_under_120_width_under_30_height-2.snap b/src/tests/cases/snapshots/bandwhich__tests__cases__ui__under-120-width-under-30-height-1.snap index 06cb645..06cb645 100644 --- a/src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout_under_120_width_under_30_height-2.snap +++ b/src/tests/cases/snapshots/bandwhich__tests__cases__ui__under-120-width-under-30-height-1.snap diff --git a/src/tests/cases/snapshots/bandwhich__tests__cases__ui__under-50-width-under-50-height-0.snap b/src/tests/cases/snapshots/bandwhich__tests__cases__ui__under-50-width-under-50-height-0.snap new file mode 100644 index 0000000..b0c7143 --- /dev/null +++ b/src/tests/cases/snapshots/bandwhich__tests__cases__ui__under-50-width-under-50-height-0.snap @@ -0,0 +1,55 @@ +--- +source: src/tests/cases/ui.rs +expression: "&terminal_draw_events_mirror[0]" +--- + Total Up / Down: 0Bps / 0Bps +┌Utilization by process name─────────────────────┐ +│Process Up / Down │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +└────────────────────────────────────────────────┘ +┌Utilization by remote address───────────────────┐ +│Remote Address Up / Down │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +└────────────────────────────────────────────────┘ + Press <SPACE> to pause. + diff --git a/src/tests/cases/snapshots/bandwhich__tests__cases__ui__under-50-width-under-50-height-1.snap b/src/tests/cases/snapshots/bandwhich__tests__cases__ui__under-50-width-under-50-height-1.snap new file mode 100644 index 0000000..8e7dbcb --- /dev/null +++ b/src/tests/cases/snapshots/bandwhich__tests__cases__ui__under-50-width-under-50-height-1.snap @@ -0,0 +1,55 @@ +--- +source: src/tests/cases/ui.rs +expression: "&terminal_draw_events_mirror[1]" +--- + 98Bps + + + 5 0Bps / 28Bps + 4 0Bps / 26Bps + 1 0Bps / 22Bps + 2 0Bps / 21Bps + + + + + + + + + + + + + + + + + + + + + 3.3.3.3 0Bps / 28Bps + 2.2.2.2 0Bps / 26Bps + 1.1.1.1 0Bps / 22Bps + 4.4.4.4 0Bps / 21Bps + + + + + + + + + + + + + + + + + + + + diff --git a/src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout_under_70_width_under_30_height.snap b/src/tests/cases/snapshots/bandwhich__tests__cases__ui__under-70-width-under-30-height-0.snap index 281674a..281674a 100644 --- a/src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout_under_70_width_under_30_height.snap +++ b/src/tests/cases/snapshots/bandwhich__tests__cases__ui__under-70-width-under-30-height-0.snap diff --git a/src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout_under_70_width_under_30_height-2.snap b/src/tests/cases/snapshots/bandwhich__tests__cases__ui__under-70-width-under-30-height-1.snap index 6f24434..6f24434 100644 --- a/src/tests/cases/snapshots/bandwhich__tests__cases__ui__layout_under_70_width_under_30_height-2.snap +++ b/src/tests/cases/snapshots/bandwhich__tests__cases__ui__under-70-width-under-30-height-1.snap diff --git a/src/tests/cases/test_utils.rs b/src/tests/cases/test_utils.rs index d2df990..bd0400e 100644 --- a/src/tests/cases/test_utils.rs +++ b/src/tests/cases/test_utils.rs @@ -9,6 +9,7 @@ use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; use packet_builder::*; use pnet::{datalink::DataLinkReceiver, packet::Packet}; use pnet_base::MacAddr; +use rstest::fixture; use crate::{ network::dns::Client, @@ -56,7 +57,8 @@ pub fn build_tcp_packet( pkt.packet().to_vec() } -pub fn sample_frames() -> Vec<Box<dyn DataLinkReceiver>> { +#[fixture] +pub fn sample_frames_short() -> Vec<Box<dyn DataLinkReceiver>> { vec![NetworkFrames::new(vec![ Some(build_tcp_packet( "10.0.0.2", @@ -82,6 +84,125 @@ pub fn sample_frames() -> Vec<Box<dyn DataLinkReceiver>> { ]) as Box<dyn DataLinkReceiver>] } +#[fixture] +pub fn sample_frames_sustained_one_process() -> Vec<Box<dyn DataLinkReceiver>> { + vec![NetworkFrames::new(vec![ + Some(build_tcp_packet( + "1.1.1.1", + "10.0.0.2", + 12345, + 443, + b"I have come from 1.1.1.1", + )), + None, // sleep + Some(build_tcp_packet( + "1.1.1.1", + "10.0.0.2", + 12345, + 443, + b"Same here, but one second later", + )), + ]) as Box<dyn DataLinkReceiver>] +} + +#[fixture] +pub fn sample_frames_sustained_multiple_processes() -> Vec<Box<dyn DataLinkReceiver>> { + vec![NetworkFrames::new(vec![ + Some(build_tcp_packet( + "1.1.1.1", + "10.0.0.2", + 12345, + 443, + b"I have come from 1.1.1.1", + )), + Some(build_tcp_packet( + "3.3.3.3", + "10.0.0.2", + 1337, + 4435, + b"I come from 3.3.3.3", + )), + None, // sleep + Some(build_tcp_packet( + "1.1.1.1", + "10.0.0.2", + 12345, + 443, + b"I have come from 1.1.1.1 one second later", + )), + Some(build_tcp_packet( + "3.3.3.3", + "10.0.0.2", + 1337, + 4435, + b"I come 3.3.3.3 one second later", + )), + ]) as Box<dyn DataLinkReceiver>] +} + +#[fixture] +pub fn sample_frames_sustained_long() -> Vec<Box<dyn DataLinkReceiver>> { + vec![NetworkFrames::new(vec![ + Some(build_tcp_packet( + "10.0.0.2", + "3.3.3.3", + 4435, + 1337, + b"omw to 3.3.3.3", + )), + Some(build_tcp_packet( + "3.3.3.3", + "10.0.0.2", + 1337, + 4435, + b"I was just there!", + )), + Some(build_tcp_packet( + "1.1.1.1", + "10.0.0.2", + 12345, + 443, + b"Is it nice there? I think 1.1.1.1 is dull", + )), + Some(build_tcp_packet( + "10.0.0.2", + "1.1.1.1", + 443, + 12345, + b"Well, I heard 1.1.1.1 is all the rage", + )), + None, // sleep + Some(build_tcp_packet( + "10.0.0.2", + "3.3.3.3", + 4435, + 1337, + b"Wait for me!", + )), + Some(build_tcp_packet( + "3.3.3.3", + "10.0.0.2", + 1337, + 4435, + b"They're waiting for you...", + )), + Some(build_tcp_packet( + "1.1.1.1", + "10.0.0.2", + 12345, + 443, + b"1.1.1.1 forever!", + )), + Some(build_tcp_packet( + "10.0.0.2", + "1.1.1.1", + 443, + 12345, + b"10.0.0.2 forever!", + )), + ]) as Box<dyn DataLinkReceiver>] +} + pub fn os_input_output( network_frames: Vec<Box<dyn DataLinkReceiver>>, sleep_num: usize, diff --git a/src/tests/cases/ui.rs b/src/tests/cases/ui.rs index bf2779b..32ac87b 100644 --- a/src/tests/cases/ui.rs +++ b/src/tests/cases/ui.rs @@ -3,12 +3,15 @@ use std::{collections::HashMap, iter, net::IpAddr}; use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; use insta::assert_snapshot; use pnet::datalink::DataLinkReceiver; +use rstest::rstest; use crate::{ start, tests::{ cases::test_utils::{ - build_tcp_packet, opts_ui, os_input_output, os_input_output_factory, sample_frames, + build_tcp_packet, opts_ui, os_input_output, os_input_output_factory, + sample_frames_short, sample_frames_sustained_long, + sample_frames_sustained_multiple_processes, sample_frames_sustained_one_process, sleep_and_quit_events, sleep_resize_and_quit_events, test_backend_factory, }, fakes::{ @@ -266,12 +269,10 @@ fn basic_only_addresses() { assert_snapshot!(&terminal_draw_events_mirror[0]); } -#[test] -fn two_packets_only_processes() { - let network_frames = sample_frames(); - +#[rstest(sample_frames_short as frames)] +fn two_packets_only_processes(frames: Vec<Box<dyn DataLinkReceiver>>) { let (_, terminal_draw_events, backend) = test_backend_factory(190, 50); - let os_input = os_input_output(network_frames, 2); + let os_input = os_input_output(frames, 2); let opts = Opt { interface: Some(String::from("interface_name")), raw: false, @@ -292,12 +293,10 @@ fn two_packets_only_processes() { assert_snapshot!(&terminal_draw_events_mirror[1]); } -#[test] -fn two_packets_only_connections() { - let network_frames = sample_frames(); - +#[rstest(sample_frames_short as frames)] +fn two_packets_only_connections(frames: Vec<Box<dyn DataLinkReceiver>>) { let (_, terminal_draw_events, backend) = test_backend_factory(190, 50); - let os_input = os_input_output(network_frames, 2); + let os_input = os_input_output(frames, 2); let opts = Opt { interface: Some(String::from("interface_name")), raw: false, @@ -318,12 +317,10 @@ fn two_packets_only_connections() { assert_snapshot!(&terminal_draw_events_mirror[1]); } -#[test] -fn two_packets_only_addresses() { - let network_frames = sample_frames(); - +#[rstest(sample_frames_short as frames)] +fn two_packets_only_addresses(frames: Vec<Box<dyn DataLinkReceiver>>) { let (_, terminal_draw_events, backend) = test_backend_factory(190, 50); - let os_input = os_input_output(network_frames, 2); + let os_input = os_input_output(frames, 2); let opts = Opt { interface: Some(String::from("interface_name")), raw: false, @@ -426,12 +423,10 @@ fn one_packet_of_traffic() { assert_snapshot!(&terminal_draw_events_mirror[1]); } -#[test] -fn bi_directional_traffic() { - let network_frames = sample_frames(); - +#[rstest(sample_frames_short as frames)] +fn bi_directional_traffic(frames: Vec<Box<dyn DataLinkReceiver>>) { let (terminal_events, terminal_draw_events, backend) = test_backend_factory(190, 50); - let os_input = os_input_output(network_frames, 2); + let os_input = os_input_output(frames, 2); let opts = opts_ui(); start(backend, os_input, opts); let terminal_draw_events_mirror = terminal_draw_events.lock().unwrap(); @@ -653,29 +648,11 @@ fn multiple_connections_from_remote_address() { assert_snapshot!(&terminal_draw_events_mirror[1]); } -#[test] -fn sustained_traffic_from_one_process() { - let network_frames = vec![NetworkFrames::new(vec![ - Some(build_tcp_packet( - "1.1.1.1", - "10.0.0.2", - 12345, - 443, - b"I have come from 1.1.1.1", - )), - None, // sleep - Some(build_tcp_packet( - "1.1.1.1", - "10.0.0.2", - 12345, - 443, - b"Same here, but one second later", - )), - ]) as Box<dyn DataLinkReceiver>]; - +#[rstest(sample_frames_sustained_one_process as frames)] +fn sustained_traffic_from_one_process(frames: Vec<Box<dyn DataLinkReceiver>>) { let (terminal_events, terminal_draw_events, backend) = test_backend_factory(190, 50); - let os_input = os_input_output(network_frames, 3); + let os_input = os_input_output(frames, 3); let opts = opts_ui(); start(backend, os_input, opts); let ter |