Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(310)

Side by Side Diff: chrome/browser/extensions/activity_log/ad_network_database_unittest.cc

Issue 242303002: Add implementation for Ad Networks Database (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
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"
18 #include "url/gurl.h"
19
20 namespace extensions {
21
22 namespace {
23
24 // A list of fake ad networks.
25 const char* kAdNetworkHosts[] = {
26 "alpha.adnetwork",
27 "bravo.adnetwork",
28 "charlie.delta.adnetwork"
29 };
30
31 // The number of ad networks for these tests.
32 const size_t kNumAdNetworkHosts = arraysize(kAdNetworkHosts);
33
34 // Each hash of an ad network is stored in an int64.
35 const size_t kAdNetworkHostHashSize = sizeof(int64);
36
37 // The total size for storing all ad network host hashes.
38 const size_t kAdNetworkHostHashesTotalSize =
39 kAdNetworkHostHashSize * kNumAdNetworkHosts;
40
41 // The size of the checksum we use in the data resource.
42 const size_t kChecksumSize = 32u;
43
44 // The total size of the data resource, including the checksum and all host
45 // hashes.
46 const size_t kDataResourceSize = kChecksumSize + kAdNetworkHostHashesTotalSize;
47
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
50 // |kAdNetworkHosts|.
51 class MockResourceDelegate : public ui::ResourceBundle::Delegate {
52 public:
53 MockResourceDelegate();
54 virtual ~MockResourceDelegate();
55
56 private:
57 // Populate |data_resource_| with fake data for ad network url hashes.
58 void MakeMockResourceData();
59
60 // ResourceBundle::Delegate implementation. We actually only care about
61 // LoadDataResourceBytes(), but they're all pure virtual, so no choice but
62 // to have them all.
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)
77 OVERRIDE;
78 virtual scoped_ptr<gfx::Font> GetFont(ResourceBundle::FontStyle style)
79 OVERRIDE;
80
81 // The raw bits of the mocked-up data resource.
82 char raw_data_[kDataResourceSize];
83
84 // The RefCountedStaticMemory wrapper around |raw_data_|.
85 scoped_refptr<base::RefCountedStaticMemory> data_resource_;
86 };
87
88 MockResourceDelegate::MockResourceDelegate() {
89 MakeMockResourceData();
90 }
91
92 MockResourceDelegate::~MockResourceDelegate() {}
93
94 void MockResourceDelegate::MakeMockResourceData() {
95 int64 host_hashes[kNumAdNetworkHosts];
96
97 for (size_t i = 0; i < kNumAdNetworkHosts; ++i) {
98 int64 hash = 0;
99 crypto::SHA256HashString(kAdNetworkHosts[i], &hash, sizeof(hash));
100 host_hashes[i] = hash;
101 }
102
103 // Create the Checksum.
104 scoped_ptr<crypto::SecureHash> hash(
105 crypto::SecureHash::Create(crypto::SecureHash::SHA256));
106 hash->Update(host_hashes, kNumAdNetworkHosts * kAdNetworkHostHashSize);
107
108 char checksum[kChecksumSize];
109 hash->Finish(checksum, kChecksumSize);
110
111 // Copy the checksum to our data.
112 memcpy(raw_data_, &checksum, kChecksumSize);
113
114 // Copy the hashes.
115 memcpy(raw_data_ + kChecksumSize, host_hashes, kAdNetworkHostHashesTotalSize);
116
117 data_resource_ =
118 new base::RefCountedStaticMemory(raw_data_, kDataResourceSize);
119 };
120
121 base::FilePath MockResourceDelegate::GetPathForResourcePack(
122 const base::FilePath& pack_path, ui::ScaleFactor scale_factor) {
123 return base::FilePath();
124 }
125
126 base::FilePath MockResourceDelegate::GetPathForLocalePack(
127 const base::FilePath& pack_path,
128 const std::string& locale) {
129 return base::FilePath();
130 }
131
132 gfx::Image MockResourceDelegate::GetImageNamed(int resource_id) {
133 return gfx::Image();
134 }
135
136 gfx::Image MockResourceDelegate::GetNativeImageNamed(
137 int resource_id, ui::ResourceBundle::ImageRTL rtl) {
138 return gfx::Image();
139 }
140
141 base::RefCountedStaticMemory* MockResourceDelegate::LoadDataResourceBytes(
142 int resource_id, ui::ScaleFactor scale_factor) {
143 if (resource_id != IDR_AD_NETWORK_HASHES)
144 return NULL;
145 return data_resource_;
146 }
147
148 bool MockResourceDelegate::GetRawDataResource(int resource_id,
149 ui::ScaleFactor scale_factor,
150 base::StringPiece* value) {
151 return false;
152 }
153
154 bool MockResourceDelegate::GetLocalizedString(int message_id,
155 base::string16* value) {
156 return false;
157 }
158
159 scoped_ptr<gfx::Font> MockResourceDelegate::GetFont(
160 ResourceBundle::FontStyle style) {
161 return scoped_ptr<gfx::Font>();
162 }
163
164 } // namespace
165
166 class AdNetworkDatabaseUnitTest : public testing::Test {
167 protected:
168 virtual void SetUp() OVERRIDE;
169
170 private:
171 scoped_ptr<MockResourceDelegate> mock_resource_delegate_;
172 };
173
174 void AdNetworkDatabaseUnitTest::SetUp() {
175 // Clear the current resource bundle, and replace it with one with our own
176 // Delegate.
177 mock_resource_delegate_.reset(new MockResourceDelegate);
178 ui::ResourceBundle::CleanupSharedInstance();
179 ui::ResourceBundle::InitSharedInstanceWithLocale(
180 "en-US", mock_resource_delegate_.get());
181 }
182
183 TEST_F(AdNetworkDatabaseUnitTest, AdNetworkDatabaseTest) {
184 const AdNetworkDatabase* database = AdNetworkDatabase::Get();
185 ASSERT_TRUE(database);
186
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")));
191
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")));
195 EXPECT_TRUE(
196 database->IsAdNetwork(GURL("http://charlie.delta.adnetwork/foo.html")));
197
198 // Then, try subdomains. These should not register, as they are treated as
199 // different hosts.
200 EXPECT_FALSE(database->IsAdNetwork(GURL("http://foo.alpha.adnetwork")));
201 EXPECT_FALSE(database->IsAdNetwork(GURL("http://foo.bar.bravo.adnetwork")));
202 EXPECT_FALSE(
203 database->IsAdNetwork(GURL("http://foo.charlie.delta.adnetwork")));
204
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")));
208
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")));
213 EXPECT_FALSE(
214 database->IsAdNetwork(GURL("file:///usr/someone/files/file.html")));
215 EXPECT_FALSE(database->IsAdNetwork(
216 GURL("chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")));
217 }
218
219 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698