1 // Copyright 2014 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 "chrome/browser/browsing_data/browsing_data_channel_id_helper.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/run_loop.h"
10 #include "chrome/test/base/testing_profile.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/test/test_browser_thread_bundle.h"
13 #include "crypto/ec_private_key.h"
14 #include "net/ssl/channel_id_service.h"
15 #include "net/url_request/url_request_context.h"
16 #include "net/url_request/url_request_context_getter.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 using content::BrowserThread
;
21 class BrowsingDataChannelIDHelperTest
22 : public testing::Test
,
23 public net::SSLConfigService::Observer
{
25 BrowsingDataChannelIDHelperTest() : ssl_config_changed_count_(0) {
28 void SetUp() override
{
29 testing_profile_
.reset(new TestingProfile());
31 testing_profile_
->GetSSLConfigService()->AddObserver(this);
34 void TearDown() override
{
35 testing_profile_
->GetSSLConfigService()->RemoveObserver(this);
38 void CreateChannelIDsForTest() {
39 net::URLRequestContext
* context
=
40 testing_profile_
->GetRequestContext()->GetURLRequestContext();
41 net::ChannelIDStore
* channel_id_store
=
42 context
->channel_id_service()->GetChannelIDStore();
43 channel_id_store
->SetChannelID(
44 make_scoped_ptr(new net::ChannelIDStore::ChannelID(
45 "https://www.google.com:443", base::Time(),
46 make_scoped_ptr(crypto::ECPrivateKey::Create()))));
47 channel_id_store
->SetChannelID(
48 make_scoped_ptr(new net::ChannelIDStore::ChannelID(
49 "https://www.youtube.com:443", base::Time(),
50 make_scoped_ptr(crypto::ECPrivateKey::Create()))));
54 const net::ChannelIDStore::ChannelIDList
& channel_ids
) {
55 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
56 channel_id_list_
= channel_ids
;
59 // net::SSLConfigService::Observer implementation:
60 void OnSSLConfigChanged() override
{ ssl_config_changed_count_
++; }
63 content::TestBrowserThreadBundle thread_bundle_
;
64 scoped_ptr
<TestingProfile
> testing_profile_
;
66 net::ChannelIDStore::ChannelIDList channel_id_list_
;
68 int ssl_config_changed_count_
;
71 TEST_F(BrowsingDataChannelIDHelperTest
, FetchData
) {
72 CreateChannelIDsForTest();
73 scoped_refptr
<BrowsingDataChannelIDHelper
> helper(
74 BrowsingDataChannelIDHelper::Create(
75 testing_profile_
->GetRequestContext()));
77 helper
->StartFetching(
78 base::Bind(&BrowsingDataChannelIDHelperTest::FetchCallback
,
79 base::Unretained(this)));
81 // Blocks until BrowsingDataChannelIDHelperTest::FetchCallback is
83 base::RunLoop().RunUntilIdle();
85 ASSERT_EQ(2UL, channel_id_list_
.size());
86 net::ChannelIDStore::ChannelIDList::const_iterator it
=
87 channel_id_list_
.begin();
89 // Correct because fetching channel_id_list_ will get them out in the
90 // same order CreateChannelIDsForTest put them in.
91 ASSERT_TRUE(it
!= channel_id_list_
.end());
92 EXPECT_EQ("https://www.google.com:443", it
->server_identifier());
94 ASSERT_TRUE(++it
!= channel_id_list_
.end());
95 EXPECT_EQ("https://www.youtube.com:443", it
->server_identifier());
97 ASSERT_TRUE(++it
== channel_id_list_
.end());
99 EXPECT_EQ(0, ssl_config_changed_count_
);
102 TEST_F(BrowsingDataChannelIDHelperTest
, DeleteChannelID
) {
103 CreateChannelIDsForTest();
104 scoped_refptr
<BrowsingDataChannelIDHelper
> helper(
105 BrowsingDataChannelIDHelper::Create(
106 testing_profile_
->GetRequestContext()));
108 helper
->DeleteChannelID("https://www.google.com:443");
110 helper
->StartFetching(
111 base::Bind(&BrowsingDataChannelIDHelperTest::FetchCallback
,
112 base::Unretained(this)));
113 base::RunLoop().RunUntilIdle();
115 EXPECT_EQ(1, ssl_config_changed_count_
);
116 ASSERT_EQ(1UL, channel_id_list_
.size());
117 net::ChannelIDStore::ChannelIDList::const_iterator it
=
118 channel_id_list_
.begin();
120 ASSERT_TRUE(it
!= channel_id_list_
.end());
121 EXPECT_EQ("https://www.youtube.com:443", it
->server_identifier());
123 ASSERT_TRUE(++it
== channel_id_list_
.end());
125 helper
->DeleteChannelID("https://www.youtube.com:443");
127 helper
->StartFetching(
128 base::Bind(&BrowsingDataChannelIDHelperTest::FetchCallback
,
129 base::Unretained(this)));
130 base::RunLoop().RunUntilIdle();
132 EXPECT_EQ(2, ssl_config_changed_count_
);
133 ASSERT_EQ(0UL, channel_id_list_
.size());
136 TEST_F(BrowsingDataChannelIDHelperTest
, CannedEmpty
) {
137 std::string origin
= "https://www.google.com";
139 scoped_refptr
<CannedBrowsingDataChannelIDHelper
> helper(
140 new CannedBrowsingDataChannelIDHelper());
142 ASSERT_TRUE(helper
->empty());
143 helper
->AddChannelID(net::ChannelIDStore::ChannelID(
144 origin
, base::Time(), make_scoped_ptr(crypto::ECPrivateKey::Create())));
145 ASSERT_FALSE(helper
->empty());
147 ASSERT_TRUE(helper
->empty());