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/run_loop.h"
9 #include "chrome/test/base/testing_profile.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/test/test_browser_thread_bundle.h"
12 #include "net/ssl/channel_id_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"
17 using content::BrowserThread
;
19 class BrowsingDataChannelIDHelperTest
20 : public testing::Test
,
21 public net::SSLConfigService::Observer
{
23 BrowsingDataChannelIDHelperTest() : ssl_config_changed_count_(0) {
26 virtual void SetUp() override
{
27 testing_profile_
.reset(new TestingProfile());
29 testing_profile_
->GetSSLConfigService()->AddObserver(this);
32 virtual void TearDown() override
{
33 testing_profile_
->GetSSLConfigService()->RemoveObserver(this);
36 void CreateChannelIDsForTest() {
37 net::URLRequestContext
* context
=
38 testing_profile_
->GetRequestContext()->GetURLRequestContext();
39 net::ChannelIDStore
* channel_id_store
=
40 context
->channel_id_service()->GetChannelIDStore();
41 channel_id_store
->SetChannelID("https://www.google.com:443",
42 base::Time(), base::Time(),
44 channel_id_store
->SetChannelID("https://www.youtube.com:443",
45 base::Time(), base::Time(),
50 const net::ChannelIDStore::ChannelIDList
& channel_ids
) {
51 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
52 channel_id_list_
= channel_ids
;
55 // net::SSLConfigService::Observer implementation:
56 virtual void OnSSLConfigChanged() override
{
57 ssl_config_changed_count_
++;
61 content::TestBrowserThreadBundle thread_bundle_
;
62 scoped_ptr
<TestingProfile
> testing_profile_
;
64 net::ChannelIDStore::ChannelIDList channel_id_list_
;
66 int ssl_config_changed_count_
;
69 TEST_F(BrowsingDataChannelIDHelperTest
, FetchData
) {
70 CreateChannelIDsForTest();
71 scoped_refptr
<BrowsingDataChannelIDHelper
> helper(
72 BrowsingDataChannelIDHelper::Create(
73 testing_profile_
->GetRequestContext()));
75 helper
->StartFetching(
76 base::Bind(&BrowsingDataChannelIDHelperTest::FetchCallback
,
77 base::Unretained(this)));
79 // Blocks until BrowsingDataChannelIDHelperTest::FetchCallback is
81 base::RunLoop().RunUntilIdle();
83 ASSERT_EQ(2UL, channel_id_list_
.size());
84 net::ChannelIDStore::ChannelIDList::const_iterator it
=
85 channel_id_list_
.begin();
87 // Correct because fetching channel_id_list_ will get them out in the
88 // same order CreateChannelIDsForTest put them in.
89 ASSERT_TRUE(it
!= channel_id_list_
.end());
90 EXPECT_EQ("https://www.google.com:443", it
->server_identifier());
92 ASSERT_TRUE(++it
!= channel_id_list_
.end());
93 EXPECT_EQ("https://www.youtube.com:443", it
->server_identifier());
95 ASSERT_TRUE(++it
== channel_id_list_
.end());
97 EXPECT_EQ(0, ssl_config_changed_count_
);
100 TEST_F(BrowsingDataChannelIDHelperTest
, DeleteChannelID
) {
101 CreateChannelIDsForTest();
102 scoped_refptr
<BrowsingDataChannelIDHelper
> helper(
103 BrowsingDataChannelIDHelper::Create(
104 testing_profile_
->GetRequestContext()));
106 helper
->DeleteChannelID("https://www.google.com:443");
108 helper
->StartFetching(
109 base::Bind(&BrowsingDataChannelIDHelperTest::FetchCallback
,
110 base::Unretained(this)));
111 base::RunLoop().RunUntilIdle();
113 EXPECT_EQ(1, ssl_config_changed_count_
);
114 ASSERT_EQ(1UL, channel_id_list_
.size());
115 net::ChannelIDStore::ChannelIDList::const_iterator it
=
116 channel_id_list_
.begin();
118 ASSERT_TRUE(it
!= channel_id_list_
.end());
119 EXPECT_EQ("https://www.youtube.com:443", it
->server_identifier());
121 ASSERT_TRUE(++it
== channel_id_list_
.end());
123 helper
->DeleteChannelID("https://www.youtube.com:443");
125 helper
->StartFetching(
126 base::Bind(&BrowsingDataChannelIDHelperTest::FetchCallback
,
127 base::Unretained(this)));
128 base::RunLoop().RunUntilIdle();
130 EXPECT_EQ(2, ssl_config_changed_count_
);
131 ASSERT_EQ(0UL, channel_id_list_
.size());
134 TEST_F(BrowsingDataChannelIDHelperTest
, CannedEmpty
) {
135 std::string origin
= "https://www.google.com";
137 scoped_refptr
<CannedBrowsingDataChannelIDHelper
> helper(
138 new CannedBrowsingDataChannelIDHelper());
140 ASSERT_TRUE(helper
->empty());
141 helper
->AddChannelID(net::ChannelIDStore::ChannelID(
142 origin
, base::Time(), base::Time(), "key", "cert"));
143 ASSERT_FALSE(helper
->empty());
145 ASSERT_TRUE(helper
->empty());