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 "base/basictypes.h"
6 #include "base/files/file_path.h"
7 #include "base/logging.h"
8 #include "base/macros.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/ref_counted_memory.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "chrome/browser/extensions/activity_log/ad_network_database.h"
13 #include "crypto/secure_hash.h"
14 #include "crypto/sha2.h"
15 #include "grit/browser_resources.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "ui/base/resource/resource_bundle.h"
20 namespace extensions
{
24 // A list of fake ad networks.
25 const char* kAdNetworkHosts
[] = {
28 "charlie.delta.adnetwork"
31 // The number of ad networks for these tests.
32 const size_t kNumAdNetworkHosts
= arraysize(kAdNetworkHosts
);
34 // Each hash of an ad network is stored in an int64.
35 const size_t kAdNetworkHostHashSize
= sizeof(int64
);
37 // The total size for storing all ad network host hashes.
38 const size_t kAdNetworkHostHashesTotalSize
=
39 kAdNetworkHostHashSize
* kNumAdNetworkHosts
;
41 // The size of the checksum we use in the data resource.
42 const size_t kChecksumSize
= 32u;
44 // The total size of the data resource, including the checksum and all host
46 const size_t kDataResourceSize
= kChecksumSize
+ kAdNetworkHostHashesTotalSize
;
48 // The MockResourceDelegate handles the call to get the data for the ad networks
49 // file, which is constructed to contain hashes for the hosts in
51 class MockResourceDelegate
: public ui::ResourceBundle::Delegate
{
53 MockResourceDelegate();
54 virtual ~MockResourceDelegate();
57 // Populate |data_resource_| with fake data for ad network url hashes.
58 void MakeMockResourceData();
60 // ResourceBundle::Delegate implementation. We actually only care about
61 // LoadDataResourceBytes(), but they're all pure virtual, so no choice but
63 virtual base::FilePath
GetPathForResourcePack(
64 const base::FilePath
& pack_path
, ui::ScaleFactor scale_factor
) OVERRIDE
;
65 virtual base::FilePath
GetPathForLocalePack(
66 const base::FilePath
& pack_path
,
67 const std::string
& locale
) OVERRIDE
;
68 virtual gfx::Image
GetImageNamed(int resource_id
) OVERRIDE
;
69 virtual gfx::Image
GetNativeImageNamed(
70 int resource_id
, ui::ResourceBundle::ImageRTL rtl
) OVERRIDE
;
71 virtual base::RefCountedStaticMemory
* LoadDataResourceBytes(
72 int resource_id
, ui::ScaleFactor scale_factor
) OVERRIDE
;
73 virtual bool GetRawDataResource(int resource_id
,
74 ui::ScaleFactor scale_factor
,
75 base::StringPiece
* value
) OVERRIDE
;
76 virtual bool GetLocalizedString(int message_id
, base::string16
* value
)
78 virtual scoped_ptr
<gfx::Font
> GetFont(ResourceBundle::FontStyle style
)
81 // The raw bits of the mocked-up data resource.
82 char raw_data_
[kDataResourceSize
];
84 // The RefCountedStaticMemory wrapper around |raw_data_|.
85 scoped_refptr
<base::RefCountedStaticMemory
> data_resource_
;
88 MockResourceDelegate::MockResourceDelegate() {
89 MakeMockResourceData();
92 MockResourceDelegate::~MockResourceDelegate() {}
94 void MockResourceDelegate::MakeMockResourceData() {
95 int64 host_hashes
[kNumAdNetworkHosts
];
97 for (size_t i
= 0; i
< kNumAdNetworkHosts
; ++i
) {
99 crypto::SHA256HashString(kAdNetworkHosts
[i
], &hash
, sizeof(hash
));
100 host_hashes
[i
] = hash
;
103 // Create the Checksum.
104 scoped_ptr
<crypto::SecureHash
> hash(
105 crypto::SecureHash::Create(crypto::SecureHash::SHA256
));
106 hash
->Update(host_hashes
, kNumAdNetworkHosts
* kAdNetworkHostHashSize
);
108 char checksum
[kChecksumSize
];
109 hash
->Finish(checksum
, kChecksumSize
);
111 // Copy the checksum to our data.
112 memcpy(raw_data_
, &checksum
, kChecksumSize
);
115 memcpy(raw_data_
+ kChecksumSize
, host_hashes
, kAdNetworkHostHashesTotalSize
);
118 new base::RefCountedStaticMemory(raw_data_
, kDataResourceSize
);
121 base::FilePath
MockResourceDelegate::GetPathForResourcePack(
122 const base::FilePath
& pack_path
, ui::ScaleFactor scale_factor
) {
123 return base::FilePath();
126 base::FilePath
MockResourceDelegate::GetPathForLocalePack(
127 const base::FilePath
& pack_path
,
128 const std::string
& locale
) {
129 return base::FilePath();
132 gfx::Image
MockResourceDelegate::GetImageNamed(int resource_id
) {
136 gfx::Image
MockResourceDelegate::GetNativeImageNamed(
137 int resource_id
, ui::ResourceBundle::ImageRTL rtl
) {
141 base::RefCountedStaticMemory
* MockResourceDelegate::LoadDataResourceBytes(
142 int resource_id
, ui::ScaleFactor scale_factor
) {
143 if (resource_id
!= IDR_AD_NETWORK_HASHES
)
145 return data_resource_
;
148 bool MockResourceDelegate::GetRawDataResource(int resource_id
,
149 ui::ScaleFactor scale_factor
,
150 base::StringPiece
* value
) {
154 bool MockResourceDelegate::GetLocalizedString(int message_id
,
155 base::string16
* value
) {
159 scoped_ptr
<gfx::Font
> MockResourceDelegate::GetFont(
160 ResourceBundle::FontStyle style
) {
161 return scoped_ptr
<gfx::Font
>();
166 class AdNetworkDatabaseUnitTest
: public testing::Test
{
168 virtual void SetUp() OVERRIDE
;
171 scoped_ptr
<MockResourceDelegate
> mock_resource_delegate_
;
174 void AdNetworkDatabaseUnitTest::SetUp() {
175 // Clear the current resource bundle, and replace it with one with our own
177 mock_resource_delegate_
.reset(new MockResourceDelegate
);
178 ui::ResourceBundle::CleanupSharedInstance();
179 ui::ResourceBundle::InitSharedInstanceWithLocale(
180 "en-US", mock_resource_delegate_
.get());
183 TEST_F(AdNetworkDatabaseUnitTest
, DISABLED_AdNetworkDatabaseTest
) {
184 const AdNetworkDatabase
* database
= AdNetworkDatabase::Get();
185 ASSERT_TRUE(database
);
187 // First, just check the basic urls in the list of ad networks.
188 EXPECT_TRUE(database
->IsAdNetwork(GURL("http://alpha.adnetwork")));
189 EXPECT_TRUE(database
->IsAdNetwork(GURL("http://bravo.adnetwork")));
190 EXPECT_TRUE(database
->IsAdNetwork(GURL("http://charlie.delta.adnetwork")));
192 // Next, try adding some paths. These should still register.
193 EXPECT_TRUE(database
->IsAdNetwork(GURL("http://alpha.adnetwork/foo")));
194 EXPECT_TRUE(database
->IsAdNetwork(GURL("http://bravo.adnetwork/foo/bar")));
196 database
->IsAdNetwork(GURL("http://charlie.delta.adnetwork/foo.html")));
198 // Then, try subdomains. These should not register, as they are treated as
200 EXPECT_FALSE(database
->IsAdNetwork(GURL("http://foo.alpha.adnetwork")));
201 EXPECT_FALSE(database
->IsAdNetwork(GURL("http://foo.bar.bravo.adnetwork")));
203 database
->IsAdNetwork(GURL("http://foo.charlie.delta.adnetwork")));
205 // Check to make sure that removing a subdomain (from charlie.delta.adnetwork)
206 // is considered different, and doesn't register.
207 EXPECT_FALSE(database
->IsAdNetwork(GURL("http://delta.adnetwork")));
209 // And, of course, try some random sites and make sure we don't miscategorize.
210 EXPECT_FALSE(database
->IsAdNetwork(GURL("http://www.google.com")));
211 EXPECT_FALSE(database
->IsAdNetwork(GURL("http://drive.google.com")));
212 EXPECT_FALSE(database
->IsAdNetwork(GURL("https://www.google.com")));
214 database
->IsAdNetwork(GURL("file:///usr/someone/files/file.html")));
215 EXPECT_FALSE(database
->IsAdNetwork(
216 GURL("chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")));
219 } // namespace extensions