All files / ethers.js/src.ts/_tests test-utils-utf8.ts

100% Statements 152/152
100% Branches 16/16
100% Functions 0/0
100% Lines 152/152

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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 1531x 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 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 4x 4x 1x 1x 1x 1x  
import assert from "assert";
 
import {
    toUtf8Bytes, toUtf8CodePoints, toUtf8String,
    Utf8ErrorFuncs
} from "../index.js";
 
export type TestCaseBadString = {
    name: string,
    bytes: Uint8Array,
    ignore: string,
    replace: string,
    error: string
};
 
export type TestCaseCodePoints = {
    name: string;
    text: string;
    codepoints: Array<number>;
};
 
describe("Tests UTF-8 bad strings", function() {
 
    const tests: Array<TestCaseBadString> = [
        {
            name: "unexpected continue",
            bytes: new Uint8Array([ 0x41,  0x80,  0x42,  0x43 ]),
            ignore: "ABC",
            replace: "A\ufffdBC",
            error: "UNEXPECTED_CONTINUE"
        },
        {
            name: "bad prefix",
            bytes: new Uint8Array([ 0x41,  0xf8,  0x42,  0x43 ]),
            ignore: "ABC",
            replace: "A\ufffdBC",
            error: "BAD_PREFIX"
        },
        {
            name: "bad prefix (multiple)",
            bytes: new Uint8Array([ 0x41,  0xf8, 0x88, 0x88, 0x42,  0x43 ]),
            ignore: "ABC",
            replace: "A\ufffdBC",
            error: "BAD_PREFIX"
        },
        {
            name: "OVERRUN",
            bytes: new Uint8Array([ 0x41,  0x42,  0xe2, 0x82 /* 0xac */ ]),
            ignore: "AB",
            replace: "AB\ufffd",
            error: "OVERRUN"
        },
        {
            name: "missing continue",
            bytes: new Uint8Array([ 0x41,  0x42,  0xe2,  0xe2, 0x82, 0xac,  0x43 ]),
            ignore: "AB\u20acC",
            replace: "AB\ufffd\u20acC",
            error: "MISSING_CONTINUE"
        },
        {
            name: "out-of-range",
            bytes: new Uint8Array([ 0x41,  0x42,  0xf7, 0xbf, 0xbf, 0xbf,  0x43 ]),
            ignore: "ABC",
            replace: "AB\ufffdC",
            error: "OUT_OF_RANGE"
        },
        {
            name: "UTF-16 surrogate (low)",
            bytes: new Uint8Array([ 0x41,  0x42,  0xed, 0xa0, 0x80,  0x43 ]),
            ignore: "ABC",
            replace: "AB\ufffdC",
            error: "UTF16_SURROGATE"
        },
        {
            name: "UTF-16 surrogate (high)",
            bytes: new Uint8Array([ 0x41,  0x42,  0xed, 0xbf, 0xbf,  0x43 ]),
            ignore: "ABC",
            replace: "AB\ufffdC",
            error: "UTF16_SURROGATE"
        },
        {
            name: "overlong",
            bytes: new Uint8Array([ 0xf0, 0x82, 0x82, 0xac ]),
            ignore: "",
            replace: "\u20ac",
            error: "OVERLONG"
        }
    ];
 
    for (const { name, bytes, ignore, replace, error } of tests) {
        it(`correctly handles ${ name }: replace strategy`, function() {
            const result = toUtf8String(bytes, Utf8ErrorFuncs.replace);
            assert.equal(result, replace);
        });
 
        it(`correctly handles ${ name }: ignore strategy`, function() {
            const result = toUtf8String(bytes, Utf8ErrorFuncs.ignore);
            assert.equal(result, ignore);
        });
 
        it(`correctly handles ${ name }: error strategy`, function() {
            assert.throws(() => {
                const result = toUtf8String(bytes);
                console.log(result);
            }, (e: any) => {
                return (e.message.indexOf(error) >= 0);
            });
        });
    }
 
    it("correctly fails to get UTF-8 bytes from incomplete surrogate", function() {
        assert.throws(() => {
            const text = String.fromCharCode(0xd800);;
            const result = toUtf8Bytes(text);
            console.log(result);
        }, (error: any) => {
            return (error.message.startsWith("invalid surrogate pair"));
        });
    });
 
    it("correctly fails to get UTF-8 bytes from invalid surrogate pair", function() {
        assert.throws(() => {
            const text = String.fromCharCode(0xd800, 0xdbff);;
            const result = toUtf8Bytes(text);
            console.log(result);
        }, (error: any) => {
            return (error.message.startsWith("invalid surrogate pair"));
        });
    });
});
 
describe("Tests UTF-8 bad strings", function() {
 
    const tests: Array<TestCaseCodePoints> = [
        {
            name: "the Euro symbol",
            text: "AB\u20acC",
            codepoints: [ 0x41, 0x42, 0x20ac, 0x43 ]
        },
    ];
 
    for (const { name, text, codepoints } of tests) {
        it(`expands strings to codepoints: ${ name }`, function() {
            const result = toUtf8CodePoints(text);
            assert.equal(result.length, codepoints.length, "codepoints.length");
            for (let i = 0; i < result.length; i++) {
                assert.equal(result[i], codepoints[i], `codepoints[${ i }]`);
            }
        });
    }
 
});