-
Notifications
You must be signed in to change notification settings - Fork 116
/
utils.test.js
392 lines (329 loc) · 13 KB
/
utils.test.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
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
389
390
391
392
// Testing lib
import * as chai from "chai";
// Helpers
import {
arrayBufferEquals,
abToHex,
coerceToArrayBuffer,
coerceToBase64,
coerceToBase64Url,
isBase64Url,
isPem,
jsObjectToB64,
pemToBase64,
str2ab
} from "../lib/main.js";
import * as h from "./helpers/fido2-helpers.js";
const assert = chai.assert;
describe("utils", function() {
describe("coerceToBase64Url", () => {
it("exists", () => {
assert.isFunction(coerceToBase64Url);
});
it("coerce ArrayBuffer to base64url", () => {
let ab = Uint8Array.from([
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x3F, 0xF8,
]).buffer;
const res = coerceToBase64Url(ab, "test");
assert.isString(res);
assert.strictEqual(res, "AAECAwQFBgcJCgsMDQ4_-A");
});
it("coerce Uint8Array to base64url", () => {
let buf = Uint8Array.from([
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x3F, 0xF8,
]);
const res = coerceToBase64Url(buf, "test");
assert.isString(res);
assert.strictEqual(res, "AAECAwQFBgcJCgsMDQ4_-A");
});
it("coerce Array to base64url", () => {
let arr = [
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x3F, 0xF8,
];
const res = coerceToBase64Url(arr, "test");
assert.isString(res);
assert.strictEqual(res, "AAECAwQFBgcJCgsMDQ4_-A");
});
it("coerce base64 to base64url", () => {
const b64 = "AAECAwQFBgcJCgsMDQ4/+A==";
const res = coerceToBase64Url(b64, "test");
assert.isString(res);
assert.strictEqual(res, "AAECAwQFBgcJCgsMDQ4_-A");
});
it("coerce base64url to base64url", () => {
const b64url = "AAECAwQFBgcJCgsMDQ4_-A";
const res = coerceToBase64Url(b64url, "test");
assert.isString(res);
assert.strictEqual(res, "AAECAwQFBgcJCgsMDQ4_-A");
});
it("throws on incompatible: number", () => {
assert.throws(() => {
coerceToBase64Url(42, "test.number");
}, Error, "could not coerce 'test.number' to string");
});
it("throws on incompatible: undefined", () => {
assert.throws(() => {
coerceToBase64Url(undefined, "test.number");
}, Error, "could not coerce 'test.number' to string");
});
it("throws if no name specified", () => {
assert.throws(coerceToBase64Url, Error, "name not specified in coerceToBase64");
});
});
describe("coerceToBase64", () => {
it("exists", () => {
assert.isFunction(coerceToBase64Url);
});
it("coerce ArrayBuffer to base64", () => {
let ab = Uint8Array.from([
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x3F, 0xF8,
]).buffer;
const res = coerceToBase64(ab, "test");
assert.isString(res);
assert.strictEqual(res, "AAECAwQFBgcJCgsMDQ4/+A==");
});
it("coerce Uint8Array to base64", () => {
let buf = Uint8Array.from([
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x3F, 0xF8,
]);
const res = coerceToBase64(buf, "test");
assert.isString(res);
assert.strictEqual(res, "AAECAwQFBgcJCgsMDQ4/+A==");
});
it("coerce Array to base64", () => {
let arr = [
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x3F, 0xF8,
];
const res = coerceToBase64(arr, "test");
assert.isString(res);
assert.strictEqual(res, "AAECAwQFBgcJCgsMDQ4/+A==");
});
it("coerce base64 to base64", () => {
const b64 = "AAECAwQFBgcJCgsMDQ4/+A==";
const res = coerceToBase64(b64, "test");
assert.isString(res);
assert.strictEqual(res, "AAECAwQFBgcJCgsMDQ4/+A==");
});
it.skip("coerce base64url to base64", () => {
const b64url = "AAECAwQFBgcJCgsMDQ4_-A";
const res = coerceToBase64(b64url, "test");
assert.isString(res);
assert.strictEqual(res, "AAECAwQFBgcJCgsMDQ4/+A==");
});
it("throws on incompatible: number", () => {
assert.throws(() => {
coerceToBase64(42, "test.number");
}, Error, "could not coerce 'test.number' to string");
});
it("throws on incompatible: undefined", () => {
assert.throws(() => {
coerceToBase64(undefined, "test.number");
}, Error, "could not coerce 'test.number' to string");
});
it("throws if no name specified", () => {
assert.throws(coerceToBase64, Error, "name not specified in coerceToBase64");
});
});
describe("coerceToArrayBuffer", () => {
it("exists", () => {
assert.isFunction(coerceToArrayBuffer);
});
it("coerce base64url to ArrayBuffer", () => {
const b64url = "AAECAwQFBgcJCgsMDQ4_-A";
const res = coerceToArrayBuffer(b64url, "test");
assert.instanceOf(res, ArrayBuffer);
let expectedAb = Uint8Array.from([
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x3F, 0xF8,
]).buffer;
assert.isTrue(arrayBufferEquals(res, expectedAb), "got expected ArrayBuffer value");
});
it("coerce base64 to ArrayBuffer", () => {
const b64 = "AAECAwQFBgcJCgsMDQ4/+A==";
const res = coerceToArrayBuffer(b64, "test");
assert.instanceOf(res, ArrayBuffer);
let expectedAb = Uint8Array.from([
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x3F, 0xF8,
]).buffer;
assert.isTrue(arrayBufferEquals(res, expectedAb), "got expected ArrayBuffer value");
});
it("coerce Array to ArrayBuffer", () => {
let arr = [
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x3F, 0xF8,
];
const res = coerceToArrayBuffer(arr, "test");
assert.instanceOf(res, ArrayBuffer);
let expectedAb = Uint8Array.from([
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x3F, 0xF8,
]).buffer;
assert.isTrue(arrayBufferEquals(res, expectedAb), "got expected ArrayBuffer value");
});
it("coerce Buffer to ArrayBuffer");
it("coerce Uint8Array to ArrayBuffer", () => {
let buf = Uint8Array.from([
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x3F, 0xF8,
]);
const res = coerceToArrayBuffer(buf, "test");
assert.instanceOf(res, ArrayBuffer);
let expectedAb = Uint8Array.from([
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x3F, 0xF8,
]).buffer;
assert.isTrue(arrayBufferEquals(res, expectedAb), "got expected ArrayBuffer value");
});
it("coerce ArrayBuffer to ArrayBuffer", () => {
let ab = Uint8Array.from([
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x3F, 0xF8,
]).buffer;
const res = coerceToArrayBuffer(ab, "test");
assert.instanceOf(res, ArrayBuffer);
let expectedAb = Uint8Array.from([
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x3F, 0xF8,
]).buffer;
assert.isTrue(arrayBufferEquals(res, expectedAb), "got expected ArrayBuffer value");
});
it("throws on incompatible: number", () => {
assert.throws(() => {
coerceToArrayBuffer(42, "test.number");
}, Error, "could not coerce 'test.number' to ArrayBuffer");
});
it("throws on incompatible: undefined", () => {
assert.throws(() => {
coerceToArrayBuffer(undefined, "test.number");
}, Error, "could not coerce 'test.number' to ArrayBuffer");
});
it("throws on incompatible: object", () => {
assert.throws(() => {
coerceToArrayBuffer({}, "test.number");
}, Error, "could not coerce 'test.number' to ArrayBuffer");
});
it("throws if no name specified", () => {
assert.throws(coerceToArrayBuffer, Error, "name not specified in coerceToArrayBuffer");
});
});
describe("ab2str", function() {
it("converts ArrayBuffer to string");
});
describe("str2ab", function() {
it("converts string to ArrayBuffer", function() {
const ab = str2ab("abc123");
let expectedAb = new Uint8Array([
0x61, 0x62, 0x63, 0x31, 0x32, 0x33,
]).buffer;
assert.isTrue(arrayBufferEquals(ab, expectedAb), "expected result from str2ab");
});
});
describe("jsObjectToB64", function() {
it("converts Object to base64 string", function() {
assert.strictEqual(jsObjectToB64({ test: true }), "eyJ0ZXN0Ijp0cnVlfQ==");
});
it("removes non UTF-8 characters", function() {
assert.strictEqual(jsObjectToB64({ alternativeDescriptions: { "ru-RU": "FIDO2 Key SDK - оÑ\x82 Hideez" } }), jsObjectToB64({ alternativeDescriptions: { "ru-RU": "FIDO2 Key SDK - Hideez" } }));
});
});
describe("arrayBufferEquals", function() {
it("compare ArrayBuffer with equal ArrayBuffer", function() {
const ab = new Uint8Array([1, 2, 3, 4]).buffer;
let expectedAb = new Uint8Array([1,2,3,4]);
assert.isFalse(arrayBufferEquals(ab, expectedAb), "expected result from arrayBufferEquals");
});
it("compare ArrayBuffer with non equal ArrayBuffer", function() {
const ab = new Uint8Array([1, 2, 3, 4]).buffer;
let expectedAb = new Uint8Array([1,2,3,5]);
assert.isFalse(arrayBufferEquals(ab, expectedAb), "expected result from arrayBufferEquals");
});
it("compare Uint8Array with ArrayBuffer", function() {
const ab = new Uint8Array([1, 2, 3, 4]);
const expectedAb = new Uint8Array([1, 2, 3, 4]).buffer;
assert.isFalse(arrayBufferEquals(ab, expectedAb), "expected result from arrayBufferEquals");
});
});
describe("isPem", function() {
it("detects mdsSigningCert", function() {
assert.isTrue(isPem(h.mds.mdsSigningCert), "correctly detects mdsSigningCert");
});
it("detects mdsRootCrl", function() {
assert.isTrue(isPem(h.mds.mdsRootCrl), "correctly detects mdsRootCrl");
});
it("detects assnPublicKey", function() {
assert.isTrue(isPem(h.lib.assnPublicKey), "correctly detects assnPublicKey");
});
it("returns false on undefined input", function() {
assert.isFalse(isPem(), "false on undefined string");
});
it("returns false on bad string", function() {
assert.isFalse(isPem("foobar"), "false on bad string");
});
it("returns false on empty string", function() {
assert.isFalse(isPem(""), "false on empty string");
});
});
describe("pemToBase64", function() {
it("converts mdsSigningCert", function() {
let ret = pemToBase64(h.mds.mdsSigningCert);
assert.strictEqual(ret, "MIICnTCCAkOgAwIBAgIORvCM1auU6FYVXUebJHcwCgYIKoZIzj0EAwIwUzELMAkGA1UEBhMCVVMxFjAU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AwMFqgLzAtMAoGA1UdFAQDAgEMMB8GA1UdIwQYMBaAFNKlHwun9mLIQNTYvbnXjtFUu7xGMAoGCCqGSM49BAMDA2kAMGYCMQCnXSfNppE9vpsGtY9DsPWyR3aVVSPs6i5/3A21a1+rCNoa1cJNWKZJ7IV4cdjIXVUCMQCDh8U8OekdTnuvcG3FaoMJO0y0C0FS5dbTzcuiADjyVbAQeaSsCauVySzyB3lVVgE=");
});
it("converts assnPublicKey", function() {
let ret = pemToBase64(h.lib.assnPublicKey);
assert.strictEqual(ret, "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAERez9aO2wBAWO54MuGbEqSdWahSnGMAg35BCNkaE3j8Q+O/ZhhKqTeIKm7El70EG6ejt4sg1ZaoQ5ELg8k3ywTg==");
});
it("throws on empty string", function() {
assert.throws(function() {
pemToBase64("");
});
}, Error, "expected PEM string as input");
it("throws on non-PEM string", function() {
assert.throws(function() {
pemToBase64("");
});
}, Error, "expected PEM string as input");
it("throws on undefined", function() {
assert.throws(function() {
pemToBase64();
});
}, Error, "expected PEM string as input");
});
describe("isBase64Url", function() {
it("returns true for base64url string", () => {
assert.isTrue(isBase64Url("dGVzdGluZyE"), "true on base64url string");
});
it("returns false for base64 string");
it("returns false for arbitrary string");
it("returns false for undefined");
it("returns false for non-string");
});
describe("abToHex", () => {
it("should throw on string parameter", () => {
assert.throws(() => {
abToHex("foobar");
}, TypeError);
});
it("should throw on Uint8Array parameter", () => {
assert.throws(() => {
abToHex(new Uint8Array([0, 1, 2]));
}, TypeError);
});
it("should not throw on ArrayBuffer parameter, and return correct hex string", () => {
const data = new Uint8Array([0, 1, 2, 255, 16, 15]);
const ab = data.buffer;
const res = abToHex(ab);
assert.equal(res, "000102ff100f");
});
});
});