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 void SetUp() override
{
27 testing_profile_
.reset(new TestingProfile());
29 testing_profile_
->GetSSLConfigService()->AddObserver(this);
32 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 void OnSSLConfigChanged() override
{ ssl_config_changed_count_
++; }
59 content::TestBrowserThreadBundle thread_bundle_
;
60 scoped_ptr
<TestingProfile
> testing_profile_
;
62 net::ChannelIDStore::ChannelIDList channel_id_list_
;
64 int ssl_config_changed_count_
;
67 TEST_F(BrowsingDataChannelIDHelperTest
, FetchData
) {
68 CreateChannelIDsForTest();
69 scoped_refptr
<BrowsingDataChannelIDHelper
> helper(
70 BrowsingDataChannelIDHelper::Create(
71 testing_profile_
->GetRequestContext()));
73 helper
->StartFetching(
74 base::Bind(&BrowsingDataChannelIDHelperTest::FetchCallback
,
75 base::Unretained(this)));
77 // Blocks until BrowsingDataChannelIDHelperTest::FetchCallback is
79 base::RunLoop().RunUntilIdle();
81 ASSERT_EQ(2UL, channel_id_list_
.size());
82 net::ChannelIDStore::ChannelIDList::const_iterator it
=
83 channel_id_list_
.begin();
85 // Correct because fetching channel_id_list_ will get them out in the
86 // same order CreateChannelIDsForTest put them in.
87 ASSERT_TRUE(it
!= channel_id_list_
.end());
88 EXPECT_EQ("https://www.google.com:443", it
->server_identifier());
90 ASSERT_TRUE(++it
!= channel_id_list_
.end());
91 EXPECT_EQ("https://www.youtube.com:443", it
->server_identifier());
93 ASSERT_TRUE(++it
== channel_id_list_
.end());
95 EXPECT_EQ(0, ssl_config_changed_count_
);
98 TEST_F(BrowsingDataChannelIDHelperTest
, DeleteChannelID
) {
99 CreateChannelIDsForTest();
100 scoped_refptr
<BrowsingDataChannelIDHelper
> helper(
101 BrowsingDataChannelIDHelper::Create(
102 testing_profile_
->GetRequestContext()));
104 helper
->DeleteChannelID("https://www.google.com:443");
106 helper
->StartFetching(
107 base::Bind(&BrowsingDataChannelIDHelperTest::FetchCallback
,
108 base::Unretained(this)));
109 base::RunLoop().RunUntilIdle();
111 EXPECT_EQ(1, ssl_config_changed_count_
);
112 ASSERT_EQ(1UL, channel_id_list_
.size());
113 net::ChannelIDStore::ChannelIDList::const_iterator it
=
114 channel_id_list_
.begin();
116 ASSERT_TRUE(it
!= channel_id_list_
.end());
117 EXPECT_EQ("https://www.youtube.com:443", it
->server_identifier());
119 ASSERT_TRUE(++it
== channel_id_list_
.end());
121 helper
->DeleteChannelID("https://www.youtube.com:443");
123 helper
->StartFetching(
124 base::Bind(&BrowsingDataChannelIDHelperTest::FetchCallback
,
125 base::Unretained(this)));
126 base::RunLoop().RunUntilIdle();
128 EXPECT_EQ(2, ssl_config_changed_count_
);
129 ASSERT_EQ(0UL, channel_id_list_
.size());
132 TEST_F(BrowsingDataChannelIDHelperTest
, CannedEmpty
) {
133 std::string origin
= "https://www.google.com";
135 scoped_refptr
<CannedBrowsingDataChannelIDHelper
> helper(
136 new CannedBrowsingDataChannelIDHelper());
138 ASSERT_TRUE(helper
->empty());
139 helper
->AddChannelID(net::ChannelIDStore::ChannelID(
140 origin
, base::Time(), base::Time(), "key", "cert"));
141 ASSERT_FALSE(helper
->empty());
143 ASSERT_TRUE(helper
->empty());