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 "content/public/test/test_browser_thread_bundle.h"
13 #include "testing/gtest/include/gtest/gtest.h"
16 class SafeBrowsingUIManagerTest
: public testing::Test
{
18 SafeBrowsingUIManagerTest() : ui_manager_(new SafeBrowsingUIManager(NULL
)) {}
19 ~SafeBrowsingUIManagerTest() override
{};
21 bool IsWhitelisted(SafeBrowsingUIManager::UnsafeResource resource
) {
22 return ui_manager_
->IsWhitelisted(resource
);
25 void AddToWhitelist(SafeBrowsingUIManager::UnsafeResource resource
) {
26 ui_manager_
->UpdateWhitelist(resource
);
30 scoped_refptr
<SafeBrowsingUIManager
> ui_manager_
;
31 content::TestBrowserThreadBundle thread_bundle_
;
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
;
41 EXPECT_FALSE(IsWhitelisted(resource
));
42 AddToWhitelist(resource
);
43 EXPECT_TRUE(IsWhitelisted(resource
));
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
;
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
;
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
;
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
));
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
;
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
;
89 EXPECT_FALSE(IsWhitelisted(google
));
90 EXPECT_FALSE(IsWhitelisted(gmail
));
91 AddToWhitelist(google
);
92 EXPECT_TRUE(IsWhitelisted(google
));
93 EXPECT_FALSE(IsWhitelisted(gmail
));
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
;
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
;
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
;
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
;
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
;
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
));
140 #endif // CHROME_BROWSER_SAFE_BROWSING_UI_MANAGER_UNITTEST_CC_