diff options
author | Aram Drevekenin <aram@poor.dev> | 2019-11-05 20:33:12 +0100 |
---|---|---|
committer | Aram Drevekenin <aram@poor.dev> | 2019-11-05 20:33:12 +0100 |
commit | 53d165b2a033615e2cce7ab5cc42ff2bf85e50da (patch) | |
tree | 1506e6e227d2d2fe622a73539480ec38bfb61c2d /src/tests | |
parent | db0f4d109403da54574a6469fe676f167c97e2c6 (diff) |
feat(raw): machine friendly output
Diffstat (limited to 'src/tests')
73 files changed, 5372 insertions, 1381 deletions
diff --git a/src/tests/cases/mod.rs b/src/tests/cases/mod.rs new file mode 100644 index 0000000..c917d49 --- /dev/null +++ b/src/tests/cases/mod.rs @@ -0,0 +1,2 @@ +pub mod ui; +pub mod raw_mode; diff --git a/src/tests/cases/raw_mode.rs b/src/tests/cases/raw_mode.rs new file mode 100644 index 0000000..4e05c3f --- /dev/null +++ b/src/tests/cases/raw_mode.rs @@ -0,0 +1,1115 @@ +use crate::tests::fakes::{ + create_fake_lookup_addr, create_fake_on_winch, get_interface, get_open_sockets, KeyboardEvents, + NetworkFrames, TestBackend, +}; + +use ::insta::assert_snapshot; +use ::std::sync::{Arc, Mutex}; +use ::termion::event::{Event, Key}; + +use ::std::collections::HashMap; +use ::std::net::IpAddr; + +use packet_builder::payload::PayloadData; +use packet_builder::*; +use pnet::packet::Packet; +use pnet_base::MacAddr; + +use ::std::io::Write; + +use crate::{start, Opt, OsInputOutput}; + +fn build_tcp_packet( + source_ip: &str, + destination_ip: &str, + source_port: u16, + destination_port: u16, + payload: &'static [u8], +) -> Vec<u8> { + let mut pkt_buf = [0u8; 1500]; + let pkt = packet_builder!( + pkt_buf, + ether({set_destination => MacAddr(0,0,0,0,0,0), set_source => MacAddr(0,0,0,0,0,0)}) / + ipv4({set_source => ipv4addr!(source_ip), set_destination => ipv4addr!(destination_ip) }) / + tcp({set_source => source_port, set_destination => destination_port }) / + payload(payload) + ); + pkt.packet().to_vec() +} + +fn format_raw_output<'t> (output: Vec<u8>) -> String { + let stdout_utf8 = String::from_utf8(output).unwrap(); + use regex::Regex; + let timestamp = Regex::new(r"<\d+>").unwrap(); + let replaced = timestamp.replace_all(&stdout_utf8, "<TIMESTAMP_REMOVED>"); + format!("{}", replaced) +} + +struct LogWithMirror<T> { + pub write: Arc<Mutex<T>>, + pub mirror: Arc<Mutex<T>>, +} + +impl<T> LogWithMirror<T> { + pub fn new(log: T) -> Self { + let write = Arc::new(Mutex::new(log)); + let mirror = write.clone(); + LogWithMirror { write, mirror } + } +} + +// #[test] +// fn basic_startup() { +// let keyboard_events = Box::new(KeyboardEvents::new(vec![ +// None, // sleep +// Some(Event::Key(Key::Ctrl('c'))), +// ])); +// let network_frames = NetworkFrames::new(vec![ +// None, // sleep +// ]); +// +// let terminal_width = Arc::new(Mutex::new(190)); +// let terminal_height = Arc::new(Mutex::new(50)); +// let terminal_events = LogWithMirror::new(Vec::new()); +// let terminal_draw_events = LogWithMirror::new(Vec::new()); +// +// let backend = TestBackend::new( +// terminal_events.write, +// terminal_draw_events.write, +// terminal_width, +// terminal_height, +// ); +// let network_interface = get_interface(); +// let lookup_addr = create_fake_lookup_addr(HashMap::new()); +// let on_winch = create_fake_on_winch(false); +// let cleanup = Box::new(|| {}); +// +// let os_input = OsInputOutput { +// network_interface, +// network_frames, +// get_open_sockets, +// keyboard_events, +// lookup_addr, +// on_winch, +// cleanup, +// }; +// let opts = Opt { +// interface: String::from("interface_name"), +// raw: true, +// no_resolve: false, +// }; +// let fake_stdout = Box::new(Vec::new()); +// let clone_stdout = fake_stdout.clone(); +// start(backend, os_input, Box::new(fake_stdout), opts); +// +// assert_snapshot!(String::from_utf8(*clone_stdout).unwrap()); +// } + +#[test] +fn one_packet_of_traffic() { + let keyboard_events = Box::new(KeyboardEvents::new(vec![ + None, // sleep + None, // sleep + Some(Event::Key(Key::Ctrl('c'))), + ])); + let network_frames = NetworkFrames::new(vec![Some(build_tcp_packet( + "10.0.0.2", + "1.1.1.1", + 443, + 12345, + b"I am a fake tcp packet", + ))]); + + let terminal_width = Arc::new(Mutex::new(190)); + let terminal_height = Arc::new(Mutex::new(50)); + let terminal_events = LogWithMirror::new(Vec::new()); + let terminal_draw_events = LogWithMirror::new(Vec::new()); + + let backend = TestBackend::new( + terminal_events.write, + terminal_draw_events.write, + terminal_width, + terminal_height, + ); + let network_interface = get_interface(); + let lookup_addr = create_fake_lookup_addr(HashMap::new()); + let on_winch = create_fake_on_winch(false); + let cleanup = Box::new(|| {}); + let stdout = Arc::new(Mutex::new(Vec::new())); + let write_to_stdout = Box::new({ + let stdout = stdout.clone(); + move |output: String| { + let mut stdout = stdout.lock().unwrap(); + writeln!(&mut stdout, "{}", output).unwrap(); + } + }); + + let os_input = OsInputOutput { + network_interface, + network_frames, + get_open_sockets, + keyboard_events, + lookup_addr, + on_winch, + cleanup, + write_to_stdout, + }; + let opts = Opt { + interface: String::from("interface_name"), + raw: true, + no_resolve: false, + }; + start(backend, os_input, opts); + let stdout = Arc::try_unwrap(stdout).unwrap().into_inner().unwrap(); + let formatted = format_raw_output(stdout); + assert_snapshot!(formatted); +} + +#[test] +fn bi_directional_traffic() { + let keyboard_events = Box::new(KeyboardEvents::new(vec![ + None, // sleep + None, // sleep + Some(Event::Key(Key::Ctrl('c'))), + ])); + let network_frames = NetworkFrames::new(vec![ + Some(build_tcp_packet( + "10.0.0.2", + "1.1.1.1", + 443, + 12345, + b"I am a fake tcp upload packet", + )), + Some(build_tcp_packet( + "1.1.1.1", + "10.0.0.2", + 12345, + 443, + b"I am a fake tcp download packet", + )), + ]); + + let terminal_width = Arc::new(Mutex::new(190)); + let terminal_height = Arc::new(Mutex::new(50)); + let terminal_events = LogWithMirror::new(Vec::new()); + let terminal_draw_events = LogWithMirror::new(Vec::new()); + + let backend = TestBackend::new( + terminal_events.write, + terminal_draw_events.write, + terminal_width, + terminal_height, + ); + let network_interface = get_interface(); + let lookup_addr = create_fake_lookup_addr(HashMap::new()); + let on_winch = create_fake_on_winch(false); + let cleanup = Box::new(|| {}); + let stdout = Arc::new(Mutex::new(Vec::new())); + let write_to_stdout = Box::new({ + let stdout = stdout.clone(); + move |output: String| { + let mut stdout = stdout.lock().unwrap(); + writeln!(&mut stdout, "{}", output).unwrap(); + } + }); + + let os_input = OsInputOutput { + network_interface, + network_frames, + get_open_sockets, + keyboard_events, + lookup_addr, + on_winch, + cleanup, + write_to_stdout, + }; + let opts = Opt { + interface: String::from("interface_name"), + raw: true, + no_resolve: false, + }; + start(backend, os_input, opts); + let stdout = Arc::try_unwrap(stdout).unwrap().into_inner().unwrap(); + let formatted = format_raw_output(stdout); + assert_snapshot!(formatted); +} + +#[test] +fn multiple_packets_of_traffic_from_different_connections() { + let keyboard_events = Box::new(KeyboardEvents::new(vec![ + None, // sleep + None, // sleep + Some(Event::Key(Key::Ctrl('c'))), + ])); + let network_frames = 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( + "2.2.2.2", + "10.0.0.2", + 54321, + 443, + b"I come from 2.2.2.2", + )), + ]); + + let terminal_width = Arc::new(Mutex::new(190)); + let terminal_height = Arc::new(Mutex::new(50)); + let terminal_events = LogWithMirror::new(Vec::new()); + let terminal_draw_events = LogWithMirror::new(Vec::new()); + + let backend = TestBackend::new( + terminal_events.write, + terminal_draw_events.write, + terminal_width, + terminal_height, + ); + let on_winch = create_fake_on_winch(false); + let cleanup = Box::new(|| {}); + let network_interface = get_interface(); + let lookup_addr = create_fake_lookup_addr(HashMap::new()); + let stdout = Arc::new(Mutex::new(Vec::new())); + let write_to_stdout = Box::new({ + let stdout = stdout.clone(); + move |output: String| { + let mut stdout = stdout.lock().unwrap(); + writeln!(&mut stdout, "{}", output).unwrap(); + } + }); + + let os_input = OsInputOutput { + network_interface, + network_frames, + get_open_sockets, + on_winch, + cleanup, + keyboard_events, + lookup_addr, + write_to_stdout, + }; + let opts = Opt { + interface: String::from("interface_name"), + raw: true, + no_resolve: false, + }; + start(backend, os_input, opts); + let stdout = Arc::try_unwrap(stdout).unwrap().into_inner().unwrap(); + let formatted = format_raw_output(stdout); + assert_snapshot!(formatted); +} + +#[test] +fn multiple_packets_of_traffic_from_single_connection() { + let keyboard_events = Box::new(KeyboardEvents::new(vec![ + None, // sleep + None, // sleep + Some(Event::Key(Key::Ctrl('c'))), + ])); + let network_frames = 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( + "1.1.1.1", + "10.0.0.2", + 12345, + 443, + b"I've come from 1.1.1.1 too!", + )), + ]); + + let terminal_width = Arc::new(Mutex::new(190)); + let terminal_height = Arc::new(Mutex::new(50)); + let terminal_events = LogWithMirror::new(Vec::new()); + let terminal_draw_events = LogWithMirror::new(Vec::new()); + + let backend = TestBackend::new( + terminal_events.write, + terminal_draw_events.write, + terminal_width, + terminal_height, + ); + let network_interface = get_interface(); + let lookup_addr = create_fake_lookup_addr(HashMap::new()); + let on_winch = create_fake_on_winch(false); + let cleanup = Box::new(|| {}); + let stdout = Arc::new(Mutex::new(Vec::new())); + let write_to_stdout = Box::new({ + let stdout = stdout.clone(); + move |output: String| { + let mut stdout = stdout.lock().unwrap(); + writeln!(&mut stdout, "{}", output).unwrap(); + } + }); + + let os_input = OsInputOutput { + network_interface, + network_frames, + get_open_sockets, + keyboard_events, + lookup_addr, + on_winch, + cleanup, + write_to_stdout, + }; + let opts = Opt { + interface: String::from("interface_name"), + raw: true, + no_resolve: false, + }; + start(backend, os_input, opts); + let stdout = Arc::try_unwrap(stdout).unwrap().into_inner().unwrap(); + let formatted = format_raw_output(stdout); + assert_snapshot!(formatted); +} + +#[test] +fn one_process_with_multiple_connections() { + let keyboard_events = Box::new(KeyboardEvents::new(vec![ + None, // sleep + None, // sleep + Some(Event::Key(Key::Ctrl('c'))), + ])); + let network_frames = 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, + 443, + b"F |