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
322
fuchsia_web / webengine / browser / explicit_sites_filter_browsertest.cc [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <fuchsia/mem/cpp/fidl.h>
#include <fuchsia/web/cpp/fidl.h>
#include <string>
#include <string_view>
#include "base/fuchsia/fuchsia_logging.h"
#include "base/run_loop.h"
#include "components/policy/content/safe_search_service.h"
#include "components/safe_search_api/stub_url_checker.h"
#include "components/safe_search_api/url_checker.h"
#include "content/public/test/browser_test.h"
#include "fuchsia_web/common/test/frame_for_test.h"
#include "fuchsia_web/common/test/frame_test_util.h"
#include "fuchsia_web/common/test/test_navigation_listener.h"
#include "fuchsia_web/webengine/browser/context_impl.h"
#include "fuchsia_web/webengine/browser/frame_impl.h"
#include "fuchsia_web/webengine/browser/frame_impl_browser_test_base.h"
#include "fuchsia_web/webengine/browser/web_engine_browser_main_parts.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
constexpr char kPage1Path[] = "/title1.html";
constexpr char kPage1Title[] = "title 1";
constexpr char kCustomErrorPageTitle[] = "Custom Error Page Title";
constexpr char kCustomExplicitSitesErrorPage[] = R"(<html>
<head><title>Custom Error Page Title</title></head>
<body>
<b>This is a custom error</b>
</body>
</html>)";
// Creates a Fuchsia memory data from |data|.
// |data| should be a short string to avoid exceeding Zircon channel limits.
fuchsia::mem::Data MemDataBytesFromShortString(std::string_view data) {
return fuchsia::mem::Data::WithBytes(
std::vector<uint8_t>(data.begin(), data.end()));
}
} // namespace
// Defines a suite of tests that exercise Frame-level functionality, such as
// navigation commands and page events.
class ExplicitSitesFilterTest : public FrameImplTestBaseWithServer {
public:
ExplicitSitesFilterTest() = default;
~ExplicitSitesFilterTest() override = default;
ExplicitSitesFilterTest(const ExplicitSitesFilterTest&) = delete;
ExplicitSitesFilterTest& operator=(const ExplicitSitesFilterTest&) = delete;
void SetUpOnMainThread() override {
FrameImplTestBaseWithServer::SetUpOnMainThread();
// Spin the message loop to allow the Context to connect, before
// |context_impl()| is called.
base::RunLoop().RunUntilIdle();
SetSafeSearchURLCheckerForBrowserContext(context_impl()->browser_context());
}
protected:
const size_t kUrlCheckerCacheSize = 1;
void SetPageIsNotExplicit() {
stub_url_checker_.SetUpValidResponse(false /* is_porn */);
}
void SetPageIsExplicit() {
stub_url_checker_.SetUpValidResponse(true /* is_porn */);
}
std::string GetPage1UrlSpec() const {
return embedded_test_server()->GetURL(kPage1Path).spec();
}
fuchsia::web::FrameHostPtr ConnectToFrameHost() {
fuchsia::web::FrameHostPtr frame_host;
zx_status_t status = published_services().Connect(frame_host.NewRequest());
ZX_CHECK(status == ZX_OK, status) << "Connect to fuchsia.web.FrameHost";
base::RunLoop().RunUntilIdle();
return frame_host;
}
void SetSafeSearchURLCheckerForBrowserContext(
content::BrowserContext* browser_context) {
SafeSearchFactory::GetInstance()
->GetForBrowserContext(browser_context)
->SetSafeSearchURLCheckerForTest(
stub_url_checker_.BuildURLChecker(kUrlCheckerCacheSize));
}
safe_search_api::StubURLChecker stub_url_checker_;
};
IN_PROC_BROWSER_TEST_F(ExplicitSitesFilterTest, FilterDisabled_SiteAllowed) {
auto frame =
FrameForTest::Create(context(), fuchsia::web::CreateFrameParams());
SetPageIsNotExplicit();
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(), {},
GetPage1UrlSpec()));
frame.navigation_listener().RunUntilTitleEquals(kPage1Title);
}
IN_PROC_BROWSER_TEST_F(ExplicitSitesFilterTest, FilterDisabled_SiteBlocked) {
auto frame =
FrameForTest::Create(context(), fuchsia::web::CreateFrameParams());
SetPageIsExplicit();
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(), {},
GetPage1UrlSpec()));
frame.navigation_listener().RunUntilTitleEquals(kPage1Title);
}
IN_PROC_BROWSER_TEST_F(ExplicitSitesFilterTest, DefaultErrorPage_SiteAllowed) {
fuchsia::web::CreateFrameParams params;
params.set_explicit_sites_filter_error_page(
fuchsia::mem::Data::WithBytes({}));
auto frame = FrameForTest::Create(context(), std::move(params));
SetPageIsNotExplicit();
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(), {},
GetPage1UrlSpec()));
frame.navigation_listener().RunUntilTitleEquals(kPage1Title);
}
IN_PROC_BROWSER_TEST_F(ExplicitSitesFilterTest, DefaultErrorPage_SiteBlocked) {
fuchsia::web::CreateFrameParams params;
params.set_explicit_sites_filter_error_page(
fuchsia::mem::Data::WithBytes({}));
auto frame = FrameForTest::Create(context(), std::move(params));
SetPageIsExplicit();
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(), {},
GetPage1UrlSpec()));
// The page title is the URL for which navigation failed without the scheme
// part ("http://");
std::string expected_title = GetPage1UrlSpec().erase(0, 7);
frame.navigation_listener().RunUntilErrorPageIsLoadedAndTitleEquals(
expected_title);
EXPECT_EQ(frame.navigation_listener().current_state()->error_detail(),
fuchsia::web::ErrorDetail::EXPLICIT_CONTENT_BLOCKED);
}
IN_PROC_BROWSER_TEST_F(ExplicitSitesFilterTest, CustomErrorPage_SiteAllowed) {
fuchsia::web::CreateFrameParams params;
params.set_explicit_sites_filter_error_page(
MemDataBytesFromShortString(kCustomExplicitSitesErrorPage));
auto frame = FrameForTest::Create(context(), std::move(params));
SetPageIsNotExplicit();
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(), {},
GetPage1UrlSpec()));
frame.navigation_listener().RunUntilTitleEquals(kPage1Title);
}
IN_PROC_BROWSER_TEST_F(ExplicitSitesFilterTest, CustomErrorPage_SiteBlocked) {
fuchsia::web::CreateFrameParams params;
params.set_explicit_sites_filter_error_page(
MemDataBytesFromShortString(kCustomExplicitSitesErrorPage));
auto frame = FrameForTest::Create(context(), std::move(params));
SetPageIsExplicit();
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(), {},
GetPage1UrlSpec()));
frame.navigation_listener().RunUntilErrorPageIsLoadedAndTitleEquals(
kCustomErrorPageTitle);
EXPECT_EQ(frame.navigation_listener().current_state()->error_detail(),
fuchsia::web::ErrorDetail::EXPLICIT_CONTENT_BLOCKED);
}
IN_PROC_BROWSER_TEST_F(ExplicitSitesFilterTest, FrameHost_SiteAllowed) {
fuchsia::web::FrameHostPtr frame_host = ConnectToFrameHost();
ASSERT_EQ(frame_host_impls().size(), 1U);
SetSafeSearchURLCheckerForBrowserContext(
frame_host_impls().front()->context_impl_for_test()->browser_context());
fuchsia::web::CreateFrameParams params;
params.set_explicit_sites_filter_error_page(
MemDataBytesFromShortString(kCustomExplicitSitesErrorPage));
auto frame = FrameForTest::Create(frame_host, std::move(params));
SetPageIsNotExplicit();
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(), {},
GetPage1UrlSpec()));
frame.navigation_listener().RunUntilTitleEquals(kPage1Title);
}
IN_PROC_BROWSER_TEST_F(ExplicitSitesFilterTest, FrameHost_SiteBlocked) {
fuchsia::web::FrameHostPtr frame_host = ConnectToFrameHost();
ASSERT_EQ(frame_host_impls().size(), 1U);
SetSafeSearchURLCheckerForBrowserContext(
frame_host_impls().front()->context_impl_for_test()->browser_context());
fuchsia::web::CreateFrameParams params;
params.set_explicit_sites_filter_error_page(
MemDataBytesFromShortString(kCustomExplicitSitesErrorPage));
auto frame = FrameForTest::Create(frame_host, std::move(params));
SetPageIsExplicit();
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(), {},
GetPage1UrlSpec()));
frame.navigation_listener().RunUntilErrorPageIsLoadedAndTitleEquals(
kCustomErrorPageTitle);
EXPECT_EQ(frame.navigation_listener().current_state()->error_detail(),
fuchsia::web::ErrorDetail::EXPLICIT_CONTENT_BLOCKED);
}
IN_PROC_BROWSER_TEST_F(ExplicitSitesFilterTest,
MultipleFrameHosts_SiteAllowed) {
fuchsia::web::FrameHostPtr frame_host1 = ConnectToFrameHost();
ASSERT_EQ(frame_host_impls().size(), 1U);
SetSafeSearchURLCheckerForBrowserContext(
frame_host_impls().front()->context_impl_for_test()->browser_context());
fuchsia::web::CreateFrameParams params;
params.set_explicit_sites_filter_error_page(
MemDataBytesFromShortString(kCustomExplicitSitesErrorPage));
auto frame1 = FrameForTest::Create(frame_host1, std::move(params));
SetPageIsNotExplicit();
EXPECT_TRUE(LoadUrlAndExpectResponse(frame1.GetNavigationController(), {},
GetPage1UrlSpec()));
frame1.navigation_listener().RunUntilTitleEquals(kPage1Title);
// Disconnect first FrameHost, causing the associated BrowserContext to be
// deleted. Then, create a new FrameHost connection, which creates a new
// BrowserContext.
frame_host1.Unbind();
frame1 = {};
fuchsia::web::FrameHostPtr frame_host2 = ConnectToFrameHost();
ASSERT_EQ(frame_host_impls().size(), 1U);
SetSafeSearchURLCheckerForBrowserContext(
frame_host_impls().front()->context_impl_for_test()->browser_context());
fuchsia::web::CreateFrameParams params2;
params2.set_explicit_sites_filter_error_page(
MemDataBytesFromShortString(kCustomExplicitSitesErrorPage));
auto frame2 = FrameForTest::Create(frame_host2, std::move(params2));
EXPECT_TRUE(LoadUrlAndExpectResponse(frame2.GetNavigationController(), {},
GetPage1UrlSpec()));
frame2.navigation_listener().RunUntilTitleEquals(kPage1Title);
}
IN_PROC_BROWSER_TEST_F(ExplicitSitesFilterTest,
MultipleFrameHosts_SiteBlocked) {
fuchsia::web::FrameHostPtr frame_host1 = ConnectToFrameHost();
ASSERT_EQ(frame_host_impls().size(), 1U);
SetSafeSearchURLCheckerForBrowserContext(
frame_host_impls().front()->context_impl_for_test()->browser_context());
fuchsia::web::CreateFrameParams params;
params.set_explicit_sites_filter_error_page(
MemDataBytesFromShortString(kCustomExplicitSitesErrorPage));
auto frame1 = FrameForTest::Create(frame_host1, std::move(params));
SetPageIsExplicit();
EXPECT_TRUE(LoadUrlAndExpectResponse(frame1.GetNavigationController(), {},
GetPage1UrlSpec()));
frame1.navigation_listener().RunUntilErrorPageIsLoadedAndTitleEquals(
kCustomErrorPageTitle);
EXPECT_EQ(frame1.navigation_listener().current_state()->error_detail(),
fuchsia::web::ErrorDetail::EXPLICIT_CONTENT_BLOCKED);
// Disconnect first FrameHost, causing the associated BrowserContext to be
// deleted. Then, create a new FrameHost connection, which creates a new
// BrowserContext.
frame_host1.Unbind();
frame1 = {};
fuchsia::web::FrameHostPtr frame_host2 = ConnectToFrameHost();
ASSERT_EQ(frame_host_impls().size(), 1U);
SetSafeSearchURLCheckerForBrowserContext(
frame_host_impls().front()->context_impl_for_test()->browser_context());
fuchsia::web::CreateFrameParams params2;
params2.set_explicit_sites_filter_error_page(
MemDataBytesFromShortString(kCustomExplicitSitesErrorPage));
auto frame2 = FrameForTest::Create(frame_host2, std::move(params2));
EXPECT_TRUE(LoadUrlAndExpectResponse(frame2.GetNavigationController(), {},
GetPage1UrlSpec()));
frame2.navigation_listener().RunUntilErrorPageIsLoadedAndTitleEquals(
kCustomErrorPageTitle);
EXPECT_EQ(frame2.navigation_listener().current_state()->error_detail(),
fuchsia::web::ErrorDetail::EXPLICIT_CONTENT_BLOCKED);
}