Fire an error if a pref used in the UI is missing once all prefs are fetched.
[chromium-blink-merge.git] / chrome / browser / safe_browsing / database_manager_unittest.cc
bloba3e2e710e486dc0fb05389102b165a6f79d46e0b
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/database_manager.h"
12 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
13 #include "components/variations/variations_associated_data.h"
14 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "content/public/test/test_utils.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "testing/platform_test.h"
18 #include "url/gurl.h"
20 using content::TestBrowserThreadBundle;
22 namespace {
24 class TestClient : public SafeBrowsingDatabaseManager::Client {
25 public:
26 TestClient() {}
27 ~TestClient() override {}
29 void OnCheckBrowseUrlResult(const GURL& url,
30 SBThreatType threat_type,
31 const std::string& metadata) override {}
33 void OnCheckDownloadUrlResult(const std::vector<GURL>& url_chain,
34 SBThreatType threat_type) override {}
36 private:
37 DISALLOW_COPY_AND_ASSIGN(TestClient);
40 } // namespace
42 class SafeBrowsingDatabaseManagerTest : public PlatformTest {
43 public:
44 bool RunSBHashTest(const safe_browsing_util::ListType list_type,
45 const std::vector<SBThreatType>& expected_threats,
46 const std::string& result_list);
48 private:
49 TestBrowserThreadBundle thread_bundle_;
52 bool SafeBrowsingDatabaseManagerTest::RunSBHashTest(
53 const safe_browsing_util::ListType list_type,
54 const std::vector<SBThreatType>& expected_threats,
55 const std::string& result_list) {
56 scoped_refptr<SafeBrowsingService> sb_service_(
57 SafeBrowsingService::CreateSafeBrowsingService());
58 scoped_refptr<SafeBrowsingDatabaseManager> db_manager_(
59 new SafeBrowsingDatabaseManager(sb_service_));
60 const SBFullHash same_full_hash = {};
62 SafeBrowsingDatabaseManager::SafeBrowsingCheck* check =
63 new SafeBrowsingDatabaseManager::SafeBrowsingCheck(
64 std::vector<GURL>(),
65 std::vector<SBFullHash>(1, same_full_hash),
66 NULL,
67 list_type,
68 expected_threats);
69 db_manager_->checks_.insert(check);
71 const SBFullHashResult full_hash_result = {
72 same_full_hash,
73 safe_browsing_util::GetListId(result_list)
76 std::vector<SBFullHashResult> fake_results(1, full_hash_result);
77 bool result = db_manager_->HandleOneCheck(check, fake_results);
78 db_manager_->checks_.erase(check);
79 delete check;
80 return result;
83 TEST_F(SafeBrowsingDatabaseManagerTest, CheckCorrespondsListType) {
84 std::vector<SBThreatType> malware_threat(1,
85 SB_THREAT_TYPE_BINARY_MALWARE_URL);
86 EXPECT_FALSE(RunSBHashTest(safe_browsing_util::BINURL,
87 malware_threat,
88 safe_browsing_util::kMalwareList));
89 EXPECT_TRUE(RunSBHashTest(safe_browsing_util::BINURL,
90 malware_threat,
91 safe_browsing_util::kBinUrlList));
93 // Check for multiple threats
94 std::vector<SBThreatType> multiple_threats;
95 multiple_threats.push_back(SB_THREAT_TYPE_URL_MALWARE);
96 multiple_threats.push_back(SB_THREAT_TYPE_URL_PHISHING);
97 EXPECT_FALSE(RunSBHashTest(safe_browsing_util::MALWARE,
98 multiple_threats,
99 safe_browsing_util::kBinUrlList));
100 EXPECT_TRUE(RunSBHashTest(safe_browsing_util::MALWARE,
101 multiple_threats,
102 safe_browsing_util::kMalwareList));
105 TEST_F(SafeBrowsingDatabaseManagerTest, GetUrlSeverestThreatType) {
106 std::vector<SBFullHashResult> full_hashes;
108 const GURL kMalwareUrl("http://www.malware.com/page.html");
109 const GURL kPhishingUrl("http://www.phishing.com/page.html");
110 const GURL kUnwantedUrl("http://www.unwanted.com/page.html");
111 const GURL kUnwantedAndMalwareUrl(
112 "http://www.unwantedandmalware.com/page.html");
113 const GURL kSafeUrl("http://www.safe.com/page.html");
115 const SBFullHash kMalwareHostHash = SBFullHashForString("malware.com/");
116 const SBFullHash kPhishingHostHash = SBFullHashForString("phishing.com/");
117 const SBFullHash kUnwantedHostHash = SBFullHashForString("unwanted.com/");
118 const SBFullHash kUnwantedAndMalwareHostHash =
119 SBFullHashForString("unwantedandmalware.com/");
120 const SBFullHash kSafeHostHash = SBFullHashForString("www.safe.com/");
123 SBFullHashResult full_hash;
124 full_hash.hash = kMalwareHostHash;
125 full_hash.list_id = static_cast<int>(safe_browsing_util::MALWARE);
126 full_hashes.push_back(full_hash);
130 SBFullHashResult full_hash;
131 full_hash.hash = kPhishingHostHash;
132 full_hash.list_id = static_cast<int>(safe_browsing_util::PHISH);
133 full_hashes.push_back(full_hash);
137 SBFullHashResult full_hash;
138 full_hash.hash = kUnwantedHostHash;
139 full_hash.list_id = static_cast<int>(safe_browsing_util::UNWANTEDURL);
140 full_hashes.push_back(full_hash);
144 // Add both MALWARE and UNWANTEDURL list IDs for
145 // kUnwantedAndMalwareHostHash.
146 SBFullHashResult full_hash_malware;
147 full_hash_malware.hash = kUnwantedAndMalwareHostHash;
148 full_hash_malware.list_id = static_cast<int>(safe_browsing_util::MALWARE);
149 full_hashes.push_back(full_hash_malware);
151 SBFullHashResult full_hash_unwanted;
152 full_hash_unwanted.hash = kUnwantedAndMalwareHostHash;
153 full_hash_unwanted.list_id =
154 static_cast<int>(safe_browsing_util::UNWANTEDURL);
155 full_hashes.push_back(full_hash_unwanted);
158 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE,
159 SafeBrowsingDatabaseManager::GetHashSeverestThreatType(
160 kMalwareHostHash, full_hashes));
162 EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING,
163 SafeBrowsingDatabaseManager::GetHashSeverestThreatType(
164 kPhishingHostHash, full_hashes));
166 EXPECT_EQ(SB_THREAT_TYPE_URL_UNWANTED,
167 SafeBrowsingDatabaseManager::GetHashSeverestThreatType(
168 kUnwantedHostHash, full_hashes));
170 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE,
171 SafeBrowsingDatabaseManager::GetHashSeverestThreatType(
172 kUnwantedAndMalwareHostHash, full_hashes));
174 EXPECT_EQ(SB_THREAT_TYPE_SAFE,
175 SafeBrowsingDatabaseManager::GetHashSeverestThreatType(
176 kSafeHostHash, full_hashes));
178 const size_t kArbitraryValue = 123456U;
179 size_t index = kArbitraryValue;
180 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE,
181 SafeBrowsingDatabaseManager::GetUrlSeverestThreatType(
182 kMalwareUrl, full_hashes, &index));
183 EXPECT_EQ(0U, index);
185 EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING,
186 SafeBrowsingDatabaseManager::GetUrlSeverestThreatType(
187 kPhishingUrl, full_hashes, &index));
188 EXPECT_EQ(1U, index);
190 EXPECT_EQ(SB_THREAT_TYPE_URL_UNWANTED,
191 SafeBrowsingDatabaseManager::GetUrlSeverestThreatType(
192 kUnwantedUrl, full_hashes, &index));
193 EXPECT_EQ(2U, index);
195 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE,
196 SafeBrowsingDatabaseManager::GetUrlSeverestThreatType(
197 kUnwantedAndMalwareUrl, full_hashes, &index));
198 EXPECT_EQ(3U, index);
200 index = kArbitraryValue;
201 EXPECT_EQ(SB_THREAT_TYPE_SAFE,
202 SafeBrowsingDatabaseManager::GetUrlSeverestThreatType(
203 kSafeUrl, full_hashes, &index));
204 EXPECT_EQ(kArbitraryValue, index);
207 TEST_F(SafeBrowsingDatabaseManagerTest, ServiceStopWithPendingChecks) {
208 // Force the "blocking pool" mode for the test. This allows test coverage
209 // for this behavior while that mode is still not the default. Additionally,
210 // it is currently required for this test to work - as RunUntilIdle() will
211 // not run tasks of the special spawned thread, but will for the worker pool.
212 // TODO(asvitkine): Clean up, when blocking pool mode is made the default.
213 base::FieldTrialList list(nullptr);
214 std::map<std::string, std::string> params;
215 params["SBThreadingMode"] = "BlockingPool2";
216 variations::AssociateVariationParams("LightSpeed", "X", params);
217 base::FieldTrialList::CreateFieldTrial("LightSpeed", "X");
219 scoped_refptr<SafeBrowsingService> sb_service(
220 SafeBrowsingService::CreateSafeBrowsingService());
221 scoped_refptr<SafeBrowsingDatabaseManager> db_manager(
222 new SafeBrowsingDatabaseManager(sb_service));
223 TestClient client;
225 // Start the service and flush tasks to ensure database is made available.
226 db_manager->StartOnIOThread();
227 content::RunAllBlockingPoolTasksUntilIdle();
228 base::RunLoop().RunUntilIdle();
229 EXPECT_TRUE(db_manager->DatabaseAvailable());
231 // Start an extension check operation, which is done asynchronously.
232 std::set<std::string> extension_ids;
233 extension_ids.insert("testtesttesttesttesttesttesttest");
234 db_manager->CheckExtensionIDs(extension_ids, &client);
236 // Stop the service without first flushing above tasks.
237 db_manager->StopOnIOThread(false);
239 // Now run posted tasks, whish should include the extension check which has
240 // been posted to the safe browsing task runner. This should not crash.
241 content::RunAllBlockingPoolTasksUntilIdle();
242 base::RunLoop().RunUntilIdle();
244 variations::testing::ClearAllVariationParams();