Fix use of SSLConfigService::SetCRLSetIfNewer() in test code.

This way the order that HTTPSCRLSetTest.* and HTTPSEVCRLSetTest.* are run does not affect their outcome.

Bug: 775323
Change-Id: I83691d98385811f6091bd38da3ec0ad4f0d33330
Reviewed-on: https://chromium-review.googlesource.com/722353
Commit-Queue: Eric Roman <[email protected]>
Reviewed-by: Matt Mueller <[email protected]>
Cr-Commit-Position: refs/heads/master@{#509275}
diff --git a/net/url_request/url_request_unittest.cc b/net/url_request/url_request_unittest.cc
index 4ebe4749..b39ae6b4 100644
--- a/net/url_request/url_request_unittest.cc
+++ b/net/url_request/url_request_unittest.cc
@@ -11234,6 +11234,23 @@
   }
 };
 
+// Helper class to set the global CRLSet, and on destruction restore the
+// previously set one.
+class ScopedSetCRLSet {
+ public:
+  ScopedSetCRLSet(scoped_refptr<CRLSet> crl_set) {
+    prev_crl_set_ = SSLConfigService::GetCRLSet();
+    SSLConfigService::SetCRLSetForTesting(std::move(crl_set));
+  }
+
+  ~ScopedSetCRLSet() {
+    SSLConfigService::SetCRLSetForTesting(std::move(prev_crl_set_));
+  }
+
+ private:
+  scoped_refptr<CRLSet> prev_crl_set_;
+};
+
 TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) {
   if (!SystemSupportsOCSP()) {
     LOG(WARNING) << "Skipping test because system doesn't support OCSP";
@@ -11244,7 +11261,7 @@
       SpawnedTestServer::SSLOptions::CERT_AUTO);
   ssl_options.ocsp_status =
       SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE;
-  SSLConfigService::SetCRLSetIfNewer(scoped_refptr<CRLSet>());
+  ScopedSetCRLSet set_crlset(nullptr);
 
   CertStatus cert_status;
   DoConnection(ssl_options, &cert_status);
@@ -11266,7 +11283,7 @@
   SpawnedTestServer::SSLOptions ssl_options(
       SpawnedTestServer::SSLOptions::CERT_AUTO);
   ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED;
-  SSLConfigService::SetCRLSetIfNewer(scoped_refptr<CRLSet>());
+  ScopedSetCRLSet set_crlset(nullptr);
 
   CertStatus cert_status;
   DoConnection(ssl_options, &cert_status);
@@ -11294,7 +11311,7 @@
   SpawnedTestServer::SSLOptions ssl_options(
       SpawnedTestServer::SSLOptions::CERT_AUTO);
   ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK;
-  SSLConfigService::SetCRLSetIfNewer(scoped_refptr<CRLSet>());
+  ScopedSetCRLSet set_crlset(nullptr);
 
   CertStatus cert_status;
   DoConnection(ssl_options, &cert_status);
@@ -11317,8 +11334,7 @@
       SpawnedTestServer::SSLOptions::CERT_AUTO);
   ssl_options.ocsp_status =
       SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE;
-  SSLConfigService::SetCRLSetIfNewer(
-      scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
+  ScopedSetCRLSet set_crlset(CRLSet::ExpiredCRLSetForTesting());
 
   CertStatus cert_status;
   DoConnection(ssl_options, &cert_status);
@@ -11341,8 +11357,7 @@
       SpawnedTestServer::SSLOptions::CERT_AUTO);
   ssl_options.ocsp_status =
       SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE;
-  SSLConfigService::SetCRLSetIfNewer(
-      scoped_refptr<CRLSet>(CRLSet::ForTesting(false, &kOCSPTestCertSPKI, "")));
+  ScopedSetCRLSet set_crlset(CRLSet::ForTesting(false, &kOCSPTestCertSPKI, ""));
 
   CertStatus cert_status;
   DoConnection(ssl_options, &cert_status);
@@ -11366,8 +11381,7 @@
       SpawnedTestServer::SSLOptions::CERT_AUTO);
   ssl_options.ocsp_status =
       SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE;
-  SSLConfigService::SetCRLSetIfNewer(
-      scoped_refptr<CRLSet>(CRLSet::EmptyCRLSetForTesting()));
+  ScopedSetCRLSet set_crlset(CRLSet::EmptyCRLSetForTesting());
 
   CertStatus cert_status = 0;
   DoConnection(ssl_options, &cert_status);
@@ -11399,8 +11413,7 @@
   SpawnedTestServer::SSLOptions ssl_options(
       SpawnedTestServer::SSLOptions::CERT_AUTO);
   ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED;
-  SSLConfigService::SetCRLSetIfNewer(
-      scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
+  ScopedSetCRLSet set_crlset(CRLSet::ExpiredCRLSetForTesting());
 
   CertStatus cert_status;
   DoConnection(ssl_options, &cert_status);
@@ -11427,8 +11440,7 @@
       SpawnedTestServer::SSLOptions::CERT_AUTO);
   ssl_options.ocsp_status =
       SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE;
-  SSLConfigService::SetCRLSetIfNewer(
-      scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
+  ScopedSetCRLSet set_crlset(CRLSet::ExpiredCRLSetForTesting());
 
   CertStatus cert_status;
   DoConnection(ssl_options, &cert_status);
@@ -11450,8 +11462,8 @@
       SpawnedTestServer::SSLOptions::CERT_AUTO);
   ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK;
   ssl_options.cert_serial = 10;
-  SSLConfigService::SetCRLSetIfNewer(scoped_refptr<CRLSet>(
-      CRLSet::ForTesting(false, &kOCSPTestCertSPKI, "\x0a")));
+  ScopedSetCRLSet set_crlset(
+      CRLSet::ForTesting(false, &kOCSPTestCertSPKI, "\x0a"));
 
   CertStatus cert_status = 0;
   DoConnection(ssl_options, &cert_status);