Files: 69dfa6d2b66e206f048fb0432ddf61cb1743a42c / tests / index.js
3036 bytesRaw
1 | const tape = require('tape') |
2 | const fs = require('fs') |
3 | const Hypervisor = require('primea-hypervisor') |
4 | const WasmContainer = require('../index.js') |
5 | const testInterface = require('./testInterface.js') |
6 | const IPFS = require('ipfs') |
7 | const ReferanceMap = require('../referanceMap.js') |
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('referance mapping', t => { |
33 | t.plan(6) |
34 | const referanceMap = new ReferanceMap() |
35 | const obj1 = {} |
36 | const obj2 = {} |
37 | const ref1 = referanceMap.add(obj1) |
38 | const ref2 = referanceMap.add(obj2) |
39 | t.equals(ref1, 0, 'should produce correct refs') |
40 | t.equals(ref2, 1, 'should produce correct refs') |
41 | |
42 | const foundObj1 = referanceMap.get(ref1) |
43 | const foundObj2 = referanceMap.get(ref2) |
44 | |
45 | t.equals(foundObj1, obj1, 'should get the correct object') |
46 | t.equals(foundObj2, obj2, 'should get the correct object') |
47 | |
48 | referanceMap.delete(ref1) |
49 | try { |
50 | referanceMap.get(ref1) |
51 | } catch (e) { |
52 | t.true(true, 'should delete refances') |
53 | } |
54 | |
55 | referanceMap.clear() |
56 | try { |
57 | referanceMap.get(ref2) |
58 | } catch (e) { |
59 | t.true(true, 'should clear refances') |
60 | } |
61 | }) |
62 | |
63 | tape('wasm container - mem', async t => { |
64 | t.plan(2) |
65 | const hypervisor = new Hypervisor(node.dag) |
66 | const readMem = fs.readFileSync(`${__dirname}/wasm/readMem.wasm`) |
67 | hypervisor.registerContainer('wasm', WasmContainer, { |
68 | env: ContainerTestInterface, |
69 | test: testInterface(t) |
70 | }) |
71 | const root = await hypervisor.createInstance('wasm', { |
72 | '/': WasmContainer.createState(readMem) |
73 | }) |
74 | const r = await root.run() |
75 | t.deepEquals(r, {}, 'should have no return value') |
76 | }) |
77 | |
78 | tape('wasm container - callbacks', async t => { |
79 | t.plan(2) |
80 | const hypervisor = new Hypervisor(node.dag) |
81 | const readMem = fs.readFileSync(`${__dirname}/wasm/callback.wasm`) |
82 | hypervisor.registerContainer('wasm', WasmContainer, { |
83 | env: ContainerTestInterface, |
84 | test: testInterface(t) |
85 | }) |
86 | const root = await hypervisor.createInstance('wasm', { |
87 | '/': WasmContainer.createState(readMem) |
88 | }) |
89 | const r = await root.run() |
90 | t.deepEquals(r, {}, 'should have no return value') |
91 | }) |
92 | |
93 | tape('wasm container - invalid', async t => { |
94 | t.plan(1) |
95 | const hypervisor = new Hypervisor(node.dag) |
96 | hypervisor.registerContainer('wasm', WasmContainer, { |
97 | env: ContainerTestInterface, |
98 | test: testInterface(t) |
99 | }) |
100 | |
101 | try { |
102 | await hypervisor.createInstance('wasm', { |
103 | '/': WasmContainer.createState(new Buffer([0x00])) |
104 | }) |
105 | } catch (e) { |
106 | console.log(e) |
107 | t.true(true, 'should trap on invalid wasm') |
108 | } |
109 | }) |
110 | }) |
111 |
Built with git-ssb-web