Files: 1acaf62122a024e6a953fb715cf4bbda94b804ad / tests / index.js
4677 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 IPFS = require('ipfs') |
8 | |
9 | const node = new IPFS({ |
10 | start: false |
11 | }) |
12 | |
13 | class ContainerTestInterface { |
14 | constructor (wasmContainer) { |
15 | this.wasmContainer = wasmContainer |
16 | } |
17 | |
18 | readMem (offset) { |
19 | return this.wasmContainer.getMemory(offset, 1) |
20 | } |
21 | |
22 | writeMem (offset, val) { |
23 | return this.wasmContainer.setMemory(offset, [val]) |
24 | } |
25 | |
26 | numOfReferances () { |
27 | return this.wasmContainer.referanceMap.size |
28 | } |
29 | |
30 | async callback (cb) { |
31 | const promise = new Promise((resolve, reject) => { |
32 | resolve() |
33 | }) |
34 | await this.wasmContainer.pushOpsQueue(promise) |
35 | this.wasmContainer.execute(cb) |
36 | } |
37 | } |
38 | |
39 | node.on('ready', () => { |
40 | tape('wasm container - main', async t => { |
41 | t.plan(1) |
42 | const hypervisor = new Hypervisor(node.dag) |
43 | const main = fs.readFileSync(`${__dirname}/wasm/run.wasm`) |
44 | hypervisor.registerContainer(WasmContainer, { |
45 | test: testInterface(t) |
46 | }) |
47 | const instance = await hypervisor.createInstance(WasmContainer.typeId, new Message({ |
48 | data: main |
49 | })) |
50 | instance.message(instance.createMessage()) |
51 | }) |
52 | |
53 | tape('referances', async t => { |
54 | t.plan(1) |
55 | const hypervisor = new Hypervisor(node.dag) |
56 | const main = fs.readFileSync(`${__dirname}/wasm/referances.wasm`) |
57 | hypervisor.registerContainer(WasmContainer, { |
58 | env: ContainerTestInterface, |
59 | test: testInterface(t) |
60 | }) |
61 | const ports = hypervisor.createChannel() |
62 | await hypervisor.createInstance(WasmContainer.typeId, new Message({ |
63 | data: main, |
64 | ports: ports |
65 | })) |
66 | }) |
67 | |
68 | tape('wasm container - mem', async t => { |
69 | t.plan(1) |
70 | try { |
71 | const hypervisor = new Hypervisor(node.dag) |
72 | const readMem = fs.readFileSync(`${__dirname}/wasm/readMem.wasm`) |
73 | hypervisor.registerContainer(WasmContainer, { |
74 | env: ContainerTestInterface, |
75 | test: testInterface(t) |
76 | }) |
77 | await hypervisor.createInstance(WasmContainer.typeId, new Message({ |
78 | data: readMem |
79 | })) |
80 | } catch (e) { |
81 | console.log(e) |
82 | } |
83 | }) |
84 | |
85 | tape('write mem', async t => { |
86 | try { |
87 | const hypervisor = new Hypervisor(node.dag) |
88 | const readMem = fs.readFileSync(`${__dirname}/wasm/writeMem.wasm`) |
89 | hypervisor.registerContainer(WasmContainer, { |
90 | env: ContainerTestInterface, |
91 | test: testInterface(t) |
92 | }) |
93 | const root = await hypervisor.createInstance(WasmContainer.typeId, new Message({ |
94 | data: readMem |
95 | })) |
96 | const mem = root.container.getMemory(0, 1) |
97 | t.equals(mem[0], 9) |
98 | t.end() |
99 | } catch (e) { |
100 | console.log(e) |
101 | } |
102 | }) |
103 | |
104 | tape('wasm container - callbacks', async t => { |
105 | t.plan(1) |
106 | const hypervisor = new Hypervisor(node.dag) |
107 | const callBackWasm = fs.readFileSync(`${__dirname}/wasm/callback.wasm`) |
108 | hypervisor.registerContainer(WasmContainer, { |
109 | env: ContainerTestInterface, |
110 | test: testInterface(t) |
111 | }) |
112 | hypervisor.createInstance(WasmContainer.typeId, new Message({ |
113 | data: callBackWasm |
114 | })) |
115 | }) |
116 | |
117 | tape('wasm container - invalid', async t => { |
118 | t.plan(1) |
119 | const hypervisor = new Hypervisor(node.dag) |
120 | hypervisor.registerContainer(WasmContainer, { |
121 | env: ContainerTestInterface, |
122 | test: testInterface(t) |
123 | }) |
124 | |
125 | const message = new Message({ |
126 | data: Buffer.from([0x00]) |
127 | }) |
128 | |
129 | const rp = message.responsePort = {destPort: {messages: []}} |
130 | |
131 | await hypervisor.createInstance(WasmContainer.typeId, message) |
132 | t.equals(rp.destPort.messages[0].data.exception, true) |
133 | }) |
134 | |
135 | tape('initailize', async t => { |
136 | t.plan(2) |
137 | |
138 | const callBackWasm = fs.readFileSync(`${__dirname}/wasm/callback.wasm`) |
139 | |
140 | class ContainerTestInterface { |
141 | constructor (wasmContainer) { |
142 | this.wasmContainer = wasmContainer |
143 | } |
144 | |
145 | readMem (offset) { |
146 | return this.wasmContainer.getMemory(offset, 1) |
147 | } |
148 | |
149 | async callback (cb) { |
150 | const promise = new Promise((resolve, reject) => { |
151 | resolve() |
152 | }) |
153 | await this.wasmContainer.pushOpsQueue(promise) |
154 | this.wasmContainer.execute(cb) |
155 | } |
156 | |
157 | static initialize (code) { |
158 | t.equals(code, callBackWasm) |
159 | return code |
160 | } |
161 | } |
162 | |
163 | const hypervisor = new Hypervisor(node.dag) |
164 | hypervisor.registerContainer(WasmContainer, { |
165 | env: ContainerTestInterface, |
166 | test: testInterface(t) |
167 | }) |
168 | |
169 | const message = new Message({ |
170 | data: callBackWasm |
171 | }) |
172 | |
173 | hypervisor.createInstance(WasmContainer.typeId, message) |
174 | }) |
175 | }) |
176 |
Built with git-ssb-web