Skip to content

Commit 0559682

Browse files
committed
add test script: test:fuzz
1 parent a14e756 commit 0559682

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ isolate-*.log
1111

1212
# flamebearer
1313
flamegraph.html
14+
15+
# jsfuzz
16+
corpus/

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
"test:te": "TEXT_ENCODING=force mocha 'test/**/*.test.ts'",
2121
"test:dist:purejs": "TS_NODE_PROJECT=tsconfig.test-dist-es5-purejs.json npm run test:purejs -- --reporter=dot",
2222
"test:cover": "npm run cover:clean && npm-run-all 'test:cover:*' && npm run cover:report",
23-
"test:cover:purejs": "npx nyc --no-clean npm run test:purejs",
24-
"test:cover:te": "npx nyc --no-clean npm run test:te",
23+
"test:cover:purejs": "npm exec nyc --no-clean npm run test:purejs",
24+
"test:cover:te": "npm exec nyc --no-clean npm run test:te",
2525
"test:deno": "deno test test/deno_test.ts",
26+
"test:fuzz": "npm exec -- jsfuzz@git+https://gitlab.com/gitlab-org/security-products/analyzers/fuzzers/jsfuzz.git --fuzzTime 5 --rss-limit-mb 256 --no-versifier test/decode.jsfuzz.js corpus",
2627
"cover:clean": "rimraf .nyc_output coverage/",
2728
"cover:report": "npx nyc report --reporter=text-summary --reporter=html --reporter=json",
2829
"test:browser": "karma start --single-run",

test/decode.jsfuzz.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* eslint-disable */
2+
const assert = require("assert");
3+
const { Decoder, encode, DecodeError, DataViewIndexOutOfBoundsError } = require("../dist/index.js");
4+
5+
/**
6+
* @param {Buffer} bytes
7+
* @returns {void}
8+
*/
9+
module.exports.fuzz = function fuzz(bytes) {
10+
const decoder = new Decoder();
11+
try {
12+
decoder.decode(bytes);
13+
} catch (e) {
14+
if (e instanceof DecodeError) {
15+
// ok
16+
} else if (e instanceof DataViewIndexOutOfBoundsError) {
17+
// ok
18+
} else {
19+
throw e;
20+
}
21+
}
22+
23+
// make sure the decoder instance is not broken
24+
const object = {
25+
foo: 1,
26+
bar: 2,
27+
baz: ["one", "two", "three"],
28+
};
29+
assert.deepStrictEqual(decoder.decode(encode(object)), object);
30+
}

0 commit comments

Comments
 (0)