From ae70cb89ed9c04a0a5dd4f3e24e02ff900189c59 Mon Sep 17 00:00:00 2001 From: 71819 Date: Thu, 26 Mar 2020 11:44:57 +0000 Subject: [PATCH 1/2] Issue 991: Add CBOR Encode operation --- package-lock.json | 14 +++ package.json | 1 + src/core/config/Categories.json | 3 +- src/core/operations/CBOREncode.mjs | 41 +++++++++ tests/operations/index.mjs | 1 + tests/operations/tests/CBOREncode.mjs | 118 ++++++++++++++++++++++++++ 6 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 src/core/operations/CBOREncode.mjs create mode 100644 tests/operations/tests/CBOREncode.mjs diff --git a/package-lock.json b/package-lock.json index caaa90b2..76ee7763 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2965,6 +2965,15 @@ "integrity": "sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw=", "dev": true }, + "cbor": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/cbor/-/cbor-5.0.1.tgz", + "integrity": "sha512-l4ghwqioCyuAaD3LvY4ONwv8NMuERz62xjbMHGdWBqERJPygVmoFER1b4+VS6iW0rXwoVGuKZPPPTofwWOg3YQ==", + "requires": { + "bignumber.js": "^9.0.0", + "nofilter": "^1.0.3" + } + }, "chai-nightwatch": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/chai-nightwatch/-/chai-nightwatch-0.4.0.tgz", @@ -9828,6 +9837,11 @@ "resolved": "https://registry.npmjs.org/nodom/-/nodom-2.4.0.tgz", "integrity": "sha512-qhfYgpoCSi37HLiViMlf94YqMQdvk3n3arI1uGbAWZK9NKCYRSI42W8lATeGloYGLYxb8us1C5rTvtsXjwdWQg==" }, + "nofilter": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/nofilter/-/nofilter-1.0.3.tgz", + "integrity": "sha512-FlUlqwRK6reQCaFLAhMcF+6VkVG2caYjKQY3YsRDTl4/SEch595Qb3oLjJRDr8dkHAAOVj2pOx3VknfnSgkE5g==" + }, "nopt": { "version": "3.0.6", "resolved": "https://registry.npmjs.org/nopt/-/nopt-3.0.6.tgz", diff --git a/package.json b/package.json index a8f4198e..45c757fa 100644 --- a/package.json +++ b/package.json @@ -96,6 +96,7 @@ "bootstrap-colorpicker": "^3.2.0", "bootstrap-material-design": "^4.1.2", "bson": "^4.0.3", + "cbor": "^5.0.1", "chi-squared": "^1.1.0", "codepage": "^1.14.0", "core-js": "^3.6.4", diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 77e3d319..ffffb52c 100755 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -61,7 +61,8 @@ "Parse TLV", "CSV to JSON", "JSON to CSV", - "Avro to JSON" + "Avro to JSON", + "CBOR Encode" ] }, { diff --git a/src/core/operations/CBOREncode.mjs b/src/core/operations/CBOREncode.mjs new file mode 100644 index 00000000..9e92239e --- /dev/null +++ b/src/core/operations/CBOREncode.mjs @@ -0,0 +1,41 @@ +/** + * @author Danh4 [dan.h4@ncsc.gov.uk] + * @copyright Crown Copyright 2020 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; +import Cbor from "cbor"; + +/** + * CBOR Encode operation + */ +class CBOREncode extends Operation { + + /** + * CBOREncode constructor + */ + constructor() { + super(); + + this.name = "CBOR Encode"; + this.module = "Default"; + this.description = "2"; + this.infoURL = "https://cbor.io"; + this.inputType = "JSON"; + this.outputType = "ArrayBuffer"; + this.args = []; + } + + /** + * @param {JSON} input + * @param {Object[]} args + * @returns {ArrayBuffer} + */ + run(input, args) { + return new Uint8Array(Cbor.encodeCanonical(input)).buffer; + } + +} + +export default CBOREncode; diff --git a/tests/operations/index.mjs b/tests/operations/index.mjs index 8d3cd623..7697fbdf 100644 --- a/tests/operations/index.mjs +++ b/tests/operations/index.mjs @@ -101,6 +101,7 @@ import "./tests/LuhnChecksum.mjs"; import "./tests/CipherSaber2.mjs"; import "./tests/Colossus.mjs"; import "./tests/ParseObjectIDTimestamp.mjs"; +import "./tests/CBOREncode.mjs"; // Cannot test operations that use the File type yet diff --git a/tests/operations/tests/CBOREncode.mjs b/tests/operations/tests/CBOREncode.mjs new file mode 100644 index 00000000..0613bfe6 --- /dev/null +++ b/tests/operations/tests/CBOREncode.mjs @@ -0,0 +1,118 @@ +/** + * CBOR Encode Tests. + * + * @author Danh4 [dan.h4@ncsc.gov.uk] + * + * @copyright Crown Copyright 2019 + * @license Apache-2.0 + */ + +import TestRegister from "../../lib/TestRegister.mjs"; + +TestRegister.addTests([ + { + name: "CBOR Encode: Can encode integer", + input: "15", + expectedOutput: "0f", + recipeConfig: [ + { + op: "CBOR Encode", + args: [] + }, + { + op: "To Hex", + args: [] + } + ] + }, + { + name: "CBOR Decode: Can encode decimal", + input: "1.5", + expectedOutput: "f9 3e 00", + recipeConfig: [ + { + op: "CBOR Encode", + args: [] + }, + { + op: "To Hex", + args: [] + } + ] + }, + { + name: "CBOR Encode: Can encode text", + input: "\"Text\"", + expectedOutput: "64 54 65 78 74", + recipeConfig: [ + { + op: "CBOR Encode", + args: [] + }, + { + op: "To Hex", + args: [] + } + ] + }, + { + name: "CBOR Encode: Can encode boolean true", + input: "true", + expectedOutput: "f5", + recipeConfig: [ + { + op: "CBOR Encode", + args: [] + }, + { + op: "To Hex", + args: [] + } + ] + }, + { + name: "CBOR Encode: Can encode boolean false", + input: "false", + expectedOutput: "f4", + recipeConfig: [ + { + op: "CBOR Encode", + args: [] + }, + { + op: "To Hex", + args: [] + } + ] + }, + { + name: "CBOR Encode: Can encode map", + input: JSON.stringify({a: 1, b: 2, c: 3}), + expectedOutput: "a3 61 61 01 61 62 02 61 63 03", + recipeConfig: [ + { + op: "CBOR Encode", + args: [] + }, + { + op: "To Hex", + args: [] + } + ] + }, + { + name: "CBOR Encode: Can encode list", + input: "[0,1,2]", + expectedOutput: "83 00 01 02", + recipeConfig: [ + { + op: "CBOR Encode", + args: [] + }, + { + op: "To Hex", + args: [] + } + ] + } +]); From 209fc07eacc7764f15bba76b76415bca0f5915a9 Mon Sep 17 00:00:00 2001 From: 71819 Date: Sat, 28 Mar 2020 20:13:49 +0000 Subject: [PATCH 2/2] Issue 991: Add CBOR Decode operation --- src/core/config/Categories.json | 3 +- src/core/operations/CBORDecode.mjs | 42 ++++++++ tests/operations/index.mjs | 1 + tests/operations/tests/CBORDecode.mjs | 145 ++++++++++++++++++++++++++ 4 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 src/core/operations/CBORDecode.mjs create mode 100644 tests/operations/tests/CBORDecode.mjs diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index ffffb52c..c9b058e2 100755 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -62,7 +62,8 @@ "CSV to JSON", "JSON to CSV", "Avro to JSON", - "CBOR Encode" + "CBOR Encode", + "CBOR Decode" ] }, { diff --git a/src/core/operations/CBORDecode.mjs b/src/core/operations/CBORDecode.mjs new file mode 100644 index 00000000..34f1abd1 --- /dev/null +++ b/src/core/operations/CBORDecode.mjs @@ -0,0 +1,42 @@ +/** + * @author Danh4 [dan.h4@ncsc.gov.uk] + * @copyright Crown Copyright 2020 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; +import Cbor from "cbor"; + +/** + * CBOR Decode operation + */ +class CBORDecode extends Operation { + + /** + * CBORDecode constructor + */ + constructor() { + super(); + + this.name = "CBOR Decode"; + this.module = "Default"; + this.description = "Decode Concise Binary Object Representation (CBOR) data format (RFC7049) to JSON."; + this.infoURL = "https://cbor.io"; + this.inputType = "ArrayBuffer"; + this.outputType = "JSON"; + this.args = [ + ]; + } + + /** + * @param {ArrayBuffer} input + * @param {Object[]} args + * @returns {JSON} + */ + run(input, args) { + return Cbor.decodeFirstSync(Buffer.from(input).toString("hex")); + } + +} + +export default CBORDecode; diff --git a/tests/operations/index.mjs b/tests/operations/index.mjs index 7697fbdf..da6a659d 100644 --- a/tests/operations/index.mjs +++ b/tests/operations/index.mjs @@ -102,6 +102,7 @@ import "./tests/CipherSaber2.mjs"; import "./tests/Colossus.mjs"; import "./tests/ParseObjectIDTimestamp.mjs"; import "./tests/CBOREncode.mjs"; +import "./tests/CBORDecode.mjs"; // Cannot test operations that use the File type yet diff --git a/tests/operations/tests/CBORDecode.mjs b/tests/operations/tests/CBORDecode.mjs new file mode 100644 index 00000000..4505b59c --- /dev/null +++ b/tests/operations/tests/CBORDecode.mjs @@ -0,0 +1,145 @@ +/** + * From Hex Tests. + * + * @author Danh4 [dan.h4@ncsc.gov.uk] + * + * @copyright Crown Copyright 2019 + * @license Apache-2.0 + */ + +import TestRegister from "../../lib/TestRegister.mjs"; + +TestRegister.addTests([ + { + name: "CBOR Decode: Can decode integer", + input: "0f", + expectedOutput: "15", + recipeConfig: [ + { + op: "From Hex", + args: [] + }, + { + op: "CBOR Decode", + args: [] + } + ] + }, + { + name: "CBOR Decode: Can decode decimal", + input: "f9 3e 00", + expectedOutput: "1.5", + recipeConfig: [ + { + op: "From Hex", + args: [] + }, + { + op: "CBOR Decode", + args: [] + } + ] + }, + { + name: "From Hex: Can decode text", + input: "64 54 65 78 74", + expectedOutput: "\"Text\"", + recipeConfig: [ + { + op: "From Hex", + args: [] + }, + { + op: "CBOR Decode", + args: [] + } + ] + }, + { + name: "From Hex: Can decode boolean true", + input: "f5", + expectedOutput: "true", + recipeConfig: [ + { + op: "From Hex", + args: [] + }, + { + op: "CBOR Decode", + args: [] + } + ] + }, + { + name: "From Hex: Can decode boolean false", + input: "f4", + expectedOutput: "false", + recipeConfig: [ + { + op: "From Hex", + args: [] + }, + { + op: "CBOR Decode", + args: [] + } + ] + }, + { + name: "From Hex: Can decode map", + input: "a3 61 61 01 61 62 02 61 63 03", + expectedOutput: JSON.stringify({a: 1, b: 2, c: 3}), + recipeConfig: [ + { + op: "From Hex", + args: [] + }, + { + op: "CBOR Decode", + args: [] + }, + { + op: "JSON Minify", + args: [] + } + ] + }, + { + name: "From Hex: Can decode list", + input: "83 00 01 02", + expectedOutput: "[0,1,2]", + recipeConfig: [ + { + op: "From Hex", + args: [] + }, + { + op: "CBOR Decode", + args: [] + }, + { + op: "JSON Minify", + args: [] + } + ] + }, + { + name: "From Hex: Can round trip with encode", + input: JSON.stringify({a: 1, b: false, c: [1, 2, 3]}), + expectedOutput: JSON.stringify({a: 1, b: false, c: [1, 2, 3]}), + recipeConfig: [ + { + op: "CBOR Encode", + args: [] + }, + { + op: "CBOR Decode", + args: [] + }, + { + op: "JSON Minify", + args: [] + } + ] + } +]);