OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "ios/web/net/crw_cert_policy_cache.h" | |
6 | |
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" | |
16 | |
17 namespace net { | |
18 namespace { | |
19 | |
20 // Test hostname for CertVerifier. | |
21 const char kHostName[] = "chromium.org"; | |
22 | |
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)); | |
32 | |
33 private: | |
34 ~CertificatePolicyCacheMock() {} // RefCounted requirement. | |
35 }; | |
36 | |
37 // Verfies that current thread is IOThread. | |
38 ACTION(CheckIOThread) { | |
39 DCHECK(web::WebThread::CurrentlyOn(web::WebThread::IO)); | |
40 } | |
41 | |
42 } // namespace | |
43 | |
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)) {} | |
53 | |
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_; | |
62 }; | |
63 | |
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()); | |
71 | |
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(); | |
77 } | |
78 | |
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))); | |
88 | |
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 }]; | |
98 | |
99 base::test::ios::WaitUntilCondition(^{ | |
100 return verification_completed; | |
101 }); | |
102 } | |
103 | |
104 } // namespace net | |
OLD | NEW |