Disable Convolver.SIMDVerification under the ThreadSanitizer.
[chromium-blink-merge.git] / chrome / renderer / content_settings_observer_browsertest.cc
blob90832815160c1cef2cb52ac657b8fdc91facb116
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"
15 using testing::_;
16 using testing::DeleteArg;
18 namespace {
20 class MockContentSettingsObserver : public ContentSettingsObserver {
21 public:
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*));
31 GURL image_url_;
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,
46 OnAllowDOMStorage)
47 IPC_MESSAGE_UNHANDLED(ADD_FAILURE())
48 IPC_END_MESSAGE_MAP()
50 // Our super class deletes the message.
51 return RenderViewObserver::Send(message);
54 } // namespace
56 TEST_F(ChromeRenderViewTest, DidBlockContentType) {
57 MockContentSettingsObserver observer(view_);
58 EXPECT_CALL(observer,
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";
69 EXPECT_CALL(observer,
70 OnContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS, kFooPlugin));
71 EXPECT_CALL(observer,
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_);
83 ON_CALL(observer,
84 OnAllowDOMStorage(_, _, _, _, _)).WillByDefault(DeleteArg<4>());
85 EXPECT_CALL(observer,
86 OnAllowDOMStorage(_, _, _, _, _));
87 observer.AllowStorage(view_->GetWebView()->focusedFrame(), true);
89 // Accessing localStorage from the same origin again shouldn't result in a
90 // new IPC.
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>"
99 "<head>"
100 "<script>document.createElement('div');</script>"
101 "</head>"
102 "<body>"
103 "</body>"
104 "</html>";
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,
116 std::string(),
117 false));
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();
125 // 3. Reload page.
126 std::string url_str = "data:text/html;charset=utf-8,";
127 url_str.append(html);
128 GURL url(url_str);
129 Reload(url);
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)
141 block_index = i;
143 EXPECT_NE(-1, navigation_index);
144 EXPECT_NE(-1, block_index);
145 EXPECT_LT(navigation_index, block_index);
148 TEST_F(ChromeRenderViewTest, PluginsTemporarilyAllowed) {
149 // Load some HTML.
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,
160 foo_plugin));
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,
176 std::string()));
177 EXPECT_TRUE(observer->IsPluginTemporarilyAllowed(foo_plugin));
178 EXPECT_TRUE(observer->IsPluginTemporarilyAllowed(bar_plugin));
181 TEST_F(ChromeRenderViewTest, ImagesBlockedByDefault) {
182 MockContentSettingsObserver mock_observer(view_);
184 // Load some HTML.
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,
195 std::string(),
196 false));
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,
213 std::string(),
214 false));
216 EXPECT_CALL(
217 mock_observer,
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_);
227 // Load some HTML.
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,
238 std::string(),
239 false));
241 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_);
242 observer->SetContentSettingRules(&content_setting_rules);
243 EXPECT_CALL(
244 mock_observer,
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,
257 std::string(),
258 false));
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,
275 std::string(),
276 false));
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>"
283 "<head>"
284 "<script src='data:foo'></script>"
285 "</head>"
286 "<body>"
287 "</body>"
288 "</html>";
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)
296 was_blocked = true;
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,
310 std::string(),
311 false));
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>"
318 "<head>"
319 "<script src='data:foo'></script>"
320 "</head>"
321 "<body>"
322 "</body>"
323 "</html>";
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)
331 was_blocked = true;
333 EXPECT_FALSE(was_blocked);
336 TEST_F(ChromeRenderViewTest, ContentSettingsInterstitialPages) {
337 MockContentSettingsObserver mock_observer(view_);
338 // Block scripts.
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,
346 std::string(),
347 false));
348 // Block images.
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,
355 std::string(),
356 false));
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>"
364 "<head>"
365 "<script src='data:foo'></script>"
366 "</head>"
367 "<body>"
368 "</body>"
369 "</html>";
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)
377 was_blocked = true;
379 EXPECT_FALSE(was_blocked);
381 // Verify that images are allowed.
382 EXPECT_CALL(
383 mock_observer,
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);