-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.js
228 lines (223 loc) · 5.59 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
var fs = require('fs');
const WAVM = require('./wasmVirtualMachine.js');
let test_file;
if (process.argv[2] == '-u') {
test_file = 'tests_unoptimized.wasm';
} else {
test_file= 'tests.wasm';
}
const FgRed = "\x1b[31m";
const FgGreen = "\x1b[32m";
const FgYellow = "\x1b[33m";
const Reset = "\x1b[0m";
const tests = [
{
name: 'test0',
print_name: 'factorial',
params: [5],
expected_output: 120,
expected_taint: {
'0': 3
}
},
{
name: 'test0',
print_name: 'factorial',
params: [-1],
expected_output: 1,
expected_taint: {
'0': 2
}
},
{
name: 'test1',
print_name: 'n plus 2',
params: [5],
expected_output: 7,
expected_taint: {
'0': 3
}
},
{
name: 'test2',
print_name: 'not_positive (branch test)',
params: [2],
expected_output: -1,
expected_taint: {
'0': 2
}
},
{
name: 'test2',
print_name: 'not_positive (branch test)',
params: [-2],
expected_output: 1,
expected_taint: {
'0': 2
}
},
{
name: 'test3',
print_name: 'a plus 3 and unused variable',
params: [3, 7],
expected_output: 6,
expected_taint: {
'0': 3
}
},
{
name: 'test4',
print_name: 'direct and indirect taint test',
params: [10, 20],
expected_output: 14,
expected_taint: {
'0': 3,
'1': 2
}
},
{
name: 'test4',
print_name: 'direct and indirect taint test',
params: [10, -20],
expected_output: 13,
expected_taint: {
'0': 3,
'1': 2
}
},
{
name: 'test5',
print_name: 'memory access linked list test',
params: [5, 3],
expected_output: 20,
expected_taint: {
'0': 3,
'1': 2
}
},
{
name: 'test5',
print_name: 'memory access linked list test',
params: [3, 5],
expected_output: -1,
expected_taint: {
'0': 2,
'1': 2
}
},
{
name: 'test6',
print_name: 'long long to short conversion test',
params: [1234],
expected_output: 1234,
expected_taint: {
'0': 3
}
},
{
name: 'test7',
print_name: 'long long to int conversion test',
params: [4321],
expected_output: 4321,
expected_taint: {
'0': 3
}
},
{
name: 'test8',
print_name: 'add 2 long longs test',
params: [1234, 1234],
expected_output: 2468,
expected_taint: {
'0': 3,
'1': 3
}
},
{
name: 'test9',
print_name: '64 bit factorial test',
params: [18],
expected_output: 6402373705728000,
expected_taint: {
'0': 3
}
},
{
name: 'test10',
print_name: 'euler totient with recursive helper function test',
params: [10],
expected_output: 4,
expected_taint: {
'0': 2
}
},
{
name: 'test11',
print_name: 'iterative euler totient test',
params: [11],
expected_output: 10,
expected_taint: {
'0': 3
}
},
{
name: 'test11',
print_name: 'iterative euler totient test',
params: [6299],
expected_output: 6298,
expected_taint: {
'0': 3
}
},
{
name: 'test12',
print_name: 'large number of variable assignments',
params: [Math.pow(2,10)],
expected_output: 1,
expected_taint: {
'0': 3
}
}
];
let byte_code;
let data = fs.readFileSync(test_file);
byte_code = new Uint8Array(data);
let VM = new WAVM(byte_code);
console.log("Running tests compiled with opimization\n");
for (let test_num = 0; test_num < tests.length; test_num++) {
//for (let test_num = tests.length-1; test_num < tests.length; test_num++) {
//for (let test_num = 7; test_num < 8; test_num++) {
let tst = tests[test_num];
console.log(Reset + "Running test " + tst.name + ": " + tst.print_name + ".\n with parameters: " + tst.params);
let output = VM.run_function(tst.name, tst.params);
if (output == -1) {
console.log(FgRed + "Fail!");
console.log(" Encountered error!");
continue;
}
let res = output[0];
let color = FgYellow;
if (res.value == tst.expected_output && JSON.stringify(res.taint) == JSON.stringify(tst.expected_taint)) {
console.log(FgGreen + "Success!");
color = FgGreen;
} else {
if (res.value != tst.expected_output) {
color = FgRed;
} else {
let keys = Object.keys(tst.expected_taint);
for (let i = 0; i < keys.length; i++) {
if (tst.expected_taint[keys[i]] == 3 && (!res.taint[keys[i]] || res.taint[keys[i]] == 2)) {
color = FgRed;
}
}
}
if (color == FgRed) {
console.log(FgRed + "Fail!");
} else {
console.log(FgYellow + "Missed indirect taint. This is expected");
}
}
console.log(color + " Expected output: " + tst.expected_output + ". Got: " + res.value);
console.log(color + " Expected taint: " + JSON.stringify(tst.expected_taint) + ". Got: " + JSON.stringify(res.taint));
console.log(Reset);
}