Skip to content

Commit cd1a9b3

Browse files
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.
1 parent 6bffe65 commit cd1a9b3

File tree

5 files changed

+65
-51
lines changed

5 files changed

+65
-51
lines changed

test/parallel/test-crypto-binary-default.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -621,18 +621,22 @@ common.expectsError(
621621
// Test RSA key signing/verification
622622
const rsaSign = crypto.createSign('SHA1');
623623
const rsaVerify = crypto.createVerify('SHA1');
624+
625+
// Expected signature was generated on unix, so Windows \r must be removed.
626+
const input = rsaPubPem.replace(/\r/g, '');
627+
624628
assert.ok(rsaSign instanceof crypto.Sign);
625629
assert.ok(rsaVerify instanceof crypto.Verify);
626630

627-
rsaSign.update(rsaPubPem);
631+
rsaSign.update(input);
628632
const rsaSignature = rsaSign.sign(rsaKeyPem, 'hex');
629633
const expectedSignature = fixtures.readKey(
630634
'rsa_public_sha1_signature_signedby_rsa_private.sha1',
631635
'hex'
632636
);
633637
assert.strictEqual(rsaSignature, expectedSignature);
634638

635-
rsaVerify.update(rsaPubPem);
639+
rsaVerify.update(input);
636640
assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true);
637641
}
638642

test/parallel/test-crypto-certificate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function checkMethods(certificate) {
6464
}
6565

6666
function stripLineEndings(obj) {
67-
return obj.replace(/\n/g, '');
67+
return obj.replace(/\r?\n/g, '');
6868
}
6969

7070
// Direct call Certificate() should return instance

test/parallel/test-crypto-key-objects.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,10 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
202202
assert.strictEqual(key.type, 'private');
203203
assert.strictEqual(key.asymmetricKeyType, keyType);
204204
assert.strictEqual(key.symmetricKeySize, undefined);
205-
assert.strictEqual(key.export(exportOptions), info.private);
205+
assert.strictEqual(
206+
key.export(exportOptions),
207+
info.private.replace(/\r/g, '')
208+
);
206209
}
207210

208211
{
@@ -212,7 +215,10 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
212215
assert.strictEqual(key.type, 'public');
213216
assert.strictEqual(key.asymmetricKeyType, keyType);
214217
assert.strictEqual(key.symmetricKeySize, undefined);
215-
assert.strictEqual(key.export(exportOptions), info.public);
218+
assert.strictEqual(
219+
key.export(exportOptions),
220+
info.public.replace(/\r/g, '')
221+
);
216222
});
217223
}
218224
});

test/parallel/test-crypto-rsa-dsa.js

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -185,50 +185,54 @@ test_rsa('RSA_PKCS1_PADDING');
185185
test_rsa('RSA_PKCS1_OAEP_PADDING');
186186

187187
// Test RSA key signing/verification
188-
let rsaSign = crypto.createSign('SHA1');
189-
let rsaVerify = crypto.createVerify('SHA1');
190-
assert.ok(rsaSign);
191-
assert.ok(rsaVerify);
192-
193-
const expectedSignature = fixtures.readKey(
194-
'rsa_public_sha1_signature_signedby_rsa_private_pkcs8.sha1',
195-
'hex'
196-
);
197-
198-
rsaSign.update(rsaPubPem);
199-
let rsaSignature = rsaSign.sign(rsaKeyPem, 'hex');
200-
assert.strictEqual(rsaSignature, expectedSignature);
201-
202-
rsaVerify.update(rsaPubPem);
203-
assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true);
204-
205-
// Test RSA PKCS#8 key signing/verification
206-
rsaSign = crypto.createSign('SHA1');
207-
rsaSign.update(rsaPubPem);
208-
rsaSignature = rsaSign.sign(rsaPkcs8KeyPem, 'hex');
209-
assert.strictEqual(rsaSignature, expectedSignature);
210-
211-
rsaVerify = crypto.createVerify('SHA1');
212-
rsaVerify.update(rsaPubPem);
213-
assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true);
214-
215-
// Test RSA key signing/verification with encrypted key
216-
rsaSign = crypto.createSign('SHA1');
217-
rsaSign.update(rsaPubPem);
218-
const signOptions = { key: rsaKeyPemEncrypted, passphrase: 'password' };
219-
rsaSignature = rsaSign.sign(signOptions, 'hex');
220-
assert.strictEqual(rsaSignature, expectedSignature);
221-
222-
rsaVerify = crypto.createVerify('SHA1');
223-
rsaVerify.update(rsaPubPem);
224-
assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true);
225-
226-
rsaSign = crypto.createSign('SHA1');
227-
rsaSign.update(rsaPubPem);
228-
assert.throws(() => {
229-
const signOptions = { key: rsaKeyPemEncrypted, passphrase: 'wrong' };
230-
rsaSign.sign(signOptions, 'hex');
231-
}, decryptError);
188+
{
189+
let rsaSign = crypto.createSign('SHA1');
190+
let rsaVerify = crypto.createVerify('SHA1');
191+
assert.ok(rsaSign);
192+
assert.ok(rsaVerify);
193+
194+
// Expected signature was generated on unix, so Windows \r must be removed.
195+
const input = rsaPubPem.replace(/\r/g, '');
196+
const expectedSignature = fixtures.readKey(
197+
'rsa_public_sha1_signature_signedby_rsa_private_pkcs8.sha1',
198+
'hex'
199+
);
200+
201+
rsaSign.update(input);
202+
let rsaSignature = rsaSign.sign(rsaKeyPem, 'hex');
203+
assert.strictEqual(rsaSignature, expectedSignature);
204+
205+
rsaVerify.update(input);
206+
assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true);
207+
208+
// Test RSA PKCS#8 key signing/verification
209+
rsaSign = crypto.createSign('SHA1');
210+
rsaSign.update(input);
211+
rsaSignature = rsaSign.sign(rsaPkcs8KeyPem, 'hex');
212+
assert.strictEqual(rsaSignature, expectedSignature);
213+
214+
rsaVerify = crypto.createVerify('SHA1');
215+
rsaVerify.update(input);
216+
assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true);
217+
218+
// Test RSA key signing/verification with encrypted key
219+
rsaSign = crypto.createSign('SHA1');
220+
rsaSign.update(input);
221+
const signOptions = { key: rsaKeyPemEncrypted, passphrase: 'password' };
222+
rsaSignature = rsaSign.sign(signOptions, 'hex');
223+
assert.strictEqual(rsaSignature, expectedSignature);
224+
225+
rsaVerify = crypto.createVerify('SHA1');
226+
rsaVerify.update(input);
227+
assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true);
228+
229+
rsaSign = crypto.createSign('SHA1');
230+
rsaSign.update(input);
231+
assert.throws(() => {
232+
const signOptions = { key: rsaKeyPemEncrypted, passphrase: 'wrong' };
233+
rsaSign.sign(signOptions, 'hex');
234+
}, decryptError);
235+
}
232236

233237
//
234238
// Test RSA signing and verification

test/parallel/test-https-foafssl.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ const options = {
4040
};
4141

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

4646
const CRLF = '\r\n';
4747
const body = 'hello world\n';

0 commit comments

Comments
 (0)