• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2010 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 "chrome/browser/content_settings/content_settings_policy_provider.h"
6 
7 #include "base/auto_reset.h"
8 #include "base/command_line.h"
9 #include "chrome/browser/content_settings/stub_settings_observer.h"
10 #include "chrome/browser/prefs/pref_service.h"
11 #include "chrome/common/chrome_switches.h"
12 #include "chrome/common/pref_names.h"
13 #include "chrome/common/url_constants.h"
14 #include "chrome/test/testing_browser_process_test.h"
15 #include "chrome/test/testing_pref_service.h"
16 #include "chrome/test/testing_profile.h"
17 #include "content/browser/browser_thread.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "googleurl/src/gurl.h"
20 
21 namespace content_settings {
22 
23 class PolicyDefaultProviderTest : public TestingBrowserProcessTest {
24  public:
PolicyDefaultProviderTest()25   PolicyDefaultProviderTest()
26       : ui_thread_(BrowserThread::UI, &message_loop_) {
27   }
28 
29  protected:
30   MessageLoop message_loop_;
31   BrowserThread ui_thread_;
32 };
33 
TEST_F(PolicyDefaultProviderTest,DefaultValues)34 TEST_F(PolicyDefaultProviderTest, DefaultValues) {
35   TestingProfile profile;
36   PolicyDefaultProvider provider(&profile);
37   TestingPrefService* prefs = profile.GetTestingPrefService();
38 
39   // By default, policies should be off.
40   ASSERT_FALSE(
41       provider.DefaultSettingIsManaged(CONTENT_SETTINGS_TYPE_COOKIES));
42 
43   // Set managed-default-content-setting through the coresponding preferences.
44   prefs->SetManagedPref(prefs::kManagedDefaultCookiesSetting,
45                         Value::CreateIntegerValue(CONTENT_SETTING_BLOCK));
46   ASSERT_TRUE(
47       provider.DefaultSettingIsManaged(CONTENT_SETTINGS_TYPE_COOKIES));
48   ASSERT_EQ(CONTENT_SETTING_BLOCK,
49             provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES));
50 
51   // Remove managed-default-content-settings-preferences.
52   prefs->RemoveManagedPref(prefs::kManagedDefaultCookiesSetting);
53   ASSERT_FALSE(
54       provider.DefaultSettingIsManaged(CONTENT_SETTINGS_TYPE_COOKIES));
55 }
56 
57 // When a default-content-setting is set to a managed setting a
58 // CONTENT_SETTINGS_CHANGED notification should be fired. The same should happen
59 // if the managed setting is removed.
TEST_F(PolicyDefaultProviderTest,ObserveManagedSettingsChange)60 TEST_F(PolicyDefaultProviderTest, ObserveManagedSettingsChange) {
61   TestingProfile profile;
62   StubSettingsObserver observer;
63   // Make sure the content settings map exists.
64   profile.GetHostContentSettingsMap();
65   TestingPrefService* prefs = profile.GetTestingPrefService();
66 
67   // Set the managed default-content-setting.
68   prefs->SetManagedPref(prefs::kManagedDefaultImagesSetting,
69                         Value::CreateIntegerValue(CONTENT_SETTING_BLOCK));
70   EXPECT_EQ(profile.GetHostContentSettingsMap(), observer.last_notifier);
71   EXPECT_EQ(ContentSettingsPattern(), observer.last_pattern);
72   EXPECT_EQ(CONTENT_SETTINGS_TYPE_DEFAULT, observer.last_type);
73   EXPECT_TRUE(observer.last_update_all);
74   EXPECT_TRUE(observer.last_update_all_types);
75   EXPECT_EQ(1, observer.counter);
76 
77   // Remove the managed default-content-setting.
78   prefs->RemoveManagedPref(prefs::kManagedDefaultImagesSetting);
79   EXPECT_EQ(profile.GetHostContentSettingsMap(), observer.last_notifier);
80   EXPECT_EQ(CONTENT_SETTINGS_TYPE_DEFAULT, observer.last_type);
81   EXPECT_EQ(ContentSettingsPattern(), observer.last_pattern);
82   EXPECT_TRUE(observer.last_update_all);
83   EXPECT_TRUE(observer.last_update_all_types);
84   EXPECT_EQ(2, observer.counter);
85 }
86 
87 class PolicyProviderTest : public testing::Test {
88  public:
PolicyProviderTest()89   PolicyProviderTest()
90       : ui_thread_(BrowserThread::UI, &message_loop_) {
91   }
92 
93  protected:
94   // TODO(markusheintz): Check if it's possible to derive the provider class
95   // from NonThreadSafe and to use native thread identifiers instead of
96   // BrowserThread IDs. Then we could get rid of the message_loop and ui_thread
97   // fields.
98   MessageLoop message_loop_;
99   BrowserThread ui_thread_;
100 };
101 
TEST_F(PolicyProviderTest,Default)102 TEST_F(PolicyProviderTest, Default) {
103   TestingProfile profile;
104   TestingPrefService* prefs = profile.GetTestingPrefService();
105 
106   ListValue* value = new ListValue();
107   value->Append(Value::CreateStringValue("[*.]google.com"));
108   prefs->SetManagedPref(prefs::kManagedImagesBlockedForUrls,
109                         value);
110 
111   PolicyProvider provider(static_cast<Profile*>(&profile));
112 
113   ContentSettingsPattern yt_url_pattern("www.youtube.com");
114   GURL youtube_url("http://www.youtube.com");
115   GURL google_url("http://mail.google.com");
116 
117   EXPECT_EQ(CONTENT_SETTING_DEFAULT,
118             provider.GetContentSetting(
119                 youtube_url, youtube_url, CONTENT_SETTINGS_TYPE_COOKIES, ""));
120   EXPECT_EQ(CONTENT_SETTING_BLOCK,
121             provider.GetContentSetting(
122                 google_url, google_url, CONTENT_SETTINGS_TYPE_IMAGES, ""));
123 
124   provider.SetContentSetting(
125       yt_url_pattern,
126       yt_url_pattern,
127       CONTENT_SETTINGS_TYPE_COOKIES,
128       "",
129       CONTENT_SETTING_BLOCK);
130   EXPECT_EQ(CONTENT_SETTING_DEFAULT,
131             provider.GetContentSetting(
132                 youtube_url, youtube_url, CONTENT_SETTINGS_TYPE_COOKIES, ""));
133 }
134 
TEST_F(PolicyProviderTest,ResourceIdentifier)135 TEST_F(PolicyProviderTest, ResourceIdentifier) {
136   CommandLine* cmd = CommandLine::ForCurrentProcess();
137   AutoReset<CommandLine> auto_reset(cmd, *cmd);
138   cmd->AppendSwitch(switches::kEnableResourceContentSettings);
139 
140   TestingProfile profile;
141   TestingPrefService* prefs = profile.GetTestingPrefService();
142 
143   ListValue* value = new ListValue();
144   value->Append(Value::CreateStringValue("[*.]google.com"));
145   prefs->SetManagedPref(prefs::kManagedPluginsAllowedForUrls,
146                         value);
147 
148   PolicyProvider provider(static_cast<Profile*>(&profile));
149 
150   GURL youtube_url("http://www.youtube.com");
151   GURL google_url("http://mail.google.com");
152 
153   EXPECT_EQ(CONTENT_SETTING_DEFAULT,
154             provider.GetContentSetting(
155                 youtube_url,
156                 youtube_url,
157                 CONTENT_SETTINGS_TYPE_PLUGINS,
158                 "someplugin"));
159 
160   // There is no policy support for resource content settings until the feature
161   // is enabled by default. Resource identifiers are simply ignored by the
162   // PolicyProvider.
163   EXPECT_EQ(CONTENT_SETTING_ALLOW,
164             provider.GetContentSetting(
165                 google_url,
166                 google_url,
167                 CONTENT_SETTINGS_TYPE_PLUGINS,
168                 ""));
169 
170   EXPECT_EQ(CONTENT_SETTING_ALLOW,
171             provider.GetContentSetting(
172                 google_url,
173                 google_url,
174                 CONTENT_SETTINGS_TYPE_PLUGINS,
175                 "someplugin"));
176 
177   EXPECT_EQ(CONTENT_SETTING_ALLOW,
178             provider.GetContentSetting(
179                 google_url,
180                 google_url,
181                 CONTENT_SETTINGS_TYPE_PLUGINS,
182                 "anotherplugin"));
183 }
184 
185 }  // namespace content_settings
186