Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(799)

Side by Side Diff: chrome/renderer/content_settings_observer_browsertest.cc

Issue 8538004: Take script URLs into account when applying script content settings. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased. Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/renderer/content_settings_observer.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/common/content_settings.h" 5 #include "chrome/common/content_settings.h"
6 #include "chrome/common/render_messages.h" 6 #include "chrome/common/render_messages.h"
7 #include "chrome/renderer/content_settings_observer.h" 7 #include "chrome/renderer/content_settings_observer.h"
8 #include "chrome/test/base/chrome_render_view_test.h" 8 #include "chrome/test/base/chrome_render_view_test.h"
9 #include "content/public/renderer/render_view.h" 9 #include "content/public/renderer/render_view.h"
10 #include "ipc/ipc_message_macros.h" 10 #include "ipc/ipc_message_macros.h"
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 "<head>" 99 "<head>"
100 "<script>document.createElement('div');</script>" 100 "<script>document.createElement('div');</script>"
101 "</head>" 101 "</head>"
102 "<body>" 102 "<body>"
103 "</body>" 103 "</body>"
104 "</html>"; 104 "</html>";
105 render_thread_->sink().ClearMessages(); 105 render_thread_->sink().ClearMessages();
106 LoadHTML(html.c_str()); 106 LoadHTML(html.c_str());
107 107
108 // 2. Block JavaScript. 108 // 2. Block JavaScript.
109 ContentSettings settings; 109 RendererContentSettingRules content_setting_rules;
110 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) 110 ContentSettingsForOneType& script_setting_rules =
111 settings.settings[i] = CONTENT_SETTING_ALLOW; 111 content_setting_rules.script_rules;
112 settings.settings[CONTENT_SETTINGS_TYPE_JAVASCRIPT] = CONTENT_SETTING_BLOCK; 112 script_setting_rules.push_back(
113 ContentSettingPatternSource(
114 ContentSettingsPattern::Wildcard(),
115 ContentSettingsPattern::Wildcard(),
116 CONTENT_SETTING_BLOCK,
117 "",
118 false));
113 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); 119 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_);
114 observer->SetContentSettings(settings); 120 observer->SetContentSettingRules(&content_setting_rules);
115 observer->SetDefaultContentSettings(&settings);
116 121
117 // Make sure no pending messages are in the queue. 122 // Make sure no pending messages are in the queue.
118 ProcessPendingMessages(); 123 ProcessPendingMessages();
119 render_thread_->sink().ClearMessages(); 124 render_thread_->sink().ClearMessages();
120 125
121 // 3. Reload page. 126 // 3. Reload page.
122 std::string url_str = "data:text/html;charset=utf-8,"; 127 std::string url_str = "data:text/html;charset=utf-8,";
123 url_str.append(html); 128 url_str.append(html);
124 GURL url(url_str); 129 GURL url(url_str);
125 Reload(url); 130 Reload(url);
(...skipping 12 matching lines...) Expand all
138 } 143 }
139 EXPECT_NE(-1, navigation_index); 144 EXPECT_NE(-1, navigation_index);
140 EXPECT_NE(-1, block_index); 145 EXPECT_NE(-1, block_index);
141 EXPECT_LT(navigation_index, block_index); 146 EXPECT_LT(navigation_index, block_index);
142 } 147 }
143 148
144 TEST_F(ChromeRenderViewTest, PluginsTemporarilyAllowed) { 149 TEST_F(ChromeRenderViewTest, PluginsTemporarilyAllowed) {
145 // Load some HTML. 150 // Load some HTML.
146 LoadHTML("<html>Foo</html>"); 151 LoadHTML("<html>Foo</html>");
147 152
148 // Block plugins.
149 ContentSettings settings;
150 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i)
151 settings.settings[i] = CONTENT_SETTING_ALLOW;
152 settings.settings[CONTENT_SETTINGS_TYPE_PLUGINS] = CONTENT_SETTING_BLOCK;
153 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); 153 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_);
154 observer->SetContentSettings(settings);
155 observer->SetDefaultContentSettings(&settings);
156 EXPECT_FALSE(observer->plugins_temporarily_allowed()); 154 EXPECT_FALSE(observer->plugins_temporarily_allowed());
157 155
158 // Temporarily allow plugins. 156 // Temporarily allow plugins.
159 OnMessageReceived(ChromeViewMsg_LoadBlockedPlugins(MSG_ROUTING_NONE)); 157 OnMessageReceived(ChromeViewMsg_LoadBlockedPlugins(MSG_ROUTING_NONE));
160 EXPECT_TRUE(observer->plugins_temporarily_allowed()); 158 EXPECT_TRUE(observer->plugins_temporarily_allowed());
161 159
162 // Simulate a navigation within the page. 160 // Simulate a navigation within the page.
163 DidNavigateWithinPage(GetMainFrame(), true); 161 DidNavigateWithinPage(GetMainFrame(), true);
164 EXPECT_TRUE(observer->plugins_temporarily_allowed()); 162 EXPECT_TRUE(observer->plugins_temporarily_allowed());
165 163
166 // Navigate to a different page. 164 // Navigate to a different page.
167 LoadHTML("<html>Bar</html>"); 165 LoadHTML("<html>Bar</html>");
168 EXPECT_FALSE(observer->plugins_temporarily_allowed()); 166 EXPECT_FALSE(observer->plugins_temporarily_allowed());
169 } 167 }
170 168
171 TEST_F(ChromeRenderViewTest, ImagesBlockedByDefault) { 169 TEST_F(ChromeRenderViewTest, ImagesBlockedByDefault) {
172 MockContentSettingsObserver mock_observer(view_); 170 MockContentSettingsObserver mock_observer(view_);
173 171
174 // Load some HTML. 172 // Load some HTML.
175 LoadHTML("<html>Foo</html>"); 173 LoadHTML("<html>Foo</html>");
176 174
177 // Set the default image blocking setting. 175 // Set the default image blocking setting.
178 ContentSettingsForOneType image_setting_rules; 176 RendererContentSettingRules content_setting_rules;
177 ContentSettingsForOneType& image_setting_rules =
178 content_setting_rules.image_rules;
179 image_setting_rules.push_back( 179 image_setting_rules.push_back(
180 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), 180 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
181 ContentSettingsPattern::Wildcard(), 181 ContentSettingsPattern::Wildcard(),
182 CONTENT_SETTING_BLOCK, 182 CONTENT_SETTING_BLOCK,
183 "", 183 "",
184 false)); 184 false));
185 185
186 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); 186 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_);
187 observer->SetImageSettingRules(&image_setting_rules); 187 observer->SetContentSettingRules(&content_setting_rules);
188 EXPECT_CALL(mock_observer, 188 EXPECT_CALL(mock_observer,
189 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())); 189 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
190 EXPECT_FALSE(observer->AllowImage(GetMainFrame(), 190 EXPECT_FALSE(observer->AllowImage(GetMainFrame(),
191 true, mock_observer.image_url_)); 191 true, mock_observer.image_url_));
192 ::testing::Mock::VerifyAndClearExpectations(&observer); 192 ::testing::Mock::VerifyAndClearExpectations(&observer);
193 193
194 // Create an exception which allows the image. 194 // Create an exception which allows the image.
195 image_setting_rules.insert( 195 image_setting_rules.insert(
196 image_setting_rules.begin(), 196 image_setting_rules.begin(),
197 ContentSettingPatternSource( 197 ContentSettingPatternSource(
(...skipping 11 matching lines...) Expand all
209 ::testing::Mock::VerifyAndClearExpectations(&observer); 209 ::testing::Mock::VerifyAndClearExpectations(&observer);
210 } 210 }
211 211
212 TEST_F(ChromeRenderViewTest, ImagesAllowedByDefault) { 212 TEST_F(ChromeRenderViewTest, ImagesAllowedByDefault) {
213 MockContentSettingsObserver mock_observer(view_); 213 MockContentSettingsObserver mock_observer(view_);
214 214
215 // Load some HTML. 215 // Load some HTML.
216 LoadHTML("<html>Foo</html>"); 216 LoadHTML("<html>Foo</html>");
217 217
218 // Set the default image blocking setting. 218 // Set the default image blocking setting.
219 ContentSettingsForOneType image_setting_rules; 219 RendererContentSettingRules content_setting_rules;
220 ContentSettingsForOneType& image_setting_rules =
221 content_setting_rules.image_rules;
220 image_setting_rules.push_back( 222 image_setting_rules.push_back(
221 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), 223 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
222 ContentSettingsPattern::Wildcard(), 224 ContentSettingsPattern::Wildcard(),
223 CONTENT_SETTING_ALLOW, 225 CONTENT_SETTING_ALLOW,
224 "", 226 "",
225 false)); 227 false));
226 228
227 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); 229 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_);
228 observer->SetImageSettingRules(&image_setting_rules); 230 observer->SetContentSettingRules(&content_setting_rules);
229 EXPECT_CALL( 231 EXPECT_CALL(
230 mock_observer, 232 mock_observer,
231 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())).Times(0); 233 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())).Times(0);
232 EXPECT_TRUE(observer->AllowImage(GetMainFrame(), true, 234 EXPECT_TRUE(observer->AllowImage(GetMainFrame(), true,
233 mock_observer.image_url_)); 235 mock_observer.image_url_));
234 ::testing::Mock::VerifyAndClearExpectations(&observer); 236 ::testing::Mock::VerifyAndClearExpectations(&observer);
235 237
236 // Create an exception which blocks the image. 238 // Create an exception which blocks the image.
237 image_setting_rules.insert( 239 image_setting_rules.insert(
238 image_setting_rules.begin(), 240 image_setting_rules.begin(),
239 ContentSettingPatternSource( 241 ContentSettingPatternSource(
240 ContentSettingsPattern::Wildcard(), 242 ContentSettingsPattern::Wildcard(),
241 ContentSettingsPattern::FromString(mock_observer.image_origin_), 243 ContentSettingsPattern::FromString(mock_observer.image_origin_),
242 CONTENT_SETTING_BLOCK, 244 CONTENT_SETTING_BLOCK,
243 "", 245 "",
244 false)); 246 false));
245 EXPECT_CALL(mock_observer, 247 EXPECT_CALL(mock_observer,
246 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())); 248 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
247 EXPECT_FALSE(observer->AllowImage(GetMainFrame(), 249 EXPECT_FALSE(observer->AllowImage(GetMainFrame(),
248 true, mock_observer.image_url_)); 250 true, mock_observer.image_url_));
249 ::testing::Mock::VerifyAndClearExpectations(&observer); 251 ::testing::Mock::VerifyAndClearExpectations(&observer);
250 } 252 }
253
254 TEST_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(
260 ContentSettingPatternSource(
261 ContentSettingsPattern::Wildcard(),
262 ContentSettingsPattern::Wildcard(),
263 CONTENT_SETTING_BLOCK,
264 "",
265 false));
266
267 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_);
268 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
290 TEST_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(
296 ContentSettingPatternSource(
297 ContentSettingsPattern::Wildcard(),
298 ContentSettingsPattern::Wildcard(),
299 CONTENT_SETTING_ALLOW,
300 "",
301 false));
302
303 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_);
304 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 }
OLDNEW
« no previous file with comments | « chrome/renderer/content_settings_observer.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698