Skip to content

Commit

Permalink
test: make crypto tests robust to Win CRLFs
Browse files Browse the repository at this point in the history
Loading expected results for crypto tests from files caused issues when
in Windows tests when the included carriage returns would change a
signature or mess up a comparison. This commit removes \r's where
necessary to avoid that problem.
  • Loading branch information
reasonablytall committed Jun 3, 2019
1 parent 6bffe65 commit cd1a9b3
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 51 deletions.
8 changes: 6 additions & 2 deletions test/parallel/test-crypto-binary-default.js
Original file line number Diff line number Diff line change
Expand Up @@ -621,18 +621,22 @@ common.expectsError(
// Test RSA key signing/verification
const rsaSign = crypto.createSign('SHA1');
const rsaVerify = crypto.createVerify('SHA1');

// Expected signature was generated on unix, so Windows \r must be removed.
const input = rsaPubPem.replace(/\r/g, '');

assert.ok(rsaSign instanceof crypto.Sign);
assert.ok(rsaVerify instanceof crypto.Verify);

rsaSign.update(rsaPubPem);
rsaSign.update(input);
const rsaSignature = rsaSign.sign(rsaKeyPem, 'hex');
const expectedSignature = fixtures.readKey(
'rsa_public_sha1_signature_signedby_rsa_private.sha1',
'hex'
);
assert.strictEqual(rsaSignature, expectedSignature);

rsaVerify.update(rsaPubPem);
rsaVerify.update(input);
assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true);
}

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-crypto-certificate.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function checkMethods(certificate) {
}

function stripLineEndings(obj) {
return obj.replace(/\n/g, '');
return obj.replace(/\r?\n/g, '');
}

// Direct call Certificate() should return instance
Expand Down
10 changes: 8 additions & 2 deletions test/parallel/test-crypto-key-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
assert.strictEqual(key.type, 'private');
assert.strictEqual(key.asymmetricKeyType, keyType);
assert.strictEqual(key.symmetricKeySize, undefined);
assert.strictEqual(key.export(exportOptions), info.private);
assert.strictEqual(
key.export(exportOptions),
info.private.replace(/\r/g, '')
);
}

{
Expand All @@ -212,7 +215,10 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
assert.strictEqual(key.type, 'public');
assert.strictEqual(key.asymmetricKeyType, keyType);
assert.strictEqual(key.symmetricKeySize, undefined);
assert.strictEqual(key.export(exportOptions), info.public);
assert.strictEqual(
key.export(exportOptions),
info.public.replace(/\r/g, '')
);
});
}
});
Expand Down
92 changes: 48 additions & 44 deletions test/parallel/test-crypto-rsa-dsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,50 +185,54 @@ test_rsa('RSA_PKCS1_PADDING');
test_rsa('RSA_PKCS1_OAEP_PADDING');

// Test RSA key signing/verification
let rsaSign = crypto.createSign('SHA1');
let rsaVerify = crypto.createVerify('SHA1');
assert.ok(rsaSign);
assert.ok(rsaVerify);

const expectedSignature = fixtures.readKey(
'rsa_public_sha1_signature_signedby_rsa_private_pkcs8.sha1',
'hex'
);

rsaSign.update(rsaPubPem);
let rsaSignature = rsaSign.sign(rsaKeyPem, 'hex');
assert.strictEqual(rsaSignature, expectedSignature);

rsaVerify.update(rsaPubPem);
assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true);

// Test RSA PKCS#8 key signing/verification
rsaSign = crypto.createSign('SHA1');
rsaSign.update(rsaPubPem);
rsaSignature = rsaSign.sign(rsaPkcs8KeyPem, 'hex');
assert.strictEqual(rsaSignature, expectedSignature);

rsaVerify = crypto.createVerify('SHA1');
rsaVerify.update(rsaPubPem);
assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true);

// Test RSA key signing/verification with encrypted key
rsaSign = crypto.createSign('SHA1');
rsaSign.update(rsaPubPem);
const signOptions = { key: rsaKeyPemEncrypted, passphrase: 'password' };
rsaSignature = rsaSign.sign(signOptions, 'hex');
assert.strictEqual(rsaSignature, expectedSignature);

rsaVerify = crypto.createVerify('SHA1');
rsaVerify.update(rsaPubPem);
assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true);

rsaSign = crypto.createSign('SHA1');
rsaSign.update(rsaPubPem);
assert.throws(() => {
const signOptions = { key: rsaKeyPemEncrypted, passphrase: 'wrong' };
rsaSign.sign(signOptions, 'hex');
}, decryptError);
{
let rsaSign = crypto.createSign('SHA1');
let rsaVerify = crypto.createVerify('SHA1');
assert.ok(rsaSign);
assert.ok(rsaVerify);

// Expected signature was generated on unix, so Windows \r must be removed.
const input = rsaPubPem.replace(/\r/g, '');
const expectedSignature = fixtures.readKey(
'rsa_public_sha1_signature_signedby_rsa_private_pkcs8.sha1',
'hex'
);

rsaSign.update(input);
let rsaSignature = rsaSign.sign(rsaKeyPem, 'hex');
assert.strictEqual(rsaSignature, expectedSignature);

rsaVerify.update(input);
assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true);

// Test RSA PKCS#8 key signing/verification
rsaSign = crypto.createSign('SHA1');
rsaSign.update(input);
rsaSignature = rsaSign.sign(rsaPkcs8KeyPem, 'hex');
assert.strictEqual(rsaSignature, expectedSignature);

rsaVerify = crypto.createVerify('SHA1');
rsaVerify.update(input);
assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true);

// Test RSA key signing/verification with encrypted key
rsaSign = crypto.createSign('SHA1');
rsaSign.update(input);
const signOptions = { key: rsaKeyPemEncrypted, passphrase: 'password' };
rsaSignature = rsaSign.sign(signOptions, 'hex');
assert.strictEqual(rsaSignature, expectedSignature);

rsaVerify = crypto.createVerify('SHA1');
rsaVerify.update(input);
assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true);

rsaSign = crypto.createSign('SHA1');
rsaSign.update(input);
assert.throws(() => {
const signOptions = { key: rsaKeyPemEncrypted, passphrase: 'wrong' };
rsaSign.sign(signOptions, 'hex');
}, decryptError);
}

//
// Test RSA signing and verification
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-https-foafssl.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ const options = {
};

const webIdUrl = 'URI:http://example.com/#me';
const modulus = fixtures.readKey('rsa_cert_foafssl_b.modulus', 'ascii').replace(/\n/g, '');
const exponent = fixtures.readKey('rsa_cert_foafssl_b.exponent', 'ascii').replace(/\n/g, '');
const modulus = fixtures.readKey('rsa_cert_foafssl_b.modulus', 'ascii').replace(/\r?\n/g, '');
const exponent = fixtures.readKey('rsa_cert_foafssl_b.exponent', 'ascii').replace(/\r?\n/g, '');

const CRLF = '\r\n';
const body = 'hello world\n';
Expand Down

0 comments on commit cd1a9b3

Please sign in to comment.