[go: nahoru, domu]

blob: 6e702d5f224f233addab1e6126c523367277f90a [file] [log] [blame]
joi@chromium.orgb0b67cf2012-01-18 21:59:571// Copyright (c) 2012 The Chromium Authors. All rights reserved.
maruel@chromium.orgf0a51fb52009-03-05 12:46:382// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
tsepez@chromium.org6136d062013-05-08 19:31:345#include "base/path_service.h"
avi@chromium.org74ebfb12013-06-07 20:48:006#include "base/strings/utf_string_conversions.h"
ananta@chromium.orgb9535422012-02-09 01:47:597#include "content/browser/child_process_security_policy_impl.h"
jam@chromium.org79ea4862011-02-24 00:46:448#include "content/browser/renderer_host/test_render_view_host.h"
avi@chromium.orgf9e4dae2012-04-10 21:26:379#include "content/browser/web_contents/navigation_controller_impl.h"
jamesr@chromium.orgc084330e02013-04-27 01:08:1510#include "content/common/input_messages.h"
jam@chromium.org1a178262011-03-23 18:14:1811#include "content/common/view_messages.h"
jam@chromium.org5a3bdf52012-05-24 15:12:5712#include "content/port/browser/render_view_host_delegate_view.h"
jam@chromium.orgad23a092011-12-28 07:02:0413#include "content/public/browser/navigation_entry.h"
creis@chromium.org8ae91352012-03-15 17:06:0014#include "content/public/common/bindings_policy.h"
darin@chromium.orgdc293a72013-07-01 11:11:2215#include "content/public/common/drop_data.h"
jam@chromium.org2905f742011-10-13 03:51:5816#include "content/public/common/page_transition_types.h"
pauljensen@chromium.orge0f35c92013-05-08 16:04:3417#include "content/public/common/url_constants.h"
jam@chromium.org08a932d52012-06-03 21:42:1218#include "content/public/test/mock_render_process_host.h"
pauljensen@chromium.orge0f35c92013-05-08 16:04:3419#include "content/test/test_content_browser_client.h"
jam@chromium.org4172b082013-02-25 18:07:3420#include "content/test/test_web_contents.h"
jam@chromium.org9d8ea302012-09-25 15:04:2221#include "net/base/net_util.h"
abarth@chromium.org2255a9332013-06-17 05:12:3122#include "third_party/WebKit/public/web/WebDragOperation.h"
maruel@chromium.orgf0a51fb52009-03-05 12:46:3823
jam@chromium.org18082852012-10-29 19:35:5724namespace content {
joi@chromium.orgc0257382012-03-12 20:15:3425
pauljensen@chromium.orge0f35c92013-05-08 16:04:3426class RenderViewHostTestBrowserClient : public TestContentBrowserClient {
27 public:
28 RenderViewHostTestBrowserClient() {}
29 virtual ~RenderViewHostTestBrowserClient() {}
30
31 virtual bool IsHandledURL(const GURL& url) OVERRIDE {
32 return url.scheme() == chrome::kFileScheme;
33 }
34
35 private:
36 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestBrowserClient);
37};
38
joi@chromium.orgc0257382012-03-12 20:15:3439class RenderViewHostTest : public RenderViewHostImplTestHarness {
pauljensen@chromium.orge0f35c92013-05-08 16:04:3440 public:
41 RenderViewHostTest() : old_browser_client_(NULL) {}
42 virtual ~RenderViewHostTest() {}
43
44 virtual void SetUp() OVERRIDE {
45 RenderViewHostImplTestHarness::SetUp();
46 old_browser_client_ = SetBrowserClientForTesting(&test_browser_client_);
47 }
48
49 virtual void TearDown() OVERRIDE {
50 SetBrowserClientForTesting(old_browser_client_);
51 RenderViewHostImplTestHarness::TearDown();
52 }
53
54 private:
55 RenderViewHostTestBrowserClient test_browser_client_;
56 ContentBrowserClient* old_browser_client_;
57
58 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTest);
maruel@chromium.orgf0a51fb52009-03-05 12:46:3859};
60
61// All about URLs reported by the renderer should get rewritten to about:blank.
jam@chromium.org3d9ec502013-01-02 22:05:2562// See RenderViewHost::OnNavigate for a discussion.
maruel@chromium.orgf0a51fb52009-03-05 12:46:3863TEST_F(RenderViewHostTest, FilterAbout) {
joi@chromium.orgc0257382012-03-12 20:15:3464 test_rvh()->SendNavigate(1, GURL("about:cache"));
nasko@chromium.org6286a372013-10-09 04:03:2765 ASSERT_TRUE(controller().GetVisibleEntry());
66 EXPECT_EQ(GURL(kAboutBlankURL), controller().GetVisibleEntry()->GetURL());
maruel@chromium.orgf0a51fb52009-03-05 12:46:3867}
cpu@google.comd2fc65d2009-07-15 01:36:1068
boliu@chromium.org484955942010-08-19 16:13:1869// Create a full screen popup RenderWidgetHost and View.
70TEST_F(RenderViewHostTest, CreateFullscreenWidget) {
71 int routing_id = process()->GetNextRoutingID();
joi@chromium.orgc0257382012-03-12 20:15:3472 test_rvh()->CreateNewFullscreenWidget(routing_id);
boliu@chromium.org484955942010-08-19 16:13:1873}
74
sky@chromium.orgd0282262010-08-20 02:52:3275// Makes sure that RenderViewHost::is_waiting_for_unload_ack_ is false when
76// reloading a page. If is_waiting_for_unload_ack_ is not false when reloading
avi@chromium.orgadfd280e2012-04-13 15:52:3977// the contents may get closed out even though the user pressed the reload
78// button.
sky@chromium.orgd0282262010-08-20 02:52:3279TEST_F(RenderViewHostTest, ResetUnloadOnReload) {
80 const GURL url1("http://foo1");
81 const GURL url2("http://foo2");
82
83 // This test is for a subtle timing bug. Here's the sequence that triggered
84 // the bug:
85 // . go to a page.
86 // . go to a new page, preferably one that takes a while to resolve, such
87 // as one on a site that doesn't exist.
88 // . After this step is_waiting_for_unload_ack_ has been set to true on
89 // the first RVH.
90 // . click stop before the page has been commited.
91 // . click reload.
92 // . is_waiting_for_unload_ack_ is still true, and the if the hang monitor
avi@chromium.orgadfd280e2012-04-13 15:52:3993 // fires the contents gets closed.
sky@chromium.orgd0282262010-08-20 02:52:3294
95 NavigateAndCommit(url1);
jam@chromium.org2905f742011-10-13 03:51:5896 controller().LoadURL(
jam@chromium.org18082852012-10-29 19:35:5797 url2, Referrer(), PAGE_TRANSITION_LINK, std::string());
tfarina@chromium.org6981d9632010-11-30 21:34:0298 // Simulate the ClosePage call which is normally sent by the net::URLRequest.
creis@chromium.org992db4c2011-05-12 15:37:1599 rvh()->ClosePage();
100 // Needed so that navigations are not suspended on the RVH.
joi@chromium.orgc0257382012-03-12 20:15:34101 test_rvh()->SendShouldCloseACK(true);
sky@chromium.orgd0282262010-08-20 02:52:32102 contents()->Stop();
103 controller().Reload(false);
creis@chromium.orge1986832013-06-14 07:27:28104 EXPECT_FALSE(test_rvh()->is_waiting_for_unload_ack());
sky@chromium.orgd0282262010-08-20 02:52:32105}
106
creis@chromium.org8ae91352012-03-15 17:06:00107// Ensure we do not grant bindings to a process shared with unprivileged views.
108TEST_F(RenderViewHostTest, DontGrantBindingsToSharedProcess) {
109 // Create another view in the same process.
avi@chromium.orgadfd280e2012-04-13 15:52:39110 scoped_ptr<TestWebContents> new_web_contents(
ajwong@chromium.orgd1198fd2012-08-13 22:50:19111 TestWebContents::Create(browser_context(), rvh()->GetSiteInstance()));
creis@chromium.org8ae91352012-03-15 17:06:00112
jam@chromium.org18082852012-10-29 19:35:57113 rvh()->AllowBindings(BINDINGS_POLICY_WEB_UI);
114 EXPECT_FALSE(rvh()->GetEnabledBindings() & BINDINGS_POLICY_WEB_UI);
creis@chromium.org8ae91352012-03-15 17:06:00115}
116
tony@chromium.org8c4575b2011-06-07 21:11:03117class MockDraggingRenderViewHostDelegateView
jam@chromium.org18082852012-10-29 19:35:57118 : public RenderViewHostDelegateView {
tony@chromium.org8c4575b2011-06-07 21:11:03119 public:
120 virtual ~MockDraggingRenderViewHostDelegateView() {}
aruslan@chromium.org24d2b172012-05-26 00:54:12121 virtual void ShowPopupMenu(const gfx::Rect& bounds,
122 int item_height,
123 double item_font_size,
124 int selected_item,
darin@chromium.org54bf9952013-07-17 06:43:20125 const std::vector<MenuItem>& items,
aruslan@chromium.org24d2b172012-05-26 00:54:12126 bool right_aligned,
127 bool allow_multiple_selection) OVERRIDE {}
darin@chromium.orgdc293a72013-07-01 11:11:22128 virtual void StartDragging(const DropData& drop_data,
tony@chromium.org8c4575b2011-06-07 21:11:03129 WebKit::WebDragOperationsMask allowed_ops,
pkotwicz@chromium.orgdeb6bb72012-07-09 20:16:59130 const gfx::ImageSkia& image,
varunjain@chromium.org41d86852012-11-07 12:23:24131 const gfx::Vector2d& image_offset,
132 const DragEventSourceInfo& event_info) OVERRIDE {
tony@chromium.org8c4575b2011-06-07 21:11:03133 drag_url_ = drop_data.url;
134 html_base_url_ = drop_data.html_base_url;
135 }
aruslan@chromium.org24d2b172012-05-26 00:54:12136 virtual void UpdateDragCursor(WebKit::WebDragOperation operation) OVERRIDE {}
137 virtual void GotFocus() OVERRIDE {}
138 virtual void TakeFocus(bool reverse) OVERRIDE {}
tony@chromium.org8c4575b2011-06-07 21:11:03139 virtual void UpdatePreferredSize(const gfx::Size& pref_size) {}
140
141 GURL drag_url() {
142 return drag_url_;
143 }
144
145 GURL html_base_url() {
146 return html_base_url_;
147 }
148
149 private:
150 GURL drag_url_;
151 GURL html_base_url_;
152};
153
154TEST_F(RenderViewHostTest, StartDragging) {
joi@chromium.org80e776ae2012-03-23 16:17:20155 TestWebContents* web_contents = contents();
jam@chromium.org5a3bdf52012-05-24 15:12:57156 MockDraggingRenderViewHostDelegateView delegate_view;
157 web_contents->set_delegate_view(&delegate_view);
tony@chromium.org8c4575b2011-06-07 21:11:03158
darin@chromium.orgdc293a72013-07-01 11:11:22159 DropData drop_data;
tony@chromium.org8c4575b2011-06-07 21:11:03160 GURL file_url = GURL("file:///home/user/secrets.txt");
161 drop_data.url = file_url;
162 drop_data.html_base_url = file_url;
jam@chromium.org3d9ec502013-01-02 22:05:25163 test_rvh()->TestOnStartDragging(drop_data);
thestig@chromium.org685c8af2013-08-06 03:10:15164 EXPECT_EQ(GURL(kAboutBlankURL), delegate_view.drag_url());
165 EXPECT_EQ(GURL(kAboutBlankURL), delegate_view.html_base_url());
tony@chromium.org8c4575b2011-06-07 21:11:03166
167 GURL http_url = GURL("http://www.domain.com/index.html");
168 drop_data.url = http_url;
169 drop_data.html_base_url = http_url;
jam@chromium.org3d9ec502013-01-02 22:05:25170 test_rvh()->TestOnStartDragging(drop_data);
jam@chromium.org5a3bdf52012-05-24 15:12:57171 EXPECT_EQ(http_url, delegate_view.drag_url());
172 EXPECT_EQ(http_url, delegate_view.html_base_url());
tony@chromium.org8c4575b2011-06-07 21:11:03173
174 GURL https_url = GURL("https://www.domain.com/index.html");
175 drop_data.url = https_url;
176 drop_data.html_base_url = https_url;
jam@chromium.org3d9ec502013-01-02 22:05:25177 test_rvh()->TestOnStartDragging(drop_data);
jam@chromium.org5a3bdf52012-05-24 15:12:57178 EXPECT_EQ(https_url, delegate_view.drag_url());
179 EXPECT_EQ(https_url, delegate_view.html_base_url());
maf@chromium.orgd36ccf52011-06-22 00:23:48180
181 GURL javascript_url = GURL("javascript:alert('I am a bookmarklet')");
182 drop_data.url = javascript_url;
183 drop_data.html_base_url = http_url;
jam@chromium.org3d9ec502013-01-02 22:05:25184 test_rvh()->TestOnStartDragging(drop_data);
jam@chromium.org5a3bdf52012-05-24 15:12:57185 EXPECT_EQ(javascript_url, delegate_view.drag_url());
186 EXPECT_EQ(http_url, delegate_view.html_base_url());
tony@chromium.org8c4575b2011-06-07 21:11:03187}
188
tsepez@chromium.orgc80ed0f2012-02-03 19:37:23189TEST_F(RenderViewHostTest, DragEnteredFileURLsStillBlocked) {
darin@chromium.orgdc293a72013-07-01 11:11:22190 DropData dropped_data;
tsepez@chromium.orgc80ed0f2012-02-03 19:37:23191 gfx::Point client_point;
192 gfx::Point screen_point;
kinuko@chromium.org03f39972012-06-26 14:50:27193 // We use "//foo/bar" path (rather than "/foo/bar") since dragged paths are
194 // expected to be absolute on any platforms.
brettw@chromium.org2dec8ec2013-02-07 19:20:34195 base::FilePath highlighted_file_path(FILE_PATH_LITERAL("//tmp/foo.html"));
196 base::FilePath dragged_file_path(FILE_PATH_LITERAL("//tmp/image.jpg"));
197 base::FilePath sensitive_file_path(FILE_PATH_LITERAL("//etc/passwd"));
tsepez@chromium.orgdc67e1c32012-06-08 00:10:40198 GURL highlighted_file_url = net::FilePathToFileURL(highlighted_file_path);
199 GURL dragged_file_url = net::FilePathToFileURL(dragged_file_path);
200 GURL sensitive_file_url = net::FilePathToFileURL(sensitive_file_path);
201 dropped_data.url = highlighted_file_url;
ajwong@chromium.orgef1bbf632013-10-24 22:54:08202 dropped_data.filenames.push_back(DropData::FileInfo(
203 UTF8ToUTF16(dragged_file_path.AsUTF8Unsafe()), string16()));
tsepez@chromium.orgdc67e1c32012-06-08 00:10:40204
tsepez@chromium.orgc80ed0f2012-02-03 19:37:23205 rvh()->DragTargetDragEnter(dropped_data, client_point, screen_point,
tsepez@chromium.orgdc67e1c32012-06-08 00:10:40206 WebKit::WebDragOperationNone, 0);
207
208 int id = process()->GetID();
209 ChildProcessSecurityPolicyImpl* policy =
210 ChildProcessSecurityPolicyImpl::GetInstance();
211
212 EXPECT_FALSE(policy->CanRequestURL(id, highlighted_file_url));
213 EXPECT_FALSE(policy->CanReadFile(id, highlighted_file_path));
214 EXPECT_TRUE(policy->CanRequestURL(id, dragged_file_url));
215 EXPECT_TRUE(policy->CanReadFile(id, dragged_file_path));
216 EXPECT_FALSE(policy->CanRequestURL(id, sensitive_file_url));
217 EXPECT_FALSE(policy->CanReadFile(id, sensitive_file_path));
tsepez@chromium.orgc80ed0f2012-02-03 19:37:23218}
219
cpu@google.comd2fc65d2009-07-15 01:36:10220// The test that follow trigger DCHECKS in debug build.
nsylvain@chromium.org20960e072011-09-20 20:59:01221#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
cpu@google.comd2fc65d2009-07-15 01:36:10222
223// Test that when we fail to de-serialize a message, RenderViewHost calls the
224// ReceivedBadMessage() handler.
225TEST_F(RenderViewHostTest, BadMessageHandlerRenderViewHost) {
226 EXPECT_EQ(0, process()->bad_msg_count());
227 // craft an incorrect ViewHostMsg_UpdateTargetURL message. The real one has
228 // two payload items but the one we construct has none.
229 IPC::Message message(0, ViewHostMsg_UpdateTargetURL::ID,
230 IPC::Message::PRIORITY_NORMAL);
joi@chromium.org446c1a302012-03-22 19:05:05231 test_rvh()->OnMessageReceived(message);
cpu@google.comd2fc65d2009-07-15 01:36:10232 EXPECT_EQ(1, process()->bad_msg_count());
233}
234
235// Test that when we fail to de-serialize a message, RenderWidgetHost calls the
236// ReceivedBadMessage() handler.
237TEST_F(RenderViewHostTest, BadMessageHandlerRenderWidgetHost) {
238 EXPECT_EQ(0, process()->bad_msg_count());
darin@chromium.org53d3f302009-12-21 04:42:05239 // craft an incorrect ViewHostMsg_UpdateRect message. The real one has
cpu@google.comd2fc65d2009-07-15 01:36:10240 // one payload item but the one we construct has none.
darin@chromium.org53d3f302009-12-21 04:42:05241 IPC::Message message(0, ViewHostMsg_UpdateRect::ID,
cpu@google.comd2fc65d2009-07-15 01:36:10242 IPC::Message::PRIORITY_NORMAL);
joi@chromium.org446c1a302012-03-22 19:05:05243 test_rvh()->OnMessageReceived(message);
cpu@google.comd2fc65d2009-07-15 01:36:10244 EXPECT_EQ(1, process()->bad_msg_count());
245}
246
jam@chromium.org3d9ec502013-01-02 22:05:25247// Test that OnInputEventAck() detects bad messages.
cpu@google.com8faa5a12009-07-17 17:17:43248TEST_F(RenderViewHostTest, BadMessageHandlerInputEventAck) {
249 EXPECT_EQ(0, process()->bad_msg_count());
jamesr@chromium.orgc084330e02013-04-27 01:08:15250 // InputHostMsg_HandleInputEvent_ACK is defined taking 0 params but
cpu@google.com8faa5a12009-07-17 17:17:43251 // the code actually expects it to have at least one int para, this this
252 // bogus message will not fail at de-serialization but should fail in
jam@chromium.org3d9ec502013-01-02 22:05:25253 // OnInputEventAck() processing.
jamesr@chromium.orgc084330e02013-04-27 01:08:15254 IPC::Message message(0, InputHostMsg_HandleInputEvent_ACK::ID,
cpu@google.com8faa5a12009-07-17 17:17:43255 IPC::Message::PRIORITY_NORMAL);
joi@chromium.org446c1a302012-03-22 19:05:05256 test_rvh()->OnMessageReceived(message);
cpu@google.com8faa5a12009-07-17 17:17:43257 EXPECT_EQ(1, process()->bad_msg_count());
258}
259
nsylvain@chromium.org20960e072011-09-20 20:59:01260#endif
jam@chromium.org18082852012-10-29 19:35:57261
tsepez@chromium.org6136d062013-05-08 19:31:34262TEST_F(RenderViewHostTest, MessageWithBadHistoryItemFiles) {
263 base::FilePath file_path;
264 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &file_path));
265 file_path = file_path.AppendASCII("foo");
266 EXPECT_EQ(0, process()->bad_msg_count());
267 test_rvh()->TestOnUpdateStateWithFile(process()->GetID(), file_path);
268 EXPECT_EQ(1, process()->bad_msg_count());
269
gbillock@chromium.orgbfcf1e92013-07-11 04:37:25270 ChildProcessSecurityPolicyImpl::GetInstance()->GrantReadFile(
271 process()->GetID(), file_path);
tsepez@chromium.org6136d062013-05-08 19:31:34272 test_rvh()->TestOnUpdateStateWithFile(process()->GetID(), file_path);
273 EXPECT_EQ(1, process()->bad_msg_count());
274}
275
276TEST_F(RenderViewHostTest, NavigationWithBadHistoryItemFiles) {
277 GURL url("http://www.google.com");
278 base::FilePath file_path;
279 EXPECT_TRUE(PathService::Get(base::DIR_TEMP, &file_path));
280 file_path = file_path.AppendASCII("bar");
281 EXPECT_EQ(0, process()->bad_msg_count());
282 test_rvh()->SendNavigateWithFile(1, url, file_path);
283 EXPECT_EQ(1, process()->bad_msg_count());
284
gbillock@chromium.orgbfcf1e92013-07-11 04:37:25285 ChildProcessSecurityPolicyImpl::GetInstance()->GrantReadFile(
286 process()->GetID(), file_path);
tsepez@chromium.org6136d062013-05-08 19:31:34287 test_rvh()->SendNavigateWithFile(process()->GetID(), url, file_path);
288 EXPECT_EQ(1, process()->bad_msg_count());
289}
290
ajwong@chromium.org1c2052f2013-08-28 08:24:34291TEST_F(RenderViewHostTest, RoutingIdSane) {
292 EXPECT_EQ(test_rvh()->GetProcess(),
293 test_rvh()->main_render_frame_host()->GetProcess());
294 EXPECT_NE(test_rvh()->GetRoutingID(),
295 test_rvh()->main_render_frame_host()->routing_id());
296}
297
jam@chromium.org18082852012-10-29 19:35:57298} // namespace content