-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathcompile.test.js
More file actions
444 lines (367 loc) · 12.9 KB
/
Copy pathcompile.test.js
File metadata and controls
444 lines (367 loc) · 12.9 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import fs from 'fs';
import test from 'node:test';
import assert from 'node:assert/strict';
import {sync as resolve} from 'resolve-protobuf-schema';
import {PbfReader, PbfWriter} from '../index.js';
import {compile, compileRaw} from '../compile.js';
test('compiles all proto files to proper js', () => {
const files = fs.readdirSync(new URL('fixtures', import.meta.url));
for (const path of files) {
if (!path.endsWith('.proto')) continue;
const proto = resolve(new URL(`fixtures/${path}`, import.meta.url));
const js = compileRaw(proto, {dev: true});
const jsPath = new URL(`fixtures/${path}`.replace('.proto', '.js'), import.meta.url);
if (process.env.UPDATE) fs.writeFileSync(jsPath, js);
const expectedJS = fs.readFileSync(jsPath, 'utf8');
assert.equal(js, expectedJS);
}
});
test('compiles vector tile proto', () => {
const proto = resolve(new URL('fixtures/vector_tile.proto', import.meta.url));
const tileBuf = fs.readFileSync(new URL('fixtures/12665.vector.pbf', import.meta.url));
const {readTile, writeTile} = compile(proto);
const tile = readTile(new PbfReader(tileBuf));
assert.equal(tile.layers.length, 11);
const pbf = new PbfWriter();
writeTile(tile, pbf);
const buf = pbf.finish();
assert.equal(buf.length, 124946);
});
test('compiles proto with embedded type reference', () => {
const proto = resolve(new URL('fixtures/embedded_type.proto', import.meta.url));
compile(proto);
});
test('compiles packed proto', () => {
const proto = resolve(new URL('fixtures/packed.proto', import.meta.url));
const {writeNotPacked, readFalsePacked} = compile(proto);
const original = {
types: [0, 1, 0, 1],
value: [300, 400, 500]
};
const pbf = new PbfWriter();
writeNotPacked(original, pbf);
const buf = pbf.finish();
const decompressed = readFalsePacked(new PbfReader(buf));
assert.equal(buf.length, 17);
assert.deepEqual(original, decompressed);
});
test('reads packed with unpacked field', () => {
const proto = resolve(new URL('fixtures/packed.proto', import.meta.url));
const {writePacked, readFalsePacked} = compile(proto);
const original = {
types: [0, 1, 0, 1],
value: [300, 400, 500]
};
const pbf = new PbfWriter();
writePacked(original, pbf);
const buf = pbf.finish();
const decompressed = readFalsePacked(new PbfReader(buf));
assert.equal(buf.length, 14);
assert.deepEqual(original, decompressed);
});
test('compiles packed proto3', () => {
const proto = resolve(new URL('fixtures/packed_proto3.proto', import.meta.url));
const {readNotPacked, writeNotPacked, writeFalsePacked} = compile(proto);
const original = {
types: [0, 1, 0, 1],
value: [300, 400, 500]
};
let pbf = new PbfWriter();
writeFalsePacked(original, pbf);
const falsePackedBuf = pbf.finish();
pbf = new PbfWriter();
writeNotPacked(original, pbf);
const notPackedBuf = pbf.finish();
const decompressed = readNotPacked(new PbfReader(falsePackedBuf));
assert.deepEqual(original, decompressed);
assert.equal(notPackedBuf.length, 14);
assert.ok(falsePackedBuf.length > notPackedBuf.length, 'Did not respect [packed=false]');
});
test('compiles packed with multi-byte tags', () => {
const proto = resolve(new URL('fixtures/packed_proto3.proto', import.meta.url));
const {readPacked, writePacked} = compile(proto);
const original = {
value: [300, 400, 500]
};
const pbf = new PbfWriter();
writePacked(original, pbf);
const buf = pbf.finish();
const decompressed = readPacked(new PbfReader(buf));
assert.equal(buf.length, 9);
assert.deepEqual(original, decompressed);
});
test('compiles packed sfixed64', () => {
const proto = resolve(new URL('fixtures/packed_proto3.proto', import.meta.url));
const {readPackedFixed, writePackedFixed} = compile(proto);
const original = {value: [1, -2, 3000000000, -3000000000]};
const pbf = new PbfWriter();
writePackedFixed(original, pbf);
const buf = pbf.finish();
// length-delimited (wire-type 2) packed form: 1 byte tag + 1 byte length + 4*8 bytes payload
assert.equal(buf.length, 34);
assert.deepEqual(readPackedFixed(new PbfReader(buf)), original);
});
test('compiles defaults', () => {
const proto = resolve(new URL('fixtures/defaults.proto', import.meta.url));
const {readEnvelope, writeEnvelope} = compile(proto);
const pbf = new PbfWriter();
writeEnvelope({}, pbf);
const buf = pbf.finish();
const data = readEnvelope(new PbfReader(buf));
assert.equal(buf.length, 0);
assert.deepEqual(data, {
type: 1,
name: 'test',
flag: true,
weight: 1.5,
id: 1
});
});
test('compiles proto3 ignoring defaults', () => {
const proto = resolve(new URL('fixtures/defaults_proto3.proto', import.meta.url));
const {readEnvelope, writeEnvelope} = compile(proto);
const pbf = new PbfWriter();
writeEnvelope({}, pbf);
const buf = pbf.finish();
const data = readEnvelope(new PbfReader(buf));
assert.equal(buf.length, 0);
assert.equal(data.type, 0);
assert.equal(data.name, '');
assert.equal(data.flag, false);
assert.equal(data.weight, 0);
assert.equal(data.id, 0);
});
test('compiles maps', () => {
const proto = resolve(new URL('fixtures/map.proto', import.meta.url));
const {readEnvelope, writeEnvelope} = compile(proto);
const original = {
kv: {
a: 'value a',
b: 'value b'
},
kn: {
a: 1,
b: 2
}
};
const pbf = new PbfWriter();
writeEnvelope(original, pbf);
const buf = pbf.finish();
const decompressed = readEnvelope(new PbfReader(buf));
assert.deepEqual(original, decompressed);
});
test('does not write undefined or null values', () => {
const proto = resolve(new URL('fixtures/embedded_type.proto', import.meta.url));
const {writeEmbeddedType} = compile(proto);
const pbf = new PbfWriter();
writeEmbeddedType({}, pbf);
writeEmbeddedType({
'sub_field': null
}, pbf);
writeEmbeddedType({
value: null
});
});
test('handles all implicit default values', () => {
const proto = resolve(new URL('fixtures/defaults_implicit.proto', import.meta.url));
const {readEnvelope, writeEnvelope} = compile(proto);
const pbf = new PbfWriter();
writeEnvelope({}, pbf);
const buf = pbf.finish();
const data = readEnvelope(new PbfReader(buf));
assert.equal(buf.length, 0);
assert.equal(data.type, 0);
assert.equal(data.name, '');
assert.equal(data.flag, false);
assert.equal(data.weight, 0);
assert.equal(data.id, 0);
assert.deepEqual(data.tags, []);
assert.deepEqual(data.numbers, []);
assert.equal(data.bytes, undefined);
assert.equal(data.custom, undefined);
assert.deepEqual(data.types, []);
});
test('sets oneof field name', () => {
const proto = resolve(new URL('fixtures/oneof.proto', import.meta.url));
const {readEnvelope, writeEnvelope} = compile(proto);
let pbf = new PbfWriter();
writeEnvelope({}, pbf);
let data = readEnvelope(new PbfReader(pbf.finish()));
assert.equal(data.value, undefined);
assert.equal(data.id, 0);
pbf = new PbfWriter();
writeEnvelope({
float: 1.5
}, pbf);
data = readEnvelope(new PbfReader(pbf.finish()));
assert.equal(data.value, 'float');
assert.equal(data[data.value], 1.5);
pbf = new PbfWriter();
writeEnvelope({
float: 0
}, pbf);
data = readEnvelope(new PbfReader(pbf.finish()));
assert.equal(data.value, 'float');
assert.equal(data[data.value], 0);
});
test('handles jstype=JS_STRING', () => {
const proto = resolve(new URL('fixtures/type_string.proto', import.meta.url));
const {readTypeString, writeTypeString, readTypeNotString} = compile(proto);
const pbf = new PbfWriter();
writeTypeString({
int: '-5',
long: '10000',
boolVal: true,
float: '12',
}, pbf);
const buf = pbf.finish();
let data = readTypeString(new PbfReader(buf));
assert.equal(data.int, '-5');
assert.equal(data.long, '10000');
assert.equal(data.boolVal, true);
assert.equal(data.float, '12');
assert.equal(data.default_implicit, '0');
assert.equal(data.default_explicit, '42');
data = readTypeNotString(new PbfReader(buf));
assert.equal(data.int, -5);
assert.equal(data.long, 10000);
assert.equal(data.boolVal, true);
assert.equal(data.float, 12);
});
test('handles negative varint', () => {
const proto = resolve(new URL('fixtures/varint.proto', import.meta.url));
const {readEnvelope, writeEnvelope} = compile(proto);
const pbf = new PbfWriter();
writeEnvelope({
int: -5,
long: -10
}, pbf);
const buf = pbf.finish();
const data = readEnvelope(new PbfReader(buf));
assert.equal(data.int, -5);
assert.equal(data.long, -10);
});
test('handles unsigned varint', () => {
const proto = resolve(new URL('fixtures/varint.proto', import.meta.url));
const {readEnvelope, writeEnvelope} = compile(proto);
const pbf = new PbfWriter();
writeEnvelope({
uint: Math.pow(2, 31),
ulong: Math.pow(2, 63)
}, pbf);
const buf = pbf.finish();
const data = readEnvelope(new PbfReader(buf));
assert.equal(data.uint, Math.pow(2, 31));
assert.equal(data.ulong, Math.pow(2, 63));
});
test('rejects proto schemas with invalid identifiers', () => {
const malicious = 'x"]; globalThis.__pbfPwned = true; ({["y';
const protoWithBadMessage = {
syntax: 3,
package: null,
imports: [],
enums: [],
messages: [{
name: malicious,
enums: [],
messages: [],
extensions: null,
fields: []
}]
};
assert.throws(() => compile(protoWithBadMessage), /Invalid protobuf identifier/);
const protoWithBadField = {
syntax: 3,
package: null,
imports: [],
enums: [],
messages: [{
name: 'Foo',
enums: [],
messages: [],
extensions: null,
fields: [{
name: malicious,
type: 'string',
tag: 1,
map: null,
oneof: null,
required: false,
repeated: false,
options: {}
}]
}]
};
assert.throws(() => compile(protoWithBadField), /Invalid protobuf identifier/);
const protoWithBadOneof = {
syntax: 3,
package: null,
imports: [],
enums: [],
messages: [{
name: 'Foo',
enums: [],
messages: [],
extensions: null,
fields: [{
name: 'a',
type: 'string',
tag: 1,
map: null,
oneof: malicious,
required: false,
repeated: false,
options: {}
}]
}]
};
assert.throws(() => compile(protoWithBadOneof), /Invalid protobuf identifier/);
const protoWithBadEnumValue = {
syntax: 3,
package: null,
imports: [],
enums: [{
name: 'E',
values: {[malicious]: {value: 0, options: {}}}
}],
messages: [],
extensions: null
};
assert.throws(() => compileRaw(protoWithBadEnumValue), /Invalid protobuf identifier/);
assert.equal(globalThis.__pbfPwned, undefined);
});
test('rejects proto schemas with invalid field tags', () => {
const makeProto = tag => ({
syntax: 3,
package: null,
imports: [],
enums: [],
messages: [{
name: 'Foo',
enums: [],
messages: [],
extensions: null,
fields: [{
name: 'a',
type: 'string',
tag,
map: null,
oneof: null,
required: false,
repeated: false,
options: {}
}]
}]
});
// code injection via a string tag interpolated into the generated source
assert.throws(() => compile(makeProto('(globalThis.__pbfTagPwned = true, 1)')), /Invalid protobuf field tag/);
assert.equal(globalThis.__pbfTagPwned, undefined);
// out-of-range and non-integer tags
assert.throws(() => compile(makeProto(0)), /Invalid protobuf field tag/);
assert.throws(() => compile(makeProto(-1)), /Invalid protobuf field tag/);
assert.throws(() => compile(makeProto(1.5)), /Invalid protobuf field tag/);
assert.throws(() => compile(makeProto(0x20000000)), /Invalid protobuf field tag/);
// valid numeric and numeric-string tags (the .proto text parser emits strings) still compile
assert.doesNotThrow(() => compile(makeProto(1)));
assert.doesNotThrow(() => compile(makeProto('2')));
});