/* eslint no-console: 0 */
/**
* nodeApi.js
*
* Test node api operations
*
* Aim of these tests is to ensure each arg type is
* handled correctly by the wrapper.
*
* @author d98762625 [d98762625@gmail.com]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import assert from "assert";
import it from "../assertionHandler";
import {
addLineNumbers,
adler32Checksum,
AESDecrypt,
affineCipherDecode,
affineCipherEncode,
bifidCipherEncode,
bitShiftRight,
cartesianProduct,
CSSMinify,
toBase64,
toHex,
} from "../../../src/node/index";
import chef from "../../../src/node/index";
import TestRegister from "../../TestRegister";
TestRegister.addApiTests([
it("ADD: toggleString argument", () => {
const result = chef.ADD("sample input", {
key: {
string: "some key",
option: "Hex"
}
});
assert.equal(result.toString(), "aO[^ZS\u000eW\\^cb");
}),
it("addLineNumbers: No arguments", () => {
const result = addLineNumbers("sample input");
assert.equal(result.toString(), "1 sample input");
}),
it("adler32Checksum: No args", () => {
const result = adler32Checksum("sample input");
assert.equal(result.toString(), "1f2304d3");
}),
it("AES decrypt: toggleString and option", () => {
const result = AESDecrypt("812c34ae6af353244a63c6ce23b7c34286b60be28ea4645523d4494700e7", {
key: {
string: "some longer key1",
option: "utf8",
},
iv: {
string: "some iv",
option: "utf8",
},
mode: "OFB",
});
assert.equal(result.toString(), "a slightly longer sampleinput?");
}),
it("AffineCipherDecode: number input", () => {
const result = affineCipherDecode("some input", {
a: 7,
b: 4
});
assert.strictEqual(result.toString(), "cuqa ifjgr");
}),
it("affineCipherEncode: number input", () => {
const result = affineCipherEncode("some input", {
a: 11,
b: 6
});
assert.strictEqual(result.toString(), "weiy qtpsh");
}),
it("analyzeHash", () => {
const result = chef.analyseHash(chef.MD5("some input"));
const expected = `Hash length: 32
Byte length: 16
Bit length: 128
Based on the length, this hash could have been generated by one of the following hashing functions:
MD5
MD4
MD2
HAVAL-128
RIPEMD-128
Snefru
Tiger-128`;
assert.strictEqual(result.toString(), expected);
}),
it("AND", () => {
const result = chef.AND("Scot-free", {
key: "Raining Cats and Dogs",
});
assert.strictEqual(result.toString(), ".\"M$(D E.");
}),
it("atBash Cipher", () => {
const result = chef.atbashCipher("Happy as a Clam");
assert.strictEqual(result.toString(), "Szkkb zh z Xozn");
}),
it("Bcrypt", () => {
const result = chef.bcrypt("Put a Sock In It");
assert.strictEqual(result.toString(), "$2a$10$ODeP1.6fMsb.ENk2ngPUCO7qTGVPyHA9TqDVcyupyed8FjsiF65L6");
}),
it("bcryptCompare", () => {
const result = chef.bcryptCompare("Put a Sock In It", {
hash: "$2a$10$2rT4a3XnIecBsd1H33dMTuyYE1HJ1n9F.V2rjQtAH73rh1qvOf/ae",
});
assert.strictEqual(result.toString(), "Match: Put a Sock In It");
}),
it("Bcrypt Parse", () => {
const result = chef.bcryptParse("$2a$10$ODeP1.6fMsb.ENk2ngPUCO7qTGVPyHA9TqDVcyupyed8FjsiF65L6");
const expected = `Rounds: 10
Salt: $2a$10$ODeP1.6fMsb.ENk2ngPUCO
Password hash: 7qTGVPyHA9TqDVcyupyed8FjsiF65L6
Full hash: $2a$10$ODeP1.6fMsb.ENk2ngPUCO7qTGVPyHA9TqDVcyupyed8FjsiF65L6`;
assert.strictEqual(result.toString(), expected);
}),
it("bifid cipher decode", () => {
const result = chef.bifidCipherDecode("Vhef Qnte Ke Xfhz Mxon Bmgf", {
keyword: "Alpha",
});
assert.strictEqual(result.toString(), "What Goes Up Must Come Down");
}),
it("bifid cipher encode: string option", () => {
const result = bifidCipherEncode("some input", {
keyword: "mykeyword",
});
assert.strictEqual(result.toString(), "nmhs zmsdo");
}),
it("bit shift left", () => {
const result = chef.bitShiftLeft("Keep Your Eyes Peeled");
assert.strictEqual(result.toString(), ".ÊÊà@²Þêä@.òÊæ@ ÊÊØÊÈ");
}),
it("bitShiftRight: number and option", () => {
const result = bitShiftRight("some bits to shift", {
type: "Arithmetic shift",
amount: 1,
});
assert.strictEqual(result.toString(), "9762\u001014:9\u0010:7\u00109443:");
}),
it("Blowfish encrypt", () => {
const result = chef.blowfishEncrypt("Fool's Gold", {
key: {
string: "One",
option: "hex",
},
iv: {
string: "Two",
option: "utf8"
}
});
assert.strictEqual(result.toString(), "8999b513bf2ff064b2977dea7e05f1b5");
}),
it("Blowfish decrypt", () => {
const result = chef.blowfishDecrypt("8999b513bf2ff064b2977dea7e05f1b5", {
key: {
string: "One",
option: "hex",
},
iv: {
string: "Two",
option: "utf8",
}
});
assert.strictEqual(result.toString(), "Fool's Gold");
}),
it("BSON Deserialise", () => {
const result = chef.BSONDeserialise("....phrase.....Mouth-watering..");
assert.strictEqual(result.toString(), "{\"phrase\": \"Mouth-watering\"}");
}),
it("BSON Serialise", () => {
const result = chef.BSONSerialise({"phrase": "Mouth-watering"});
assert.strictEqual(result.toString(), " ....phrase.....Mouth-watering..");
}),
it("Bzip2 Decompress", () => {
const result = chef.bzip2Decompress(chef.fromBase64("QlpoOTFBWSZTWUdQlt0AAAIVgEAAAQAmJAwAIAAxBkxA0A2pTL6U2CozxdyRThQkEdQlt0A="));
assert.strictEqual(result.toString(), "Fit as a Fiddle");
}),
it("cartesianProduct: binary string", () => {
const result = cartesianProduct("1:2\\n\\n3:4", {
itemDelimiter: ":",
});
assert.strictEqual(result.toString(), "(1,3):(1,4):(2,3):(2,4)");
}),
it("Change IP format", () => {
const result = chef.changeIPFormat("172.20.23.54", {
inputFormat: "Dotted Decimal",
outputFormat: "Hex",
});
assert.strictEqual(result.toString(), "ac141736");
}),
it("Chi square", () => {
const result = chef.chiSquare("Burst Your Bubble");
assert.strictEqual(result.toString(), "433.55399816176475");
}),
it("Compare CTPH Hashes", () => {
const result = chef.compareCTPHHashes("1234\n3456");
assert.strictEqual(result.toString(), "0");
}),
it("Compare SSDEEPHashes", () => {
const result = chef.compareCTPHHashes("1234\n3456");
assert.strictEqual(result.toString(), "0");
}),
it("Convert area", () => {
const result = chef.convertArea("12345", {
inputUnits: "Square metre (sq m)",
outputUnits: "Isle of Wight"
});
assert.strictEqual(result.toString(), "0.00003248684210526316");
}),
it("Convert data units", () => {
const result = chef.convertDataUnits("12345", {
inputUnits: "Bits (b)",
outputUnits: "Kilobytes (KB)",
});
assert.strictEqual(result.toString(), "1.543125");
}),
it("Convert distance", () => {
const result = chef.convertDistance("1234567", {
inputUnits: "Nanometres (nm)",
outputUnits: "Furlongs (fur)",
});
assert.strictEqual(result.toString(), "0.00000613699494949495");
}),
it("Convert mass", () => {
const result = chef.convertMass("123", {
inputUnits: "Earth mass (M⊕)",
outputUnits: "Great Pyramid of Giza (6,000,000 tonnes)",
});
assert.strictEqual(result.toString(), "122429895000000000");
}),
it("Convert speed", () => {
const result = chef.convertSpeed("123", {
inputUnits: "Lunar escape velocity",
outputUnits: "Jet airliner cruising speed",
});
assert.strictEqual(result.toString(), "1168.5");
}),
it("Count occurrences", () => {
const result = chef.countOccurrences("Talk the Talk", {
searchString: {
string: "Tal",
option: "Simple string",
}
});
assert.strictEqual(result.toString(), "2");
}),
it("CRC16 Checksum", () => {
const result = chef.CRC16Checksum("Rain on Your Parade");
assert.strictEqual(result.toString(), "db1c");
}),
it("CRC32 Checksum", () => {
const result = chef.CRC32Checksum("Rain on Your Parade");
assert.strictEqual(result.toString(), "e902f76c");
}),
it("CSS Beautify", () => {
const result = chef.CSSBeautify("header {color:black;padding:3rem;}");
const expected = `header {
\\tcolor:black;
\\tpadding:3rem;
}
`;
assert.strictEqual(result.toString(), expected);
}),
it("CSS minify: boolean", () => {
const input = `header {
// comment
width: 100%;
color: white;
}`;
const result = CSSMinify(input, {
preserveComments: true,
});
assert.strictEqual(result.toString(), "header {// comment width: 100%;color: white;}");
}),
it("CSS Selector", () => {
const result = chef.CSSSelector("Hello