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