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

Side by Side Diff: chrome/browser/storage/durable_storage_permission_context_unittest.cc

Issue 2385653005: [DurableStorage] Don't grant durable if origin cannot write cookies. (Closed)
Patch Set: Added third party frame check, and incognito check, and fixed cookies check Created 4 years, 2 months 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/browser/storage/durable_storage_permission_context.h" 5 #include "chrome/browser/storage/durable_storage_permission_context.h"
6 6
7 #include "base/bind.h"
7 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
11 #include "chrome/browser/content_settings/cookie_settings_factory.h"
12 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
13 #include "chrome/browser/permissions/permission_request_id.h"
14 #include "chrome/browser/permissions/permission_request_manager.h"
15 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "components/bookmarks/test/bookmark_test_helpers.h"
18 #include "components/content_settings/core/browser/cookie_settings.h"
19 #include "components/content_settings/core/browser/host_content_settings_map.h"
20 #include "content/public/browser/permission_manager.h"
21 #include "content/public/browser/render_process_host.h"
8 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
9 23
10 using bookmarks::BookmarkModel; 24 using bookmarks::BookmarkModel;
11 25
26 namespace {
27
28 void DoNothing(ContentSetting content_setting) {}
29
30 class TestDurablePermissionContext : public DurableStoragePermissionContext {
31 public:
32 explicit TestDurablePermissionContext(Profile* profile)
33 : DurableStoragePermissionContext(profile),
34 permission_set_count_(0),
35 last_permission_set_persisted_(false),
36 last_permission_set_setting_(CONTENT_SETTING_DEFAULT) {}
37
38 int permission_set_count() const { return permission_set_count_; }
39 bool last_permission_set_persisted() const {
40 return last_permission_set_persisted_;
41 }
42 ContentSetting last_permission_set_setting() const {
43 return last_permission_set_setting_;
44 }
45
46 ContentSetting GetContentSettingFromMap(const GURL& url_a,
47 const GURL& url_b) {
48 return HostContentSettingsMapFactory::GetForProfile(profile())
49 ->GetContentSetting(url_a.GetOrigin(), url_b.GetOrigin(),
50 CONTENT_SETTINGS_TYPE_DURABLE_STORAGE,
51 std::string());
52 }
53
54 private:
55 // NotificationPermissionContext:
56 void NotifyPermissionSet(const PermissionRequestID& id,
57 const GURL& requesting_origin,
58 const GURL& embedder_origin,
59 const BrowserPermissionCallback& callback,
60 bool persist,
61 ContentSetting content_setting) override {
62 permission_set_count_++;
63 last_permission_set_persisted_ = persist;
64 last_permission_set_setting_ = content_setting;
65 DurableStoragePermissionContext::NotifyPermissionSet(
66 id, requesting_origin, embedder_origin, callback, persist,
67 content_setting);
68 }
69
70 int permission_set_count_;
71 bool last_permission_set_persisted_;
72 ContentSetting last_permission_set_setting_;
73 };
74
75 } // namespace
76
12 class BookmarksOriginTest : public ::testing::Test { 77 class BookmarksOriginTest : public ::testing::Test {
13 protected: 78 protected:
14 static std::vector<BookmarkModel::URLAndTitle> MakeBookmarks( 79 static std::vector<BookmarkModel::URLAndTitle> MakeBookmarks(
15 const std::string urls[], 80 const std::string urls[],
16 const int array_size) { 81 const int array_size) {
17 std::vector<BookmarkModel::URLAndTitle> bookmarks; 82 std::vector<BookmarkModel::URLAndTitle> bookmarks;
18 for (int i = 0; i < array_size; ++i) { 83 for (int i = 0; i < array_size; ++i) {
19 BookmarkModel::URLAndTitle bookmark; 84 BookmarkModel::URLAndTitle bookmark;
20 bookmark.url = GURL(urls[i]); 85 bookmark.url = GURL(urls[i]);
21 EXPECT_TRUE(bookmark.url.is_valid()); 86 EXPECT_TRUE(bookmark.url.is_valid());
(...skipping 20 matching lines...) Expand all
42 std::string urls[] = { 107 std::string urls[] = {
43 "http://www.google.com/", 108 "http://www.google.com/",
44 "https://www.google.com/", 109 "https://www.google.com/",
45 }; 110 };
46 std::vector<BookmarkModel::URLAndTitle> bookmarks = 111 std::vector<BookmarkModel::URLAndTitle> bookmarks =
47 MakeBookmarks(urls, arraysize(urls)); 112 MakeBookmarks(urls, arraysize(urls));
48 GURL looking_for("https://dogs.com"); 113 GURL looking_for("https://dogs.com");
49 EXPECT_FALSE(DurableStoragePermissionContext::IsOriginBookmarked( 114 EXPECT_FALSE(DurableStoragePermissionContext::IsOriginBookmarked(
50 bookmarks, looking_for)); 115 bookmarks, looking_for));
51 } 116 }
117
118 class DurableStoragePermissionContextTest
119 : public ChromeRenderViewHostTestHarness {
120 protected:
121 void AddBookmark(const GURL& origin) {
122 if (!model_) {
123 profile()->CreateBookmarkModel(true);
124 model_ = BookmarkModelFactory::GetForBrowserContext(profile());
125 bookmarks::test::WaitForBookmarkModelToLoad(model_);
126 }
127
128 model_->AddURL(model_->bookmark_bar_node(), 0,
129 base::ASCIIToUTF16(origin.spec()), origin);
130 }
131
132 BookmarkModel* model_ = nullptr;
133 };
134
135 TEST_F(DurableStoragePermissionContextTest, Bookmark) {
136 TestDurablePermissionContext permission_context(profile());
137 GURL url("http://www.google.com");
138 AddBookmark(url);
139 NavigateAndCommit(url);
140
141 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(),
142 web_contents()->GetMainFrame()->GetRoutingID(),
143 -1);
144
145 ASSERT_EQ(0, permission_context.permission_set_count());
146 ASSERT_FALSE(permission_context.last_permission_set_persisted());
147 ASSERT_EQ(CONTENT_SETTING_DEFAULT,
148 permission_context.last_permission_set_setting());
149
150 permission_context.DecidePermission(web_contents(), id, url, url,
151 true /* user_gesture */,
152 base::Bind(&DoNothing));
153
154 EXPECT_EQ(1, permission_context.permission_set_count());
155 EXPECT_TRUE(permission_context.last_permission_set_persisted());
156 EXPECT_EQ(CONTENT_SETTING_ALLOW,
157 permission_context.last_permission_set_setting());
158 }
159
160 TEST_F(DurableStoragePermissionContextTest, NoBookmark) {
161 TestDurablePermissionContext permission_context(profile());
162 GURL url("http://www.google.com");
163 NavigateAndCommit(url);
164
165 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(),
166 web_contents()->GetMainFrame()->GetRoutingID(),
167 -1);
168
169 ASSERT_EQ(0, permission_context.permission_set_count());
170 ASSERT_FALSE(permission_context.last_permission_set_persisted());
171 ASSERT_EQ(CONTENT_SETTING_DEFAULT,
172 permission_context.last_permission_set_setting());
173
174 permission_context.DecidePermission(web_contents(), id, url, url,
175 true /* user_gesture */,
176 base::Bind(&DoNothing));
177
178 // We shouldn't be granted.
179 EXPECT_EQ(1, permission_context.permission_set_count());
180 EXPECT_FALSE(permission_context.last_permission_set_persisted());
181 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
182 permission_context.last_permission_set_setting());
183 }
184
185 TEST_F(DurableStoragePermissionContextTest, CookiesNotAllowed) {
186 TestDurablePermissionContext permission_context(profile());
187 GURL url("http://www.google.com");
188 AddBookmark(url);
189 NavigateAndCommit(url);
190
191 scoped_refptr<content_settings::CookieSettings> cookie_settings =
192 CookieSettingsFactory::GetForProfile(profile());
193
194 cookie_settings->SetCookieSetting(url, CONTENT_SETTING_BLOCK);
195
196 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(),
197 web_contents()->GetMainFrame()->GetRoutingID(),
198 -1);
199
200 ASSERT_EQ(0, permission_context.permission_set_count());
201 ASSERT_FALSE(permission_context.last_permission_set_persisted());
202 ASSERT_EQ(CONTENT_SETTING_DEFAULT,
203 permission_context.last_permission_set_setting());
204
205 permission_context.DecidePermission(web_contents(), id, url, url,
206 true /* user_gesture */,
207 base::Bind(&DoNothing));
208 // We shouldn't be granted.
209 EXPECT_EQ(1, permission_context.permission_set_count());
210 EXPECT_FALSE(permission_context.last_permission_set_persisted());
211 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
212 permission_context.last_permission_set_setting());
213 }
214
215 TEST_F(DurableStoragePermissionContextTest, IncognitoMode) {
216 TestDurablePermissionContext permission_context(
217 profile()->GetOffTheRecordProfile());
218 GURL url("http://www.google.com");
219 AddBookmark(url);
220 NavigateAndCommit(url);
221
222 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(),
223 web_contents()->GetMainFrame()->GetRoutingID(),
224 -1);
225
226 ASSERT_EQ(0, permission_context.permission_set_count());
227 ASSERT_FALSE(permission_context.last_permission_set_persisted());
228 ASSERT_EQ(CONTENT_SETTING_DEFAULT,
229 permission_context.last_permission_set_setting());
230
231 permission_context.DecidePermission(web_contents(), id, url, url,
232 true /* user_gesture */,
233 base::Bind(&DoNothing));
234 // We shouldn't be granted.
235 EXPECT_EQ(1, permission_context.permission_set_count());
236 EXPECT_FALSE(permission_context.last_permission_set_persisted());
237 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
238 permission_context.last_permission_set_setting());
239 }
240
241 TEST_F(DurableStoragePermissionContextTest, EmbeddedFrame) {
242 TestDurablePermissionContext permission_context(profile());
243 GURL url("http://www.google.com");
244 GURL requesting_url("http://www.youtube.com");
245 AddBookmark(url);
246 NavigateAndCommit(url);
247
248 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(),
249 web_contents()->GetMainFrame()->GetRoutingID(),
250 -1);
251
252 ASSERT_EQ(0, permission_context.permission_set_count());
253 ASSERT_FALSE(permission_context.last_permission_set_persisted());
254 ASSERT_EQ(CONTENT_SETTING_DEFAULT,
255 permission_context.last_permission_set_setting());
256
257 permission_context.DecidePermission(web_contents(), id, requesting_url, url,
258 true /* user_gesture */,
259 base::Bind(&DoNothing));
260 // We shouldn't be granted.
261 EXPECT_EQ(1, permission_context.permission_set_count());
262 EXPECT_FALSE(permission_context.last_permission_set_persisted());
263 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
264 permission_context.last_permission_set_setting());
265 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698