Skip to content

Commit

Permalink
test: scatter fd_read
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed May 4, 2021
1 parent 1b10013 commit 3d9fa22
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions test/unittests/wasi_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,51 @@ TEST_F(wasi_mocked_test, fd_read)
EXPECT_EQ(instance->memory->substr(0x0c, 4), from_hex("04000000"));
}

TEST_F(wasi_mocked_test, fd_read_scatter)
{
/* wat2wasm
(func (import "wasi_snapshot_preview1" "fd_read") (param i32 i32 i32 i32) (result i32))
(memory (export "memory") 1)
(data (i32.const 0) "\10\00\00\00") ;; buf1 ptr
(data (i32.const 0x04) "\04\00\00\00") ;; buf1 len
(data (i32.const 0x08) "\14\00\00\00") ;; buf2 ptr
(data (i32.const 0x0c) "\08\00\00\00") ;; buf2 len
(data (i32.const 0x10) "\12\34\56\78") ;; buf1 data
(data (i32.const 0x14) "\11\22\33\44\55\66\77\88") ;; buf2 data
(data (i32.const 0x1c) "\de\ad\be\ef") ;; will be overwritten with nread
(func (export "_start")
(call 0
(i32.const 0) ;; fd
(i32.const 0) ;; iov_ptr
(i32.const 2) ;; iov_cnt
(i32.const 0x1c)) ;; nread_ptr
(if (i32.popcnt) (then unreachable)))
*/
const auto wasm = from_hex(
"0061736d01000000010c0260047f7f7f7f017f60000002220116776173695f736e617073686f745f7072657669"
"6577310766645f726561640000030201010503010001071302066d656d6f72790200065f737461727400010a13"
"011100410041004102411c1000690440000b0b0b44070041000b04100000000041040b04040000000041080b04"
"1400000000410c0b04080000000041100b04123456780041140b08112233445566778800411c0b04deadbeef");

auto instance = wasi::instantiate(wasm);

EXPECT_FALSE(mock_uvwasi->init_called);
EXPECT_FALSE(mock_uvwasi->read_fd.has_value());

std::ostringstream err;
EXPECT_TRUE(wasi::run(*instance, 0, nullptr, err)) << err.str();

EXPECT_TRUE(mock_uvwasi->init_called);
ASSERT_TRUE(mock_uvwasi->read_fd.has_value());
EXPECT_EQ(*mock_uvwasi->read_fd, 0);

// read data
EXPECT_EQ(instance->memory->substr(0x10, 4), mock_uvwasi->data_read.substr(0, 4));
EXPECT_EQ(instance->memory->substr(0x14, 8), mock_uvwasi->data_read.substr(4, 8));
// nread
EXPECT_EQ(instance->memory->substr(0x1c, 4), from_hex("0c000000"));
}

TEST_F(wasi_mocked_test, fd_prestat_get)
{
/* wat2wasm
Expand Down

0 comments on commit 3d9fa22

Please sign in to comment.