felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame^] | 1 | // Copyright 2015 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 | #ifndef CHROME_BROWSER_SAFE_BROWSING_UI_MANAGER_UNITTEST_CC_ |
| 6 | #define CHROME_BROWSER_SAFE_BROWSING_UI_MANAGER_UNITTEST_CC_ |
| 7 | |
| 8 | #include "base/memory/scoped_ptr.h" |
| 9 | #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 10 | #include "chrome/browser/safe_browsing/safe_browsing_util.h" |
| 11 | #include "chrome/browser/safe_browsing/ui_manager.h" |
| 12 | #include "content/public/test/test_browser_thread_bundle.h" |
| 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | #include "url/gurl.h" |
| 15 | |
| 16 | class SafeBrowsingUIManagerTest : public testing::Test { |
| 17 | public: |
| 18 | SafeBrowsingUIManagerTest() : ui_manager_(new SafeBrowsingUIManager(NULL)) {} |
| 19 | ~SafeBrowsingUIManagerTest() override{}; |
| 20 | |
| 21 | bool IsWhitelisted(SafeBrowsingUIManager::UnsafeResource resource) { |
| 22 | return ui_manager_->IsWhitelisted(resource); |
| 23 | } |
| 24 | |
| 25 | void AddToWhitelist(SafeBrowsingUIManager::UnsafeResource resource) { |
| 26 | ui_manager_->UpdateWhitelist(resource); |
| 27 | } |
| 28 | |
| 29 | private: |
| 30 | scoped_refptr<SafeBrowsingUIManager> ui_manager_; |
| 31 | content::TestBrowserThreadBundle thread_bundle_; |
| 32 | }; |
| 33 | |
| 34 | TEST_F(SafeBrowsingUIManagerTest, Whitelist) { |
| 35 | SafeBrowsingUIManager::UnsafeResource resource; |
| 36 | resource.url = GURL("https://www.google.com"); |
| 37 | resource.render_process_host_id = 14; |
| 38 | resource.render_view_id = 10; |
| 39 | resource.threat_type = SB_THREAT_TYPE_URL_MALWARE; |
| 40 | |
| 41 | EXPECT_FALSE(IsWhitelisted(resource)); |
| 42 | AddToWhitelist(resource); |
| 43 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 44 | } |
| 45 | |
| 46 | TEST_F(SafeBrowsingUIManagerTest, WhitelistId) { |
| 47 | SafeBrowsingUIManager::UnsafeResource resource; |
| 48 | resource.url = GURL("https://www.google.com"); |
| 49 | resource.render_process_host_id = 14; |
| 50 | resource.render_view_id = 10; |
| 51 | resource.threat_type = SB_THREAT_TYPE_URL_MALWARE; |
| 52 | |
| 53 | // Different render view ID. |
| 54 | SafeBrowsingUIManager::UnsafeResource resource_view_id; |
| 55 | resource_view_id.url = GURL("https://www.google.com"); |
| 56 | resource_view_id.render_process_host_id = 14; |
| 57 | resource_view_id.render_view_id = 3; |
| 58 | resource_view_id.threat_type = SB_THREAT_TYPE_URL_MALWARE; |
| 59 | |
| 60 | // Different render process host ID. |
| 61 | SafeBrowsingUIManager::UnsafeResource resource_process_id; |
| 62 | resource_process_id.url = GURL("https://www.google.com"); |
| 63 | resource_process_id.render_process_host_id = 6; |
| 64 | resource_process_id.render_view_id = 10; |
| 65 | resource_process_id.threat_type = SB_THREAT_TYPE_URL_MALWARE; |
| 66 | |
| 67 | EXPECT_FALSE(IsWhitelisted(resource)); |
| 68 | EXPECT_FALSE(IsWhitelisted(resource_view_id)); |
| 69 | EXPECT_FALSE(IsWhitelisted(resource_process_id)); |
| 70 | AddToWhitelist(resource); |
| 71 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 72 | EXPECT_FALSE(IsWhitelisted(resource_view_id)); |
| 73 | EXPECT_FALSE(IsWhitelisted(resource_process_id)); |
| 74 | } |
| 75 | |
| 76 | TEST_F(SafeBrowsingUIManagerTest, WhitelistUrl) { |
| 77 | SafeBrowsingUIManager::UnsafeResource google; |
| 78 | google.url = GURL("https://www.google.com"); |
| 79 | google.render_process_host_id = 14; |
| 80 | google.render_view_id = 10; |
| 81 | google.threat_type = SB_THREAT_TYPE_URL_MALWARE; |
| 82 | |
| 83 | SafeBrowsingUIManager::UnsafeResource gmail; |
| 84 | gmail.url = GURL("https://www.gmail.com"); |
| 85 | gmail.render_process_host_id = 14; |
| 86 | gmail.render_view_id = 10; |
| 87 | gmail.threat_type = SB_THREAT_TYPE_URL_MALWARE; |
| 88 | |
| 89 | EXPECT_FALSE(IsWhitelisted(google)); |
| 90 | EXPECT_FALSE(IsWhitelisted(gmail)); |
| 91 | AddToWhitelist(google); |
| 92 | EXPECT_TRUE(IsWhitelisted(google)); |
| 93 | EXPECT_FALSE(IsWhitelisted(gmail)); |
| 94 | } |
| 95 | |
| 96 | TEST_F(SafeBrowsingUIManagerTest, WhitelistThreat) { |
| 97 | SafeBrowsingUIManager::UnsafeResource list_malware; |
| 98 | list_malware.url = GURL("https://www.google.com"); |
| 99 | list_malware.render_process_host_id = 14; |
| 100 | list_malware.render_view_id = 10; |
| 101 | list_malware.threat_type = SB_THREAT_TYPE_URL_MALWARE; |
| 102 | |
| 103 | SafeBrowsingUIManager::UnsafeResource client_malware; |
| 104 | client_malware.url = GURL("https://www.google.com"); |
| 105 | client_malware.render_process_host_id = 14; |
| 106 | client_malware.render_view_id = 10; |
| 107 | client_malware.threat_type = SB_THREAT_TYPE_CLIENT_SIDE_MALWARE_URL; |
| 108 | |
| 109 | SafeBrowsingUIManager::UnsafeResource unwanted; |
| 110 | unwanted.url = GURL("https://www.google.com"); |
| 111 | unwanted.render_process_host_id = 14; |
| 112 | unwanted.render_view_id = 10; |
| 113 | unwanted.threat_type = SB_THREAT_TYPE_URL_UNWANTED; |
| 114 | |
| 115 | SafeBrowsingUIManager::UnsafeResource phishing; |
| 116 | phishing.url = GURL("https://www.google.com"); |
| 117 | phishing.render_process_host_id = 14; |
| 118 | phishing.render_view_id = 10; |
| 119 | phishing.threat_type = SB_THREAT_TYPE_URL_PHISHING; |
| 120 | |
| 121 | SafeBrowsingUIManager::UnsafeResource client_phishing; |
| 122 | client_phishing.url = GURL("https://www.google.com"); |
| 123 | client_phishing.render_process_host_id = 14; |
| 124 | client_phishing.render_view_id = 10; |
| 125 | client_phishing.threat_type = SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL; |
| 126 | |
| 127 | EXPECT_FALSE(IsWhitelisted(list_malware)); |
| 128 | EXPECT_FALSE(IsWhitelisted(client_malware)); |
| 129 | EXPECT_FALSE(IsWhitelisted(unwanted)); |
| 130 | EXPECT_FALSE(IsWhitelisted(phishing)); |
| 131 | EXPECT_FALSE(IsWhitelisted(client_phishing)); |
| 132 | AddToWhitelist(list_malware); |
| 133 | EXPECT_TRUE(IsWhitelisted(list_malware)); |
| 134 | EXPECT_TRUE(IsWhitelisted(client_malware)); |
| 135 | EXPECT_TRUE(IsWhitelisted(unwanted)); |
| 136 | EXPECT_TRUE(IsWhitelisted(phishing)); |
| 137 | EXPECT_TRUE(IsWhitelisted(client_phishing)); |
| 138 | } |
| 139 | |
| 140 | #endif // CHROME_BROWSER_SAFE_BROWSING_UI_MANAGER_UNITTEST_CC_ |