From COBS operation invalid input tests

This commit is contained in:
Imantas Lukenskas 2026-04-09 22:12:09 +03:00 committed by Imantas Lukenskas
parent bf09d2158e
commit 82f7beb3fd
2 changed files with 52 additions and 1 deletions

View File

@ -4,6 +4,8 @@
* @license Apache-2.0
*/
import OperationError from "../errors/OperationError.mjs";
/**
* COBS-encode a byte array
* @param {Uint8Array} data
@ -23,7 +25,7 @@ export function toCobs(data) {
if ((endIndex < 0 || endIndex > 254) && data.length > 254) {
output.push(255);
output.push(...data.slice(1, 255));
data = [...data.slice(255)];
data = data.slice(255);
if (data.length !== 0) {
data = [0, ...data];
}
@ -51,6 +53,10 @@ export function fromCobs(data) {
return new Uint8Array();
}
if (data.findIndex((value) => value === 0) >= 0) {
throw new OperationError("Could not decode from COBS: payload must not contain a 0x00 byte");
}
const output = [];
while (data.length > 0) {

View File

@ -428,4 +428,49 @@ TestRegister.addTests([
}
]
},
{
"name": "From COBS Invalid input #1",
"input": "00 48 45 4C 4C 4F",
"expectedOutput": "Could not decode from COBS: payload must not contain a 0x00 byte",
"recipeConfig": [
{
"op": "From Hex",
"args": ["Auto"]
},
{
"op": "From COBS",
"args": []
}
],
},
{
"name": "From COBS Invalid input #2",
"input": "48 45 00 4C 4C 4F",
"expectedOutput": "Could not decode from COBS: payload must not contain a 0x00 byte",
"recipeConfig": [
{
"op": "From Hex",
"args": ["Auto"]
},
{
"op": "From COBS",
"args": []
}
],
},
{
"name": "From COBS Invalid input #3",
"input": "48 45 4C 4C 4F 00",
"expectedOutput": "Could not decode from COBS: payload must not contain a 0x00 byte",
"recipeConfig": [
{
"op": "From Hex",
"args": ["Auto"]
},
{
"op": "From COBS",
"args": []
}
],
},
]);