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