-
Notifications
You must be signed in to change notification settings - Fork 2
/
metering_test.go
104 lines (92 loc) · 2.97 KB
/
metering_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
package metering
import (
"bytes"
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"github.com/yyh1102/go-wasm-metering/test"
"github.com/yyh1102/go-wasm-metering/toolkit"
"io/ioutil"
"path"
"testing"
)
func readCostTable(path string) (toolkit.JSON, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
jsonObj := make(toolkit.JSON)
err = json.Unmarshal(data, &jsonObj)
if err != nil {
return nil, err
}
return jsonObj, nil
}
func readWasmModule(path string) ([]toolkit.JSON, error) {
var jsonArr []toolkit.JSON
jsonData, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
if err = json.Unmarshal(jsonData, &jsonArr); err != nil {
return nil, err
}
return jsonArr, nil
}
func TestBasic(t *testing.T) {
wasm, err := ioutil.ReadFile(path.Join("test", "in", "wasm", "basic.wasm"))
assert.Nil(t, err)
meteredWasm, _, err := MeterWASM(wasm, &Options{
CostTable: test.DefaultCostTable,
})
assert.Nil(t, err)
meteredJson := toolkit.Wasm2Json(meteredWasm)
expectedWasm, err := ioutil.ReadFile(path.Join("test", "expected-out", "wasm", "basic.wasm"))
assert.Nil(t, err)
expectedJson := toolkit.Wasm2Json(expectedWasm)
//fmt.Printf("%#v\n%#v\n", meteredJson, expectedJson)
assert.Equal(t, true, assert.ObjectsAreEqual(meteredJson, expectedJson))
assert.Equal(t, 0, bytes.Compare(meteredWasm, expectedWasm))
//entries1 := meteredJson[1]["entries"].([]toolkit.TypeEntry)
//entries2 := meteredJson[2]["entries"].([]toolkit.TypeEntry)
//assert.Equal(t, "metering", entries2[0]["module_str"])
//assert.Equal(t, "usegas", entries2[0]["field_str"].(string))
//assert.Equal(t, "i32", entries1[1]["params"].([]interface{})[0].(string))
}
func TestBasicMeteringTests(t *testing.T) {
dirName := path.Join("test", "in")
dir, err := ioutil.ReadDir(path.Join(dirName, "wasm"))
assert.Nil(t, err)
for _, file := range dir {
// read wasm json.
wasm, err := ioutil.ReadFile(path.Join(dirName, "wasm", file.Name()))
assert.Nil(t, err)
module := toolkit.Wasm2Json(wasm)
// read cost table json.
metering := Metering{
opts: Options{
CostTable: test.DefaultCostTable,
ModuleStr: defaultModuleStr,
FieldStr: defaultFieldStr,
MeterType: defaultMeterType,
},
}
//fmt.Printf("%s %#v\n", file.Name(), module)
meteredModule, _, err := metering.meterJSON(module)
if err != nil {
assert.Equal(t, "basic+import.wasm", file.Name())
continue
}
//fmt.Printf("%s old %#v\n", file.Name(), meteredModule)
//fmt.Printf("Gas: %v\n", gasCost)
expectedWasm, err := ioutil.ReadFile(path.Join("test", "expected-out", "wasm", file.Name()))
assert.Nil(t, err)
expectedJson := toolkit.Wasm2Json(expectedWasm)
//fmt.Printf("%s exp %#v\n", file.Name(), expectedJson)
if !assert.Equal(t, true, assert.ObjectsAreEqual(meteredModule, expectedJson)) {
fmt.Printf("file name %s\n", file.Name())
fmt.Printf("%#v\n%#v\n", meteredModule, expectedJson)
}
}
//fmt.Printf("Basic metering tests failed cases %d", failed)
}