blob: 5bbed1f469ed84c29499ae8240ffd302fb43ca42 [file] [log] [blame]
[email protected]51ffaf72012-05-09 21:00:491// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]5e56df82011-04-18 17:00:152// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/common/content_settings.h"
6#include "chrome/common/render_messages.h"
7#include "chrome/renderer/content_settings_observer.h"
[email protected]c6d068ff2011-10-14 17:28:238#include "chrome/test/base/chrome_render_view_test.h"
[email protected]068970d2011-10-11 00:05:169#include "content/public/renderer/render_view.h"
[email protected]4dad4a82011-04-27 15:44:5210#include "ipc/ipc_message_macros.h"
11#include "testing/gmock/include/gmock/gmock.h"
[email protected]5e56df82011-04-18 17:00:1512#include "testing/gtest/include/gtest/gtest.h"
[email protected]2255a9332013-06-17 05:12:3113#include "third_party/WebKit/public/web/WebView.h"
[email protected]566fa0f2011-06-01 23:26:0614
15using testing::_;
16using testing::DeleteArg;
[email protected]5e56df82011-04-18 17:00:1517
[email protected]4dad4a82011-04-27 15:44:5218namespace {
19
20class MockContentSettingsObserver : public ContentSettingsObserver {
21 public:
[email protected]a86c0e962013-12-17 17:10:3922 explicit MockContentSettingsObserver(content::RenderFrame* render_frame);
[email protected]4dad4a82011-04-27 15:44:5223
24 virtual bool Send(IPC::Message* message);
25
[email protected]d49f6f7e2013-12-11 15:47:3626 MOCK_METHOD1(OnContentBlocked,
27 void(ContentSettingsType));
[email protected]566fa0f2011-06-01 23:26:0628
29 MOCK_METHOD5(OnAllowDOMStorage,
30 void(int, const GURL&, const GURL&, bool, IPC::Message*));
[email protected]c14406092011-10-27 13:43:4231 GURL image_url_;
32 std::string image_origin_;
[email protected]4dad4a82011-04-27 15:44:5233};
34
35MockContentSettingsObserver::MockContentSettingsObserver(
[email protected]a86c0e962013-12-17 17:10:3936 content::RenderFrame* render_frame)
37 : ContentSettingsObserver(render_frame, NULL),
[email protected]c14406092011-10-27 13:43:4238 image_url_("http://www.foo.com/image.jpg"),
39 image_origin_("http://www.foo.com") {
[email protected]4dad4a82011-04-27 15:44:5240}
41
42bool MockContentSettingsObserver::Send(IPC::Message* message) {
43 IPC_BEGIN_MESSAGE_MAP(MockContentSettingsObserver, *message)
[email protected]2ccf45c2011-08-19 23:35:5044 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ContentBlocked, OnContentBlocked)
45 IPC_MESSAGE_HANDLER_DELAY_REPLY(ChromeViewHostMsg_AllowDOMStorage,
[email protected]566fa0f2011-06-01 23:26:0646 OnAllowDOMStorage)
[email protected]4dad4a82011-04-27 15:44:5247 IPC_MESSAGE_UNHANDLED(ADD_FAILURE())
48 IPC_END_MESSAGE_MAP()
49
50 // Our super class deletes the message.
[email protected]a86c0e962013-12-17 17:10:3951 return RenderFrameObserver::Send(message);
[email protected]4dad4a82011-04-27 15:44:5252}
53
54} // namespace
55
[email protected]c6d068ff2011-10-14 17:28:2356TEST_F(ChromeRenderViewTest, DidBlockContentType) {
[email protected]a86c0e962013-12-17 17:10:3957 MockContentSettingsObserver observer(view_->GetMainRenderFrame());
[email protected]4dad4a82011-04-27 15:44:5258 EXPECT_CALL(observer,
[email protected]d49f6f7e2013-12-11 15:47:3659 OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
60 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES);
[email protected]4dad4a82011-04-27 15:44:5261
62 // Blocking the same content type a second time shouldn't send a notification.
[email protected]d49f6f7e2013-12-11 15:47:3663 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES);
[email protected]4dad4a82011-04-27 15:44:5264 ::testing::Mock::VerifyAndClearExpectations(&observer);
[email protected]4dad4a82011-04-27 15:44:5265}
66
[email protected]566fa0f2011-06-01 23:26:0667// Tests that multiple invokations of AllowDOMStorage result in a single IPC.
[email protected]2955280a2011-11-15 14:52:1668// Fails due to http://crbug.com/104300
[email protected]984aa49c2012-05-20 20:44:1369TEST_F(ChromeRenderViewTest, DISABLED_AllowDOMStorage) {
[email protected]566fa0f2011-06-01 23:26:0670 // Load some HTML, so we have a valid security origin.
71 LoadHTML("<html></html>");
[email protected]a86c0e962013-12-17 17:10:3972 MockContentSettingsObserver observer(view_->GetMainRenderFrame());
[email protected]566fa0f2011-06-01 23:26:0673 ON_CALL(observer,
74 OnAllowDOMStorage(_, _, _, _, _)).WillByDefault(DeleteArg<4>());
75 EXPECT_CALL(observer,
76 OnAllowDOMStorage(_, _, _, _, _));
[email protected]098ac6f2014-04-08 15:02:4477 observer.allowStorage(true);
[email protected]566fa0f2011-06-01 23:26:0678
79 // Accessing localStorage from the same origin again shouldn't result in a
80 // new IPC.
[email protected]098ac6f2014-04-08 15:02:4481 observer.allowStorage(true);
[email protected]566fa0f2011-06-01 23:26:0682 ::testing::Mock::VerifyAndClearExpectations(&observer);
83}
84
[email protected]5e56df82011-04-18 17:00:1585// Regression test for http://crbug.com/35011
[email protected]c6d068ff2011-10-14 17:28:2386TEST_F(ChromeRenderViewTest, JSBlockSentAfterPageLoad) {
[email protected]5e56df82011-04-18 17:00:1587 // 1. Load page with JS.
88 std::string html = "<html>"
[email protected]4dad4a82011-04-27 15:44:5289 "<head>"
90 "<script>document.createElement('div');</script>"
91 "</head>"
92 "<body>"
93 "</body>"
94 "</html>";
[email protected]c6d068ff2011-10-14 17:28:2395 render_thread_->sink().ClearMessages();
[email protected]5e56df82011-04-18 17:00:1596 LoadHTML(html.c_str());
97
98 // 2. Block JavaScript.
[email protected]edece212011-11-16 11:56:5699 RendererContentSettingRules content_setting_rules;
100 ContentSettingsForOneType& script_setting_rules =
101 content_setting_rules.script_rules;
102 script_setting_rules.push_back(
[email protected]007b3f82013-04-09 08:46:45103 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
104 ContentSettingsPattern::Wildcard(),
105 CONTENT_SETTING_BLOCK,
106 std::string(),
107 false));
[email protected]a86c0e962013-12-17 17:10:39108 ContentSettingsObserver* observer = ContentSettingsObserver::Get(
109 view_->GetMainRenderFrame());
[email protected]edece212011-11-16 11:56:56110 observer->SetContentSettingRules(&content_setting_rules);
[email protected]5e56df82011-04-18 17:00:15111
112 // Make sure no pending messages are in the queue.
113 ProcessPendingMessages();
[email protected]c6d068ff2011-10-14 17:28:23114 render_thread_->sink().ClearMessages();
[email protected]5e56df82011-04-18 17:00:15115
116 // 3. Reload page.
[email protected]5e56df82011-04-18 17:00:15117 std::string url_str = "data:text/html;charset=utf-8,";
118 url_str.append(html);
119 GURL url(url_str);
[email protected]bcd90b72011-10-17 23:19:57120 Reload(url);
[email protected]5e56df82011-04-18 17:00:15121 ProcessPendingMessages();
122
123 // 4. Verify that the notification that javascript was blocked is sent after
[email protected]6be50d72014-05-08 23:49:40124 // the navigation notification is sent.
[email protected]5e56df82011-04-18 17:00:15125 int navigation_index = -1;
126 int block_index = -1;
[email protected]c6d068ff2011-10-14 17:28:23127 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) {
128 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i);
[email protected]bcd90b72011-10-17 23:19:57129 if (msg->type() == GetNavigationIPCType())
[email protected]5e56df82011-04-18 17:00:15130 navigation_index = i;
[email protected]2ccf45c2011-08-19 23:35:50131 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID)
[email protected]5e56df82011-04-18 17:00:15132 block_index = i;
133 }
134 EXPECT_NE(-1, navigation_index);
135 EXPECT_NE(-1, block_index);
136 EXPECT_LT(navigation_index, block_index);
137}
[email protected]134efc32011-08-02 18:14:21138
[email protected]c6d068ff2011-10-14 17:28:23139TEST_F(ChromeRenderViewTest, PluginsTemporarilyAllowed) {
[email protected]134efc32011-08-02 18:14:21140 // Load some HTML.
141 LoadHTML("<html>Foo</html>");
142
[email protected]51ffaf72012-05-09 21:00:49143 std::string foo_plugin = "foo";
144 std::string bar_plugin = "bar";
[email protected]134efc32011-08-02 18:14:21145
[email protected]a86c0e962013-12-17 17:10:39146 ContentSettingsObserver* observer =
147 ContentSettingsObserver::Get(view_->GetMainRenderFrame());
[email protected]51ffaf72012-05-09 21:00:49148 EXPECT_FALSE(observer->IsPluginTemporarilyAllowed(foo_plugin));
149
150 // Temporarily allow the "foo" plugin.
[email protected]a86c0e962013-12-17 17:10:39151 observer->OnLoadBlockedPlugins(foo_plugin);
[email protected]51ffaf72012-05-09 21:00:49152 EXPECT_TRUE(observer->IsPluginTemporarilyAllowed(foo_plugin));
153 EXPECT_FALSE(observer->IsPluginTemporarilyAllowed(bar_plugin));
[email protected]134efc32011-08-02 18:14:21154
155 // Simulate a navigation within the page.
[email protected]068970d2011-10-11 00:05:16156 DidNavigateWithinPage(GetMainFrame(), true);
[email protected]51ffaf72012-05-09 21:00:49157 EXPECT_TRUE(observer->IsPluginTemporarilyAllowed(foo_plugin));
158 EXPECT_FALSE(observer->IsPluginTemporarilyAllowed(bar_plugin));
[email protected]134efc32011-08-02 18:14:21159
160 // Navigate to a different page.
161 LoadHTML("<html>Bar</html>");
[email protected]51ffaf72012-05-09 21:00:49162 EXPECT_FALSE(observer->IsPluginTemporarilyAllowed(foo_plugin));
163 EXPECT_FALSE(observer->IsPluginTemporarilyAllowed(bar_plugin));
164
165 // Temporarily allow all plugins.
[email protected]a86c0e962013-12-17 17:10:39166 observer->OnLoadBlockedPlugins(std::string());
[email protected]51ffaf72012-05-09 21:00:49167 EXPECT_TRUE(observer->IsPluginTemporarilyAllowed(foo_plugin));
168 EXPECT_TRUE(observer->IsPluginTemporarilyAllowed(bar_plugin));
[email protected]134efc32011-08-02 18:14:21169}
[email protected]c14406092011-10-27 13:43:42170
171TEST_F(ChromeRenderViewTest, ImagesBlockedByDefault) {
[email protected]a86c0e962013-12-17 17:10:39172 MockContentSettingsObserver mock_observer(view_->GetMainRenderFrame());
[email protected]c14406092011-10-27 13:43:42173
174 // Load some HTML.
175 LoadHTML("<html>Foo</html>");
176
177 // Set the default image blocking setting.
[email protected]edece212011-11-16 11:56:56178 RendererContentSettingRules content_setting_rules;
179 ContentSettingsForOneType& image_setting_rules =
180 content_setting_rules.image_rules;
[email protected]c14406092011-10-27 13:43:42181 image_setting_rules.push_back(
182 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
183 ContentSettingsPattern::Wildcard(),
184 CONTENT_SETTING_BLOCK,
[email protected]007b3f82013-04-09 08:46:45185 std::string(),
[email protected]c14406092011-10-27 13:43:42186 false));
187
[email protected]a86c0e962013-12-17 17:10:39188 ContentSettingsObserver* observer = ContentSettingsObserver::Get(
189 view_->GetMainRenderFrame());
[email protected]edece212011-11-16 11:56:56190 observer->SetContentSettingRules(&content_setting_rules);
[email protected]c14406092011-10-27 13:43:42191 EXPECT_CALL(mock_observer,
[email protected]d49f6f7e2013-12-11 15:47:36192 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES));
[email protected]098ac6f2014-04-08 15:02:44193 EXPECT_FALSE(observer->allowImage(true, mock_observer.image_url_));
[email protected]c14406092011-10-27 13:43:42194 ::testing::Mock::VerifyAndClearExpectations(&observer);
195
196 // Create an exception which allows the image.
197 image_setting_rules.insert(
198 image_setting_rules.begin(),
199 ContentSettingPatternSource(
200 ContentSettingsPattern::Wildcard(),
201 ContentSettingsPattern::FromString(mock_observer.image_origin_),
202 CONTENT_SETTING_ALLOW,
[email protected]007b3f82013-04-09 08:46:45203 std::string(),
[email protected]c14406092011-10-27 13:43:42204 false));
205
206 EXPECT_CALL(
207 mock_observer,
[email protected]d49f6f7e2013-12-11 15:47:36208 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES)).Times(0);
[email protected]098ac6f2014-04-08 15:02:44209 EXPECT_TRUE(observer->allowImage(true, mock_observer.image_url_));
[email protected]c14406092011-10-27 13:43:42210 ::testing::Mock::VerifyAndClearExpectations(&observer);
211}
212
213TEST_F(ChromeRenderViewTest, ImagesAllowedByDefault) {
[email protected]a86c0e962013-12-17 17:10:39214 MockContentSettingsObserver mock_observer(view_->GetMainRenderFrame());
[email protected]c14406092011-10-27 13:43:42215
216 // Load some HTML.
217 LoadHTML("<html>Foo</html>");
218
219 // Set the default image blocking setting.
[email protected]edece212011-11-16 11:56:56220 RendererContentSettingRules content_setting_rules;
221 ContentSettingsForOneType& image_setting_rules =
222 content_setting_rules.image_rules;
[email protected]c14406092011-10-27 13:43:42223 image_setting_rules.push_back(
224 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
225 ContentSettingsPattern::Wildcard(),
226 CONTENT_SETTING_ALLOW,
[email protected]007b3f82013-04-09 08:46:45227 std::string(),
[email protected]c14406092011-10-27 13:43:42228 false));
229
[email protected]a86c0e962013-12-17 17:10:39230 ContentSettingsObserver* observer =
231 ContentSettingsObserver::Get(view_->GetMainRenderFrame());
[email protected]edece212011-11-16 11:56:56232 observer->SetContentSettingRules(&content_setting_rules);
[email protected]c14406092011-10-27 13:43:42233 EXPECT_CALL(
234 mock_observer,
[email protected]d49f6f7e2013-12-11 15:47:36235 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES)).Times(0);
[email protected]098ac6f2014-04-08 15:02:44236 EXPECT_TRUE(observer->allowImage(true, mock_observer.image_url_));
[email protected]c14406092011-10-27 13:43:42237 ::testing::Mock::VerifyAndClearExpectations(&observer);
238
239 // Create an exception which blocks the image.
240 image_setting_rules.insert(
241 image_setting_rules.begin(),
242 ContentSettingPatternSource(
243 ContentSettingsPattern::Wildcard(),
244 ContentSettingsPattern::FromString(mock_observer.image_origin_),
245 CONTENT_SETTING_BLOCK,
[email protected]007b3f82013-04-09 08:46:45246 std::string(),
[email protected]c14406092011-10-27 13:43:42247 false));
248 EXPECT_CALL(mock_observer,
[email protected]d49f6f7e2013-12-11 15:47:36249 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES));
[email protected]098ac6f2014-04-08 15:02:44250 EXPECT_FALSE(observer->allowImage(true, mock_observer.image_url_));
[email protected]c14406092011-10-27 13:43:42251 ::testing::Mock::VerifyAndClearExpectations(&observer);
252}
[email protected]edece212011-11-16 11:56:56253
254TEST_F(ChromeRenderViewTest, ContentSettingsBlockScripts) {
255 // Set the content settings for scripts.
256 RendererContentSettingRules content_setting_rules;
257 ContentSettingsForOneType& script_setting_rules =
258 content_setting_rules.script_rules;
259 script_setting_rules.push_back(
[email protected]007b3f82013-04-09 08:46:45260 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
261 ContentSettingsPattern::Wildcard(),
262 CONTENT_SETTING_BLOCK,
263 std::string(),
264 false));
[email protected]edece212011-11-16 11:56:56265
[email protected]a86c0e962013-12-17 17:10:39266 ContentSettingsObserver* observer =
267 ContentSettingsObserver::Get(view_->GetMainRenderFrame());
[email protected]edece212011-11-16 11:56:56268 observer->SetContentSettingRules(&content_setting_rules);
269
270 // Load a page which contains a script.
271 std::string html = "<html>"
272 "<head>"
273 "<script src='data:foo'></script>"
274 "</head>"
275 "<body>"
276 "</body>"
277 "</html>";
278 LoadHTML(html.c_str());
279
280 // Verify that the script was blocked.
281 bool was_blocked = false;
282 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) {
283 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i);
284 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID)
285 was_blocked = true;
286 }
287 EXPECT_TRUE(was_blocked);
288}
289
290TEST_F(ChromeRenderViewTest, ContentSettingsAllowScripts) {
291 // Set the content settings for scripts.
292 RendererContentSettingRules content_setting_rules;
293 ContentSettingsForOneType& script_setting_rules =
294 content_setting_rules.script_rules;
295 script_setting_rules.push_back(
[email protected]007b3f82013-04-09 08:46:45296 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
297 ContentSettingsPattern::Wildcard(),
298 CONTENT_SETTING_ALLOW,
299 std::string(),
300 false));
[email protected]edece212011-11-16 11:56:56301
[email protected]a86c0e962013-12-17 17:10:39302 ContentSettingsObserver* observer =
303 ContentSettingsObserver::Get(view_->GetMainRenderFrame());
[email protected]edece212011-11-16 11:56:56304 observer->SetContentSettingRules(&content_setting_rules);
305
306 // Load a page which contains a script.
307 std::string html = "<html>"
308 "<head>"
309 "<script src='data:foo'></script>"
310 "</head>"
311 "<body>"
312 "</body>"
313 "</html>";
314 LoadHTML(html.c_str());
315
316 // Verify that the script was not blocked.
317 bool was_blocked = false;
318 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) {
319 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i);
320 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID)
321 was_blocked = true;
322 }
323 EXPECT_FALSE(was_blocked);
324}
[email protected]6f9f7d82011-12-08 12:13:34325
326TEST_F(ChromeRenderViewTest, ContentSettingsInterstitialPages) {
[email protected]a86c0e962013-12-17 17:10:39327 MockContentSettingsObserver mock_observer(view_->GetMainRenderFrame());
[email protected]6f9f7d82011-12-08 12:13:34328 // Block scripts.
329 RendererContentSettingRules content_setting_rules;
330 ContentSettingsForOneType& script_setting_rules =
331 content_setting_rules.script_rules;
332 script_setting_rules.push_back(
[email protected]007b3f82013-04-09 08:46:45333 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
334 ContentSettingsPattern::Wildcard(),
335 CONTENT_SETTING_BLOCK,
336 std::string(),
337 false));
[email protected]6f9f7d82011-12-08 12:13:34338 // Block images.
339 ContentSettingsForOneType& image_setting_rules =
340 content_setting_rules.image_rules;
341 image_setting_rules.push_back(
[email protected]007b3f82013-04-09 08:46:45342 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
343 ContentSettingsPattern::Wildcard(),
344 CONTENT_SETTING_BLOCK,
345 std::string(),
346 false));
[email protected]6f9f7d82011-12-08 12:13:34347
[email protected]a86c0e962013-12-17 17:10:39348 ContentSettingsObserver* observer =
349 ContentSettingsObserver::Get(view_->GetMainRenderFrame());
[email protected]6f9f7d82011-12-08 12:13:34350 observer->SetContentSettingRules(&content_setting_rules);
[email protected]85f0a572012-02-07 22:20:13351 observer->OnSetAsInterstitial();
[email protected]6f9f7d82011-12-08 12:13:34352
353 // Load a page which contains a script.
354 std::string html = "<html>"
355 "<head>"
356 "<script src='data:foo'></script>"
357 "</head>"
358 "<body>"
359 "</body>"
360 "</html>";
361 LoadHTML(html.c_str());
362
363 // Verify that the script was allowed.
364 bool was_blocked = false;
365 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) {
366 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i);
367 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID)
368 was_blocked = true;
369 }
370 EXPECT_FALSE(was_blocked);
371
372 // Verify that images are allowed.
373 EXPECT_CALL(
374 mock_observer,
[email protected]d49f6f7e2013-12-11 15:47:36375 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES)).Times(0);
[email protected]098ac6f2014-04-08 15:02:44376 EXPECT_TRUE(observer->allowImage(true, mock_observer.image_url_));
[email protected]6f9f7d82011-12-08 12:13:34377 ::testing::Mock::VerifyAndClearExpectations(&observer);
378}