cookie_monster_store_test.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // Copyright (c) 2012 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. #include "net/cookies/cookie_monster_store_test.h"
  5. #include "base/bind.h"
  6. #include "base/location.h"
  7. #include "base/strings/stringprintf.h"
  8. #include "base/task/single_thread_task_runner.h"
  9. #include "base/threading/thread_task_runner_handle.h"
  10. #include "base/time/time.h"
  11. #include "net/cookies/cookie_constants.h"
  12. #include "net/cookies/cookie_util.h"
  13. #include "net/cookies/parsed_cookie.h"
  14. #include "testing/gtest/include/gtest/gtest.h"
  15. #include "url/gurl.h"
  16. namespace net {
  17. CookieStoreCommand::CookieStoreCommand(
  18. Type type,
  19. CookieMonster::PersistentCookieStore::LoadedCallback loaded_callback,
  20. const std::string& key)
  21. : type(type), loaded_callback(std::move(loaded_callback)), key(key) {}
  22. CookieStoreCommand::CookieStoreCommand(Type type, const CanonicalCookie& cookie)
  23. : type(type), cookie(cookie) {}
  24. CookieStoreCommand::CookieStoreCommand(CookieStoreCommand&& other) = default;
  25. CookieStoreCommand::~CookieStoreCommand() = default;
  26. MockPersistentCookieStore::MockPersistentCookieStore() = default;
  27. void MockPersistentCookieStore::SetLoadExpectation(
  28. bool return_value,
  29. std::vector<std::unique_ptr<CanonicalCookie>> result) {
  30. load_return_value_ = return_value;
  31. load_result_.swap(result);
  32. }
  33. void MockPersistentCookieStore::Load(LoadedCallback loaded_callback,
  34. const NetLogWithSource& /* net_log */) {
  35. if (store_load_commands_) {
  36. commands_.push_back(CookieStoreCommand(CookieStoreCommand::LOAD,
  37. std::move(loaded_callback), ""));
  38. return;
  39. }
  40. std::vector<std::unique_ptr<CanonicalCookie>> out_cookies;
  41. if (load_return_value_) {
  42. out_cookies.swap(load_result_);
  43. loaded_ = true;
  44. }
  45. base::ThreadTaskRunnerHandle::Get()->PostTask(
  46. FROM_HERE,
  47. base::BindOnce(std::move(loaded_callback), std::move(out_cookies)));
  48. }
  49. void MockPersistentCookieStore::LoadCookiesForKey(
  50. const std::string& key,
  51. LoadedCallback loaded_callback) {
  52. if (store_load_commands_) {
  53. commands_.push_back(
  54. CookieStoreCommand(CookieStoreCommand::LOAD_COOKIES_FOR_KEY,
  55. std::move(loaded_callback), key));
  56. return;
  57. }
  58. if (!loaded_) {
  59. Load(std::move(loaded_callback), NetLogWithSource());
  60. } else {
  61. std::vector<std::unique_ptr<CanonicalCookie>> empty_cookies;
  62. base::ThreadTaskRunnerHandle::Get()->PostTask(
  63. FROM_HERE,
  64. base::BindOnce(std::move(loaded_callback), std::move(empty_cookies)));
  65. }
  66. }
  67. void MockPersistentCookieStore::AddCookie(const CanonicalCookie& cookie) {
  68. commands_.push_back(CookieStoreCommand(CookieStoreCommand::ADD, cookie));
  69. }
  70. void MockPersistentCookieStore::UpdateCookieAccessTime(
  71. const CanonicalCookie& cookie) {
  72. }
  73. void MockPersistentCookieStore::DeleteCookie(const CanonicalCookie& cookie) {
  74. commands_.push_back(CookieStoreCommand(CookieStoreCommand::REMOVE, cookie));
  75. }
  76. void MockPersistentCookieStore::SetForceKeepSessionState() {}
  77. void MockPersistentCookieStore::SetBeforeCommitCallback(
  78. base::RepeatingClosure callback) {}
  79. void MockPersistentCookieStore::Flush(base::OnceClosure callback) {
  80. if (!callback.is_null())
  81. base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
  82. std::move(callback));
  83. }
  84. MockPersistentCookieStore::~MockPersistentCookieStore() = default;
  85. std::unique_ptr<CanonicalCookie> BuildCanonicalCookie(
  86. const GURL& url,
  87. const std::string& cookie_line,
  88. const base::Time& creation_time) {
  89. // Parse the cookie line.
  90. ParsedCookie pc(cookie_line);
  91. EXPECT_TRUE(pc.IsValid());
  92. // This helper is simplistic in interpreting a parsed cookie, in order to
  93. // avoid duplicated CookieMonster's CanonPath() and CanonExpiration()
  94. // functions. Would be nice to export them, and re-use here.
  95. EXPECT_FALSE(pc.HasMaxAge());
  96. EXPECT_TRUE(pc.HasPath());
  97. base::Time cookie_expires =
  98. pc.HasExpires() ? cookie_util::ParseCookieExpirationTime(pc.Expires())
  99. : base::Time();
  100. std::string cookie_path = pc.Path();
  101. return CanonicalCookie::CreateUnsafeCookieForTesting(
  102. pc.Name(), pc.Value(), "." + url.host(), cookie_path, creation_time,
  103. cookie_expires, base::Time(), base::Time(), pc.IsSecure(),
  104. pc.IsHttpOnly(), pc.SameSite(), pc.Priority(), pc.IsSameParty());
  105. }
  106. void AddCookieToList(const GURL& url,
  107. const std::string& cookie_line,
  108. const base::Time& creation_time,
  109. std::vector<std::unique_ptr<CanonicalCookie>>* out_list) {
  110. std::unique_ptr<CanonicalCookie> cookie(
  111. BuildCanonicalCookie(url, cookie_line, creation_time));
  112. out_list->push_back(std::move(cookie));
  113. }
  114. MockSimplePersistentCookieStore::MockSimplePersistentCookieStore() = default;
  115. void MockSimplePersistentCookieStore::Load(
  116. LoadedCallback loaded_callback,
  117. const NetLogWithSource& /* net_log */) {
  118. std::vector<std::unique_ptr<CanonicalCookie>> out_cookies;
  119. for (const auto& cookie_map_it : cookies_) {
  120. out_cookies.push_back(
  121. std::make_unique<CanonicalCookie>(cookie_map_it.second));
  122. }
  123. base::ThreadTaskRunnerHandle::Get()->PostTask(
  124. FROM_HERE,
  125. base::BindOnce(std::move(loaded_callback), std::move(out_cookies)));
  126. loaded_ = true;
  127. }
  128. void MockSimplePersistentCookieStore::LoadCookiesForKey(
  129. const std::string& key,
  130. LoadedCallback loaded_callback) {
  131. if (!loaded_) {
  132. Load(std::move(loaded_callback), NetLogWithSource());
  133. } else {
  134. std::vector<std::unique_ptr<CanonicalCookie>> empty_cookies;
  135. base::ThreadTaskRunnerHandle::Get()->PostTask(
  136. FROM_HERE,
  137. base::BindOnce(std::move(loaded_callback), std::move(empty_cookies)));
  138. }
  139. }
  140. void MockSimplePersistentCookieStore::AddCookie(const CanonicalCookie& cookie) {
  141. const auto& key = cookie.UniqueKey();
  142. EXPECT_TRUE(cookies_.find(key) == cookies_.end());
  143. cookies_[key] = cookie;
  144. }
  145. void MockSimplePersistentCookieStore::UpdateCookieAccessTime(
  146. const CanonicalCookie& cookie) {
  147. const auto& key = cookie.UniqueKey();
  148. ASSERT_TRUE(cookies_.find(key) != cookies_.end());
  149. cookies_[key].SetLastAccessDate(base::Time::Now());
  150. }
  151. void MockSimplePersistentCookieStore::DeleteCookie(
  152. const CanonicalCookie& cookie) {
  153. const auto& key = cookie.UniqueKey();
  154. auto it = cookies_.find(key);
  155. ASSERT_TRUE(it != cookies_.end());
  156. cookies_.erase(it);
  157. }
  158. void MockSimplePersistentCookieStore::SetForceKeepSessionState() {}
  159. void MockSimplePersistentCookieStore::SetBeforeCommitCallback(
  160. base::RepeatingClosure callback) {}
  161. void MockSimplePersistentCookieStore::Flush(base::OnceClosure callback) {
  162. if (!callback.is_null())
  163. base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
  164. std::move(callback));
  165. }
  166. std::unique_ptr<CookieMonster> CreateMonsterFromStoreForGC(
  167. int num_secure_cookies,
  168. int num_old_secure_cookies,
  169. int num_non_secure_cookies,
  170. int num_old_non_secure_cookies,
  171. int days_old) {
  172. base::Time current(base::Time::Now());
  173. base::Time past_creation(base::Time::Now() - base::Days(100));
  174. auto store = base::MakeRefCounted<MockSimplePersistentCookieStore>();
  175. int total_cookies = num_secure_cookies + num_non_secure_cookies;
  176. int base = 0;
  177. // Must expire to be persistent
  178. for (int i = 0; i < total_cookies; i++) {
  179. int num_old_cookies;
  180. bool secure;
  181. if (i < num_secure_cookies) {
  182. num_old_cookies = num_old_secure_cookies;
  183. secure = true;
  184. } else {
  185. base = num_secure_cookies;
  186. num_old_cookies = num_old_non_secure_cookies;
  187. secure = false;
  188. }
  189. base::Time creation_time = past_creation + base::Microseconds(i);
  190. base::Time expiration_time = current + base::Days(30);
  191. base::Time last_access_time = ((i - base) < num_old_cookies)
  192. ? current - base::Days(days_old)
  193. : current;
  194. // The URL must be HTTPS since |secure| can be true or false, and because
  195. // strict secure cookies are enforced, the cookie will fail to be created if
  196. // |secure| is true but the URL is an insecure scheme.
  197. std::unique_ptr<CanonicalCookie> cc =
  198. CanonicalCookie::CreateUnsafeCookieForTesting(
  199. "a", "1", base::StringPrintf("h%05d.izzle", i), "/path",
  200. creation_time, expiration_time, base::Time(), base::Time(), secure,
  201. false, CookieSameSite::NO_RESTRICTION, COOKIE_PRIORITY_DEFAULT,
  202. false);
  203. cc->SetLastAccessDate(last_access_time);
  204. store->AddCookie(*cc);
  205. }
  206. return std::make_unique<CookieMonster>(store.get(), /*net_log=*/nullptr,
  207. /*first_party_sets_enabled=*/false);
  208. }
  209. MockSimplePersistentCookieStore::~MockSimplePersistentCookieStore() = default;
  210. } // namespace net