Skip to content

Commit

Permalink
Add rust test case
Browse files Browse the repository at this point in the history
Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
  • Loading branch information
evacchi committed Aug 2, 2023
1 parent 70f38cc commit 65d5b3c
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ path = "wasi.rs"

[dependencies]
libc = "0.2"
mio = {version="0.8", features=["os-poll", "net"]}
105 changes: 105 additions & 0 deletions imports/wasi_snapshot_preview1/testdata/cargo-wasi/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ use std::net::{TcpListener};
use std::os::wasi::io::FromRawFd;
use std::process::exit;
use std::str::from_utf8;
use std::error::Error;

use std::collections::HashMap;
use std::time::Duration;

// Until NotADirectory is implemented, read the underlying error raised by
// wasi-libc. See https://github.com/rust-lang/rust/issues/86442
Expand All @@ -23,6 +27,7 @@ fn main() {
}
"stat" => main_stat(),
"sock" => main_sock(),
"mixed" => main_mixed().unwrap(),
_ => {
writeln!(io::stderr(), "unknown command: {}", args[1]).unwrap();
exit(1);
Expand Down Expand Up @@ -87,3 +92,103 @@ fn main_sock() {
}
}
}

fn main_mixed() -> Result<(), Box<dyn Error>> {

use mio::net::{TcpListener, TcpStream};
use mio::{Events, Interest, Poll, Token};

// Some tokens to allow us to identify which event is for which socket.
const SERVER: Token = Token(0);
const STDIN: Token = Token(1);

// Create a poll instance.
let mut poll = Poll::new()?;
// Create storage for events.
let mut events = Events::with_capacity(128);

let mut server = unsafe { TcpListener::from_raw_fd(3) };
let mut stdin = unsafe { TcpStream::from_raw_fd(0) };


// Start listening for incoming connections.
poll.registry()
.register(&mut server, SERVER, Interest::READABLE)?;

// Keep track of incoming connections.
let mut m: HashMap<Token, TcpStream> = HashMap::new();

let mut count = 2;

// Start an event loop.
loop {
// Poll Mio for events, blocking until we get an event.
if let Err(e) = poll.poll(&mut events, Some(Duration::from_nanos(0))) {
// Ignore EINTR.
if e.kind() == std::io::ErrorKind::Interrupted {
continue;
}
return Err(Box::from(e))
}

// Process each event.
for event in events.iter() {
// We can use the token we previously provided to `register` to
// determine for which socket the event is.
match event.token() {
SERVER => {
// If this is an event for the server, it means a connection
// is ready to be accepted.
//
// Accept the connection and add it to the map.
match server.accept() {
Ok((mut connection, _addr)) => {
let tok = Token(count);
_ = poll.registry()
.register(&mut connection, tok, Interest::READABLE);
m.insert(tok, connection);
// drop(connection);
count+=1;
},
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
// ignore
},
Err(err) => panic!("ERROR! {}", err),
}
},

STDIN => {
// There is for reading on one of our connections, read it and echo.
let mut buf = [0u8; 32];
match stdin.read(&mut buf) {
Ok(n) if n>0 =>
println!("{}", String::from_utf8_lossy(&buf[0..n])),
_ => {} // ignore error.
}
},


conn_id => {
// There is for reading on one of our connections, read it and echo.
let mut buf = [0u8; 32];
let mut el = m.get(&conn_id).unwrap();
match el.read(&mut buf) {
Ok(n) if n>0 => {
let s = String::from_utf8_lossy(&buf[0..n]);
println!("{}", s);
// Quit when the socket contains the string wazero.
if s.contains("wazero") {
return Ok(());
}
},

_ => {} // ignore error.
}
}
}
}

}
}


Binary file modified imports/wasi_snapshot_preview1/testdata/cargo-wasi/wasi.wasm
Binary file not shown.
4 changes: 2 additions & 2 deletions imports/wasi_snapshot_preview1/wasi_stdlib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,8 @@ func testLargeStdout(t *testing.T, tname string, bin []byte) {

func Test_Mixed(t *testing.T) {
toolchains := map[string][]byte{
// TODO: "cargo-wasi": wasmCargoWasi,
"zig-cc": wasmZigCc,
"cargo-wasi": wasmCargoWasi,
"zig-cc": wasmZigCc,
}
if wasmGotip != nil {
toolchains["gotip"] = wasmGotip
Expand Down

0 comments on commit 65d5b3c

Please sign in to comment.