From 6539d69a616f970d8e449b37dd10f8142cc80f62 Mon Sep 17 00:00:00 2001 From: Cykesiopka Date: Wed, 10 Feb 2016 21:40:00 +0100 Subject: [PATCH] Bug 1243193 - Use Assert.throws() more in PSM tests. r=keeler --- .../manager/ssl/tests/unit/test_cert_dbKey.js | 10 ++------- .../unit/test_constructX509FromBase64.js | 22 ++++++++----------- .../ssl/tests/unit/test_logoutAndTeardown.js | 14 ++++-------- .../ssl/tests/unit/test_pinning_dynamic.js | 13 +++++------ .../tests/unit/test_pinning_header_parsing.js | 7 ++---- .../ssl/tests/unit/test_pkcs11_safe_mode.js | 10 ++------- .../manager/ssl/tests/unit/test_sts_fqdn.js | 7 ++---- 7 files changed, 26 insertions(+), 57 deletions(-) diff --git a/security/manager/ssl/tests/unit/test_cert_dbKey.js b/security/manager/ssl/tests/unit/test_cert_dbKey.js index f04e56a1d0b..ca47a499797 100644 --- a/security/manager/ssl/tests/unit/test_cert_dbKey.js +++ b/security/manager/ssl/tests/unit/test_cert_dbKey.js @@ -53,14 +53,8 @@ function encodeCommonNameAsBytes(commonName) { } function testInvalidDBKey(certDB, dbKey) { - let exceptionCaught = false; - try { - let cert = certDB.findCertByDBKey(dbKey); - } catch(e) { - do_print(e); - exceptionCaught = true; - } - ok(exceptionCaught, "should have thrown and caught an exception"); + throws(() => certDB.findCertByDBKey(dbKey), /NS_ERROR_ILLEGAL_INPUT/, + `findCertByDBKey(${dbKey}) should raise NS_ERROR_ILLEGAL_INPUT`); } function testDBKeyForNonexistentCert(certDB, dbKey) { diff --git a/security/manager/ssl/tests/unit/test_constructX509FromBase64.js b/security/manager/ssl/tests/unit/test_constructX509FromBase64.js index e565c6cb99f..a7b722a4118 100644 --- a/security/manager/ssl/tests/unit/test_constructX509FromBase64.js +++ b/security/manager/ssl/tests/unit/test_constructX509FromBase64.js @@ -35,26 +35,22 @@ function testGood(data) { } function testBad(data) { - try { - let cert = certDB.constructX509FromBase64(data.input); - ok(false, `Should have gotten an exception for "${data.input}"`); - } catch (e) { - equal(e.result, data.result, - "Actual and expected exception result should match"); - } + throws(() => certDB.constructX509FromBase64(data.input), data.result, + `Should get "${data.result}" for "${data.input}"`); } function run_test() { const badCases = [ // Wrong type or too short - { input: null, result: Cr.NS_ERROR_ILLEGAL_VALUE }, - { input: "", result: Cr.NS_ERROR_ILLEGAL_VALUE }, - { input: "=", result: Cr.NS_ERROR_ILLEGAL_VALUE }, - { input: "==", result: Cr.NS_ERROR_ILLEGAL_VALUE }, + { input: null, result: /NS_ERROR_ILLEGAL_VALUE/ }, + { input: "", result: /NS_ERROR_ILLEGAL_VALUE/ }, + { input: "=", result: /NS_ERROR_ILLEGAL_VALUE/ }, + { input: "==", result: /NS_ERROR_ILLEGAL_VALUE/ }, // Not base64 - { input: "forty-four dead stone lions", result: Cr.NS_ERROR_ILLEGAL_VALUE }, + { input: "forty-four dead stone lions", result: /NS_ERROR_ILLEGAL_VALUE/ }, // Not a cert - { input: "Zm9ydHktZm91ciBkZWFkIHN0b25lIGxpb25z", result: Cr.NS_ERROR_FAILURE } + { input: "Zm9ydHktZm91ciBkZWFkIHN0b25lIGxpb25z", + result: /NS_ERROR_FAILURE/ }, ]; // Real certs with all three padding levels diff --git a/security/manager/ssl/tests/unit/test_logoutAndTeardown.js b/security/manager/ssl/tests/unit/test_logoutAndTeardown.js index 853d3e2854b..2bf8855fe12 100644 --- a/security/manager/ssl/tests/unit/test_logoutAndTeardown.js +++ b/security/manager/ssl/tests/unit/test_logoutAndTeardown.js @@ -16,16 +16,10 @@ function connect_and_teardown() { let reader = { onInputStreamReady: function(stream) { - try { - stream.available(); - Assert.ok(false, "stream.available() should have thrown"); - } - catch (e) { - Assert.equal(e.result, Components.results.NS_ERROR_FAILURE, - "stream should be in an error state"); - Assert.ok(tearDown, "this should be as a result of logoutAndTeardown"); - run_next_test(); - } + throws(() => stream.available(), /NS_ERROR_FAILURE/, + "stream should be in an error state"); + ok(tearDown, "A tear down attempt should have occurred"); + run_next_test(); } }; diff --git a/security/manager/ssl/tests/unit/test_pinning_dynamic.js b/security/manager/ssl/tests/unit/test_pinning_dynamic.js index 8adcd005755..4b8e819265b 100644 --- a/security/manager/ssl/tests/unit/test_pinning_dynamic.js +++ b/security/manager/ssl/tests/unit/test_pinning_dynamic.js @@ -149,12 +149,10 @@ function checkStateRead(aSubject, aTopic, aData) { checkDefaultSiteHPKPStatus(); // failure to insert new pin entry leaves previous pin behavior - try { + throws(() => { gSSService.setKeyPins("a.pinning2.example.com", true, 1000, 1, ["not a hash"]); - ok(false, "Attempting to set an invalid pin should have failed"); - } catch(e) { - } + }, /NS_ERROR_ILLEGAL_VALUE/, "Attempting to set an invalid pin should fail"); checkFail(certFromFile('cn-a.pinning2.example.com-badca'), "a.pinning2.example.com"); checkOK(certFromFile('cn-a.pinning2.example.com-pinningroot'), "a.pinning2.example.com"); checkOK(certFromFile('cn-x.a.pinning2.example.com-badca'), "x.a.pinning2.example.com"); @@ -170,12 +168,11 @@ function checkStateRead(aSubject, aTopic, aData) { checkDefaultSiteHPKPStatus(); // Incorrect size results in failure - try { + throws(() => { gSSService.setKeyPins("a.pinning2.example.com", true, 1000, 2, ["not a hash"]); - ok(false, "Attempting to set a pin with an incorrect size should have failed"); - } catch(e) { - } + }, /NS_ERROR_XPC_NOT_ENOUGH_ELEMENTS_IN_ARRAY/, + "Attempting to set a pin with an incorrect size should fail"); // Ensure built-in pins work as expected ok(!gSSService.isSecureHost(Ci.nsISiteSecurityService.HEADER_HPKP, diff --git a/security/manager/ssl/tests/unit/test_pinning_header_parsing.js b/security/manager/ssl/tests/unit/test_pinning_header_parsing.js index 9a594120305..9ab3b636add 100644 --- a/security/manager/ssl/tests/unit/test_pinning_header_parsing.js +++ b/security/manager/ssl/tests/unit/test_pinning_header_parsing.js @@ -25,13 +25,10 @@ function checkFailParseInvalidPin(pinValue) { let sslStatus = new FakeSSLStatus( certFromFile('cn-a.pinning2.example.com-pinningroot')); let uri = Services.io.newURI("https://a.pinning2.example.com", null, null); - try { + throws(() => { gSSService.processHeader(Ci.nsISiteSecurityService.HEADER_HPKP, uri, pinValue, sslStatus, 0); - ok(false, "Invalid pin should have been rejected"); - } catch (e) { - ok(true, "Invalid pin should be rejected"); - } + }, /NS_ERROR_FAILURE/, `Invalid pin "${pinValue}" should be rejected`); } function checkPassValidPin(pinValue, settingPin) { diff --git a/security/manager/ssl/tests/unit/test_pkcs11_safe_mode.js b/security/manager/ssl/tests/unit/test_pkcs11_safe_mode.js index e3ac01509f3..8264c4214c8 100644 --- a/security/manager/ssl/tests/unit/test_pkcs11_safe_mode.js +++ b/security/manager/ssl/tests/unit/test_pkcs11_safe_mode.js @@ -46,12 +46,6 @@ function run_test() { libraryFile.append("pkcs11testmodule"); libraryFile.append(libraryName); ok(libraryFile.exists(), "The pkcs11testmodule file should exist"); - let exceptionCaught = false; - try { - pkcs11.addModule("PKCS11 Test Module", libraryFile.path, 0, 0); - ok(false, "addModule should have thrown an exception"); - } catch (e) { - exceptionCaught = true; - } - ok(exceptionCaught, "addModule should have thrown an exception"); + throws(() => pkcs11.addModule("PKCS11 Test Module", libraryFile.path, 0, 0), + /NS_ERROR_FAILURE/, "addModule should throw when in safe mode"); } diff --git a/security/manager/ssl/tests/unit/test_sts_fqdn.js b/security/manager/ssl/tests/unit/test_sts_fqdn.js index be49318f2e3..c2c3eb2bd16 100644 --- a/security/manager/ssl/tests/unit/test_sts_fqdn.js +++ b/security/manager/ssl/tests/unit/test_sts_fqdn.js @@ -44,10 +44,7 @@ function run_test() { // gracefully. uri = Services.io.newURI("https://../foo", null, null); equal(uri.host, ".."); - try { + throws(() => { SSService.isSecureURI(Ci.nsISiteSecurityService.HEADER_HSTS, uri, 0); - ok(false); // this shouldn't run - } catch (e) { - equal(e.result, Cr.NS_ERROR_UNEXPECTED); - } + }, /NS_ERROR_UNEXPECTED/, "Malformed URI should be rejected"); }