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