1 // Copyright (c) 2012 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.
5 #include "chrome/common/content_settings.h"
6 #include "chrome/common/render_messages.h"
7 #include "chrome/renderer/content_settings_observer.h"
8 #include "chrome/test/base/chrome_render_view_test.h"
9 #include "content/public/renderer/render_view.h"
10 #include "ipc/ipc_message_macros.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/WebKit/public/web/WebView.h"
16 using testing::DeleteArg
;
20 class MockContentSettingsObserver
: public ContentSettingsObserver
{
22 explicit MockContentSettingsObserver(content::RenderView
* render_view
);
24 virtual bool Send(IPC::Message
* message
);
26 MOCK_METHOD2(OnContentBlocked
,
27 void(ContentSettingsType
, const std::string
&));
29 MOCK_METHOD5(OnAllowDOMStorage
,
30 void(int, const GURL
&, const GURL
&, bool, IPC::Message
*));
32 std::string image_origin_
;
35 MockContentSettingsObserver::MockContentSettingsObserver(
36 content::RenderView
* render_view
)
37 : ContentSettingsObserver(render_view
),
38 image_url_("http://www.foo.com/image.jpg"),
39 image_origin_("http://www.foo.com") {
42 bool MockContentSettingsObserver::Send(IPC::Message
* message
) {
43 IPC_BEGIN_MESSAGE_MAP(MockContentSettingsObserver
, *message
)
44 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ContentBlocked
, OnContentBlocked
)
45 IPC_MESSAGE_HANDLER_DELAY_REPLY(ChromeViewHostMsg_AllowDOMStorage
,
47 IPC_MESSAGE_UNHANDLED(ADD_FAILURE())
50 // Our super class deletes the message.
51 return RenderViewObserver::Send(message
);
56 TEST_F(ChromeRenderViewTest
, DidBlockContentType
) {
57 MockContentSettingsObserver
observer(view_
);
59 OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
, std::string()));
60 observer
.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES
, std::string());
62 // Blocking the same content type a second time shouldn't send a notification.
63 observer
.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES
, std::string());
64 ::testing::Mock::VerifyAndClearExpectations(&observer
);
66 // Blocking two different plugins should send two notifications.
67 std::string kFooPlugin
= "foo";
68 std::string kBarPlugin
= "bar";
70 OnContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS
, kFooPlugin
));
72 OnContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS
, kBarPlugin
));
73 observer
.DidBlockContentType(CONTENT_SETTINGS_TYPE_PLUGINS
, kFooPlugin
);
74 observer
.DidBlockContentType(CONTENT_SETTINGS_TYPE_PLUGINS
, kBarPlugin
);
77 // Tests that multiple invokations of AllowDOMStorage result in a single IPC.
78 // Fails due to http://crbug.com/104300
79 TEST_F(ChromeRenderViewTest
, DISABLED_AllowDOMStorage
) {
80 // Load some HTML, so we have a valid security origin.
81 LoadHTML("<html></html>");
82 MockContentSettingsObserver
observer(view_
);
84 OnAllowDOMStorage(_
, _
, _
, _
, _
)).WillByDefault(DeleteArg
<4>());
86 OnAllowDOMStorage(_
, _
, _
, _
, _
));
87 observer
.AllowStorage(view_
->GetWebView()->focusedFrame(), true);
89 // Accessing localStorage from the same origin again shouldn't result in a
91 observer
.AllowStorage(view_
->GetWebView()->focusedFrame(), true);
92 ::testing::Mock::VerifyAndClearExpectations(&observer
);
95 // Regression test for http://crbug.com/35011
96 TEST_F(ChromeRenderViewTest
, JSBlockSentAfterPageLoad
) {
97 // 1. Load page with JS.
98 std::string html
= "<html>"
100 "<script>document.createElement('div');</script>"
105 render_thread_
->sink().ClearMessages();
106 LoadHTML(html
.c_str());
108 // 2. Block JavaScript.
109 RendererContentSettingRules content_setting_rules
;
110 ContentSettingsForOneType
& script_setting_rules
=
111 content_setting_rules
.script_rules
;
112 script_setting_rules
.push_back(
113 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
114 ContentSettingsPattern::Wildcard(),
115 CONTENT_SETTING_BLOCK
,
118 ContentSettingsObserver
* observer
= ContentSettingsObserver::Get(view_
);
119 observer
->SetContentSettingRules(&content_setting_rules
);
121 // Make sure no pending messages are in the queue.
122 ProcessPendingMessages();
123 render_thread_
->sink().ClearMessages();
126 std::string url_str
= "data:text/html;charset=utf-8,";
127 url_str
.append(html
);
130 ProcessPendingMessages();
132 // 4. Verify that the notification that javascript was blocked is sent after
133 // the navigation notifiction is sent.
134 int navigation_index
= -1;
135 int block_index
= -1;
136 for (size_t i
= 0; i
< render_thread_
->sink().message_count(); ++i
) {
137 const IPC::Message
* msg
= render_thread_
->sink().GetMessageAt(i
);
138 if (msg
->type() == GetNavigationIPCType())
139 navigation_index
= i
;
140 if (msg
->type() == ChromeViewHostMsg_ContentBlocked::ID
)
143 EXPECT_NE(-1, navigation_index
);
144 EXPECT_NE(-1, block_index
);
145 EXPECT_LT(navigation_index
, block_index
);
148 TEST_F(ChromeRenderViewTest
, PluginsTemporarilyAllowed
) {
150 LoadHTML("<html>Foo</html>");
152 std::string foo_plugin
= "foo";
153 std::string bar_plugin
= "bar";
155 ContentSettingsObserver
* observer
= ContentSettingsObserver::Get(view_
);
156 EXPECT_FALSE(observer
->IsPluginTemporarilyAllowed(foo_plugin
));
158 // Temporarily allow the "foo" plugin.
159 OnMessageReceived(ChromeViewMsg_LoadBlockedPlugins(MSG_ROUTING_NONE
,
161 EXPECT_TRUE(observer
->IsPluginTemporarilyAllowed(foo_plugin
));
162 EXPECT_FALSE(observer
->IsPluginTemporarilyAllowed(bar_plugin
));
164 // Simulate a navigation within the page.
165 DidNavigateWithinPage(GetMainFrame(), true);
166 EXPECT_TRUE(observer
->IsPluginTemporarilyAllowed(foo_plugin
));
167 EXPECT_FALSE(observer
->IsPluginTemporarilyAllowed(bar_plugin
));
169 // Navigate to a different page.
170 LoadHTML("<html>Bar</html>");
171 EXPECT_FALSE(observer
->IsPluginTemporarilyAllowed(foo_plugin
));
172 EXPECT_FALSE(observer
->IsPluginTemporarilyAllowed(bar_plugin
));
174 // Temporarily allow all plugins.
175 OnMessageReceived(ChromeViewMsg_LoadBlockedPlugins(MSG_ROUTING_NONE
,
177 EXPECT_TRUE(observer
->IsPluginTemporarilyAllowed(foo_plugin
));
178 EXPECT_TRUE(observer
->IsPluginTemporarilyAllowed(bar_plugin
));
181 TEST_F(ChromeRenderViewTest
, ImagesBlockedByDefault
) {
182 MockContentSettingsObserver
mock_observer(view_
);
185 LoadHTML("<html>Foo</html>");
187 // Set the default image blocking setting.
188 RendererContentSettingRules content_setting_rules
;
189 ContentSettingsForOneType
& image_setting_rules
=
190 content_setting_rules
.image_rules
;
191 image_setting_rules
.push_back(
192 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
193 ContentSettingsPattern::Wildcard(),
194 CONTENT_SETTING_BLOCK
,
198 ContentSettingsObserver
* observer
= ContentSettingsObserver::Get(view_
);
199 observer
->SetContentSettingRules(&content_setting_rules
);
200 EXPECT_CALL(mock_observer
,
201 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
202 EXPECT_FALSE(observer
->AllowImage(GetMainFrame(),
203 true, mock_observer
.image_url_
));
204 ::testing::Mock::VerifyAndClearExpectations(&observer
);
206 // Create an exception which allows the image.
207 image_setting_rules
.insert(
208 image_setting_rules
.begin(),
209 ContentSettingPatternSource(
210 ContentSettingsPattern::Wildcard(),
211 ContentSettingsPattern::FromString(mock_observer
.image_origin_
),
212 CONTENT_SETTING_ALLOW
,
218 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES
, std::string())).Times(0);
219 EXPECT_TRUE(observer
->AllowImage(GetMainFrame(), true,
220 mock_observer
.image_url_
));
221 ::testing::Mock::VerifyAndClearExpectations(&observer
);
224 TEST_F(ChromeRenderViewTest
, ImagesAllowedByDefault
) {
225 MockContentSettingsObserver
mock_observer(view_
);
228 LoadHTML("<html>Foo</html>");
230 // Set the default image blocking setting.
231 RendererContentSettingRules content_setting_rules
;
232 ContentSettingsForOneType
& image_setting_rules
=
233 content_setting_rules
.image_rules
;
234 image_setting_rules
.push_back(
235 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
236 ContentSettingsPattern::Wildcard(),
237 CONTENT_SETTING_ALLOW
,
241 ContentSettingsObserver
* observer
= ContentSettingsObserver::Get(view_
);
242 observer
->SetContentSettingRules(&content_setting_rules
);
245 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES
, std::string())).Times(0);
246 EXPECT_TRUE(observer
->AllowImage(GetMainFrame(), true,
247 mock_observer
.image_url_
));
248 ::testing::Mock::VerifyAndClearExpectations(&observer
);
250 // Create an exception which blocks the image.
251 image_setting_rules
.insert(
252 image_setting_rules
.begin(),
253 ContentSettingPatternSource(
254 ContentSettingsPattern::Wildcard(),
255 ContentSettingsPattern::FromString(mock_observer
.image_origin_
),
256 CONTENT_SETTING_BLOCK
,
259 EXPECT_CALL(mock_observer
,
260 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
261 EXPECT_FALSE(observer
->AllowImage(GetMainFrame(),
262 true, mock_observer
.image_url_
));
263 ::testing::Mock::VerifyAndClearExpectations(&observer
);
266 TEST_F(ChromeRenderViewTest
, ContentSettingsBlockScripts
) {
267 // Set the content settings for scripts.
268 RendererContentSettingRules content_setting_rules
;
269 ContentSettingsForOneType
& script_setting_rules
=
270 content_setting_rules
.script_rules
;
271 script_setting_rules
.push_back(
272 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
273 ContentSettingsPattern::Wildcard(),
274 CONTENT_SETTING_BLOCK
,
278 ContentSettingsObserver
* observer
= ContentSettingsObserver::Get(view_
);
279 observer
->SetContentSettingRules(&content_setting_rules
);
281 // Load a page which contains a script.
282 std::string html
= "<html>"
284 "<script src='data:foo'></script>"
289 LoadHTML(html
.c_str());
291 // Verify that the script was blocked.
292 bool was_blocked
= false;
293 for (size_t i
= 0; i
< render_thread_
->sink().message_count(); ++i
) {
294 const IPC::Message
* msg
= render_thread_
->sink().GetMessageAt(i
);
295 if (msg
->type() == ChromeViewHostMsg_ContentBlocked::ID
)
298 EXPECT_TRUE(was_blocked
);
301 TEST_F(ChromeRenderViewTest
, ContentSettingsAllowScripts
) {
302 // Set the content settings for scripts.
303 RendererContentSettingRules content_setting_rules
;
304 ContentSettingsForOneType
& script_setting_rules
=
305 content_setting_rules
.script_rules
;
306 script_setting_rules
.push_back(
307 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
308 ContentSettingsPattern::Wildcard(),
309 CONTENT_SETTING_ALLOW
,
313 ContentSettingsObserver
* observer
= ContentSettingsObserver::Get(view_
);
314 observer
->SetContentSettingRules(&content_setting_rules
);
316 // Load a page which contains a script.
317 std::string html
= "<html>"
319 "<script src='data:foo'></script>"
324 LoadHTML(html
.c_str());
326 // Verify that the script was not blocked.
327 bool was_blocked
= false;
328 for (size_t i
= 0; i
< render_thread_
->sink().message_count(); ++i
) {
329 const IPC::Message
* msg
= render_thread_
->sink().GetMessageAt(i
);
330 if (msg
->type() == ChromeViewHostMsg_ContentBlocked::ID
)
333 EXPECT_FALSE(was_blocked
);
336 TEST_F(ChromeRenderViewTest
, ContentSettingsInterstitialPages
) {
337 MockContentSettingsObserver
mock_observer(view_
);
339 RendererContentSettingRules content_setting_rules
;
340 ContentSettingsForOneType
& script_setting_rules
=
341 content_setting_rules
.script_rules
;
342 script_setting_rules
.push_back(
343 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
344 ContentSettingsPattern::Wildcard(),
345 CONTENT_SETTING_BLOCK
,
349 ContentSettingsForOneType
& image_setting_rules
=
350 content_setting_rules
.image_rules
;
351 image_setting_rules
.push_back(
352 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
353 ContentSettingsPattern::Wildcard(),
354 CONTENT_SETTING_BLOCK
,
358 ContentSettingsObserver
* observer
= ContentSettingsObserver::Get(view_
);
359 observer
->SetContentSettingRules(&content_setting_rules
);
360 observer
->OnSetAsInterstitial();
362 // Load a page which contains a script.
363 std::string html
= "<html>"
365 "<script src='data:foo'></script>"
370 LoadHTML(html
.c_str());
372 // Verify that the script was allowed.
373 bool was_blocked
= false;
374 for (size_t i
= 0; i
< render_thread_
->sink().message_count(); ++i
) {
375 const IPC::Message
* msg
= render_thread_
->sink().GetMessageAt(i
);
376 if (msg
->type() == ChromeViewHostMsg_ContentBlocked::ID
)
379 EXPECT_FALSE(was_blocked
);
381 // Verify that images are allowed.
384 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES
, std::string())).Times(0);
385 EXPECT_TRUE(observer
->AllowImage(GetMainFrame(), true,
386 mock_observer
.image_url_
));
387 ::testing::Mock::VerifyAndClearExpectations(&observer
);