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" |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame^] | 12 | #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 13 | #include "chrome/test/base/testing_profile.h" |
| 14 | #include "content/public/browser/render_process_host.h" |
| 15 | #include "content/public/browser/render_view_host.h" |
| 16 | #include "content/public/browser/web_contents.h" |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 17 | #include "content/public/test/test_browser_thread_bundle.h" |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame^] | 18 | #include "content/public/test/web_contents_tester.h" |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 19 | #include "testing/gtest/include/gtest/gtest.h" |
| 20 | #include "url/gurl.h" |
| 21 | |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame^] | 22 | static const char* kGoodURL = "https://www.good.com"; |
| 23 | static const char* kBadURL = "https://www.malware.com"; |
| 24 | static const char* kBadURLWithPath = "https://www.malware.com/index.html"; |
| 25 | |
| 26 | class SafeBrowsingUIManagerTest : public ChromeRenderViewHostTestHarness { |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 27 | public: |
| 28 | SafeBrowsingUIManagerTest() : ui_manager_(new SafeBrowsingUIManager(NULL)) {} |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame^] | 29 | |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 30 | ~SafeBrowsingUIManagerTest() override{}; |
| 31 | |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame^] | 32 | void SetUp() override { ChromeRenderViewHostTestHarness::SetUp(); } |
| 33 | |
| 34 | void TearDown() override { ChromeRenderViewHostTestHarness::TearDown(); } |
| 35 | |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 36 | bool IsWhitelisted(SafeBrowsingUIManager::UnsafeResource resource) { |
| 37 | return ui_manager_->IsWhitelisted(resource); |
| 38 | } |
| 39 | |
| 40 | void AddToWhitelist(SafeBrowsingUIManager::UnsafeResource resource) { |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame^] | 41 | ui_manager_->AddToWhitelist(resource); |
| 42 | } |
| 43 | |
| 44 | SafeBrowsingUIManager::UnsafeResource MakeUnsafeResource(const char* url) { |
| 45 | SafeBrowsingUIManager::UnsafeResource resource; |
| 46 | resource.url = GURL(url); |
| 47 | resource.render_process_host_id = |
| 48 | web_contents()->GetRenderProcessHost()->GetID(); |
| 49 | resource.render_view_id = |
| 50 | web_contents()->GetRenderViewHost()->GetRoutingID(); |
| 51 | resource.threat_type = SB_THREAT_TYPE_URL_MALWARE; |
| 52 | return resource; |
| 53 | } |
| 54 | |
| 55 | SafeBrowsingUIManager::UnsafeResource MakeUnsafeResourceAndNavigate( |
| 56 | const char* url) { |
| 57 | SafeBrowsingUIManager::UnsafeResource resource = MakeUnsafeResource(url); |
| 58 | |
| 59 | // The WC doesn't have a URL without a navigation. Normally the |
| 60 | // interstitial would provide this instead of a fully committed navigation. |
| 61 | EXPECT_FALSE(IsWhitelisted(resource)); |
| 62 | NavigateAndCommit(GURL(url)); |
| 63 | return resource; |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | private: |
| 67 | scoped_refptr<SafeBrowsingUIManager> ui_manager_; |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | TEST_F(SafeBrowsingUIManagerTest, Whitelist) { |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame^] | 71 | SafeBrowsingUIManager::UnsafeResource resource = |
| 72 | MakeUnsafeResourceAndNavigate(kBadURL); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 73 | AddToWhitelist(resource); |
| 74 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 75 | } |
| 76 | |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame^] | 77 | TEST_F(SafeBrowsingUIManagerTest, WhitelistIgnoresSitesNotAdded) { |
| 78 | SafeBrowsingUIManager::UnsafeResource resource = |
| 79 | MakeUnsafeResourceAndNavigate(kGoodURL); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 80 | EXPECT_FALSE(IsWhitelisted(resource)); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame^] | 81 | } |
| 82 | |
| 83 | TEST_F(SafeBrowsingUIManagerTest, WhitelistIgnoresPath) { |
| 84 | SafeBrowsingUIManager::UnsafeResource resource = |
| 85 | MakeUnsafeResourceAndNavigate(kBadURL); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 86 | AddToWhitelist(resource); |
| 87 | EXPECT_TRUE(IsWhitelisted(resource)); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame^] | 88 | |
| 89 | SafeBrowsingUIManager::UnsafeResource resource_path = |
| 90 | MakeUnsafeResource(kBadURLWithPath); |
| 91 | EXPECT_TRUE(IsWhitelisted(resource_path)); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 92 | } |
| 93 | |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame^] | 94 | TEST_F(SafeBrowsingUIManagerTest, WhitelistIgnoresThreatType) { |
| 95 | SafeBrowsingUIManager::UnsafeResource resource = |
| 96 | MakeUnsafeResourceAndNavigate(kBadURL); |
| 97 | AddToWhitelist(resource); |
| 98 | EXPECT_TRUE(IsWhitelisted(resource)); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 99 | |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame^] | 100 | SafeBrowsingUIManager::UnsafeResource resource_phishing = |
| 101 | MakeUnsafeResource(kBadURL); |
| 102 | resource_phishing.threat_type = SB_THREAT_TYPE_URL_PHISHING; |
| 103 | EXPECT_TRUE(IsWhitelisted(resource_phishing)); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | #endif // CHROME_BROWSER_SAFE_BROWSING_UI_MANAGER_UNITTEST_CC_ |