From 299d518b48c6078a660ab03e5cc46a3c44171edb Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sat, 14 Dec 2019 20:59:04 -0500 Subject: [PATCH] test: improve WASI start() coverage This commit adds additional test cases to test-wasi-start-validation.js, which gets the JS test coverage of start() to 100%. PR-URL: https://github.com/nodejs/node/pull/30972 Reviewed-By: Jiawen Geng Reviewed-By: Rich Trott Reviewed-By: James M Snell --- test/wasi/test-wasi-start-validation.js | 67 ++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/test/wasi/test-wasi-start-validation.js b/test/wasi/test-wasi-start-validation.js index 2a5cb764943f80..3f530a42f791f8 100644 --- a/test/wasi/test-wasi-start-validation.js +++ b/test/wasi/test-wasi-start-validation.js @@ -1,7 +1,7 @@ // Flags: --experimental-wasi-unstable-preview0 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); const { WASI } = require('wasi'); @@ -30,3 +30,68 @@ const fixtures = require('../common/fixtures'); ); })(); } + +(async () => { + const wasi = new WASI(); + const bufferSource = fixtures.readSync('simple.wasm'); + const wasm = await WebAssembly.compile(bufferSource); + const instance = await WebAssembly.instantiate(wasm); + const values = [undefined, null, 'foo', 42, true, false, () => {}]; + let cnt = 0; + + // Mock instance.exports to trigger start() validation. + Object.defineProperty(instance, 'exports', { + get() { return values[cnt++]; } + }); + + values.forEach((val) => { + assert.throws( + () => { wasi.start(instance); }, + { code: 'ERR_INVALID_ARG_TYPE', message: /\binstance\.exports\b/ } + ); + }); +})(); + +(async () => { + const wasi = new WASI(); + const bufferSource = fixtures.readSync('simple.wasm'); + const wasm = await WebAssembly.compile(bufferSource); + const instance = await WebAssembly.instantiate(wasm); + + // Mock instance.exports.memory to bypass start() validation. + Object.defineProperty(instance, 'exports', { + get() { + return { + memory: new WebAssembly.Memory({ initial: 1 }) + }; + } + }); + + wasi.start(instance); + assert.throws( + () => { wasi.start(instance); }, + { + code: 'ERR_WASI_ALREADY_STARTED', + message: /^WASI instance has already started$/ + } + ); +})(); + +(async () => { + const wasi = new WASI(); + const bufferSource = fixtures.readSync('simple.wasm'); + const wasm = await WebAssembly.compile(bufferSource); + const instance = await WebAssembly.instantiate(wasm); + + // Mock instance.exports to bypass start() validation. + Object.defineProperty(instance, 'exports', { + get() { + return { + memory: new WebAssembly.Memory({ initial: 1 }), + __wasi_unstable_reactor_start: common.mustCall() + }; + } + }); + + wasi.start(instance); +})();