Skip to content

Commit f863095

Browse files
committed
test: update expected error messages
Updated the the crypto keygen test's expected error messages.
1 parent 3679047 commit f863095

File tree

1 file changed

+183
-43
lines changed

1 file changed

+183
-43
lines changed

test/parallel/test-crypto-keygen.js

Lines changed: 183 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,57 +1032,189 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
10321032
}
10331033
}
10341034

1035+
function addNumericalSeparator(val) {
1036+
val = String(val);
1037+
let res = '';
1038+
let i = val.length;
1039+
const start = val[0] === '-' ? 1 : 0;
1040+
for (; i >= start + 4; i -= 3) {
1041+
res = `_${val.slice(i - 3, i)}${res}`;
1042+
}
1043+
return `${val.slice(0, i)}${res}`;
1044+
}
1045+
1046+
10351047
// Test RSA parameters.
10361048
{
1037-
// Test invalid modulus lengths.
1038-
for (const modulusLength of [undefined, null, 'a', true, {}, [], 512.1, -1]) {
1049+
// Test invalid modulus lengths. (non-number)
1050+
for (const modulusLength of [undefined, null, 'a', true, {}, []]) {
10391051
assert.throws(() => generateKeyPair('rsa', {
10401052
modulusLength
10411053
}, common.mustNotCall()), {
10421054
name: 'TypeError',
1043-
code: 'ERR_INVALID_ARG_VALUE',
1044-
message: "The property 'options.modulusLength' is invalid. " +
1055+
code: 'ERR_INVALID_ARG_TYPE',
1056+
message:
1057+
'The "options.modulusLength" property must be of type number.' +
1058+
common.invalidArgTypeHelper(modulusLength)
1059+
});
1060+
}
1061+
1062+
// Test invalid modulus lengths. (non-integer)
1063+
for (const modulusLength of [512.1, 1.3, 1.1, 5000.9, 100.5]) {
1064+
assert.throws(() => generateKeyPair('rsa', {
1065+
modulusLength
1066+
}, common.mustNotCall()), {
1067+
name: 'RangeError',
1068+
code: 'ERR_OUT_OF_RANGE',
1069+
message:
1070+
'The value of "options.modulusLength" is out of range. ' +
1071+
'It must be an integer. ' +
10451072
`Received ${inspect(modulusLength)}`
10461073
});
10471074
}
10481075

1049-
// Test invalid exponents.
1050-
for (const publicExponent of ['a', true, {}, [], 3.5, -1]) {
1076+
// Test invalid modulus lengths. (out of range)
1077+
for (const modulusLength of [-1, -9, 4294967297]) {
1078+
assert.throws(() => generateKeyPair('rsa', {
1079+
modulusLength
1080+
}, common.mustNotCall()), {
1081+
name: 'RangeError',
1082+
code: 'ERR_OUT_OF_RANGE',
1083+
message:
1084+
'The value of "options.modulusLength" is out of range. ' +
1085+
'It must be >= 0 && < 4294967296. ' +
1086+
`Received ${addNumericalSeparator(modulusLength)}`
1087+
});
1088+
}
1089+
1090+
// Test invalid exponents. (non-number)
1091+
for (const publicExponent of ['a', true, {}, []]) {
10511092
assert.throws(() => generateKeyPair('rsa', {
10521093
modulusLength: 4096,
10531094
publicExponent
10541095
}, common.mustNotCall()), {
10551096
name: 'TypeError',
1056-
code: 'ERR_INVALID_ARG_VALUE',
1057-
message: "The property 'options.publicExponent' is invalid. " +
1097+
code: 'ERR_INVALID_ARG_TYPE',
1098+
message:
1099+
'The "options.publicExponent" property must be of type number.' +
1100+
common.invalidArgTypeHelper(publicExponent)
1101+
});
1102+
}
1103+
1104+
// Test invalid exponents. (non-integer)
1105+
for (const publicExponent of [3.5, 1.1, 50.5, 510.5]) {
1106+
assert.throws(() => generateKeyPair('rsa', {
1107+
modulusLength: 4096,
1108+
publicExponent
1109+
}, common.mustNotCall()), {
1110+
name: 'RangeError',
1111+
code: 'ERR_OUT_OF_RANGE',
1112+
message:
1113+
'The value of "options.publicExponent" is out of range. ' +
1114+
'It must be an integer. ' +
10581115
`Received ${inspect(publicExponent)}`
10591116
});
10601117
}
1118+
1119+
// Test invalid exponents. (out of range)
1120+
for (const publicExponent of [-5, -3, 4294967297]) {
1121+
assert.throws(() => generateKeyPair('rsa', {
1122+
modulusLength: 4096,
1123+
publicExponent
1124+
}, common.mustNotCall()), {
1125+
name: 'RangeError',
1126+
code: 'ERR_OUT_OF_RANGE',
1127+
message:
1128+
'The value of "options.publicExponent" is out of range. ' +
1129+
'It must be >= 0 && < 4294967296. ' +
1130+
`Received ${addNumericalSeparator(publicExponent)}`
1131+
});
1132+
}
10611133
}
10621134

10631135
// Test DSA parameters.
10641136
{
1065-
// Test invalid modulus lengths.
1066-
for (const modulusLength of [undefined, null, 'a', true, {}, [], 4096.1]) {
1137+
// Test invalid modulus lengths. (non-number)
1138+
for (const modulusLength of [undefined, null, 'a', true, {}, []]) {
10671139
assert.throws(() => generateKeyPair('dsa', {
10681140
modulusLength
10691141
}, common.mustNotCall()), {
10701142
name: 'TypeError',
1071-
code: 'ERR_INVALID_ARG_VALUE',
1072-
message: "The property 'options.modulusLength' is invalid. " +
1143+
code: 'ERR_INVALID_ARG_TYPE',
1144+
message:
1145+
'The "options.modulusLength" property must be of type number.' +
1146+
common.invalidArgTypeHelper(modulusLength)
1147+
});
1148+
}
1149+
1150+
// Test invalid modulus lengths. (non-integer)
1151+
for (const modulusLength of [512.1, 1.3, 1.1, 5000.9, 100.5]) {
1152+
assert.throws(() => generateKeyPair('dsa', {
1153+
modulusLength
1154+
}, common.mustNotCall()), {
1155+
name: 'RangeError',
1156+
code: 'ERR_OUT_OF_RANGE',
1157+
message:
1158+
'The value of "options.modulusLength" is out of range. ' +
1159+
'It must be an integer. ' +
10731160
`Received ${inspect(modulusLength)}`
10741161
});
10751162
}
10761163

1077-
// Test invalid divisor lengths.
1078-
for (const divisorLength of ['a', true, {}, [], 4096.1, 2147483648, -1]) {
1164+
// Test invalid modulus lengths. (out of range)
1165+
for (const modulusLength of [-1, -9, 4294967297]) {
1166+
assert.throws(() => generateKeyPair('dsa', {
1167+
modulusLength
1168+
}, common.mustNotCall()), {
1169+
name: 'RangeError',
1170+
code: 'ERR_OUT_OF_RANGE',
1171+
message:
1172+
'The value of "options.modulusLength" is out of range. ' +
1173+
'It must be >= 0 && < 4294967296. ' +
1174+
`Received ${addNumericalSeparator(modulusLength)}`
1175+
});
1176+
}
1177+
1178+
// Test invalid divisor lengths. (non-number)
1179+
for (const divisorLength of ['a', true, {}, []]) {
10791180
assert.throws(() => generateKeyPair('dsa', {
10801181
modulusLength: 2048,
10811182
divisorLength
10821183
}, common.mustNotCall()), {
10831184
name: 'TypeError',
1084-
code: 'ERR_INVALID_ARG_VALUE',
1085-
message: "The property 'options.divisorLength' is invalid. " +
1185+
code: 'ERR_INVALID_ARG_TYPE',
1186+
message:
1187+
'The "options.divisorLength" property must be of type number.' +
1188+
common.invalidArgTypeHelper(divisorLength)
1189+
});
1190+
}
1191+
1192+
// Test invalid divisor lengths. (non-integer)
1193+
for (const divisorLength of [4096.1, 5.1, 6.9, 9.5]) {
1194+
assert.throws(() => generateKeyPair('dsa', {
1195+
modulusLength: 2048,
1196+
divisorLength
1197+
}, common.mustNotCall()), {
1198+
name: 'RangeError',
1199+
code: 'ERR_OUT_OF_RANGE',
1200+
message:
1201+
'The value of "options.divisorLength" is out of range. ' +
1202+
'It must be an integer. ' +
1203+
`Received ${inspect(divisorLength)}`
1204+
});
1205+
}
1206+
1207+
// Test invalid divisor lengths. (out of range)
1208+
for (const divisorLength of [-6, -9, 2147483648]) {
1209+
assert.throws(() => generateKeyPair('dsa', {
1210+
modulusLength: 2048,
1211+
divisorLength
1212+
}, common.mustNotCall()), {
1213+
name: 'RangeError',
1214+
code: 'ERR_OUT_OF_RANGE',
1215+
message:
1216+
'The value of "options.divisorLength" is out of range. ' +
1217+
'It must be >= 0 && <= 2147483647. ' +
10861218
`Received ${inspect(divisorLength)}`
10871219
});
10881220
}
@@ -1112,9 +1244,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
11121244
});
11131245
}, {
11141246
name: 'TypeError',
1115-
code: 'ERR_INVALID_ARG_VALUE',
1116-
message: "The property 'options.namedCurve' is invalid. " +
1117-
`Received ${inspect(namedCurve)}`
1247+
code: 'ERR_INVALID_ARG_TYPE',
1248+
message:
1249+
'The "options.namedCurve" property must be of type string.' +
1250+
common.invalidArgTypeHelper(namedCurve)
11181251
});
11191252
}
11201253

@@ -1203,9 +1336,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
12031336
primeLength: 2147483648
12041337
}, common.mustNotCall());
12051338
}, {
1206-
name: 'TypeError',
1207-
code: 'ERR_INVALID_ARG_VALUE',
1208-
message: "The property 'options.primeLength' is invalid. " +
1339+
name: 'RangeError',
1340+
code: 'ERR_OUT_OF_RANGE',
1341+
message: 'The value of "options.primeLength" is out of range. ' +
1342+
'It must be >= 0 && <= 2147483647. ' +
12091343
'Received 2147483648',
12101344
});
12111345

@@ -1214,9 +1348,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
12141348
primeLength: -1
12151349
}, common.mustNotCall());
12161350
}, {
1217-
name: 'TypeError',
1218-
code: 'ERR_INVALID_ARG_VALUE',
1219-
message: "The property 'options.primeLength' is invalid. " +
1351+
name: 'RangeError',
1352+
code: 'ERR_OUT_OF_RANGE',
1353+
message: 'The value of "options.primeLength" is out of range. ' +
1354+
'It must be >= 0 && <= 2147483647. ' +
12201355
'Received -1',
12211356
});
12221357

@@ -1226,9 +1361,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
12261361
generator: 2147483648,
12271362
}, common.mustNotCall());
12281363
}, {
1229-
name: 'TypeError',
1230-
code: 'ERR_INVALID_ARG_VALUE',
1231-
message: "The property 'options.generator' is invalid. " +
1364+
name: 'RangeError',
1365+
code: 'ERR_OUT_OF_RANGE',
1366+
message: 'The value of "options.generator" is out of range. ' +
1367+
'It must be >= 0 && <= 2147483647. ' +
12321368
'Received 2147483648',
12331369
});
12341370

@@ -1238,9 +1374,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
12381374
generator: -1,
12391375
}, common.mustNotCall());
12401376
}, {
1241-
name: 'TypeError',
1242-
code: 'ERR_INVALID_ARG_VALUE',
1243-
message: "The property 'options.generator' is invalid. " +
1377+
name: 'RangeError',
1378+
code: 'ERR_OUT_OF_RANGE',
1379+
message: 'The value of "options.generator" is out of range. ' +
1380+
'It must be >= 0 && <= 2147483647. ' +
12441381
'Received -1',
12451382
});
12461383

@@ -1299,9 +1436,9 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
12991436
});
13001437
}, {
13011438
name: 'TypeError',
1302-
code: 'ERR_INVALID_ARG_VALUE',
1303-
message: "The property 'options.hash' is invalid. " +
1304-
`Received ${inspect(hashValue)}`
1439+
code: 'ERR_INVALID_ARG_TYPE',
1440+
message: 'The "options.hash" property must be of type string.' +
1441+
common.invalidArgTypeHelper(hashValue)
13051442
});
13061443
}
13071444

@@ -1314,9 +1451,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
13141451
mgf1Hash: 'sha256'
13151452
}, common.mustNotCall());
13161453
}, {
1317-
name: 'TypeError',
1318-
code: 'ERR_INVALID_ARG_VALUE',
1319-
message: "The property 'options.saltLength' is invalid. " +
1454+
name: 'RangeError',
1455+
code: 'ERR_OUT_OF_RANGE',
1456+
message: 'The value of "options.saltLength" is out of range. ' +
1457+
'It must be >= 0 && <= 2147483647. ' +
13201458
'Received 2147483648'
13211459
});
13221460

@@ -1328,9 +1466,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
13281466
mgf1Hash: 'sha256'
13291467
}, common.mustNotCall());
13301468
}, {
1331-
name: 'TypeError',
1332-
code: 'ERR_INVALID_ARG_VALUE',
1333-
message: "The property 'options.saltLength' is invalid. " +
1469+
name: 'RangeError',
1470+
code: 'ERR_OUT_OF_RANGE',
1471+
message: 'The value of "options.saltLength" is out of range. ' +
1472+
'It must be >= 0 && <= 2147483647. ' +
13341473
'Received -1'
13351474
});
13361475

@@ -1445,9 +1584,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
14451584
},
14461585
{
14471586
name: 'TypeError',
1448-
code: 'ERR_INVALID_ARG_VALUE',
1449-
message: "The property 'options.mgf1Hash' is invalid. " +
1450-
`Received ${inspect(mgf1Hash)}`
1587+
code: 'ERR_INVALID_ARG_TYPE',
1588+
message:
1589+
'The "options.mgf1Hash" property must be of type string.' +
1590+
common.invalidArgTypeHelper(mgf1Hash)
14511591

14521592
}
14531593
);

0 commit comments

Comments
 (0)