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