-
Notifications
You must be signed in to change notification settings - Fork 258
/
Copy pathbinary.test.ts
144 lines (125 loc) · 6.22 KB
/
binary.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
import { expect } from 'chai';
import * as vm from 'node:vm';
import { Binary, BSON } from '../register-bson';
describe('class Binary', () => {
context('constructor()', () => {
it('creates an 256 byte Binary with subtype 0 by default', () => {
const binary = new Binary();
expect(binary).to.have.property('buffer');
expect(binary).to.have.property('position', 0);
expect(binary).to.have.property('sub_type', 0);
expect(binary).to.have.nested.property('buffer.byteLength', 256);
const emptyZeroedArray = new Uint8Array(256);
emptyZeroedArray.fill(0x00);
expect(binary.buffer).to.deep.equal(emptyZeroedArray);
});
});
context('createFromHexString()', () => {
context('when called with a hex sequence', () => {
it('returns a Binary instance with the decoded bytes', () => {
const bytes = Buffer.from('abc', 'utf8');
const binary = Binary.createFromHexString(bytes.toString('hex'));
expect(binary).to.have.deep.property('buffer', bytes);
expect(binary).to.have.property('sub_type', 0);
});
it('returns a Binary instance with the decoded bytes and subtype', () => {
const bytes = Buffer.from('abc', 'utf8');
const binary = Binary.createFromHexString(bytes.toString('hex'), 0x23);
expect(binary).to.have.deep.property('buffer', bytes);
expect(binary).to.have.property('sub_type', 0x23);
});
});
context('when called with an empty string', () => {
it('creates an empty binary', () => {
const binary = Binary.createFromHexString('');
expect(binary).to.have.deep.property('buffer', new Uint8Array(0));
expect(binary).to.have.property('sub_type', 0);
});
it('creates an empty binary with subtype', () => {
const binary = Binary.createFromHexString('', 0x42);
expect(binary).to.have.deep.property('buffer', new Uint8Array(0));
expect(binary).to.have.property('sub_type', 0x42);
});
});
});
context('createFromBase64()', () => {
context('when called with a base64 sequence', () => {
it('returns a Binary instance with the decoded bytes', () => {
const bytes = Buffer.from('abc', 'utf8');
const binary = Binary.createFromBase64(bytes.toString('base64'));
expect(binary).to.have.deep.property('buffer', bytes);
expect(binary).to.have.property('sub_type', 0);
});
it('returns a Binary instance with the decoded bytes and subtype', () => {
const bytes = Buffer.from('abc', 'utf8');
const binary = Binary.createFromBase64(bytes.toString('base64'), 0x23);
expect(binary).to.have.deep.property('buffer', bytes);
expect(binary).to.have.property('sub_type', 0x23);
});
});
context('when called with an empty string', () => {
it('creates an empty binary', () => {
const binary = Binary.createFromBase64('');
expect(binary).to.have.deep.property('buffer', new Uint8Array(0));
expect(binary).to.have.property('sub_type', 0);
});
it('creates an empty binary with subtype', () => {
const binary = Binary.createFromBase64('', 0x42);
expect(binary).to.have.deep.property('buffer', new Uint8Array(0));
expect(binary).to.have.property('sub_type', 0x42);
});
});
});
context('inspect()', () => {
it('when value is default returns "Binary.createFromBase64("", 0)"', () => {
expect(new Binary().inspect()).to.equal('Binary.createFromBase64("", 0)');
});
it('when value is empty returns "Binary.createFromBase64("", 0)"', () => {
expect(new Binary(new Uint8Array(0)).inspect()).to.equal('Binary.createFromBase64("", 0)');
});
it('when value is default with a subtype returns "Binary.createFromBase64("", 35)"', () => {
expect(new Binary(null, 0x23).inspect()).to.equal('Binary.createFromBase64("", 35)');
});
it('when value is empty with a subtype returns "Binary.createFromBase64("", 35)"', () => {
expect(new Binary(new Uint8Array(0), 0x23).inspect()).to.equal(
'Binary.createFromBase64("", 35)'
);
});
it('when value has utf8 "abcdef" encoded returns "Binary.createFromBase64("YWJjZGVm", 0)"', () => {
expect(new Binary(Buffer.from('abcdef', 'utf8')).inspect()).to.equal(
'Binary.createFromBase64("YWJjZGVm", 0)'
);
});
context('when result is executed', () => {
it('has a position of zero when constructed with default space', () => {
const bsonValue = new Binary();
const ctx = { ...BSON, module: { exports: { result: null } } };
vm.runInNewContext(`module.exports.result = ${bsonValue.inspect()}`, ctx);
expect(ctx.module.exports.result).to.have.property('position', 0);
expect(ctx.module.exports.result).to.have.property('sub_type', 0);
// While the default Binary has 256 bytes the newly constructed one will have 0
// both will have a position of zero so when serialized to BSON they are the equivalent.
expect(ctx.module.exports.result).to.have.nested.property('buffer.byteLength', 0);
expect(bsonValue).to.have.nested.property('buffer.byteLength', 256);
});
it('is deep equal with a Binary that has no data', () => {
const bsonValue = new Binary(new Uint8Array(0));
const ctx = { ...BSON, module: { exports: { result: null } } };
vm.runInNewContext(`module.exports.result = ${bsonValue.inspect()}`, ctx);
expect(ctx.module.exports.result).to.deep.equal(bsonValue);
});
it('is deep equal with a Binary that has a subtype but no data', () => {
const bsonValue = new Binary(new Uint8Array(0), 0x23);
const ctx = { ...BSON, module: { exports: { result: null } } };
vm.runInNewContext(`module.exports.result = ${bsonValue.inspect()}`, ctx);
expect(ctx.module.exports.result).to.deep.equal(bsonValue);
});
it('is deep equal with a Binary that has data', () => {
const bsonValue = new Binary(Buffer.from('abc', 'utf8'));
const ctx = { ...BSON, module: { exports: { result: null } } };
vm.runInNewContext(`module.exports.result = ${bsonValue.inspect()}`, ctx);
expect(ctx.module.exports.result).to.deep.equal(bsonValue);
});
});
});
});