Skip to content

Commit 5260dbd

Browse files
committed
Enhanced Network Test
1 parent b42da4a commit 5260dbd

3 files changed

Lines changed: 77 additions & 21 deletions

File tree

tests/common.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ use uhyvelib::{
1212
vm::{UhyveVm, VmResult},
1313
};
1414

15+
pub const HERMIT_GATEWAY: &str = "10.0.5.2";
16+
pub const HERMIT_IP: &str = "10.0.5.3";
17+
1518
/// Uses Cargo to build a kernel in the `tests/test-kernels` directory.
1619
/// Returns a path to the build binary.
1720
pub fn build_hermit_bin(kernel: impl AsRef<Path> + std::fmt::Display) -> PathBuf {
@@ -27,6 +30,8 @@ pub fn build_hermit_bin(kernel: impl AsRef<Path> + std::fmt::Display) -> PathBuf
2730
.arg("--bin")
2831
.arg(kernel)
2932
.env("HERMIT_LOG_LEVEL_FILTER", "Debug")
33+
.env("HERMIT_IP", HERMIT_IP)
34+
.env("HERMIT_GATEWAY", HERMIT_GATEWAY)
3035
.current_dir(kernel_src_path)
3136
.status()
3237
.expect("failed to execute `cargo build`");

tests/network-test.rs

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,62 @@
11
mod common;
22

3-
use common::{build_hermit_bin, run_simple_vm};
3+
use std::{
4+
io::Write,
5+
net::TcpStream,
6+
thread::{self, sleep},
7+
time::Duration,
8+
};
9+
10+
use byte_unit::{Byte, Unit};
11+
use common::{HERMIT_IP, build_hermit_bin};
12+
use uhyvelib::{
13+
params::{Output, Params},
14+
vm::UhyveVm,
15+
};
416

517
#[test]
618
fn network_test() {
19+
let mut builder = env_logger::Builder::from_default_env();
20+
// The precise timestampe can be important when debugging networking,
21+
builder.format_timestamp_nanos().init();
22+
723
let bin_path = build_hermit_bin("network_test");
8-
run_simple_vm(bin_path);
9-
panic!();
24+
let params = Params {
25+
cpu_count: 1.try_into().unwrap(),
26+
memory_size: Byte::from_u64_with_unit(64, Unit::MiB)
27+
.unwrap()
28+
.try_into()
29+
.unwrap(),
30+
output: Output::Buffer,
31+
stats: true,
32+
aslr: false,
33+
..Default::default()
34+
};
35+
36+
let t = thread::spawn(move || {
37+
sleep(Duration::from_secs(2));
38+
39+
let mut hermit_ip = String::from(HERMIT_IP);
40+
hermit_ip.push_str(":9975");
41+
let mut stream = TcpStream::connect(hermit_ip).unwrap();
42+
for i in 0..10_u8 {
43+
let mut v = Vec::with_capacity(i as usize);
44+
for _ in 0..=i {
45+
v.push(b'a' + i);
46+
}
47+
println!("Sending {v:?}");
48+
stream.write(&v).unwrap();
49+
// TODO: Currently this test fails without the delay. Remove once fixed.
50+
sleep(Duration::from_millis(100));
51+
}
52+
stream.write(b"exit").unwrap();
53+
});
54+
55+
let res = UhyveVm::new(bin_path, params).unwrap().run(None);
56+
57+
t.join().unwrap();
58+
59+
for t in ["a\n", "bb\n", "ccc\n", "dddd\n", "eeeee\n"] {
60+
assert!(res.output.as_ref().unwrap().contains(t));
61+
}
1062
}

tests/test-kernels/src/bin/network_test.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,31 @@
33

44
use std::{
55
fs::File,
6-
io::{Error, Read},
7-
net::{Ipv4Addr, SocketAddrV4, TcpListener},
6+
io::{Error, Read, Write},
7+
net::{Ipv4Addr, SocketAddrV4, TcpListener, TcpStream},
88
};
99

1010
#[cfg(target_os = "hermit")]
1111
use hermit as _;
1212

1313
fn main() -> Result<(), Error> {
14-
println!("Network Test - ");
15-
// let loopback = Ipv4Addr::new(10, 8, 8, 11);
16-
// println!("1");
17-
// let socket = SocketAddrV4::new(loopback, 0);
18-
// println!("2");
19-
// let listener = TcpListener::bind(socket)?;
20-
// println!("3");
21-
14+
println!("Network Test");
2215
let listener = TcpListener::bind("127.0.0.1:9975").unwrap();
16+
println!("socket bound");
2317
let (mut socket, _) = listener.accept().unwrap();
24-
let mut buf = [0u8; 1000];
25-
println!("about to read");
26-
match socket.read(&mut buf) {
27-
Err(e) => {
28-
println!("read err {e:?}");
29-
}
30-
Ok(received) => {
31-
print!("read {}", std::str::from_utf8(&buf[..received]).unwrap());
18+
println!("connection established");
19+
loop {
20+
let mut buf = [0u8; 1500];
21+
match socket.read(&mut buf) {
22+
Err(e) => {
23+
println!("read err {e:?}");
24+
}
25+
Ok(received) => {
26+
if &buf[0..4] == b"exit" {
27+
break;
28+
}
29+
println!("read {}", std::str::from_utf8(&buf[..received]).unwrap());
30+
}
3231
}
3332
}
3433
Ok(())

0 commit comments

Comments
 (0)