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 "chrome/browser/extensions/activity_log/hashed_ad_network_database.h"
14 #include "crypto/secure_hash.h"
15 #include "crypto/sha2.h"
16 #include "testing/gtest/include/gtest/gtest.h"
19 namespace extensions {
23 // A list of fake ad networks.
24 const char* kAdNetworkHosts[] = {
27 "charlie.delta.adnetwork"
30 // The number of ad networks for these tests.
31 const size_t kNumAdNetworkHosts = arraysize(kAdNetworkHosts);
33 // Each hash of an ad network is stored in an int64.
34 const size_t kAdNetworkHostHashSize = sizeof(int64);
36 // The total size for storing all ad network host hashes.
37 const size_t kAdNetworkHostHashesTotalSize =
38 kAdNetworkHostHashSize * kNumAdNetworkHosts;
40 // The size of the checksum we use in the data resource.
41 const size_t kChecksumSize = 32u;
43 // The total size of the data resource, including the checksum and all host
45 const size_t kDataResourceSize = kChecksumSize + kAdNetworkHostHashesTotalSize;
49 class HashedAdNetworkDatabaseUnitTest : public testing::Test {
51 virtual void SetUp() OVERRIDE;
54 // Generate a piece of memory with a hash structure identical to the real one,
55 // but with only mock data.
56 void GenerateMockMemory();
58 // The raw bits of the mocked-up data resource.
59 char raw_data_[kDataResourceSize];
61 // The RefCountedStaticMemory wrapper around |raw_data_|.
62 scoped_refptr<base::RefCountedStaticMemory> memory_;
65 void HashedAdNetworkDatabaseUnitTest::SetUp() {
67 AdNetworkDatabase::SetForTesting(
68 scoped_ptr<AdNetworkDatabase>(new HashedAdNetworkDatabase(memory_)));
71 void HashedAdNetworkDatabaseUnitTest::GenerateMockMemory() {
72 int64 host_hashes[kNumAdNetworkHosts];
74 for (size_t i = 0; i < kNumAdNetworkHosts; ++i) {
76 crypto::SHA256HashString(kAdNetworkHosts[i], &hash, sizeof(hash));
77 host_hashes[i] = hash;
80 // Create the Checksum.
81 scoped_ptr<crypto::SecureHash> hash(
82 crypto::SecureHash::Create(crypto::SecureHash::SHA256));
83 hash->Update(host_hashes, kNumAdNetworkHosts * kAdNetworkHostHashSize);
85 char checksum[kChecksumSize];
86 hash->Finish(checksum, kChecksumSize);
88 // Copy the checksum to our data.
89 memcpy(raw_data_, &checksum, kChecksumSize);
92 memcpy(raw_data_ + kChecksumSize, host_hashes, kAdNetworkHostHashesTotalSize);
94 memory_ = new base::RefCountedStaticMemory(raw_data_, kDataResourceSize);
97 TEST_F(HashedAdNetworkDatabaseUnitTest, HashedAdNetworkDatabaseTest) {
98 const AdNetworkDatabase* database = AdNetworkDatabase::Get();
99 ASSERT_TRUE(database);
101 // First, just check the basic urls in the list of ad networks.
102 EXPECT_TRUE(database->IsAdNetwork(GURL("http://alpha.adnetwork")));
103 EXPECT_TRUE(database->IsAdNetwork(GURL("http://bravo.adnetwork")));
104 EXPECT_TRUE(database->IsAdNetwork(GURL("http://charlie.delta.adnetwork")));
106 // Next, try adding some paths. These should still register.
107 EXPECT_TRUE(database->IsAdNetwork(GURL("http://alpha.adnetwork/foo")));
108 EXPECT_TRUE(database->IsAdNetwork(GURL("http://bravo.adnetwork/foo/bar")));
110 database->IsAdNetwork(GURL("http://charlie.delta.adnetwork/foo.html")));
112 // Then, try subdomains. These should not register, as they are treated as
114 EXPECT_FALSE(database->IsAdNetwork(GURL("http://foo.alpha.adnetwork")));
115 EXPECT_FALSE(database->IsAdNetwork(GURL("http://foo.bar.bravo.adnetwork")));
117 database->IsAdNetwork(GURL("http://foo.charlie.delta.adnetwork")));
119 // Check to make sure that removing a subdomain (from charlie.delta.adnetwork)
120 // is considered different, and doesn't register.
121 EXPECT_FALSE(database->IsAdNetwork(GURL("http://delta.adnetwork")));
123 // And, of course, try some random sites and make sure we don't miscategorize.
124 EXPECT_FALSE(database->IsAdNetwork(GURL("http://www.google.com")));
125 EXPECT_FALSE(database->IsAdNetwork(GURL("http://drive.google.com")));
126 EXPECT_FALSE(database->IsAdNetwork(GURL("https://www.google.com")));
128 database->IsAdNetwork(GURL("file:///usr/someone/files/file.html")));
129 EXPECT_FALSE(database->IsAdNetwork(
130 GURL("chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")));
133 } // namespace extensions