ethers.js/src.ts/_tests/test-rlp.ts

102 lines
3.2 KiB
TypeScript
Raw Permalink Normal View History

2022-09-05 16:14:43 -04:00
import assert from "assert";
import { loadTests } from "./utils.js";
import { decodeRlp, encodeRlp } from "../index.js";
import type { TestCaseRlp } from "./types.js";
describe("Test RLP Coder", function() {
const tests = loadTests<TestCaseRlp>("rlp");
tests.forEach(({ name, encoded, decoded }) => {
it(`encodes RLP: ${ name }`, function() {
assert.equal(encodeRlp(decoded), encoded);
});
});
tests.forEach(({ name, encoded, decoded }) => {
it(`decodes RLP: ${ name }`, function() {
assert.deepStrictEqual(decodeRlp(encoded), decoded);
});
});
});
describe("Test bad RLP Data", function() {
2022-11-27 21:59:20 -05:00
it("correctly fails encoding data with invalid values", function() {
2022-09-05 16:14:43 -04:00
assert.throws(() => {
encodeRlp([ "0x1234", <string><unknown>1234 ]);
}, (error: any) => {
return (error.code === "INVALID_ARGUMENT" &&
error.argument === "object" &&
error.value === 1234)
});
});
2022-11-27 21:59:20 -05:00
it("correctlyfails decoding data with trailing junk", function() {
2022-09-05 16:14:43 -04:00
assert.throws(() => {
// Zeros_1
decodeRlp("0x0042");
}, (error: any) => {
return (error.code === "INVALID_ARGUMENT" &&
error.message.match(/^unexpected junk after rlp payload/) &&
error.argument === "data" &&
error.value === "0x0042")
});
});
2022-11-27 21:59:20 -05:00
it ("correctlyfails decoding short data", function() {
2022-09-05 16:14:43 -04:00
assert.throws(() => {
decodeRlp("0x");
}, (error: any) => {
return (error.code === "BUFFER_OVERRUN" &&
error.message.match(/^data too short/) &&
Buffer.from(error.buffer).toString("hex") === "" &&
error.offset === 1 &&
error.length === 0)
});
});
2022-11-27 21:59:20 -05:00
it ("correctlyfails decoding short data in child", function() {
2022-09-05 16:14:43 -04:00
assert.throws(() => {
decodeRlp("0xc8880102030405060708");
}, (error: any) => {
return (error.code === "BUFFER_OVERRUN" &&
error.message.match(/^child data too short/) &&
Buffer.from(error.buffer).toString("hex") === "c8880102030405060708" &&
error.offset === 0 &&
error.length === 8)
});
});
2022-11-27 21:59:20 -05:00
it ("correctlyfails decoding short segment data", function() {
2022-09-05 16:14:43 -04:00
assert.throws(() => {
// [["0x4243"], ["0x3145"]] = 0xc8 c3 82 4243 c3 82 3145
// XXXX
decodeRlp("0xc8c382c3823145");
}, (error: any) => {
return (error.code === "BUFFER_OVERRUN" &&
error.message.match(/^data short segment too short/) &&
Buffer.from(error.buffer).toString("hex") === "c8c382c3823145" &&
error.offset === 9 &&
error.length === 7)
});
});
});
/*
utils.RLP.encode([["0x4243"], ["0x3145"]])
0xc8 c3 82 4243 c3 82 3145
{
"name": "arrayShort2",
"decoded": [
"0x48656c6c6f20576f726c64",
"0x48656c6c6f20576f726c64"
],
"encoded": "0xd8 8b 48656c6c6f20576f726c64 8b 48656c6c6f20576f726c64"
},
*/