geolocation_content_settings_map_unittest.cc revision c407dc5cd9bdc5668497f21b26b09d988ab439de
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/geolocation/geolocation_content_settings_map.h"
6
7#include "chrome/common/pref_names.h"
8#include "chrome/test/testing_profile.h"
9#include "testing/gtest/include/gtest/gtest.h"
10
11
12namespace {
13
14class GeolocationContentSettingsMapTests : public testing::Test {
15 public:
16  GeolocationContentSettingsMapTests()
17    : ui_thread_(ChromeThread::UI, &message_loop_) {
18  }
19
20 protected:
21  MessageLoop message_loop_;
22  ChromeThread ui_thread_;
23};
24
25TEST_F(GeolocationContentSettingsMapTests, DefaultValues) {
26  TestingProfile profile;
27  GeolocationContentSettingsMap* map =
28      profile.GetGeolocationContentSettingsMap();
29
30  // Check setting defaults.
31  EXPECT_EQ(CONTENT_SETTING_ASK, map->GetDefaultContentSetting());
32  map->SetDefaultContentSetting(CONTENT_SETTING_BLOCK);
33  EXPECT_EQ(CONTENT_SETTING_BLOCK, map->GetDefaultContentSetting());
34}
35
36TEST_F(GeolocationContentSettingsMapTests, Embedder) {
37  TestingProfile profile;
38  GeolocationContentSettingsMap* map =
39      profile.GetGeolocationContentSettingsMap();
40  GURL top_level("http://www.toplevel0.com/foo/bar");
41  EXPECT_EQ(CONTENT_SETTING_ASK, map->GetContentSetting(top_level, top_level));
42  // Now set the permission for requester_0.
43  map->SetContentSetting(top_level, top_level, CONTENT_SETTING_ALLOW);
44  EXPECT_EQ(CONTENT_SETTING_ALLOW,
45            map->GetContentSetting(top_level, top_level));
46
47  GURL requester_0("http://www.frame0.com/foo/bar");
48  EXPECT_EQ(CONTENT_SETTING_ASK,
49            map->GetContentSetting(requester_0, top_level));
50  // Now set the permission for only requester_1.
51  map->SetContentSetting(requester_0, top_level, CONTENT_SETTING_ALLOW);
52  EXPECT_EQ(CONTENT_SETTING_ALLOW,
53            map->GetContentSetting(top_level, top_level));
54  EXPECT_EQ(CONTENT_SETTING_ALLOW,
55            map->GetContentSetting(requester_0, top_level));
56  // Block only requester_1.
57  map->SetContentSetting(requester_0, top_level, CONTENT_SETTING_BLOCK);
58  EXPECT_EQ(CONTENT_SETTING_BLOCK,
59            map->GetContentSetting(requester_0, top_level));
60  EXPECT_EQ(CONTENT_SETTING_ALLOW,
61            map->GetContentSetting(top_level, top_level));
62}
63
64TEST_F(GeolocationContentSettingsMapTests, MultipleEmbeddersAndOrigins) {
65  TestingProfile profile;
66  GeolocationContentSettingsMap* map =
67      profile.GetGeolocationContentSettingsMap();
68  GURL requester_0("http://www.iframe0.com/foo/bar");
69  GURL requester_1("http://www.iframe1.co.uk/bar/foo");
70  GURL embedder_0("http://www.toplevel0.com/foo/bar");
71  EXPECT_EQ(CONTENT_SETTING_ASK,
72            map->GetContentSetting(requester_0, embedder_0));
73  EXPECT_EQ(CONTENT_SETTING_ASK,
74            map->GetContentSetting(requester_1, embedder_0));
75  // Now set the permission for only one origin.
76  map->SetContentSetting(requester_0, embedder_0, CONTENT_SETTING_ALLOW);
77  EXPECT_EQ(CONTENT_SETTING_ALLOW,
78            map->GetContentSetting(requester_0, embedder_0));
79  EXPECT_EQ(CONTENT_SETTING_ASK,
80            map->GetContentSetting(requester_1, embedder_0));
81  // Set the permission for the other origin.
82  map->SetContentSetting(requester_1, embedder_0, CONTENT_SETTING_ALLOW);
83  EXPECT_EQ(CONTENT_SETTING_ALLOW,
84            map->GetContentSetting(requester_1, embedder_0));
85  // Check they're not allowed on a different embedder.
86  GURL embedder_1("http://www.toplevel1.com/foo/bar");
87  EXPECT_EQ(CONTENT_SETTING_ASK,
88            map->GetContentSetting(requester_0, embedder_1));
89  EXPECT_EQ(CONTENT_SETTING_ASK,
90            map->GetContentSetting(requester_1, embedder_1));
91  // Check all settings are valid.
92  GeolocationContentSettingsMap::AllOriginsSettings content_settings(
93      map->GetAllOriginsSettings());
94  EXPECT_EQ(0U, content_settings.count(requester_0));
95  EXPECT_EQ(1U, content_settings.count(requester_0.GetOrigin()));
96  GeolocationContentSettingsMap::OneOriginSettings one_origin_settings(
97      content_settings[requester_0.GetOrigin()]);
98  EXPECT_EQ(CONTENT_SETTING_ALLOW, one_origin_settings[embedder_0.GetOrigin()]);
99
100  EXPECT_EQ(0U, content_settings.count(requester_1));
101  EXPECT_EQ(1U, content_settings.count(requester_1.GetOrigin()));
102  one_origin_settings = content_settings[requester_1.GetOrigin()];
103  EXPECT_EQ(CONTENT_SETTING_ALLOW, one_origin_settings[embedder_0.GetOrigin()]);
104  // Block requester_1 on the first embedder and add it to the second.
105  map->SetContentSetting(requester_1, embedder_0, CONTENT_SETTING_BLOCK);
106  map->SetContentSetting(requester_1, embedder_1, CONTENT_SETTING_ALLOW);
107  EXPECT_EQ(CONTENT_SETTING_BLOCK,
108            map->GetContentSetting(requester_1, embedder_0));
109  EXPECT_EQ(CONTENT_SETTING_ALLOW,
110            map->GetContentSetting(requester_1, embedder_1));
111}
112
113TEST_F(GeolocationContentSettingsMapTests, SetContentSettingDefault) {
114  TestingProfile profile;
115  GeolocationContentSettingsMap* map =
116      profile.GetGeolocationContentSettingsMap();
117  GURL top_level("http://www.toplevel0.com/foo/bar");
118  map->SetContentSetting(top_level, top_level, CONTENT_SETTING_ALLOW);
119  EXPECT_EQ(CONTENT_SETTING_ALLOW,
120            map->GetContentSetting(top_level, top_level));
121  // Set to CONTENT_SETTING_DEFAULT and check the actual value has changed.
122  map->SetContentSetting(top_level, top_level, CONTENT_SETTING_DEFAULT);
123  EXPECT_EQ(CONTENT_SETTING_ASK, map->GetContentSetting(top_level, top_level));
124}
125
126TEST_F(GeolocationContentSettingsMapTests, Reset) {
127  TestingProfile profile;
128  GeolocationContentSettingsMap* map =
129      profile.GetGeolocationContentSettingsMap();
130  GURL requester_0("http://www.iframe0.com/foo/bar");
131  GURL embedder_0("http://www.toplevel0.com/foo/bar");
132  map->SetContentSetting(requester_0, embedder_0, CONTENT_SETTING_ALLOW);
133  EXPECT_EQ(CONTENT_SETTING_ALLOW,
134            map->GetContentSetting(requester_0, embedder_0));
135  GeolocationContentSettingsMap::AllOriginsSettings content_settings(
136      map->GetAllOriginsSettings());
137  EXPECT_EQ(1U, content_settings.size());
138
139  // Set to CONTENT_SETTING_DEFAULT and check the actual value has changed.
140  map->SetContentSetting(requester_0, embedder_0, CONTENT_SETTING_DEFAULT);
141  EXPECT_EQ(CONTENT_SETTING_ASK,
142            map->GetContentSetting(requester_0, embedder_0));
143  content_settings = map->GetAllOriginsSettings();
144  EXPECT_TRUE(content_settings.empty());
145}
146
147TEST_F(GeolocationContentSettingsMapTests, ClearsWhenSettingBackToDefault) {
148  TestingProfile profile;
149  GeolocationContentSettingsMap* map =
150      profile.GetGeolocationContentSettingsMap();
151  GURL requester_0("http://www.iframe0.com/foo/bar");
152  GURL requester_1("http://www.iframe1.com/foo/bar");
153  GURL embedder_0("http://www.toplevel0.com/foo/bar");
154  map->SetContentSetting(requester_0, embedder_0, CONTENT_SETTING_ALLOW);
155  EXPECT_EQ(CONTENT_SETTING_ALLOW,
156            map->GetContentSetting(requester_0, embedder_0));
157  GeolocationContentSettingsMap::AllOriginsSettings content_settings(
158      map->GetAllOriginsSettings());
159  EXPECT_EQ(1U, content_settings.size());
160
161  map->SetContentSetting(requester_1, embedder_0, CONTENT_SETTING_ALLOW);
162  EXPECT_EQ(CONTENT_SETTING_ALLOW,
163            map->GetContentSetting(requester_1, embedder_0));
164  content_settings = map->GetAllOriginsSettings();
165  EXPECT_EQ(2U, content_settings.size());
166  EXPECT_EQ(1U, content_settings[requester_0.GetOrigin()].size());
167  EXPECT_EQ(1U, content_settings[requester_1.GetOrigin()].size());
168
169  // Set to default.
170  map->SetContentSetting(requester_0, embedder_0, CONTENT_SETTING_DEFAULT);
171  content_settings = map->GetAllOriginsSettings();
172  EXPECT_EQ(1U, content_settings.size());
173
174  map->SetContentSetting(requester_1, embedder_0, CONTENT_SETTING_DEFAULT);
175  content_settings = map->GetAllOriginsSettings();
176  EXPECT_TRUE(content_settings.empty());
177}
178
179TEST_F(GeolocationContentSettingsMapTests, WildCardForEmptyEmbedder) {
180  TestingProfile profile;
181  GeolocationContentSettingsMap* map =
182      profile.GetGeolocationContentSettingsMap();
183  GURL requester_0("http://www.iframe0.com/foo/bar");
184  GURL embedder_0("http://www.toplevel0.com/foo/bar");
185  GURL embedder_1("http://www.toplevel1.com/foo/bar");
186  GURL empty_url;
187  map->SetContentSetting(requester_0, embedder_0, CONTENT_SETTING_BLOCK);
188  map->SetContentSetting(requester_0, empty_url, CONTENT_SETTING_ALLOW);
189  EXPECT_EQ(CONTENT_SETTING_BLOCK,
190            map->GetContentSetting(requester_0, embedder_0));
191  EXPECT_EQ(CONTENT_SETTING_ALLOW,
192            map->GetContentSetting(requester_0, embedder_1));
193  EXPECT_EQ(CONTENT_SETTING_ASK,
194            map->GetContentSetting(requester_0, requester_0));
195
196  // Change the wildcard behavior.
197  map->SetContentSetting(requester_0, embedder_0, CONTENT_SETTING_ALLOW);
198  map->SetContentSetting(requester_0, empty_url, CONTENT_SETTING_BLOCK);
199  map->SetDefaultContentSetting(CONTENT_SETTING_ALLOW);
200  EXPECT_EQ(CONTENT_SETTING_ALLOW,
201            map->GetContentSetting(requester_0, embedder_0));
202  EXPECT_EQ(CONTENT_SETTING_BLOCK,
203            map->GetContentSetting(requester_0, embedder_1));
204  EXPECT_EQ(CONTENT_SETTING_ALLOW,
205            map->GetContentSetting(requester_0, requester_0));
206}
207
208TEST_F(GeolocationContentSettingsMapTests, IgnoreInvalidURLsInPrefs) {
209  TestingProfile profile;
210  DictionaryValue* all_settings_dictionary =
211      profile.GetPrefs()->GetMutableDictionary(
212          prefs::kGeolocationContentSettings);
213  // For simplicity, use the overloads that do path expansion. As '.' is the
214  // path separator, we can't have dotted hostnames (which is fine).
215  all_settings_dictionary->SetInteger(L"http://a/.http://b/",
216                                      CONTENT_SETTING_ALLOW);
217  all_settings_dictionary->SetInteger(L"bad_requester.http://b/",
218                                      CONTENT_SETTING_ALLOW);
219  all_settings_dictionary->SetInteger(L"http://a/.bad-embedder",
220                                      CONTENT_SETTING_ALLOW);
221
222  GeolocationContentSettingsMap* map =
223      profile.GetGeolocationContentSettingsMap();
224  EXPECT_EQ(CONTENT_SETTING_ASK, map->GetDefaultContentSetting());
225
226  // Check the valid entry was read OK.
227  EXPECT_EQ(CONTENT_SETTING_ALLOW,
228            map->GetContentSetting(GURL("http://a/"), GURL("http://b/")));
229  // But everything else should be according to the default.
230  EXPECT_EQ(CONTENT_SETTING_ASK,
231            map->GetContentSetting(GURL("http://a/"),
232            GURL("http://bad-embedder")));
233  EXPECT_EQ(CONTENT_SETTING_ASK,
234            map->GetContentSetting(GURL("http://a/"),
235            GURL("http://example.com")));
236  EXPECT_EQ(CONTENT_SETTING_ASK,
237            map->GetContentSetting(GURL("http://bad_requester/"),
238            GURL("http://b/")));
239}
240
241}  // namespace
242