Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / chrome / browser / safe_browsing / ui_manager_unittest.cc
blob9c5806e0273418ca66941b4b95bb4386af860c20
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.
5 #ifndef CHROME_BROWSER_SAFE_BROWSING_UI_MANAGER_UNITTEST_CC_
6 #define CHROME_BROWSER_SAFE_BROWSING_UI_MANAGER_UNITTEST_CC_
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 "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"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "content/public/test/web_contents_tester.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "url/gurl.h"
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";
26 class SafeBrowsingUIManagerTest : public ChromeRenderViewHostTestHarness {
27 public:
28 SafeBrowsingUIManagerTest() : ui_manager_(new SafeBrowsingUIManager(NULL)) {}
30 ~SafeBrowsingUIManagerTest() override{};
32 void SetUp() override { ChromeRenderViewHostTestHarness::SetUp(); }
34 void TearDown() override { ChromeRenderViewHostTestHarness::TearDown(); }
36 bool IsWhitelisted(SafeBrowsingUIManager::UnsafeResource resource) {
37 return ui_manager_->IsWhitelisted(resource);
40 void AddToWhitelist(SafeBrowsingUIManager::UnsafeResource resource) {
41 ui_manager_->AddToWhitelist(resource);
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;
55 SafeBrowsingUIManager::UnsafeResource MakeUnsafeResourceAndNavigate(
56 const char* url) {
57 SafeBrowsingUIManager::UnsafeResource resource = MakeUnsafeResource(url);
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;
66 private:
67 scoped_refptr<SafeBrowsingUIManager> ui_manager_;
70 TEST_F(SafeBrowsingUIManagerTest, Whitelist) {
71 SafeBrowsingUIManager::UnsafeResource resource =
72 MakeUnsafeResourceAndNavigate(kBadURL);
73 AddToWhitelist(resource);
74 EXPECT_TRUE(IsWhitelisted(resource));
77 TEST_F(SafeBrowsingUIManagerTest, WhitelistIgnoresSitesNotAdded) {
78 SafeBrowsingUIManager::UnsafeResource resource =
79 MakeUnsafeResourceAndNavigate(kGoodURL);
80 EXPECT_FALSE(IsWhitelisted(resource));
83 TEST_F(SafeBrowsingUIManagerTest, WhitelistIgnoresPath) {
84 SafeBrowsingUIManager::UnsafeResource resource =
85 MakeUnsafeResourceAndNavigate(kBadURL);
86 AddToWhitelist(resource);
87 EXPECT_TRUE(IsWhitelisted(resource));
89 SafeBrowsingUIManager::UnsafeResource resource_path =
90 MakeUnsafeResource(kBadURLWithPath);
91 EXPECT_TRUE(IsWhitelisted(resource_path));
94 TEST_F(SafeBrowsingUIManagerTest, WhitelistIgnoresThreatType) {
95 SafeBrowsingUIManager::UnsafeResource resource =
96 MakeUnsafeResourceAndNavigate(kBadURL);
97 AddToWhitelist(resource);
98 EXPECT_TRUE(IsWhitelisted(resource));
100 SafeBrowsingUIManager::UnsafeResource resource_phishing =
101 MakeUnsafeResource(kBadURL);
102 resource_phishing.threat_type = SB_THREAT_TYPE_URL_PHISHING;
103 EXPECT_TRUE(IsWhitelisted(resource_phishing));
106 #endif // CHROME_BROWSER_SAFE_BROWSING_UI_MANAGER_UNITTEST_CC_