-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathindexer_test.js
334 lines (300 loc) · 12.6 KB
/
indexer_test.js
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/**
* @fileoverview Tests for indexer.js.
*/
goog.module('protobuf.binary.IndexerTest');
goog.setTestOnly();
// Note to the reader:
// Since the index behavior changes with the checking level some of the tests
// in this file have to know which checking level is enabled to make correct
// assertions.
// Test are run in all checking levels.
const BinaryStorage = goog.require('protobuf.runtime.BinaryStorage');
const BufferDecoder = goog.require('protobuf.binary.BufferDecoder');
const WireType = goog.require('protobuf.binary.WireType');
const {CHECK_CRITICAL_STATE} = goog.require('protobuf.internal.checks');
const {Field, IndexEntry} = goog.require('protobuf.binary.field');
const {buildIndex} = goog.require('protobuf.binary.indexer');
const {createBufferDecoder} = goog.require('protobuf.binary.bufferDecoderHelper');
/**
* Returns the number of fields stored.
*
* @param {!BinaryStorage} storage
* @return {number}
*/
function getStorageSize(storage) {
let size = 0;
storage.forEach(() => void size++);
return size;
}
/**
* @type {number}
*/
const PIVOT = 1;
/**
* Asserts a single IndexEntry at a given field number.
* @param {!BinaryStorage} storage
* @param {number} fieldNumber
* @param {...!IndexEntry} expectedEntries
*/
function assertStorageEntries(storage, fieldNumber, ...expectedEntries) {
expect(getStorageSize(storage)).toBe(1);
const entryArray = storage.get(fieldNumber).getIndexArray();
expect(entryArray).not.toBeUndefined();
expect(entryArray.length).toBe(expectedEntries.length);
for (let i = 0; i < entryArray.length; i++) {
const storageEntry = entryArray[i];
const expectedEntry = expectedEntries[i];
expect(storageEntry).toBe(expectedEntry);
}
}
describe('Indexer does', () => {
it('return empty storage for empty array', () => {
const storage = buildIndex(createBufferDecoder(), PIVOT);
expect(storage).not.toBeNull();
expect(getStorageSize(storage)).toBe(0);
});
it('throw for null array', () => {
expect(
() => buildIndex(
/** @type {!BufferDecoder} */ (/** @type {*} */ (null)), PIVOT))
.toThrow();
});
it('fail for invalid wire type (6)', () => {
expect(() => buildIndex(createBufferDecoder(0x0E, 0x01), PIVOT))
.toThrowError('Unexpected wire type: 6');
});
it('fail for invalid wire type (7)', () => {
expect(() => buildIndex(createBufferDecoder(0x0F, 0x01), PIVOT))
.toThrowError('Unexpected wire type: 7');
});
it('index varint', () => {
const data = createBufferDecoder(0x08, 0x01, 0x08, 0x01);
const storage = buildIndex(data, PIVOT);
assertStorageEntries(
storage, /* fieldNumber= */ 1,
Field.encodeIndexEntry(WireType.VARINT, /* startIndex= */ 1),
Field.encodeIndexEntry(WireType.VARINT, /* startIndex= */ 3));
});
it('index varint with two bytes field number', () => {
const data = createBufferDecoder(0xF8, 0x01, 0x01);
const storage = buildIndex(data, PIVOT);
assertStorageEntries(
storage, /* fieldNumber= */ 31,
Field.encodeIndexEntry(WireType.VARINT, /* startIndex= */ 2));
});
it('fail for varints that are longer than 10 bytes', () => {
const data = createBufferDecoder(
0x08, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00);
if (CHECK_CRITICAL_STATE) {
expect(() => buildIndex(data, PIVOT))
.toThrowError('Index out of bounds: index: 12 size: 11');
} else {
// Note in unchecked mode we produce invalid output for invalid inputs.
// This test just documents our behavior in those cases.
// These values might change at any point and are not considered
// what the implementation should be doing here.
const storage = buildIndex(data, PIVOT);
assertStorageEntries(
storage, /* fieldNumber= */ 1,
Field.encodeIndexEntry(WireType.VARINT, /* startIndex= */ 1));
}
});
it('fail for varints with no data', () => {
const data = createBufferDecoder(0x08);
expect(() => buildIndex(data, PIVOT)).toThrow();
});
it('index fixed64', () => {
const data = createBufferDecoder(
/* first= */ 0x09, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
/* second= */ 0x09, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08);
const storage = buildIndex(data, PIVOT);
assertStorageEntries(
storage, /* fieldNumber= */ 1,
Field.encodeIndexEntry(WireType.FIXED64, /* startIndex= */ 1),
Field.encodeIndexEntry(WireType.FIXED64, /* startIndex= */ 10));
});
it('fail for fixed64 data missing in input', () => {
const data =
createBufferDecoder(0x09, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07);
if (CHECK_CRITICAL_STATE) {
expect(() => buildIndex(data, PIVOT))
.toThrowError('Index out of bounds: index: 9 size: 8');
} else {
// Note in unchecked mode we produce invalid output for invalid inputs.
// This test just documents our behavior in those cases.
// These values might change at any point and are not considered
// what the implementation should be doing here.
const storage = buildIndex(data, PIVOT);
assertStorageEntries(
storage, /* fieldNumber= */ 1,
Field.encodeIndexEntry(WireType.FIXED64, /* startIndex= */ 1));
}
});
it('fail for fixed64 tag that has no data after it', () => {
if (CHECK_CRITICAL_STATE) {
const data = createBufferDecoder(0x09);
expect(() => buildIndex(data, PIVOT))
.toThrowError('Index out of bounds: index: 9 size: 1');
} else {
// Note in unchecked mode we produce invalid output for invalid inputs.
// This test just documents our behavior in those cases.
// These values might change at any point and are not considered
// what the implementation should be doing here.
const data = createBufferDecoder(0x09);
const storage = buildIndex(data, PIVOT);
assertStorageEntries(
storage, /* fieldNumber= */ 1,
Field.encodeIndexEntry(WireType.FIXED64, /* startIndex= */ 1));
}
});
it('index delimited', () => {
const data = createBufferDecoder(
/* first= */ 0x0A, 0x02, 0x00, 0x01, /* second= */ 0x0A, 0x02, 0x00,
0x01);
const storage = buildIndex(data, PIVOT);
assertStorageEntries(
storage, /* fieldNumber= */ 1,
Field.encodeIndexEntry(WireType.DELIMITED, /* startIndex= */ 1),
Field.encodeIndexEntry(WireType.DELIMITED, /* startIndex= */ 5));
});
it('fail for length deliimted field data missing in input', () => {
const data = createBufferDecoder(0x0A, 0x04, 0x00, 0x01);
if (CHECK_CRITICAL_STATE) {
expect(() => buildIndex(data, PIVOT))
.toThrowError('Index out of bounds: index: 6 size: 4');
} else {
// Note in unchecked mode we produce invalid output for invalid inputs.
// This test just documents our behavior in those cases.
// These values might change at any point and are not considered
// what the implementation should be doing here.
const storage = buildIndex(data, PIVOT);
assertStorageEntries(
storage, /* fieldNumber= */ 1,
Field.encodeIndexEntry(WireType.DELIMITED, /* startIndex= */ 1));
}
});
it('fail for delimited tag that has no data after it', () => {
const data = createBufferDecoder(0x0A);
expect(() => buildIndex(data, PIVOT)).toThrow();
});
it('index fixed32', () => {
const data = createBufferDecoder(
/* first= */ 0x0D, 0x01, 0x02, 0x03, 0x04, /* second= */ 0x0D, 0x01,
0x02, 0x03, 0x04);
const storage = buildIndex(data, PIVOT);
assertStorageEntries(
storage, /* fieldNumber= */ 1,
Field.encodeIndexEntry(WireType.FIXED32, /* startIndex= */ 1),
Field.encodeIndexEntry(WireType.FIXED32, /* startIndex= */ 6));
});
it('fail for fixed32 data missing in input', () => {
const data = createBufferDecoder(0x0D, 0x01, 0x02, 0x03);
if (CHECK_CRITICAL_STATE) {
expect(() => buildIndex(data, PIVOT))
.toThrowError('Index out of bounds: index: 5 size: 4');
} else {
// Note in unchecked mode we produce invalid output for invalid inputs.
// This test just documents our behavior in those cases.
// These values might change at any point and are not considered
// what the implementation should be doing here.
const storage = buildIndex(data, PIVOT);
assertStorageEntries(
storage, /* fieldNumber= */ 1,
Field.encodeIndexEntry(WireType.FIXED32, /* startIndex= */ 1));
}
});
it('fail for fixed32 tag that has no data after it', () => {
if (CHECK_CRITICAL_STATE) {
const data = createBufferDecoder(0x0D);
expect(() => buildIndex(data, PIVOT))
.toThrowError('Index out of bounds: index: 5 size: 1');
} else {
// Note in unchecked mode we produce invalid output for invalid inputs.
// This test just documents our behavior in those cases.
// These values might change at any point and are not considered
// what the implementation should be doing here.
const data = createBufferDecoder(0x0D);
const storage = buildIndex(data, PIVOT);
assertStorageEntries(
storage, /* fieldNumber= */ 1,
Field.encodeIndexEntry(WireType.FIXED32, /* startIndex= */ 1));
}
});
it('index group', () => {
const data = createBufferDecoder(
/* first= */ 0x0B, 0x08, 0x01, 0x0C, /* second= */ 0x0B, 0x08, 0x01,
0x0C);
const storage = buildIndex(data, PIVOT);
assertStorageEntries(
storage, /* fieldNumber= */ 1,
Field.encodeIndexEntry(WireType.START_GROUP, /* startIndex= */ 1),
Field.encodeIndexEntry(WireType.START_GROUP, /* startIndex= */ 5));
});
it('index group and skips inner group', () => {
const data =
createBufferDecoder(0x0B, 0x0B, 0x08, 0x01, 0x0C, 0x08, 0x01, 0x0C);
const storage = buildIndex(data, PIVOT);
assertStorageEntries(
storage, /* fieldNumber= */ 1,
Field.encodeIndexEntry(WireType.START_GROUP, /* startIndex= */ 1));
});
it('fail on unmatched stop group', () => {
const data = createBufferDecoder(0x0C, 0x01);
expect(() => buildIndex(data, PIVOT))
.toThrowError('Unexpected wire type: 4');
});
it('fail for groups without matching stop group', () => {
const data = createBufferDecoder(0x0B, 0x08, 0x01, 0x1C);
if (CHECK_CRITICAL_STATE) {
expect(() => buildIndex(data, PIVOT))
.toThrowError('Expected stop group for fieldnumber 1 not found.');
} else {
// Note in unchecked mode we produce invalid output for invalid inputs.
// This test just documents our behavior in those cases.
// These values might change at any point and are not considered
// what the implementation should be doing here.
const storage = buildIndex(data, PIVOT);
assertStorageEntries(
storage, /* fieldNumber= */ 1,
Field.encodeIndexEntry(WireType.START_GROUP, /* startIndex= */ 1));
}
});
it('fail for groups without stop group', () => {
const data = createBufferDecoder(0x0B, 0x08, 0x01);
if (CHECK_CRITICAL_STATE) {
expect(() => buildIndex(data, PIVOT)).toThrowError('No end group found.');
} else {
// Note in unchecked mode we produce invalid output for invalid inputs.
// This test just documents our behavior in those cases.
// These values might change at any point and are not considered
// what the implementation should be doing here.
const storage = buildIndex(data, PIVOT);
assertStorageEntries(
storage, /* fieldNumber= */ 1,
Field.encodeIndexEntry(WireType.START_GROUP, /* startIndex= */ 1));
}
});
it('fail for group tag that has no data after it', () => {
const data = createBufferDecoder(0x0B);
if (CHECK_CRITICAL_STATE) {
expect(() => buildIndex(data, PIVOT)).toThrowError('No end group found.');
} else {
// Note in unchecked mode we produce invalid output for invalid inputs.
// This test just documents our behavior in those cases.
// These values might change at any point and are not considered
// what the implementation should be doing here.
const storage = buildIndex(data, PIVOT);
assertStorageEntries(
storage, /* fieldNumber= */ 1,
Field.encodeIndexEntry(WireType.START_GROUP, /* startIndex= */ 1));
}
});
it('index too large tag', () => {
const data = createBufferDecoder(0xF8, 0xFF, 0xFF, 0xFF, 0xFF);
expect(() => buildIndex(data, PIVOT)).toThrow();
});
it('fail for varint tag that has no data after it', () => {
const data = createBufferDecoder(0x08);
expect(() => buildIndex(data, PIVOT)).toThrow();
});
});