Bug 1243193 - Use Assert.throws() more in PSM tests. r=keeler

This commit is contained in:
Cykesiopka 2016-02-10 21:40:00 +01:00
parent 88519e1241
commit 6539d69a61
7 changed files with 26 additions and 57 deletions

View File

@ -53,14 +53,8 @@ function encodeCommonNameAsBytes(commonName) {
} }
function testInvalidDBKey(certDB, dbKey) { function testInvalidDBKey(certDB, dbKey) {
let exceptionCaught = false; throws(() => certDB.findCertByDBKey(dbKey), /NS_ERROR_ILLEGAL_INPUT/,
try { `findCertByDBKey(${dbKey}) should raise NS_ERROR_ILLEGAL_INPUT`);
let cert = certDB.findCertByDBKey(dbKey);
} catch(e) {
do_print(e);
exceptionCaught = true;
}
ok(exceptionCaught, "should have thrown and caught an exception");
} }
function testDBKeyForNonexistentCert(certDB, dbKey) { function testDBKeyForNonexistentCert(certDB, dbKey) {

View File

@ -35,26 +35,22 @@ function testGood(data) {
} }
function testBad(data) { function testBad(data) {
try { throws(() => certDB.constructX509FromBase64(data.input), data.result,
let cert = certDB.constructX509FromBase64(data.input); `Should get "${data.result}" for "${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");
}
} }
function run_test() { function run_test() {
const badCases = [ const badCases = [
// Wrong type or too short // Wrong type or too short
{ input: null, result: Cr.NS_ERROR_ILLEGAL_VALUE }, { input: null, result: /NS_ERROR_ILLEGAL_VALUE/ },
{ input: "", result: Cr.NS_ERROR_ILLEGAL_VALUE }, { input: "", result: /NS_ERROR_ILLEGAL_VALUE/ },
{ input: "=", result: Cr.NS_ERROR_ILLEGAL_VALUE }, { input: "=", result: /NS_ERROR_ILLEGAL_VALUE/ },
{ input: "==", result: Cr.NS_ERROR_ILLEGAL_VALUE }, { input: "==", result: /NS_ERROR_ILLEGAL_VALUE/ },
// Not base64 // 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 // Not a cert
{ input: "Zm9ydHktZm91ciBkZWFkIHN0b25lIGxpb25z", result: Cr.NS_ERROR_FAILURE } { input: "Zm9ydHktZm91ciBkZWFkIHN0b25lIGxpb25z",
result: /NS_ERROR_FAILURE/ },
]; ];
// Real certs with all three padding levels // Real certs with all three padding levels

View File

@ -16,16 +16,10 @@ function connect_and_teardown() {
let reader = { let reader = {
onInputStreamReady: function(stream) { onInputStreamReady: function(stream) {
try { throws(() => stream.available(), /NS_ERROR_FAILURE/,
stream.available(); "stream should be in an error state");
Assert.ok(false, "stream.available() should have thrown"); ok(tearDown, "A tear down attempt should have occurred");
} run_next_test();
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();
}
} }
}; };

View File

@ -149,12 +149,10 @@ function checkStateRead(aSubject, aTopic, aData) {
checkDefaultSiteHPKPStatus(); checkDefaultSiteHPKPStatus();
// failure to insert new pin entry leaves previous pin behavior // failure to insert new pin entry leaves previous pin behavior
try { throws(() => {
gSSService.setKeyPins("a.pinning2.example.com", true, 1000, 1, gSSService.setKeyPins("a.pinning2.example.com", true, 1000, 1,
["not a hash"]); ["not a hash"]);
ok(false, "Attempting to set an invalid pin should have failed"); }, /NS_ERROR_ILLEGAL_VALUE/, "Attempting to set an invalid pin should fail");
} catch(e) {
}
checkFail(certFromFile('cn-a.pinning2.example.com-badca'), "a.pinning2.example.com"); 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-a.pinning2.example.com-pinningroot'), "a.pinning2.example.com");
checkOK(certFromFile('cn-x.a.pinning2.example.com-badca'), "x.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(); checkDefaultSiteHPKPStatus();
// Incorrect size results in failure // Incorrect size results in failure
try { throws(() => {
gSSService.setKeyPins("a.pinning2.example.com", true, 1000, 2, gSSService.setKeyPins("a.pinning2.example.com", true, 1000, 2,
["not a hash"]); ["not a hash"]);
ok(false, "Attempting to set a pin with an incorrect size should have failed"); }, /NS_ERROR_XPC_NOT_ENOUGH_ELEMENTS_IN_ARRAY/,
} catch(e) { "Attempting to set a pin with an incorrect size should fail");
}
// Ensure built-in pins work as expected // Ensure built-in pins work as expected
ok(!gSSService.isSecureHost(Ci.nsISiteSecurityService.HEADER_HPKP, ok(!gSSService.isSecureHost(Ci.nsISiteSecurityService.HEADER_HPKP,

View File

@ -25,13 +25,10 @@ function checkFailParseInvalidPin(pinValue) {
let sslStatus = new FakeSSLStatus( let sslStatus = new FakeSSLStatus(
certFromFile('cn-a.pinning2.example.com-pinningroot')); certFromFile('cn-a.pinning2.example.com-pinningroot'));
let uri = Services.io.newURI("https://a.pinning2.example.com", null, null); let uri = Services.io.newURI("https://a.pinning2.example.com", null, null);
try { throws(() => {
gSSService.processHeader(Ci.nsISiteSecurityService.HEADER_HPKP, uri, gSSService.processHeader(Ci.nsISiteSecurityService.HEADER_HPKP, uri,
pinValue, sslStatus, 0); pinValue, sslStatus, 0);
ok(false, "Invalid pin should have been rejected"); }, /NS_ERROR_FAILURE/, `Invalid pin "${pinValue}" should be rejected`);
} catch (e) {
ok(true, "Invalid pin should be rejected");
}
} }
function checkPassValidPin(pinValue, settingPin) { function checkPassValidPin(pinValue, settingPin) {

View File

@ -46,12 +46,6 @@ function run_test() {
libraryFile.append("pkcs11testmodule"); libraryFile.append("pkcs11testmodule");
libraryFile.append(libraryName); libraryFile.append(libraryName);
ok(libraryFile.exists(), "The pkcs11testmodule file should exist"); ok(libraryFile.exists(), "The pkcs11testmodule file should exist");
let exceptionCaught = false; throws(() => pkcs11.addModule("PKCS11 Test Module", libraryFile.path, 0, 0),
try { /NS_ERROR_FAILURE/, "addModule should throw when in safe mode");
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");
} }

View File

@ -44,10 +44,7 @@ function run_test() {
// gracefully. // gracefully.
uri = Services.io.newURI("https://../foo", null, null); uri = Services.io.newURI("https://../foo", null, null);
equal(uri.host, ".."); equal(uri.host, "..");
try { throws(() => {
SSService.isSecureURI(Ci.nsISiteSecurityService.HEADER_HSTS, uri, 0); SSService.isSecureURI(Ci.nsISiteSecurityService.HEADER_HSTS, uri, 0);
ok(false); // this shouldn't run }, /NS_ERROR_UNEXPECTED/, "Malformed URI should be rejected");
} catch (e) {
equal(e.result, Cr.NS_ERROR_UNEXPECTED);
}
} }