git ssb

0+

wanderer🌟 / js-primea-annotations



Tree: 37c7f336bf09edb2faf7bbc393a9c945e1524224

Files: 37c7f336bf09edb2faf7bbc393a9c945e1524224 / tests / index.js

4129 bytesRaw
1const tape = require('tape')
2const fs = require('fs')
3const wabt = require('wabt')
4const {wasm2json} = require('wasm-json-toolkit')
5const types = require('../')
6
7tape('basic', t => {
8 const wat = fs.readFileSync(`${__dirname}/wast/caller.wast`).toString()
9 const json = JSON.parse(fs.readFileSync(`${__dirname}/wast/caller.json`))
10 const mod = wabt.parseWat('module.wast', wat)
11
12 const r = mod.toBinary({
13 log: true
14 })
15 let binary = Buffer.from(r.buffer)
16 binary = types.encodeAndInject(json, binary)
17 const moduleJSON = wasm2json(binary)
18
19 const mergedJson = types.mergeTypeSections(moduleJSON)
20 const expectedJson = {
21 'types': [{
22 'form': 'func',
23 'params': [
24 'func'
25 ]
26 }, {
27 'form': 'func',
28 'params': [
29 'i32'
30 ]
31 }],
32 'indexes': {
33 '1': 0,
34 '2': 1
35 },
36 'exports': {
37 'call': 1
38 }
39 }
40
41 t.deepEquals(mergedJson, expectedJson)
42 t.end()
43})
44
45tape('empty', t => {
46 const wat = fs.readFileSync(`${__dirname}/wast/empty.wast`).toString()
47 const mod = wabt.parseWat('module.wast', wat)
48
49 const r = mod.toBinary({
50 log: true
51 })
52 let binary = Buffer.from(r.buffer)
53 const moduleJSON = wasm2json(binary)
54
55 const mergedJson = types.mergeTypeSections(moduleJSON)
56 const expectedJson = {
57 'types': [],
58 'indexes': {},
59 'exports': {}
60 }
61
62 t.deepEquals(mergedJson, expectedJson)
63 t.end()
64})
65
66
67tape('invalid function type', t => {
68 const wat = fs.readFileSync(`${__dirname}/wast/invalid.wast`).toString()
69 const mod = wabt.parseWat('module.wast', wat)
70 const r = mod.toBinary({
71 log: true
72 })
73 let binary = Buffer.from(r.buffer)
74 try {
75 const moduleJSON = wasm2json(binary)
76 types.mergeTypeSections(moduleJSON)
77 } catch (e) {
78 t.pass('should invaldate function with return types')
79 t.end()
80 }
81})
82
83tape('invalid function signature', t => {
84 t.plan(1)
85 const wat = fs.readFileSync(`${__dirname}/wast/globals.wast`).toString()
86 const json = JSON.parse(fs.readFileSync(`${__dirname}/wast/invalid.json`))
87 const mod = wabt.parseWat('module.wast', wat)
88
89 const buf = types.encode(json)
90 const r = mod.toBinary({
91 log: true
92 })
93 let binary = Buffer.from(r.buffer)
94 binary = types.injectCustomSection(buf, binary)
95
96 try {
97 const moduleJSON = wasm2json(binary)
98 types.mergeTypeSections(moduleJSON)
99 } catch (e) {
100 t.pass('should invalidate function sings that dont match')
101 }
102})
103
104tape('invalid function signature, wrong base type', t => {
105 t.plan(1)
106 const wat = fs.readFileSync(`${__dirname}/wast/invalidBaseType.wast`).toString()
107 const json = JSON.parse(fs.readFileSync(`${__dirname}/wast/caller.json`))
108 const mod = wabt.parseWat('module.wast', wat)
109
110 const buf = types.encode(json)
111 const r = mod.toBinary({
112 log: true
113 })
114 let binary = Buffer.from(r.buffer)
115 binary = types.injectCustomSection(buf, binary)
116
117 try {
118 const moduleJSON = wasm2json(binary)
119 types.mergeTypeSections(moduleJSON)
120 } catch (e) {
121 t.pass('should invalidate function sings that dont match')
122 }
123})
124
125tape('invalid type encoding', t => {
126 t.plan(3)
127 const json = JSON.parse(fs.readFileSync(`${__dirname}/wast/caller.json`))
128 let buf = types.encodeType(json.types)
129 const invalidBuf = Buffer.concat([buf, Buffer.from([0])])
130 try {
131 types.decodeType(invalidBuf)
132 } catch (e) {
133 t.pass('should catch invalid type encodings')
134 }
135
136 try {
137 const invalidForm = Buffer.from(buf)
138 invalidForm[1] = 0
139 types.decodeType(invalidForm)
140 } catch (e) {
141 t.pass('should catch invalid type encoding, invild form')
142 }
143
144 try {
145 const invalidParam = Buffer.from(buf)
146 invalidParam[3] = 77
147 types.decodeType(invalidParam)
148 } catch (e) {
149 t.pass('should catch invalid type encoding, invild param')
150 }
151})
152
153tape('invalid typeMap encoding', t => {
154 t.plan(1)
155 const json = JSON.parse(fs.readFileSync(`${__dirname}/wast/caller.json`))
156 let buf = types.encodeTypeMap(json.typeMap)
157 const invalidBuf = Buffer.concat([buf, Buffer.from([0])])
158 try {
159 types.decodeTypeMap(invalidBuf)
160 } catch (e) {
161 t.pass('should catch invalid typeMap encodings')
162 }
163})
164

Built with git-ssb-web