1 package util
2
3 import (
4 "fmt"
5 "testing"
6
7 "github.com/tetratelabs/wazero/internal/gojs/custom"
8 "github.com/tetratelabs/wazero/internal/testing/require"
9 "github.com/tetratelabs/wazero/internal/wasm"
10 )
11
12 func TestMustRead(t *testing.T) {
13 mem := &wasm.MemoryInstance{Buffer: []byte{1, 2, 3, 4, 5, 6, 7, 8}, Min: 1}
14
15 tests := []struct {
16 name string
17 funcName string
18 paramIdx int
19 offset, byteCount uint32
20 expected []byte
21 expectedPanic string
22 }{
23 {
24 name: "read nothing",
25 },
26 {
27 name: "read all",
28 offset: 0,
29 byteCount: 8,
30 expected: []byte{1, 2, 3, 4, 5, 6, 7, 8},
31 },
32 {
33 name: "read some",
34 offset: 4,
35 byteCount: 2,
36 expected: []byte{5, 6},
37 },
38 {
39 name: "read too many",
40 funcName: custom.NameSyscallCopyBytesToGo,
41 offset: 4,
42 byteCount: 5,
43 expectedPanic: "out of memory reading dst",
44 },
45 {
46 name: "read too many - function not in names",
47 funcName: "not_in_names",
48 offset: 4,
49 byteCount: 5,
50 expectedPanic: "out of memory reading not_in_names param[0]",
51 },
52 {
53 name: "read too many - in names, but no params",
54 funcName: custom.NameDebug,
55 offset: 4,
56 byteCount: 5,
57 expectedPanic: "out of memory reading debug param[0]",
58 },
59 }
60
61 for _, tt := range tests {
62 tc := tt
63
64 t.Run(tc.name, func(t *testing.T) {
65 if tc.expectedPanic != "" {
66 err := require.CapturePanic(func() {
67 MustRead(mem, tc.funcName, tc.paramIdx, tc.offset, tc.byteCount)
68 })
69 require.EqualError(t, err, tc.expectedPanic)
70 } else {
71 buf := MustRead(mem, tc.funcName, tc.paramIdx, tc.offset, tc.byteCount)
72 require.Equal(t, tc.expected, buf)
73 }
74 })
75 }
76 }
77
78 func TestResolvePath(t *testing.T) {
79 t.Parallel()
80
81 tests := []struct {
82 cwd, path string
83 expected string
84 }{
85 {cwd: "/", path: ".", expected: "/"},
86 {cwd: "/", path: "/", expected: "/"},
87 {cwd: "/", path: "..", expected: "/"},
88 {cwd: "/", path: "a", expected: "/a"},
89 {cwd: "/", path: "/a", expected: "/a"},
90 {cwd: "/", path: "./a/", expected: "/a/"},
91 {cwd: "/", path: "./a/.", expected: "/a"},
92 {cwd: "/", path: "a/.", expected: "/a"},
93 {cwd: "/a", path: "/..", expected: "/"},
94 {cwd: "/a", path: "/", expected: "/"},
95 {cwd: "/a", path: "b", expected: "/a/b"},
96 {cwd: "/a", path: "/b", expected: "/b"},
97 {cwd: "/a", path: "/b/", expected: "/b/"},
98 {cwd: "/a", path: "./b/.", expected: "/a/b"},
99 {cwd: "/a/b", path: ".", expected: "/a/b"},
100 {cwd: "/a/b", path: "../.", expected: "/a"},
101 {cwd: "/a/b", path: "../..", expected: "/"},
102 {cwd: "/a/b", path: "../../..", expected: "/"},
103 }
104
105 for _, tt := range tests {
106 tc := tt
107
108 t.Run(fmt.Sprintf("%s,%s", tc.cwd, tc.path), func(t *testing.T) {
109 require.Equal(t, tc.expected, ResolvePath(tc.cwd, tc.path))
110 })
111 }
112 }
113
View as plain text