use std::fs::File;
use unkernel_rs::{ExecOptions, SwitchAttachment, VirtualSwitch, Vm, VmConfig};
#[test]
fn multi_vm_virtual_l2_switch_ping_tcp_cut_reconnect_and_pcapng() {
let dir = std::env::temp_dir().join(format!("unkernel-rs-lab-test-{}", std::process::id()));
let _ = std::fs::create_dir_all(&dir);
let pcap_path = dir.join("lab.pcapng");
let pcap_file = File::create(&pcap_path).expect("create pcapng");
let sw = VirtualSwitch::new("switch0");
sw.attach_pcap(pcap_file).expect("attach pcapng");
let (mac1, ip1) = VirtualSwitch::allocate_identity(0);
let (mac2, ip2) = VirtualSwitch::allocate_identity(1);
let mut cfg1 = VmConfig {
offline: true,
..VmConfig::default()
};
cfg1.switch = Some(SwitchAttachment {
switch: sw.clone(),
node_name: "node1".to_string(),
mac: mac1,
ip: ip1,
});
let mut cfg2 = VmConfig {
offline: true,
..VmConfig::default()
};
cfg2.switch = Some(SwitchAttachment {
switch: sw.clone(),
node_name: "node2".to_string(),
mac: mac2,
ip: ip2,
});
let h1 = std::thread::spawn(move || Vm::boot(cfg1));
let h2 = std::thread::spawn(move || Vm::boot(cfg2));
let vm1 = h1.join().expect("join vm1").expect("boot vm1");
let vm2 = h2.join().expect("join vm2").expect("boot vm2");
let setup1 = "ip link set eth0 up && ip addr flush dev eth0 && ip addr add 10.0.2.10/24 dev eth0 && printf '127.0.0.1 localhost\\n10.0.2.10 node1\\n10.0.2.11 node2\\n' > /etc/hosts";
let setup2 = "ip link set eth0 up && ip addr flush dev eth0 && ip addr add 10.0.2.11/24 dev eth0 && printf '127.0.0.1 localhost\\n10.0.2.10 node1\\n10.0.2.11 node2\\n' > /etc/hosts";
let mut o1 = ExecOptions::shell(setup1);
o1.user = Some("root".to_string());
assert_eq!(vm1.exec(&o1).expect("setup vm1").exit_code, Some(0));
let mut o2 = ExecOptions::shell(setup2);
o2.user = Some("root".to_string());
assert_eq!(vm2.exec(&o2).expect("setup vm2").exit_code, Some(0));
let mut ping_ok = ExecOptions::shell("ping -c 2 -W 2 node2");
ping_ok.user = Some("root".to_string());
let res = vm1.exec(&ping_ok).expect("ping node2");
let out = String::from_utf8_lossy(&res.stdout);
assert_eq!(
res.exit_code,
Some(0),
"expected ping success, stdout={out}"
);
assert!(out.contains("0% packet loss"), "stdout={out}");
let mut start_srv = ExecOptions::shell(
"nohup sh -c 'printf \"HELLO_OVER_SWITCH0\\n\" | nc -l -p 9090' >/dev/null 2>&1 &",
);
start_srv.user = Some("root".to_string());
let _ = vm2.exec(&start_srv).expect("start nc server on node2");
std::thread::sleep(std::time::Duration::from_millis(150));
let mut nc_client = ExecOptions::shell("nc -w 2 node2 9090");
nc_client.user = Some("root".to_string());
let tcp_res = vm1.exec(&nc_client).expect("nc connect to node2");
let tcp_out = String::from_utf8_lossy(&tcp_res.stdout);
assert!(
tcp_out.contains("HELLO_OVER_SWITCH0"),
"expected TCP payload across virtual L2 switch, got stdout={tcp_out:?} stderr={:?}",
String::from_utf8_lossy(&tcp_res.stderr)
);
assert!(sw.cut_link("node2"));
let mut ping_cut = ExecOptions::shell("ping -c 1 -W 1 node2");
ping_cut.user = Some("root".to_string());
let res_cut = vm1.exec(&ping_cut).expect("ping while cut");
assert_ne!(
res_cut.exit_code,
Some(0),
"expected ping to fail while cable is cut"
);
assert!(sw.reconnect_link("node2"));
let res_back = vm1.exec(&ping_ok).expect("ping after reconnect");
assert_eq!(
res_back.exit_code,
Some(0),
"expected ping recovery after reconnect"
);
let macs = sw.mac_table();
assert_eq!(macs.len(), 2, "expected 2 learned MACs, got {macs:?}");
assert!(sw.total_frames() >= 8, "expected >= 8 switched frames");
sw.flush_pcap();
vm1.shutdown();
vm2.shutdown();
let raw = std::fs::read(&pcap_path).expect("read pcapng");
let _ = std::fs::remove_file(&pcap_path);
let _ = std::fs::remove_dir(&dir);
assert_eq!(
&raw[0..4],
&0x0a0d_0d0au32.to_le_bytes(),
"invalid PCAPng SHB magic"
);
assert!(
raw.windows(18).any(|w| w == b"HELLO_OVER_SWITCH0"),
"expected TCP payload captured inside lab.pcapng ({} bytes)",
raw.len()
);
}