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() {}
27 void SetUp() override
{
28 testing_profile_
.reset(new TestingProfile());
30 testing_profile_
->GetSSLConfigService()->AddObserver(this);
33 void TearDown() override
{
34 testing_profile_
->GetSSLConfigService()->RemoveObserver(this);
37 void CreateChannelIDsForTest() {
38 net::URLRequestContext
* context
=
39 testing_profile_
->GetRequestContext()->GetURLRequestContext();
40 net::ChannelIDStore
* channel_id_store
=
41 context
->channel_id_service()->GetChannelIDStore();
42 channel_id_store
->SetChannelID(
43 make_scoped_ptr(new net::ChannelIDStore::ChannelID(
44 "https://www.google.com:443", base::Time(),
45 make_scoped_ptr(crypto::ECPrivateKey::Create()))));
46 channel_id_store
->SetChannelID(
47 make_scoped_ptr(new net::ChannelIDStore::ChannelID(
48 "https://www.youtube.com:443", base::Time(),
49 make_scoped_ptr(crypto::ECPrivateKey::Create()))));
53 const net::ChannelIDStore::ChannelIDList
& channel_ids
) {
54 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
55 channel_id_list_
= channel_ids
;
58 // net::SSLConfigService::Observer implementation:
59 void OnSSLConfigChanged() override
{ ssl_config_changed_count_
++; }
62 content::TestBrowserThreadBundle thread_bundle_
;
63 scoped_ptr
<TestingProfile
> testing_profile_
;
65 net::ChannelIDStore::ChannelIDList channel_id_list_
;
67 int ssl_config_changed_count_
= 0;
70 TEST_F(BrowsingDataChannelIDHelperTest
, FetchData
) {
71 CreateChannelIDsForTest();
72 scoped_refptr
<BrowsingDataChannelIDHelper
> helper(
73 BrowsingDataChannelIDHelper::Create(
74 testing_profile_
->GetRequestContext()));
76 helper
->StartFetching(
77 base::Bind(&BrowsingDataChannelIDHelperTest::FetchCallback
,
78 base::Unretained(this)));
80 // Blocks until BrowsingDataChannelIDHelperTest::FetchCallback is
82 base::RunLoop().RunUntilIdle();
84 ASSERT_EQ(2UL, channel_id_list_
.size());
85 net::ChannelIDStore::ChannelIDList::const_iterator it
=
86 channel_id_list_
.begin();
88 // Correct because fetching channel_id_list_ will get them out in the
89 // same order CreateChannelIDsForTest put them in.
90 ASSERT_TRUE(it
!= channel_id_list_
.end());
91 EXPECT_EQ("https://www.google.com:443", it
->server_identifier());
93 ASSERT_TRUE(++it
!= channel_id_list_
.end());
94 EXPECT_EQ("https://www.youtube.com:443", it
->server_identifier());
96 ASSERT_TRUE(++it
== channel_id_list_
.end());
98 EXPECT_EQ(0, ssl_config_changed_count_
);
101 TEST_F(BrowsingDataChannelIDHelperTest
, DeleteChannelID
) {
102 CreateChannelIDsForTest();
103 scoped_refptr
<BrowsingDataChannelIDHelper
> helper(
104 BrowsingDataChannelIDHelper::Create(
105 testing_profile_
->GetRequestContext()));
107 helper
->DeleteChannelID("https://www.google.com:443");
109 helper
->StartFetching(
110 base::Bind(&BrowsingDataChannelIDHelperTest::FetchCallback
,
111 base::Unretained(this)));
112 base::RunLoop().RunUntilIdle();
114 EXPECT_EQ(1, ssl_config_changed_count_
);
115 ASSERT_EQ(1UL, channel_id_list_
.size());
116 net::ChannelIDStore::ChannelIDList::const_iterator it
=
117 channel_id_list_
.begin();
119 ASSERT_TRUE(it
!= channel_id_list_
.end());
120 EXPECT_EQ("https://www.youtube.com:443", it
->server_identifier());
122 ASSERT_TRUE(++it
== channel_id_list_
.end());
124 helper
->DeleteChannelID("https://www.youtube.com:443");
126 helper
->StartFetching(
127 base::Bind(&BrowsingDataChannelIDHelperTest::FetchCallback
,
128 base::Unretained(this)));
129 base::RunLoop().RunUntilIdle();
131 EXPECT_EQ(2, ssl_config_changed_count_
);
132 ASSERT_EQ(0UL, channel_id_list_
.size());
135 TEST_F(BrowsingDataChannelIDHelperTest
, CannedEmpty
) {
136 std::string origin
= "https://www.google.com";
138 scoped_refptr
<CannedBrowsingDataChannelIDHelper
> helper(
139 new CannedBrowsingDataChannelIDHelper());
141 ASSERT_TRUE(helper
->empty());
142 helper
->AddChannelID(net::ChannelIDStore::ChannelID(
143 origin
, base::Time(), make_scoped_ptr(crypto::ECPrivateKey::Create())));
144 ASSERT_FALSE(helper
->empty());
146 ASSERT_TRUE(helper
->empty());