Skip to content

Commit cbda4e9

Browse files
panvarichardlau
authored andcommitted
assert,crypto: make KeyObject and CryptoKey testable for equality
PR-URL: #50897 Reviewed-By: Antoine du Hamel <[email protected]>
1 parent adb5d69 commit cbda4e9

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

lib/internal/util/comparisons.js

+18
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ const {
4444
isSymbolObject,
4545
isFloat32Array,
4646
isFloat64Array,
47+
isKeyObject,
48+
isCryptoKey,
4749
} = types;
4850
const {
4951
constants: {
@@ -61,6 +63,8 @@ const kIsArray = 1;
6163
const kIsSet = 2;
6264
const kIsMap = 3;
6365

66+
let kKeyObject;
67+
6468
// Check if they have the same source and flags
6569
function areSimilarRegExps(a, b) {
6670
return a.source === b.source &&
@@ -251,6 +255,20 @@ function innerDeepEqual(val1, val2, strict, memos) {
251255
isNativeError(val2) ||
252256
val2 instanceof Error) {
253257
return false;
258+
} else if (isKeyObject(val1)) {
259+
if (!isKeyObject(val2) || !val1.equals(val2)) {
260+
return false;
261+
}
262+
} else if (isCryptoKey(val1)) {
263+
kKeyObject ??= require('internal/crypto/util').kKeyObject;
264+
if (!isCryptoKey(val2) ||
265+
val1.extractable !== val2.extractable ||
266+
!innerDeepEqual(val1.algorithm, val2.algorithm, strict, memos) ||
267+
!innerDeepEqual(val1.usages, val2.usages, strict, memos) ||
268+
!innerDeepEqual(val1[kKeyObject], val2[kKeyObject], strict, memos)
269+
) {
270+
return false;
271+
}
254272
}
255273
return keyCheck(val1, val2, strict, memos, kNoIterator);
256274
}

test/parallel/test-assert-deep.js

+58-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
require('../common');
3+
const common = require('../common');
44
const assert = require('assert');
55
const util = require('util');
66
const { AssertionError } = assert;
@@ -1220,3 +1220,60 @@ assert.throws(
12201220

12211221
assertNotDeepOrStrict(a, b);
12221222
}
1223+
1224+
// eslint-disable-next-line node-core/crypto-check
1225+
if (common.hasCrypto) {
1226+
const crypto = require('crypto');
1227+
const { subtle } = globalThis.crypto;
1228+
1229+
{
1230+
const a = crypto.createSecretKey(Buffer.alloc(1, 0));
1231+
const b = crypto.createSecretKey(Buffer.alloc(1, 1));
1232+
1233+
assertNotDeepOrStrict(a, b);
1234+
}
1235+
1236+
{
1237+
const a = crypto.createSecretKey(Buffer.alloc(0));
1238+
const b = crypto.createSecretKey(Buffer.alloc(0));
1239+
1240+
assertDeepAndStrictEqual(a, b);
1241+
}
1242+
1243+
(async () => {
1244+
{
1245+
const a = await subtle.importKey('raw', Buffer.alloc(1, 0), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']);
1246+
const b = await subtle.importKey('raw', Buffer.alloc(1, 1), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']);
1247+
1248+
assertNotDeepOrStrict(a, b);
1249+
}
1250+
1251+
{
1252+
const a = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']);
1253+
const b = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']);
1254+
1255+
assertNotDeepOrStrict(a, b);
1256+
}
1257+
1258+
{
1259+
const a = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']);
1260+
const b = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-384' }, true, ['sign']);
1261+
1262+
assertNotDeepOrStrict(a, b);
1263+
}
1264+
1265+
{
1266+
const a = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']);
1267+
const b = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, true, ['verify']);
1268+
1269+
assertNotDeepOrStrict(a, b);
1270+
}
1271+
1272+
{
1273+
const a = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']);
1274+
const b = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']);
1275+
1276+
assertDeepAndStrictEqual(a, b);
1277+
}
1278+
})().then(common.mustCall());
1279+
}

0 commit comments

Comments
 (0)