Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / activity_log / hashed_ad_network_database_unittest.cc
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.
4
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"
17 #include "url/gurl.h"
18
19 namespace extensions {
20
21 namespace {
22
23 // A list of fake ad networks.
24 const char* kAdNetworkHosts[] = {
25   "alpha.adnetwork",
26   "bravo.adnetwork",
27   "charlie.delta.adnetwork"
28 };
29
30 // The number of ad networks for these tests.
31 const size_t kNumAdNetworkHosts = arraysize(kAdNetworkHosts);
32
33 // Each hash of an ad network is stored in an int64.
34 const size_t kAdNetworkHostHashSize = sizeof(int64);
35
36 // The total size for storing all ad network host hashes.
37 const size_t kAdNetworkHostHashesTotalSize =
38     kAdNetworkHostHashSize * kNumAdNetworkHosts;
39
40 // The size of the checksum we use in the data resource.
41 const size_t kChecksumSize = 32u;
42
43 // The total size of the data resource, including the checksum and all host
44 // hashes.
45 const size_t kDataResourceSize = kChecksumSize + kAdNetworkHostHashesTotalSize;
46
47 }  // namespace
48
49 class HashedAdNetworkDatabaseUnitTest : public testing::Test {
50  protected:
51   virtual void SetUp() OVERRIDE;
52
53  private:
54   // Generate a piece of memory with a hash structure identical to the real one,
55   // but with only mock data.
56   void GenerateMockMemory();
57
58   // The raw bits of the mocked-up data resource.
59   char raw_data_[kDataResourceSize];
60
61   // The RefCountedStaticMemory wrapper around |raw_data_|.
62   scoped_refptr<base::RefCountedStaticMemory> memory_;
63 };
64
65 void HashedAdNetworkDatabaseUnitTest::SetUp() {
66   GenerateMockMemory();
67   AdNetworkDatabase::SetForTesting(
68       scoped_ptr<AdNetworkDatabase>(new HashedAdNetworkDatabase(memory_)));
69 }
70
71 void HashedAdNetworkDatabaseUnitTest::GenerateMockMemory() {
72   int64 host_hashes[kNumAdNetworkHosts];
73
74   for (size_t i = 0; i < kNumAdNetworkHosts; ++i) {
75     int64 hash = 0;
76     crypto::SHA256HashString(kAdNetworkHosts[i], &hash, sizeof(hash));
77     host_hashes[i] = hash;
78   }
79
80   // Create the Checksum.
81   scoped_ptr<crypto::SecureHash> hash(
82       crypto::SecureHash::Create(crypto::SecureHash::SHA256));
83   hash->Update(host_hashes, kNumAdNetworkHosts * kAdNetworkHostHashSize);
84
85   char checksum[kChecksumSize];
86   hash->Finish(checksum, kChecksumSize);
87
88   // Copy the checksum to our data.
89   memcpy(raw_data_, &checksum, kChecksumSize);
90
91   // Copy the hashes.
92   memcpy(raw_data_ + kChecksumSize, host_hashes, kAdNetworkHostHashesTotalSize);
93
94   memory_ = new base::RefCountedStaticMemory(raw_data_, kDataResourceSize);
95 };
96
97 TEST_F(HashedAdNetworkDatabaseUnitTest, HashedAdNetworkDatabaseTest) {
98   const AdNetworkDatabase* database = AdNetworkDatabase::Get();
99   ASSERT_TRUE(database);
100
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")));
105
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")));
109   EXPECT_TRUE(
110       database->IsAdNetwork(GURL("http://charlie.delta.adnetwork/foo.html")));
111
112   // Then, try subdomains. These should not register, as they are treated as
113   // different hosts.
114   EXPECT_FALSE(database->IsAdNetwork(GURL("http://foo.alpha.adnetwork")));
115   EXPECT_FALSE(database->IsAdNetwork(GURL("http://foo.bar.bravo.adnetwork")));
116   EXPECT_FALSE(
117       database->IsAdNetwork(GURL("http://foo.charlie.delta.adnetwork")));
118
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")));
122
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")));
127   EXPECT_FALSE(
128       database->IsAdNetwork(GURL("file:///usr/someone/files/file.html")));
129   EXPECT_FALSE(database->IsAdNetwork(
130       GURL("chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")));
131 }
132
133 }  // namespace extensions