|
| 1 | +use macros::{guest, host}; |
| 2 | +use std::io::{ErrorKind, Read}; |
| 3 | +use std::mem; |
| 4 | +use std::net::TcpStream; |
| 5 | +use std::time::Duration; |
| 6 | + |
| 7 | +pub struct TestTsiTcpGuestConnect; |
| 8 | + |
| 9 | +fn stream_expect_msg(stream: &mut TcpStream, expected: &[u8]) { |
| 10 | + let mut buf = vec![0; expected.len()]; |
| 11 | + stream.read_exact(&mut buf[..]).unwrap(); |
| 12 | + assert_eq!(&buf[..], expected); |
| 13 | +} |
| 14 | + |
| 15 | +fn stream_expect_wouldblock(stream: &mut TcpStream) { |
| 16 | + stream.set_nonblocking(true).unwrap(); |
| 17 | + let err = stream.read(&mut [0u8; 1]).unwrap_err(); |
| 18 | + stream.set_nonblocking(false).unwrap(); |
| 19 | + assert_eq!(err.kind(), ErrorKind::WouldBlock); |
| 20 | +} |
| 21 | + |
| 22 | +fn stream_set_timeouts(stream: &mut TcpStream) { |
| 23 | + stream |
| 24 | + .set_read_timeout(Some(Duration::from_millis(500))) |
| 25 | + .unwrap(); |
| 26 | + stream |
| 27 | + .set_write_timeout(Some(Duration::from_millis(500))) |
| 28 | + .unwrap(); |
| 29 | +} |
| 30 | + |
| 31 | +#[host] |
| 32 | +mod host { |
| 33 | + use super::*; |
| 34 | + |
| 35 | + use crate::common::setup_fs_and_enter; |
| 36 | + use crate::{krun_call, krun_call_u32}; |
| 37 | + use crate::{Test, TestSetup}; |
| 38 | + use krun_sys::*; |
| 39 | + use std::ffi::CStr; |
| 40 | + use std::io::Write; |
| 41 | + use std::net::TcpListener; |
| 42 | + use std::thread; |
| 43 | + |
| 44 | + fn server(listener: TcpListener) { |
| 45 | + let (mut stream, _addr) = listener.accept().unwrap(); |
| 46 | + eprintln!("accepted a new connection"); |
| 47 | + stream_set_timeouts(&mut stream); |
| 48 | + stream.write_all(b"ping!").unwrap(); |
| 49 | + stream_expect_msg(&mut stream, b"pong!"); |
| 50 | + stream_expect_wouldblock(&mut stream); |
| 51 | + stream.write_all(b"bye!").unwrap(); |
| 52 | + mem::forget(listener); |
| 53 | + } |
| 54 | + |
| 55 | + impl Test for TestTsiTcpGuestConnect { |
| 56 | + fn start_vm(self: Box<Self>, test_setup: TestSetup) -> anyhow::Result<()> { |
| 57 | + unsafe { |
| 58 | + krun_call!(krun_set_log_level(0)); |
| 59 | + } |
| 60 | + let listener = TcpListener::bind("0.0.0.0:8121").unwrap(); |
| 61 | + thread::spawn(move || server(listener)); |
| 62 | + thread::sleep(Duration::from_secs(1)); |
| 63 | + |
| 64 | + eprintln!("spawned server"); |
| 65 | + unsafe { |
| 66 | + let ctx = krun_call_u32!(krun_create_ctx())?; |
| 67 | + krun_call!(krun_set_vm_config(ctx, 1, 512))?; |
| 68 | + setup_fs_and_enter(ctx, test_setup)?; |
| 69 | + } |
| 70 | + Ok(()) |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +#[guest] |
| 76 | +mod guest { |
| 77 | + use super::*; |
| 78 | + use crate::Test; |
| 79 | + use std::io::{stderr, Write}; |
| 80 | + use std::net::SocketAddr; |
| 81 | + use std::str::FromStr; |
| 82 | + |
| 83 | + impl Test for TestTsiTcpGuestConnect { |
| 84 | + fn in_guest(self: Box<Self>) { |
| 85 | + eprintln!("In guest"); |
| 86 | + let addr = SocketAddr::from_str("127.0.0.1:8121").unwrap(); |
| 87 | + let mut stream = match TcpStream::connect(&addr) { |
| 88 | + Ok(stream) => stream, |
| 89 | + Err(err) => { |
| 90 | + let raw_err = err.raw_os_error().unwrap(); |
| 91 | + eprintln!("connect error: {err}, ({raw_err})"); |
| 92 | + return; |
| 93 | + } |
| 94 | + }; |
| 95 | + stream_set_timeouts(&mut stream); |
| 96 | + eprintln!("guest connected!"); |
| 97 | + stderr().flush(); |
| 98 | + stream_expect_msg(&mut stream, b"ping!"); |
| 99 | + stream_expect_wouldblock(&mut stream); |
| 100 | + stream.write_all(b"pong!").unwrap(); |
| 101 | + stream_expect_msg(&mut stream, b"bye!"); |
| 102 | + println!("OK"); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments