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