|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const BSON = require('../register-bson'); |
| 4 | +const BSONRegExp = BSON.BSONRegExp; |
| 5 | + |
| 6 | +describe('BSON Corpus Prose Tests', function () { |
| 7 | + /** |
| 8 | + * The BSON spec uses null-terminated strings to represent document field names and |
| 9 | + * regex components (i.e. pattern and flags/options). Drivers MUST assert that null |
| 10 | + * bytes are prohibited in the following contexts when encoding BSON (i.e. creating |
| 11 | + * raw BSON bytes or constructing BSON-specific type classes): |
| 12 | + * - Field name within a root document |
| 13 | + * - Field name within a sub-document |
| 14 | + * - Pattern for a regular expression |
| 15 | + * - Flags/options for a regular expression |
| 16 | + * Depending on how drivers implement BSON encoding, they MAY expect an error when |
| 17 | + * constructing a type class (e.g. BSON Document or Regex class) or when encoding a |
| 18 | + * language representation to BSON (e.g. converting a dictionary, which might allow |
| 19 | + * null bytes in its keys, to raw BSON bytes). |
| 20 | + */ |
| 21 | + describe('1. Prohibit null bytes in null-terminated strings when encoding BSON', () => { |
| 22 | + it('Field name within a root document', () => { |
| 23 | + expect(() => BSON.serialize({ 'a\x00b': 1 })).to.throw(/null bytes/); |
| 24 | + }); |
| 25 | + |
| 26 | + it('Field name within a sub-document', () => { |
| 27 | + expect(() => BSON.serialize({ a: { 'a\x00b': 1 } })).to.throw(/null bytes/); |
| 28 | + }); |
| 29 | + |
| 30 | + it('Pattern for a regular expression', () => { |
| 31 | + // eslint-disable-next-line no-control-regex |
| 32 | + expect(() => BSON.serialize({ a: new RegExp('a\x00b') })).to.throw(/null bytes/); |
| 33 | + expect(() => BSON.serialize({ a: new BSONRegExp('a\x00b') })).to.throw(/null bytes/); |
| 34 | + }); |
| 35 | + |
| 36 | + it('Flags/options for a regular expression', () => { |
| 37 | + expect(() => BSON.serialize({ a: new BSONRegExp('a', 'i\x00m') })).to.throw(/null bytes/); |
| 38 | + // TODO: should we test RegExp, too? |
| 39 | + }); |
| 40 | + }); |
| 41 | +}); |
0 commit comments