-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Expand file tree
/
Copy pathtest-webcrypto-util.js
More file actions
70 lines (63 loc) · 2.04 KB
/
test-webcrypto-util.js
File metadata and controls
70 lines (63 loc) · 2.04 KB
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
// Flags: --expose-internals
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const {
bigIntArrayToUnsignedInt,
normalizeAlgorithm,
} = require('internal/crypto/util');
// bigIntArrayToUnsignedInt must return an unsigned 32-bit value even when
// the most significant byte has its top bit set. Otherwise the signed `<<`
// operator yields a negative Int32 for inputs like [0x80, 0x00, 0x00, 0x01].
{
assert.strictEqual(
bigIntArrayToUnsignedInt(new Uint8Array([0x80, 0x00, 0x00, 0x01])),
0x80000001);
assert.strictEqual(
bigIntArrayToUnsignedInt(new Uint8Array([0xff, 0xff, 0xff, 0xff])),
0xffffffff);
assert.strictEqual(
bigIntArrayToUnsignedInt(new Uint8Array([1, 0, 1])),
65537);
assert.strictEqual(
bigIntArrayToUnsignedInt(new Uint8Array([1, 0, 0, 0, 0])),
undefined);
}
{
// Check that normalizeAlgorithm does not mutate object inputs.
const algorithm = { name: 'ECDSA', hash: 'SHA-256' };
assert.strictEqual(normalizeAlgorithm(algorithm, 'sign') !== algorithm, true);
assert.deepStrictEqual(algorithm, { name: 'ECDSA', hash: 'SHA-256' });
}
// The algorithm name getter should only be invoked once during
// normalizeAlgorithm, including for algorithms with a non-null desiredType
// where step 6 runs the specialized dictionary converter.
// Refs: https://github.com/web-platform-tests/wpt/pull/57614#pullrequestreview-3808145365
{
let nameReadCount = 0;
const algorithm = {
get name() {
nameReadCount++;
return 'AES-GCM';
},
iv: new Uint8Array(12),
};
const normalized = normalizeAlgorithm(algorithm, 'encrypt');
assert.strictEqual(normalized.name, 'AES-GCM');
assert.strictEqual(nameReadCount, 1);
}
{
let nameReadCount = 0;
const algorithm = {
get name() {
nameReadCount++;
return 'ECDSA';
},
hash: 'SHA-256',
};
const normalized = normalizeAlgorithm(algorithm, 'sign');
assert.strictEqual(normalized.name, 'ECDSA');
assert.strictEqual(nameReadCount, 1);
}