Skip to content

Commit

Permalink
assert,crypto: make KeyObject and CryptoKey testable for equality
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Nov 25, 2023
1 parent 2eaefe6 commit ca444db
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
18 changes: 18 additions & 0 deletions lib/internal/util/comparisons.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const {
isSymbolObject,
isFloat32Array,
isFloat64Array,
isKeyObject,
isCryptoKey,
} = types;
const {
constants: {
Expand All @@ -60,6 +62,8 @@ const kIsArray = 1;
const kIsSet = 2;
const kIsMap = 3;

let kKeyObject;

// Check if they have the same source and flags
function areSimilarRegExps(a, b) {
return a.source === b.source &&
Expand Down Expand Up @@ -249,6 +253,20 @@ function innerDeepEqual(val1, val2, strict, memos) {
isNativeError(val2) ||
val2 instanceof Error) {
return false;
} else if (isKeyObject(val1)) {
if (!isKeyObject(val2) || !val1.equals(val2)) {
return false;
}
} else if (isCryptoKey(val1)) {
kKeyObject ??= require('internal/crypto/util').kKeyObject;
if (!isCryptoKey(val2) ||
val1.extractable !== val2.extractable ||
!innerDeepEqual(val1.algorithm, val2.algorithm, strict, memos) ||
!innerDeepEqual(val1.usages, val2.usages, strict, memos) ||
!innerDeepEqual(val1[kKeyObject], val2[kKeyObject], strict, memos)
) {
return false;
}
}
return keyCheck(val1, val2, strict, memos, kNoIterator);
}
Expand Down
59 changes: 58 additions & 1 deletion test/parallel/test-assert-deep.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

require('../common');
const common = require('../common');
const assert = require('assert');
const util = require('util');
const { AssertionError } = assert;
Expand Down Expand Up @@ -1220,3 +1220,60 @@ assert.throws(

assertNotDeepOrStrict(a, b);
}

// eslint-disable-next-line node-core/crypto-check
if (common.hasCrypto) {
const crypto = require('crypto');
const { subtle } = globalThis.crypto;

{
const a = crypto.createSecretKey(Buffer.alloc(1, 0));
const b = crypto.createSecretKey(Buffer.alloc(1, 1));

assertNotDeepOrStrict(a, b);
}

{
const a = crypto.createSecretKey(Buffer.alloc(0));
const b = crypto.createSecretKey(Buffer.alloc(0));

assertDeepAndStrictEqual(a, b);
}

(async () => {
{
const a = await subtle.importKey('raw', Buffer.alloc(1, 0), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']);
const b = await subtle.importKey('raw', Buffer.alloc(1, 1), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']);

assertNotDeepOrStrict(a, b);
}

{
const a = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']);
const b = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']);

assertNotDeepOrStrict(a, b);
}

{
const a = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']);
const b = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-384' }, true, ['sign']);

assertNotDeepOrStrict(a, b);
}

{
const a = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']);
const b = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, true, ['verify']);

assertNotDeepOrStrict(a, b);
}

{
const a = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']);
const b = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']);

assertDeepAndStrictEqual(a, b);
}
})().then(common.mustCall());
}

0 comments on commit ca444db

Please sign in to comment.