wasi: enforce fd_preXXX functions only work on pre-opened files (#919)

At the moment, the only pre-open support we have is the file system
itself (root a.k.a. / or file-descriptor 3). We may in the future add
the ability to pre-open sockets, but in any case, this is where we are
today.

This change hardens logic around fd_preXXX functions, ensuring they only
work on actual pre-opens. This also fixes the path returned in filestat
as we sometimes returned a full path, when typically the basename is the
only part that can be returned.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Crypt Keeper
2022-12-13 16:42:53 +09:00
committed by GitHub
parent 3b70504f3f
commit 2e13f57f56
7 changed files with 148 additions and 66 deletions

View File

@@ -4,8 +4,10 @@ import (
"context"
"embed"
"errors"
"io"
"io/fs"
"os"
"path"
"testing"
"testing/fstest"
@@ -19,13 +21,6 @@ var testCtx = context.WithValue(context.Background(), struct{}{}, "arbitrary")
//go:embed testdata
var testdata embed.FS
var testFS = fstest.MapFS{
"empty.txt": {},
"test.txt": {Data: []byte("animals\n")},
"sub": {Mode: fs.ModeDir},
"sub/test.txt": {Data: []byte("greet sub dir\n")},
}
func TestNewFSContext(t *testing.T) {
embedFS, err := fs.Sub(testdata, "testdata")
require.NoError(t, err)
@@ -49,11 +44,7 @@ func TestNewFSContext(t *testing.T) {
},
{
name: "fstest.MapFS",
fs: testFS,
},
{
name: "fstest.MapFS",
fs: testFS,
fs: fstest.MapFS{},
},
}
@@ -66,7 +57,7 @@ func TestNewFSContext(t *testing.T) {
defer fsc.Close(testCtx)
require.Equal(t, tc.fs, fsc.fs)
require.Equal(t, "/", fsc.openedFiles[FdRoot].Path)
require.Equal(t, "/", fsc.openedFiles[FdRoot].Name)
rootFile := fsc.openedFiles[FdRoot].File
require.NotNil(t, rootFile)
@@ -118,6 +109,65 @@ func TestEmptyFSContext(t *testing.T) {
})
}
func TestContext_File(t *testing.T) {
embedFS, err := fs.Sub(testdata, "testdata")
require.NoError(t, err)
fsc, err := NewFSContext(embedFS)
require.NoError(t, err)
defer fsc.Close(testCtx)
tests := []struct {
name string
expected string
}{
{
name: "empty.txt",
},
{
name: "test.txt",
expected: "animals\n",
},
{
name: "sub/test.txt",
expected: "greet sub dir\n",
},
{
name: "sub/sub/test.txt",
expected: "greet sub sub dir\n",
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(b *testing.T) {
fd, err := fsc.OpenFile(testCtx, tc.name)
require.NoError(t, err)
defer fsc.CloseFile(testCtx, fd)
f, ok := fsc.OpenedFile(testCtx, fd)
require.True(t, ok)
stat, err := f.File.Stat()
require.NoError(t, err)
// Ensure the name is the basename and matches the stat name.
require.Equal(t, path.Base(tc.name), f.Name)
require.Equal(t, f.Name, stat.Name())
buf := make([]byte, stat.Size())
size, err := f.File.Read(buf)
if err != nil {
require.Equal(t, io.EOF, err)
}
require.Equal(t, stat.Size(), int64(size))
require.Equal(t, tc.expected, string(buf[:size]))
})
}
}
func TestContext_Close(t *testing.T) {
fsc, err := NewFSContext(testfs.FS{"foo": &testfs.File{}})
require.NoError(t, err)