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