-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest.ts
388 lines (313 loc) · 13.3 KB
/
test.ts
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
import * as child_process from 'child_process';
import * as protobufjs from 'protobufjs';
import * as assert from 'assert';
import * as Long from 'long';
import * as fs from 'fs';
import { parseSchema } from './index';
import {
Enum,
EnumTest,
MapTestLongAndBool,
MapTestIntAndString,
Optional,
RepeatedPacked,
RepeatedUnpacked,
} from './test.proto';
function parseTestProto(): typeof import('./test.proto') {
return parseSchema(fs.readFileSync('./test.proto', 'utf8')).compile();
}
function prngUint32(): () => number {
let seed = 1;
return () => {
const temp = (seed * 20077 + (seed & 0xFFFF) * 1103495168 + 12345) & 0x7FFFFFFF;
seed = (temp * 20077 + (temp & 0xFFFF) * 1103495168 + 12345) & 0x7FFFFFFF;
return ((temp & 0xFFFF) | (seed << 16)) >>> 0;
};
}
function* randomMessageStream(): Iterable<Optional> {
const randomUint32 = prngUint32();
const randomFloat64 = () => randomUint32() / (-1 >>> 0);
for (let i = 0; i < 1000; i++) {
yield {
field_int32: randomUint32() | 0,
field_int64: new Long(randomUint32(), randomUint32()),
field_uint32: randomUint32(),
field_uint64: new Long(randomUint32(), randomUint32(), true),
field_sint32: randomUint32() | 0,
field_sint64: new Long(randomUint32(), randomUint32()),
field_fixed64: new Long(randomUint32(), randomUint32(), true),
field_sfixed64: new Long(randomUint32(), randomUint32()),
field_double: randomFloat64(),
field_fixed32: randomUint32(),
field_sfixed32: randomUint32() | 0,
field_float: Math.fround(randomFloat64()),
};
}
}
////////////////////////////////////////////////////////////////////////////////
it('optional', async () => {
const schema = parseTestProto();
const message: Optional = {
field_int32: -1,
field_int64: new Long(-1, -2),
field_uint32: -1 >>> 0,
field_uint64: new Long(-1 >>> 0, -2 >>> 0, true),
field_sint32: -1,
field_sint64: new Long(-1, -2),
field_bool: true,
field_fixed64: new Long(12345678, 87654321, true),
field_sfixed64: new Long(-87654321, -12345678),
field_double: 2.5,
field_string: 'testing 🙉🙈🙊',
field_bytes: new Uint8Array([1, 2, 3, 4, 5]),
field_fixed32: -1 >>> 0,
field_sfixed32: -1,
field_float: 3.25,
field_nested: { x: 1.5 },
};
const buffer = schema.encodeOptional(message);
const message2 = schema.decodeOptional(buffer);
assert.deepEqual(message2, message);
const root = new protobufjs.Root();
await root.load('./test.proto', { keepCase: true });
const Optional = root.lookupType('test.Optional');
const message3 = Optional.decode(buffer);
assert.deepEqual(message3, message);
const buffer2 = Optional.encode(message).finish();
assert.deepEqual(buffer2, buffer);
});
////////////////////////////////////////////////////////////////////////////////
it('repeated unpacked', async () => {
const schema = parseTestProto();
const message: RepeatedUnpacked = {
field_int32: [-1, -2],
field_int64: [new Long(-1, -2), new Long(-3, -4)],
field_uint32: [-1 >>> 0, -2 >>> 0],
field_uint64: [new Long(-1 >>> 0, -2 >>> 0, true), new Long(-3 >>> 0, -4 >>> 0, true)],
field_sint32: [-1, -2],
field_sint64: [new Long(-1, -2), new Long(-3, -4)],
field_bool: [true, false],
field_fixed64: [new Long(12345678, 87654321, true), new Long(8765, 1234, true)],
field_sfixed64: [new Long(-87654321, -12345678), new Long(-1234, -8765)],
field_double: [2.5, -2.5],
field_string: ['testing', '🙉🙈🙊'],
field_bytes: [new Uint8Array([1, 2, 3, 4, 5]), new Uint8Array([]), new Uint8Array([5, 4, 3])],
field_fixed32: [-1 >>> 0, -2 >>> 0],
field_sfixed32: [-1, -2],
field_float: [3.25, -3.25],
field_nested: [{ x: 1.5 }, {}, { y: 0.5 }],
};
const buffer = schema.encodeRepeatedUnpacked(message);
const message2 = schema.decodeRepeatedUnpacked(buffer);
assert.deepEqual(message2, message);
const root = new protobufjs.Root();
await root.load('./test.proto', { keepCase: true });
const RepeatedUnpacked = root.lookupType('test.RepeatedUnpacked');
const message3 = RepeatedUnpacked.decode(buffer);
assert.deepEqual(message3, message);
const buffer2 = RepeatedUnpacked.encode(message).finish();
assert.deepEqual(buffer2, buffer);
});
////////////////////////////////////////////////////////////////////////////////
it('repeated packed', async () => {
const schema = parseTestProto();
const message: RepeatedPacked = {
field_int32: [-1, -2],
field_int64: [new Long(-1, -2), new Long(-3, -4)],
field_uint32: [-1 >>> 0, -2 >>> 0],
field_uint64: [new Long(-1 >>> 0, -2 >>> 0, true), new Long(-3 >>> 0, -4 >>> 0, true)],
field_sint32: [-1, -2],
field_sint64: [new Long(-1, -2), new Long(-3, -4)],
field_bool: [true, false],
field_fixed64: [new Long(12345678, 87654321, true), new Long(8765, 1234, true)],
field_sfixed64: [new Long(-87654321, -12345678), new Long(-1234, -8765)],
field_double: [2.5, -2.5],
field_string: ['testing', '🙉🙈🙊'],
field_bytes: [new Uint8Array([1, 2, 3, 4, 5]), new Uint8Array([]), new Uint8Array([5, 4, 3])],
field_fixed32: [-1 >>> 0, -2 >>> 0],
field_sfixed32: [-1, -2],
field_float: [3.25, -3.25],
field_nested: [{ x: 1.5 }, {}, { y: 0.5 }],
};
const buffer = schema.encodeRepeatedPacked(message);
const message2 = schema.decodeRepeatedPacked(buffer);
assert.deepEqual(message2, message);
const root = new protobufjs.Root();
await root.load('./test.proto', { keepCase: true });
const RepeatedPacked = root.lookupType('test.RepeatedPacked');
const message3 = RepeatedPacked.decode(buffer);
assert.deepEqual(message3, message);
const buffer2 = RepeatedPacked.encode(message).finish();
assert.deepEqual(buffer2, buffer);
});
////////////////////////////////////////////////////////////////////////////////
it('enum test', async () => {
const schema = parseTestProto();
const message: EnumTest = {
a: Enum.B,
b: Enum.A,
c: [Enum.A, Enum.B],
};
const buffer = schema.encodeEnumTest(message);
const message2 = schema.decodeEnumTest(buffer);
assert.deepEqual(message2, message);
});
////////////////////////////////////////////////////////////////////////////////
it('map test (int and string keys)', async () => {
const schema = parseTestProto();
const message: MapTestIntAndString = {
field_int32: { [-1]: 'testing', [1]: '🙉🙈🙊' },
field_uint32: { [-1 >>> 0]: new Uint8Array([1, 2, 3]), [1]: new Uint8Array([254, 255]) },
field_sint32: { [-1]: new Long(123, 234), [1]: new Long(-1, -2) },
field_string: { 'testing': 12.34, '🙉🙈🙊': 56.78 },
field_fixed32: { [-1 >>> 0]: false, [1]: true },
field_sfixed32: { [-1]: { x: 2 }, [1]: { y: -2 } },
};
const buffer = schema.encodeMapTestIntAndString(message);
const message2 = schema.decodeMapTestIntAndString(buffer);
assert.deepEqual(message2, message);
const root = new protobufjs.Root();
await root.load('./test.proto', { keepCase: true });
const MapTestIntAndString = root.lookupType('test.MapTestIntAndString');
const message3 = MapTestIntAndString.decode(buffer);
assert.deepEqual(message3, message);
const buffer2 = MapTestIntAndString.encode(message).finish();
assert.deepEqual(buffer2, buffer);
});
////////////////////////////////////////////////////////////////////////////////
it('map test (long and bool keys)', async () => {
const schema = parseTestProto();
const message: MapTestLongAndBool = {
field_int64: { '\uFEDC\uBA98\u7654\u3210': 'testing 🙉🙈🙊' },
field_uint64: { '\uBA98\u7654\u3210\uFEDC': new Uint8Array([0, 1, 254, 255]) },
field_sint64: { '\u7654\u3210\uFEDC\uBA98': new Long(-1, -2) },
field_fixed64: { '\u3210\uFEDC\uBA98\u7654': 12.34 },
field_sfixed64: { '\uFEDC\uBA98\u7654\u3210': true },
field_bool: { false: { x: 2 }, true: { y: -2 } },
};
// Note: The output can't be compared against protobuf.js because it has a
// bug that prevents it from round-tripping 64-bit keys correctly. The keys
// are encoded in decimal but decoded in binary. Whoops! See this for more
// information: https://github.com/protobufjs/protobuf.js/issues/1203.
// Because 64-bit keys are broken in protobuf.js, this library uses a more
// efficient 16-bit encoding of 64-bit keys instead of an 8-bit encoding.
//
// It also seems to have a bug that breaks round-tripping boolean keys. I
// couldn't find an associated bug but I also couldn't get it to work.
const buffer = schema.encodeMapTestLongAndBool(message);
const message2 = schema.decodeMapTestLongAndBool(buffer);
assert.deepEqual(message2, message);
const root = new protobufjs.Root();
await root.load('./test.proto', { keepCase: true });
// Even though we can't compare against the contents, still test that the
// buffer is valid and can be decoded without throwing an error.
const MapTestLongAndBool = root.lookupType('test.MapTestLongAndBool');
MapTestLongAndBool.decode(buffer);
});
////////////////////////////////////////////////////////////////////////////////
it('fuzzing protobufjs', async () => {
const schema = parseTestProto();
for (const message of randomMessageStream()) {
const buffer = schema.encodeOptional(message);
const message2 = schema.decodeOptional(buffer);
assert.deepEqual(message2, message);
}
});
////////////////////////////////////////////////////////////////////////////////
it('fuzzing pbjs', async () => {
const root = new protobufjs.Root();
await root.load('./test.proto', { keepCase: true });
const Optional = root.lookupType('test.Optional');
for (const message of randomMessageStream()) {
const buffer = Optional.encode(message).finish();
const message2 = Optional.decode(buffer);
assert.deepEqual(message2, message);
}
});
////////////////////////////////////////////////////////////////////////////////
it('javascript (es5)', () => {
const js = fs.readFileSync('./test.proto.es5.js', 'utf8');
const js2 = parseSchema(fs.readFileSync('./test.proto', 'utf8')).toJavaScript();
assert.strictEqual(js, js2);
});
////////////////////////////////////////////////////////////////////////////////
it('javascript (es6)', () => {
const js = fs.readFileSync('./test.proto.es6.js', 'utf8');
const js2 = parseSchema(fs.readFileSync('./test.proto', 'utf8')).toJavaScript({ es6: true });
assert.strictEqual(js, js2);
});
////////////////////////////////////////////////////////////////////////////////
it('typescript', () => {
const ts = fs.readFileSync('./test.proto.ts', 'utf8');
const ts2 = parseSchema(fs.readFileSync('./test.proto', 'utf8')).toTypeScript();
assert.strictEqual(ts, ts2);
});
////////////////////////////////////////////////////////////////////////////////
it('cli: generate javascript (es5)', async () => {
try {
fs.unlinkSync('./temp.js');
} catch (e) {
}
const js = fs.readFileSync('./test.proto.es5.js', 'utf8');
const cli = child_process.spawn('node', ['./cli.js', './test.proto', '--es5', './temp.js']);
await new Promise(resolve => cli.on('close', resolve));
const js2 = fs.readFileSync('./temp.js', 'utf8');
fs.unlinkSync('./temp.js');
assert.strictEqual(js, js2);
});
////////////////////////////////////////////////////////////////////////////////
it('cli: generate javascript (es6)', async () => {
try {
fs.unlinkSync('./temp.js');
} catch (e) {
}
const js = fs.readFileSync('./test.proto.es6.js', 'utf8');
const cli = child_process.spawn('node', ['./cli.js', './test.proto', '--es6', './temp.js']);
await new Promise(resolve => cli.on('close', resolve));
const js2 = fs.readFileSync('./temp.js', 'utf8');
fs.unlinkSync('./temp.js');
assert.strictEqual(js, js2);
});
////////////////////////////////////////////////////////////////////////////////
it('cli: generate typescript', async () => {
try {
fs.unlinkSync('./temp.ts');
} catch (e) {
}
const ts = fs.readFileSync('./test.proto.ts', 'utf8');
const cli = child_process.spawn('node', ['./cli.js', './test.proto', '--ts', './temp.ts']);
await new Promise(resolve => cli.on('close', resolve));
const ts2 = fs.readFileSync('./temp.ts', 'utf8');
fs.unlinkSync('./temp.ts');
assert.strictEqual(ts, ts2);
});
////////////////////////////////////////////////////////////////////////////////
it('cli: encode', async () => {
const schema = parseTestProto();
const message = {
x: 1.5,
y: -2.5,
};
const cli = child_process.spawn('node', ['./cli.js', './test.proto', '--encode', 'Nested']);
const chunks: Buffer[] = [];
cli.stdin.write(JSON.stringify(message));
cli.stdin.end();
cli.stdout.on('data', chunk => chunks.push(chunk));
await new Promise(resolve => cli.on('close', resolve));
assert.deepStrictEqual(new Uint8Array(Buffer.concat(chunks)), schema.encodeNested(message));
});
////////////////////////////////////////////////////////////////////////////////
it('cli: decode', async () => {
const schema = parseTestProto();
const message = {
x: 1.5,
y: -2.5,
};
const cli = child_process.spawn('node', ['./cli.js', './test.proto', '--decode', 'Nested']);
const chunks: Buffer[] = [];
cli.stdin.write(schema.encodeNested(message));
cli.stdin.end();
cli.stdout.on('data', chunk => chunks.push(chunk));
await new Promise(resolve => cli.on('close', resolve));
assert.strictEqual(Buffer.concat(chunks).toString(), JSON.stringify(message, null, 2) + '\n');
});