Skip to content

Commit 513e6aa

Browse files
richardlautargos
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 <luigipinca@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent e16a04e commit 513e6aa

2 files changed

Lines changed: 32 additions & 12 deletions

File tree

‎test/common/index.js‎

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

60-
consthasOpenSSL3=hasCrypto&&
61-
require('crypto').constants.OPENSSL_VERSION_NUMBER>=0x30000000;
62-
63-
consthasOpenSSL31=hasCrypto&&
64-
require('crypto').constants.OPENSSL_VERSION_NUMBER>=0x30100000;
60+
// Synthesize OPENSSL_VERSION_NUMBER format with the layout 0xMNN00PPSL
61+
constopensslVersionNumber=(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-
consthasOpenSSL32=hasCrypto&&
67-
require('crypto').constants.OPENSSL_VERSION_NUMBER>=0x30200000;
68+
letOPENSSL_VERSION_NUMBER;
69+
consthasOpenSSL=(major=0,minor=0,patch=0)=>{
70+
if(!hasCrypto)returnfalse;
71+
if(OPENSSL_VERSION_NUMBER===undefined){
72+
constregexp=/(?<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+
returnOPENSSL_VERSION_NUMBER>=opensslVersionNumber(major,minor,patch);
77+
};
6878

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

@@ -969,9 +979,7 @@ const common = {
969979
getTTYfd,
970980
hasIntl,
971981
hasCrypto,
972-
hasOpenSSL3,
973-
hasOpenSSL31,
974-
hasOpenSSL32,
982+
hasOpenSSL,
975983
hasQuic,
976984
hasMultiLocalhost,
977985
invalidArgTypeHelper,
@@ -1032,6 +1040,18 @@ const common = {
10321040
});
10331041
},
10341042

1043+
gethasOpenSSL3(){
1044+
returnhasOpenSSL(3);
1045+
},
1046+
1047+
gethasOpenSSL31(){
1048+
returnhasOpenSSL(3,1);
1049+
},
1050+
1051+
gethasOpenSSL32(){
1052+
returnhasOpenSSL(3,2);
1053+
},
1054+
10351055
getinFreeBSDJail(){
10361056
if(inFreeBSDJail!==null)returninFreeBSDJail;
10371057

‎test/parallel/test-crypto-dh.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ const crypto = require('crypto');
8686
}
8787

8888
{
89-
constv=crypto.constants.OPENSSL_VERSION_NUMBER;
90-
consthasOpenSSL3WithNewErrorMessage=(v>=0x300000c0&&v<=0x30100000)||(v>=0x30100040&&v<=0x30200000);
89+
consthasOpenSSL3WithNewErrorMessage=(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)