2022-09-05 23:57:11 +03:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2023-04-25 14:04:48 +03:00
|
|
|
const tslib_1 = require("tslib");
|
|
|
|
const assert_1 = tslib_1.__importDefault(require("assert"));
|
2022-09-05 23:57:11 +03:00
|
|
|
const utils_js_1 = require("./utils.js");
|
|
|
|
const index_js_1 = require("../index.js");
|
|
|
|
describe("Test RLP Coder", function () {
|
|
|
|
const tests = (0, utils_js_1.loadTests)("rlp");
|
|
|
|
tests.forEach(({ name, encoded, decoded }) => {
|
|
|
|
it(`encodes RLP: ${name}`, function () {
|
|
|
|
assert_1.default.equal((0, index_js_1.encodeRlp)(decoded), encoded);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
tests.forEach(({ name, encoded, decoded }) => {
|
|
|
|
it(`decodes RLP: ${name}`, function () {
|
2023-05-06 09:16:41 +03:00
|
|
|
assert_1.default.deepEqual((0, index_js_1.decodeRlp)(encoded), decoded);
|
2022-09-05 23:57:11 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe("Test bad RLP Data", function () {
|
2022-11-30 23:44:23 +03:00
|
|
|
it("correctly fails encoding data with invalid values", function () {
|
2022-09-05 23:57:11 +03:00
|
|
|
assert_1.default.throws(() => {
|
|
|
|
(0, index_js_1.encodeRlp)(["0x1234", 1234]);
|
|
|
|
}, (error) => {
|
|
|
|
return (error.code === "INVALID_ARGUMENT" &&
|
|
|
|
error.argument === "object" &&
|
|
|
|
error.value === 1234);
|
|
|
|
});
|
|
|
|
});
|
2022-11-30 23:44:23 +03:00
|
|
|
it("correctlyfails decoding data with trailing junk", function () {
|
2022-09-05 23:57:11 +03:00
|
|
|
assert_1.default.throws(() => {
|
|
|
|
// Zeros_1
|
|
|
|
(0, index_js_1.decodeRlp)("0x0042");
|
|
|
|
}, (error) => {
|
|
|
|
return (error.code === "INVALID_ARGUMENT" &&
|
|
|
|
error.message.match(/^unexpected junk after rlp payload/) &&
|
|
|
|
error.argument === "data" &&
|
|
|
|
error.value === "0x0042");
|
|
|
|
});
|
|
|
|
});
|
2022-11-30 23:44:23 +03:00
|
|
|
it("correctlyfails decoding short data", function () {
|
2022-09-05 23:57:11 +03:00
|
|
|
assert_1.default.throws(() => {
|
|
|
|
(0, index_js_1.decodeRlp)("0x");
|
|
|
|
}, (error) => {
|
|
|
|
return (error.code === "BUFFER_OVERRUN" &&
|
|
|
|
error.message.match(/^data too short/) &&
|
2023-05-06 09:16:41 +03:00
|
|
|
(0, index_js_1.hexlify)(error.buffer) === "0x" &&
|
2022-09-05 23:57:11 +03:00
|
|
|
error.offset === 1 &&
|
|
|
|
error.length === 0);
|
|
|
|
});
|
|
|
|
});
|
2022-11-30 23:44:23 +03:00
|
|
|
it("correctlyfails decoding short data in child", function () {
|
2022-09-05 23:57:11 +03:00
|
|
|
assert_1.default.throws(() => {
|
|
|
|
(0, index_js_1.decodeRlp)("0xc8880102030405060708");
|
|
|
|
}, (error) => {
|
|
|
|
return (error.code === "BUFFER_OVERRUN" &&
|
|
|
|
error.message.match(/^child data too short/) &&
|
2023-05-06 09:16:41 +03:00
|
|
|
(0, index_js_1.hexlify)(error.buffer) === "0xc8880102030405060708" &&
|
2022-09-05 23:57:11 +03:00
|
|
|
error.offset === 0 &&
|
|
|
|
error.length === 8);
|
|
|
|
});
|
|
|
|
});
|
2022-11-30 23:44:23 +03:00
|
|
|
it("correctlyfails decoding short segment data", function () {
|
2022-09-05 23:57:11 +03:00
|
|
|
assert_1.default.throws(() => {
|
|
|
|
// [["0x4243"], ["0x3145"]] = 0xc8 c3 82 4243 c3 82 3145
|
|
|
|
// XXXX
|
|
|
|
(0, index_js_1.decodeRlp)("0xc8c382c3823145");
|
|
|
|
}, (error) => {
|
|
|
|
return (error.code === "BUFFER_OVERRUN" &&
|
|
|
|
error.message.match(/^data short segment too short/) &&
|
2023-05-06 09:16:41 +03:00
|
|
|
(0, index_js_1.hexlify)(error.buffer) === "0xc8c382c3823145" &&
|
2022-09-05 23:57:11 +03:00
|
|
|
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"
|
|
|
|
},
|
|
|
|
*/
|
|
|
|
//# sourceMappingURL=test-rlp.js.map
|