Skip to content

Commit 54e0ba8

Browse files
richardlaumarco-ippolito
authored andcommitted
test: check against run-time OpenSSL version
Update `common.hasOpenSSL3*` to check against the run-time version of OpenSSL instead of the version of OpenSSL that Node.js was compiled against. Add a generalized `common.hasOpenSSL()` so we do not need to keep adding new checks for each new major/minor of OpenSSL. PR-URL: #53456 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent a0879ad commit 54e0ba8

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

test/common/index.js

+30-10
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,24 @@ const noop = () => {};
5757
const hasCrypto = Boolean(process.versions.openssl) &&
5858
!process.env.NODE_SKIP_CRYPTO;
5959

60-
const hasOpenSSL3 = hasCrypto &&
61-
require('crypto').constants.OPENSSL_VERSION_NUMBER >= 0x30000000;
62-
63-
const hasOpenSSL31 = hasCrypto &&
64-
require('crypto').constants.OPENSSL_VERSION_NUMBER >= 0x30100000;
60+
// Synthesize OPENSSL_VERSION_NUMBER format with the layout 0xMNN00PPSL
61+
const opensslVersionNumber = (major = 0, minor = 0, patch = 0) => {
62+
assert(major >= 0 && major <= 0xf);
63+
assert(minor >= 0 && minor <= 0xff);
64+
assert(patch >= 0 && patch <= 0xff);
65+
return (major << 28) | (minor << 20) | (patch << 4);
66+
};
6567

66-
const hasOpenSSL32 = hasCrypto &&
67-
require('crypto').constants.OPENSSL_VERSION_NUMBER >= 0x30200000;
68+
let OPENSSL_VERSION_NUMBER;
69+
const hasOpenSSL = (major = 0, minor = 0, patch = 0) => {
70+
if (!hasCrypto) return false;
71+
if (OPENSSL_VERSION_NUMBER === undefined) {
72+
const regexp = /(?<m>\d+)\.(?<n>\d+)\.(?<p>\d+)/;
73+
const { m, n, p } = process.versions.openssl.match(regexp).groups;
74+
OPENSSL_VERSION_NUMBER = opensslVersionNumber(m, n, p);
75+
}
76+
return OPENSSL_VERSION_NUMBER >= opensslVersionNumber(major, minor, patch);
77+
};
6878

6979
const hasQuic = hasCrypto && !!process.config.variables.openssl_quic;
7080

@@ -960,9 +970,7 @@ const common = {
960970
getTTYfd,
961971
hasIntl,
962972
hasCrypto,
963-
hasOpenSSL3,
964-
hasOpenSSL31,
965-
hasOpenSSL32,
973+
hasOpenSSL,
966974
hasQuic,
967975
hasMultiLocalhost,
968976
invalidArgTypeHelper,
@@ -1023,6 +1031,18 @@ const common = {
10231031
});
10241032
},
10251033

1034+
get hasOpenSSL3() {
1035+
return hasOpenSSL(3);
1036+
},
1037+
1038+
get hasOpenSSL31() {
1039+
return hasOpenSSL(3, 1);
1040+
},
1041+
1042+
get hasOpenSSL32() {
1043+
return hasOpenSSL(3, 2);
1044+
},
1045+
10261046
get inFreeBSDJail() {
10271047
if (inFreeBSDJail !== null) return inFreeBSDJail;
10281048

test/parallel/test-crypto-dh.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ const crypto = require('crypto');
8686
}
8787

8888
{
89-
const v = crypto.constants.OPENSSL_VERSION_NUMBER;
90-
const hasOpenSSL3WithNewErrorMessage = (v >= 0x300000c0 && v <= 0x30100000) || (v >= 0x30100040 && v <= 0x30200000);
89+
const hasOpenSSL3WithNewErrorMessage = (common.hasOpenSSL(3, 0, 12) && !common.hasOpenSSL(3, 1, 1)) ||
90+
(common.hasOpenSSL(3, 1, 4) && !common.hasOpenSSL(3, 2, 1));
9191
assert.throws(() => {
9292
dh3.computeSecret('');
9393
}, { message: common.hasOpenSSL3 && !hasOpenSSL3WithNewErrorMessage ?

0 commit comments

Comments
 (0)