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
3// found in the LICENSE file.
4
5#include "base/auto_reset.h"
6#include "base/command_line.h"
7#include "base/json/json_reader.h"
8#include "base/json/json_writer.h"
9#include "chrome/browser/content_settings/content_settings_details.h"
10#include "chrome/browser/content_settings/host_content_settings_map.h"
11#include "chrome/browser/content_settings/stub_settings_observer.h"
12#include "chrome/browser/prefs/pref_service.h"
13#include "chrome/browser/prefs/scoped_user_pref_update.h"
14#include "chrome/common/chrome_switches.h"
15#include "chrome/common/pref_names.h"
16#include "chrome/common/url_constants.h"
17#include "chrome/test/testing_browser_process_test.h"
18#include "chrome/test/testing_pref_service.h"
19#include "chrome/test/testing_profile.h"
20#include "googleurl/src/gurl.h"
21#include "net/base/static_cookie_policy.h"
22#include "testing/gtest/include/gtest/gtest.h"
23
24namespace {
25
26bool SettingsEqual(const ContentSettings& settings1,
27                   const ContentSettings& settings2) {
28  for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
29    if (settings1.settings[i] != settings2.settings[i])
30      return false;
31  }
32  return true;
33}
34
35class HostContentSettingsMapTest : public TestingBrowserProcessTest {
36 public:
37  HostContentSettingsMapTest() : ui_thread_(BrowserThread::UI, &message_loop_) {
38  }
39
40 protected:
41  MessageLoop message_loop_;
42  BrowserThread ui_thread_;
43};
44
45TEST_F(HostContentSettingsMapTest, DefaultValues) {
46  TestingProfile profile;
47  HostContentSettingsMap* host_content_settings_map =
48      profile.GetHostContentSettingsMap();
49
50  // Check setting defaults.
51  EXPECT_EQ(CONTENT_SETTING_ALLOW,
52            host_content_settings_map->GetDefaultContentSetting(
53                CONTENT_SETTINGS_TYPE_JAVASCRIPT));
54  host_content_settings_map->SetDefaultContentSetting(
55      CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK);
56  EXPECT_EQ(CONTENT_SETTING_BLOCK,
57            host_content_settings_map->GetDefaultContentSetting(
58                CONTENT_SETTINGS_TYPE_IMAGES));
59  EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting(
60                GURL(chrome::kChromeUINewTabURL),
61                CONTENT_SETTINGS_TYPE_IMAGES, ""));
62  {
63    // Click-to-play needs to be enabled to set the content setting to ASK.
64    CommandLine* cmd = CommandLine::ForCurrentProcess();
65    AutoReset<CommandLine> auto_reset(cmd, *cmd);
66    cmd->AppendSwitch(switches::kEnableClickToPlay);
67
68    host_content_settings_map->SetDefaultContentSetting(
69        CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_ASK);
70    EXPECT_EQ(CONTENT_SETTING_ASK,
71              host_content_settings_map->GetDefaultContentSetting(
72                  CONTENT_SETTINGS_TYPE_PLUGINS));
73  }
74  host_content_settings_map->SetDefaultContentSetting(
75      CONTENT_SETTINGS_TYPE_POPUPS, CONTENT_SETTING_ALLOW);
76  EXPECT_EQ(CONTENT_SETTING_ALLOW,
77            host_content_settings_map->GetDefaultContentSetting(
78                CONTENT_SETTINGS_TYPE_POPUPS));
79  host_content_settings_map->ResetToDefaults();
80  EXPECT_EQ(CONTENT_SETTING_ALLOW,
81            host_content_settings_map->GetDefaultContentSetting(
82                CONTENT_SETTINGS_TYPE_PLUGINS));
83
84  // Check returning individual settings.
85  GURL host("http://example.com/");
86  ContentSettingsPattern pattern("[*.]example.com");
87  EXPECT_EQ(CONTENT_SETTING_ALLOW,
88            host_content_settings_map->GetContentSetting(
89                host, CONTENT_SETTINGS_TYPE_IMAGES, ""));
90  host_content_settings_map->SetContentSetting(pattern,
91      CONTENT_SETTINGS_TYPE_IMAGES, "", CONTENT_SETTING_DEFAULT);
92  EXPECT_EQ(CONTENT_SETTING_ALLOW,
93            host_content_settings_map->GetContentSetting(
94                host, CONTENT_SETTINGS_TYPE_IMAGES, ""));
95  host_content_settings_map->SetContentSetting(pattern,
96      CONTENT_SETTINGS_TYPE_IMAGES, "", CONTENT_SETTING_BLOCK);
97  EXPECT_EQ(CONTENT_SETTING_BLOCK,
98            host_content_settings_map->GetContentSetting(
99                host, CONTENT_SETTINGS_TYPE_IMAGES, ""));
100  EXPECT_EQ(CONTENT_SETTING_ALLOW,
101            host_content_settings_map->GetContentSetting(
102                host, CONTENT_SETTINGS_TYPE_PLUGINS, ""));
103
104  // Check returning all settings for a host.
105  ContentSettings desired_settings;
106  desired_settings.settings[CONTENT_SETTINGS_TYPE_COOKIES] =
107      CONTENT_SETTING_ALLOW;
108  host_content_settings_map->SetContentSetting(pattern,
109      CONTENT_SETTINGS_TYPE_IMAGES, "", CONTENT_SETTING_DEFAULT);
110  desired_settings.settings[CONTENT_SETTINGS_TYPE_IMAGES] =
111      CONTENT_SETTING_ALLOW;
112  host_content_settings_map->SetContentSetting(pattern,
113      CONTENT_SETTINGS_TYPE_JAVASCRIPT, "", CONTENT_SETTING_BLOCK);
114  desired_settings.settings[CONTENT_SETTINGS_TYPE_JAVASCRIPT] =
115      CONTENT_SETTING_BLOCK;
116  host_content_settings_map->SetContentSetting(pattern,
117      CONTENT_SETTINGS_TYPE_PLUGINS, "", CONTENT_SETTING_ALLOW);
118  desired_settings.settings[CONTENT_SETTINGS_TYPE_PLUGINS] =
119      CONTENT_SETTING_ALLOW;
120  desired_settings.settings[CONTENT_SETTINGS_TYPE_POPUPS] =
121      CONTENT_SETTING_BLOCK;
122  desired_settings.settings[CONTENT_SETTINGS_TYPE_GEOLOCATION] =
123      CONTENT_SETTING_ASK;
124  desired_settings.settings[CONTENT_SETTINGS_TYPE_NOTIFICATIONS] =
125      CONTENT_SETTING_ASK;
126  desired_settings.settings[CONTENT_SETTINGS_TYPE_PRERENDER] =
127      CONTENT_SETTING_ALLOW;
128  ContentSettings settings =
129      host_content_settings_map->GetContentSettings(host);
130  EXPECT_TRUE(SettingsEqual(desired_settings, settings));
131
132  // Check returning all hosts for a setting.
133  ContentSettingsPattern pattern2("[*.]example.org");
134  host_content_settings_map->SetContentSetting(pattern2,
135      CONTENT_SETTINGS_TYPE_IMAGES, "", CONTENT_SETTING_BLOCK);
136  host_content_settings_map->SetContentSetting(pattern2,
137      CONTENT_SETTINGS_TYPE_PLUGINS, "", CONTENT_SETTING_BLOCK);
138  HostContentSettingsMap::SettingsForOneType host_settings;
139  host_content_settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_IMAGES,
140                                                   "",
141                                                   &host_settings);
142  EXPECT_EQ(1U, host_settings.size());
143  host_content_settings_map->GetSettingsForOneType(
144      CONTENT_SETTINGS_TYPE_PLUGINS, "", &host_settings);
145  EXPECT_EQ(2U, host_settings.size());
146  host_content_settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_POPUPS,
147                                                   "",
148                                                   &host_settings);
149  EXPECT_EQ(0U, host_settings.size());
150  host_content_settings_map->ResetToDefaults();
151  host_content_settings_map->GetSettingsForOneType(
152      CONTENT_SETTINGS_TYPE_PLUGINS, "", &host_settings);
153  EXPECT_EQ(0U, host_settings.size());
154
155  // Check clearing one type.
156  ContentSettingsPattern pattern3("[*.]example.net");
157  host_content_settings_map->SetContentSetting(pattern,
158      CONTENT_SETTINGS_TYPE_IMAGES, "", CONTENT_SETTING_BLOCK);
159  host_content_settings_map->SetContentSetting(pattern2,
160      CONTENT_SETTINGS_TYPE_IMAGES, "", CONTENT_SETTING_BLOCK);
161  host_content_settings_map->SetContentSetting(pattern2,
162      CONTENT_SETTINGS_TYPE_PLUGINS, "", CONTENT_SETTING_BLOCK);
163  host_content_settings_map->SetContentSetting(pattern3,
164      CONTENT_SETTINGS_TYPE_IMAGES, "", CONTENT_SETTING_BLOCK);
165  host_content_settings_map->ClearSettingsForOneType(
166      CONTENT_SETTINGS_TYPE_IMAGES);
167  host_content_settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_IMAGES,
168                                                   "",
169                                                   &host_settings);
170  EXPECT_EQ(0U, host_settings.size());
171  host_content_settings_map->GetSettingsForOneType(
172      CONTENT_SETTINGS_TYPE_PLUGINS, "", &host_settings);
173  EXPECT_EQ(1U, host_settings.size());
174}
175
176TEST_F(HostContentSettingsMapTest, Patterns) {
177  TestingProfile profile;
178  HostContentSettingsMap* host_content_settings_map =
179      profile.GetHostContentSettingsMap();
180
181  GURL host1("http://example.com/");
182  GURL host2("http://www.example.com/");
183  GURL host3("http://example.org/");
184  ContentSettingsPattern pattern1("[*.]example.com");
185  ContentSettingsPattern pattern2("example.org");
186  EXPECT_EQ(CONTENT_SETTING_ALLOW,
187            host_content_settings_map->GetContentSetting(
188                host1, CONTENT_SETTINGS_TYPE_IMAGES, ""));
189  host_content_settings_map->SetContentSetting(pattern1,
190      CONTENT_SETTINGS_TYPE_IMAGES, "", CONTENT_SETTING_BLOCK);
191  EXPECT_EQ(CONTENT_SETTING_BLOCK,
192            host_content_settings_map->GetContentSetting(
193                host1, CONTENT_SETTINGS_TYPE_IMAGES, ""));
194  EXPECT_EQ(CONTENT_SETTING_BLOCK,
195            host_content_settings_map->GetContentSetting(
196                host2, CONTENT_SETTINGS_TYPE_IMAGES, ""));
197  EXPECT_EQ(CONTENT_SETTING_ALLOW,
198            host_content_settings_map->GetContentSetting(
199                host3, CONTENT_SETTINGS_TYPE_IMAGES, ""));
200  host_content_settings_map->SetContentSetting(pattern2,
201      CONTENT_SETTINGS_TYPE_IMAGES, "", CONTENT_SETTING_BLOCK);
202  EXPECT_EQ(CONTENT_SETTING_BLOCK,
203            host_content_settings_map->GetContentSetting(
204                host3, CONTENT_SETTINGS_TYPE_IMAGES, ""));
205}
206
207TEST_F(HostContentSettingsMapTest, Observer) {
208  TestingProfile profile;
209  HostContentSettingsMap* host_content_settings_map =
210      profile.GetHostContentSettingsMap();
211  StubSettingsObserver observer;
212
213  ContentSettingsPattern pattern("[*.]example.com");
214  host_content_settings_map->SetContentSetting(pattern,
215      CONTENT_SETTINGS_TYPE_IMAGES, "", CONTENT_SETTING_ALLOW);
216  EXPECT_EQ(host_content_settings_map, observer.last_notifier);
217  EXPECT_EQ(pattern, observer.last_pattern);
218  EXPECT_FALSE(observer.last_update_all);
219  EXPECT_FALSE(observer.last_update_all_types);
220  EXPECT_EQ(1, observer.counter);
221
222  host_content_settings_map->ClearSettingsForOneType(
223      CONTENT_SETTINGS_TYPE_IMAGES);
224  EXPECT_EQ(host_content_settings_map, observer.last_notifier);
225  EXPECT_TRUE(observer.last_update_all);
226  EXPECT_FALSE(observer.last_update_all_types);
227  EXPECT_EQ(2, observer.counter);
228
229  host_content_settings_map->ResetToDefaults();
230  EXPECT_EQ(host_content_settings_map, observer.last_notifier);
231  EXPECT_TRUE(observer.last_update_all);
232  EXPECT_TRUE(observer.last_update_all_types);
233  EXPECT_EQ(3, observer.counter);
234
235  host_content_settings_map->SetDefaultContentSetting(
236      CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK);
237  EXPECT_EQ(host_content_settings_map, observer.last_notifier);
238  EXPECT_TRUE(observer.last_update_all);
239  EXPECT_FALSE(observer.last_update_all_types);
240  EXPECT_EQ(4, observer.counter);
241}
242
243TEST_F(HostContentSettingsMapTest, ObserveDefaultPref) {
244  TestingProfile profile;
245  HostContentSettingsMap* host_content_settings_map =
246      profile.GetHostContentSettingsMap();
247
248  PrefService* prefs = profile.GetPrefs();
249
250  // Make a copy of the default pref value so we can reset it later.
251  scoped_ptr<Value> default_value(prefs->FindPreference(
252      prefs::kDefaultContentSettings)->GetValue()->DeepCopy());
253
254  GURL host("http://example.com");
255
256  host_content_settings_map->SetDefaultContentSetting(
257      CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK);
258  EXPECT_EQ(CONTENT_SETTING_BLOCK,
259            host_content_settings_map->GetContentSetting(
260                host, CONTENT_SETTINGS_TYPE_COOKIES, ""));
261
262  // Make a copy of the pref's new value so we can reset it later.
263  scoped_ptr<Value> new_value(prefs->FindPreference(
264      prefs::kDefaultContentSettings)->GetValue()->DeepCopy());
265
266  // Clearing the backing pref should also clear the internal cache.
267  prefs->Set(prefs::kDefaultContentSettings, *default_value);
268  EXPECT_EQ(CONTENT_SETTING_ALLOW,
269            host_content_settings_map->GetContentSetting(
270                host, CONTENT_SETTINGS_TYPE_COOKIES, ""));
271
272  // Reseting the pref to its previous value should update the cache.
273  prefs->Set(prefs::kDefaultContentSettings, *new_value);
274  EXPECT_EQ(CONTENT_SETTING_BLOCK,
275            host_content_settings_map->GetContentSetting(
276                host, CONTENT_SETTINGS_TYPE_COOKIES, ""));
277}
278
279TEST_F(HostContentSettingsMapTest, ObserveExceptionPref) {
280  TestingProfile profile;
281  HostContentSettingsMap* host_content_settings_map =
282      profile.GetHostContentSettingsMap();
283
284  PrefService* prefs = profile.GetPrefs();
285
286  // Make a copy of the default pref value so we can reset it later.
287  scoped_ptr<Value> default_value(prefs->FindPreference(
288      prefs::kContentSettingsPatterns)->GetValue()->DeepCopy());
289
290  ContentSettingsPattern pattern("[*.]example.com");
291  GURL host("http://example.com");
292
293  host_content_settings_map->SetContentSetting(pattern,
294      CONTENT_SETTINGS_TYPE_COOKIES, "", CONTENT_SETTING_BLOCK);
295  EXPECT_EQ(CONTENT_SETTING_BLOCK,
296            host_content_settings_map->GetContentSetting(
297                host, CONTENT_SETTINGS_TYPE_COOKIES, ""));
298
299  // Make a copy of the pref's new value so we can reset it later.
300  scoped_ptr<Value> new_value(prefs->FindPreference(
301      prefs::kContentSettingsPatterns)->GetValue()->DeepCopy());
302
303  // Clearing the backing pref should also clear the internal cache.
304  prefs->Set(prefs::kContentSettingsPatterns, *default_value);
305  EXPECT_EQ(CONTENT_SETTING_ALLOW,
306            host_content_settings_map->GetContentSetting(
307                host, CONTENT_SETTINGS_TYPE_COOKIES, ""));
308
309  // Reseting the pref to its previous value should update the cache.
310  prefs->Set(prefs::kContentSettingsPatterns, *new_value);
311  EXPECT_EQ(CONTENT_SETTING_BLOCK,
312            host_content_settings_map->GetContentSetting(
313                host, CONTENT_SETTINGS_TYPE_COOKIES, ""));
314}
315
316TEST_F(HostContentSettingsMapTest, HostTrimEndingDotCheck) {
317  TestingProfile profile;
318  HostContentSettingsMap* host_content_settings_map =
319      profile.GetHostContentSettingsMap();
320
321  ContentSettingsPattern pattern("[*.]example.com");
322  GURL host_ending_with_dot("http://example.com./");
323
324  EXPECT_EQ(CONTENT_SETTING_ALLOW,
325            host_content_settings_map->GetContentSetting(
326                host_ending_with_dot, CONTENT_SETTINGS_TYPE_IMAGES, ""));
327  host_content_settings_map->SetContentSetting(pattern,
328      CONTENT_SETTINGS_TYPE_IMAGES, "", CONTENT_SETTING_DEFAULT);
329  EXPECT_EQ(CONTENT_SETTING_ALLOW,
330            host_content_settings_map->GetContentSetting(
331                host_ending_with_dot, CONTENT_SETTINGS_TYPE_IMAGES, ""));
332  host_content_settings_map->SetContentSetting(pattern,
333      CONTENT_SETTINGS_TYPE_IMAGES, "", CONTENT_SETTING_BLOCK);
334  EXPECT_EQ(CONTENT_SETTING_BLOCK,
335            host_content_settings_map->GetContentSetting(
336                host_ending_with_dot, CONTENT_SETTINGS_TYPE_IMAGES, ""));
337
338  EXPECT_EQ(CONTENT_SETTING_ALLOW,
339            host_content_settings_map->GetContentSetting(
340                host_ending_with_dot, CONTENT_SETTINGS_TYPE_COOKIES, ""));
341  host_content_settings_map->SetContentSetting(pattern,
342      CONTENT_SETTINGS_TYPE_COOKIES, "", CONTENT_SETTING_DEFAULT);
343  EXPECT_EQ(CONTENT_SETTING_ALLOW,
344            host_content_settings_map->GetContentSetting(
345                host_ending_with_dot, CONTENT_SETTINGS_TYPE_COOKIES, ""));
346  host_content_settings_map->SetContentSetting(pattern,
347      CONTENT_SETTINGS_TYPE_COOKIES, "", CONTENT_SETTING_BLOCK);
348  EXPECT_EQ(CONTENT_SETTING_BLOCK,
349            host_content_settings_map->GetContentSetting(
350                host_ending_with_dot, CONTENT_SETTINGS_TYPE_COOKIES, ""));
351
352  EXPECT_EQ(CONTENT_SETTING_ALLOW,
353            host_content_settings_map->GetContentSetting(
354                host_ending_with_dot, CONTENT_SETTINGS_TYPE_JAVASCRIPT, ""));
355  host_content_settings_map->SetContentSetting(pattern,
356      CONTENT_SETTINGS_TYPE_JAVASCRIPT, "", CONTENT_SETTING_DEFAULT);
357  EXPECT_EQ(CONTENT_SETTING_ALLOW,
358            host_content_settings_map->GetContentSetting(
359                host_ending_with_dot, CONTENT_SETTINGS_TYPE_JAVASCRIPT, ""));
360  host_content_settings_map->SetContentSetting(pattern,
361      CONTENT_SETTINGS_TYPE_JAVASCRIPT, "", CONTENT_SETTING_BLOCK);
362  EXPECT_EQ(CONTENT_SETTING_BLOCK,
363            host_content_settings_map->GetContentSetting(
364                host_ending_with_dot, CONTENT_SETTINGS_TYPE_JAVASCRIPT, ""));
365
366  EXPECT_EQ(CONTENT_SETTING_ALLOW,
367            host_content_settings_map->GetContentSetting(
368                host_ending_with_dot, CONTENT_SETTINGS_TYPE_PLUGINS, ""));
369  host_content_settings_map->SetContentSetting(pattern,
370      CONTENT_SETTINGS_TYPE_PLUGINS, "", CONTENT_SETTING_DEFAULT);
371  EXPECT_EQ(CONTENT_SETTING_ALLOW,
372            host_content_settings_map->GetContentSetting(
373                host_ending_with_dot, CONTENT_SETTINGS_TYPE_PLUGINS, ""));
374  host_content_settings_map->SetContentSetting(pattern,
375      CONTENT_SETTINGS_TYPE_PLUGINS, "", CONTENT_SETTING_BLOCK);
376  EXPECT_EQ(CONTENT_SETTING_BLOCK,
377            host_content_settings_map->GetContentSetting(
378                host_ending_with_dot, CONTENT_SETTINGS_TYPE_PLUGINS, ""));
379
380  EXPECT_EQ(CONTENT_SETTING_BLOCK,
381            host_content_settings_map->GetContentSetting(
382                host_ending_with_dot, CONTENT_SETTINGS_TYPE_POPUPS, ""));
383  host_content_settings_map->SetContentSetting(pattern,
384      CONTENT_SETTINGS_TYPE_POPUPS, "", CONTENT_SETTING_DEFAULT);
385  EXPECT_EQ(CONTENT_SETTING_BLOCK,
386            host_content_settings_map->GetContentSetting(
387                host_ending_with_dot, CONTENT_SETTINGS_TYPE_POPUPS, ""));
388  host_content_settings_map->SetContentSetting(pattern,
389      CONTENT_SETTINGS_TYPE_POPUPS, "", CONTENT_SETTING_ALLOW);
390  EXPECT_EQ(CONTENT_SETTING_ALLOW,
391            host_content_settings_map->GetContentSetting(
392                host_ending_with_dot, CONTENT_SETTINGS_TYPE_POPUPS, ""));
393}
394
395TEST_F(HostContentSettingsMapTest, NestedSettings) {
396  TestingProfile profile;
397  HostContentSettingsMap* host_content_settings_map =
398      profile.GetHostContentSettingsMap();
399
400  GURL host("http://a.b.example.com/");
401  ContentSettingsPattern pattern1("[*.]example.com");
402  ContentSettingsPattern pattern2("[*.]b.example.com");
403  ContentSettingsPattern pattern3("a.b.example.com");
404
405  host_content_settings_map->SetContentSetting(pattern1,
406      CONTENT_SETTINGS_TYPE_IMAGES, "", CONTENT_SETTING_BLOCK);
407  host_content_settings_map->SetContentSetting(pattern2,
408      CONTENT_SETTINGS_TYPE_COOKIES, "", CONTENT_SETTING_BLOCK);
409  host_content_settings_map->SetContentSetting(pattern3,
410      CONTENT_SETTINGS_TYPE_PLUGINS, "", CONTENT_SETTING_BLOCK);
411  host_content_settings_map->SetDefaultContentSetting(
412      CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK);
413
414  ContentSettings desired_settings;
415  desired_settings.settings[CONTENT_SETTINGS_TYPE_COOKIES] =
416      CONTENT_SETTING_BLOCK;
417  desired_settings.settings[CONTENT_SETTINGS_TYPE_IMAGES] =
418      CONTENT_SETTING_BLOCK;
419  desired_settings.settings[CONTENT_SETTINGS_TYPE_JAVASCRIPT] =
420      CONTENT_SETTING_BLOCK;
421  desired_settings.settings[CONTENT_SETTINGS_TYPE_PLUGINS] =
422      CONTENT_SETTING_BLOCK;
423  desired_settings.settings[CONTENT_SETTINGS_TYPE_POPUPS] =
424      CONTENT_SETTING_BLOCK;
425  desired_settings.settings[CONTENT_SETTINGS_TYPE_GEOLOCATION] =
426      CONTENT_SETTING_ASK;
427  desired_settings.settings[CONTENT_SETTINGS_TYPE_NOTIFICATIONS] =
428      CONTENT_SETTING_ASK;
429  desired_settings.settings[CONTENT_SETTINGS_TYPE_PRERENDER] =
430      CONTENT_SETTING_ALLOW;
431  ContentSettings settings =
432      host_content_settings_map->GetContentSettings(host);
433  EXPECT_TRUE(SettingsEqual(desired_settings, settings));
434  EXPECT_EQ(desired_settings.settings[CONTENT_SETTINGS_TYPE_COOKIES],
435            settings.settings[CONTENT_SETTINGS_TYPE_COOKIES]);
436  EXPECT_EQ(desired_settings.settings[CONTENT_SETTINGS_TYPE_IMAGES],
437            settings.settings[CONTENT_SETTINGS_TYPE_IMAGES]);
438  EXPECT_EQ(desired_settings.settings[CONTENT_SETTINGS_TYPE_PLUGINS],
439            settings.settings[CONTENT_SETTINGS_TYPE_PLUGINS]);
440  EXPECT_EQ(desired_settings.settings[CONTENT_SETTINGS_TYPE_POPUPS],
441            settings.settings[CONTENT_SETTINGS_TYPE_POPUPS]);
442  EXPECT_EQ(desired_settings.settings[CONTENT_SETTINGS_TYPE_GEOLOCATION],
443            settings.settings[CONTENT_SETTINGS_TYPE_GEOLOCATION]);
444  EXPECT_EQ(desired_settings.settings[CONTENT_SETTINGS_TYPE_COOKIES],
445            settings.settings[CONTENT_SETTINGS_TYPE_COOKIES]);
446}
447
448TEST_F(HostContentSettingsMapTest, OffTheRecord) {
449  TestingProfile profile;
450  HostContentSettingsMap* host_content_settings_map =
451      profile.GetHostContentSettingsMap();
452  profile.set_incognito(true);
453  scoped_refptr<HostContentSettingsMap> otr_map(
454      new HostContentSettingsMap(&profile));
455  profile.set_incognito(false);
456
457  GURL host("http://example.com/");
458  ContentSettingsPattern pattern("[*.]example.com");
459
460  EXPECT_EQ(CONTENT_SETTING_ALLOW,
461            host_content_settings_map->GetContentSetting(
462                host, CONTENT_SETTINGS_TYPE_IMAGES, ""));
463  EXPECT_EQ(CONTENT_SETTING_ALLOW,
464            otr_map->GetContentSetting(
465                host, CONTENT_SETTINGS_TYPE_IMAGES, ""));
466
467  // Changing content settings on the main map should also affect the
468  // incognito map.
469  host_content_settings_map->SetContentSetting(pattern,
470      CONTENT_SETTINGS_TYPE_IMAGES, "", CONTENT_SETTING_BLOCK);
471  EXPECT_EQ(CONTENT_SETTING_BLOCK,
472            host_content_settings_map->GetContentSetting(
473                host, CONTENT_SETTINGS_TYPE_IMAGES, ""));
474  EXPECT_EQ(CONTENT_SETTING_BLOCK,
475            otr_map->GetContentSetting(
476                host, CONTENT_SETTINGS_TYPE_IMAGES, ""));
477
478  // Changing content settings on the incognito map should NOT affect the
479  // main map.
480  otr_map->SetContentSetting(pattern,
481      CONTENT_SETTINGS_TYPE_IMAGES, "", CONTENT_SETTING_ALLOW);
482  EXPECT_EQ(CONTENT_SETTING_BLOCK,
483            host_content_settings_map->GetContentSetting(
484                host, CONTENT_SETTINGS_TYPE_IMAGES, ""));
485  EXPECT_EQ(CONTENT_SETTING_ALLOW,
486            otr_map->GetContentSetting(
487                host, CONTENT_SETTINGS_TYPE_IMAGES, ""));
488}
489
490TEST_F(HostContentSettingsMapTest, MigrateObsoletePrefs) {
491  // This feature is currently behind a flag.
492  CommandLine* cmd = CommandLine::ForCurrentProcess();
493  AutoReset<CommandLine> auto_reset(cmd, *cmd);
494  cmd->AppendSwitch(switches::kEnableResourceContentSettings);
495
496  TestingProfile profile;
497  PrefService* prefs = profile.GetPrefs();
498
499  // Set obsolete data.
500  prefs->SetInteger(prefs::kCookieBehavior,
501                    net::StaticCookiePolicy::BLOCK_ALL_COOKIES);
502
503  ListValue popup_hosts;
504  popup_hosts.Append(new StringValue("[*.]example.com"));
505  prefs->Set(prefs::kPopupWhitelistedHosts, popup_hosts);
506
507  HostContentSettingsMap* host_content_settings_map =
508      profile.GetHostContentSettingsMap();
509
510  EXPECT_EQ(CONTENT_SETTING_BLOCK,
511            host_content_settings_map->GetDefaultContentSetting(
512                CONTENT_SETTINGS_TYPE_COOKIES));
513
514  GURL host("http://example.com");
515  EXPECT_EQ(CONTENT_SETTING_ALLOW,
516            host_content_settings_map->GetContentSetting(
517                host, CONTENT_SETTINGS_TYPE_POPUPS, ""));
518}
519
520TEST_F(HostContentSettingsMapTest, MigrateObsoleteNotificationsPrefs) {
521  TestingProfile profile;
522  PrefService* prefs = profile.GetPrefs();
523
524  // Set obsolete data.
525  prefs->SetInteger(prefs::kDesktopNotificationDefaultContentSetting,
526                    CONTENT_SETTING_ALLOW);
527
528  HostContentSettingsMap* host_content_settings_map =
529      profile.GetHostContentSettingsMap();
530
531  EXPECT_EQ(CONTENT_SETTING_ALLOW,
532            host_content_settings_map->GetDefaultContentSetting(
533                CONTENT_SETTINGS_TYPE_NOTIFICATIONS));
534
535  // Check if the pref was migrated correctly.
536  const DictionaryValue* default_settings_dictionary =
537      prefs->GetDictionary(prefs::kDefaultContentSettings);
538  int value;
539  default_settings_dictionary->GetIntegerWithoutPathExpansion(
540      "notifications", &value);
541  EXPECT_EQ(CONTENT_SETTING_ALLOW, ContentSetting(value));
542}
543
544// For a single Unicode encoded pattern, check if it gets converted to punycode
545// and old pattern gets deleted.
546TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeOnly) {
547  TestingProfile profile;
548  PrefService* prefs = profile.GetPrefs();
549
550  // Set utf-8 data.
551  {
552    DictionaryPrefUpdate update(prefs, prefs::kContentSettingsPatterns);
553    DictionaryValue* all_settings_dictionary = update.Get();
554    ASSERT_TRUE(NULL != all_settings_dictionary);
555
556    DictionaryValue* dummy_payload = new DictionaryValue;
557    dummy_payload->SetInteger("images", CONTENT_SETTING_ALLOW);
558    all_settings_dictionary->SetWithoutPathExpansion("[*.]\xC4\x87ira.com",
559                                                     dummy_payload);
560  }
561  profile.GetHostContentSettingsMap();
562
563  const DictionaryValue* all_settings_dictionary =
564      prefs->GetDictionary(prefs::kContentSettingsPatterns);
565  DictionaryValue* result = NULL;
566  EXPECT_FALSE(all_settings_dictionary->GetDictionaryWithoutPathExpansion(
567      "[*.]\xC4\x87ira.com", &result));
568  EXPECT_TRUE(all_settings_dictionary->GetDictionaryWithoutPathExpansion(
569      "[*.]xn--ira-ppa.com", &result));
570}
571
572// If both Unicode and its punycode pattern exist, make sure we don't touch the
573// settings for the punycode, and that Unicode pattern gets deleted.
574TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeAndPunycode) {
575  // This feature is currently behind a flag.
576  CommandLine* cmd = CommandLine::ForCurrentProcess();
577  AutoReset<CommandLine> auto_reset(cmd, *cmd);
578  cmd->AppendSwitch(switches::kEnableResourceContentSettings);
579
580  TestingProfile profile;
581
582  scoped_ptr<Value> value(base::JSONReader::Read(
583      "{\"[*.]\\xC4\\x87ira.com\":{\"per_plugin\":{\"pluginx\":2}}}", false));
584  profile.GetPrefs()->Set(prefs::kContentSettingsPatterns, *value);
585
586  // Set punycode equivalent, with different setting.
587  scoped_ptr<Value> puny_value(base::JSONReader::Read(
588      "{\"[*.]xn--ira-ppa.com\":{\"per_plugin\":{\"pluginy\":2}}}", false));
589  profile.GetPrefs()->Set(prefs::kContentSettingsPatterns, *puny_value);
590
591  // Initialize the content map.
592  profile.GetHostContentSettingsMap();
593
594  const DictionaryValue* content_setting_prefs =
595      profile.GetPrefs()->GetDictionary(prefs::kContentSettingsPatterns);
596  std::string prefs_as_json;
597  base::JSONWriter::Write(content_setting_prefs, false, &prefs_as_json);
598  EXPECT_STREQ("{\"[*.]xn--ira-ppa.com\":{\"per_plugin\":{\"pluginy\":2}}}",
599               prefs_as_json.c_str());
600}
601
602TEST_F(HostContentSettingsMapTest, NonDefaultSettings) {
603  TestingProfile profile;
604  HostContentSettingsMap* host_content_settings_map =
605      profile.GetHostContentSettingsMap();
606
607  GURL host("http://example.com/");
608  ContentSettingsPattern pattern("[*.]example.com");
609
610  ContentSettings desired_settings(CONTENT_SETTING_DEFAULT);
611  ContentSettings settings =
612    host_content_settings_map->GetNonDefaultContentSettings(host);
613  EXPECT_TRUE(SettingsEqual(desired_settings, settings));
614
615  host_content_settings_map->SetContentSetting(pattern,
616      CONTENT_SETTINGS_TYPE_IMAGES, "", CONTENT_SETTING_BLOCK);
617  desired_settings.settings[CONTENT_SETTINGS_TYPE_IMAGES] =
618      CONTENT_SETTING_BLOCK;
619  settings =
620    host_content_settings_map->GetNonDefaultContentSettings(host);
621  EXPECT_TRUE(SettingsEqual(desired_settings, settings));
622}
623
624TEST_F(HostContentSettingsMapTest, ResourceIdentifier) {
625  // This feature is currently behind a flag.
626  CommandLine* cmd = CommandLine::ForCurrentProcess();
627  AutoReset<CommandLine> auto_reset(cmd, *cmd);
628  cmd->AppendSwitch(switches::kEnableResourceContentSettings);
629
630  TestingProfile profile;
631  HostContentSettingsMap* host_content_settings_map =
632      profile.GetHostContentSettingsMap();
633
634  GURL host("http://example.com/");
635  ContentSettingsPattern pattern("[*.]example.com");
636  std::string resource1("someplugin");
637  std::string resource2("otherplugin");
638
639  EXPECT_EQ(CONTENT_SETTING_ALLOW,
640            host_content_settings_map->GetContentSetting(
641                host, CONTENT_SETTINGS_TYPE_PLUGINS, resource1));
642  host_content_settings_map->SetContentSetting(pattern,
643      CONTENT_SETTINGS_TYPE_PLUGINS, resource1, CONTENT_SETTING_BLOCK);
644  EXPECT_EQ(CONTENT_SETTING_BLOCK,
645            host_content_settings_map->GetContentSetting(
646                host, CONTENT_SETTINGS_TYPE_PLUGINS, resource1));
647  EXPECT_EQ(CONTENT_SETTING_ALLOW,
648            host_content_settings_map->GetContentSetting(
649                host, CONTENT_SETTINGS_TYPE_PLUGINS, resource2));
650
651  // If resource content settings are enabled GetContentSettings should return
652  // CONTENT_SETTING_DEFAULT for content types that require resource
653  // identifiers.
654  ContentSettings settings =
655      host_content_settings_map->GetContentSettings(host);
656  EXPECT_EQ(CONTENT_SETTING_DEFAULT,
657            settings.settings[CONTENT_SETTINGS_TYPE_PLUGINS]);
658}
659
660TEST_F(HostContentSettingsMapTest, ResourceIdentifierPrefs) {
661  // This feature is currently behind a flag.
662  CommandLine* cmd = CommandLine::ForCurrentProcess();
663  AutoReset<CommandLine> auto_reset(cmd, *cmd);
664  cmd->AppendSwitch(switches::kEnableResourceContentSettings);
665
666  TestingProfile profile;
667  scoped_ptr<Value> value(base::JSONReader::Read(
668      "{\"[*.]example.com\":{\"per_plugin\":{\"someplugin\":2}}}", false));
669  profile.GetPrefs()->Set(prefs::kContentSettingsPatterns, *value);
670  HostContentSettingsMap* host_content_settings_map =
671      profile.GetHostContentSettingsMap();
672
673  GURL host("http://example.com/");
674  ContentSettingsPattern pattern("[*.]example.com");
675  std::string resource1("someplugin");
676  std::string resource2("otherplugin");
677
678  EXPECT_EQ(CONTENT_SETTING_BLOCK,
679            host_content_settings_map->GetContentSetting(
680                host, CONTENT_SETTINGS_TYPE_PLUGINS, resource1));
681
682  host_content_settings_map->SetContentSetting(pattern,
683      CONTENT_SETTINGS_TYPE_PLUGINS, resource1, CONTENT_SETTING_DEFAULT);
684
685  const DictionaryValue* content_setting_prefs =
686      profile.GetPrefs()->GetDictionary(prefs::kContentSettingsPatterns);
687  std::string prefs_as_json;
688  base::JSONWriter::Write(content_setting_prefs, false, &prefs_as_json);
689  EXPECT_STREQ("{}", prefs_as_json.c_str());
690
691  host_content_settings_map->SetContentSetting(pattern,
692      CONTENT_SETTINGS_TYPE_PLUGINS, resource2, CONTENT_SETTING_BLOCK);
693
694  content_setting_prefs =
695      profile.GetPrefs()->GetDictionary(prefs::kContentSettingsPatterns);
696  base::JSONWriter::Write(content_setting_prefs, false, &prefs_as_json);
697  EXPECT_STREQ("{\"[*.]example.com\":{\"per_plugin\":{\"otherplugin\":2}}}",
698               prefs_as_json.c_str());
699}
700
701// If a default-content-setting is managed, the managed value should be used
702// instead of the default value.
703TEST_F(HostContentSettingsMapTest, ManagedDefaultContentSetting) {
704  TestingProfile profile;
705  HostContentSettingsMap* host_content_settings_map =
706      profile.GetHostContentSettingsMap();
707  TestingPrefService* prefs = profile.GetTestingPrefService();
708
709  EXPECT_EQ(CONTENT_SETTING_ALLOW,
710            host_content_settings_map->GetDefaultContentSetting(
711                CONTENT_SETTINGS_TYPE_JAVASCRIPT));
712
713  // Set managed-default-content-setting through the coresponding preferences.
714  prefs->SetManagedPref(prefs::kManagedDefaultJavaScriptSetting,
715                        Value::CreateIntegerValue(CONTENT_SETTING_BLOCK));
716  EXPECT_EQ(CONTENT_SETTING_BLOCK,
717            host_content_settings_map->GetDefaultContentSetting(
718                CONTENT_SETTINGS_TYPE_JAVASCRIPT));
719
720  // Remove managed-default-content-settings-preferences.
721  prefs->RemoveManagedPref(prefs::kManagedDefaultJavaScriptSetting);
722  EXPECT_EQ(CONTENT_SETTING_ALLOW,
723            host_content_settings_map->GetDefaultContentSetting(
724                CONTENT_SETTINGS_TYPE_JAVASCRIPT));
725
726  // Set preference to manage the default-content-setting for Plugins.
727  prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting,
728                        Value::CreateIntegerValue(CONTENT_SETTING_BLOCK));
729  EXPECT_EQ(CONTENT_SETTING_BLOCK,
730            host_content_settings_map->GetDefaultContentSetting(
731                CONTENT_SETTINGS_TYPE_PLUGINS));
732
733  // Remove the preference to manage the default-content-setting for Plugins.
734  prefs->RemoveManagedPref(prefs::kManagedDefaultPluginsSetting);
735  EXPECT_EQ(CONTENT_SETTING_ALLOW,
736            host_content_settings_map->GetDefaultContentSetting(
737                CONTENT_SETTINGS_TYPE_PLUGINS));
738}
739
740TEST_F(HostContentSettingsMapTest,
741       GetNonDefaultContentSettingsIfTypeManaged) {
742  TestingProfile profile;
743  HostContentSettingsMap* host_content_settings_map =
744      profile.GetHostContentSettingsMap();
745  TestingPrefService* prefs = profile.GetTestingPrefService();
746
747  // Set pattern for JavaScript setting.
748  ContentSettingsPattern pattern("[*.]example.com");
749  host_content_settings_map->SetContentSetting(pattern,
750      CONTENT_SETTINGS_TYPE_JAVASCRIPT, "", CONTENT_SETTING_BLOCK);
751
752  EXPECT_EQ(CONTENT_SETTING_ALLOW,
753            host_content_settings_map->GetDefaultContentSetting(
754                CONTENT_SETTINGS_TYPE_JAVASCRIPT));
755
756  GURL host("http://example.com/");
757  EXPECT_EQ(CONTENT_SETTING_BLOCK,
758            host_content_settings_map->GetContentSetting(
759                host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, ""));
760
761  // Set managed-default-content-setting for content-settings-type JavaScript.
762  prefs->SetManagedPref(prefs::kManagedDefaultJavaScriptSetting,
763                        Value::CreateIntegerValue(CONTENT_SETTING_ALLOW));
764  EXPECT_EQ(CONTENT_SETTING_ALLOW,
765            host_content_settings_map->GetContentSetting(
766                host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, ""));
767}
768
769// Managed default content setting should have higher priority
770// than user defined patterns.
771TEST_F(HostContentSettingsMapTest,
772       ManagedDefaultContentSettingIgnoreUserPattern) {
773  TestingProfile profile;
774  HostContentSettingsMap* host_content_settings_map =
775      profile.GetHostContentSettingsMap();
776  TestingPrefService* prefs = profile.GetTestingPrefService();
777
778  // Block all JavaScript.
779  host_content_settings_map->SetDefaultContentSetting(
780      CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK);
781
782  // Set an exception to allow "[*.]example.com"
783  ContentSettingsPattern pattern("[*.]example.com");
784  host_content_settings_map->SetContentSetting(pattern,
785      CONTENT_SETTINGS_TYPE_JAVASCRIPT, "", CONTENT_SETTING_ALLOW);
786
787  EXPECT_EQ(CONTENT_SETTING_BLOCK,
788            host_content_settings_map->GetDefaultContentSetting(
789                CONTENT_SETTINGS_TYPE_JAVASCRIPT));
790  GURL host("http://example.com/");
791  EXPECT_EQ(CONTENT_SETTING_ALLOW,
792            host_content_settings_map->GetContentSetting(
793                host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, ""));
794
795  // Set managed-default-content-settings-preferences.
796  prefs->SetManagedPref(prefs::kManagedDefaultJavaScriptSetting,
797                        Value::CreateIntegerValue(CONTENT_SETTING_BLOCK));
798  EXPECT_EQ(CONTENT_SETTING_BLOCK,
799            host_content_settings_map->GetContentSetting(
800                host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, ""));
801
802  // Remove managed-default-content-settings-preferences.
803  prefs->RemoveManagedPref(prefs::kManagedDefaultJavaScriptSetting);
804  EXPECT_EQ(CONTENT_SETTING_ALLOW,
805            host_content_settings_map->GetContentSetting(
806                host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, ""));
807}
808
809// If a default-content-setting is set to managed setting, the user defined
810// setting should be preserved.
811TEST_F(HostContentSettingsMapTest, OverwrittenDefaultContentSetting) {
812  TestingProfile profile;
813  HostContentSettingsMap* host_content_settings_map =
814      profile.GetHostContentSettingsMap();
815  TestingPrefService* prefs = profile.GetTestingPrefService();
816
817  // Set user defined default-content-setting for Cookies.
818  host_content_settings_map->SetDefaultContentSetting(
819      CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK);
820  EXPECT_EQ(CONTENT_SETTING_BLOCK,
821            host_content_settings_map->GetDefaultContentSetting(
822                CONTENT_SETTINGS_TYPE_COOKIES));
823
824  // Set preference to manage the default-content-setting for Cookies.
825  prefs->SetManagedPref(prefs::kManagedDefaultCookiesSetting,
826                        Value::CreateIntegerValue(CONTENT_SETTING_ALLOW));
827  EXPECT_EQ(CONTENT_SETTING_ALLOW,
828            host_content_settings_map->GetDefaultContentSetting(
829                CONTENT_SETTINGS_TYPE_COOKIES));
830
831  // Remove the preference to manage the default-content-setting for Cookies.
832  prefs->RemoveManagedPref(prefs::kManagedDefaultCookiesSetting);
833  EXPECT_EQ(CONTENT_SETTING_BLOCK,
834            host_content_settings_map->GetDefaultContentSetting(
835                CONTENT_SETTINGS_TYPE_COOKIES));
836}
837
838// If a setting for a default-content-setting-type is set while the type is
839// managed, then the new setting should be preserved and used after the
840// default-content-setting-type is not managed anymore.
841TEST_F(HostContentSettingsMapTest, SettingDefaultContentSettingsWhenManaged) {
842  TestingProfile profile;
843  HostContentSettingsMap* host_content_settings_map =
844      profile.GetHostContentSettingsMap();
845  TestingPrefService* prefs = profile.GetTestingPrefService();
846
847  prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting,
848                        Value::CreateIntegerValue(CONTENT_SETTING_ALLOW));
849  EXPECT_EQ(CONTENT_SETTING_ALLOW,
850            host_content_settings_map->GetDefaultContentSetting(
851                CONTENT_SETTINGS_TYPE_PLUGINS));
852
853  host_content_settings_map->SetDefaultContentSetting(
854      CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK);
855  EXPECT_EQ(CONTENT_SETTING_ALLOW,
856            host_content_settings_map->GetDefaultContentSetting(
857                CONTENT_SETTINGS_TYPE_PLUGINS));
858
859  prefs->RemoveManagedPref(prefs::kManagedDefaultPluginsSetting);
860  EXPECT_EQ(CONTENT_SETTING_BLOCK,
861            host_content_settings_map->GetDefaultContentSetting(
862                CONTENT_SETTINGS_TYPE_PLUGINS));
863}
864
865TEST_F(HostContentSettingsMapTest, ResetToDefaultsWhenManaged) {
866  TestingProfile profile;
867  HostContentSettingsMap* host_content_settings_map =
868      profile.GetHostContentSettingsMap();
869  TestingPrefService* prefs = profile.GetTestingPrefService();
870
871  prefs->SetManagedPref(prefs::kBlockThirdPartyCookies,
872                        Value::CreateBooleanValue(true));
873  prefs->SetUserPref(prefs::kBlockThirdPartyCookies,
874                     Value::CreateBooleanValue(true));
875
876  EXPECT_TRUE(host_content_settings_map->IsBlockThirdPartyCookiesManaged());
877  EXPECT_TRUE(host_content_settings_map->BlockThirdPartyCookies());
878
879  // Reset to the default value (false).
880  host_content_settings_map->ResetToDefaults();
881  // Since the preference BlockThirdPartyCookies is managed the
882  // HostContentSettingsMap should still return the managed value which is true.
883  EXPECT_TRUE(host_content_settings_map->IsBlockThirdPartyCookiesManaged());
884  EXPECT_TRUE(host_content_settings_map->BlockThirdPartyCookies());
885
886  // After unsetting the managed value for the preference BlockThirdPartyCookies
887  // the default value should be returned now.
888  prefs->RemoveManagedPref(prefs::kBlockThirdPartyCookies);
889  EXPECT_FALSE(host_content_settings_map->IsBlockThirdPartyCookiesManaged());
890  EXPECT_FALSE(host_content_settings_map->BlockThirdPartyCookies());
891}
892
893}  // namespace
894