Skip to content

Commit bc30f0d

Browse files
Copilotstreamich
andcommitted
Add automated.spec.ts and fuzzer.spec.ts tests for BSON codec
Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
1 parent a4e745b commit bc30f0d

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

src/bson/BsonEncoder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export class BsonEncoder implements BinaryJsonEncoder {
8181

8282
public writeFloat(float: number): void {
8383
const writer = this.writer;
84-
writer.ensureCapacity(4);
84+
writer.ensureCapacity(8);
8585
writer.view.setFloat64(writer.x, float, true);
8686
writer.x += 8;
8787
}

src/bson/__tests__/automated.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import {Writer} from '@jsonjoy.com/util/lib/buffers/Writer';
2+
import {JsonValue} from '../../types';
3+
import {BsonEncoder} from '../BsonEncoder';
4+
import {BsonDecoder} from '../BsonDecoder';
5+
import {documents} from '../../__tests__/json-documents';
6+
import {binaryDocuments} from '../../__tests__/binary-documents';
7+
8+
const writer = new Writer(8);
9+
const encoder = new BsonEncoder(writer);
10+
const decoder = new BsonDecoder();
11+
12+
const assertEncoder = (value: JsonValue) => {
13+
// BSON only supports objects at the root level, so wrap non-objects
14+
const bsonValue = value && typeof value === 'object' && value.constructor === Object ? value : {value};
15+
const encoded = encoder.encode(bsonValue);
16+
const decoded = decoder.decode(encoded);
17+
expect(decoded).toEqual(bsonValue);
18+
};
19+
20+
describe('Sample JSON documents', () => {
21+
for (const t of documents) {
22+
(t.only ? test.only : test)(t.name, () => {
23+
assertEncoder(t.json as any);
24+
});
25+
}
26+
});
27+
28+
describe('Sample binary documents', () => {
29+
for (const t of binaryDocuments) {
30+
(t.only ? test.only : test)(t.name, () => {
31+
assertEncoder(t.json as any);
32+
});
33+
}
34+
});

src/bson/__tests__/fuzzer.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import {RandomJson} from '@jsonjoy.com/util/lib/json-random';
2+
import {Writer} from '@jsonjoy.com/util/lib/buffers/Writer';
3+
import {JsonValue} from '../../types';
4+
import {BsonEncoder} from '../BsonEncoder';
5+
import {BsonDecoder} from '../BsonDecoder';
6+
7+
const writer = new Writer(2);
8+
const encoder = new BsonEncoder(writer);
9+
const decoder = new BsonDecoder();
10+
11+
const assertEncoder = (value: JsonValue) => {
12+
// BSON only supports objects at the root level, so wrap non-objects
13+
const bsonValue = value && typeof value === 'object' && value.constructor === Object ? value : {value};
14+
const encoded = encoder.encode(bsonValue);
15+
try {
16+
const decoded = decoder.decode(encoded);
17+
expect(decoded).toEqual(bsonValue);
18+
} catch (error) {
19+
/* tslint:disable no-console */
20+
console.log('value', value);
21+
console.log('bsonValue', bsonValue);
22+
console.log('JSON.stringify', JSON.stringify(bsonValue));
23+
console.log('encoded length', encoded.length);
24+
/* tslint:enable no-console */
25+
throw error;
26+
}
27+
};
28+
29+
test('fuzzing', () => {
30+
for (let i = 0; i < 1000; i++) {
31+
const json = RandomJson.generate();
32+
assertEncoder(json as any);
33+
}
34+
}, 50000);

0 commit comments

Comments
 (0)