-
Notifications
You must be signed in to change notification settings - Fork 258
/
Copy pathuuid_tests.js
206 lines (174 loc) · 7.65 KB
/
uuid_tests.js
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
'use strict';
const { Buffer } = require('buffer');
const { Binary, UUID } = require('../register-bson');
const { inspect } = require('util');
const { validate: uuidStringValidate, version: uuidStringVersion } = require('uuid');
const BSON = require('../register-bson');
const BSONTypeError = BSON.BSONTypeError;
const BSON_DATA_BINARY = BSON.BSON_DATA_BINARY;
const BSON_BINARY_SUBTYPE_UUID_NEW = BSON.BSON_BINARY_SUBTYPE_UUID_NEW;
// Test values
const UPPERCASE_DASH_SEPARATED_UUID_STRING = 'AAAAAAAA-AAAA-4AAA-AAAA-AAAAAAAAAAAA';
const UPPERCASE_VALUES_ONLY_UUID_STRING = 'AAAAAAAAAAAA4AAAAAAAAAAAAAAAAAAA';
const LOWERCASE_DASH_SEPARATED_UUID_STRING = 'aaaaaaaa-aaaa-4aaa-aaaa-aaaaaaaaaaaa';
const LOWERCASE_VALUES_ONLY_UUID_STRING = 'aaaaaaaaaaaa4aaaaaaaaaaaaaaaaaaa';
describe('UUID', () => {
/**
* @ignore
*/
it('should correctly generate a valid UUID v4 from empty constructor', () => {
const uuid = new UUID();
const uuidHexStr = uuid.toHexString();
expect(uuidStringValidate(uuidHexStr)).to.be.true;
expect(uuidStringVersion(uuidHexStr)).to.equal(Binary.SUBTYPE_UUID);
});
/**
* @ignore
*/
it('should correctly create UUIDs from UPPERCASE & lowercase 36 char dash-separated hex string', () => {
const uuid1 = new UUID(UPPERCASE_DASH_SEPARATED_UUID_STRING);
expect(uuid1.equals(UPPERCASE_DASH_SEPARATED_UUID_STRING)).to.be.true;
expect(uuid1.toString()).to.equal(LOWERCASE_DASH_SEPARATED_UUID_STRING);
const uuid2 = new UUID(LOWERCASE_DASH_SEPARATED_UUID_STRING);
expect(uuid2.equals(LOWERCASE_DASH_SEPARATED_UUID_STRING)).to.be.true;
expect(uuid2.toString()).to.equal(LOWERCASE_DASH_SEPARATED_UUID_STRING);
});
/**
* @ignore
*/
it('should correctly create UUIDs from UPPERCASE & lowercase 32 char hex string (no dash separators)', () => {
const uuid1 = new UUID(UPPERCASE_VALUES_ONLY_UUID_STRING);
expect(uuid1.equals(UPPERCASE_VALUES_ONLY_UUID_STRING)).to.be.true;
expect(uuid1.toHexString(false)).to.equal(LOWERCASE_VALUES_ONLY_UUID_STRING);
const uuid2 = new UUID(LOWERCASE_VALUES_ONLY_UUID_STRING);
expect(uuid2.equals(LOWERCASE_VALUES_ONLY_UUID_STRING)).to.be.true;
expect(uuid2.toHexString(false)).to.equal(LOWERCASE_VALUES_ONLY_UUID_STRING);
});
/**
* @ignore
*/
it('should correctly create UUID from Buffer', () => {
const uuid1 = new UUID(Buffer.from(UPPERCASE_VALUES_ONLY_UUID_STRING, 'hex'));
expect(uuid1.equals(UPPERCASE_DASH_SEPARATED_UUID_STRING)).to.be.true;
expect(uuid1.toString()).to.equal(LOWERCASE_DASH_SEPARATED_UUID_STRING);
const uuid2 = new UUID(Buffer.from(LOWERCASE_VALUES_ONLY_UUID_STRING, 'hex'));
expect(uuid2.equals(LOWERCASE_DASH_SEPARATED_UUID_STRING)).to.be.true;
expect(uuid2.toString()).to.equal(LOWERCASE_DASH_SEPARATED_UUID_STRING);
});
/**
* @ignore
*/
it('should correctly create UUID from UUID (copying existing buffer)', () => {
const org = new UUID();
const copy = new UUID(org);
expect(org.id).to.not.equal(copy.id);
expect(org.id.equals(copy.id)).to.be.true;
});
/**
* @ignore
*/
it('should throw if passed invalid 36-char uuid hex string', () => {
expect(() => new UUID(LOWERCASE_DASH_SEPARATED_UUID_STRING)).to.not.throw();
expect(() => new UUID('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa')).to.throw(BSONTypeError);
// Note: The version is missing here ^
});
/**
* @ignore
*/
it('should throw if passed unsupported argument', () => {
expect(() => new UUID(LOWERCASE_DASH_SEPARATED_UUID_STRING)).to.not.throw();
expect(() => new UUID({})).to.throw(BSONTypeError);
});
/**
* @ignore
*/
it('should correctly check if a buffer isValid', () => {
const validBuffer = Buffer.from(UPPERCASE_VALUES_ONLY_UUID_STRING, 'hex');
const invalidBuffer1 = Buffer.from('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'hex');
const invalidBuffer2 = Buffer.alloc(16);
expect(validBuffer.length).to.equal(invalidBuffer1.length);
expect(validBuffer.length).to.equal(invalidBuffer2.length);
expect(UUID.isValid(invalidBuffer1)).to.be.false;
expect(UUID.isValid(invalidBuffer2)).to.be.false;
expect(UUID.isValid(validBuffer)).to.be.true;
});
/**
* @ignore
*/
it('should correctly convert to and from a Binary instance', () => {
const uuid = new UUID(LOWERCASE_DASH_SEPARATED_UUID_STRING);
expect(UUID.isValid(uuid)).to.be.true;
const bin = uuid.toBinary();
expect(bin).to.be.instanceOf(Binary);
const uuid2 = bin.toUUID();
expect(uuid2.toHexString()).to.equal(LOWERCASE_DASH_SEPARATED_UUID_STRING);
});
/**
* @ignore
*/
it('should correctly convert to and from a Binary instance', () => {
const uuid = new UUID(LOWERCASE_DASH_SEPARATED_UUID_STRING);
expect(UUID.isValid(uuid)).to.be.true;
const bin = uuid.toBinary();
expect(bin).to.be.instanceOf(Binary);
const uuid2 = bin.toUUID();
expect(uuid.equals(uuid2)).to.be.true;
});
/**
* @ignore
*/
it('should throw when converted from an incompatible Binary instance', () => {
const validRandomBuffer = Buffer.from('Hello World!');
const binRand = new Binary(validRandomBuffer);
expect(() => binRand.toUUID()).to.throw();
const validUuidV3String = '25f0d698-15b9-3a7a-96b1-a573061e29c9';
const validUuidV3Buffer = Buffer.from(validUuidV3String.replace(/-/g, ''), 'hex');
const binV3 = new Binary(validUuidV3Buffer, Binary.SUBTYPE_UUID_OLD);
expect(() => binV3.toUUID()).to.throw();
const validUuidV4String = 'bd2d74fe-bad8-430c-aeac-b01d073a1eb6';
const validUuidV4Buffer = Buffer.from(validUuidV4String.replace(/-/g, ''), 'hex');
const binV4 = new Binary(validUuidV4Buffer, Binary.SUBTYPE_UUID);
expect(() => binV4.toUUID()).to.not.throw();
});
/**
* @ignore
*/
it('should correctly allow for node.js inspect to work with UUID', () => {
const uuid = new UUID(UPPERCASE_DASH_SEPARATED_UUID_STRING);
expect(inspect(uuid)).to.equal(`new UUID("${LOWERCASE_DASH_SEPARATED_UUID_STRING}")`);
});
describe('serialize', () => {
it('should serialize BSON.UUID() input the same as BSON.UUID().toBinary()', () => {
const exampleUUID = new BSON.UUID();
const toBinarySerialization = BSON.serialize({ uuid: exampleUUID.toBinary() });
const plainUUIDSerialization = BSON.serialize({ uuid: exampleUUID });
expect(plainUUIDSerialization).to.deep.equal(toBinarySerialization);
});
it('should have a valid UUID _bsontype with Object input without error', () => {
const output = BSON.serialize({ uuid: new BSON.UUID() });
expect(output[4]).to.equal(BSON_DATA_BINARY);
expect(output[14]).to.equal(BSON_BINARY_SUBTYPE_UUID_NEW);
});
it('should have a valid UUID _bsontype with Map input without error', () => {
const output = BSON.serialize(new Map([['uuid', new BSON.UUID()]]));
expect(output[4]).to.equal(BSON_DATA_BINARY);
expect(output[14]).to.equal(BSON_BINARY_SUBTYPE_UUID_NEW);
});
it('should have as a valid UUID _bsontype with Array input without error', () => {
const output = BSON.serialize({ a: [new BSON.UUID()] });
expect(output[11]).to.equal(BSON_DATA_BINARY);
expect(output[18]).to.equal(BSON_BINARY_SUBTYPE_UUID_NEW);
});
});
describe('deserialize', () => {
it('should return UUID object when deserializing UUID subtype', () => {
const exampleUUID = new BSON.UUID('878dac12-01cc-4830-b271-cbc8518e63ad');
const serializedUUID = BSON.serialize({ uuid: exampleUUID });
const deserializedUUID = BSON.deserialize(serializedUUID);
const expectedResult = {
uuid: new UUID('878dac1201cc4830b271cbc8518e63ad')
};
expect(deserializedUUID).to.deep.equal(expectedResult);
});
});
});