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

Side by Side Diff: chrome/browser/browsing_data/browsing_data_media_license_helper_unittest.cc

Issue 2359393002: Adds media license nodes to cookie tree model and cookies view. (Closed)
Patch Set: rebase 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
(Empty)
1 // Copyright 2016 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 <stddef.h>
6 #include <algorithm>
7 #include <memory>
8 #include <string>
9 #include <vector>
10
11 #include "base/bind.h"
12 #include "base/bind_helpers.h"
13 #include "base/files/file_util.h"
14 #include "base/macros.h"
15 #include "base/memory/ptr_util.h"
16 #include "base/message_loop/message_loop.h"
17 #include "base/run_loop.h"
18 #include "base/stl_util.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "chrome/browser/browsing_data/browsing_data_media_license_helper.h"
21 #include "chrome/test/base/testing_profile.h"
22 #include "content/public/browser/storage_partition.h"
23 #include "content/public/test/test_browser_thread.h"
24 #include "content/public/test/test_browser_thread_bundle.h"
25 #include "ppapi/shared_impl/ppapi_constants.h"
26 #include "storage/browser/fileapi/async_file_util.h"
27 #include "storage/browser/fileapi/file_system_context.h"
28 #include "storage/browser/fileapi/file_system_operation_context.h"
29 #include "storage/browser/fileapi/file_system_url.h"
30 #include "storage/browser/fileapi/isolated_context.h"
31 #include "storage/browser/quota/quota_manager.h"
32 #include "storage/common/fileapi/file_system_types.h"
33 #include "storage/common/fileapi/file_system_util.h"
34 #include "testing/gtest/include/gtest/gtest.h"
35
36 using content::BrowserContext;
37 using content::BrowserThread;
38
39 namespace {
40
41 // We'll use these three distinct origins for testing, both as strings and as
42 // GURLs in appropriate contexts.
43 const char kTestOrigin1[] = "http://host1:1/";
44 const char kTestOrigin2[] = "http://host2:2/";
45 const char kTestOrigin3[] = "http://host3:1/";
46
47 const GURL kOrigin1(kTestOrigin1);
48 const GURL kOrigin2(kTestOrigin2);
49 const GURL kOrigin3(kTestOrigin3);
50
51 const char kWidevineCdmPluginId[] = "application_x-ppapi-widevine-cdm";
52 const char kClearKeyCdmPluginId[] = "application_x-ppapi-clearkey-cdm";
53
54 class AwaitCompletionHelper {
55 public:
56 AwaitCompletionHelper() : start_(false), already_quit_(false) {}
57 virtual ~AwaitCompletionHelper() {}
58
59 void BlockUntilNotified() {
60 if (!already_quit_) {
61 DCHECK(!start_);
62 start_ = true;
63 base::RunLoop().Run();
64 } else {
65 DCHECK(!start_);
66 already_quit_ = false;
67 }
68 }
69
70 base::Closure NotifyClosure() {
71 return base::Bind(&AwaitCompletionHelper::Notify, base::Unretained(this));
72 }
73
74 private:
75 void Notify() {
76 if (start_) {
77 DCHECK(!already_quit_);
78 base::MessageLoop::current()->QuitWhenIdle();
79 start_ = false;
80 } else {
81 DCHECK(!already_quit_);
82 already_quit_ = true;
83 }
84 }
85
86 // Helps prevent from running message_loop, if the callback invoked
87 // immediately.
88 bool start_;
89 bool already_quit_;
90
91 DISALLOW_COPY_AND_ASSIGN(AwaitCompletionHelper);
92 };
93
94 // The FileSystem APIs are all asynchronous; this testing class wraps up the
95 // boilerplate code necessary to deal with waiting for responses. In a nutshell,
96 // any async call whose response we want to test ought to be followed by a call
97 // to BlockUntilNotified(), which will block until Notify() is called.
98 class BrowsingDataMediaLicenseHelperTest : public testing::Test {
99 public:
100 BrowsingDataMediaLicenseHelperTest() {
101 now_ = base::Time::Now();
102 profile_.reset(new TestingProfile());
103 filesystem_context_ =
104 BrowserContext::GetDefaultStoragePartition(profile_.get())
105 ->GetFileSystemContext();
106 helper_ = BrowsingDataMediaLicenseHelper::Create(filesystem_context_);
107 base::RunLoop().RunUntilIdle();
108 }
109
110 ~BrowsingDataMediaLicenseHelperTest() override {
111 // Avoid memory leaks.
112 profile_.reset();
113 base::RunLoop().RunUntilIdle();
114 }
115
116 // Calls StartFetching() on the test's BrowsingDataMediaLicenseHelper
117 // object, then blocks until the callback is executed.
118 void FetchMediaLicenses() {
119 AwaitCompletionHelper await_completion;
120 helper_->StartFetching(
121 base::Bind(&BrowsingDataMediaLicenseHelperTest::OnFetchMediaLicenses,
122 base::Unretained(this), await_completion.NotifyClosure()));
123 await_completion.BlockUntilNotified();
124 }
125
126 // Callback that should be executed in response to StartFetching(), and stores
127 // found file systems locally so that they are available via GetFileSystems().
128 void OnFetchMediaLicenses(
129 const base::Closure& done_cb,
130 const std::list<BrowsingDataMediaLicenseHelper::MediaLicenseInfo>&
131 media_license_info_list) {
132 media_license_info_list_.reset(
133 new std::list<BrowsingDataMediaLicenseHelper::MediaLicenseInfo>(
134 media_license_info_list));
135 done_cb.Run();
136 }
137
138 // Add some files to the PluginPrivateFileSystem. They are created as follows:
139 // kOrigin1 - ClearKey - 1 file - timestamp 10 days ago
140 // kOrigin2 - Widevine - 2 files - timestamps now and 60 days ago
141 // kOrigin3 - Widevine - 2 files - timestamps 20 and 30 days ago
142 virtual void PopulateTestMediaLicenseData() {
143 const base::Time ten_days_ago = now_ - base::TimeDelta::FromDays(10);
144 const base::Time twenty_days_ago = now_ - base::TimeDelta::FromDays(20);
145 const base::Time thirty_days_ago = now_ - base::TimeDelta::FromDays(30);
146 const base::Time sixty_days_ago = now_ - base::TimeDelta::FromDays(60);
147
148 std::string clearkey_fsid =
149 CreateFileSystem(kClearKeyCdmPluginId, kOrigin1);
150 storage::FileSystemURL clearkey_file =
151 CreateFile(kOrigin1, clearkey_fsid, "foo");
152 SetFileTimestamp(clearkey_file, ten_days_ago);
153
154 std::string widevine_fsid =
155 CreateFileSystem(kWidevineCdmPluginId, kOrigin2);
156 storage::FileSystemURL widevine_file1 =
157 CreateFile(kOrigin2, widevine_fsid, "bar1");
158 storage::FileSystemURL widevine_file2 =
159 CreateFile(kOrigin2, widevine_fsid, "bar2");
160 SetFileTimestamp(widevine_file1, now_);
161 SetFileTimestamp(widevine_file2, sixty_days_ago);
162
163 std::string widevine_fsid2 =
164 CreateFileSystem(kWidevineCdmPluginId, kOrigin3);
165 storage::FileSystemURL widevine_file3 =
166 CreateFile(kOrigin3, widevine_fsid2, "test1");
167 storage::FileSystemURL widevine_file4 =
168 CreateFile(kOrigin3, widevine_fsid2, "test2");
169 SetFileTimestamp(widevine_file3, twenty_days_ago);
170 SetFileTimestamp(widevine_file4, thirty_days_ago);
171 }
172
173 const base::Time Now() { return now_; }
174
175 void DeleteMediaLicenseOrigin(const GURL& origin) {
176 helper_->DeleteMediaLicenseOrigin(origin);
177 }
178
179 std::list<BrowsingDataMediaLicenseHelper::MediaLicenseInfo>*
180 ReturnedMediaLicenseInfo() const {
181 return media_license_info_list_.get();
182 }
183
184 private:
185 // Creates a PluginPrivateFileSystem for the |plugin_name| and |origin|
186 // provided. Returns the file system ID for the created
187 // PluginPrivateFileSystem.
188 std::string CreateFileSystem(const std::string& plugin_name,
189 const GURL& origin) {
190 AwaitCompletionHelper await_completion;
191 std::string fsid = storage::IsolatedContext::GetInstance()
192 ->RegisterFileSystemForVirtualPath(
193 storage::kFileSystemTypePluginPrivate,
194 ppapi::kPluginPrivateRootName, base::FilePath());
195 EXPECT_TRUE(storage::ValidateIsolatedFileSystemId(fsid));
196 filesystem_context_->OpenPluginPrivateFileSystem(
197 origin, storage::kFileSystemTypePluginPrivate, fsid, plugin_name,
198 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
199 base::Bind(&BrowsingDataMediaLicenseHelperTest::OnFileSystemOpened,
200 base::Unretained(this), await_completion.NotifyClosure()));
201 await_completion.BlockUntilNotified();
202 return fsid;
203 }
204
205 void OnFileSystemOpened(const base::Closure& done_cb,
206 base::File::Error result) {
207 EXPECT_EQ(base::File::FILE_OK, result) << base::File::ErrorToString(result);
208 done_cb.Run();
209 }
210
211 // Creates a file named |file_name| in the PluginPrivateFileSystem identified
212 // by |origin| and |fsid|. The file is empty (so size = 0). Returns the URL
213 // for the created file. The file must not already exist or the test will
214 // fail.
215 storage::FileSystemURL CreateFile(const GURL& origin,
216 const std::string& fsid,
217 const std::string& file_name) {
218 AwaitCompletionHelper await_completion;
219 std::string root = storage::GetIsolatedFileSystemRootURIString(
220 origin, fsid, ppapi::kPluginPrivateRootName);
221 storage::FileSystemURL file_url =
222 filesystem_context_->CrackURL(GURL(root + file_name));
223 storage::AsyncFileUtil* file_util = filesystem_context_->GetAsyncFileUtil(
224 storage::kFileSystemTypePluginPrivate);
225 std::unique_ptr<storage::FileSystemOperationContext> operation_context =
226 base::WrapUnique(
227 new storage::FileSystemOperationContext(filesystem_context_));
228 operation_context->set_allowed_bytes_growth(
229 storage::QuotaManager::kNoLimit);
230 file_util->EnsureFileExists(
231 std::move(operation_context), file_url,
232 base::Bind(&BrowsingDataMediaLicenseHelperTest::OnFileCreated,
233 base::Unretained(this), await_completion.NotifyClosure()));
234 await_completion.BlockUntilNotified();
235 return file_url;
236 }
237
238 void OnFileCreated(const base::Closure& done_cb,
239 base::File::Error result,
240 bool created) {
241 EXPECT_EQ(base::File::FILE_OK, result) << base::File::ErrorToString(result);
242 EXPECT_TRUE(created);
243 done_cb.Run();
244 }
245
246 // Sets the last_access_time and last_modified_time to |time_stamp| on the
247 // file specified by |file_url|. The file must already exist.
248 void SetFileTimestamp(const storage::FileSystemURL& file_url,
249 const base::Time& time_stamp) {
250 AwaitCompletionHelper await_completion;
251 storage::AsyncFileUtil* file_util = filesystem_context_->GetAsyncFileUtil(
252 storage::kFileSystemTypePluginPrivate);
253 std::unique_ptr<storage::FileSystemOperationContext> operation_context =
254 base::WrapUnique(
255 new storage::FileSystemOperationContext(filesystem_context_));
256 file_util->Touch(
257 std::move(operation_context), file_url, time_stamp, time_stamp,
258 base::Bind(&BrowsingDataMediaLicenseHelperTest::OnFileTouched,
259 base::Unretained(this), await_completion.NotifyClosure()));
260 await_completion.BlockUntilNotified();
261 }
262
263 void OnFileTouched(const base::Closure& done_cb, base::File::Error result) {
264 EXPECT_EQ(base::File::FILE_OK, result) << base::File::ErrorToString(result);
265 done_cb.Run();
266 }
267
268 content::TestBrowserThreadBundle thread_bundle_;
269 std::unique_ptr<TestingProfile> profile_;
270 scoped_refptr<BrowsingDataMediaLicenseHelper> helper_;
271
272 // Keep a fixed "now" so that we can compare timestamps.
273 base::Time now_;
274
275 // We don't own this pointer.
276 storage::FileSystemContext* filesystem_context_;
277
278 // Storage to pass information back from callbacks.
279 std::unique_ptr<std::list<BrowsingDataMediaLicenseHelper::MediaLicenseInfo>>
280 media_license_info_list_;
281
282 DISALLOW_COPY_AND_ASSIGN(BrowsingDataMediaLicenseHelperTest);
283 };
284
285 // Verifies that the BrowsingDataMediaLicenseHelper correctly handles an empty
286 // filesystem.
287 TEST_F(BrowsingDataMediaLicenseHelperTest, Empty) {
288 FetchMediaLicenses();
289 EXPECT_EQ(0u, ReturnedMediaLicenseInfo()->size());
290 }
291
292 // Verifies that the BrowsingDataMediaLicenseHelper correctly finds the test
293 // data, and that each media license returned contains the expected data.
294 TEST_F(BrowsingDataMediaLicenseHelperTest, FetchData) {
295 PopulateTestMediaLicenseData();
296
297 FetchMediaLicenses();
298 EXPECT_EQ(3u, ReturnedMediaLicenseInfo()->size());
299
300 // Order is arbitrary, verify both origins.
301 bool test_hosts_found[] = {false, false, false};
302 for (const auto& info : *ReturnedMediaLicenseInfo()) {
303 if (info.origin == kOrigin1) {
304 EXPECT_FALSE(test_hosts_found[0]);
305 test_hosts_found[0] = true;
306 EXPECT_EQ(0u, info.size);
307 // Single file for origin1 should be 10 days ago.
308 EXPECT_EQ(10, (Now() - info.last_modified_time).InDays());
309 } else if (info.origin == kOrigin2) {
310 EXPECT_FALSE(test_hosts_found[1]);
311 test_hosts_found[1] = true;
312 EXPECT_EQ(0u, info.size);
313 // Files for origin2 are now and 60 days ago, so it should report now.
314 EXPECT_EQ(0, (Now() - info.last_modified_time).InDays());
315 } else if (info.origin == kOrigin3) {
316 EXPECT_FALSE(test_hosts_found[2]);
317 test_hosts_found[2] = true;
318 EXPECT_EQ(0u, info.size);
319 // Files for origin3 are 20 and 30 days ago, so it should report 20.
320 EXPECT_EQ(20, (Now() - info.last_modified_time).InDays());
321 } else {
322 ADD_FAILURE() << info.origin.spec() << " isn't an origin we added.";
323 }
324 }
325 for (size_t i = 0; i < arraysize(test_hosts_found); i++) {
326 EXPECT_TRUE(test_hosts_found[i]);
327 }
328 }
329
330 // Verifies that the BrowsingDataMediaLicenseHelper correctly deletes media
331 // licenses via DeleteMediaLicenseOrigin().
332 TEST_F(BrowsingDataMediaLicenseHelperTest, DeleteData) {
333 PopulateTestMediaLicenseData();
334
335 DeleteMediaLicenseOrigin(kOrigin1);
336 DeleteMediaLicenseOrigin(kOrigin2);
337
338 FetchMediaLicenses();
339 EXPECT_EQ(1u, ReturnedMediaLicenseInfo()->size());
340
341 BrowsingDataMediaLicenseHelper::MediaLicenseInfo info =
342 *(ReturnedMediaLicenseInfo()->begin());
343 EXPECT_EQ(kOrigin3, info.origin);
344 EXPECT_EQ(0u, info.size);
345 EXPECT_EQ(20, (Now() - info.last_modified_time).InDays());
346 }
347
348 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/browsing_data/browsing_data_media_license_helper.cc ('k') | chrome/browser/browsing_data/cookies_tree_model.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698