Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: refactor assert.equal, update assert.throws #9914

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions test/parallel/test-buffer-arraybuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const buf = Buffer.from(ab);


assert.ok(buf instanceof Buffer);
assert.equal(buf.parent, buf.buffer);
assert.equal(buf.buffer, ab);
assert.equal(buf.length, ab.byteLength);
assert.strictEqual(buf.parent, buf.buffer);
assert.strictEqual(buf.buffer, ab);
assert.strictEqual(buf.length, ab.byteLength);


buf.fill(0xC);
Expand Down Expand Up @@ -44,7 +44,7 @@ assert.throws(function() {
}, TypeError);

// write{Double,Float}{LE,BE} with noAssert should not crash, cf. #3766
var b = Buffer.allocUnsafe(1);
const b = Buffer.allocUnsafe(1);
b.writeFloatLE(11.11, 0, true);
b.writeFloatBE(11.11, 0, true);
b.writeDoubleLE(11.11, 0, true);
Expand Down
18 changes: 9 additions & 9 deletions test/parallel/test-child-process-stdio-inherit.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
'use strict';
var common = require('../common');
var assert = require('assert');
var spawn = require('child_process').spawn;
const common = require('../common');
const assert = require('assert');
const spawn = require('child_process').spawn;

if (process.argv[2] === 'parent')
parent();
else
grandparent();

function grandparent() {
var child = spawn(process.execPath, [__filename, 'parent']);
let child = spawn(process.execPath, [__filename, 'parent']);
child.stderr.pipe(process.stderr);
var output = '';
var input = 'asdfasdf';
let output = '';
const input = 'asdfasdf';

child.stdout.on('data', function(chunk) {
output += chunk;
Expand All @@ -22,10 +22,10 @@ function grandparent() {
child.stdin.end(input);

child.on('close', function(code, signal) {
assert.equal(code, 0);
assert.equal(signal, null);
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
// cat on windows adds a \r\n at the end.
assert.equal(output.trim(), input.trim());
assert.strictEqual(output.trim(), input.trim());
});
}

Expand Down
74 changes: 37 additions & 37 deletions test/parallel/test-crypto-dh.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ var key2 = dh2.generateKeys('hex');
var secret1 = dh1.computeSecret(key2, 'hex', 'base64');
var secret2 = dh2.computeSecret(key1, 'latin1', 'buffer');

assert.equal(secret1, secret2.toString('base64'));
assert.equal(dh1.verifyError, 0);
assert.equal(dh2.verifyError, 0);
assert.strictEqual(secret1, secret2.toString('base64'));
assert.strictEqual(dh1.verifyError, 0);
assert.strictEqual(dh2.verifyError, 0);

assert.throws(function() {
crypto.createDiffieHellman([0x1, 0x2]);
});
}, /^First argument should be number, string or Buffer$/);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you also need to add the TypeError part or the test will fail:

/^TypeError: First argument should be number, string or Buffer$/


assert.throws(function() {
crypto.createDiffieHellman(function() { });
});
}, /^First argument should be number, string or Buffer$/);

assert.throws(function() {
crypto.createDiffieHellman(/abc/);
});
}, /^First argument should be number, string or Buffer$/);

assert.throws(function() {
crypto.createDiffieHellman({});
});
}, /^First argument should be number, string or Buffer$/);

// Create "another dh1" using generated keys from dh1,
// and compute secret again
Expand All @@ -50,11 +50,11 @@ assert.deepStrictEqual(dh1.getPrime(), dh3.getPrime());
assert.deepStrictEqual(dh1.getGenerator(), dh3.getGenerator());
assert.deepStrictEqual(dh1.getPublicKey(), dh3.getPublicKey());
assert.deepStrictEqual(dh1.getPrivateKey(), dh3.getPrivateKey());
assert.equal(dh3.verifyError, 0);
assert.strictEqual(dh3.verifyError, 0);

var secret3 = dh3.computeSecret(key2, 'hex', 'base64');

assert.equal(secret1, secret3);
assert.strictEqual(secret1, secret3);

// Run this one twice to make sure that the dh3 clears its error properly
{
Expand All @@ -78,9 +78,9 @@ alice.generateKeys();
bob.generateKeys();
var aSecret = alice.computeSecret(bob.getPublicKey()).toString('hex');
var bSecret = bob.computeSecret(alice.getPublicKey()).toString('hex');
assert.equal(aSecret, bSecret);
assert.equal(alice.verifyError, DH_NOT_SUITABLE_GENERATOR);
assert.equal(bob.verifyError, DH_NOT_SUITABLE_GENERATOR);
assert.strictEqual(aSecret, bSecret);
assert.strictEqual(alice.verifyError, DH_NOT_SUITABLE_GENERATOR);
assert.strictEqual(bob.verifyError, DH_NOT_SUITABLE_GENERATOR);

/* Ensure specific generator (buffer) works as expected.
* The values below (modp2/modp2buf) are for a 1024 bits long prime from
Expand All @@ -106,9 +106,9 @@ modp2.generateKeys();
exmodp2.generateKeys();
var modp2Secret = modp2.computeSecret(exmodp2.getPublicKey()).toString('hex');
var exmodp2Secret = exmodp2.computeSecret(modp2.getPublicKey()).toString('hex');
assert.equal(modp2Secret, exmodp2Secret);
assert.equal(modp2.verifyError, DH_NOT_SUITABLE_GENERATOR);
assert.equal(exmodp2.verifyError, DH_NOT_SUITABLE_GENERATOR);
assert.strictEqual(modp2Secret, exmodp2Secret);
assert.strictEqual(modp2.verifyError, DH_NOT_SUITABLE_GENERATOR);
assert.strictEqual(exmodp2.verifyError, DH_NOT_SUITABLE_GENERATOR);


// Ensure specific generator (string with encoding) works as expected.
Expand All @@ -117,8 +117,8 @@ exmodp2_2.generateKeys();
modp2Secret = modp2.computeSecret(exmodp2_2.getPublicKey()).toString('hex');
var exmodp2_2Secret = exmodp2_2.computeSecret(modp2.getPublicKey())
.toString('hex');
assert.equal(modp2Secret, exmodp2_2Secret);
assert.equal(exmodp2_2.verifyError, DH_NOT_SUITABLE_GENERATOR);
assert.strictEqual(modp2Secret, exmodp2_2Secret);
assert.strictEqual(exmodp2_2.verifyError, DH_NOT_SUITABLE_GENERATOR);


// Ensure specific generator (string without encoding) works as expected.
Expand All @@ -127,8 +127,8 @@ exmodp2_3.generateKeys();
modp2Secret = modp2.computeSecret(exmodp2_3.getPublicKey()).toString('hex');
var exmodp2_3Secret = exmodp2_3.computeSecret(modp2.getPublicKey())
.toString('hex');
assert.equal(modp2Secret, exmodp2_3Secret);
assert.equal(exmodp2_3.verifyError, DH_NOT_SUITABLE_GENERATOR);
assert.strictEqual(modp2Secret, exmodp2_3Secret);
assert.strictEqual(exmodp2_3.verifyError, DH_NOT_SUITABLE_GENERATOR);


// Ensure specific generator (numeric) works as expected.
Expand All @@ -137,16 +137,16 @@ exmodp2_4.generateKeys();
modp2Secret = modp2.computeSecret(exmodp2_4.getPublicKey()).toString('hex');
var exmodp2_4Secret = exmodp2_4.computeSecret(modp2.getPublicKey())
.toString('hex');
assert.equal(modp2Secret, exmodp2_4Secret);
assert.equal(exmodp2_4.verifyError, DH_NOT_SUITABLE_GENERATOR);
assert.strictEqual(modp2Secret, exmodp2_4Secret);
assert.strictEqual(exmodp2_4.verifyError, DH_NOT_SUITABLE_GENERATOR);


var p = 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74' +
'020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437' +
'4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' +
'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF';
var bad_dh = crypto.createDiffieHellman(p, 'hex');
assert.equal(bad_dh.verifyError, DH_NOT_SUITABLE_GENERATOR);
assert.strictEqual(bad_dh.verifyError, DH_NOT_SUITABLE_GENERATOR);


// Test ECDH
Expand All @@ -157,15 +157,15 @@ key2 = ecdh2.generateKeys('hex');
secret1 = ecdh1.computeSecret(key2, 'hex', 'base64');
secret2 = ecdh2.computeSecret(key1, 'latin1', 'buffer');

assert.equal(secret1, secret2.toString('base64'));
assert.strictEqual(secret1, secret2.toString('base64'));

// Oakley curves do not clean up ERR stack, it was causing unexpected failure
// when accessing other OpenSSL APIs afterwards.
crypto.createECDH('Oakley-EC2N-3');
crypto.createHash('sha256');

// Point formats
assert.equal(ecdh1.getPublicKey('buffer', 'uncompressed')[0], 4);
assert.strictEqual(ecdh1.getPublicKey('buffer', 'uncompressed')[0], 4);
let firstByte = ecdh1.getPublicKey('buffer', 'compressed')[0];
assert(firstByte === 2 || firstByte === 3);
firstByte = ecdh1.getPublicKey('buffer', 'hybrid')[0];
Expand All @@ -177,7 +177,7 @@ const key3 = ecdh3.generateKeys();

assert.throws(function() {
ecdh2.computeSecret(key3, 'latin1', 'buffer');
});
}, /^Failed to translate Buffer to a EC_POINT$/);

// ECDH should allow .setPrivateKey()/.setPublicKey()
const ecdh4 = crypto.createECDH('prime256v1');
Expand Down Expand Up @@ -209,9 +209,9 @@ const cafebabePubPtUnComp =
'04672a31bfc59d3f04548ec9b7daeeba2f61814e8ccc40448045007f5479f693a3' +
'2e02c7f93d13dc2732b760ca377a5897b9dd41a1c1b29dc0442fdce6d0a04d1d';
ecdh5.setPrivateKey(cafebabeKey, 'hex');
assert.equal(ecdh5.getPrivateKey('hex'), cafebabeKey);
assert.strictEqual(ecdh5.getPrivateKey('hex'), cafebabeKey);
// Show that the public point (key) is generated while setting the private key.
assert.equal(ecdh5.getPublicKey('hex'), cafebabePubPtUnComp);
assert.strictEqual(ecdh5.getPublicKey('hex'), cafebabePubPtUnComp);

// Compressed and uncompressed public points/keys for other party's private key
// 0xDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF
Expand All @@ -224,24 +224,24 @@ const peerPubPtUnComp =
const sharedSecret =
'1da220b5329bbe8bfd19ceef5a5898593f411a6f12ea40f2a8eead9a5cf59970';

assert.equal(ecdh5.computeSecret(peerPubPtComp, 'hex', 'hex'), sharedSecret);
assert.equal(ecdh5.computeSecret(peerPubPtUnComp, 'hex', 'hex'), sharedSecret);
assert.strictEqual(ecdh5.computeSecret(peerPubPtComp, 'hex', 'hex'), sharedSecret);
assert.strictEqual(ecdh5.computeSecret(peerPubPtUnComp, 'hex', 'hex'), sharedSecret);

// Verify that we still have the same key pair as before the computation.
assert.equal(ecdh5.getPrivateKey('hex'), cafebabeKey);
assert.equal(ecdh5.getPublicKey('hex'), cafebabePubPtUnComp);
assert.strictEqual(ecdh5.getPrivateKey('hex'), cafebabeKey);
assert.strictEqual(ecdh5.getPublicKey('hex'), cafebabePubPtUnComp);

// Verify setting and getting compressed and non-compressed serializations.
ecdh5.setPublicKey(cafebabePubPtComp, 'hex');
assert.equal(ecdh5.getPublicKey('hex'), cafebabePubPtUnComp);
assert.equal(ecdh5.getPublicKey('hex', 'compressed'), cafebabePubPtComp);
assert.strictEqual(ecdh5.getPublicKey('hex'), cafebabePubPtUnComp);
assert.strictEqual(ecdh5.getPublicKey('hex', 'compressed'), cafebabePubPtComp);
ecdh5.setPublicKey(cafebabePubPtUnComp, 'hex');
assert.equal(ecdh5.getPublicKey('hex'), cafebabePubPtUnComp);
assert.equal(ecdh5.getPublicKey('hex', 'compressed'), cafebabePubPtComp);
assert.strictEqual(ecdh5.getPublicKey('hex'), cafebabePubPtUnComp);
assert.strictEqual(ecdh5.getPublicKey('hex', 'compressed'), cafebabePubPtComp);

// Show why allowing the public key to be set on this type does not make sense.
ecdh5.setPublicKey(peerPubPtComp, 'hex');
assert.equal(ecdh5.getPublicKey('hex'), peerPubPtUnComp);
assert.strictEqual(ecdh5.getPublicKey('hex'), peerPubPtUnComp);
assert.throws(function() {
// Error because the public key does not match the private key anymore.
ecdh5.computeSecret(peerPubPtComp, 'hex', 'hex');
Expand All @@ -260,5 +260,5 @@ ecdh5.setPrivateKey(cafebabeKey, 'hex');
ecdh5.setPrivateKey(element, 'hex');
}, /Private key is not valid for specified curve/);
// Verify object state did not change.
assert.equal(ecdh5.getPrivateKey('hex'), cafebabeKey);
assert.strictEqual(ecdh5.getPrivateKey('hex'), cafebabeKey);
});