Skip to content

Commit acaf8c4

Browse files
committed
test: update expected error messages
Updated the the crypto keygen test's expected error messages.
1 parent 19b46d4 commit acaf8c4

File tree

1 file changed

+184
-43
lines changed

1 file changed

+184
-43
lines changed

test/parallel/test-crypto-keygen.js

Lines changed: 184 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,57 +1122,189 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
11221122
}
11231123
}
11241124

1125+
function addNumericalSeparator(val) {
1126+
val = String(val);
1127+
let res = '';
1128+
let i = val.length;
1129+
const start = val[0] === '-' ? 1 : 0;
1130+
for (; i >= start + 4; i -= 3) {
1131+
res = `_${val.slice(i - 3, i)}${res}`;
1132+
}
1133+
return `${val.slice(0, i)}${res}`;
1134+
}
1135+
1136+
11251137
// Test RSA parameters.
11261138
{
1127-
// Test invalid modulus lengths.
1128-
for (const modulusLength of [undefined, null, 'a', true, {}, [], 512.1, -1]) {
1139+
// Test invalid modulus lengths. (non-number)
1140+
for (const modulusLength of [undefined, null, 'a', true, {}, []]) {
11291141
assert.throws(() => generateKeyPair('rsa', {
11301142
modulusLength
11311143
}, common.mustNotCall()), {
11321144
name: 'TypeError',
1133-
code: 'ERR_INVALID_ARG_VALUE',
1134-
message: "The property 'options.modulusLength' is invalid. " +
1145+
code: 'ERR_INVALID_ARG_TYPE',
1146+
message:
1147+
'The "options.modulusLength" property must be of type number.' +
1148+
common.invalidArgTypeHelper(modulusLength)
1149+
});
1150+
}
1151+
1152+
// Test invalid modulus lengths. (non-integer)
1153+
for (const modulusLength of [512.1, 1.3, 1.1, 5000.9, 100.5]) {
1154+
assert.throws(() => generateKeyPair('rsa', {
1155+
modulusLength
1156+
}, common.mustNotCall()), {
1157+
name: 'RangeError',
1158+
code: 'ERR_OUT_OF_RANGE',
1159+
message:
1160+
'The value of "options.modulusLength" is out of range. ' +
1161+
'It must be an integer. ' +
11351162
`Received ${inspect(modulusLength)}`
11361163
});
11371164
}
11381165

1139-
// Test invalid exponents.
1140-
for (const publicExponent of ['a', true, {}, [], 3.5, -1]) {
1166+
// Test invalid modulus lengths. (out of range)
1167+
for (const modulusLength of [-1, -9, 4294967297]) {
1168+
assert.throws(() => generateKeyPair('rsa', {
1169+
modulusLength
1170+
}, common.mustNotCall()), {
1171+
name: 'RangeError',
1172+
code: 'ERR_OUT_OF_RANGE',
1173+
message:
1174+
'The value of "options.modulusLength" is out of range. ' +
1175+
'It must be >= 0 && < 4294967296. ' +
1176+
`Received ${addNumericalSeparator(modulusLength)}`
1177+
});
1178+
}
1179+
1180+
// Test invalid exponents. (non-number)
1181+
for (const publicExponent of ['a', true, {}, []]) {
11411182
assert.throws(() => generateKeyPair('rsa', {
11421183
modulusLength: 4096,
11431184
publicExponent
11441185
}, common.mustNotCall()), {
11451186
name: 'TypeError',
1146-
code: 'ERR_INVALID_ARG_VALUE',
1147-
message: "The property 'options.publicExponent' is invalid. " +
1187+
code: 'ERR_INVALID_ARG_TYPE',
1188+
message:
1189+
'The "options.publicExponent" property must be of type number.' +
1190+
common.invalidArgTypeHelper(publicExponent)
1191+
});
1192+
}
1193+
1194+
// Test invalid exponents. (non-integer)
1195+
for (const publicExponent of [3.5, 1.1, 50.5, 510.5]) {
1196+
assert.throws(() => generateKeyPair('rsa', {
1197+
modulusLength: 4096,
1198+
publicExponent
1199+
}, common.mustNotCall()), {
1200+
name: 'RangeError',
1201+
code: 'ERR_OUT_OF_RANGE',
1202+
message:
1203+
'The value of "options.publicExponent" is out of range. ' +
1204+
'It must be an integer. ' +
11481205
`Received ${inspect(publicExponent)}`
11491206
});
11501207
}
1208+
1209+
// Test invalid exponents. (out of range)
1210+
for (const publicExponent of [-5, -3, 4294967297]) {
1211+
assert.throws(() => generateKeyPair('rsa', {
1212+
modulusLength: 4096,
1213+
publicExponent
1214+
}, common.mustNotCall()), {
1215+
name: 'RangeError',
1216+
code: 'ERR_OUT_OF_RANGE',
1217+
message:
1218+
'The value of "options.publicExponent" is out of range. ' +
1219+
'It must be >= 0 && < 4294967296. ' +
1220+
`Received ${addNumericalSeparator(publicExponent)}`
1221+
});
1222+
}
11511223
}
11521224

11531225
// Test DSA parameters.
11541226
{
1155-
// Test invalid modulus lengths.
1156-
for (const modulusLength of [undefined, null, 'a', true, {}, [], 4096.1]) {
1227+
// Test invalid modulus lengths. (non-number)
1228+
for (const modulusLength of [undefined, null, 'a', true, {}, []]) {
11571229
assert.throws(() => generateKeyPair('dsa', {
11581230
modulusLength
11591231
}, common.mustNotCall()), {
11601232
name: 'TypeError',
1161-
code: 'ERR_INVALID_ARG_VALUE',
1162-
message: "The property 'options.modulusLength' is invalid. " +
1233+
code: 'ERR_INVALID_ARG_TYPE',
1234+
message:
1235+
'The "options.modulusLength" property must be of type number.' +
1236+
common.invalidArgTypeHelper(modulusLength)
1237+
});
1238+
}
1239+
1240+
// Test invalid modulus lengths. (non-integer)
1241+
for (const modulusLength of [512.1, 1.3, 1.1, 5000.9, 100.5]) {
1242+
assert.throws(() => generateKeyPair('dsa', {
1243+
modulusLength
1244+
}, common.mustNotCall()), {
1245+
name: 'RangeError',
1246+
code: 'ERR_OUT_OF_RANGE',
1247+
message:
1248+
'The value of "options.modulusLength" is out of range. ' +
1249+
'It must be an integer. ' +
11631250
`Received ${inspect(modulusLength)}`
11641251
});
11651252
}
11661253

1167-
// Test invalid divisor lengths.
1168-
for (const divisorLength of ['a', true, {}, [], 4096.1, 2147483648, -1]) {
1254+
// Test invalid modulus lengths. (out of range)
1255+
for (const modulusLength of [-1, -9, 4294967297]) {
1256+
assert.throws(() => generateKeyPair('dsa', {
1257+
modulusLength
1258+
}, common.mustNotCall()), {
1259+
name: 'RangeError',
1260+
code: 'ERR_OUT_OF_RANGE',
1261+
message:
1262+
'The value of "options.modulusLength" is out of range. ' +
1263+
'It must be >= 0 && < 4294967296. ' +
1264+
`Received ${addNumericalSeparator(modulusLength)}`
1265+
});
1266+
}
1267+
1268+
// Test invalid divisor lengths. (non-number)
1269+
for (const divisorLength of ['a', true, {}, []]) {
11691270
assert.throws(() => generateKeyPair('dsa', {
11701271
modulusLength: 2048,
11711272
divisorLength
11721273
}, common.mustNotCall()), {
11731274
name: 'TypeError',
1174-
code: 'ERR_INVALID_ARG_VALUE',
1175-
message: "The property 'options.divisorLength' is invalid. " +
1275+
code: 'ERR_INVALID_ARG_TYPE',
1276+
message:
1277+
'The "options.divisorLength" property must be of type number.' +
1278+
common.invalidArgTypeHelper(divisorLength)
1279+
});
1280+
}
1281+
1282+
// Test invalid divisor lengths. (non-integer)
1283+
for (const divisorLength of [4096.1, 5.1, 6.9, 9.5]) {
1284+
assert.throws(() => generateKeyPair('dsa', {
1285+
modulusLength: 2048,
1286+
divisorLength
1287+
}, common.mustNotCall()), {
1288+
name: 'RangeError',
1289+
code: 'ERR_OUT_OF_RANGE',
1290+
message:
1291+
'The value of "options.divisorLength" is out of range. ' +
1292+
'It must be an integer. ' +
1293+
`Received ${inspect(divisorLength)}`
1294+
});
1295+
}
1296+
1297+
// Test invalid divisor lengths. (out of range)
1298+
for (const divisorLength of [-6, -9, 2147483648]) {
1299+
assert.throws(() => generateKeyPair('dsa', {
1300+
modulusLength: 2048,
1301+
divisorLength
1302+
}, common.mustNotCall()), {
1303+
name: 'RangeError',
1304+
code: 'ERR_OUT_OF_RANGE',
1305+
message:
1306+
'The value of "options.divisorLength" is out of range. ' +
1307+
'It must be >= 0 && <= 2147483647. ' +
11761308
`Received ${inspect(divisorLength)}`
11771309
});
11781310
}
@@ -1202,9 +1334,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
12021334
});
12031335
}, {
12041336
name: 'TypeError',
1205-
code: 'ERR_INVALID_ARG_VALUE',
1206-
message: "The property 'options.namedCurve' is invalid. " +
1207-
`Received ${inspect(namedCurve)}`
1337+
code: 'ERR_INVALID_ARG_TYPE',
1338+
message:
1339+
'The "options.namedCurve" property must be of type string.' +
1340+
common.invalidArgTypeHelper(namedCurve)
12081341
});
12091342
}
12101343

@@ -1293,9 +1426,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
12931426
primeLength: 2147483648
12941427
}, common.mustNotCall());
12951428
}, {
1296-
name: 'TypeError',
1297-
code: 'ERR_INVALID_ARG_VALUE',
1298-
message: "The property 'options.primeLength' is invalid. " +
1429+
name: 'RangeError',
1430+
code: 'ERR_OUT_OF_RANGE',
1431+
message: 'The value of "options.primeLength" is out of range. ' +
1432+
'It must be >= 0 && <= 2147483647. ' +
12991433
'Received 2147483648',
13001434
});
13011435

@@ -1304,9 +1438,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
13041438
primeLength: -1
13051439
}, common.mustNotCall());
13061440
}, {
1307-
name: 'TypeError',
1308-
code: 'ERR_INVALID_ARG_VALUE',
1309-
message: "The property 'options.primeLength' is invalid. " +
1441+
name: 'RangeError',
1442+
code: 'ERR_OUT_OF_RANGE',
1443+
message: 'The value of "options.primeLength" is out of range. ' +
1444+
'It must be >= 0 && <= 2147483647. ' +
13101445
'Received -1',
13111446
});
13121447

@@ -1316,9 +1451,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
13161451
generator: 2147483648,
13171452
}, common.mustNotCall());
13181453
}, {
1319-
name: 'TypeError',
1320-
code: 'ERR_INVALID_ARG_VALUE',
1321-
message: "The property 'options.generator' is invalid. " +
1454+
name: 'RangeError',
1455+
code: 'ERR_OUT_OF_RANGE',
1456+
message: 'The value of "options.generator" is out of range. ' +
1457+
'It must be >= 0 && <= 2147483647. ' +
13221458
'Received 2147483648',
13231459
});
13241460

@@ -1328,9 +1464,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
13281464
generator: -1,
13291465
}, common.mustNotCall());
13301466
}, {
1331-
name: 'TypeError',
1332-
code: 'ERR_INVALID_ARG_VALUE',
1333-
message: "The property 'options.generator' is invalid. " +
1467+
name: 'RangeError',
1468+
code: 'ERR_OUT_OF_RANGE',
1469+
message: 'The value of "options.generator" is out of range. ' +
1470+
'It must be >= 0 && <= 2147483647. ' +
13341471
'Received -1',
13351472
});
13361473

@@ -1389,9 +1526,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
13891526
});
13901527
}, {
13911528
name: 'TypeError',
1392-
code: 'ERR_INVALID_ARG_VALUE',
1393-
message: "The property 'options.hashAlgorithm' is invalid. " +
1394-
`Received ${inspect(hashValue)}`
1529+
code: 'ERR_INVALID_ARG_TYPE',
1530+
message:
1531+
'The "options.hashAlgorithm" property must be of type string.' +
1532+
common.invalidArgTypeHelper(hashValue)
13951533
});
13961534
}
13971535

@@ -1404,9 +1542,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
14041542
mgf1HashAlgorithm: 'sha256'
14051543
}, common.mustNotCall());
14061544
}, {
1407-
name: 'TypeError',
1408-
code: 'ERR_INVALID_ARG_VALUE',
1409-
message: "The property 'options.saltLength' is invalid. " +
1545+
name: 'RangeError',
1546+
code: 'ERR_OUT_OF_RANGE',
1547+
message: 'The value of "options.saltLength" is out of range. ' +
1548+
'It must be >= 0 && <= 2147483647. ' +
14101549
'Received 2147483648'
14111550
});
14121551

@@ -1418,9 +1557,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
14181557
mgf1HashAlgorithm: 'sha256'
14191558
}, common.mustNotCall());
14201559
}, {
1421-
name: 'TypeError',
1422-
code: 'ERR_INVALID_ARG_VALUE',
1423-
message: "The property 'options.saltLength' is invalid. " +
1560+
name: 'RangeError',
1561+
code: 'ERR_OUT_OF_RANGE',
1562+
message: 'The value of "options.saltLength" is out of range. ' +
1563+
'It must be >= 0 && <= 2147483647. ' +
14241564
'Received -1'
14251565
});
14261566

@@ -1534,9 +1674,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
15341674
},
15351675
{
15361676
name: 'TypeError',
1537-
code: 'ERR_INVALID_ARG_VALUE',
1538-
message: "The property 'options.mgf1HashAlgorithm' is invalid. " +
1539-
`Received ${inspect(mgf1HashAlgorithm)}`
1677+
code: 'ERR_INVALID_ARG_TYPE',
1678+
message:
1679+
'The "options.mgf1HashAlgorithm" property must be of type string.' +
1680+
common.invalidArgTypeHelper(mgf1HashAlgorithm)
15401681

15411682
}
15421683
);

0 commit comments

Comments
 (0)