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

100% Statements 101/101
100% Branches 21/21
100% Functions 0/0
100% Lines 101/101

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 1021x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 61x 61x 61x 1x 1x 1x 61x 61x 61x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import assert from "assert";
 
import { loadTests } from "./utils.js";
 
import { decodeRlp, encodeRlp, hexlify } 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.deepEqual(decodeRlp(encoded), decoded);
        });
    });
});
 
describe("Test bad RLP Data", function() {
    it("correctly fails encoding data with invalid values", function() {
        assert.throws(() => {
            encodeRlp([ "0x1234", <string><unknown>1234 ]);
        }, (error: any) => {
            return (error.code === "INVALID_ARGUMENT" &&
                error.argument === "object" &&
                error.value === 1234)
        });
    });
 
    it("correctlyfails decoding data with trailing junk", function() {
        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")
        });
    });
 
    it ("correctlyfails decoding short data", function() {
        assert.throws(() => {
            decodeRlp("0x");
        }, (error: any) => {
            return (error.code === "BUFFER_OVERRUN" &&
                error.message.match(/^data too short/) &&
                hexlify(error.buffer) === "0x" &&
                error.offset === 1 &&
                error.length === 0)
        });
    });
 
    it ("correctlyfails decoding short data in child", function() {
        assert.throws(() => {
            decodeRlp("0xc8880102030405060708");
        }, (error: any) => {
            return (error.code === "BUFFER_OVERRUN" &&
                error.message.match(/^child data too short/) &&
                hexlify(error.buffer) === "0xc8880102030405060708" &&
                error.offset === 0 &&
                error.length === 8)
        });
    });
 
    it ("correctlyfails decoding short segment data", function() {
        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/) &&
                hexlify(error.buffer) === "0xc8c382c3823145" &&
                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"
 },
*/