From cd1a9b34cbeea7cf3b568fcbb6c7cee0e2c2f4f9 Mon Sep 17 00:00:00 2001 From: Alex Aubuchon Date: Mon, 3 Jun 2019 16:11:52 -0700 Subject: [PATCH] test: make crypto tests robust to Win CRLFs 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. --- test/parallel/test-crypto-binary-default.js | 8 +- test/parallel/test-crypto-certificate.js | 2 +- test/parallel/test-crypto-key-objects.js | 10 ++- test/parallel/test-crypto-rsa-dsa.js | 92 +++++++++++---------- test/parallel/test-https-foafssl.js | 4 +- 5 files changed, 65 insertions(+), 51 deletions(-) diff --git a/test/parallel/test-crypto-binary-default.js b/test/parallel/test-crypto-binary-default.js index c96253bc44d450..386731c73b22e2 100644 --- a/test/parallel/test-crypto-binary-default.js +++ b/test/parallel/test-crypto-binary-default.js @@ -621,10 +621,14 @@ 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', @@ -632,7 +636,7 @@ common.expectsError( ); assert.strictEqual(rsaSignature, expectedSignature); - rsaVerify.update(rsaPubPem); + rsaVerify.update(input); assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true); } diff --git a/test/parallel/test-crypto-certificate.js b/test/parallel/test-crypto-certificate.js index e987be212958a7..c59900b4f26ad3 100644 --- a/test/parallel/test-crypto-certificate.js +++ b/test/parallel/test-crypto-certificate.js @@ -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 diff --git a/test/parallel/test-crypto-key-objects.js b/test/parallel/test-crypto-key-objects.js index e3ce5976267026..7fc945eff67649 100644 --- a/test/parallel/test-crypto-key-objects.js +++ b/test/parallel/test-crypto-key-objects.js @@ -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, '') + ); } { @@ -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, '') + ); }); } }); diff --git a/test/parallel/test-crypto-rsa-dsa.js b/test/parallel/test-crypto-rsa-dsa.js index 120a9f1f94f570..da035b9442f7e6 100644 --- a/test/parallel/test-crypto-rsa-dsa.js +++ b/test/parallel/test-crypto-rsa-dsa.js @@ -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 diff --git a/test/parallel/test-https-foafssl.js b/test/parallel/test-https-foafssl.js index 43057817043789..ab1136f7368c81 100644 --- a/test/parallel/test-https-foafssl.js +++ b/test/parallel/test-https-foafssl.js @@ -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';