git ssb

0+

wanderer🌟 / js-primea-wasm-container



Tree: 4909b17957f15afad22524b52ef87280808c9990

Files: 4909b17957f15afad22524b52ef87280808c9990 / tests / index.js

4860 bytesRaw
1const tape = require('tape')
2const fs = require('fs')
3const Hypervisor = require('primea-hypervisor')
4const Message = require('primea-message')
5const WasmContainer = require('../index.js')
6const testInterface = require('./testInterface.js')
7const level = require('level')
8const RadixTree = require('dfinity-radix-tree')
9const db = level('./testdb')
10
11const tree = new RadixTree({
12 db: db
13})
14
15class ContainerTestInterface {
16 constructor (wasmContainer) {
17 this.wasmContainer = wasmContainer
18 }
19
20 readMem (offset) {
21 return this.wasmContainer.getMemory(offset, 1)
22 }
23
24 writeMem (offset, val) {
25 return this.wasmContainer.setMemory(offset, [val])
26 }
27
28 numOfReferances () {
29 return this.wasmContainer.referanceMap.size
30 }
31
32 async callback (cb) {
33 const promise = new Promise((resolve, reject) => {
34 resolve()
35 })
36 await this.wasmContainer.pushOpsQueue(promise)
37 this.wasmContainer.execute(cb)
38 }
39}
40
41tape('wasm container - main', async t => {
42 t.plan(1)
43 const hypervisor = new Hypervisor(tree)
44 const main = fs.readFileSync(`${__dirname}/wasm/run.wasm`)
45 hypervisor.registerContainer(WasmContainer, {
46 test: testInterface(t)
47 })
48
49 const port = hypervisor.creationService.getPort()
50
51 let instance = await hypervisor.send(port, new Message({
52 data: {
53 type: WasmContainer.typeId,
54 code: main
55 }
56 }))
57 instance.message(instance.createMessage())
58})
59
60tape('referances', async t => {
61 t.plan(1)
62 const hypervisor = new Hypervisor(tree)
63 const main = fs.readFileSync(`${__dirname}/wasm/referances.wasm`)
64 hypervisor.registerContainer(WasmContainer, {
65 env: ContainerTestInterface,
66 test: testInterface(t)
67 })
68 const ports = hypervisor.createChannel()
69 const port = hypervisor.creationService.getPort()
70
71 hypervisor.send(port, new Message({
72 data: {
73 type: WasmContainer.typeId,
74 code: main
75 },
76 ports: ports
77 }))
78})
79
80tape('wasm container - mem', async t => {
81 t.plan(1)
82 const hypervisor = new Hypervisor(tree)
83 const readMem = fs.readFileSync(`${__dirname}/wasm/readMem.wasm`)
84 hypervisor.registerContainer(WasmContainer, {
85 env: ContainerTestInterface,
86 test: testInterface(t)
87 })
88
89 const port = hypervisor.creationService.getPort()
90
91 hypervisor.send(port, new Message({
92 data: {
93 type: WasmContainer.typeId,
94 code: readMem
95 }
96 }))
97})
98
99tape('write mem', async t => {
100 const hypervisor = new Hypervisor(tree)
101 const readMem = fs.readFileSync(`${__dirname}/wasm/writeMem.wasm`)
102 hypervisor.registerContainer(WasmContainer, {
103 env: ContainerTestInterface,
104 test: testInterface(t)
105 })
106
107 const port = hypervisor.creationService.getPort()
108 const root = await hypervisor.send(port, new Message({
109 data: {
110 type: WasmContainer.typeId,
111 code: readMem
112 }
113 }))
114 const mem = root.container.getMemory(0, 1)
115 t.equals(mem[0], 9)
116 t.end()
117})
118
119tape('wasm container - callbacks', async t => {
120 t.plan(1)
121 const hypervisor = new Hypervisor(tree)
122 const callBackWasm = fs.readFileSync(`${__dirname}/wasm/callback.wasm`)
123 hypervisor.registerContainer(WasmContainer, {
124 env: ContainerTestInterface,
125 test: testInterface(t)
126 })
127
128 const port = hypervisor.creationService.getPort()
129 await hypervisor.send(port, new Message({
130 data: {
131 type: WasmContainer.typeId,
132 code: callBackWasm
133 }
134 }))
135})
136
137tape('wasm container - invalid', async t => {
138 t.plan(1)
139 const hypervisor = new Hypervisor(tree)
140 hypervisor.registerContainer(WasmContainer, {
141 env: ContainerTestInterface,
142 test: testInterface(t)
143 })
144
145 const message = new Message({
146 data: {
147 type: WasmContainer.typeId,
148 code: Buffer.from([0x00])
149 }
150 })
151
152 const rp = message.responsePort = {destPort: {messages: []}}
153
154 const port = hypervisor.creationService.getPort()
155 await hypervisor.send(port, message)
156
157 t.equals(rp.destPort.messages[0].data.exception, true)
158})
159
160tape('initailize', async t => {
161 t.plan(2)
162
163 const callBackWasm = fs.readFileSync(`${__dirname}/wasm/callback.wasm`)
164
165 class ContainerTestInterface {
166 constructor (wasmContainer) {
167 this.wasmContainer = wasmContainer
168 }
169
170 readMem (offset) {
171 return this.wasmContainer.getMemory(offset, 1)
172 }
173
174 async callback (cb) {
175 const promise = new Promise((resolve, reject) => {
176 resolve()
177 })
178 await this.wasmContainer.pushOpsQueue(promise)
179 this.wasmContainer.execute(cb)
180 }
181
182 static initialize (code) {
183 t.equals(code, callBackWasm)
184 return code
185 }
186 }
187
188 const hypervisor = new Hypervisor(tree)
189 hypervisor.registerContainer(WasmContainer, {
190 env: ContainerTestInterface,
191 test: testInterface(t)
192 })
193
194 const port = hypervisor.creationService.getPort()
195 hypervisor.send(port, new Message({
196 data: {
197 type: WasmContainer.typeId,
198 code: callBackWasm
199 }
200 }))
201})
202

Built with git-ssb-web