|
| 1 | +use crate::tcp_tester::TcpTester; |
| 2 | +use macros::{guest, host}; |
| 3 | + |
| 4 | +const PORT: u16 = 8001; |
| 5 | + |
| 6 | +pub struct TestTsiTcpGuestListen { |
| 7 | + tcp_tester: TcpTester, |
| 8 | +} |
| 9 | + |
| 10 | +impl TestTsiTcpGuestListen { |
| 11 | + pub fn new() -> Self { |
| 12 | + Self { |
| 13 | + tcp_tester: TcpTester::new(PORT), |
| 14 | + } |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +#[host] |
| 19 | +mod host { |
| 20 | + use super::*; |
| 21 | + use crate::common::setup_fs_and_enter; |
| 22 | + use crate::{krun_call, krun_call_u32, Test, TestSetup}; |
| 23 | + use krun_sys::*; |
| 24 | + use std::ffi::CString; |
| 25 | + use std::ptr::null; |
| 26 | + use std::thread; |
| 27 | + use std::time::Duration; |
| 28 | + |
| 29 | + impl Test for TestTsiTcpGuestListen { |
| 30 | + fn start_vm(self: Box<Self>, test_setup: TestSetup) -> anyhow::Result<()> { |
| 31 | + unsafe { |
| 32 | + thread::spawn(move || { |
| 33 | + thread::sleep(Duration::from_secs(1)); |
| 34 | + self.tcp_tester.run_client(); |
| 35 | + }); |
| 36 | + |
| 37 | + let ctx = krun_call_u32!(krun_create_ctx())?; |
| 38 | + let port_mapping = format!("{PORT}:{PORT}"); |
| 39 | + let port_mapping = CString::new(port_mapping).unwrap(); |
| 40 | + let port_map = [port_mapping.as_ptr(), null()]; |
| 41 | + |
| 42 | + krun_call!(krun_set_port_map(ctx, port_map.as_ptr()))?; |
| 43 | + krun_call!(krun_set_vm_config(ctx, 1, 512))?; |
| 44 | + setup_fs_and_enter(ctx, test_setup)?; |
| 45 | + println!("OK"); |
| 46 | + } |
| 47 | + Ok(()) |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +#[guest] |
| 53 | +mod guest { |
| 54 | + use super::*; |
| 55 | + use crate::Test; |
| 56 | + |
| 57 | + impl Test for TestTsiTcpGuestListen { |
| 58 | + fn in_guest(self: Box<Self>) { |
| 59 | + let listener = self.tcp_tester.create_server_socket(); |
| 60 | + self.tcp_tester.run_server(listener); |
| 61 | + println!("OK"); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments