Files
wazero/internal/gojs/util/util_test.go
Crypt Keeper 8464474e21 gojs: adds support for uid and gid (#1245)
This adds `gojs.WithOSUser` which passes through current user IDs so
that GOOS=js compiled wasm can read them. This also adds support for
reading back the uid and gid on files. In summary, this passes
`os.TestChown` except on windows where it will not work due to lack of
support.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-03-16 11:07:27 +08:00

113 lines
2.8 KiB
Go

package util
import (
"fmt"
"testing"
"github.com/tetratelabs/wazero/internal/gojs/custom"
"github.com/tetratelabs/wazero/internal/testing/require"
"github.com/tetratelabs/wazero/internal/wasm"
)
func TestMustRead(t *testing.T) {
mem := &wasm.MemoryInstance{Buffer: []byte{1, 2, 3, 4, 5, 6, 7, 8}, Min: 1}
tests := []struct {
name string
funcName string
paramIdx int
offset, byteCount uint32
expected []byte
expectedPanic string
}{
{
name: "read nothing",
},
{
name: "read all",
offset: 0,
byteCount: 8,
expected: []byte{1, 2, 3, 4, 5, 6, 7, 8},
},
{
name: "read some",
offset: 4,
byteCount: 2,
expected: []byte{5, 6},
},
{
name: "read too many",
funcName: custom.NameSyscallCopyBytesToGo,
offset: 4,
byteCount: 5,
expectedPanic: "out of memory reading dst",
},
{
name: "read too many - function not in names",
funcName: "not_in_names",
offset: 4,
byteCount: 5,
expectedPanic: "out of memory reading not_in_names param[0]",
},
{
name: "read too many - in names, but no params",
funcName: custom.NameDebug,
offset: 4,
byteCount: 5,
expectedPanic: "out of memory reading debug param[0]",
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
if tc.expectedPanic != "" {
err := require.CapturePanic(func() {
MustRead(mem, tc.funcName, tc.paramIdx, tc.offset, tc.byteCount)
})
require.EqualError(t, err, tc.expectedPanic)
} else {
buf := MustRead(mem, tc.funcName, tc.paramIdx, tc.offset, tc.byteCount)
require.Equal(t, tc.expected, buf)
}
})
}
}
func TestResolvePath(t *testing.T) {
t.Parallel()
tests := []struct {
cwd, path string
expected string
}{
{cwd: "/", path: ".", expected: "/"},
{cwd: "/", path: "/", expected: "/"},
{cwd: "/", path: "..", expected: "/"},
{cwd: "/", path: "a", expected: "/a"},
{cwd: "/", path: "/a", expected: "/a"},
{cwd: "/", path: "./a/", expected: "/a/"}, // retain trailing slash
{cwd: "/", path: "./a/.", expected: "/a"},
{cwd: "/", path: "a/.", expected: "/a"},
{cwd: "/a", path: "/..", expected: "/"},
{cwd: "/a", path: "/", expected: "/"},
{cwd: "/a", path: "b", expected: "/a/b"},
{cwd: "/a", path: "/b", expected: "/b"},
{cwd: "/a", path: "/b/", expected: "/b/"}, // retain trailing slash
{cwd: "/a", path: "./b/.", expected: "/a/b"},
{cwd: "/a/b", path: ".", expected: "/a/b"},
{cwd: "/a/b", path: "../.", expected: "/a"},
{cwd: "/a/b", path: "../..", expected: "/"},
{cwd: "/a/b", path: "../../..", expected: "/"},
}
for _, tt := range tests {
tc := tt
t.Run(fmt.Sprintf("%s,%s", tc.cwd, tc.path), func(t *testing.T) {
require.Equal(t, tc.expected, ResolvePath(tc.cwd, tc.path))
})
}
}