forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
124 lines (109 loc) · 3.47 KB
/
test.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const { readFileSync } = require('fs');
function stringDecoder(mem) {
return function (addr) {
const i8 = new Int8Array(mem.buffer);
const start = addr;
var s = "";
while (i8[addr] != 0) {
s += String.fromCharCode(i8[addr++]);
}
return s;
}
}
function red(text) {
return '\x1b[0m\x1b[31m' + text + '\x1b[0m';
}
function green(text) {
return '\x1b[0m\x1b[32m' + text + '\x1b[0m';
}
function yellow(text) {
return '\x1b[0m\x1b[33m' + text + '\x1b[0m';
}
function namespace(cache, func, note) {
let key = func + note;
if (key in cache) {
cache[key] += 1;
note = note + ' (' + cache[key] + ')'
} else {
cache[key] = 0;
}
return note;
}
function report(passed, error, msg) {
if (passed === true) {
if (process.env.VERBOSE === '1') {
console.log(green('PASS'), msg);
}
} else if (error === undefined) {
console.log(yellow('FAIL'), msg);
} else {
console.log(red('ERROR'), msg, error);
}
}
async function test(executable) {
const mem = new WebAssembly.Memory({ initial: 3 });
const addr2string = stringDecoder(mem);
let cache = {};
let failedOrErrored = 0;
let seenFuncs = {};
const module = await WebAssembly.instantiate(readFileSync(executable), {
env: {
memory: mem,
opa_builtin0: (_1, _2) => { return 0; },
opa_builtin1: (_1, _2, _3, _4) => { return 0; },
opa_builtin2: (_1, _2, _3, _4) => { return 0; },
opa_builtin3: (_1, _2, _3, _4, _5) => { return 0; },
opa_builtin4: (_1, _2, _3, _4, _5, _6) => { return 0; },
opa_println: (msg) => {
console.log(addr2string(msg));
},
opa_abort: (msg) => {
throw 'abort: ' + addr2string(msg);
},
opa_test_pass: (note, func) => {
note = addr2string(note);
func = addr2string(func);
note = namespace(cache, func, note);
seenFuncs[func] = true;
let key = func + '/' + note
report(true, undefined, key);
},
opa_test_fail: (note, func, file, line) => {
note = addr2string(note);
func = addr2string(func);
note = namespace(cache, func, note);
seenFuncs[func] = true;
let key = func + '/' + note;
failedOrErrored++;
report(false, undefined, key + ' ' + addr2string(file) + ':' + line);
},
}
});
for (let key in module.instance.exports) {
if (key.startsWith("test_")) {
try {
module.instance.exports[key]();
if (!(key in seenFuncs)) {
report(true, undefined, key);
}
} catch (e) {
report(false, e, key)
}
}
}
// NOTE(sr): seenFuncs will not contain all tests run, but only those that
// actually call opa_test_{pass,fail}. However, if it's empty, something is
// definitely wrong.
if (Object.keys(seenFuncs).length == 0) {
console.log(red('ERROR'), "no tests executed");
process.exit(2);
}
if (failedOrErrored > 0) {
process.exit(1);
}
}
if (process.argv.length != 3) {
console.log(process.argv[1] + " <test executable path>");
process.exit(1);
}
test(process.argv[2]);