-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathvar-array_test.js
More file actions
249 lines (201 loc) · 7.88 KB
/
var-array_test.js
File metadata and controls
249 lines (201 loc) · 7.88 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import { XdrReader } from '../../src/serialization/xdr-reader';
import { XdrWriter } from '../../src/serialization/xdr-writer';
const subject = new XDR.VarArray(XDR.Int, 2);
describe('VarArray#read', function () {
function createFixedSizeChild() {
let calls = 0;
return {
FixedSizeChild: class FixedSizeChild {
static read(io) {
calls += 1;
return io.readInt32BE();
}
static write() {}
static isValid() {
return true;
}
},
getCalls() {
return calls;
}
};
}
it('decodes correctly', function () {
expect(read([0x00, 0x00, 0x00, 0x00])).to.eql([]);
expect(read([0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00])).to.eql([0]);
expect(read([0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])).to.eql([1]);
expect(
read([
0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
])
).to.eql([0, 1]);
expect(
read([
0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01
])
).to.eql([1, 1]);
});
it('throws read error when the encoded array is too large', function () {
expect(() => read([0x00, 0x00, 0x00, 0x03])).to.throw(/read error/i);
});
it('fast-fails when declared length cannot fit remaining bytes for fixed-size child', function () {
const { FixedSizeChild, getCalls } = createFixedSizeChild();
const arr = new XDR.VarArray(FixedSizeChild, 10);
const io = new XdrReader([0x00, 0x00, 0x00, 0x02]);
const remainingAfterLengthPrefix = io.remainingBytes() - 4;
expect(() => arr.read(io)).to.throw(
new RegExp(`exceeds remaining ${remainingAfterLengthPrefix} bytes`, 'i')
);
expect(getCalls()).to.eql(0);
});
it('decodes on exact-fit payload and reads each child once', function () {
const { FixedSizeChild, getCalls } = createFixedSizeChild();
const arr = new XDR.VarArray(FixedSizeChild, 10);
const io = new XdrReader([
0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02
]);
expect(arr.read(io)).to.eql([1, 2]);
expect(getCalls()).to.eql(2);
});
it('checks maxLength before insufficient-bytes fast-fail', function () {
const { FixedSizeChild, getCalls } = createFixedSizeChild();
const arr = new XDR.VarArray(FixedSizeChild, 1);
const io = new XdrReader([0x00, 0x00, 0x00, 0x02]);
expect(() => arr.read(io)).to.throw(/max allowed/i);
expect(getCalls()).to.eql(0);
});
it('consumes only the 4-byte length prefix on VarArray insufficient-bytes fast-fail', function () {
const { FixedSizeChild } = createFixedSizeChild();
const arr = new XDR.VarArray(FixedSizeChild, 10);
const io = new XdrReader([0x00, 0x00, 0x00, 0x02]);
const before = io.remainingBytes();
const remainingAfterLengthPrefix = io.remainingBytes() - 4;
expect(() => arr.read(io)).to.throw(
new RegExp(`exceeds remaining ${remainingAfterLengthPrefix} bytes`, 'i')
);
expect(before - io.remainingBytes()).to.eql(4);
});
function read(bytes) {
let io = new XdrReader(bytes);
return subject.read(io);
}
});
describe('VarArray#read maxDepth', function () {
it('throws when depth exceeds maxDepth', function () {
const varArrayType = new XDR.VarArray(XDR.Int, 10, 2);
const bytes = [0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05];
const reader = new XdrReader(bytes);
expect(() => varArrayType.read(reader, -1)).to.throw(
/exceeded max decoding depth.*/i
);
});
it('succeeds when depth is within maxDepth', function () {
const varArrayType = new XDR.VarArray(XDR.Int, 10, 5);
const bytes = [
0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x0a
];
const reader = new XdrReader(bytes);
const result = varArrayType.read(reader, 4);
expect(result).to.eql([5, 10]);
});
it('uses default maxDepth of 200', function () {
const varArrayType = new XDR.VarArray(XDR.Int, 10);
const bytes = [0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05];
const reader = new XdrReader(bytes);
expect(varArrayType._maxDepth).to.equal(200);
expect(() => varArrayType.read(reader, -1)).to.throw(
/exceeded max decoding depth.*/i
);
});
it('uses the root maxDepth for child types', function () {
const innerArray = new XDR.VarArray(XDR.Int, 10, 2);
const outerArray = new XDR.VarArray(innerArray, 10, 5);
const bytes = [
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01
];
const reader = new XdrReader(bytes);
const result = outerArray.read(reader, 2);
expect(result).to.eql([[1]]);
});
it('VarArray containing Strings respects depth', function () {
const stringType = new XDR.String(100);
const arrayOfStrings = new XDR.VarArray(stringType, 10, 3);
const bytes = [
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x41, 0x00, 0x00, 0x00
];
const reader = new XdrReader(bytes);
const result = arrayOfStrings.read(reader, 2);
expect(result.length).to.eql(1);
expect(result[0].toString('utf8')).to.eql('A');
});
it('deeply nested VarArrays read at appropriate depth', function () {
const level3 = new XDR.VarArray(XDR.Int, 10, 4);
const level2 = new XDR.VarArray(level3, 10, 4);
const level1 = new XDR.VarArray(level2, 10, 4);
const bytes = [
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x01
];
const reader = new XdrReader(bytes);
const result = level1.read(reader, 2);
expect(result).to.eql([[[1]]]);
});
it('deeply nested VarArrays throw when depth exceeds limit', function () {
const level3 = new XDR.VarArray(XDR.Int, 10, 2);
const level2 = new XDR.VarArray(level3, 10, 2);
const level1 = new XDR.VarArray(level2, 10, 2);
const bytes = [
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x01
];
const reader = new XdrReader(bytes);
expect(() => level1.read(reader, 1)).to.throw(
/exceeded max decoding depth.*/i
);
});
});
describe('VarArray#write', function () {
it('encodes correctly', function () {
expect(write([])).to.eql([0x00, 0x00, 0x00, 0x00]);
expect(write([0])).to.eql([0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]);
expect(write([0, 1])).to.eql([
0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
]);
});
it('throws a write error if the value is too large', function () {
expect(() => write([1, 2, 3])).to.throw(/write error/i);
});
it('throws a write error if a child element is of the wrong type', function () {
expect(() => write([1, null])).to.throw(/write error/i);
expect(() => write([1, undefined])).to.throw(/write error/i);
expect(() => write([1, 'hi'])).to.throw(/write error/i);
});
function write(value) {
let io = new XdrWriter(256);
subject.write(value, io);
return io.toArray();
}
});
describe('VarArray#isValid', function () {
it('returns true for an array of the correct sizes with the correct types', function () {
expect(subject.isValid([])).to.be.true;
expect(subject.isValid([1])).to.be.true;
expect(subject.isValid([1, 2])).to.be.true;
});
it('returns false for arrays of the wrong size', function () {
expect(subject.isValid([1, 2, 3])).to.be.false;
});
it('returns false if a child element is invalid for the child type', function () {
expect(subject.isValid([1, null])).to.be.false;
expect(subject.isValid([1, undefined])).to.be.false;
expect(subject.isValid([1, 'hello'])).to.be.false;
expect(subject.isValid([1, []])).to.be.false;
expect(subject.isValid([1, {}])).to.be.false;
});
});
describe('VarArray#constructor', function () {
let subject = new XDR.VarArray(XDR.Int);
it('defaults to max length of a uint max value', function () {
expect(subject._maxLength).to.eql(Math.pow(2, 32) - 1);
});
});