Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / extensions / activity_log / hashed_ad_network_database_unittest.cc
blob8b53cece9fd70e623ba2df733b762b22f8bda0d0
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/macros.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/stl_util.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "chrome/browser/extensions/activity_log/hashed_ad_network_database.h"
11 #include "crypto/sha2.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "url/gurl.h"
15 namespace extensions {
17 namespace {
19 // A list of fake ad networks.
20 const char* const kAdNetworkHosts[] = {
21 "alpha.adnetwork",
22 "bravo.adnetwork",
23 "charlie.delta.adnetwork"
26 // The number of ad networks for these tests.
27 const size_t kNumAdNetworkHosts = arraysize(kAdNetworkHosts);
29 } // namespace
31 class HashedAdNetworkDatabaseUnitTest : public testing::Test {
32 protected:
33 void SetUp() override;
35 AdNetworkDatabase* database() { return database_.get(); }
37 private:
38 void GenerateHashes();
40 // The fake hashes for the ad networks.
41 const char* ad_networks_[kNumAdNetworkHosts];
43 // The backing data for the ad networks. Since everything expects a const
44 // char, we need this little hack in order to generate the data.
45 std::vector<std::string> ad_networks_data_;
47 // The database used in testing.
48 scoped_ptr<HashedAdNetworkDatabase> database_;
51 void HashedAdNetworkDatabaseUnitTest::SetUp() {
52 GenerateHashes();
53 database_.reset(new HashedAdNetworkDatabase());
54 database_->set_entries_for_testing(ad_networks_, kNumAdNetworkHosts);
57 void HashedAdNetworkDatabaseUnitTest::GenerateHashes() {
58 for (size_t i = 0; i < kNumAdNetworkHosts; ++i) {
59 char hash[8u];
60 crypto::SHA256HashString(kAdNetworkHosts[i], hash, 8u);
61 ad_networks_data_.push_back(base::HexEncode(hash, 8u));
64 // HashedAdNetworkDatabase assumes the list is sorted.
65 std::sort(ad_networks_data_.begin(), ad_networks_data_.end());
66 for (size_t i = 0u; i < ad_networks_data_.size(); ++i)
67 ad_networks_[i] = ad_networks_data_[i].c_str();
70 // Test that the logic for the Ad Network Database works. That is, the hashing
71 // scheme works, correctly reports when URLs are present in the database,
72 // treats hosts and sumdomains correctly, etc.
73 TEST_F(HashedAdNetworkDatabaseUnitTest, HashedAdNetworkDatabaseTest) {
74 // First, just check the basic urls in the list of ad networks.
75 EXPECT_TRUE(database()->IsAdNetwork(GURL("http://alpha.adnetwork")));
76 EXPECT_TRUE(database()->IsAdNetwork(GURL("http://bravo.adnetwork")));
77 EXPECT_TRUE(database()->IsAdNetwork(GURL("http://charlie.delta.adnetwork")));
79 // Next, try adding some paths. These should still register.
80 EXPECT_TRUE(database()->IsAdNetwork(GURL("http://alpha.adnetwork/foo")));
81 EXPECT_TRUE(database()->IsAdNetwork(GURL("http://bravo.adnetwork/foo/bar")));
82 EXPECT_TRUE(
83 database()->IsAdNetwork(GURL("http://charlie.delta.adnetwork/foo.html")));
85 // Then, try subdomains. These should not register, as they are treated as
86 // different hosts.
87 EXPECT_FALSE(database()->IsAdNetwork(GURL("http://foo.alpha.adnetwork")));
88 EXPECT_FALSE(database()->IsAdNetwork(GURL("http://foo.bar.bravo.adnetwork")));
89 EXPECT_FALSE(
90 database()->IsAdNetwork(GURL("http://foo.charlie.delta.adnetwork")));
92 // Check to make sure that removing a subdomain (from charlie.delta.adnetwork)
93 // is considered different, and doesn't register.
94 EXPECT_FALSE(database()->IsAdNetwork(GURL("http://delta.adnetwork")));
96 // And, of course, try some random sites and make sure we don't miscategorize.
97 EXPECT_FALSE(database()->IsAdNetwork(GURL("http://www.google.com")));
98 EXPECT_FALSE(database()->IsAdNetwork(GURL("http://drive.google.com")));
99 EXPECT_FALSE(database()->IsAdNetwork(GURL("https://www.google.com")));
100 EXPECT_FALSE(
101 database()->IsAdNetwork(GURL("file:///usr/someone/files/file.html")));
102 EXPECT_FALSE(database()->IsAdNetwork(
103 GURL("chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")));
106 // Test that the HashAdNetworkDatabse test works with the real file. We have
107 // inserted a fake URL in the used dataset for testing purposes.
108 TEST(HashedAdNetworkDatabaseUnitTest2, RealFile) {
109 HashedAdNetworkDatabase database;
110 AdNetworkDatabase* db = static_cast<AdNetworkDatabase*>(&database);
111 EXPECT_TRUE(db->IsAdNetwork(
112 GURL("http://definitely.surely.always.an.adnetwork")));
113 EXPECT_FALSE(db->IsAdNetwork(GURL("http://definitely.not.one")));
116 } // namespace extensions