OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stddef.h> | 5 #include <stddef.h> |
6 #include <utility> | 6 #include <utility> |
7 | 7 |
8 #include "base/base64.h" | 8 #include "base/base64.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 17 #include "content/browser/frame_host/interstitial_page_impl.h" |
| 18 #include "content/browser/web_contents/web_contents_impl.h" |
17 #include "content/public/browser/devtools_agent_host.h" | 19 #include "content/public/browser/devtools_agent_host.h" |
| 20 #include "content/public/browser/interstitial_page_delegate.h" |
18 #include "content/public/browser/javascript_dialog_manager.h" | 21 #include "content/public/browser/javascript_dialog_manager.h" |
| 22 #include "content/public/browser/navigation_controller.h" |
| 23 #include "content/public/browser/navigation_entry.h" |
19 #include "content/public/browser/navigation_handle.h" | 24 #include "content/public/browser/navigation_handle.h" |
20 #include "content/public/browser/render_frame_host.h" | 25 #include "content/public/browser/render_frame_host.h" |
21 #include "content/public/browser/render_view_host.h" | 26 #include "content/public/browser/render_view_host.h" |
22 #include "content/public/browser/render_widget_host_view.h" | 27 #include "content/public/browser/render_widget_host_view.h" |
| 28 #include "content/public/browser/ssl_status.h" |
23 #include "content/public/browser/web_contents.h" | 29 #include "content/public/browser/web_contents.h" |
24 #include "content/public/common/url_constants.h" | 30 #include "content/public/common/url_constants.h" |
25 #include "content/public/test/browser_test_utils.h" | 31 #include "content/public/test/browser_test_utils.h" |
26 #include "content/public/test/content_browser_test.h" | 32 #include "content/public/test/content_browser_test.h" |
27 #include "content/public/test/content_browser_test_utils.h" | 33 #include "content/public/test/content_browser_test_utils.h" |
28 #include "content/public/test/test_navigation_observer.h" | 34 #include "content/public/test/test_navigation_observer.h" |
29 #include "content/shell/browser/shell.h" | 35 #include "content/shell/browser/shell.h" |
30 #include "net/dns/mock_host_resolver.h" | 36 #include "net/dns/mock_host_resolver.h" |
| 37 #include "net/test/cert_test_util.h" |
31 #include "net/test/embedded_test_server/embedded_test_server.h" | 38 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 39 #include "net/test/test_data_directory.h" |
32 #include "testing/gmock/include/gmock/gmock.h" | 40 #include "testing/gmock/include/gmock/gmock.h" |
33 #include "third_party/skia/include/core/SkBitmap.h" | 41 #include "third_party/skia/include/core/SkBitmap.h" |
34 #include "ui/compositor/compositor_switches.h" | 42 #include "ui/compositor/compositor_switches.h" |
35 #include "ui/gfx/codec/png_codec.h" | 43 #include "ui/gfx/codec/png_codec.h" |
36 | 44 |
37 #define EXPECT_SIZE_EQ(expected, actual) \ | 45 #define EXPECT_SIZE_EQ(expected, actual) \ |
38 do { \ | 46 do { \ |
39 EXPECT_EQ((expected).width(), (actual).width()); \ | 47 EXPECT_EQ((expected).width(), (actual).width()); \ |
40 EXPECT_EQ((expected).height(), (actual).height()); \ | 48 EXPECT_EQ((expected).height(), (actual).height()); \ |
41 } while (false) | 49 } while (false) |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 | 118 |
111 } | 119 } |
112 | 120 |
113 class DevToolsProtocolTest : public ContentBrowserTest, | 121 class DevToolsProtocolTest : public ContentBrowserTest, |
114 public DevToolsAgentHostClient, | 122 public DevToolsAgentHostClient, |
115 public WebContentsDelegate { | 123 public WebContentsDelegate { |
116 public: | 124 public: |
117 DevToolsProtocolTest() | 125 DevToolsProtocolTest() |
118 : last_sent_id_(0), | 126 : last_sent_id_(0), |
119 waiting_for_command_result_id_(0), | 127 waiting_for_command_result_id_(0), |
120 in_dispatch_(false) { | 128 in_dispatch_(false), |
| 129 last_shown_certificate_(nullptr), |
| 130 ok_cert_(nullptr), |
| 131 expired_cert_(nullptr) {} |
| 132 |
| 133 void SetUpOnMainThread() override { |
| 134 ok_cert_ = |
| 135 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem"); |
| 136 expired_cert_ = net::ImportCertFromFile(net::GetTestCertsDirectory(), |
| 137 "expired_cert.pem"); |
121 } | 138 } |
122 | 139 |
123 protected: | 140 protected: |
124 // WebContentsDelegate method: | 141 // WebContentsDelegate method: |
125 bool AddMessageToConsole(WebContents* source, | 142 bool AddMessageToConsole(WebContents* source, |
126 int32_t level, | 143 int32_t level, |
127 const base::string16& message, | 144 const base::string16& message, |
128 int32_t line_no, | 145 int32_t line_no, |
129 const base::string16& source_id) override { | 146 const base::string16& source_id) override { |
130 console_messages_.push_back(base::UTF16ToUTF8(message)); | 147 console_messages_.push_back(base::UTF16ToUTF8(message)); |
131 return true; | 148 return true; |
132 } | 149 } |
133 | 150 |
| 151 void ShowCertificateViewerInDevTools( |
| 152 WebContents* web_contents, |
| 153 scoped_refptr<net::X509Certificate> certificate) override { |
| 154 last_shown_certificate_ = certificate; |
| 155 } |
| 156 |
134 void SendCommand(const std::string& method, | 157 void SendCommand(const std::string& method, |
135 std::unique_ptr<base::DictionaryValue> params) { | 158 std::unique_ptr<base::DictionaryValue> params) { |
136 SendCommand(method, std::move(params), true); | 159 SendCommand(method, std::move(params), true); |
137 } | 160 } |
138 | 161 |
139 void SendCommand(const std::string& method, | 162 void SendCommand(const std::string& method, |
140 std::unique_ptr<base::DictionaryValue> params, | 163 std::unique_ptr<base::DictionaryValue> params, |
141 bool wait) { | 164 bool wait) { |
142 in_dispatch_ = true; | 165 in_dispatch_ = true; |
143 base::DictionaryValue command; | 166 base::DictionaryValue command; |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 | 288 |
266 std::vector<std::string> GetAllFrameUrls() { | 289 std::vector<std::string> GetAllFrameUrls() { |
267 std::vector<std::string> urls; | 290 std::vector<std::string> urls; |
268 for (RenderFrameHost* render_frame_host : | 291 for (RenderFrameHost* render_frame_host : |
269 shell()->web_contents()->GetAllFrames()) { | 292 shell()->web_contents()->GetAllFrames()) { |
270 urls.push_back(RemovePort(render_frame_host->GetLastCommittedURL())); | 293 urls.push_back(RemovePort(render_frame_host->GetLastCommittedURL())); |
271 } | 294 } |
272 return urls; | 295 return urls; |
273 } | 296 } |
274 | 297 |
| 298 const scoped_refptr<net::X509Certificate>& last_shown_certificate() { |
| 299 return last_shown_certificate_; |
| 300 } |
| 301 |
| 302 const scoped_refptr<net::X509Certificate>& ok_cert() { return ok_cert_; } |
| 303 |
| 304 const scoped_refptr<net::X509Certificate>& expired_cert() { |
| 305 return expired_cert_; |
| 306 } |
| 307 |
275 std::unique_ptr<base::DictionaryValue> result_; | 308 std::unique_ptr<base::DictionaryValue> result_; |
276 scoped_refptr<DevToolsAgentHost> agent_host_; | 309 scoped_refptr<DevToolsAgentHost> agent_host_; |
277 int last_sent_id_; | 310 int last_sent_id_; |
278 std::vector<int> result_ids_; | 311 std::vector<int> result_ids_; |
279 std::vector<std::string> notifications_; | 312 std::vector<std::string> notifications_; |
280 std::vector<std::string> console_messages_; | 313 std::vector<std::string> console_messages_; |
281 std::unique_ptr<base::DictionaryValue> requested_notification_params_; | 314 std::unique_ptr<base::DictionaryValue> requested_notification_params_; |
282 | 315 |
283 private: | 316 private: |
284 void DispatchProtocolMessage(DevToolsAgentHost* agent_host, | 317 void DispatchProtocolMessage(DevToolsAgentHost* agent_host, |
(...skipping 29 matching lines...) Expand all Loading... |
314 } | 347 } |
315 } | 348 } |
316 | 349 |
317 void AgentHostClosed(DevToolsAgentHost* agent_host, bool replaced) override { | 350 void AgentHostClosed(DevToolsAgentHost* agent_host, bool replaced) override { |
318 EXPECT_TRUE(false); | 351 EXPECT_TRUE(false); |
319 } | 352 } |
320 | 353 |
321 std::string waiting_for_notification_; | 354 std::string waiting_for_notification_; |
322 int waiting_for_command_result_id_; | 355 int waiting_for_command_result_id_; |
323 bool in_dispatch_; | 356 bool in_dispatch_; |
| 357 scoped_refptr<net::X509Certificate> last_shown_certificate_; |
| 358 scoped_refptr<net::X509Certificate> ok_cert_; |
| 359 scoped_refptr<net::X509Certificate> expired_cert_; |
| 360 }; |
| 361 |
| 362 class TestInterstitialDelegate : public InterstitialPageDelegate { |
| 363 private: |
| 364 // InterstitialPageDelegate: |
| 365 std::string GetHTMLContents() override { return "<p>Interstitial</p>"; } |
324 }; | 366 }; |
325 | 367 |
326 class SyntheticKeyEventTest : public DevToolsProtocolTest { | 368 class SyntheticKeyEventTest : public DevToolsProtocolTest { |
327 protected: | 369 protected: |
328 void SendKeyEvent(const std::string& type, | 370 void SendKeyEvent(const std::string& type, |
329 int modifier, | 371 int modifier, |
330 int windowsKeyCode, | 372 int windowsKeyCode, |
331 int nativeKeyCode, | 373 int nativeKeyCode, |
332 const std::string& key) { | 374 const std::string& key) { |
333 std::unique_ptr<base::DictionaryValue> params(new base::DictionaryValue()); | 375 std::unique_ptr<base::DictionaryValue> params(new base::DictionaryValue()); |
(...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
930 params.reset(new base::DictionaryValue()); | 972 params.reset(new base::DictionaryValue()); |
931 params->SetString("policy", "advance"); | 973 params->SetString("policy", "advance"); |
932 params->SetInteger("budget", 1000); | 974 params->SetInteger("budget", 1000); |
933 SendCommand("Emulation.setVirtualTimePolicy", std::move(params), true); | 975 SendCommand("Emulation.setVirtualTimePolicy", std::move(params), true); |
934 | 976 |
935 WaitForNotification("Emulation.virtualTimeBudgetExpired"); | 977 WaitForNotification("Emulation.virtualTimeBudgetExpired"); |
936 | 978 |
937 EXPECT_THAT(console_messages_, ElementsAre("before", "done", "after")); | 979 EXPECT_THAT(console_messages_, ElementsAre("before", "done", "after")); |
938 } | 980 } |
939 | 981 |
| 982 // Tests that the Security.showCertificateViewer command shows the |
| 983 // certificate corresponding to the visible navigation entry, even when |
| 984 // an interstitial is showing. Regression test for |
| 985 // https://crbug.com/647759. |
| 986 IN_PROC_BROWSER_TEST_F(DevToolsProtocolTest, ShowCertificateViewer) { |
| 987 // First test that the correct certificate is shown for a normal |
| 988 // (non-interstitial) page. |
| 989 NavigateToURLBlockUntilNavigationsComplete(shell(), GURL("about:blank"), 1); |
| 990 Attach(); |
| 991 |
| 992 // Set a dummy certificate on the NavigationEntry. |
| 993 shell() |
| 994 ->web_contents() |
| 995 ->GetController() |
| 996 .GetVisibleEntry() |
| 997 ->GetSSL() |
| 998 .certificate = ok_cert(); |
| 999 |
| 1000 std::unique_ptr<base::DictionaryValue> params1(new base::DictionaryValue()); |
| 1001 SendCommand("Security.showCertificateViewer", std::move(params1), true); |
| 1002 |
| 1003 scoped_refptr<net::X509Certificate> normal_page_cert = shell() |
| 1004 ->web_contents() |
| 1005 ->GetController() |
| 1006 .GetVisibleEntry() |
| 1007 ->GetSSL() |
| 1008 .certificate; |
| 1009 ASSERT_TRUE(normal_page_cert); |
| 1010 EXPECT_EQ(normal_page_cert, last_shown_certificate()); |
| 1011 |
| 1012 // Now test that the correct certificate is shown on an interstitial. |
| 1013 TestInterstitialDelegate* delegate = new TestInterstitialDelegate; |
| 1014 WebContentsImpl* web_contents = |
| 1015 static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 1016 GURL interstitial_url("https://example.test"); |
| 1017 InterstitialPageImpl* interstitial = new InterstitialPageImpl( |
| 1018 web_contents, static_cast<RenderWidgetHostDelegate*>(web_contents), true, |
| 1019 interstitial_url, delegate); |
| 1020 interstitial->Show(); |
| 1021 WaitForInterstitialAttach(web_contents); |
| 1022 |
| 1023 // Set the transient navigation entry certificate. |
| 1024 NavigationEntry* transient_entry = |
| 1025 web_contents->GetController().GetTransientEntry(); |
| 1026 ASSERT_TRUE(transient_entry); |
| 1027 transient_entry->GetSSL().certificate = expired_cert(); |
| 1028 ASSERT_TRUE(transient_entry->GetSSL().certificate); |
| 1029 |
| 1030 std::unique_ptr<base::DictionaryValue> params2(new base::DictionaryValue()); |
| 1031 SendCommand("Security.showCertificateViewer", std::move(params2), true); |
| 1032 EXPECT_EQ(transient_entry->GetSSL().certificate, last_shown_certificate()); |
| 1033 } |
| 1034 |
940 } // namespace content | 1035 } // namespace content |
OLD | NEW |