Files: 8f3ba18dfc2d7fcca5d7f73252e8549416f5d033 / tests / index.js
4904 bytesRaw
1 | const tape = require('tape') |
2 | const fs = require('fs') |
3 | const Hypervisor = require('primea-hypervisor') |
4 | const Message = require('primea-message') |
5 | const WasmContainer = require('../index.js') |
6 | const testInterface = require('./testInterface.js') |
7 | const level = require('level') |
8 | const RadixTree = require('dfinity-radix-tree') |
9 | const db = level('./testdb') |
10 | |
11 | const tree = new RadixTree({ |
12 | db: db |
13 | }) |
14 | |
15 | class 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 | |
41 | tape('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: Buffer.concat([Buffer.from([0]), Buffer.from([WasmContainer.typeId]), main]) |
53 | })) |
54 | instance.message(instance.createMessage()) |
55 | }) |
56 | |
57 | tape('referances', async t => { |
58 | t.plan(1) |
59 | const hypervisor = new Hypervisor(tree) |
60 | const main = fs.readFileSync(`${__dirname}/wasm/referances.wasm`) |
61 | hypervisor.registerContainer(WasmContainer, { |
62 | env: ContainerTestInterface, |
63 | test: testInterface(t) |
64 | }) |
65 | const ports = hypervisor.createChannel() |
66 | const port = hypervisor.creationService.getPort() |
67 | |
68 | hypervisor.send(port, new Message({ |
69 | data: Buffer.concat([Buffer.from([0]), Buffer.from([WasmContainer.typeId]), main]), |
70 | ports: ports |
71 | })) |
72 | }) |
73 | |
74 | tape('wasm container - mem', async t => { |
75 | t.plan(1) |
76 | const hypervisor = new Hypervisor(tree) |
77 | const readMem = fs.readFileSync(`${__dirname}/wasm/readMem.wasm`) |
78 | hypervisor.registerContainer(WasmContainer, { |
79 | env: ContainerTestInterface, |
80 | test: testInterface(t) |
81 | }) |
82 | |
83 | const port = hypervisor.creationService.getPort() |
84 | |
85 | hypervisor.send(port, new Message({ |
86 | data: Buffer.concat([Buffer.from([0]), Buffer.from([WasmContainer.typeId]), readMem]) |
87 | })) |
88 | }) |
89 | |
90 | tape('write mem', async t => { |
91 | const hypervisor = new Hypervisor(tree) |
92 | const readMem = fs.readFileSync(`${__dirname}/wasm/writeMem.wasm`) |
93 | hypervisor.registerContainer(WasmContainer, { |
94 | env: ContainerTestInterface, |
95 | test: testInterface(t) |
96 | }) |
97 | |
98 | const port = hypervisor.creationService.getPort() |
99 | const root = await hypervisor.send(port, new Message({ |
100 | data: Buffer.concat([Buffer.from([0]), Buffer.from([WasmContainer.typeId]), readMem]) |
101 | })) |
102 | const mem = root.container.getMemory(0, 1) |
103 | t.equals(mem[0], 9) |
104 | t.end() |
105 | }) |
106 | |
107 | tape('wasm container - callbacks', async t => { |
108 | t.plan(1) |
109 | const hypervisor = new Hypervisor(tree) |
110 | const callBackWasm = fs.readFileSync(`${__dirname}/wasm/callback.wasm`) |
111 | hypervisor.registerContainer(WasmContainer, { |
112 | env: ContainerTestInterface, |
113 | test: testInterface(t) |
114 | }) |
115 | |
116 | const port = hypervisor.creationService.getPort() |
117 | await hypervisor.send(port, new Message({ |
118 | data: Buffer.concat([Buffer.from([0]), Buffer.from([WasmContainer.typeId]), callBackWasm]) |
119 | })) |
120 | }) |
121 | |
122 | tape('wasm container - invalid', async t => { |
123 | t.plan(1) |
124 | const hypervisor = new Hypervisor(tree) |
125 | hypervisor.registerContainer(WasmContainer, { |
126 | env: ContainerTestInterface, |
127 | test: testInterface(t) |
128 | }) |
129 | |
130 | const message = new Message({ |
131 | data: Buffer.concat([Buffer.from([0]), Buffer.from([WasmContainer.typeId]), Buffer.from([0])]) |
132 | }) |
133 | |
134 | const rp = message.responsePort = {destPort: {messages: []}} |
135 | |
136 | const port = hypervisor.creationService.getPort() |
137 | await hypervisor.send(port, message) |
138 | |
139 | t.equals(rp.destPort.messages[0].data.exception, true) |
140 | }) |
141 | |
142 | tape('initailize', async t => { |
143 | t.plan(2) |
144 | |
145 | const callBackWasm = fs.readFileSync(`${__dirname}/wasm/callback.wasm`) |
146 | |
147 | class ContainerTestInterface { |
148 | constructor (wasmContainer) { |
149 | this.wasmContainer = wasmContainer |
150 | } |
151 | |
152 | async callback (cb) { |
153 | const promise = new Promise((resolve, reject) => { |
154 | resolve() |
155 | }) |
156 | await this.wasmContainer.pushOpsQueue(promise) |
157 | this.wasmContainer.execute(cb) |
158 | } |
159 | |
160 | static initialize (code) { |
161 | t.deepEquals(code, callBackWasm) |
162 | return code |
163 | } |
164 | } |
165 | |
166 | const hypervisor = new Hypervisor(tree) |
167 | hypervisor.registerContainer(WasmContainer, { |
168 | env: ContainerTestInterface, |
169 | test: testInterface(t) |
170 | }) |
171 | |
172 | const port = hypervisor.creationService.getPort() |
173 | hypervisor.send(port, new Message({ |
174 | data: Buffer.concat([Buffer.from([0]), Buffer.from([WasmContainer.typeId]), callBackWasm]) |
175 | })) |
176 | }) |
177 |
Built with git-ssb-web