Skip to content

Commit

Permalink
test(fuzz): add fuzz tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hulkoba committed Dec 12, 2023
1 parent aeb467a commit 85bea89
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
node_modules/
npm*
test/lib/
test/typescript/definitions.js
dist/
openpgp.store/
coverage
test/fuzz/reports
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
"prepare": "npm run build",
"test": "mocha --timeout 120000 test/unittests.js",
"test-type-definitions": "node --loader ts-node/esm test/typescript/definitions.ts",
"fuzz": "jazzer test/fuzz/$TARGET.cjs -- -artifact_prefix=test/fuzz/reports/",
"fuzz-coverage": "jazzer test/fuzz/$TARGET.cjs --coverage --cov_dir=test/fuzz/coverage -- -artifact_prefix=test/fuzz/reports/",
"benchmark-time": "node test/benchmarks/time.js",
"benchmark-memory-usage": "node test/benchmarks/memory_usage.js",
"start": "http-server",
Expand Down
14 changes: 14 additions & 0 deletions test/fuzz/createCleartextMessage.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { FuzzedDataProvider } = require('@jazzer.js/core');

/**
* @param { Buffer } inputData
*/
module.exports.fuzz = function(inputData) {
import('../initOpenpgp.js').then(openpgp => {
const data = new FuzzedDataProvider(inputData);
const text = data.bufToPrintableString(inputData, 2, 444, 'utf-8');
return openpgp.default.createCleartextMessage({ text });
});

};

9 changes: 9 additions & 0 deletions test/fuzz/createMessageBinary.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @param { Buffer } inputData
*/
module.exports.fuzz = function(inputData) {
import('../initOpenpgp.js').then(openpgp => {
return openpgp.default.createMessage({ binary: new Uint8Array(inputData) });
});
};

11 changes: 11 additions & 0 deletions test/fuzz/createMessageText.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { FuzzedDataProvider } = require('@jazzer.js/core');

/**
* @param { Buffer } inputData
*/
module.exports.fuzz = function(inputData) {
import('../initOpenpgp.js').then(openpgp => {
const data = new FuzzedDataProvider(inputData);
return openpgp.default.createMessage({ text: data.consumeString(444, 'utf-8') });
});
};
22 changes: 22 additions & 0 deletions test/fuzz/generateKey.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { FuzzedDataProvider } = require('@jazzer.js/core');

/**
* @param { Buffer } inputData
*/
module.exports.fuzz = function(inputData) {
import('../initOpenpgp.js').then(openpgp => {
const data = new FuzzedDataProvider(inputData);
const asciiString = data.consumeString(111);
const utf8String = data.consumeString(33, 'utf-8');

return openpgp.default.generateKey({ userIDs: [
{ name: utf8String },
{ email: utf8String },
{ comment: asciiString },
{ name: utf8String, email: utf8String, comment: asciiString }
],
passphrase: asciiString,
format: 'object' });
});
};

26 changes: 26 additions & 0 deletions test/fuzz/readKeyArmored.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { FuzzedDataProvider } = require('@jazzer.js/core');

const ignored = ['Misformed armored text'];

function ignoredError(error) {
return ignored.some(message => error.message.includes(message));
}

/**
* @param { Buffer } inputData
*/
module.exports.fuzz = function(inputData) {
import('../initOpenpgp.js').then(openpgp => {
const data = new FuzzedDataProvider(inputData);
const fuzzedText = data.consumeString(333, 'utf-8');
const armoredKey = `-----BEGIN PGP PRIVATE KEY BLOCK----- ${fuzzedText} -----END PGP PRIVATE KEY BLOCK-----`;

return openpgp.default.readKey({ armoredKey })
.catch(error => {
if (error.message && !ignoredError(error)) {
throw error;
}
});
});
};

22 changes: 22 additions & 0 deletions test/fuzz/readKeyBinary.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const ignored = ['This message / key probably does not conform to a valid OpenPGP format'];

function ignoredError(error) {
return ignored.some(message => error.message.includes(message));
}

/**
* @param { Buffer } inputData
*/
module.exports.fuzz = function(inputData) {
import('../initOpenpgp.js').then(openpgp => {
const binaryKey = new Uint8Array(`-----BEGIN PGP PRIVATE KEY BLOCK----- ${inputData} -----END PGP PRIVATE KEY BLOCK-----`);

return openpgp.default.readKey({ binaryKey })
.catch(error => {
if (error.message && !ignoredError(error)) {
throw error;
}
});
});
};

22 changes: 22 additions & 0 deletions test/fuzz/readMessageBinary.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const ignored = ['This message / key probably does not conform to a valid OpenPGP format'];

function ignoredError(error) {
return ignored.some(message => error.message.includes(message));
}

/**
* @param { Buffer } inputData
*/
module.exports.fuzz = function(inputData) {
import('../initOpenpgp.js').then(openpgp => {
const binaryMessage = new Uint8Array(`-----BEGIN PGP MESSAGE----- ${inputData} -----END PGP MESSAGE-----`);

return openpgp.default.readMessage({ binaryMessage })
.catch(error => {
if (error.message && !ignoredError(error)) {
throw error;
}
});
});
};

26 changes: 26 additions & 0 deletions test/fuzz/readMessageText.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { FuzzedDataProvider } = require('@jazzer.js/core');

const ignored = ['Misformed armored text'];

function ignoredError(error) {
return ignored.some(message => error.message.includes(message));
}

/**
* @param { Buffer } inputData
*/
module.exports.fuzz = function(inputData) {
import('../initOpenpgp.js').then(openpgp => {
const data = new FuzzedDataProvider(inputData);
const fuzzedText = data.consumeString(333, 'utf-8');
const armoredMessage = `-----BEGIN PGP MESSAGE----- ${fuzzedText} -----END PGP MESSAGE-----`;

return openpgp.default.readMessage({ armoredMessage })
.catch(error => {
if (error.message && !ignoredError(error)) {
throw error;
}
});
});
};

0 comments on commit 85bea89

Please sign in to comment.