From 03ab5a97ada523782c2560b9c787d6d5350f8a98 Mon Sep 17 00:00:00 2001 From: Crypt Keeper <64215+codefromthecrypt@users.noreply.github.com> Date: Fri, 17 Mar 2023 08:34:39 +0800 Subject: [PATCH] Removes partially completed fstest (#1247) Before, we didn't have a suite of tests for our filesystem used by ABI, now we do (`sysfs`). Moreover, we are testing stdlibs and wasi-testsuite on commit. We don't need this fstest anymore. Signed-off-by: Adrian Cole --- internal/integration_test/fs/fs_test.go | 186 ------------------ .../integration_test/fs/testdata/animals.txt | 5 - internal/integration_test/fs/testdata/fs.wasm | Bin 440 -> 0 bytes internal/integration_test/fs/testdata/fs.wat | 30 --- 4 files changed, 221 deletions(-) delete mode 100644 internal/integration_test/fs/fs_test.go delete mode 100644 internal/integration_test/fs/testdata/animals.txt delete mode 100644 internal/integration_test/fs/testdata/fs.wasm delete mode 100644 internal/integration_test/fs/testdata/fs.wat diff --git a/internal/integration_test/fs/fs_test.go b/internal/integration_test/fs/fs_test.go deleted file mode 100644 index fba21e15..00000000 --- a/internal/integration_test/fs/fs_test.go +++ /dev/null @@ -1,186 +0,0 @@ -package fs - -import ( - "context" - _ "embed" - "fmt" - "io" - "io/fs" - "testing" - "testing/iotest" - - "github.com/tetratelabs/wazero" - "github.com/tetratelabs/wazero/api" - "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" - "github.com/tetratelabs/wazero/internal/fstest" - "github.com/tetratelabs/wazero/internal/testing/require" - . "github.com/tetratelabs/wazero/internal/wasi_snapshot_preview1" -) - -var testCtx = context.Background() - -//go:embed testdata/animals.txt -var animals []byte - -// fsWasm was generated by the following: -// -// cd testdata; wat2wasm --debug-names fs.wat -// -//go:embed testdata/fs.wasm -var fsWasm []byte - -// wasiFs is an implementation of fs.Fs calling into wasiWASI. Not thread-safe because we use -// fixed Memory offsets for transferring data with wasm. -type wasiFs struct { - t *testing.T - - wasm wazero.Runtime - memory api.Memory - - workdirFd uint32 - - pathOpen api.Function - fdClose api.Function - fdRead api.Function - fdSeek api.Function -} - -func (fs *wasiFs) Open(name string) (fs.File, error) { - pathBytes := []byte(name) - // Pick anywhere in memory to write the path to. - pathPtr := uint32(0) - ok := fs.memory.Write(pathPtr, pathBytes) - require.True(fs.t, ok) - resultOpenedFd := pathPtr + uint32(len(pathBytes)) - - fd := fs.workdirFd - dirflags := uint32(0) // arbitrary dirflags - pathLen := len(pathBytes) - oflags := uint32(0) // arbitrary oflags - // rights are ignored per https://github.com/WebAssembly/WASI/issues/469#issuecomment-1045251844 - fsRightsBase, fsRightsInheriting := uint64(1), uint64(2) - fdflags := uint32(0) // arbitrary fdflags - res, err := fs.pathOpen.Call( - testCtx, - uint64(fd), uint64(dirflags), uint64(pathPtr), uint64(pathLen), uint64(oflags), - fsRightsBase, fsRightsInheriting, uint64(fdflags), uint64(resultOpenedFd)) - require.NoError(fs.t, err) - require.Equal(fs.t, uint64(ErrnoSuccess), res[0]) - - resFd, ok := fs.memory.ReadUint32Le(resultOpenedFd) - require.True(fs.t, ok) - - return &wasiFile{fd: resFd, fs: fs}, nil -} - -// wasiFile implements io.Reader and io.Seeker using wasiWASI functions. It does not -// implement io.ReaderAt because there is no wasiWASI function for directly reading -// from an offset. -type wasiFile struct { - fd uint32 - fs *wasiFs -} - -func (f *wasiFile) Stat() (fs.FileInfo, error) { - // We currently don't implement wasi's fd_stat but also don't use this method from this test. - panic("unused") -} - -func (f *wasiFile) Read(bytes []byte) (int, error) { - // Pick anywhere in memory for wasm to write resultSize too. We do this first since it's fixed length - // while iovs is variable. - resultSizeOff := uint32(0) - // next place iovs - iovsOff := uint32(4) - // We do not directly write to hardware, there is no need for more than one iovec - iovsCount := uint32(1) - // iov starts at iovsOff + 8 because we first write four bytes for the offset itself, and - // four bytes for the length of the iov. - iovOff := iovsOff + uint32(8) - ok := f.fs.memory.WriteUint32Le(iovsOff, iovOff) - require.True(f.fs.t, ok) - // next write the length. - ok = f.fs.memory.WriteUint32Le(iovsOff+uint32(4), uint32(len(bytes))) - require.True(f.fs.t, ok) - - res, err := f.fs.fdRead.Call(testCtx, uint64(f.fd), uint64(iovsOff), uint64(iovsCount), uint64(resultSizeOff)) - require.NoError(f.fs.t, err) - - require.NotEqual(f.fs.t, uint64(ErrnoFault), res[0]) - - numRead, ok := f.fs.memory.ReadUint32Le(resultSizeOff) - require.True(f.fs.t, ok) - - if numRead == 0 { - if len(bytes) == 0 { - return 0, nil - } - if Errno(res[0]) == ErrnoSuccess { - return 0, io.EOF - } else { - return 0, fmt.Errorf("could not read from file") - } - } - - buf, ok := f.fs.memory.Read(iovOff, numRead) - require.True(f.fs.t, ok) - copy(bytes, buf) - return int(numRead), nil -} - -func (f *wasiFile) Close() error { - res, err := f.fs.fdClose.Call(testCtx, uint64(f.fd)) - require.NoError(f.fs.t, err) - require.NotEqual(f.fs.t, uint64(ErrnoFault), res[0]) - return nil -} - -func (f *wasiFile) Seek(offset int64, whence int) (int64, error) { - // Pick anywhere in memory for wasm to write the result newOffset to - resultNewoffsetOff := uint32(0) - - res, err := f.fs.fdSeek.Call(testCtx, uint64(f.fd), uint64(offset), uint64(whence), uint64(resultNewoffsetOff)) - require.NoError(f.fs.t, err) - require.NotEqual(f.fs.t, uint64(ErrnoFault), res[0]) - - newOffset, ok := f.fs.memory.ReadUint32Le(resultNewoffsetOff) - require.True(f.fs.t, ok) - - return int64(newOffset), nil -} - -func TestReader(t *testing.T) { - r := wazero.NewRuntime(testCtx) - defer r.Close(testCtx) - - wasi_snapshot_preview1.MustInstantiate(testCtx, r) - - sys := wazero.NewModuleConfig().WithFS(fstest.FS) - - // Create a module that just delegates to wasi functions. - mod, err := r.InstantiateWithConfig(testCtx, fsWasm, sys) - require.NoError(t, err) - - pathOpen := mod.ExportedFunction("path_open") - fdClose := mod.ExportedFunction("fd_close") - fdRead := mod.ExportedFunction("fd_read") - fdSeek := mod.ExportedFunction("fd_seek") - - wasiFs := &wasiFs{ - t: t, - wasm: r, - memory: mod.Memory(), - workdirFd: uint32(3), - pathOpen: pathOpen, - fdClose: fdClose, - fdRead: fdRead, - fdSeek: fdSeek, - } - - f, err := wasiFs.Open("animals.txt") - require.NoError(t, err) - defer f.Close() - - err = iotest.TestReader(f, animals) - require.NoError(t, err) -} diff --git a/internal/integration_test/fs/testdata/animals.txt b/internal/integration_test/fs/testdata/animals.txt deleted file mode 100644 index b5b2f23d..00000000 --- a/internal/integration_test/fs/testdata/animals.txt +++ /dev/null @@ -1,5 +0,0 @@ -bear -cat -shark -dinosaur -human diff --git a/internal/integration_test/fs/testdata/fs.wasm b/internal/integration_test/fs/testdata/fs.wasm deleted file mode 100644 index 2267993c7870fc2b122a4fe37e8ee5ffd7efd4ee..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 440 zcmaJ-v2MaJ5WTZ62@q&AFUi;aNEyb|V8_s^u&%Wme z#_Pk^@XVheWkG;q>^N4nDemUeZBUV4EaNU+t;&{L+g}7M-f0aA z6e^*lB2}nTRch8iU5<2v^)jC};vUEv0(GWkXr5CX2e0|Z*?#c&N^tXn*@~E)$jJzO Z5IhYR$2v!ctAwYV#Bzc$6q!-{#vd#KejNY+ diff --git a/internal/integration_test/fs/testdata/fs.wat b/internal/integration_test/fs/testdata/fs.wat deleted file mode 100644 index d74b3bb9..00000000 --- a/internal/integration_test/fs/testdata/fs.wat +++ /dev/null @@ -1,30 +0,0 @@ -(module $wasi_fs_exports - (type $type_path_open (func (param $fd i32) (param $dirflags i32) (param $path i32) (param $path_len i32) (param $oflags i32) (param $fs_rights_base i64) (param $fs_rights_inheriting i64) (param $fdflags i32) (param $result.opened_fd i32) (result (;errno;) i32))) - (type $type_fd_close (func (param $fd i32) (result (;errno;) i32))) - (type $type_fd_read (func (param $fd i32) (param $iovs i32) (param $iovs_len i32) (param $result.size i32) (result (;errno;) i32))) - (type $type_fd_seek (func (param $fd i32) (param $offset i64) (param $whence i32) (param $result.newoffset i32) (result (;errno;) i32))) - - (import "wasi_snapshot_preview1" "path_open" (func $path_open (type $type_path_open))) - (import "wasi_snapshot_preview1" "fd_close" (func $fd_close (type $type_fd_close))) - (import "wasi_snapshot_preview1" "fd_read" (func $fd_read (type $type_fd_read))) - (import "wasi_snapshot_preview1" "fd_seek" (func $fd_seek (type $type_fd_seek))) - - (func (export "path_open") (type $type_path_open) - local.get 0 local.get 1 local.get 2 local.get 3 local.get 4 local.get 5 local.get 6 local.get 7 local.get 8 - call $path_open - ) - (func (export "fd_close") (type $type_fd_close) - local.get 0 - call $fd_close - ) - (func (export "fd_read") (type $type_fd_read) - local.get 0 local.get 1 local.get 2 local.get 3 - call $fd_read - ) - (func (export "fd_seek") (type $type_fd_seek) - local.get 0 local.get 1 local.get 2 local.get 3 - call $fd_seek - ) - - (memory (export "memory") 1 1) ;; memory is required for WASI -)