OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "chrome/browser/browsing_data_server_bound_cert_helper.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/message_loop.h" | |
9 #include "base/synchronization/waitable_event.h" | |
10 #include "chrome/test/base/testing_profile.h" | |
11 #include "content/public/test/test_browser_thread.h" | |
12 #include "net/base/server_bound_cert_service.h" | |
13 #include "net/url_request/url_request_context.h" | |
14 #include "net/url_request/url_request_context_getter.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 using content::BrowserThread; | |
18 | |
19 class BrowsingDataServerBoundCertHelperTest : public testing::Test { | |
20 public: | |
21 virtual void SetUp() { | |
22 ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI, | |
23 &message_loop_)); | |
24 io_thread_.reset(new content::TestBrowserThread(BrowserThread::IO, | |
25 &message_loop_)); | |
26 testing_profile_.reset(new TestingProfile()); | |
27 testing_profile_->CreateRequestContext(); | |
28 } | |
29 | |
30 void CreateCertsForTest() { | |
31 net::URLRequestContext* context = | |
32 testing_profile_->GetRequestContext()->GetURLRequestContext(); | |
33 net::ServerBoundCertStore* cert_store = | |
34 context->server_bound_cert_service()->GetCertStore(); | |
35 cert_store->SetServerBoundCert("https://www.google.com:443", | |
36 net::CLIENT_CERT_RSA_SIGN, | |
37 base::Time(), base::Time(), | |
38 "key", "cert"); | |
39 cert_store->SetServerBoundCert("https://www.youtube.com:443", | |
40 net::CLIENT_CERT_RSA_SIGN, | |
41 base::Time(), base::Time(), | |
42 "key", "cert"); | |
43 } | |
44 | |
45 void FetchCallback( | |
46 const net::ServerBoundCertStore::ServerBoundCertList& certs) { | |
47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
48 server_bound_cert_list_ = certs; | |
49 MessageLoop::current()->Quit(); | |
50 } | |
51 | |
52 protected: | |
53 MessageLoop message_loop_; | |
54 scoped_ptr<content::TestBrowserThread> ui_thread_; | |
55 scoped_ptr<content::TestBrowserThread> io_thread_; | |
56 scoped_ptr<TestingProfile> testing_profile_; | |
57 | |
58 net::ServerBoundCertStore::ServerBoundCertList server_bound_cert_list_; | |
59 }; | |
60 | |
61 TEST_F(BrowsingDataServerBoundCertHelperTest, FetchData) { | |
62 CreateCertsForTest(); | |
63 scoped_refptr<BrowsingDataServerBoundCertHelper> helper( | |
64 BrowsingDataServerBoundCertHelper::Create(testing_profile_.get())); | |
65 | |
66 helper->StartFetching( | |
67 base::Bind(&BrowsingDataServerBoundCertHelperTest::FetchCallback, | |
68 base::Unretained(this))); | |
69 | |
70 // Blocks until BrowsingDataServerBoundCertHelperTest::FetchCallback is | |
71 // notified. | |
72 MessageLoop::current()->Run(); | |
73 | |
74 ASSERT_EQ(2UL, server_bound_cert_list_.size()); | |
75 net::ServerBoundCertStore::ServerBoundCertList::const_iterator it = | |
76 server_bound_cert_list_.begin(); | |
77 | |
78 // Correct because fetching server_bound_cert_list_ will get them out in the | |
79 // same order CreateCertsForTest put them in. | |
80 ASSERT_TRUE(it != server_bound_cert_list_.end()); | |
81 EXPECT_EQ("https://www.google.com:443", it->server_identifier()); | |
82 | |
83 ASSERT_TRUE(++it != server_bound_cert_list_.end()); | |
84 EXPECT_EQ("https://www.youtube.com:443", it->server_identifier()); | |
85 | |
86 ASSERT_TRUE(++it == server_bound_cert_list_.end()); | |
87 } | |
88 | |
89 TEST_F(BrowsingDataServerBoundCertHelperTest, DeleteCert) { | |
90 CreateCertsForTest(); | |
91 scoped_refptr<BrowsingDataServerBoundCertHelper> helper( | |
92 BrowsingDataServerBoundCertHelper::Create(testing_profile_.get())); | |
93 | |
94 helper->DeleteServerBoundCert("https://www.google.com:443"); | |
95 | |
96 helper->StartFetching( | |
97 base::Bind(&BrowsingDataServerBoundCertHelperTest::FetchCallback, | |
98 base::Unretained(this))); | |
99 MessageLoop::current()->Run(); | |
100 | |
101 ASSERT_EQ(1UL, server_bound_cert_list_.size()); | |
102 net::ServerBoundCertStore::ServerBoundCertList::const_iterator it = | |
103 server_bound_cert_list_.begin(); | |
104 | |
105 ASSERT_TRUE(it != server_bound_cert_list_.end()); | |
106 EXPECT_EQ("https://www.youtube.com:443", it->server_identifier()); | |
107 | |
108 ASSERT_TRUE(++it == server_bound_cert_list_.end()); | |
109 | |
110 helper->DeleteServerBoundCert("https://www.youtube.com:443"); | |
111 | |
112 helper->StartFetching( | |
113 base::Bind(&BrowsingDataServerBoundCertHelperTest::FetchCallback, | |
114 base::Unretained(this))); | |
115 MessageLoop::current()->Run(); | |
116 ASSERT_EQ(0UL, server_bound_cert_list_.size()); | |
117 } | |
118 | |
119 TEST_F(BrowsingDataServerBoundCertHelperTest, CannedUnique) { | |
120 std::string origin = "https://www.google.com:443"; | |
121 | |
122 scoped_refptr<CannedBrowsingDataServerBoundCertHelper> helper( | |
123 new CannedBrowsingDataServerBoundCertHelper()); | |
124 | |
125 ASSERT_TRUE(helper->empty()); | |
126 helper->AddServerBoundCert(net::ServerBoundCertStore::ServerBoundCert( | |
127 origin, net::CLIENT_CERT_RSA_SIGN, base::Time(), base::Time(), "key", | |
128 "cert")); | |
129 helper->AddServerBoundCert(net::ServerBoundCertStore::ServerBoundCert( | |
130 origin, net::CLIENT_CERT_ECDSA_SIGN, base::Time(), base::Time(), "key", | |
131 "cert")); | |
132 | |
133 helper->StartFetching( | |
134 base::Bind(&BrowsingDataServerBoundCertHelperTest::FetchCallback, | |
135 base::Unretained(this))); | |
136 MessageLoop::current()->Run(); | |
137 | |
138 ASSERT_EQ(1UL, server_bound_cert_list_.size()); | |
139 net::ServerBoundCertStore::ServerBoundCert& cert = | |
140 server_bound_cert_list_.front(); | |
141 | |
142 EXPECT_EQ("https://www.google.com:443", cert.server_identifier()); | |
143 EXPECT_EQ(net::CLIENT_CERT_ECDSA_SIGN, cert.type()); | |
144 } | |
145 | |
146 TEST_F(BrowsingDataServerBoundCertHelperTest, CannedEmpty) { | |
147 std::string origin = "https://www.google.com"; | |
148 | |
149 scoped_refptr<CannedBrowsingDataServerBoundCertHelper> helper( | |
150 new CannedBrowsingDataServerBoundCertHelper()); | |
151 | |
152 ASSERT_TRUE(helper->empty()); | |
153 helper->AddServerBoundCert(net::ServerBoundCertStore::ServerBoundCert( | |
154 origin, net::CLIENT_CERT_RSA_SIGN, base::Time(), base::Time(), "key", | |
155 "cert")); | |
156 ASSERT_FALSE(helper->empty()); | |
157 helper->Reset(); | |
158 ASSERT_TRUE(helper->empty()); | |
159 } | |
OLD | NEW |