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::RenderFrame
* render_frame
);
24 virtual bool Send(IPC::Message
* message
);
26 MOCK_METHOD1(OnContentBlocked
,
27 void(ContentSettingsType
));
29 MOCK_METHOD5(OnAllowDOMStorage
,
30 void(int, const GURL
&, const GURL
&, bool, IPC::Message
*));
32 std::string image_origin_
;
35 MockContentSettingsObserver::MockContentSettingsObserver(
36 content::RenderFrame
* render_frame
)
37 : ContentSettingsObserver(render_frame
, NULL
),
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 RenderFrameObserver::Send(message
);
56 TEST_F(ChromeRenderViewTest
, DidBlockContentType
) {
57 MockContentSettingsObserver
observer(view_
->GetMainRenderFrame());
59 OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
60 observer
.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES
);
62 // Blocking the same content type a second time shouldn't send a notification.
63 observer
.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES
);
64 ::testing::Mock::VerifyAndClearExpectations(&observer
);
67 // Tests that multiple invokations of AllowDOMStorage result in a single IPC.
68 // Fails due to http://crbug.com/104300
69 TEST_F(ChromeRenderViewTest
, DISABLED_AllowDOMStorage
) {
70 // Load some HTML, so we have a valid security origin.
71 LoadHTML("<html></html>");
72 MockContentSettingsObserver
observer(view_
->GetMainRenderFrame());
74 OnAllowDOMStorage(_
, _
, _
, _
, _
)).WillByDefault(DeleteArg
<4>());
76 OnAllowDOMStorage(_
, _
, _
, _
, _
));
77 observer
.allowStorage(view_
->GetWebView()->focusedFrame(), true);
79 // Accessing localStorage from the same origin again shouldn't result in a
81 observer
.allowStorage(view_
->GetWebView()->focusedFrame(), true);
82 ::testing::Mock::VerifyAndClearExpectations(&observer
);
85 // Regression test for http://crbug.com/35011
86 TEST_F(ChromeRenderViewTest
, JSBlockSentAfterPageLoad
) {
87 // 1. Load page with JS.
88 std::string html
= "<html>"
90 "<script>document.createElement('div');</script>"
95 render_thread_
->sink().ClearMessages();
96 LoadHTML(html
.c_str());
98 // 2. Block JavaScript.
99 RendererContentSettingRules content_setting_rules
;
100 ContentSettingsForOneType
& script_setting_rules
=
101 content_setting_rules
.script_rules
;
102 script_setting_rules
.push_back(
103 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
104 ContentSettingsPattern::Wildcard(),
105 CONTENT_SETTING_BLOCK
,
108 ContentSettingsObserver
* observer
= ContentSettingsObserver::Get(
109 view_
->GetMainRenderFrame());
110 observer
->SetContentSettingRules(&content_setting_rules
);
112 // Make sure no pending messages are in the queue.
113 ProcessPendingMessages();
114 render_thread_
->sink().ClearMessages();
117 std::string url_str
= "data:text/html;charset=utf-8,";
118 url_str
.append(html
);
121 ProcessPendingMessages();
123 // 4. Verify that the notification that javascript was blocked is sent after
124 // the navigation notifiction is sent.
125 int navigation_index
= -1;
126 int block_index
= -1;
127 for (size_t i
= 0; i
< render_thread_
->sink().message_count(); ++i
) {
128 const IPC::Message
* msg
= render_thread_
->sink().GetMessageAt(i
);
129 if (msg
->type() == GetNavigationIPCType())
130 navigation_index
= i
;
131 if (msg
->type() == ChromeViewHostMsg_ContentBlocked::ID
)
134 EXPECT_NE(-1, navigation_index
);
135 EXPECT_NE(-1, block_index
);
136 EXPECT_LT(navigation_index
, block_index
);
139 TEST_F(ChromeRenderViewTest
, PluginsTemporarilyAllowed
) {
141 LoadHTML("<html>Foo</html>");
143 std::string foo_plugin
= "foo";
144 std::string bar_plugin
= "bar";
146 ContentSettingsObserver
* observer
=
147 ContentSettingsObserver::Get(view_
->GetMainRenderFrame());
148 EXPECT_FALSE(observer
->IsPluginTemporarilyAllowed(foo_plugin
));
150 // Temporarily allow the "foo" plugin.
151 observer
->OnLoadBlockedPlugins(foo_plugin
);
152 EXPECT_TRUE(observer
->IsPluginTemporarilyAllowed(foo_plugin
));
153 EXPECT_FALSE(observer
->IsPluginTemporarilyAllowed(bar_plugin
));
155 // Simulate a navigation within the page.
156 DidNavigateWithinPage(GetMainFrame(), true);
157 EXPECT_TRUE(observer
->IsPluginTemporarilyAllowed(foo_plugin
));
158 EXPECT_FALSE(observer
->IsPluginTemporarilyAllowed(bar_plugin
));
160 // Navigate to a different page.
161 LoadHTML("<html>Bar</html>");
162 EXPECT_FALSE(observer
->IsPluginTemporarilyAllowed(foo_plugin
));
163 EXPECT_FALSE(observer
->IsPluginTemporarilyAllowed(bar_plugin
));
165 // Temporarily allow all plugins.
166 observer
->OnLoadBlockedPlugins(std::string());
167 EXPECT_TRUE(observer
->IsPluginTemporarilyAllowed(foo_plugin
));
168 EXPECT_TRUE(observer
->IsPluginTemporarilyAllowed(bar_plugin
));
171 TEST_F(ChromeRenderViewTest
, ImagesBlockedByDefault
) {
172 MockContentSettingsObserver
mock_observer(view_
->GetMainRenderFrame());
175 LoadHTML("<html>Foo</html>");
177 // Set the default image blocking setting.
178 RendererContentSettingRules content_setting_rules
;
179 ContentSettingsForOneType
& image_setting_rules
=
180 content_setting_rules
.image_rules
;
181 image_setting_rules
.push_back(
182 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
183 ContentSettingsPattern::Wildcard(),
184 CONTENT_SETTING_BLOCK
,
188 ContentSettingsObserver
* observer
= ContentSettingsObserver::Get(
189 view_
->GetMainRenderFrame());
190 observer
->SetContentSettingRules(&content_setting_rules
);
191 EXPECT_CALL(mock_observer
,
192 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES
));
193 EXPECT_FALSE(observer
->allowImage(GetMainFrame(),
194 true, mock_observer
.image_url_
));
195 ::testing::Mock::VerifyAndClearExpectations(&observer
);
197 // Create an exception which allows the image.
198 image_setting_rules
.insert(
199 image_setting_rules
.begin(),
200 ContentSettingPatternSource(
201 ContentSettingsPattern::Wildcard(),
202 ContentSettingsPattern::FromString(mock_observer
.image_origin_
),
203 CONTENT_SETTING_ALLOW
,
209 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES
)).Times(0);
210 EXPECT_TRUE(observer
->allowImage(GetMainFrame(), true,
211 mock_observer
.image_url_
));
212 ::testing::Mock::VerifyAndClearExpectations(&observer
);
215 TEST_F(ChromeRenderViewTest
, ImagesAllowedByDefault
) {
216 MockContentSettingsObserver
mock_observer(view_
->GetMainRenderFrame());
219 LoadHTML("<html>Foo</html>");
221 // Set the default image blocking setting.
222 RendererContentSettingRules content_setting_rules
;
223 ContentSettingsForOneType
& image_setting_rules
=
224 content_setting_rules
.image_rules
;
225 image_setting_rules
.push_back(
226 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
227 ContentSettingsPattern::Wildcard(),
228 CONTENT_SETTING_ALLOW
,
232 ContentSettingsObserver
* observer
=
233 ContentSettingsObserver::Get(view_
->GetMainRenderFrame());
234 observer
->SetContentSettingRules(&content_setting_rules
);
237 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES
)).Times(0);
238 EXPECT_TRUE(observer
->allowImage(GetMainFrame(), true,
239 mock_observer
.image_url_
));
240 ::testing::Mock::VerifyAndClearExpectations(&observer
);
242 // Create an exception which blocks the image.
243 image_setting_rules
.insert(
244 image_setting_rules
.begin(),
245 ContentSettingPatternSource(
246 ContentSettingsPattern::Wildcard(),
247 ContentSettingsPattern::FromString(mock_observer
.image_origin_
),
248 CONTENT_SETTING_BLOCK
,
251 EXPECT_CALL(mock_observer
,
252 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES
));
253 EXPECT_FALSE(observer
->allowImage(GetMainFrame(),
254 true, mock_observer
.image_url_
));
255 ::testing::Mock::VerifyAndClearExpectations(&observer
);
258 TEST_F(ChromeRenderViewTest
, ContentSettingsBlockScripts
) {
259 // Set the content settings for scripts.
260 RendererContentSettingRules content_setting_rules
;
261 ContentSettingsForOneType
& script_setting_rules
=
262 content_setting_rules
.script_rules
;
263 script_setting_rules
.push_back(
264 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
265 ContentSettingsPattern::Wildcard(),
266 CONTENT_SETTING_BLOCK
,
270 ContentSettingsObserver
* observer
=
271 ContentSettingsObserver::Get(view_
->GetMainRenderFrame());
272 observer
->SetContentSettingRules(&content_setting_rules
);
274 // Load a page which contains a script.
275 std::string html
= "<html>"
277 "<script src='data:foo'></script>"
282 LoadHTML(html
.c_str());
284 // Verify that the script was blocked.
285 bool was_blocked
= false;
286 for (size_t i
= 0; i
< render_thread_
->sink().message_count(); ++i
) {
287 const IPC::Message
* msg
= render_thread_
->sink().GetMessageAt(i
);
288 if (msg
->type() == ChromeViewHostMsg_ContentBlocked::ID
)
291 EXPECT_TRUE(was_blocked
);
294 TEST_F(ChromeRenderViewTest
, ContentSettingsAllowScripts
) {
295 // Set the content settings for scripts.
296 RendererContentSettingRules content_setting_rules
;
297 ContentSettingsForOneType
& script_setting_rules
=
298 content_setting_rules
.script_rules
;
299 script_setting_rules
.push_back(
300 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
301 ContentSettingsPattern::Wildcard(),
302 CONTENT_SETTING_ALLOW
,
306 ContentSettingsObserver
* observer
=
307 ContentSettingsObserver::Get(view_
->GetMainRenderFrame());
308 observer
->SetContentSettingRules(&content_setting_rules
);
310 // Load a page which contains a script.
311 std::string html
= "<html>"
313 "<script src='data:foo'></script>"
318 LoadHTML(html
.c_str());
320 // Verify that the script was not blocked.
321 bool was_blocked
= false;
322 for (size_t i
= 0; i
< render_thread_
->sink().message_count(); ++i
) {
323 const IPC::Message
* msg
= render_thread_
->sink().GetMessageAt(i
);
324 if (msg
->type() == ChromeViewHostMsg_ContentBlocked::ID
)
327 EXPECT_FALSE(was_blocked
);
330 TEST_F(ChromeRenderViewTest
, ContentSettingsInterstitialPages
) {
331 MockContentSettingsObserver
mock_observer(view_
->GetMainRenderFrame());
333 RendererContentSettingRules content_setting_rules
;
334 ContentSettingsForOneType
& script_setting_rules
=
335 content_setting_rules
.script_rules
;
336 script_setting_rules
.push_back(
337 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
338 ContentSettingsPattern::Wildcard(),
339 CONTENT_SETTING_BLOCK
,
343 ContentSettingsForOneType
& image_setting_rules
=
344 content_setting_rules
.image_rules
;
345 image_setting_rules
.push_back(
346 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
347 ContentSettingsPattern::Wildcard(),
348 CONTENT_SETTING_BLOCK
,
352 ContentSettingsObserver
* observer
=
353 ContentSettingsObserver::Get(view_
->GetMainRenderFrame());
354 observer
->SetContentSettingRules(&content_setting_rules
);
355 observer
->OnSetAsInterstitial();
357 // Load a page which contains a script.
358 std::string html
= "<html>"
360 "<script src='data:foo'></script>"
365 LoadHTML(html
.c_str());
367 // Verify that the script was allowed.
368 bool was_blocked
= false;
369 for (size_t i
= 0; i
< render_thread_
->sink().message_count(); ++i
) {
370 const IPC::Message
* msg
= render_thread_
->sink().GetMessageAt(i
);
371 if (msg
->type() == ChromeViewHostMsg_ContentBlocked::ID
)
374 EXPECT_FALSE(was_blocked
);
376 // Verify that images are allowed.
379 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES
)).Times(0);
380 EXPECT_TRUE(observer
->allowImage(GetMainFrame(), true,
381 mock_observer
.image_url_
));
382 ::testing::Mock::VerifyAndClearExpectations(&observer
);