| 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; | |
| 53 | 52 |
| 54 private: | 53 private: |
| 55 // Generate a piece of memory with a hash structure identical to the real one, | 54 // Generate a piece of memory with a hash structure identical to the real one, |
| 56 // but with only mock data. | 55 // but with only mock data. |
| 57 void GenerateMockMemory(); | 56 void GenerateMockMemory(); |
| 58 | 57 |
| 59 // The raw bits of the mocked-up data resource. | 58 // The raw bits of the mocked-up data resource. |
| 60 char raw_data_[kDataResourceSize]; | 59 char raw_data_[kDataResourceSize]; |
| 61 | 60 |
| 62 // The RefCountedStaticMemory wrapper around |raw_data_|. | 61 // The RefCountedStaticMemory wrapper around |raw_data_|. |
| 63 scoped_refptr<base::RefCountedStaticMemory> memory_; | 62 scoped_refptr<base::RefCountedStaticMemory> memory_; |
| 64 }; | 63 }; |
| 65 | 64 |
| 66 void HashedAdNetworkDatabaseUnitTest::SetUp() { | 65 void HashedAdNetworkDatabaseUnitTest::SetUp() { |
| 67 GenerateMockMemory(); | 66 GenerateMockMemory(); |
| 68 AdNetworkDatabase::SetForTesting( | 67 AdNetworkDatabase::SetForTesting( |
| 69 scoped_ptr<AdNetworkDatabase>(new HashedAdNetworkDatabase(memory_))); | 68 scoped_ptr<AdNetworkDatabase>(new HashedAdNetworkDatabase(memory_))); |
| 70 } | 69 } |
| 71 | 70 |
| 72 void HashedAdNetworkDatabaseUnitTest::TearDown() { | |
| 73 // Reset the database. | |
| 74 AdNetworkDatabase::SetForTesting(scoped_ptr<AdNetworkDatabase>()); | |
| 75 } | |
| 76 | |
| 77 void HashedAdNetworkDatabaseUnitTest::GenerateMockMemory() { | 71 void HashedAdNetworkDatabaseUnitTest::GenerateMockMemory() { |
| 78 int64 host_hashes[kNumAdNetworkHosts]; | 72 int64 host_hashes[kNumAdNetworkHosts]; |
| 79 | 73 |
| 80 for (size_t i = 0; i < kNumAdNetworkHosts; ++i) { | 74 for (size_t i = 0; i < kNumAdNetworkHosts; ++i) { |
| 81 int64 hash = 0; | 75 int64 hash = 0; |
| 82 crypto::SHA256HashString(kAdNetworkHosts[i], &hash, sizeof(hash)); | 76 crypto::SHA256HashString(kAdNetworkHosts[i], &hash, sizeof(hash)); |
| 83 host_hashes[i] = hash; | 77 host_hashes[i] = hash; |
| 84 } | 78 } |
| 85 | 79 |
| 86 // Create the Checksum. | 80 // Create the Checksum. |
| 87 scoped_ptr<crypto::SecureHash> hash( | 81 scoped_ptr<crypto::SecureHash> hash( |
| 88 crypto::SecureHash::Create(crypto::SecureHash::SHA256)); | 82 crypto::SecureHash::Create(crypto::SecureHash::SHA256)); |
| 89 hash->Update(host_hashes, kNumAdNetworkHosts * kAdNetworkHostHashSize); | 83 hash->Update(host_hashes, kNumAdNetworkHosts * kAdNetworkHostHashSize); |
| 90 | 84 |
| 91 char checksum[kChecksumSize]; | 85 char checksum[kChecksumSize]; |
| 92 hash->Finish(checksum, kChecksumSize); | 86 hash->Finish(checksum, kChecksumSize); |
| 93 | 87 |
| 94 // Copy the checksum to our data. | 88 // Copy the checksum to our data. |
| 95 memcpy(raw_data_, &checksum, kChecksumSize); | 89 memcpy(raw_data_, &checksum, kChecksumSize); |
| 96 | 90 |
| 97 // Copy the hashes. | 91 // Copy the hashes. |
| 98 memcpy(raw_data_ + kChecksumSize, host_hashes, kAdNetworkHostHashesTotalSize); | 92 memcpy(raw_data_ + kChecksumSize, host_hashes, kAdNetworkHostHashesTotalSize); |
| 99 | 93 |
| 100 memory_ = new base::RefCountedStaticMemory(raw_data_, kDataResourceSize); | 94 memory_ = new base::RefCountedStaticMemory(raw_data_, kDataResourceSize); |
| 101 }; | 95 }; |
| 102 | 96 |
| 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. | |
| 106 TEST_F(HashedAdNetworkDatabaseUnitTest, HashedAdNetworkDatabaseTest) { | 97 TEST_F(HashedAdNetworkDatabaseUnitTest, HashedAdNetworkDatabaseTest) { |
| 107 const AdNetworkDatabase* database = AdNetworkDatabase::Get(); | 98 const AdNetworkDatabase* database = AdNetworkDatabase::Get(); |
| 108 ASSERT_TRUE(database); | 99 ASSERT_TRUE(database); |
| 109 | 100 |
| 110 // 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. |
| 111 EXPECT_TRUE(database->IsAdNetwork(GURL("http://alpha.adnetwork"))); | 102 EXPECT_TRUE(database->IsAdNetwork(GURL("http://alpha.adnetwork"))); |
| 112 EXPECT_TRUE(database->IsAdNetwork(GURL("http://bravo.adnetwork"))); | 103 EXPECT_TRUE(database->IsAdNetwork(GURL("http://bravo.adnetwork"))); |
| 113 EXPECT_TRUE(database->IsAdNetwork(GURL("http://charlie.delta.adnetwork"))); | 104 EXPECT_TRUE(database->IsAdNetwork(GURL("http://charlie.delta.adnetwork"))); |
| 114 | 105 |
| 115 // Next, try adding some paths. These should still register. | 106 // Next, try adding some paths. These should still register. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 132 // And, of course, try some random sites and make sure we don't miscategorize. | 123 // And, of course, try some random sites and make sure we don't miscategorize. |
| 133 EXPECT_FALSE(database->IsAdNetwork(GURL("http://www.google.com"))); | 124 EXPECT_FALSE(database->IsAdNetwork(GURL("http://www.google.com"))); |
| 134 EXPECT_FALSE(database->IsAdNetwork(GURL("http://drive.google.com"))); | 125 EXPECT_FALSE(database->IsAdNetwork(GURL("http://drive.google.com"))); |
| 135 EXPECT_FALSE(database->IsAdNetwork(GURL("https://www.google.com"))); | 126 EXPECT_FALSE(database->IsAdNetwork(GURL("https://www.google.com"))); |
| 136 EXPECT_FALSE( | 127 EXPECT_FALSE( |
| 137 database->IsAdNetwork(GURL("file:///usr/someone/files/file.html"))); | 128 database->IsAdNetwork(GURL("file:///usr/someone/files/file.html"))); |
| 138 EXPECT_FALSE(database->IsAdNetwork( | 129 EXPECT_FALSE(database->IsAdNetwork( |
| 139 GURL("chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))); | 130 GURL("chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))); |
| 140 } | 131 } |
| 141 | 132 |
| 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 | |
| 161 } // namespace extensions | 133 } // namespace extensions |
| OLD | NEW |