Update V8 to version 4.6.61.
[chromium-blink-merge.git] / ios / web / net / crw_cert_policy_cache_unittest.mm
blob3e3e2bf32d4a158bd790c3983db790c0b829ae44
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ios/web/net/crw_cert_policy_cache.h"
7 #import "base/mac/scoped_nsobject.h"
8 #import "base/memory/scoped_ptr.h"
9 #include "base/strings/sys_string_conversions.h"
10 #include "base/test/ios/wait_util.h"
11 #include "ios/web/public/certificate_policy_cache.h"
12 #include "ios/web/public/test/test_web_thread_bundle.h"
13 #include "ios/web/public/web_thread.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/platform_test.h"
17 namespace net {
18 namespace {
20 // Test hostname for CertVerifier.
21 const char kHostName[] = "chromium.org";
23 // Mocks CertificatePolicyCache for CRWCertPolicyCache testing.
24 class CertificatePolicyCacheMock : public web::CertificatePolicyCache {
25  public:
26   MOCK_METHOD3(AllowCertForHost,
27                void(X509Certificate *cert, const std::string &host,
28                     net::CertStatus error));
29   MOCK_METHOD3(QueryPolicy, web::CertPolicy::Judgment(X509Certificate *cert,
30                                                       const std::string &host,
31                                                       net::CertStatus error));
33 private:
34   ~CertificatePolicyCacheMock() {}  // RefCounted requirement.
37 // Verfies that current thread is IOThread.
38 ACTION(CheckIOThread) {
39   DCHECK(web::WebThread::CurrentlyOn(web::WebThread::IO));
42 }  // namespace
44 // Test fixture to test CRWCertPolicyCache class.
45 class CRWCertPolicyCacheTest : public PlatformTest {
46  protected:
47   CRWCertPolicyCacheTest()
48       : cert_(new X509Certificate("test", "test", base::Time(), base::Time())),
49         cache_mock_(new CertificatePolicyCacheMock()),
50         testable_([[CRWCertPolicyCache alloc] initWithCache:cache_mock_]),
51         thread_bundle_(new web::TestWebThreadBundle(
52             web::TestWebThreadBundle::REAL_IO_THREAD)) {}
54   // Fake certificate created for testing.
55   scoped_refptr<X509Certificate> cert_;
56   // CertificatePolicyCacheMock.
57   scoped_refptr<CertificatePolicyCacheMock> cache_mock_;
58   // Testable |CertVerifierBlockAdapter| object.
59   base::scoped_nsobject<CRWCertPolicyCache> testable_;
60   // The threads used for testing.
61   scoped_ptr<web::TestWebThreadBundle> thread_bundle_;
64 // Tests |allowCert:forHost:status:| with default arguments.
65 TEST_F(CRWCertPolicyCacheTest, TestAllowingCertForHost) {
66   // Set up expectation.
67   EXPECT_CALL(*cache_mock_,
68               AllowCertForHost(cert_.get(), kHostName, CERT_STATUS_ALL_ERRORS))
69       .Times(1)
70       .WillOnce(CheckIOThread());
72   // Run and wait for IO thread completion.
73   [testable_ allowCert:cert_.get()
74                forHost:base::SysUTF8ToNSString(kHostName)
75                 status:CERT_STATUS_ALL_ERRORS];
76   thread_bundle_.reset();
79 // Tests |queryJudgementForCert:forHost:status:| with default arguments.
80 TEST_F(CRWCertPolicyCacheTest, TestQueryJudgment) {
81   // Set up expectation.
82   web::CertPolicy::Judgment expected_policy = web::CertPolicy::ALLOWED;
83   EXPECT_CALL(*cache_mock_,
84               QueryPolicy(cert_.get(), kHostName, CERT_STATUS_ALL_ERRORS))
85       .Times(1)
86       .WillOnce(
87           testing::DoAll(CheckIOThread(), testing::Return(expected_policy)));
89   // Run and wait for completion.
90   __block bool verification_completed = false;
91   [testable_ queryJudgementForCert:cert_.get()
92                            forHost:base::SysUTF8ToNSString(kHostName)
93                             status:CERT_STATUS_ALL_ERRORS
94                  completionHandler:^(web::CertPolicy::Judgment policy) {
95                    DCHECK_EQ(expected_policy, policy);
96                    verification_completed = true;
97                  }];
99   base::test::ios::WaitUntilCondition(^{
100     return verification_completed;
101   });
104 }  // namespace net