Update mojo surfaces bindings and mojo/cc/ glue
[chromium-blink-merge.git] / chrome / browser / safe_browsing / database_manager_unittest.cc
blob5639046de527bcd8892cac6d2d0dd3fa77d59f16
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 "chrome/browser/safe_browsing/database_manager.h"
11 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
12 #include "content/public/test/test_browser_thread_bundle.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "testing/platform_test.h"
15 #include "url/gurl.h"
17 using content::TestBrowserThreadBundle;
19 class SafeBrowsingDatabaseManagerTest : public PlatformTest {
20 public:
21 bool RunSBHashTest(const safe_browsing_util::ListType list_type,
22 const std::vector<SBThreatType>& expected_threats,
23 const std::string& result_list);
25 private:
26 TestBrowserThreadBundle thread_bundle_;
29 bool SafeBrowsingDatabaseManagerTest::RunSBHashTest(
30 const safe_browsing_util::ListType list_type,
31 const std::vector<SBThreatType>& expected_threats,
32 const std::string& result_list) {
33 scoped_refptr<SafeBrowsingService> sb_service_(
34 SafeBrowsingService::CreateSafeBrowsingService());
35 scoped_refptr<SafeBrowsingDatabaseManager> db_manager_(
36 new SafeBrowsingDatabaseManager(sb_service_));
37 const SBFullHash same_full_hash = {};
39 SafeBrowsingDatabaseManager::SafeBrowsingCheck* check =
40 new SafeBrowsingDatabaseManager::SafeBrowsingCheck(
41 std::vector<GURL>(),
42 std::vector<SBFullHash>(1, same_full_hash),
43 NULL,
44 list_type,
45 expected_threats);
46 db_manager_->checks_.insert(check);
48 const SBFullHashResult full_hash_result = {
49 same_full_hash,
50 safe_browsing_util::GetListId(result_list)
53 std::vector<SBFullHashResult> fake_results(1, full_hash_result);
54 bool result = db_manager_->HandleOneCheck(check, fake_results);
55 db_manager_->checks_.erase(check);
56 delete check;
57 return result;
60 TEST_F(SafeBrowsingDatabaseManagerTest, CheckCorrespondsListType) {
61 std::vector<SBThreatType> malware_threat(1,
62 SB_THREAT_TYPE_BINARY_MALWARE_URL);
63 EXPECT_FALSE(RunSBHashTest(safe_browsing_util::BINURL,
64 malware_threat,
65 safe_browsing_util::kMalwareList));
66 EXPECT_TRUE(RunSBHashTest(safe_browsing_util::BINURL,
67 malware_threat,
68 safe_browsing_util::kBinUrlList));
70 // Check for multiple threats
71 std::vector<SBThreatType> multiple_threats;
72 multiple_threats.push_back(SB_THREAT_TYPE_URL_MALWARE);
73 multiple_threats.push_back(SB_THREAT_TYPE_URL_PHISHING);
74 EXPECT_FALSE(RunSBHashTest(safe_browsing_util::MALWARE,
75 multiple_threats,
76 safe_browsing_util::kBinUrlList));
77 EXPECT_TRUE(RunSBHashTest(safe_browsing_util::MALWARE,
78 multiple_threats,
79 safe_browsing_util::kMalwareList));
82 TEST_F(SafeBrowsingDatabaseManagerTest, GetUrlThreatType) {
83 std::vector<SBFullHashResult> full_hashes;
85 const GURL kMalwareUrl("http://www.malware.com/page.html");
86 const GURL kPhishingUrl("http://www.phishing.com/page.html");
87 const GURL kSafeUrl("http://www.safe.com/page.html");
89 const SBFullHash kMalwareHostHash = SBFullHashForString("malware.com/");
90 const SBFullHash kPhishingHostHash = SBFullHashForString("phishing.com/");
91 const SBFullHash kSafeHostHash = SBFullHashForString("www.safe.com/");
94 SBFullHashResult full_hash;
95 full_hash.hash = kMalwareHostHash;
96 full_hash.list_id = static_cast<int>(safe_browsing_util::MALWARE);
97 full_hashes.push_back(full_hash);
101 SBFullHashResult full_hash;
102 full_hash.hash = kPhishingHostHash;
103 full_hash.list_id = static_cast<int>(safe_browsing_util::PHISH);
104 full_hashes.push_back(full_hash);
107 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE,
108 SafeBrowsingDatabaseManager::GetHashThreatType(
109 kMalwareHostHash, full_hashes));
111 EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING,
112 SafeBrowsingDatabaseManager::GetHashThreatType(
113 kPhishingHostHash, full_hashes));
115 EXPECT_EQ(SB_THREAT_TYPE_SAFE,
116 SafeBrowsingDatabaseManager::GetHashThreatType(
117 kSafeHostHash, full_hashes));
119 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE,
120 SafeBrowsingDatabaseManager::GetUrlThreatType(
121 kMalwareUrl, full_hashes));
123 EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING,
124 SafeBrowsingDatabaseManager::GetUrlThreatType(
125 kPhishingUrl, full_hashes));
127 EXPECT_EQ(SB_THREAT_TYPE_SAFE,
128 SafeBrowsingDatabaseManager::GetUrlThreatType(
129 kSafeUrl, full_hashes));