Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 <algorithm> | |
| 6 #include <memory> | |
| 7 #include <set> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/files/file_path.h" | |
| 11 #include "base/run_loop.h" | |
| 12 #include "chrome/browser/browsing_data/site_data_counting_helper.h" | |
| 13 #include "chrome/browser/profiles/profile.h" | |
| 14 #include "chrome/browser/ui/browser.h" | |
| 15 #include "chrome/test/base/in_process_browser_test.h" | |
|
msramek
2017/01/10 17:21:33
nit: Not needed anymore (but I'm surprised that th
dullweber
2017/03/03 12:24:14
Done.
| |
| 16 #include "chrome/test/base/testing_profile.h" | |
| 17 #include "content/public/browser/browser_context.h" | |
| 18 #include "content/public/browser/browser_thread.h" | |
| 19 #include "content/public/browser/storage_partition.h" | |
| 20 #include "content/public/test/test_browser_thread_bundle.h" | |
| 21 #include "net/cookies/cookie_store.h" | |
| 22 #include "net/url_request/url_request_context.h" | |
| 23 #include "net/url_request/url_request_context_getter.h" | |
| 24 | |
| 25 using content::BrowserThread; | |
| 26 | |
| 27 class SiteDataCountingHelperTest : public testing::Test { | |
| 28 public: | |
| 29 const int64_t kTimeoutMs = 10; | |
| 30 | |
| 31 void SetUp() override { | |
| 32 profile_.reset(new TestingProfile()); | |
| 33 tasks_ = 0; | |
| 34 cookie_callback_ = base::Bind(&SiteDataCountingHelperTest::CookieCallback, | |
| 35 base::Unretained(this)); | |
| 36 } | |
| 37 | |
| 38 void TearDown() override { | |
| 39 profile_.reset(); | |
| 40 base::RunLoop().RunUntilIdle(); | |
| 41 } | |
| 42 | |
| 43 void CookieCallback(int count) { | |
| 44 // Negative values represent an unexpected error. | |
| 45 DCHECK(count >= 0); | |
| 46 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 47 last_count_ = count; | |
| 48 | |
| 49 if (run_loop_) | |
| 50 run_loop_->Quit(); | |
| 51 } | |
| 52 | |
| 53 void DoneOnIOThread(bool success) { | |
| 54 DCHECK(success); | |
| 55 if (--tasks_ > 0) | |
| 56 return; | |
| 57 BrowserThread::PostTask( | |
| 58 BrowserThread::UI, FROM_HERE, | |
| 59 base::Bind(&SiteDataCountingHelperTest::DoneCallback, | |
| 60 base::Unretained(this))); | |
| 61 } | |
| 62 | |
| 63 void DoneCallback() { | |
| 64 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 65 if (run_loop_) | |
| 66 run_loop_->Quit(); | |
| 67 } | |
| 68 | |
| 69 void WaitForTasksOnIOThread() { | |
| 70 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 71 run_loop_.reset(new base::RunLoop()); | |
| 72 run_loop_->Run(); | |
| 73 } | |
| 74 | |
| 75 void CreateCookies(base::Time creation_time, | |
| 76 const std::vector<std::string>& urls) { | |
| 77 content::StoragePartition* partition = | |
| 78 content::BrowserContext::GetDefaultStoragePartition(profile()); | |
| 79 net::URLRequestContextGetter* rq_context = | |
| 80 partition->GetURLRequestContext(); | |
| 81 BrowserThread::PostTask( | |
| 82 BrowserThread::IO, FROM_HERE, | |
| 83 base::Bind(&SiteDataCountingHelperTest::CreateCookiesOnIOThread, | |
| 84 base::Unretained(this), make_scoped_refptr(rq_context), | |
| 85 creation_time, urls)); | |
| 86 } | |
| 87 | |
| 88 void CreateLocalStorage( | |
| 89 base::Time creation_time, | |
| 90 const std::vector<base::FilePath::StringPieceType>& storage_origins) { | |
| 91 // Note: This test depends on details of how the dom_storage library | |
| 92 // stores data in the host file system. | |
| 93 base::FilePath storage_path = | |
| 94 profile()->GetPath().AppendASCII("Local Storage"); | |
| 95 base::CreateDirectory(storage_path); | |
| 96 | |
| 97 // Write some files. | |
| 98 for (const auto& origin : storage_origins) { | |
| 99 base::WriteFile(storage_path.Append(origin), NULL, 0); | |
| 100 base::TouchFile(storage_path.Append(origin), creation_time, | |
| 101 creation_time); | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 void CreateCookiesOnIOThread( | |
| 106 const scoped_refptr<net::URLRequestContextGetter>& rq_context, | |
| 107 base::Time creation_time, | |
| 108 std::vector<std::string> urls) { | |
| 109 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 110 | |
| 111 net::CookieStore* cookie_store = | |
| 112 rq_context->GetURLRequestContext()->cookie_store(); | |
| 113 | |
| 114 tasks_ = urls.size(); | |
| 115 int i = 0; | |
| 116 for (const std::string& url_string : urls) { | |
| 117 GURL url(url_string); | |
| 118 // Cookies need a unique creation time. | |
| 119 base::Time time = creation_time + base::TimeDelta::FromMilliseconds(i++); | |
| 120 cookie_store->SetCookieWithDetailsAsync( | |
| 121 url, "name", "A=1", url.host(), url.path(), time, base::Time(), time, | |
| 122 true, false, net::CookieSameSite::DEFAULT_MODE, false, | |
| 123 net::COOKIE_PRIORITY_DEFAULT, | |
| 124 base::Bind(&SiteDataCountingHelperTest::DoneOnIOThread, | |
| 125 base::Unretained(this))); | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 void CountEntries(base::Time begin_time) { | |
| 130 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 131 last_count_ = -1; | |
| 132 auto helper = | |
| 133 new SiteDataCountingHelper(profile(), begin_time, cookie_callback_); | |
| 134 helper->CountAndDestroySelfWhenFinished(); | |
| 135 } | |
| 136 | |
| 137 int64_t GetResult() { | |
| 138 DCHECK_GE(last_count_, 0); | |
| 139 return last_count_; | |
| 140 } | |
| 141 | |
| 142 Profile* profile() { return profile_.get(); } | |
| 143 | |
| 144 private: | |
| 145 base::Callback<void(int)> cookie_callback_; | |
| 146 std::unique_ptr<base::RunLoop> run_loop_; | |
| 147 content::TestBrowserThreadBundle thread_bundle_; | |
| 148 std::unique_ptr<TestingProfile> profile_; | |
| 149 | |
| 150 int tasks_; | |
| 151 int64_t last_count_; | |
| 152 }; | |
| 153 | |
| 154 TEST_F(SiteDataCountingHelperTest, CheckEmptyResult) { | |
| 155 CountEntries(base::Time()); | |
| 156 WaitForTasksOnIOThread(); | |
| 157 | |
| 158 DCHECK_EQ(0, GetResult()); | |
| 159 } | |
| 160 | |
| 161 TEST_F(SiteDataCountingHelperTest, CountCookies) { | |
| 162 base::Time now = base::Time::Now(); | |
| 163 base::Time last_hour = now - base::TimeDelta::FromHours(1); | |
| 164 base::Time yesterday = now - base::TimeDelta::FromDays(1); | |
| 165 | |
| 166 CreateCookies(last_hour, {"https://example.com"}); | |
| 167 WaitForTasksOnIOThread(); | |
| 168 | |
| 169 CreateCookies(yesterday, {"https://google.com", "https://bing.com"}); | |
| 170 WaitForTasksOnIOThread(); | |
| 171 | |
| 172 CountEntries(base::Time()); | |
| 173 WaitForTasksOnIOThread(); | |
| 174 DCHECK_EQ(3, GetResult()); | |
| 175 | |
| 176 CountEntries(yesterday); | |
| 177 WaitForTasksOnIOThread(); | |
| 178 DCHECK_EQ(3, GetResult()); | |
| 179 | |
| 180 CountEntries(last_hour); | |
| 181 WaitForTasksOnIOThread(); | |
| 182 DCHECK_EQ(1, GetResult()); | |
| 183 | |
| 184 CountEntries(now); | |
| 185 WaitForTasksOnIOThread(); | |
| 186 DCHECK_EQ(0, GetResult()); | |
| 187 } | |
| 188 | |
| 189 TEST_F(SiteDataCountingHelperTest, LocalStorage) { | |
| 190 base::Time now = base::Time::Now(); | |
| 191 CreateLocalStorage(now, | |
| 192 {FILE_PATH_LITERAL("https_example.com_443.localstorage"), | |
| 193 FILE_PATH_LITERAL("https_bing.com_443.localstorage")}); | |
| 194 | |
| 195 CountEntries(base::Time()); | |
| 196 WaitForTasksOnIOThread(); | |
| 197 DCHECK_EQ(2, GetResult()); | |
| 198 } | |
| 199 | |
| 200 TEST_F(SiteDataCountingHelperTest, CookiesAndLocalStorage) { | |
| 201 base::Time now = base::Time::Now(); | |
| 202 CreateCookies(now, {"https://example.com", "https://google.com"}); | |
| 203 CreateLocalStorage(now, | |
| 204 {FILE_PATH_LITERAL("https_example.com_443.localstorage"), | |
| 205 FILE_PATH_LITERAL("https_bing.com_443.localstorage")}); | |
| 206 WaitForTasksOnIOThread(); | |
| 207 | |
| 208 CountEntries(base::Time()); | |
| 209 WaitForTasksOnIOThread(); | |
| 210 DCHECK_EQ(3, GetResult()); | |
| 211 } | |
| OLD | NEW |