forked from bytecodealliance/wasmtime-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_test.go
121 lines (99 loc) · 2.53 KB
/
store_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
package wasmtime
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestStore(t *testing.T) {
engine := NewEngine()
NewStore(engine)
}
func TestInterruptWasm(t *testing.T) {
config := NewConfig()
config.SetEpochInterruption(true)
store := NewStore(NewEngineWithConfig(config))
store.SetEpochDeadline(1)
wasm, err := Wat2Wasm(`
(import "" "" (func))
(func
call 0
(loop br 0))
(start 1)
`)
require.NoError(t, err)
module, err := NewModule(store.Engine, wasm)
require.NoError(t, err)
engine := store.Engine
f := WrapFunc(store, func() {
engine.IncrementEpoch()
})
instance, err := NewInstance(store, module, []AsExtern{f})
require.Nil(t, instance)
require.Error(t, err)
trap := err.(*Trap)
require.NotNil(t, trap)
}
func TestFuelConsumed(t *testing.T) {
engine := NewEngine()
store := NewStore(engine)
fuel, enable := store.FuelConsumed()
require.False(t, enable)
require.Equal(t, fuel, uint64(0))
}
func TestAddFuel(t *testing.T) {
config := NewConfig()
config.SetConsumeFuel(true)
engine := NewEngineWithConfig(config)
store := NewStore(engine)
fuel, enable := store.FuelConsumed()
require.True(t, enable)
require.Equal(t, fuel, uint64(0))
const add_fuel = 3
err := store.AddFuel(add_fuel)
require.NoError(t, err)
}
func TestConsumeFuel(t *testing.T) {
config := NewConfig()
config.SetConsumeFuel(true)
engine := NewEngineWithConfig(config)
store := NewStore(engine)
fuel, enable := store.FuelConsumed()
require.True(t, enable)
require.Equal(t, fuel, uint64(0))
const add_fuel = 3
err := store.AddFuel(add_fuel)
require.NoError(t, err)
consume_fuel := uint64(1)
remaining, err := store.ConsumeFuel(consume_fuel)
require.NoError(t, err)
require.Equal(t, (add_fuel - consume_fuel), remaining)
}
func TestLimiterMemorySizeFail(t *testing.T) {
engine := NewEngine()
store := NewStore(engine)
store.Limiter(2*64*1024, -1, -1, -1, -1)
wasm, err := Wat2Wasm(`
(module
(memory 3)
)
`)
require.NoError(t, err)
module, err := NewModule(store.Engine, wasm)
require.NoError(t, err)
_, err = NewInstance(store, module, []AsExtern{})
require.Error(t, err, "memory minimum size of 3 pages exceeds memory limits")
}
func TestLimiterMemorySizeSuccess(t *testing.T) {
engine := NewEngine()
store := NewStore(engine)
store.Limiter(4*64*1024, -1, -1, -1, -1)
wasm, err := Wat2Wasm(`
(module
(memory 3)
)
`)
require.NoError(t, err)
module, err := NewModule(store.Engine, wasm)
require.NoError(t, err)
_, err = NewInstance(store, module, []AsExtern{})
require.NoError(t, err)
}