forked from tetratelabs/wazero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock_test.go
297 lines (261 loc) · 7.93 KB
/
clock_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package wasi_snapshot_preview1_test
import (
_ "embed"
"testing"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/internal/testing/require"
"github.com/tetratelabs/wazero/internal/wasip1"
)
func Test_clockResGet(t *testing.T) {
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig())
defer r.Close(testCtx)
expectedMemoryMicro := []byte{
'?', // resultResolution is after this
0xe8, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // little endian-encoded resolution (fixed to 1000).
'?', // stopped after encoding
}
expectedMemoryNano := []byte{
'?', // resultResolution is after this
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // little endian-encoded resolution (fixed to 1000).
'?', // stopped after encoding
}
tests := []struct {
name string
clockID uint32
expectedMemory []byte
expectedLog string
}{
{
name: "Realtime",
clockID: wasip1.ClockIDRealtime,
expectedMemory: expectedMemoryMicro,
expectedLog: `
==> wasi_snapshot_preview1.clock_res_get(id=realtime)
<== (resolution=1000,errno=ESUCCESS)
`,
},
{
name: "Monotonic",
clockID: wasip1.ClockIDMonotonic,
expectedMemory: expectedMemoryNano,
expectedLog: `
==> wasi_snapshot_preview1.clock_res_get(id=monotonic)
<== (resolution=1,errno=ESUCCESS)
`,
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
defer log.Reset()
resultResolution := 16 // arbitrary offset
maskMemory(t, mod, resultResolution+len(tc.expectedMemory))
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.ClockResGetName, uint64(tc.clockID), uint64(resultResolution))
require.Equal(t, tc.expectedLog, "\n"+log.String())
actual, ok := mod.Memory().Read(uint32(resultResolution-1), uint32(len(tc.expectedMemory)))
require.True(t, ok)
require.Equal(t, tc.expectedMemory, actual)
})
}
}
func Test_clockResGet_Unsupported(t *testing.T) {
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig())
defer r.Close(testCtx)
tests := []struct {
name string
clockID uint32
expectedErrno wasip1.Errno
expectedLog string
}{
{
name: "process cputime",
clockID: 2,
expectedErrno: wasip1.ErrnoInval,
expectedLog: `
==> wasi_snapshot_preview1.clock_res_get(id=2)
<== (resolution=,errno=EINVAL)
`,
},
{
name: "thread cputime",
clockID: 3,
expectedErrno: wasip1.ErrnoInval,
expectedLog: `
==> wasi_snapshot_preview1.clock_res_get(id=3)
<== (resolution=,errno=EINVAL)
`,
},
{
name: "undefined",
clockID: 100,
expectedErrno: wasip1.ErrnoInval,
expectedLog: `
==> wasi_snapshot_preview1.clock_res_get(id=100)
<== (resolution=,errno=EINVAL)
`,
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
defer log.Reset()
resultResolution := 16 // arbitrary offset
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.ClockResGetName, uint64(tc.clockID), uint64(resultResolution))
require.Equal(t, tc.expectedLog, "\n"+log.String())
})
}
}
func Test_clockTimeGet(t *testing.T) {
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig())
defer r.Close(testCtx)
tests := []struct {
name string
clockID uint32
expectedMemory []byte
expectedLog string
}{
{
name: "Realtime",
clockID: wasip1.ClockIDRealtime,
expectedMemory: []byte{
'?', // resultTimestamp is after this
0x0, 0x0, 0x1f, 0xa6, 0x70, 0xfc, 0xc5, 0x16, // little endian-encoded epochNanos
'?', // stopped after encoding
},
expectedLog: `
==> wasi_snapshot_preview1.clock_time_get(id=realtime,precision=0)
<== (timestamp=1640995200000000000,errno=ESUCCESS)
`,
},
{
name: "Monotonic",
clockID: wasip1.ClockIDMonotonic,
expectedMemory: []byte{
'?', // resultTimestamp is after this
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // fake nanotime starts at zero
'?', // stopped after encoding
},
expectedLog: `
==> wasi_snapshot_preview1.clock_time_get(id=monotonic,precision=0)
<== (timestamp=0,errno=ESUCCESS)
`,
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
defer log.Reset()
resultTimestamp := 16 // arbitrary offset
maskMemory(t, mod, resultTimestamp+len(tc.expectedMemory))
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.ClockTimeGetName, uint64(tc.clockID), 0 /* TODO: precision */, uint64(resultTimestamp))
require.Equal(t, tc.expectedLog, "\n"+log.String())
actual, ok := mod.Memory().Read(uint32(resultTimestamp-1), uint32(len(tc.expectedMemory)))
require.True(t, ok)
require.Equal(t, tc.expectedMemory, actual)
})
}
}
// Similar to https://github.com/WebAssembly/wasi-testsuite/blob/dc7f8d27be1030cd4788ebdf07d9b57e5d23441e/tests/c/testsuite/clock_gettime-monotonic.c
func Test_clockTimeGet_monotonic(t *testing.T) {
mod, r, _ := requireProxyModule(t, wazero.NewModuleConfig().
// Important not to use fake time!
WithSysNanotime())
defer r.Close(testCtx)
getMonotonicTime := func() uint64 {
const offset uint32 = 0
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.ClockTimeGetName, uint64(wasip1.ClockIDMonotonic),
0 /* TODO: precision */, uint64(offset))
timestamp, ok := mod.Memory().ReadUint64Le(offset)
require.True(t, ok)
return timestamp
}
t1 := getMonotonicTime()
t2 := getMonotonicTime()
t3 := getMonotonicTime()
t4 := getMonotonicTime()
require.True(t, t1 < t2)
require.True(t, t2 < t3)
require.True(t, t3 < t4)
}
func Test_clockTimeGet_Unsupported(t *testing.T) {
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig())
defer r.Close(testCtx)
tests := []struct {
name string
clockID uint32
expectedErrno wasip1.Errno
expectedLog string
}{
{
name: "process cputime",
clockID: 2,
expectedErrno: wasip1.ErrnoInval,
expectedLog: `
==> wasi_snapshot_preview1.clock_time_get(id=2,precision=0)
<== (timestamp=,errno=EINVAL)
`,
},
{
name: "thread cputime",
clockID: 3,
expectedErrno: wasip1.ErrnoInval,
expectedLog: `
==> wasi_snapshot_preview1.clock_time_get(id=3,precision=0)
<== (timestamp=,errno=EINVAL)
`,
},
{
name: "undefined",
clockID: 100,
expectedErrno: wasip1.ErrnoInval,
expectedLog: `
==> wasi_snapshot_preview1.clock_time_get(id=100,precision=0)
<== (timestamp=,errno=EINVAL)
`,
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
defer log.Reset()
resultTimestamp := 16 // arbitrary offset
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.ClockTimeGetName, uint64(tc.clockID), uint64(0) /* TODO: precision */, uint64(resultTimestamp))
require.Equal(t, tc.expectedLog, "\n"+log.String())
})
}
}
func Test_clockTimeGet_Errors(t *testing.T) {
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig())
defer r.Close(testCtx)
memorySize := mod.Memory().Size()
tests := []struct {
name string
resultTimestamp, argvLen uint32
expectedLog string
}{
{
name: "resultTimestamp OOM",
resultTimestamp: memorySize,
expectedLog: `
==> wasi_snapshot_preview1.clock_time_get(id=realtime,precision=0)
<== (timestamp=,errno=EFAULT)
`,
},
{
name: "resultTimestamp exceeds the maximum valid address by 1",
resultTimestamp: memorySize - 4 + 1, // 4 is the size of uint32, the type of the count of args
expectedLog: `
==> wasi_snapshot_preview1.clock_time_get(id=realtime,precision=0)
<== (timestamp=,errno=EFAULT)
`,
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
defer log.Reset()
requireErrnoResult(t, wasip1.ErrnoFault, mod, wasip1.ClockTimeGetName, uint64(0) /* TODO: id */, uint64(0) /* TODO: precision */, uint64(tc.resultTimestamp))
require.Equal(t, tc.expectedLog, "\n"+log.String())
})
}
}