explicit_sites_filter_browsertest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // Copyright 2020 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. #include <fuchsia/mem/cpp/fidl.h>
  5. #include <fuchsia/web/cpp/fidl.h>
  6. #include <string>
  7. #include "base/fuchsia/fuchsia_logging.h"
  8. #include "components/policy/content/safe_search_service.h"
  9. #include "components/safe_search_api/stub_url_checker.h"
  10. #include "components/safe_search_api/url_checker.h"
  11. #include "content/public/test/browser_test.h"
  12. #include "fuchsia_web/common/test/frame_test_util.h"
  13. #include "fuchsia_web/common/test/test_navigation_listener.h"
  14. #include "fuchsia_web/webengine/browser/context_impl.h"
  15. #include "fuchsia_web/webengine/browser/frame_impl.h"
  16. #include "fuchsia_web/webengine/browser/frame_impl_browser_test_base.h"
  17. #include "fuchsia_web/webengine/browser/web_engine_browser_main_parts.h"
  18. #include "fuchsia_web/webengine/test/frame_for_test.h"
  19. #include "testing/gtest/include/gtest/gtest.h"
  20. namespace {
  21. constexpr char kPage1Path[] = "/title1.html";
  22. constexpr char kPage1Title[] = "title 1";
  23. constexpr char kCustomErrorPageTitle[] = "Custom Error Page Title";
  24. constexpr char kCustomExplicitSitesErrorPage[] = R"(<html>
  25. <head><title>Custom Error Page Title</title></head>
  26. <body>
  27. <b>This is a custom error</b>
  28. </body>
  29. </html>)";
  30. // Creates a Fuchsia memory data from |data|.
  31. // |data| should be a short string to avoid exceeding Zircon channel limits.
  32. fuchsia::mem::Data MemDataBytesFromShortString(base::StringPiece data) {
  33. return fuchsia::mem::Data::WithBytes(
  34. std::vector<uint8_t>(data.begin(), data.end()));
  35. }
  36. } // namespace
  37. // Defines a suite of tests that exercise Frame-level functionality, such as
  38. // navigation commands and page events.
  39. class ExplicitSitesFilterTest : public FrameImplTestBaseWithServer {
  40. public:
  41. ExplicitSitesFilterTest() = default;
  42. ~ExplicitSitesFilterTest() override = default;
  43. ExplicitSitesFilterTest(const ExplicitSitesFilterTest&) = delete;
  44. ExplicitSitesFilterTest& operator=(const ExplicitSitesFilterTest&) = delete;
  45. void SetUpOnMainThread() override {
  46. FrameImplTestBaseWithServer::SetUpOnMainThread();
  47. // Spin the message loop to allow the Context to connect, before
  48. // |context_impl()| is called.
  49. base::RunLoop().RunUntilIdle();
  50. SetSafeSearchURLCheckerForBrowserContext(context_impl()->browser_context());
  51. }
  52. protected:
  53. const size_t kUrlCheckerCacheSize = 1;
  54. void SetPageIsNotExplicit() {
  55. stub_url_checker_.SetUpValidResponse(false /* is_porn */);
  56. }
  57. void SetPageIsExplicit() {
  58. stub_url_checker_.SetUpValidResponse(true /* is_porn */);
  59. }
  60. std::string GetPage1UrlSpec() const {
  61. return embedded_test_server()->GetURL(kPage1Path).spec();
  62. }
  63. fuchsia::web::FrameHostPtr ConnectToFrameHost() {
  64. fuchsia::web::FrameHostPtr frame_host;
  65. zx_status_t status = published_services().Connect(frame_host.NewRequest());
  66. ZX_CHECK(status == ZX_OK, status) << "Connect to fuchsia.web.FrameHost";
  67. base::RunLoop().RunUntilIdle();
  68. return frame_host;
  69. }
  70. void SetSafeSearchURLCheckerForBrowserContext(
  71. content::BrowserContext* browser_context) {
  72. SafeSearchFactory::GetInstance()
  73. ->GetForBrowserContext(browser_context)
  74. ->SetSafeSearchURLCheckerForTest(
  75. stub_url_checker_.BuildURLChecker(kUrlCheckerCacheSize));
  76. }
  77. safe_search_api::StubURLChecker stub_url_checker_;
  78. };
  79. IN_PROC_BROWSER_TEST_F(ExplicitSitesFilterTest, FilterDisabled_SiteAllowed) {
  80. auto frame =
  81. FrameForTest::Create(context(), fuchsia::web::CreateFrameParams());
  82. SetPageIsNotExplicit();
  83. fuchsia::web::NavigationControllerPtr controller;
  84. frame->GetNavigationController(controller.NewRequest());
  85. EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(), {},
  86. GetPage1UrlSpec()));
  87. frame.navigation_listener().RunUntilTitleEquals(kPage1Title);
  88. }
  89. IN_PROC_BROWSER_TEST_F(ExplicitSitesFilterTest, FilterDisabled_SiteBlocked) {
  90. auto frame =
  91. FrameForTest::Create(context(), fuchsia::web::CreateFrameParams());
  92. SetPageIsExplicit();
  93. EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(), {},
  94. GetPage1UrlSpec()));
  95. frame.navigation_listener().RunUntilTitleEquals(kPage1Title);
  96. }
  97. IN_PROC_BROWSER_TEST_F(ExplicitSitesFilterTest, DefaultErrorPage_SiteAllowed) {
  98. fuchsia::web::CreateFrameParams params;
  99. params.set_explicit_sites_filter_error_page(
  100. fuchsia::mem::Data::WithBytes({}));
  101. auto frame = FrameForTest::Create(context(), std::move(params));
  102. SetPageIsNotExplicit();
  103. EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(), {},
  104. GetPage1UrlSpec()));
  105. frame.navigation_listener().RunUntilTitleEquals(kPage1Title);
  106. }
  107. IN_PROC_BROWSER_TEST_F(ExplicitSitesFilterTest, DefaultErrorPage_SiteBlocked) {
  108. fuchsia::web::CreateFrameParams params;
  109. params.set_explicit_sites_filter_error_page(
  110. fuchsia::mem::Data::WithBytes({}));
  111. auto frame = FrameForTest::Create(context(), std::move(params));
  112. SetPageIsExplicit();
  113. EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(), {},
  114. GetPage1UrlSpec()));
  115. // The page title is the URL for which navigation failed without the scheme
  116. // part ("http://");
  117. std::string expected_title = GetPage1UrlSpec().erase(0, 7);
  118. frame.navigation_listener().RunUntilErrorPageIsLoadedAndTitleEquals(
  119. expected_title);
  120. }
  121. IN_PROC_BROWSER_TEST_F(ExplicitSitesFilterTest, CustomErrorPage_SiteAllowed) {
  122. fuchsia::web::CreateFrameParams params;
  123. params.set_explicit_sites_filter_error_page(
  124. MemDataBytesFromShortString(kCustomExplicitSitesErrorPage));
  125. auto frame = FrameForTest::Create(context(), std::move(params));
  126. SetPageIsNotExplicit();
  127. EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(), {},
  128. GetPage1UrlSpec()));
  129. frame.navigation_listener().RunUntilTitleEquals(kPage1Title);
  130. }
  131. IN_PROC_BROWSER_TEST_F(ExplicitSitesFilterTest, CustomErrorPage_SiteBlocked) {
  132. fuchsia::web::CreateFrameParams params;
  133. params.set_explicit_sites_filter_error_page(
  134. MemDataBytesFromShortString(kCustomExplicitSitesErrorPage));
  135. auto frame = FrameForTest::Create(context(), std::move(params));
  136. SetPageIsExplicit();
  137. EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(), {},
  138. GetPage1UrlSpec()));
  139. frame.navigation_listener().RunUntilErrorPageIsLoadedAndTitleEquals(
  140. kCustomErrorPageTitle);
  141. }
  142. IN_PROC_BROWSER_TEST_F(ExplicitSitesFilterTest, FrameHost_SiteAllowed) {
  143. fuchsia::web::FrameHostPtr frame_host = ConnectToFrameHost();
  144. ASSERT_EQ(frame_host_impls().size(), 1U);
  145. SetSafeSearchURLCheckerForBrowserContext(
  146. frame_host_impls().front()->context_impl_for_test()->browser_context());
  147. fuchsia::web::CreateFrameParams params;
  148. params.set_explicit_sites_filter_error_page(
  149. MemDataBytesFromShortString(kCustomExplicitSitesErrorPage));
  150. auto frame = FrameForTest::Create(frame_host, std::move(params));
  151. SetPageIsNotExplicit();
  152. EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(), {},
  153. GetPage1UrlSpec()));
  154. frame.navigation_listener().RunUntilTitleEquals(kPage1Title);
  155. }
  156. IN_PROC_BROWSER_TEST_F(ExplicitSitesFilterTest, FrameHost_SiteBlocked) {
  157. fuchsia::web::FrameHostPtr frame_host = ConnectToFrameHost();
  158. ASSERT_EQ(frame_host_impls().size(), 1U);
  159. SetSafeSearchURLCheckerForBrowserContext(
  160. frame_host_impls().front()->context_impl_for_test()->browser_context());
  161. fuchsia::web::CreateFrameParams params;
  162. params.set_explicit_sites_filter_error_page(
  163. MemDataBytesFromShortString(kCustomExplicitSitesErrorPage));
  164. auto frame = FrameForTest::Create(frame_host, std::move(params));
  165. SetPageIsExplicit();
  166. EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(), {},
  167. GetPage1UrlSpec()));
  168. frame.navigation_listener().RunUntilErrorPageIsLoadedAndTitleEquals(
  169. kCustomErrorPageTitle);
  170. }
  171. IN_PROC_BROWSER_TEST_F(ExplicitSitesFilterTest,
  172. MultipleFrameHosts_SiteAllowed) {
  173. fuchsia::web::FrameHostPtr frame_host1 = ConnectToFrameHost();
  174. ASSERT_EQ(frame_host_impls().size(), 1U);
  175. SetSafeSearchURLCheckerForBrowserContext(
  176. frame_host_impls().front()->context_impl_for_test()->browser_context());
  177. fuchsia::web::CreateFrameParams params;
  178. params.set_explicit_sites_filter_error_page(
  179. MemDataBytesFromShortString(kCustomExplicitSitesErrorPage));
  180. auto frame1 = FrameForTest::Create(frame_host1, std::move(params));
  181. SetPageIsNotExplicit();
  182. EXPECT_TRUE(LoadUrlAndExpectResponse(frame1.GetNavigationController(), {},
  183. GetPage1UrlSpec()));
  184. frame1.navigation_listener().RunUntilTitleEquals(kPage1Title);
  185. // Disconnect first FrameHost, causing the associated BrowserContext to be
  186. // deleted. Then, create a new FrameHost connection, which creates a new
  187. // BrowserContext.
  188. frame_host1.Unbind();
  189. fuchsia::web::FrameHostPtr frame_host2 = ConnectToFrameHost();
  190. ASSERT_EQ(frame_host_impls().size(), 1U);
  191. SetSafeSearchURLCheckerForBrowserContext(
  192. frame_host_impls().front()->context_impl_for_test()->browser_context());
  193. fuchsia::web::CreateFrameParams params2;
  194. params2.set_explicit_sites_filter_error_page(
  195. MemDataBytesFromShortString(kCustomExplicitSitesErrorPage));
  196. auto frame2 = FrameForTest::Create(frame_host2, std::move(params2));
  197. EXPECT_TRUE(LoadUrlAndExpectResponse(frame2.GetNavigationController(), {},
  198. GetPage1UrlSpec()));
  199. frame2.navigation_listener().RunUntilTitleEquals(kPage1Title);
  200. }
  201. IN_PROC_BROWSER_TEST_F(ExplicitSitesFilterTest,
  202. MultipleFrameHosts_SiteBlocked) {
  203. fuchsia::web::FrameHostPtr frame_host1 = ConnectToFrameHost();
  204. ASSERT_EQ(frame_host_impls().size(), 1U);
  205. SetSafeSearchURLCheckerForBrowserContext(
  206. frame_host_impls().front()->context_impl_for_test()->browser_context());
  207. fuchsia::web::CreateFrameParams params;
  208. params.set_explicit_sites_filter_error_page(
  209. MemDataBytesFromShortString(kCustomExplicitSitesErrorPage));
  210. auto frame1 = FrameForTest::Create(frame_host1, std::move(params));
  211. SetPageIsExplicit();
  212. EXPECT_TRUE(LoadUrlAndExpectResponse(frame1.GetNavigationController(), {},
  213. GetPage1UrlSpec()));
  214. frame1.navigation_listener().RunUntilErrorPageIsLoadedAndTitleEquals(
  215. kCustomErrorPageTitle);
  216. // Disconnect first FrameHost, causing the associated BrowserContext to be
  217. // deleted. Then, create a new FrameHost connection, which creates a new
  218. // BrowserContext.
  219. frame_host1.Unbind();
  220. fuchsia::web::FrameHostPtr frame_host2 = ConnectToFrameHost();
  221. ASSERT_EQ(frame_host_impls().size(), 1U);
  222. SetSafeSearchURLCheckerForBrowserContext(
  223. frame_host_impls().front()->context_impl_for_test()->browser_context());
  224. fuchsia::web::CreateFrameParams params2;
  225. params2.set_explicit_sites_filter_error_page(
  226. MemDataBytesFromShortString(kCustomExplicitSitesErrorPage));
  227. auto frame2 = FrameForTest::Create(frame_host2, std::move(params2));
  228. EXPECT_TRUE(LoadUrlAndExpectResponse(frame2.GetNavigationController(), {},
  229. GetPage1UrlSpec()));
  230. frame2.navigation_listener().RunUntilErrorPageIsLoadedAndTitleEquals(
  231. kCustomErrorPageTitle);
  232. }