Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 991 - CBOR operations #999

Merged
merged 2 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@
"Parse TLV",
"CSV to JSON",
"JSON to CSV",
"Avro to JSON"
"Avro to JSON",
"CBOR Encode",
"CBOR Decode"
]
},
{
Expand Down
42 changes: 42 additions & 0 deletions src/core/operations/CBORDecode.mjs
Original file line number Diff line number Diff line change
@@ -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;
41 changes: 41 additions & 0 deletions src/core/operations/CBOREncode.mjs
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions tests/operations/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ import "./tests/LuhnChecksum.mjs";
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
Expand Down
145 changes: 145 additions & 0 deletions tests/operations/tests/CBORDecode.mjs
Original file line number Diff line number Diff line change
@@ -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: []
}
]
}
]);
Loading