toolkit/modules/tests/chrome/test_bug544442_checkCert.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/modules/tests/chrome/test_bug544442_checkCert.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,155 @@
     1.4 +<?xml version="1.0"?>
     1.5 +<!--
     1.6 +/* Any copyright is dedicated to the Public Domain.
     1.7 + * http://creativecommons.org/publicdomain/zero/1.0/
     1.8 + */
     1.9 +-->
    1.10 +
    1.11 +<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
    1.12 +<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
    1.13 +
    1.14 +<window title="Test CertUtils.jsm checkCert - bug 340198 and bug 544442"
    1.15 +        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
    1.16 +        onload="testStart();">
    1.17 +<script type="application/javascript"
    1.18 +        src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
    1.19 +
    1.20 +<script type="application/javascript">
    1.21 +<![CDATA[
    1.22 +
    1.23 +const Cc = Components.classes;
    1.24 +const Ci = Components.interfaces;
    1.25 +const Cr = Components.results;
    1.26 +
    1.27 +SimpleTest.waitForExplicitFinish();
    1.28 +
    1.29 +Components.utils.import("resource://gre/modules/CertUtils.jsm");
    1.30 +
    1.31 +function testStart() {
    1.32 +  ok(true, "Entering testStart");
    1.33 +
    1.34 +  var request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].
    1.35 +                createInstance(Ci.nsIXMLHttpRequest);
    1.36 +  request.open("GET", "https://example.com/", true);
    1.37 +  request.channel.notificationCallbacks = new BadCertHandler(true);
    1.38 +  request.onerror = function(event) { testXHRError(event); };
    1.39 +  request.onload = function(event) { testXHRLoad(event); };
    1.40 +  request.send(null);
    1.41 +}
    1.42 +
    1.43 +function testXHRError(aEvent) {
    1.44 +  ok(true, "Entering testXHRError - something went wrong");
    1.45 +
    1.46 +  var request = aEvent.target;
    1.47 +  var status = 0;
    1.48 +  try {
    1.49 +    status = request.status;
    1.50 +  }
    1.51 +  catch (e) {
    1.52 +  }
    1.53 +
    1.54 +  if (status == 0)
    1.55 +    status = request.channel.QueryInterface(Ci.nsIRequest).status;
    1.56 +
    1.57 +  ok(false, "XHR onerror called: " + status);
    1.58 +
    1.59 +  SimpleTest.finish();
    1.60 +}
    1.61 +
    1.62 +function getCheckCertResult(aChannel, aAllowNonBuiltIn, aCerts) {
    1.63 +  try {
    1.64 +    checkCert(aChannel, aAllowNonBuiltIn, aCerts);
    1.65 +  }
    1.66 +  catch (e) {
    1.67 +    return e.result;
    1.68 +  }
    1.69 +  return Cr.NS_OK;
    1.70 +}
    1.71 +
    1.72 +function testXHRLoad(aEvent) {
    1.73 +  ok(true, "Entering testXHRLoad");
    1.74 +
    1.75 +  var channel = aEvent.target.channel;
    1.76 +
    1.77 +  var certs = null;
    1.78 +  is(getCheckCertResult(channel, false, certs), Cr.NS_ERROR_ABORT,
    1.79 +     "checkCert should throw NS_ERROR_ABORT when the certificate attributes " +
    1.80 +     "array passed to checkCert is null and the certificate is not builtin");
    1.81 +
    1.82 +  is(getCheckCertResult(channel, true, certs), Cr.NS_OK,
    1.83 +     "checkCert should not throw when the certificate attributes array " +
    1.84 +     "passed to checkCert is null and builtin certificates aren't enforced");
    1.85 +
    1.86 +  certs = [ { invalidAttribute: "Invalid attribute" } ];
    1.87 +  is(getCheckCertResult(channel, false, certs), Cr.NS_ERROR_ILLEGAL_VALUE,
    1.88 +     "checkCert should throw NS_ERROR_ILLEGAL_VALUE when the certificate " +
    1.89 +     "attributes array passed to checkCert has an element that has an " +
    1.90 +     "attribute that does not exist on the certificate");
    1.91 +
    1.92 +  certs = [ { issuerName: "Incorrect issuerName" } ];
    1.93 +  is(getCheckCertResult(channel, false, certs), Cr.NS_ERROR_ILLEGAL_VALUE,
    1.94 +     "checkCert should throw NS_ERROR_ILLEGAL_VALUE when the certificate " +
    1.95 +     "attributes array passed to checkCert has an element that has an " +
    1.96 +     "issuerName that is not the same as the certificate's");
    1.97 +
    1.98 +  var cert = channel.securityInfo.QueryInterface(Ci.nsISSLStatusProvider).
    1.99 +             SSLStatus.QueryInterface(Ci.nsISSLStatus).serverCert;
   1.100 +
   1.101 +  certs = [ { issuerName: cert.issuerName,
   1.102 +              commonName: cert.commonName } ];
   1.103 +  is(getCheckCertResult(channel, false, certs), Cr.NS_ERROR_ABORT,
   1.104 +     "checkCert should throw NS_ERROR_ABORT when the certificate attributes " +
   1.105 +     "array passed to checkCert has a single element that has the same " +
   1.106 +     "issuerName and commonName as the certificate's and the certificate is " +
   1.107 +     "not builtin");
   1.108 +
   1.109 +  is(getCheckCertResult(channel, true, certs), Cr.NS_OK,
   1.110 +     "checkCert should not throw when the certificate attributes array " +
   1.111 +     "passed to checkCert has a single element that has the same issuerName " +
   1.112 +     "and commonName as the certificate's and and builtin certificates " +
   1.113 +     "aren't enforced");
   1.114 +
   1.115 +  certs = [ { issuerName: "Incorrect issuerName",
   1.116 +              invalidAttribute: "Invalid attribute" },
   1.117 +            { issuerName: cert.issuerName,
   1.118 +              commonName: "Invalid Common Name" },
   1.119 +            { issuerName: cert.issuerName,
   1.120 +              commonName: cert.commonName } ];
   1.121 +  is(getCheckCertResult(channel, false, certs), Cr.NS_ERROR_ABORT,
   1.122 +     "checkCert should throw NS_ERROR_ABORT when the certificate attributes " +
   1.123 +     "array passed to checkCert has an element that has the same issuerName " +
   1.124 +     "and commonName as the certificate's and the certificate is not builtin");
   1.125 +
   1.126 +  is(getCheckCertResult(channel, true, certs), Cr.NS_OK,
   1.127 +     "checkCert should not throw when the certificate attributes array " +
   1.128 +     "passed to checkCert has an element that has the same issuerName and " +
   1.129 +     "commonName as the certificate's and builtin certificates aren't enforced");
   1.130 +
   1.131 +  var mockChannel = { originalURI: Cc["@mozilla.org/network/io-service;1"].
   1.132 +                                   getService(Ci.nsIIOService).
   1.133 +                                   newURI("http://example.com/", null, null) };
   1.134 +
   1.135 +  certs = [ ];
   1.136 +  is(getCheckCertResult(mockChannel, false, certs), Cr.NS_ERROR_UNEXPECTED,
   1.137 +     "checkCert should throw NS_ERROR_UNEXPECTED when the certificate " +
   1.138 +     "attributes array passed to checkCert is not null and the channel's " +
   1.139 +     "originalURI is not https");
   1.140 +
   1.141 +  certs = null;
   1.142 +  is(getCheckCertResult(mockChannel, false, certs), Cr.NS_OK,
   1.143 +     "checkCert should not throw when the certificate attributes object " +
   1.144 +     "passed to checkCert is null and the the channel's originalURI is not " +
   1.145 +     "https");
   1.146 +
   1.147 +  SimpleTest.finish();
   1.148 +}
   1.149 +
   1.150 +]]>
   1.151 +</script>
   1.152 +
   1.153 +<body xmlns="http://www.w3.org/1999/xhtml">
   1.154 +  <p id="display"></p>
   1.155 +  <div id="content" style="display: none"></div>
   1.156 +  <pre id="test"></pre>
   1.157 +</body>
   1.158 +</window>

mercurial