permissions_browsertest.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2021 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 "base/files/file_util.h"
  5. #include "base/fuchsia/mem_buffer_util.h"
  6. #include "base/strings/stringprintf.h"
  7. #include "base/threading/scoped_blocking_call.h"
  8. #include "content/public/test/browser_test.h"
  9. #include "fuchsia_web/common/test/frame_test_util.h"
  10. #include "fuchsia_web/common/test/test_navigation_listener.h"
  11. #include "fuchsia_web/webengine/browser/frame_impl_browser_test_base.h"
  12. #include "fuchsia_web/webengine/test/frame_for_test.h"
  13. #include "fuchsia_web/webengine/test/test_data.h"
  14. #include "net/test/embedded_test_server/http_request.h"
  15. #include "net/test/embedded_test_server/http_response.h"
  16. #include "testing/gtest/include/gtest/gtest.h"
  17. static const char kIframeTestPagePath[] = "/iframe.html";
  18. static const char kMicTestPagePath[] = "/mic.html";
  19. static const char kMicNoPermissionTestPagePath[] = "/mic.html?NoPermission";
  20. class PermissionsBrowserTest : public FrameImplTestBaseWithServer {
  21. public:
  22. PermissionsBrowserTest() = default;
  23. ~PermissionsBrowserTest() override = default;
  24. void SetUpOnMainThread() override {
  25. FrameImplTestBaseWithServer::SetUpOnMainThread();
  26. frame_ = FrameForTest::Create(context(), fuchsia::web::CreateFrameParams());
  27. }
  28. void GrantPermission(fuchsia::web::PermissionType type,
  29. const std::string& origin) {
  30. fuchsia::web::PermissionDescriptor permission;
  31. permission.set_type(type);
  32. frame_->SetPermissionState(std::move(permission), origin,
  33. fuchsia::web::PermissionState::GRANTED);
  34. }
  35. protected:
  36. void InjectBeforeLoadJs(const std::string& code);
  37. // Loads iframe.html with the specified URL used for the embedded page.
  38. void LoadPageInIframe(const std::string& url);
  39. std::unique_ptr<net::test_server::HttpResponse>
  40. RequestHandlerWithPermissionPolicy(
  41. const net::test_server::HttpRequest& request);
  42. uint64_t before_load_js_id_ = 1;
  43. FrameForTest frame_;
  44. };
  45. void PermissionsBrowserTest::InjectBeforeLoadJs(const std::string& code) {
  46. frame_->AddBeforeLoadJavaScript(
  47. before_load_js_id_++, {"*"}, base::MemBufferFromString(code, "test"),
  48. [](fuchsia::web::Frame_AddBeforeLoadJavaScript_Result result) {
  49. EXPECT_TRUE(result.is_response());
  50. });
  51. }
  52. void PermissionsBrowserTest::LoadPageInIframe(const std::string& url) {
  53. // Before loading a page on the default embedded test server, set the iframe
  54. // src to be |url|.
  55. InjectBeforeLoadJs(base::StringPrintf("iframeSrc = '%s';", url.c_str()));
  56. fuchsia::web::NavigationControllerPtr controller;
  57. frame_->GetNavigationController(controller.NewRequest());
  58. EXPECT_TRUE(LoadUrlAndExpectResponse(
  59. controller.get(), {},
  60. embedded_test_server()->GetURL(kIframeTestPagePath).spec()));
  61. }
  62. IN_PROC_BROWSER_TEST_F(PermissionsBrowserTest, PermissionInSameOriginIframe) {
  63. GrantPermission(
  64. fuchsia::web::PermissionType::MICROPHONE,
  65. embedded_test_server()->GetURL("/").DeprecatedGetOriginAsURL().spec());
  66. // Mic permission is expected to be granted since the iframe is loaded from
  67. // the same origin.
  68. GURL iframe_src = embedded_test_server()->GetURL(kMicTestPagePath);
  69. ASSERT_NO_FATAL_FAILURE(LoadPageInIframe(iframe_src.spec()));
  70. frame_.navigation_listener().RunUntilTitleEquals("ended");
  71. }
  72. IN_PROC_BROWSER_TEST_F(PermissionsBrowserTest, NoPermissionInSameOriginIframe) {
  73. // Mic permission is expected to be denied since it wasn't granted to the
  74. // parent frame.
  75. GURL iframe_src =
  76. embedded_test_server()->GetURL(kMicNoPermissionTestPagePath);
  77. ASSERT_NO_FATAL_FAILURE(LoadPageInIframe(iframe_src.spec()));
  78. frame_.navigation_listener().RunUntilTitleEquals("ended-NotFoundError");
  79. }
  80. IN_PROC_BROWSER_TEST_F(PermissionsBrowserTest, PermissionInCrossOriginIframe) {
  81. GrantPermission(
  82. fuchsia::web::PermissionType::MICROPHONE,
  83. embedded_test_server()->GetURL("/").DeprecatedGetOriginAsURL().spec());
  84. // Start a second embedded test server. It's used to load the page inside
  85. // the <iframe> from an origin different from the origin of the embedding
  86. // page.
  87. net::EmbeddedTestServer second_test_server;
  88. second_test_server.ServeFilesFromSourceDirectory(
  89. base::FilePath(kTestServerRoot));
  90. ASSERT_TRUE(second_test_server.Start());
  91. // Mic permissions are expected to be denied since the page is cross-origin.
  92. GURL iframe_src = second_test_server.GetURL(kMicNoPermissionTestPagePath);
  93. ASSERT_NO_FATAL_FAILURE(LoadPageInIframe(iframe_src.spec()));
  94. frame_.navigation_listener().RunUntilTitleEquals("ended-NotAllowedError");
  95. }
  96. IN_PROC_BROWSER_TEST_F(PermissionsBrowserTest,
  97. PermissionInCrossOriginIframeWithPermissionPolicy) {
  98. GrantPermission(
  99. fuchsia::web::PermissionType::MICROPHONE,
  100. embedded_test_server()->GetURL("/").DeprecatedGetOriginAsURL().spec());
  101. // Start a second embedded test server. It's used to load the page inside
  102. // the <iframe> from an origin different from the origin of the embedding
  103. // page.
  104. net::EmbeddedTestServer second_test_server;
  105. second_test_server.ServeFilesFromSourceDirectory(
  106. base::FilePath(kTestServerRoot));
  107. ASSERT_TRUE(second_test_server.Start());
  108. // Mic permissions are expected to be granted because the parent frame has
  109. // access to the microphone and it's delegated to the child by the permission
  110. // policy (see code below).
  111. GURL iframe_src = second_test_server.GetURL(kMicTestPagePath);
  112. InjectBeforeLoadJs(
  113. base::StringPrintf("iframePermissionPolicy = 'microphone %s';",
  114. iframe_src.DeprecatedGetOriginAsURL().spec().c_str()));
  115. ASSERT_NO_FATAL_FAILURE(LoadPageInIframe(iframe_src.spec()));
  116. frame_.navigation_listener().RunUntilTitleEquals("ended");
  117. }