-
Notifications
You must be signed in to change notification settings - Fork 894
/
domain_block_page_browsertest.cc
321 lines (266 loc) · 12 KB
/
domain_block_page_browsertest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "brave/browser/brave_shields/ad_block_service_browsertest.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "brave/browser/brave_browser_process_impl.h"
#include "brave/components/brave_shields/browser/ad_block_custom_filters_service.h"
#include "brave/components/brave_shields/common/features.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/interstitials/security_interstitial_page_test_utils.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/security_interstitials/content/security_interstitial_tab_helper.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "url/gurl.h"
using brave_shields::features::kBraveDomainBlock;
class DomainBlockTestBase : public AdBlockServiceTest {
public:
DomainBlockTestBase() {}
void SetUp() override {
request_count_ = 0;
embedded_test_server()->RegisterRequestMonitor(base::BindLambdaForTesting(
[&](const net::test_server::HttpRequest& request) {
request_count_ += 1;
}));
AdBlockServiceTest::SetUp();
}
void BlockDomainByURL(const GURL& url) {
UpdateAdBlockInstanceWithRules("||" + url.host() + "^");
}
content::WebContents* web_contents() {
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
DCHECK(web_contents);
return web_contents;
}
bool IsShowingInterstitial() {
return chrome_browser_interstitials::IsShowingInterstitial(web_contents());
}
void NavigateTo(const GURL& url) {
ui_test_utils::NavigateToURL(browser(), url);
content::RenderFrameHost* frame = web_contents()->GetMainFrame();
ASSERT_TRUE(WaitForRenderFrameReady(frame));
}
void Click(const std::string& id) {
content::RenderFrameHost* frame = web_contents()->GetMainFrame();
frame->ExecuteJavaScriptForTests(
base::ASCIIToUTF16("document.getElementById('" + id + "').click();\n"),
base::NullCallback());
}
void ClickAndWaitForNavigation(const std::string& id) {
content::TestNavigationObserver observer(web_contents());
Click(id);
observer.WaitForNavigationFinished();
}
protected:
int request_count_;
DISALLOW_COPY_AND_ASSIGN(DomainBlockTestBase);
};
class DomainBlockTest : public DomainBlockTestBase {
public:
DomainBlockTest() { feature_list_.InitAndEnableFeature(kBraveDomainBlock); }
private:
base::test::ScopedFeatureList feature_list_;
};
class DomainBlockDisabledTest : public DomainBlockTestBase {
public:
DomainBlockDisabledTest() {
feature_list_.InitAndDisableFeature(kBraveDomainBlock);
}
private:
base::test::ScopedFeatureList feature_list_;
};
IN_PROC_BROWSER_TEST_F(DomainBlockTest, ShowInterstitial) {
ASSERT_TRUE(InstallDefaultAdBlockExtension());
GURL url = embedded_test_server()->GetURL("a.com", "/simple.html");
// Navigate to a page on a.com. This should work normally.
NavigateTo(url);
ASSERT_FALSE(IsShowingInterstitial());
// Block a.com, then attempt to navigate to a page on a.com. This should be
// interrupted by the domain block interstitial.
BlockDomainByURL(url);
NavigateTo(url);
ASSERT_TRUE(IsShowingInterstitial());
}
IN_PROC_BROWSER_TEST_F(DomainBlockTest, ShowInterstitialAndProceed) {
ASSERT_TRUE(InstallDefaultAdBlockExtension());
GURL url = embedded_test_server()->GetURL("a.com", "/simple.html");
// Navigate to a page on a.com. This should work normally.
NavigateTo(url);
ASSERT_FALSE(IsShowingInterstitial());
// Block a.com, then attempt to navigate to a page on a.com. This should be
// interrupted by the domain block interstitial.
BlockDomainByURL(url);
NavigateTo(url);
ASSERT_TRUE(IsShowingInterstitial());
// Simulate click on "Proceed anyway" button. This should navigate to the
// originally requested page.
ClickAndWaitForNavigation("proceed-button");
ASSERT_FALSE(IsShowingInterstitial());
base::string16 expected_title = base::ASCIIToUTF16("OK");
content::TitleWatcher watcher(web_contents(), expected_title);
EXPECT_EQ(expected_title, watcher.WaitAndGetTitle());
}
IN_PROC_BROWSER_TEST_F(DomainBlockTest, ShowInterstitialAndReload) {
ASSERT_TRUE(InstallDefaultAdBlockExtension());
GURL url = embedded_test_server()->GetURL("a.com", "/simple.html");
// Navigate to a page on a.com. This should work normally.
NavigateTo(url);
ASSERT_FALSE(IsShowingInterstitial());
// Block a.com, then attempt to navigate to a page on a.com. This should be
// interrupted by the domain block interstitial.
BlockDomainByURL(url);
NavigateTo(url);
ASSERT_TRUE(IsShowingInterstitial());
// Attempt to reload page, which should again be interrupted by the
// interstitial.
web_contents()->GetController().Reload(content::ReloadType::NORMAL, true);
ASSERT_TRUE(IsShowingInterstitial());
}
IN_PROC_BROWSER_TEST_F(DomainBlockTest, ProceedAndReload) {
ASSERT_TRUE(InstallDefaultAdBlockExtension());
GURL url = embedded_test_server()->GetURL("a.com", "/simple.html");
// Navigate to a page on a.com. This should work normally.
NavigateTo(url);
ASSERT_FALSE(IsShowingInterstitial());
// Block a.com, then attempt to navigate to a page on a.com. This should be
// interrupted by the domain block interstitial.
BlockDomainByURL(url);
NavigateTo(url);
ASSERT_TRUE(IsShowingInterstitial());
// Simulate click on "Proceed anyway" button. This should navigate to the
// originally requested page.
ClickAndWaitForNavigation("proceed-button");
ASSERT_FALSE(IsShowingInterstitial());
base::string16 expected_title = base::ASCIIToUTF16("OK");
content::TitleWatcher watcher(web_contents(), expected_title);
EXPECT_EQ(expected_title, watcher.WaitAndGetTitle());
// Reload page. This should work normally and not by interrupted by the
// interstitial, because we chose to proceed in this tab, and that decision
// should persist for the lifetime of the tab.
web_contents()->GetController().Reload(content::ReloadType::NORMAL, true);
ASSERT_FALSE(IsShowingInterstitial());
content::TitleWatcher watcher2(web_contents(), expected_title);
EXPECT_EQ(expected_title, watcher2.WaitAndGetTitle());
EXPECT_EQ(web_contents()->GetURL().host(), "a.com");
}
IN_PROC_BROWSER_TEST_F(DomainBlockTest, ProceedDoesNotAffectNewTabs) {
ASSERT_TRUE(InstallDefaultAdBlockExtension());
GURL url = embedded_test_server()->GetURL("a.com", "/simple.html");
// Navigate to a page on a.com. This should work normally.
NavigateTo(url);
ASSERT_FALSE(IsShowingInterstitial());
// Block a.com, then attempt to navigate to a page on a.com. This should be
// interrupted by the domain block interstitial.
BlockDomainByURL(url);
NavigateTo(url);
ASSERT_TRUE(IsShowingInterstitial());
// Simulate click on "Proceed anyway" button. This should navigate to the
// originally requested page.
ClickAndWaitForNavigation("proceed-button");
ASSERT_FALSE(IsShowingInterstitial());
base::string16 expected_title = base::ASCIIToUTF16("OK");
content::TitleWatcher watcher(web_contents(), expected_title);
EXPECT_EQ(expected_title, watcher.WaitAndGetTitle());
// Open a new tab and navigate to a page on a.com. This should be interrupted
// by the domain block interstitial, because the permission we gave by
// clicking "Proceed anyway" in the other tab is tab-specific.
ui_test_utils::AllBrowserTabAddedWaiter new_tab;
ui_test_utils::NavigateToURLWithDisposition(
browser(), url, WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
new_tab.Wait();
ASSERT_TRUE(IsShowingInterstitial());
}
IN_PROC_BROWSER_TEST_F(DomainBlockTest, DontWarnAgainAndProceed) {
ASSERT_TRUE(InstallDefaultAdBlockExtension());
GURL url = embedded_test_server()->GetURL("a.com", "/simple.html");
// Navigate to a page on a.com. This should work normally.
NavigateTo(url);
ASSERT_FALSE(IsShowingInterstitial());
// Block a.com, then attempt to navigate to a page on a.com. This should be
// interrupted by the domain block interstitial.
BlockDomainByURL(url);
NavigateTo(url);
ASSERT_TRUE(IsShowingInterstitial());
// Simulate click on "Don't warn again" checkbox. This should not navigate.
// We should still be on the interstitial page.
Click("dont-warn-again-checkbox");
ASSERT_TRUE(IsShowingInterstitial());
// Simulate click on "Proceed anyway" button. This should save the "don't warn
// again" choice and navigate to the originally requested page.
ClickAndWaitForNavigation("proceed-button");
WaitForAdBlockServiceThreads();
ASSERT_FALSE(IsShowingInterstitial());
base::string16 expected_title = base::ASCIIToUTF16("OK");
content::TitleWatcher watcher(web_contents(), expected_title);
EXPECT_EQ(expected_title, watcher.WaitAndGetTitle());
// Open a new tab and navigate to a page on a.com. This should navigate
// directly, because we previously saved the "don't warn again" choice for
// this domain and are now respecting that choice.
ui_test_utils::AllBrowserTabAddedWaiter new_tab;
ui_test_utils::NavigateToURLWithDisposition(
browser(), url, WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
new_tab.Wait();
ASSERT_FALSE(IsShowingInterstitial());
EXPECT_EQ(web_contents()->GetURL().host(), "a.com");
}
IN_PROC_BROWSER_TEST_F(DomainBlockTest, ShowInterstitialAndGoBack) {
ASSERT_TRUE(InstallDefaultAdBlockExtension());
GURL url_a = embedded_test_server()->GetURL("a.com", "/simple.html");
GURL url_b = embedded_test_server()->GetURL("b.com", "/simple.html");
// Navigate to a page on a.com. This should work normally.
NavigateTo(url_a);
ASSERT_FALSE(IsShowingInterstitial());
// Block b.com, then attempt to navigate to a page on b.com, which should be
// interrupted by the domain block interstitial.
BlockDomainByURL(url_b);
NavigateTo(url_b);
ASSERT_TRUE(IsShowingInterstitial());
// Simulate click on "Go back" button. This should return to previous page on
// a.com.
ClickAndWaitForNavigation("primary-button");
ASSERT_FALSE(IsShowingInterstitial());
EXPECT_EQ(web_contents()->GetURL().host(), "a.com");
}
IN_PROC_BROWSER_TEST_F(DomainBlockTest, NoFetch) {
ASSERT_TRUE(InstallDefaultAdBlockExtension());
ASSERT_EQ(0, request_count_);
GURL url = embedded_test_server()->GetURL("a.com", "/simple.html");
BlockDomainByURL(url);
ui_test_utils::AllBrowserTabAddedWaiter new_tab;
ui_test_utils::NavigateToURLWithDisposition(
browser(), url, WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
new_tab.Wait();
// Should be showing domain blocked interstitial page.
ASSERT_TRUE(IsShowingInterstitial());
// Should be zero network traffic (not even a favicon fetch).
ASSERT_EQ(0, request_count_);
}
IN_PROC_BROWSER_TEST_F(DomainBlockDisabledTest, NoInterstitial) {
ASSERT_TRUE(InstallDefaultAdBlockExtension());
GURL url = embedded_test_server()->GetURL("a.com", "/simple.html");
// Navigate to a page on a.com. This should work normally.
NavigateTo(url);
ASSERT_FALSE(IsShowingInterstitial());
// Block a.com, then attempt to navigate to a page on a.com. This should
// still navigate normally because domain blocking has been explicitly
// disabled in this test.
BlockDomainByURL(url);
NavigateTo(url);
ASSERT_FALSE(IsShowingInterstitial());
// Ensure we ended up on the expected page.
base::string16 expected_title = base::ASCIIToUTF16("OK");
content::TitleWatcher watcher(web_contents(), expected_title);
EXPECT_EQ(expected_title, watcher.WaitAndGetTitle());
}