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

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: 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 // Creates a PluginPrivateFileSystem for the |plugin_name| and |origin|
174 // provided. Returns the file system ID for the created
175 // PluginPrivateFileSystem.
176 std::string CreateFileSystem(const std::string& plugin_name,
msramek 2016/09/26 18:28:05 nit: This method and everything below looks like i
jrummell 2016/09/27 18:21:15 Done.
177 const GURL& origin) {
178 AwaitCompletionHelper await_completion;
179 std::string fsid = storage::IsolatedContext::GetInstance()
180 ->RegisterFileSystemForVirtualPath(
181 storage::kFileSystemTypePluginPrivate,
182 ppapi::kPluginPrivateRootName, base::FilePath());
183 EXPECT_TRUE(storage::ValidateIsolatedFileSystemId(fsid));
184 filesystem_context_->OpenPluginPrivateFileSystem(
185 origin, storage::kFileSystemTypePluginPrivate, fsid, plugin_name,
186 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
187 base::Bind(&BrowsingDataMediaLicenseHelperTest::OnFileSystemOpened,
188 base::Unretained(this), await_completion.NotifyClosure()));
189 await_completion.BlockUntilNotified();
190 return fsid;
191 }
192
193 void OnFileSystemOpened(const base::Closure& done_cb,
194 base::File::Error result) {
195 EXPECT_EQ(base::File::FILE_OK, result) << base::File::ErrorToString(result);
196 done_cb.Run();
197 }
198
199 // Creates a file named |file_name| in the PluginPrivateFileSystem identified
200 // by |origin| and |fsid|. The file is empty (so size = 0). Returns the URL
201 // for the created file. The file must not already exist or the test will
202 // fail.
203 storage::FileSystemURL CreateFile(const GURL& origin,
204 const std::string& fsid,
205 const std::string& file_name) {
206 AwaitCompletionHelper await_completion;
207 std::string root = storage::GetIsolatedFileSystemRootURIString(
208 origin, fsid, ppapi::kPluginPrivateRootName);
209 storage::FileSystemURL file_url =
210 filesystem_context_->CrackURL(GURL(root + file_name));
211 storage::AsyncFileUtil* file_util = filesystem_context_->GetAsyncFileUtil(
212 storage::kFileSystemTypePluginPrivate);
213 std::unique_ptr<storage::FileSystemOperationContext> operation_context =
214 base::WrapUnique(
215 new storage::FileSystemOperationContext(filesystem_context_));
216 operation_context->set_allowed_bytes_growth(
217 storage::QuotaManager::kNoLimit);
218 file_util->EnsureFileExists(
219 std::move(operation_context), file_url,
220 base::Bind(&BrowsingDataMediaLicenseHelperTest::OnFileCreated,
221 base::Unretained(this), await_completion.NotifyClosure()));
222 await_completion.BlockUntilNotified();
223 return file_url;
224 }
225
226 void OnFileCreated(const base::Closure& done_cb,
227 base::File::Error result,
228 bool created) {
229 EXPECT_EQ(base::File::FILE_OK, result) << base::File::ErrorToString(result);
230 EXPECT_TRUE(created);
231 done_cb.Run();
232 }
233
234 // Sets the last_access_time and last_modified_time to |time_stamp| on the
235 // file specified by |file_url|. The file must already exist.
236 void SetFileTimestamp(const storage::FileSystemURL& file_url,
237 const base::Time& time_stamp) {
238 AwaitCompletionHelper await_completion;
239 storage::AsyncFileUtil* file_util = filesystem_context_->GetAsyncFileUtil(
240 storage::kFileSystemTypePluginPrivate);
241 std::unique_ptr<storage::FileSystemOperationContext> operation_context =
242 base::WrapUnique(
243 new storage::FileSystemOperationContext(filesystem_context_));
244 file_util->Touch(
245 std::move(operation_context), file_url, time_stamp, time_stamp,
246 base::Bind(&BrowsingDataMediaLicenseHelperTest::OnFileTouched,
247 base::Unretained(this), await_completion.NotifyClosure()));
248 await_completion.BlockUntilNotified();
249 }
250
251 void OnFileTouched(const base::Closure& done_cb, base::File::Error result) {
252 EXPECT_EQ(base::File::FILE_OK, result) << base::File::ErrorToString(result);
253 done_cb.Run();
254 }
255
256 const base::Time Now() { return now_; }
257
258 protected:
msramek 2016/09/26 18:28:05 Why is this protected? Do we inherit from this tes
jrummell 2016/09/27 18:21:15 Made private. I think I used browsing_data_file_sy
259 content::TestBrowserThreadBundle thread_bundle_;
260 std::unique_ptr<TestingProfile> profile_;
261 scoped_refptr<BrowsingDataMediaLicenseHelper> helper_;
262
263 // Keep a fixed "now" so that we can compare timestamps.
264 base::Time now_;
265
266 // We don't own this pointer.
267 storage::FileSystemContext* filesystem_context_;
268
269 // Storage to pass information back from callbacks.
270 std::unique_ptr<std::list<BrowsingDataMediaLicenseHelper::MediaLicenseInfo>>
271 media_license_info_list_;
272
273 private:
274 DISALLOW_COPY_AND_ASSIGN(BrowsingDataMediaLicenseHelperTest);
275 };
276
277 // Verifies that the BrowsingDataMediaLicenseHelper correctly handles an empty
278 // filesystem.
279 TEST_F(BrowsingDataMediaLicenseHelperTest, Empty) {
280 FetchMediaLicenses();
281 EXPECT_EQ(0u, media_license_info_list_->size());
282 }
283
284 // Verifies that the BrowsingDataMediaLicenseHelper correctly finds the test
285 // data, and that each media license returned contains the expected data.
286 TEST_F(BrowsingDataMediaLicenseHelperTest, FetchData) {
287 PopulateTestMediaLicenseData();
288
289 FetchMediaLicenses();
290 EXPECT_EQ(3u, media_license_info_list_->size());
291
292 // Order is arbitrary, verify both origins.
293 bool test_hosts_found[] = {false, false, false};
294 for (const auto& info : *media_license_info_list_) {
295 if (info.origin == kOrigin1) {
296 EXPECT_FALSE(test_hosts_found[0]);
297 test_hosts_found[0] = true;
298 EXPECT_EQ(0u, info.size);
299 // Single file for origin1 should be 10 days ago.
300 EXPECT_EQ(10, (Now() - info.last_modified_time).InDays());
301 } else if (info.origin == kOrigin2) {
302 EXPECT_FALSE(test_hosts_found[1]);
303 test_hosts_found[1] = true;
304 EXPECT_EQ(0u, info.size);
305 // Files for origin2 are now and 60 days ago, so it should report now.
306 EXPECT_EQ(0, (Now() - info.last_modified_time).InDays());
307 } else if (info.origin == kOrigin3) {
308 EXPECT_FALSE(test_hosts_found[2]);
309 test_hosts_found[2] = true;
310 EXPECT_EQ(0u, info.size);
311 // Files for origin3 are 20 and 30 days ago, so it should report 20.
312 EXPECT_EQ(20, (Now() - info.last_modified_time).InDays());
313 } else {
314 ADD_FAILURE() << info.origin.spec() << " isn't an origin we added.";
315 }
316 }
317 for (size_t i = 0; i < arraysize(test_hosts_found); i++) {
318 EXPECT_TRUE(test_hosts_found[i]);
319 }
320 }
321
322 // Verifies that the BrowsingDataMediaLicenseHelper correctly deletes media
323 // licenses via DeleteMediaLicenseOrigin().
324 TEST_F(BrowsingDataMediaLicenseHelperTest, DeleteData) {
325 PopulateTestMediaLicenseData();
326
327 helper_->DeleteMediaLicenseOrigin(kOrigin1);
328 helper_->DeleteMediaLicenseOrigin(kOrigin2);
329
330 FetchMediaLicenses();
331 EXPECT_EQ(1u, media_license_info_list_->size());
332
333 BrowsingDataMediaLicenseHelper::MediaLicenseInfo info =
334 *(media_license_info_list_->begin());
335 EXPECT_EQ(kOrigin3, info.origin);
336 EXPECT_EQ(0u, info.size);
337 EXPECT_EQ(20, (Now() - info.last_modified_time).InDays());
338 }
339
340 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698