Bug 933109: Add tests to verify that we retry OCSP when we have a cached Unknown response, r=keeler, a=test-only
authorBrian Smith <brian@briansmith.org>
Mon, 06 Jan 2014 14:41:04 -0800
changeset 175579 cd228d859f4b8eaeeeb4ff712a9db043d9d1978c
parent 175578 0b825ae5294affa69206e039072686db69a27d29
child 175580 911af5c9467118153e45a8d30bfca00c9cd8a9bb
push id445
push userffxbld
push dateMon, 10 Mar 2014 22:05:19 +0000
treeherdermozilla-release@dc38b741b04e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskeeler, test-only
bugs933109
milestone28.0a2
Bug 933109: Add tests to verify that we retry OCSP when we have a cached Unknown response, r=keeler, a=test-only
security/manager/ssl/tests/unit/head_psm.js
security/manager/ssl/tests/unit/test_ocsp_unknown_caching.js
security/manager/ssl/tests/unit/xpcshell.ini
--- a/security/manager/ssl/tests/unit/head_psm.js
+++ b/security/manager/ssl/tests/unit/head_psm.js
@@ -148,21 +148,22 @@ function clearSessionCache() {
 "use strict";
 
 // <documentation on your test>
 
 function run_test() {
   do_get_profile();
   add_tls_server_setup("<test-server-name>");
 
-  add_connection_test("<test-name-1>.example.com", Cr.<expected result>,
-                      <ocsp stapling enabled>);
+  add_connection_test("<test-name-1>.example.com",
+                      getXPCOMStatusFromNSS(SEC_ERROR_xxx),
+                      function() { ... },
+                      function(aTransportSecurityInfo) { ... });
   [...]
-  add_connection_test("<test-name-n>.example.com", Cr.<expected result>,
-                      <ocsp stapling enabled>);
+  add_connection_test("<test-name-n>.example.com", Cr.NS_OK);
 
   run_next_test();
 }
 
 */
 function add_tls_server_setup(serverBinName) {
   add_test(function() {
     _setupTLSServerTest(serverBinName);
@@ -246,24 +247,20 @@ function add_connection_test(aHost, aExp
     return connection.go();
   }
 
   add_test(function() {
     if (aBeforeConnect) {
       aBeforeConnect();
     }
     connectTo(aHost).then(function(conn) {
-      dump("hello #0\n");
       do_check_eq(conn.result, aExpectedResult);
-      dump("hello #0.5\n");
       if (aWithSecurityInfo) {
-        dump("hello #1\n");
         aWithSecurityInfo(conn.transport.securityInfo
                               .QueryInterface(Ci.nsITransportSecurityInfo));
-        dump("hello #2\n");
       }
       run_next_test();
     });
   });
 }
 
 function _getBinaryUtil(binaryUtilName) {
   let directoryService = Cc["@mozilla.org/file/directory_service;1"]
new file mode 100644
--- /dev/null
+++ b/security/manager/ssl/tests/unit/test_ocsp_unknown_caching.js
@@ -0,0 +1,87 @@
+// -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+"use strict";
+
+let gFetchCount = 0;
+
+function run_test() {
+  do_get_profile();
+  Services.prefs.setBoolPref("security.ssl.enable_ocsp_stapling", true);
+  add_tls_server_setup("OCSPStaplingServer");
+
+  let ocspResponder = new HttpServer();
+  ocspResponder.registerPrefixHandler("/", function(request, response) {
+    ++gFetchCount;
+
+    do_print("gFetchCount: " + gFetchCount);
+
+    if (gFetchCount != 2) {
+      do_print("returning 500 Internal Server Error");
+
+      response.setStatusLine(request.httpVersion, 500, "Internal Server Error");
+      let body = "Refusing to return a response";
+      response.bodyOutputStream.write(body, body.length);
+      return;
+    }
+
+    do_print("returning 200 OK");
+
+    let nickname = "localhostAndExampleCom";
+    do_print("Generating ocsp response for '" + nickname + "'");
+    let args = [ ["good", nickname, "unused" ] ];
+    let ocspResponses = generateOCSPResponses(args, "tlsserver");
+    let goodResponse = ocspResponses[0];
+
+    response.setStatusLine(request.httpVersion, 200, "OK");
+    response.setHeader("Content-Type", "application/ocsp-response");
+    response.bodyOutputStream.write(goodResponse, goodResponse.length);
+  });
+  ocspResponder.start(8080);
+
+  // This test assumes that OCSPStaplingServer uses the same cert for
+  // ocsp-stapling-unknown.example.com and ocsp-stapling-none.example.com.
+
+  // Get an Unknown response for the *.exmaple.com cert and put it in the
+  // OCSP cache.
+  add_connection_test("ocsp-stapling-unknown.example.com",
+                      getXPCOMStatusFromNSS(SEC_ERROR_OCSP_UNKNOWN_CERT),
+                      clearSessionCache);
+  add_test(function() { do_check_eq(gFetchCount, 0); run_next_test(); });
+
+  // A failure to retrieve an OCSP response must result in the cached Unkown
+  // response being recognized and honored.
+  add_connection_test("ocsp-stapling-none.example.com",
+                      getXPCOMStatusFromNSS(SEC_ERROR_OCSP_UNKNOWN_CERT),
+                      clearSessionCache);
+  add_test(function() { do_check_eq(gFetchCount, 1); run_next_test(); });
+
+  // A valid Good response from the OCSP responder must override the cached
+  // Unknown response.
+  //
+  // Note that We need to make sure that the Unknown response and the Good
+  // response have different thisUpdate timestamps; otherwise, the Good
+  // response will be seen as "not newer" and it won't replace the existing
+  // entry.
+  add_test(function() {
+    let duration = 1200;
+    do_print("Sleeping for " + duration + "ms");
+    let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
+    timer.initWithCallback(run_next_test, duration, Ci.nsITimer.TYPE_ONE_SHOT);
+  });
+  add_connection_test("ocsp-stapling-none.example.com", Cr.NS_OK,
+                      clearSessionCache);
+  add_test(function() { do_check_eq(gFetchCount, 2); run_next_test(); });
+
+  // The Good response retrieved from the previous fetch must have replaced
+  // the Unknown response in the cache, resulting in the catched Good response
+  // being returned and no fetch.
+  add_connection_test("ocsp-stapling-none.example.com", Cr.NS_OK,
+                      clearSessionCache);
+  add_test(function() { do_check_eq(gFetchCount, 2); run_next_test(); });
+
+  add_test(function() { ocspResponder.stop(run_next_test); run_next_test(); });
+
+  run_next_test();
+}
--- a/security/manager/ssl/tests/unit/xpcshell.ini
+++ b/security/manager/ssl/tests/unit/xpcshell.ini
@@ -31,15 +31,19 @@ skip-if = os == "android"
 [test_ocsp_stapling.js]
 run-sequentially = hardcoded ports
 # Bug 676972: test fails consistently on Android
 fail-if = os == "android"
 [test_ocsp_stapling_expired.js]
 run-sequentially = hardcoded ports
 # Bug 676972: test fails consistently on Android
 fail-if = os == "android"
+[test_ocsp_unknown_caching.js]
+run-sequentially = hardcoded ports
+# Bug 676972: test fails consistently on Android
+fail-if = os == "android"
 [test_sts_ipv4_ipv6.js]
 [test_cert_signatures.js]
 # Bug 676972: test fails consistently on Android
 fail-if = os == "android"
 [test_ev_certs.js]
 # Bug 676972: test fails consistently on Android
 fail-if = os == "android"