OLD | NEW |
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" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 | 42 |
43 // 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 |
44 // hashes. | 44 // hashes. |
45 const size_t kDataResourceSize = kChecksumSize + kAdNetworkHostHashesTotalSize; | 45 const size_t kDataResourceSize = kChecksumSize + kAdNetworkHostHashesTotalSize; |
46 | 46 |
47 } // namespace | 47 } // namespace |
48 | 48 |
49 class HashedAdNetworkDatabaseUnitTest : public testing::Test { | 49 class HashedAdNetworkDatabaseUnitTest : public testing::Test { |
50 protected: | 50 protected: |
51 virtual void SetUp() OVERRIDE; | 51 virtual void SetUp() OVERRIDE; |
| 52 virtual void TearDown() OVERRIDE; |
52 | 53 |
53 private: | 54 private: |
54 // Generate a piece of memory with a hash structure identical to the real one, | 55 // Generate a piece of memory with a hash structure identical to the real one, |
55 // but with only mock data. | 56 // but with only mock data. |
56 void GenerateMockMemory(); | 57 void GenerateMockMemory(); |
57 | 58 |
58 // The raw bits of the mocked-up data resource. | 59 // The raw bits of the mocked-up data resource. |
59 char raw_data_[kDataResourceSize]; | 60 char raw_data_[kDataResourceSize]; |
60 | 61 |
61 // The RefCountedStaticMemory wrapper around |raw_data_|. | 62 // The RefCountedStaticMemory wrapper around |raw_data_|. |
62 scoped_refptr<base::RefCountedStaticMemory> memory_; | 63 scoped_refptr<base::RefCountedStaticMemory> memory_; |
63 }; | 64 }; |
64 | 65 |
65 void HashedAdNetworkDatabaseUnitTest::SetUp() { | 66 void HashedAdNetworkDatabaseUnitTest::SetUp() { |
66 GenerateMockMemory(); | 67 GenerateMockMemory(); |
67 AdNetworkDatabase::SetForTesting( | 68 AdNetworkDatabase::SetForTesting( |
68 scoped_ptr<AdNetworkDatabase>(new HashedAdNetworkDatabase(memory_))); | 69 scoped_ptr<AdNetworkDatabase>(new HashedAdNetworkDatabase(memory_))); |
69 } | 70 } |
70 | 71 |
| 72 void HashedAdNetworkDatabaseUnitTest::TearDown() { |
| 73 // Reset the database. |
| 74 AdNetworkDatabase::SetForTesting(scoped_ptr<AdNetworkDatabase>()); |
| 75 } |
| 76 |
71 void HashedAdNetworkDatabaseUnitTest::GenerateMockMemory() { | 77 void HashedAdNetworkDatabaseUnitTest::GenerateMockMemory() { |
72 int64 host_hashes[kNumAdNetworkHosts]; | 78 int64 host_hashes[kNumAdNetworkHosts]; |
73 | 79 |
74 for (size_t i = 0; i < kNumAdNetworkHosts; ++i) { | 80 for (size_t i = 0; i < kNumAdNetworkHosts; ++i) { |
75 int64 hash = 0; | 81 int64 hash = 0; |
76 crypto::SHA256HashString(kAdNetworkHosts[i], &hash, sizeof(hash)); | 82 crypto::SHA256HashString(kAdNetworkHosts[i], &hash, sizeof(hash)); |
77 host_hashes[i] = hash; | 83 host_hashes[i] = hash; |
78 } | 84 } |
79 | 85 |
80 // Create the Checksum. | 86 // Create the Checksum. |
81 scoped_ptr<crypto::SecureHash> hash( | 87 scoped_ptr<crypto::SecureHash> hash( |
82 crypto::SecureHash::Create(crypto::SecureHash::SHA256)); | 88 crypto::SecureHash::Create(crypto::SecureHash::SHA256)); |
83 hash->Update(host_hashes, kNumAdNetworkHosts * kAdNetworkHostHashSize); | 89 hash->Update(host_hashes, kNumAdNetworkHosts * kAdNetworkHostHashSize); |
84 | 90 |
85 char checksum[kChecksumSize]; | 91 char checksum[kChecksumSize]; |
86 hash->Finish(checksum, kChecksumSize); | 92 hash->Finish(checksum, kChecksumSize); |
87 | 93 |
88 // Copy the checksum to our data. | 94 // Copy the checksum to our data. |
89 memcpy(raw_data_, &checksum, kChecksumSize); | 95 memcpy(raw_data_, &checksum, kChecksumSize); |
90 | 96 |
91 // Copy the hashes. | 97 // Copy the hashes. |
92 memcpy(raw_data_ + kChecksumSize, host_hashes, kAdNetworkHostHashesTotalSize); | 98 memcpy(raw_data_ + kChecksumSize, host_hashes, kAdNetworkHostHashesTotalSize); |
93 | 99 |
94 memory_ = new base::RefCountedStaticMemory(raw_data_, kDataResourceSize); | 100 memory_ = new base::RefCountedStaticMemory(raw_data_, kDataResourceSize); |
95 }; | 101 }; |
96 | 102 |
| 103 // Test that the logic for the Ad Network Database works. That is, the hashing |
| 104 // scheme works, correctly reports when URLs are present in the database, |
| 105 // treats hosts and sumdomains correctly, etc. |
97 TEST_F(HashedAdNetworkDatabaseUnitTest, HashedAdNetworkDatabaseTest) { | 106 TEST_F(HashedAdNetworkDatabaseUnitTest, HashedAdNetworkDatabaseTest) { |
98 const AdNetworkDatabase* database = AdNetworkDatabase::Get(); | 107 const AdNetworkDatabase* database = AdNetworkDatabase::Get(); |
99 ASSERT_TRUE(database); | 108 ASSERT_TRUE(database); |
100 | 109 |
101 // First, just check the basic urls in the list of ad networks. | 110 // First, just check the basic urls in the list of ad networks. |
102 EXPECT_TRUE(database->IsAdNetwork(GURL("http://alpha.adnetwork"))); | 111 EXPECT_TRUE(database->IsAdNetwork(GURL("http://alpha.adnetwork"))); |
103 EXPECT_TRUE(database->IsAdNetwork(GURL("http://bravo.adnetwork"))); | 112 EXPECT_TRUE(database->IsAdNetwork(GURL("http://bravo.adnetwork"))); |
104 EXPECT_TRUE(database->IsAdNetwork(GURL("http://charlie.delta.adnetwork"))); | 113 EXPECT_TRUE(database->IsAdNetwork(GURL("http://charlie.delta.adnetwork"))); |
105 | 114 |
106 // Next, try adding some paths. These should still register. | 115 // Next, try adding some paths. These should still register. |
(...skipping 16 matching lines...) Expand all Loading... |
123 // And, of course, try some random sites and make sure we don't miscategorize. | 132 // And, of course, try some random sites and make sure we don't miscategorize. |
124 EXPECT_FALSE(database->IsAdNetwork(GURL("http://www.google.com"))); | 133 EXPECT_FALSE(database->IsAdNetwork(GURL("http://www.google.com"))); |
125 EXPECT_FALSE(database->IsAdNetwork(GURL("http://drive.google.com"))); | 134 EXPECT_FALSE(database->IsAdNetwork(GURL("http://drive.google.com"))); |
126 EXPECT_FALSE(database->IsAdNetwork(GURL("https://www.google.com"))); | 135 EXPECT_FALSE(database->IsAdNetwork(GURL("https://www.google.com"))); |
127 EXPECT_FALSE( | 136 EXPECT_FALSE( |
128 database->IsAdNetwork(GURL("file:///usr/someone/files/file.html"))); | 137 database->IsAdNetwork(GURL("file:///usr/someone/files/file.html"))); |
129 EXPECT_FALSE(database->IsAdNetwork( | 138 EXPECT_FALSE(database->IsAdNetwork( |
130 GURL("chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))); | 139 GURL("chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))); |
131 } | 140 } |
132 | 141 |
| 142 // Test that the HashAdNetworkDatabse test works with the real file. For |
| 143 // security and privacy purposes, we cannot verify that real URLs are |
| 144 // recognized. However, we can at least verify that the file is recognized and |
| 145 // parsed. |
| 146 TEST(HashedAdNetworkDatabaseWithRealFileUnitTest, |
| 147 HashedAdNetworkDatabaseRealFileTest) { |
| 148 // This constructs the database, and, since we didn't mock up any memory, it |
| 149 // uses the real file. |
| 150 const AdNetworkDatabase* database = AdNetworkDatabase::Get(); |
| 151 ASSERT_TRUE(database); |
| 152 const HashedAdNetworkDatabase* hashed_database = |
| 153 static_cast<const HashedAdNetworkDatabase*>(database); |
| 154 EXPECT_TRUE(hashed_database->is_valid()); |
| 155 |
| 156 // We can also safely assume that a made-up url is not in the database. |
| 157 EXPECT_FALSE(database->IsAdNetwork( |
| 158 GURL("http://www.this-is-definitely-not-a-real-site.orarealtld"))); |
| 159 } |
| 160 |
133 } // namespace extensions | 161 } // namespace extensions |
OLD | NEW |