blob: 9c5806e0273418ca66941b4b95bb4386af860c20 [file] [log] [blame]
feltbc2eda2d2015-06-23 02:06:031// 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"
feltfb118572015-08-18 05:22:0112#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"
feltbc2eda2d2015-06-23 02:06:0317#include "content/public/test/test_browser_thread_bundle.h"
feltfb118572015-08-18 05:22:0118#include "content/public/test/web_contents_tester.h"
feltbc2eda2d2015-06-23 02:06:0319#include "testing/gtest/include/gtest/gtest.h"
20#include "url/gurl.h"
21
feltfb118572015-08-18 05:22:0122static const char* kGoodURL = "https://www.good.com";
23static const char* kBadURL = "https://www.malware.com";
24static const char* kBadURLWithPath = "https://www.malware.com/index.html";
25
26class SafeBrowsingUIManagerTest : public ChromeRenderViewHostTestHarness {
feltbc2eda2d2015-06-23 02:06:0327 public:
28 SafeBrowsingUIManagerTest() : ui_manager_(new SafeBrowsingUIManager(NULL)) {}
feltfb118572015-08-18 05:22:0129
feltbc2eda2d2015-06-23 02:06:0330 ~SafeBrowsingUIManagerTest() override{};
31
feltfb118572015-08-18 05:22:0132 void SetUp() override { ChromeRenderViewHostTestHarness::SetUp(); }
33
34 void TearDown() override { ChromeRenderViewHostTestHarness::TearDown(); }
35
feltbc2eda2d2015-06-23 02:06:0336 bool IsWhitelisted(SafeBrowsingUIManager::UnsafeResource resource) {
37 return ui_manager_->IsWhitelisted(resource);
38 }
39
40 void AddToWhitelist(SafeBrowsingUIManager::UnsafeResource resource) {
feltfb118572015-08-18 05:22:0141 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;
feltbc2eda2d2015-06-23 02:06:0364 }
65
66 private:
67 scoped_refptr<SafeBrowsingUIManager> ui_manager_;
feltbc2eda2d2015-06-23 02:06:0368};
69
70TEST_F(SafeBrowsingUIManagerTest, Whitelist) {
feltfb118572015-08-18 05:22:0171 SafeBrowsingUIManager::UnsafeResource resource =
72 MakeUnsafeResourceAndNavigate(kBadURL);
feltbc2eda2d2015-06-23 02:06:0373 AddToWhitelist(resource);
74 EXPECT_TRUE(IsWhitelisted(resource));
75}
76
feltfb118572015-08-18 05:22:0177TEST_F(SafeBrowsingUIManagerTest, WhitelistIgnoresSitesNotAdded) {
78 SafeBrowsingUIManager::UnsafeResource resource =
79 MakeUnsafeResourceAndNavigate(kGoodURL);
feltbc2eda2d2015-06-23 02:06:0380 EXPECT_FALSE(IsWhitelisted(resource));
feltfb118572015-08-18 05:22:0181}
82
83TEST_F(SafeBrowsingUIManagerTest, WhitelistIgnoresPath) {
84 SafeBrowsingUIManager::UnsafeResource resource =
85 MakeUnsafeResourceAndNavigate(kBadURL);
feltbc2eda2d2015-06-23 02:06:0386 AddToWhitelist(resource);
87 EXPECT_TRUE(IsWhitelisted(resource));
feltfb118572015-08-18 05:22:0188
89 SafeBrowsingUIManager::UnsafeResource resource_path =
90 MakeUnsafeResource(kBadURLWithPath);
91 EXPECT_TRUE(IsWhitelisted(resource_path));
feltbc2eda2d2015-06-23 02:06:0392}
93
feltfb118572015-08-18 05:22:0194TEST_F(SafeBrowsingUIManagerTest, WhitelistIgnoresThreatType) {
95 SafeBrowsingUIManager::UnsafeResource resource =
96 MakeUnsafeResourceAndNavigate(kBadURL);
97 AddToWhitelist(resource);
98 EXPECT_TRUE(IsWhitelisted(resource));
feltbc2eda2d2015-06-23 02:06:0399
feltfb118572015-08-18 05:22:01100 SafeBrowsingUIManager::UnsafeResource resource_phishing =
101 MakeUnsafeResource(kBadURL);
102 resource_phishing.threat_type = SB_THREAT_TYPE_URL_PHISHING;
103 EXPECT_TRUE(IsWhitelisted(resource_phishing));
feltbc2eda2d2015-06-23 02:06:03104}
105
106#endif // CHROME_BROWSER_SAFE_BROWSING_UI_MANAGER_UNITTEST_CC_