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

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

Issue 263953003: Re-enable ActivityLog AdNetworkDatabase unittest (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 7 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/files/file_path.h" 6 #include "base/files/file_path.h"
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "base/memory/ref_counted_memory.h" 10 #include "base/memory/ref_counted_memory.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "chrome/browser/extensions/activity_log/ad_network_database.h" 12 #include "chrome/browser/extensions/activity_log/ad_network_database.h"
13 #include "chrome/browser/extensions/activity_log/hashed_ad_network_database.h"
13 #include "crypto/secure_hash.h" 14 #include "crypto/secure_hash.h"
14 #include "crypto/sha2.h" 15 #include "crypto/sha2.h"
15 #include "grit/browser_resources.h"
16 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "ui/base/resource/resource_bundle.h"
18 #include "url/gurl.h" 17 #include "url/gurl.h"
19 18
20 namespace extensions { 19 namespace extensions {
21 20
22 namespace { 21 namespace {
23 22
24 // A list of fake ad networks. 23 // A list of fake ad networks.
25 const char* kAdNetworkHosts[] = { 24 const char* kAdNetworkHosts[] = {
26 "alpha.adnetwork", 25 "alpha.adnetwork",
27 "bravo.adnetwork", 26 "bravo.adnetwork",
(...skipping 10 matching lines...) Expand all
38 const size_t kAdNetworkHostHashesTotalSize = 37 const size_t kAdNetworkHostHashesTotalSize =
39 kAdNetworkHostHashSize * kNumAdNetworkHosts; 38 kAdNetworkHostHashSize * kNumAdNetworkHosts;
40 39
41 // The size of the checksum we use in the data resource. 40 // The size of the checksum we use in the data resource.
42 const size_t kChecksumSize = 32u; 41 const size_t kChecksumSize = 32u;
43 42
44 // The total size of the data resource, including the checksum and all host 43 // The total size of the data resource, including the checksum and all host
45 // hashes. 44 // hashes.
46 const size_t kDataResourceSize = kChecksumSize + kAdNetworkHostHashesTotalSize; 45 const size_t kDataResourceSize = kChecksumSize + kAdNetworkHostHashesTotalSize;
47 46
48 // The MockResourceDelegate handles the call to get the data for the ad networks 47 } // namespace
49 // file, which is constructed to contain hashes for the hosts in 48
50 // |kAdNetworkHosts|. 49 class HashedAdNetworkDatabaseUnitTest : public testing::Test {
51 class MockResourceDelegate : public ui::ResourceBundle::Delegate { 50 protected:
52 public: 51 virtual void SetUp() OVERRIDE;
53 MockResourceDelegate();
54 virtual ~MockResourceDelegate();
55 52
56 private: 53 private:
57 // Populate |data_resource_| with fake data for ad network url hashes. 54 // Generate a piece of memory with a hash structure identical to the real one,
58 void MakeMockResourceData(); 55 // but with only mock data.
59 56 void GenerateMockMemory();
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 57
81 // The raw bits of the mocked-up data resource. 58 // The raw bits of the mocked-up data resource.
82 char raw_data_[kDataResourceSize]; 59 char raw_data_[kDataResourceSize];
83 60
84 // The RefCountedStaticMemory wrapper around |raw_data_|. 61 // The RefCountedStaticMemory wrapper around |raw_data_|.
85 scoped_refptr<base::RefCountedStaticMemory> data_resource_; 62 scoped_refptr<base::RefCountedStaticMemory> memory_;
86 }; 63 };
87 64
88 MockResourceDelegate::MockResourceDelegate() { 65 void HashedAdNetworkDatabaseUnitTest::SetUp() {
89 MakeMockResourceData(); 66 GenerateMockMemory();
67 AdNetworkDatabase::SetForTesting(
68 scoped_ptr<AdNetworkDatabase>(new HashedAdNetworkDatabase(memory_)));
90 } 69 }
91 70
92 MockResourceDelegate::~MockResourceDelegate() {} 71 void HashedAdNetworkDatabaseUnitTest::GenerateMockMemory() {
93
94 void MockResourceDelegate::MakeMockResourceData() {
95 int64 host_hashes[kNumAdNetworkHosts]; 72 int64 host_hashes[kNumAdNetworkHosts];
96 73
97 for (size_t i = 0; i < kNumAdNetworkHosts; ++i) { 74 for (size_t i = 0; i < kNumAdNetworkHosts; ++i) {
98 int64 hash = 0; 75 int64 hash = 0;
99 crypto::SHA256HashString(kAdNetworkHosts[i], &hash, sizeof(hash)); 76 crypto::SHA256HashString(kAdNetworkHosts[i], &hash, sizeof(hash));
100 host_hashes[i] = hash; 77 host_hashes[i] = hash;
101 } 78 }
102 79
103 // Create the Checksum. 80 // Create the Checksum.
104 scoped_ptr<crypto::SecureHash> hash( 81 scoped_ptr<crypto::SecureHash> hash(
105 crypto::SecureHash::Create(crypto::SecureHash::SHA256)); 82 crypto::SecureHash::Create(crypto::SecureHash::SHA256));
106 hash->Update(host_hashes, kNumAdNetworkHosts * kAdNetworkHostHashSize); 83 hash->Update(host_hashes, kNumAdNetworkHosts * kAdNetworkHostHashSize);
107 84
108 char checksum[kChecksumSize]; 85 char checksum[kChecksumSize];
109 hash->Finish(checksum, kChecksumSize); 86 hash->Finish(checksum, kChecksumSize);
110 87
111 // Copy the checksum to our data. 88 // Copy the checksum to our data.
112 memcpy(raw_data_, &checksum, kChecksumSize); 89 memcpy(raw_data_, &checksum, kChecksumSize);
113 90
114 // Copy the hashes. 91 // Copy the hashes.
115 memcpy(raw_data_ + kChecksumSize, host_hashes, kAdNetworkHostHashesTotalSize); 92 memcpy(raw_data_ + kChecksumSize, host_hashes, kAdNetworkHostHashesTotalSize);
116 93
117 data_resource_ = 94 memory_ = new base::RefCountedStaticMemory(raw_data_, kDataResourceSize);
118 new base::RefCountedStaticMemory(raw_data_, kDataResourceSize);
119 }; 95 };
120 96
121 base::FilePath MockResourceDelegate::GetPathForResourcePack( 97 TEST_F(HashedAdNetworkDatabaseUnitTest, HashedAdNetworkDatabaseTest) {
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, DISABLED_AdNetworkDatabaseTest) {
184 const AdNetworkDatabase* database = AdNetworkDatabase::Get(); 98 const AdNetworkDatabase* database = AdNetworkDatabase::Get();
185 ASSERT_TRUE(database); 99 ASSERT_TRUE(database);
186 100
187 // First, just check the basic urls in the list of ad networks. 101 // First, just check the basic urls in the list of ad networks.
188 EXPECT_TRUE(database->IsAdNetwork(GURL("http://alpha.adnetwork"))); 102 EXPECT_TRUE(database->IsAdNetwork(GURL("http://alpha.adnetwork")));
189 EXPECT_TRUE(database->IsAdNetwork(GURL("http://bravo.adnetwork"))); 103 EXPECT_TRUE(database->IsAdNetwork(GURL("http://bravo.adnetwork")));
190 EXPECT_TRUE(database->IsAdNetwork(GURL("http://charlie.delta.adnetwork"))); 104 EXPECT_TRUE(database->IsAdNetwork(GURL("http://charlie.delta.adnetwork")));
191 105
192 // Next, try adding some paths. These should still register. 106 // Next, try adding some paths. These should still register.
193 EXPECT_TRUE(database->IsAdNetwork(GURL("http://alpha.adnetwork/foo"))); 107 EXPECT_TRUE(database->IsAdNetwork(GURL("http://alpha.adnetwork/foo")));
(...skipping 16 matching lines...) Expand all
210 EXPECT_FALSE(database->IsAdNetwork(GURL("http://www.google.com"))); 124 EXPECT_FALSE(database->IsAdNetwork(GURL("http://www.google.com")));
211 EXPECT_FALSE(database->IsAdNetwork(GURL("http://drive.google.com"))); 125 EXPECT_FALSE(database->IsAdNetwork(GURL("http://drive.google.com")));
212 EXPECT_FALSE(database->IsAdNetwork(GURL("https://www.google.com"))); 126 EXPECT_FALSE(database->IsAdNetwork(GURL("https://www.google.com")));
213 EXPECT_FALSE( 127 EXPECT_FALSE(
214 database->IsAdNetwork(GURL("file:///usr/someone/files/file.html"))); 128 database->IsAdNetwork(GURL("file:///usr/someone/files/file.html")));
215 EXPECT_FALSE(database->IsAdNetwork( 129 EXPECT_FALSE(database->IsAdNetwork(
216 GURL("chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))); 130 GURL("chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")));
217 } 131 }
218 132
219 } // namespace extensions 133 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/activity_log/hashed_ad_network_database.cc ('k') | chrome/chrome_browser_extensions.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698