Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Nasko Oskov | 8a73f7d | 2019-09-27 22:29:32 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Daniel Cheng | 0d360f55 | 2020-12-01 23:11:42 | [diff] [blame] | 5 | #include "base/strings/utf_string_conversions.h" |
Nasko Oskov | 8a73f7d | 2019-09-27 22:29:32 | [diff] [blame] | 6 | #include "content/browser/child_process_security_policy_impl.h" |
| 7 | #include "content/browser/renderer_host/render_process_host_impl.h" |
David Sanders | ee13241 | 2025-06-25 17:50:29 | [diff] [blame] | 8 | #include "content/public/browser/web_contents.h" |
Peter Kasting | 919ce65 | 2020-05-07 10:22:36 | [diff] [blame] | 9 | #include "content/public/test/browser_test.h" |
Nasko Oskov | 8a73f7d | 2019-09-27 22:29:32 | [diff] [blame] | 10 | #include "content/public/test/browser_test_utils.h" |
| 11 | #include "content/public/test/content_browser_test.h" |
| 12 | #include "content/public/test/content_browser_test_utils.h" |
| 13 | #include "content/shell/browser/shell.h" |
Lukasz Anforowicz | bb5ab43 | 2020-07-27 20:18:06 | [diff] [blame] | 14 | #include "testing/gmock/include/gmock/gmock.h" |
Nasko Oskov | 8a73f7d | 2019-09-27 22:29:32 | [diff] [blame] | 15 | |
| 16 | namespace content { |
| 17 | |
Daniel Cheng | 0d360f55 | 2020-12-01 23:11:42 | [diff] [blame] | 18 | class ProcessInternalsWebUiBrowserTest : public ContentBrowserTest { |
| 19 | protected: |
| 20 | // Executing javascript in the WebUI requires using an isolated world in which |
| 21 | // to execute the script because WebUI has a default CSP policy denying |
| 22 | // "eval()", which is what EvalJs uses under the hood. |
| 23 | bool ExecJsInWebUI(const std::string& script) { |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 24 | return ExecJs(shell()->web_contents()->GetPrimaryMainFrame(), script, |
Daniel Cheng | 0d360f55 | 2020-12-01 23:11:42 | [diff] [blame] | 25 | EXECUTE_SCRIPT_DEFAULT_OPTIONS, 1 /* world_id */); |
| 26 | } |
| 27 | }; |
Nasko Oskov | 8a73f7d | 2019-09-27 22:29:32 | [diff] [blame] | 28 | |
| 29 | // This test verifies that loading of the process-internals WebUI works |
| 30 | // correctly and the process rendering it has no WebUI bindings. |
| 31 | IN_PROC_BROWSER_TEST_F(ProcessInternalsWebUiBrowserTest, NoProcessBindings) { |
| 32 | GURL url("chrome://process-internals/#web-contents"); |
| 33 | EXPECT_TRUE(NavigateToURL(shell(), url)); |
| 34 | |
| 35 | EXPECT_FALSE(ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings( |
Emily Andrews | d15fd76 | 2024-12-10 20:41:54 | [diff] [blame] | 36 | shell() |
| 37 | ->web_contents() |
| 38 | ->GetPrimaryMainFrame() |
| 39 | ->GetProcess() |
| 40 | ->GetDeprecatedID())); |
Nasko Oskov | 8a73f7d | 2019-09-27 22:29:32 | [diff] [blame] | 41 | |
| 42 | // Execute script to ensure the page has loaded correctly and was successful |
| 43 | // at retrieving data from the browser process. |
| 44 | // Note: This requires using an isolated world in which to execute the |
| 45 | // script because WebUI has a default CSP policy denying "eval()", which is |
| 46 | // what EvalJs uses under the hood. |
Lukasz Anforowicz | bb5ab43 | 2020-07-27 20:18:06 | [diff] [blame] | 47 | std::string page_contents = |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 48 | EvalJs(shell()->web_contents()->GetPrimaryMainFrame(), |
| 49 | "document.body.innerHTML", EXECUTE_SCRIPT_DEFAULT_OPTIONS, |
| 50 | 1 /* world_id */) |
Lukasz Anforowicz | bb5ab43 | 2020-07-27 20:18:06 | [diff] [blame] | 51 | .ExtractString(); |
| 52 | |
| 53 | // Crude verification that the page had the right content. |
| 54 | EXPECT_THAT(page_contents, ::testing::HasSubstr("Process Internals")); |
Nasko Oskov | 8a73f7d | 2019-09-27 22:29:32 | [diff] [blame] | 55 | } |
| 56 | |
Daniel Cheng | 0d360f55 | 2020-12-01 23:11:42 | [diff] [blame] | 57 | IN_PROC_BROWSER_TEST_F(ProcessInternalsWebUiBrowserTest, |
| 58 | MojoJsBindingsCorrectlyScoped) { |
Jan Wilken Dörrie | 8aeb574 | 2021-03-23 19:27:02 | [diff] [blame] | 59 | const std::u16string passed_title = u"passed"; |
Daniel Cheng | 0d360f55 | 2020-12-01 23:11:42 | [diff] [blame] | 60 | |
| 61 | GURL url("chrome://process-internals/#web-contents"); |
| 62 | EXPECT_TRUE(NavigateToURL(shell(), url)); |
| 63 | { |
| 64 | TitleWatcher sent_title_watcher(shell()->web_contents(), passed_title); |
| 65 | EXPECT_TRUE( |
| 66 | ExecJsInWebUI("document.title = window.Mojo? 'passed' : 'failed';")); |
| 67 | EXPECT_EQ(passed_title, sent_title_watcher.WaitAndGetTitle()); |
| 68 | } |
| 69 | |
| 70 | EXPECT_TRUE(NavigateToURL(shell(), GURL("about:blank"))); |
| 71 | { |
| 72 | TitleWatcher sent_title_watcher(shell()->web_contents(), passed_title); |
| 73 | EXPECT_TRUE( |
| 74 | ExecJsInWebUI("document.title = window.Mojo? 'failed' : 'passed';")); |
| 75 | EXPECT_EQ(passed_title, sent_title_watcher.WaitAndGetTitle()); |
| 76 | } |
| 77 | } |
| 78 | |
Nasko Oskov | 8a73f7d | 2019-09-27 22:29:32 | [diff] [blame] | 79 | } // namespace content |