|
| 1 | +// kind of hand-written fuzzing data |
| 2 | +// any errors should not break Encoder/Decoder instance states |
1 | 3 | import assert from "assert"; |
2 | | -import { encode, decode, decodeAsync } from "../src"; |
| 4 | +import { encode, decode, decodeAsync, Encoder, Decoder } from "../src"; |
3 | 5 | import { DataViewIndexOutOfBoundsError } from "../src/Decoder"; |
4 | 6 |
|
| 7 | +function testEncoder(encoder: Encoder): void { |
| 8 | + const object = { |
| 9 | + foo: 1, |
| 10 | + bar: 2, |
| 11 | + baz: ["one", "two", "three"], |
| 12 | + }; |
| 13 | + assert.deepStrictEqual(decode(encoder.encode(object)), object); |
| 14 | +} |
| 15 | + |
| 16 | +function testDecoder(decoder: Decoder): void { |
| 17 | + const object = { |
| 18 | + foo: 1, |
| 19 | + bar: 2, |
| 20 | + baz: ["one", "two", "three"], |
| 21 | + }; |
| 22 | + assert.deepStrictEqual(decoder.decode(encode(object)), object); |
| 23 | +} |
| 24 | + |
5 | 25 | describe("edge cases", () => { |
6 | 26 | context("try to encode cyclic refs", () => { |
7 | 27 | it("throws errors on arrays", () => { |
| 28 | + const encoder = new Encoder(); |
8 | 29 | const cyclicRefs: Array<any> = []; |
9 | 30 | cyclicRefs.push(cyclicRefs); |
10 | 31 | assert.throws(() => { |
11 | | - encode(cyclicRefs); |
| 32 | + encoder.encode(cyclicRefs); |
12 | 33 | }, /too deep/i); |
| 34 | + testEncoder(encoder); |
13 | 35 | }); |
14 | 36 |
|
15 | 37 | it("throws errors on objects", () => { |
| 38 | + const encoder = new Encoder(); |
16 | 39 | const cyclicRefs: Record<string, any> = {}; |
17 | 40 | cyclicRefs["foo"] = cyclicRefs; |
18 | 41 | assert.throws(() => { |
19 | | - encode(cyclicRefs); |
| 42 | + encoder.encode(cyclicRefs); |
20 | 43 | }, /too deep/i); |
| 44 | + testEncoder(encoder); |
21 | 45 | }); |
22 | 46 | }); |
23 | 47 |
|
24 | | - context("try to encode non-encodable objects", () => { |
| 48 | + context("try to encode unrecognized objects", () => { |
25 | 49 | it("throws errors", () => { |
| 50 | + const encoder = new Encoder(); |
26 | 51 | assert.throws(() => { |
27 | 52 | encode(() => {}); |
28 | 53 | }, /unrecognized object/i); |
| 54 | + testEncoder(encoder); |
29 | 55 | }); |
30 | 56 | }); |
31 | 57 |
|
32 | 58 | context("try to decode a map with non-string keys (asynchronous)", () => { |
33 | 59 | it("throws errors", async () => { |
| 60 | + const decoder = new Decoder(); |
34 | 61 | const createStream = async function* () { |
35 | 62 | yield [0x81]; // fixmap size=1 |
36 | 63 | yield encode(null); |
37 | 64 | yield encode(null); |
38 | 65 | }; |
39 | 66 |
|
40 | 67 | await assert.rejects(async () => { |
41 | | - await decodeAsync(createStream()); |
| 68 | + await decoder.decodeAsync(createStream()); |
42 | 69 | }, /The type of key must be string/i); |
| 70 | + testDecoder(decoder); |
43 | 71 | }); |
44 | 72 | }); |
45 | 73 |
|
46 | | - context("try to decode invlid MessagePack binary", () => { |
| 74 | + context("try to decode invalid MessagePack binary", () => { |
47 | 75 | it("throws errors", () => { |
| 76 | + const decoder = new Decoder(); |
48 | 77 | const TYPE_NEVER_USED = 0xc1; |
49 | 78 |
|
50 | 79 | assert.throws(() => { |
51 | | - decode([TYPE_NEVER_USED]); |
| 80 | + decoder.decode([TYPE_NEVER_USED]); |
52 | 81 | }, /unrecognized type byte/i); |
| 82 | + testDecoder(decoder); |
53 | 83 | }); |
54 | 84 | }); |
55 | 85 |
|
56 | 86 | context("try to decode insufficient data", () => { |
57 | 87 | it("throws errors (synchronous)", () => { |
| 88 | + const decoder = new Decoder(); |
58 | 89 | assert.throws(() => { |
59 | | - decode([ |
| 90 | + decoder.decode([ |
60 | 91 | 0x92, // fixarray size=2 |
61 | 92 | 0xc0, // nil |
62 | 93 | ]); |
63 | 94 | // [IE11] A raw error thrown by DataView |
64 | 95 | }, DataViewIndexOutOfBoundsError); |
| 96 | + testDecoder(decoder); |
65 | 97 | }); |
66 | 98 |
|
67 | 99 | it("throws errors (asynchronous)", async () => { |
| 100 | + const decoder = new Decoder(); |
68 | 101 | const createStream = async function* () { |
69 | 102 | yield [0x92]; // fixarray size=2 |
70 | 103 | yield encode(null); |
71 | 104 | }; |
72 | 105 |
|
73 | 106 | await assert.rejects(async () => { |
74 | | - await decodeAsync(createStream()); |
| 107 | + await decoder.decodeAsync(createStream()); |
75 | 108 | }, RangeError); |
| 109 | + testDecoder(decoder); |
76 | 110 | }); |
77 | 111 | }); |
78 | 112 |
|
79 | 113 | context("try to decode data with extra bytes", () => { |
80 | 114 | it("throws errors (synchronous)", () => { |
| 115 | + const decoder = new Decoder(); |
81 | 116 | assert.throws(() => { |
82 | | - decode([ |
| 117 | + decoder.decode([ |
83 | 118 | 0x90, // fixarray size=0 |
84 | 119 | ...encode(null), |
85 | 120 | ]); |
86 | 121 | }, RangeError); |
| 122 | + testDecoder(decoder); |
87 | 123 | }); |
88 | 124 |
|
89 | 125 | it("throws errors (asynchronous)", async () => { |
| 126 | + const decoder = new Decoder(); |
90 | 127 | const createStream = async function* () { |
91 | 128 | yield [0x90]; // fixarray size=0 |
92 | 129 | yield encode(null); |
93 | 130 | }; |
94 | 131 |
|
95 | 132 | await assert.rejects(async () => { |
96 | | - await decodeAsync(createStream()); |
| 133 | + await decoder.decodeAsync(createStream()); |
97 | 134 | }, RangeError); |
| 135 | + testDecoder(decoder); |
98 | 136 | }); |
99 | 137 |
|
100 | 138 | it("throws errors (asynchronous)", async () => { |
| 139 | + const decoder = new Decoder(); |
101 | 140 | const createStream = async function* () { |
102 | 141 | yield [0x90, ...encode(null)]; // fixarray size=0 + nil |
103 | 142 | }; |
104 | 143 |
|
105 | 144 | await assert.rejects(async () => { |
106 | | - await decodeAsync(createStream()); |
| 145 | + await decoder.decodeAsync(createStream()); |
107 | 146 | }, RangeError); |
| 147 | + testDecoder(decoder); |
108 | 148 | }); |
109 | 149 | }); |
110 | 150 | }); |
0 commit comments