Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / chrome / browser / safe_browsing / local_database_manager_unittest.cc
blobde8dc964612989ca866c12f6ba2683000136a343
1 // Copyright 2013 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 <string>
6 #include <vector>
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/run_loop.h"
11 #include "chrome/browser/safe_browsing/local_database_manager.h"
12 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "content/public/test/test_utils.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "testing/platform_test.h"
17 #include "url/gurl.h"
19 using content::TestBrowserThreadBundle;
21 namespace {
23 class TestClient : public SafeBrowsingDatabaseManager::Client {
24 public:
25 TestClient() {}
26 ~TestClient() override {}
28 void OnCheckBrowseUrlResult(const GURL& url,
29 SBThreatType threat_type,
30 const std::string& metadata) override {}
32 void OnCheckDownloadUrlResult(const std::vector<GURL>& url_chain,
33 SBThreatType threat_type) override {}
35 private:
36 DISALLOW_COPY_AND_ASSIGN(TestClient);
39 } // namespace
41 class SafeBrowsingDatabaseManagerTest : public PlatformTest {
42 public:
43 bool RunSBHashTest(const safe_browsing_util::ListType list_type,
44 const std::vector<SBThreatType>& expected_threats,
45 const std::string& result_list);
47 private:
48 TestBrowserThreadBundle thread_bundle_;
51 bool SafeBrowsingDatabaseManagerTest::RunSBHashTest(
52 const safe_browsing_util::ListType list_type,
53 const std::vector<SBThreatType>& expected_threats,
54 const std::string& result_list) {
55 scoped_refptr<SafeBrowsingService> sb_service_(
56 SafeBrowsingService::CreateSafeBrowsingService());
57 scoped_refptr<LocalSafeBrowsingDatabaseManager> db_manager_(
58 new LocalSafeBrowsingDatabaseManager(sb_service_));
59 const SBFullHash same_full_hash = {};
61 LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck* check =
62 new LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck(
63 std::vector<GURL>(),
64 std::vector<SBFullHash>(1, same_full_hash),
65 NULL,
66 list_type,
67 expected_threats);
68 db_manager_->checks_.insert(check);
70 const SBFullHashResult full_hash_result = {
71 same_full_hash,
72 safe_browsing_util::GetListId(result_list)
75 std::vector<SBFullHashResult> fake_results(1, full_hash_result);
76 bool result = db_manager_->HandleOneCheck(check, fake_results);
77 db_manager_->checks_.erase(check);
78 delete check;
79 return result;
82 TEST_F(SafeBrowsingDatabaseManagerTest, CheckCorrespondsListType) {
83 std::vector<SBThreatType> malware_threat(1,
84 SB_THREAT_TYPE_BINARY_MALWARE_URL);
85 EXPECT_FALSE(RunSBHashTest(safe_browsing_util::BINURL,
86 malware_threat,
87 safe_browsing_util::kMalwareList));
88 EXPECT_TRUE(RunSBHashTest(safe_browsing_util::BINURL,
89 malware_threat,
90 safe_browsing_util::kBinUrlList));
92 // Check for multiple threats
93 std::vector<SBThreatType> multiple_threats;
94 multiple_threats.push_back(SB_THREAT_TYPE_URL_MALWARE);
95 multiple_threats.push_back(SB_THREAT_TYPE_URL_PHISHING);
96 EXPECT_FALSE(RunSBHashTest(safe_browsing_util::MALWARE,
97 multiple_threats,
98 safe_browsing_util::kBinUrlList));
99 EXPECT_TRUE(RunSBHashTest(safe_browsing_util::MALWARE,
100 multiple_threats,
101 safe_browsing_util::kMalwareList));
104 TEST_F(SafeBrowsingDatabaseManagerTest, GetUrlSeverestThreatType) {
105 std::vector<SBFullHashResult> full_hashes;
107 const GURL kMalwareUrl("http://www.malware.com/page.html");
108 const GURL kPhishingUrl("http://www.phishing.com/page.html");
109 const GURL kUnwantedUrl("http://www.unwanted.com/page.html");
110 const GURL kUnwantedAndMalwareUrl(
111 "http://www.unwantedandmalware.com/page.html");
112 const GURL kSafeUrl("http://www.safe.com/page.html");
114 const SBFullHash kMalwareHostHash = SBFullHashForString("malware.com/");
115 const SBFullHash kPhishingHostHash = SBFullHashForString("phishing.com/");
116 const SBFullHash kUnwantedHostHash = SBFullHashForString("unwanted.com/");
117 const SBFullHash kUnwantedAndMalwareHostHash =
118 SBFullHashForString("unwantedandmalware.com/");
119 const SBFullHash kSafeHostHash = SBFullHashForString("www.safe.com/");
122 SBFullHashResult full_hash;
123 full_hash.hash = kMalwareHostHash;
124 full_hash.list_id = static_cast<int>(safe_browsing_util::MALWARE);
125 full_hashes.push_back(full_hash);
129 SBFullHashResult full_hash;
130 full_hash.hash = kPhishingHostHash;
131 full_hash.list_id = static_cast<int>(safe_browsing_util::PHISH);
132 full_hashes.push_back(full_hash);
136 SBFullHashResult full_hash;
137 full_hash.hash = kUnwantedHostHash;
138 full_hash.list_id = static_cast<int>(safe_browsing_util::UNWANTEDURL);
139 full_hashes.push_back(full_hash);
143 // Add both MALWARE and UNWANTEDURL list IDs for
144 // kUnwantedAndMalwareHostHash.
145 SBFullHashResult full_hash_malware;
146 full_hash_malware.hash = kUnwantedAndMalwareHostHash;
147 full_hash_malware.list_id = static_cast<int>(safe_browsing_util::MALWARE);
148 full_hashes.push_back(full_hash_malware);
150 SBFullHashResult full_hash_unwanted;
151 full_hash_unwanted.hash = kUnwantedAndMalwareHostHash;
152 full_hash_unwanted.list_id =
153 static_cast<int>(safe_browsing_util::UNWANTEDURL);
154 full_hashes.push_back(full_hash_unwanted);
157 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE,
158 LocalSafeBrowsingDatabaseManager::GetHashSeverestThreatType(
159 kMalwareHostHash, full_hashes));
161 EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING,
162 LocalSafeBrowsingDatabaseManager::GetHashSeverestThreatType(
163 kPhishingHostHash, full_hashes));
165 EXPECT_EQ(SB_THREAT_TYPE_URL_UNWANTED,
166 LocalSafeBrowsingDatabaseManager::GetHashSeverestThreatType(
167 kUnwantedHostHash, full_hashes));
169 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE,
170 LocalSafeBrowsingDatabaseManager::GetHashSeverestThreatType(
171 kUnwantedAndMalwareHostHash, full_hashes));
173 EXPECT_EQ(SB_THREAT_TYPE_SAFE,
174 LocalSafeBrowsingDatabaseManager::GetHashSeverestThreatType(
175 kSafeHostHash, full_hashes));
177 const size_t kArbitraryValue = 123456U;
178 size_t index = kArbitraryValue;
179 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE,
180 LocalSafeBrowsingDatabaseManager::GetUrlSeverestThreatType(
181 kMalwareUrl, full_hashes, &index));
182 EXPECT_EQ(0U, index);
184 EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING,
185 LocalSafeBrowsingDatabaseManager::GetUrlSeverestThreatType(
186 kPhishingUrl, full_hashes, &index));
187 EXPECT_EQ(1U, index);
189 EXPECT_EQ(SB_THREAT_TYPE_URL_UNWANTED,
190 LocalSafeBrowsingDatabaseManager::GetUrlSeverestThreatType(
191 kUnwantedUrl, full_hashes, &index));
192 EXPECT_EQ(2U, index);
194 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE,
195 LocalSafeBrowsingDatabaseManager::GetUrlSeverestThreatType(
196 kUnwantedAndMalwareUrl, full_hashes, &index));
197 EXPECT_EQ(3U, index);
199 index = kArbitraryValue;
200 EXPECT_EQ(SB_THREAT_TYPE_SAFE,
201 LocalSafeBrowsingDatabaseManager::GetUrlSeverestThreatType(
202 kSafeUrl, full_hashes, &index));
203 EXPECT_EQ(kArbitraryValue, index);
206 TEST_F(SafeBrowsingDatabaseManagerTest, ServiceStopWithPendingChecks) {
207 scoped_refptr<SafeBrowsingService> sb_service(
208 SafeBrowsingService::CreateSafeBrowsingService());
209 scoped_refptr<LocalSafeBrowsingDatabaseManager> db_manager(
210 new LocalSafeBrowsingDatabaseManager(sb_service));
211 TestClient client;
213 // Start the service and flush tasks to ensure database is made available.
214 db_manager->StartOnIOThread();
215 content::RunAllBlockingPoolTasksUntilIdle();
216 base::RunLoop().RunUntilIdle();
217 EXPECT_TRUE(db_manager->DatabaseAvailable());
219 // Start an extension check operation, which is done asynchronously.
220 std::set<std::string> extension_ids;
221 extension_ids.insert("testtesttesttesttesttesttesttest");
222 db_manager->CheckExtensionIDs(extension_ids, &client);
224 // Stop the service without first flushing above tasks.
225 db_manager->StopOnIOThread(false);
227 // Now run posted tasks, whish should include the extension check which has
228 // been posted to the safe browsing task runner. This should not crash.
229 content::RunAllBlockingPoolTasksUntilIdle();
230 base::RunLoop().RunUntilIdle();