summaryrefslogtreecommitdiff
path: root/test/wasi/test-wasi.js
blob: fa2e0894c906ce24bb1aa902d16264dcf615cbea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'use strict';
const common = require('../common');

if (process.argv[2] === 'wasi-child') {
  const fixtures = require('../common/fixtures');
  const tmpdir = require('../../test/common/tmpdir');
  const fs = require('fs');
  const path = require('path');

  common.expectWarning('ExperimentalWarning',
                       'WASI is an experimental feature. This feature could ' +
                       'change at any time');

  const { WASI } = require('wasi');
  tmpdir.refresh();
  const wasmDir = path.join(__dirname, 'wasm');
  const wasi = new WASI({
    args: [],
    env: process.env,
    preopens: {
      '/sandbox': fixtures.path('wasi'),
      '/tmp': tmpdir.path
    }
  });
  const importObject = { wasi_unstable: wasi.wasiImport };
  const modulePath = path.join(wasmDir, `${process.argv[3]}.wasm`);
  const buffer = fs.readFileSync(modulePath);

  (async () => {
    const { instance } = await WebAssembly.instantiate(buffer, importObject);

    wasi.start(instance);
  })();
} else {
  const assert = require('assert');
  const cp = require('child_process');
  const { EOL } = require('os');

  function runWASI(options) {
    console.log('executing', options.test);
    const opts = { env: { ...process.env, NODE_DEBUG_NATIVE: 'wasi' } };

    if (options.stdin !== undefined)
      opts.input = options.stdin;

    const child = cp.spawnSync(process.execPath, [
      '--experimental-wasi-unstable-preview0',
      '--experimental-wasm-bigint',
      __filename,
      'wasi-child',
      options.test
    ], opts);
    console.log(child.stderr.toString());
    assert.strictEqual(child.status, options.exitCode || 0);
    assert.strictEqual(child.signal, null);
    assert.strictEqual(child.stdout.toString(), options.stdout || '');
  }

  runWASI({ test: 'cant_dotdot' });
  runWASI({ test: 'clock_getres' });
  runWASI({ test: 'exitcode', exitCode: 120 });
  runWASI({ test: 'fd_prestat_get_refresh' });
  runWASI({ test: 'getentropy' });
  runWASI({ test: 'getrusage' });
  runWASI({ test: 'gettimeofday' });
  runWASI({ test: 'notdir' });
  // runWASI({ test: 'poll' });
  runWASI({ test: 'preopen_populates' });
  runWASI({ test: 'read_file', stdout: `hello from input.txt${EOL}` });
  runWASI({
    test: 'read_file_twice',
    stdout: `hello from input.txt${EOL}hello from input.txt${EOL}`
  });
  runWASI({ test: 'stat' });
  runWASI({ test: 'write_file' });

  // Tests that are currently unsupported on Windows.
  if (!common.isWindows) {
    runWASI({ test: 'stdin', stdin: 'hello world', stdout: 'hello world' });
  }
}