Merge branch 'master' of github.com:gchq/CyberChef into simplify-recipe

This commit is contained in:
d98762625 2020-03-20 13:08:18 +00:00
commit a49d97f898
46 changed files with 9132 additions and 4884 deletions

View file

@ -10,6 +10,8 @@
* @license Apache-2.0
*/
import Chef from "../../src/core/Chef.mjs";
import Utils from "../../src/core/Utils.mjs";
import cliProgress from "cli-progress";
/**
* Object to store and run the list of tests.
@ -47,68 +49,99 @@ class TestRegister {
/**
* Runs all the tests in the register.
*/
runTests () {
console.log("Running tests...");
return Promise.all(
this.tests.map(function(test, i) {
const chef = new Chef();
async runTests () {
const progBar = new cliProgress.SingleBar({
format: formatter,
stopOnComplete: true
}, cliProgress.Presets.shades_classic);
const testResults = [];
return chef.bake(
test.input,
test.recipeConfig,
{},
0,
false
).then(function(result) {
const ret = {
test: test,
status: null,
output: null,
};
console.log("Running operation tests...");
progBar.start(this.tests.length, 0, {
msg: "Setting up"
});
if (result.error) {
if (test.expectedError) {
ret.status = "passing";
} else {
ret.status = "erroring";
ret.output = result.error.displayStr;
}
} else {
if (test.expectedError) {
ret.status = "failing";
ret.output = "Expected an error but did not receive one.";
} else if (result.result === test.expectedOutput) {
ret.status = "passing";
} else if ("expectedMatch" in test && test.expectedMatch.test(result.result)) {
ret.status = "passing";
} else {
ret.status = "failing";
const expected = test.expectedOutput ? test.expectedOutput :
test.expectedMatch ? test.expectedMatch.toString() : "unknown";
ret.output = [
"Expected",
"\t" + expected.replace(/\n/g, "\n\t"),
"Received",
"\t" + result.result.replace(/\n/g, "\n\t"),
].join("\n");
}
}
for (const test of this.tests) {
progBar.update(testResults.length, {
msg: test.name
});
return ret;
});
})
);
const chef = new Chef();
const result = await chef.bake(
test.input,
test.recipeConfig,
{},
0,
false
);
const ret = {
test: test,
status: null,
output: null,
duration: result.duration
};
if (result.error) {
if (test.expectedError) {
ret.status = "passing";
} else {
ret.status = "erroring";
ret.output = result.error.displayStr;
}
} else {
if (test.expectedError) {
ret.status = "failing";
ret.output = "Expected an error but did not receive one.";
} else if (result.result === test.expectedOutput) {
ret.status = "passing";
} else if ("expectedMatch" in test && test.expectedMatch.test(result.result)) {
ret.status = "passing";
} else {
ret.status = "failing";
const expected = test.expectedOutput ? test.expectedOutput :
test.expectedMatch ? test.expectedMatch.toString() : "unknown";
ret.output = [
"Expected",
"\t" + expected.replace(/\n/g, "\n\t"),
"Received",
"\t" + result.result.replace(/\n/g, "\n\t"),
].join("\n");
}
}
testResults.push(ret);
progBar.increment();
}
return testResults;
}
/**
* Run all api related tests and wrap results in report format
*/
runApiTests() {
return Promise.all(this.apiTests.map(async function(test, i) {
async runApiTests() {
const progBar = new cliProgress.SingleBar({
format: formatter,
stopOnComplete: true
}, cliProgress.Presets.shades_classic);
const testResults = [];
console.log("Running Node API tests...");
progBar.start(this.apiTests.length, 0, {
msg: "Setting up"
});
global.TESTING = true;
for (const test of this.apiTests) {
progBar.update(testResults.length, {
msg: test.name
});
const result = {
test: test,
status: null,
output: null,
output: null
};
try {
await test.run();
@ -117,10 +150,37 @@ class TestRegister {
result.status = "erroring";
result.output = e.message;
}
return result;
}));
testResults.push(result);
progBar.increment();
}
return testResults;
}
}
/**
* Formatter for the progress bar
*
* @param {Object} options
* @param {Object} params
* @param {Object} payload
* @returns {string}
*/
function formatter(options, params, payload) {
const bar = options.barCompleteString.substr(0, Math.round(params.progress * options.barsize)) +
options.barIncompleteString.substr(0, Math.round((1-params.progress) * options.barsize));
const percentage = Math.floor(params.progress * 100),
duration = Math.floor((Date.now() - params.startTime) / 1000);
let testName = payload.msg ? payload.msg : "";
if (params.value >= params.total) testName = "Tests completed";
testName = Utils.truncate(testName, 25).padEnd(25, " ");
return `${testName} ${bar} ${params.value}/${params.total} | ${percentage}% | Duration: ${duration}s`;
}
// Export an instance to make a singleton
export default new TestRegister();

View file

@ -33,6 +33,10 @@ function handleTestResult(testStatus, testResult) {
testStatus.allTestsPassing = testStatus.allTestsPassing && testResult.status === "passing";
testStatus.counts[testResult.status] = (testStatus.counts[testResult.status] || 0) + 1;
testStatus.counts.total += 1;
if (testResult.duration > 2000) {
console.log(`'${testResult.test.name}' took ${(testResult.duration / 1000).toFixed(1)}s to complete`);
}
}
/**
@ -42,8 +46,6 @@ function handleTestResult(testStatus, testResult) {
* @param {Object[]} results - results from TestRegister
*/
export function logTestReport(testStatus, results) {
console.log("Tests completed.");
results.forEach(r => handleTestResult(testStatus, r));
console.log();
@ -80,8 +82,9 @@ export function logTestReport(testStatus, results) {
* Fail if the process takes longer than 60 seconds.
*/
export function setLongTestFailure() {
const timeLimit = 120;
setTimeout(function() {
console.log("Tests took longer than 60 seconds to run, returning.");
console.log(`Tests took longer than ${timeLimit} seconds to run, returning.`);
process.exit(1);
}, 60 * 1000);
}, timeLimit * 1000);
}

View file

@ -35,6 +35,7 @@ setLongTestFailure();
const logOpsTestReport = logTestReport.bind(null, testStatus);
TestRegister.runApiTests()
.then(logOpsTestReport);
(async function() {
const results = await TestRegister.runApiTests();
logOpsTestReport(results);
})();

View file

@ -588,7 +588,7 @@ Password: 034148`;
const result = await chef.generatePGPKeyPair("Back To the Drawing Board", {
keyType: "ECC-256",
});
assert.strictEqual(result.toString().length, 2005);
assert.strictEqual(result.toString().length, 2007);
}),
it("Generate UUID", () => {

View file

@ -96,6 +96,9 @@ import "./tests/DefangIP.mjs";
import "./tests/ParseUDP.mjs";
import "./tests/AvroToJSON.mjs";
import "./tests/Lorenz.mjs";
import "./tests/LuhnChecksum.mjs";
import "./tests/CipherSaber2.mjs";
import "./tests/Colossus.mjs";
// Cannot test operations that use the File type yet
@ -112,5 +115,8 @@ setLongTestFailure();
const logOpsTestReport = logTestReport.bind(null, testStatus);
TestRegister.runTests()
.then(logOpsTestReport);
(async function() {
const results = await TestRegister.runTests();
logOpsTestReport(results);
})();

View file

@ -0,0 +1,45 @@
/**
* Ciphersaber2 tests.
*
* @author n1073645 [n1073645@gmail.com]
*
* @copyright Crown Copyright 2020
* @license Apache-2.0
*/
import TestRegister from "../../lib/TestRegister.mjs";
TestRegister.addTests([
{
name: "CipherSaber2 Encrypt",
input: "Hello World",
expectedMatch: /.{21}/s,
recipeConfig: [
{
op: "CipherSaber2 Encrypt",
args: [{ "option": "Latin1", "string": "test" }, 20],
},
],
},
{
name: "CipherSaber2 Decrypt",
input: "\x5d\xd9\x7f\xeb\x77\x3c\x42\x9d\xfe\x9c\x3b\x21\x63\xbd\x53\x38\x18\x7c\x36\x37",
expectedOutput: "helloworld",
recipeConfig: [
{
op: "CipherSaber2 Decrypt",
args: [{ "option": "Latin1", "string": "test" }, 20],
},
],
},
{
name: "CipherSaber2 Encrypt",
input: "",
expectedMatch: /.{10}/s,
recipeConfig: [
{
op: "CipherSaber2 Encrypt",
args: [{ "option": "Latin1", "string": "" }, 20],
},
],
},
]);

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,32 @@
/**
* @author MarvinJWendt [git@marvinjwendt.com]
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/
import TestRegister from "../../lib/TestRegister.mjs";
TestRegister.addTests([
{
name: "Convert to NATO alphabet: nothing",
input: "",
expectedOutput: "",
recipeConfig: [
{
op: "Convert to NATO alphabet",
args: []
}
]
},
{
name: "Convert to NATO alphabet: full alphabet with numbers",
input: "abcdefghijklmnopqrstuvwxyz0123456789,/.",
expectedOutput: "Alfa Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu Zero One Two Three Four Five Six Seven Eight Nine Comma Fraction bar Full stop ",
recipeConfig: [
{
op: "Convert to NATO alphabet",
args: []
}
]
}
]);

View file

@ -0,0 +1,66 @@
/**
* From Decimal tests
*
* @author n1073645 [n1073645@gmail.com]
* @copyright Crown Copyright 2020
* @licence Apache-2.0
*/
import TestRegister from "../../lib/TestRegister.mjs";
TestRegister.addTests([
{
name: "Luhn Checksum on standard data",
input: "35641709012469",
expectedOutput: "Checksum: 7\nCheckdigit: 0\nLuhn Validated String: 356417090124690",
recipeConfig: [
{
op: "Luhn Checksum",
args: []
},
],
},
{
name: "Luhn Checksum on standard data 2",
input: "896101950123440000",
expectedOutput: "Checksum: 5\nCheckdigit: 1\nLuhn Validated String: 8961019501234400001",
recipeConfig: [
{
op: "Luhn Checksum",
args: []
},
],
},
{
name: "Luhn Checksum on standard data 3",
input: "35726908971331",
expectedOutput: "Checksum: 6\nCheckdigit: 7\nLuhn Validated String: 357269089713317",
recipeConfig: [
{
op: "Luhn Checksum",
args: []
},
],
},
{
name: "Luhn Checksum on invalid data",
input: "35641709b012469",
expectedOutput: "Character: b is not a digit.",
recipeConfig: [
{
op: "Luhn Checksum",
args: []
},
],
},
{
name: "Luhn Checksum on empty data",
input: "",
expectedOutput: "",
recipeConfig: [
{
op: "Luhn Checksum",
args: []
},
],
}
]);