cookie_monster_store_test.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. // This file contains test infrastructure for multiple files
  5. // (current cookie_monster_unittest.cc and cookie_monster_perftest.cc)
  6. // that need to test out CookieMonster interactions with the backing store.
  7. // It should only be included by test code.
  8. #ifndef NET_COOKIES_COOKIE_MONSTER_STORE_TEST_H_
  9. #define NET_COOKIES_COOKIE_MONSTER_STORE_TEST_H_
  10. #include <stdint.h>
  11. #include <map>
  12. #include <memory>
  13. #include <string>
  14. #include <utility>
  15. #include <vector>
  16. #include "net/cookies/canonical_cookie.h"
  17. #include "net/cookies/cookie_monster.h"
  18. #include "net/log/net_log_with_source.h"
  19. class GURL;
  20. namespace base {
  21. class Time;
  22. }
  23. namespace net {
  24. // Describes a call to one of the 5 functions of PersistentCookieStore.
  25. struct CookieStoreCommand {
  26. enum Type {
  27. LOAD,
  28. LOAD_COOKIES_FOR_KEY,
  29. // UPDATE_ACCESS_TIME is not included in this list, because get cookie
  30. // commands may or may not end updating the access time, unless they have
  31. // the option set not to do so.
  32. ADD,
  33. REMOVE,
  34. };
  35. // Constructor for LOAD and LOAD_COOKIES_FOR_KEY calls. |key| should be empty
  36. // for LOAD_COOKIES_FOR_KEY.
  37. CookieStoreCommand(
  38. Type type,
  39. CookieMonster::PersistentCookieStore::LoadedCallback loaded_callback,
  40. const std::string& key);
  41. // Constructor for ADD, UPDATE_ACCESS_TIME, and REMOVE calls.
  42. CookieStoreCommand(Type type, const CanonicalCookie& cookie);
  43. CookieStoreCommand(CookieStoreCommand&& other);
  44. ~CookieStoreCommand();
  45. Type type;
  46. // Only non-null for LOAD and LOAD_COOKIES_FOR_KEY.
  47. CookieMonster::PersistentCookieStore::LoadedCallback loaded_callback;
  48. // Only non-empty for LOAD_COOKIES_FOR_KEY.
  49. std::string key;
  50. // Only non-null for ADD, UPDATE_ACCESS_TIME, and REMOVE.
  51. CanonicalCookie cookie;
  52. };
  53. // Implementation of PersistentCookieStore that captures the
  54. // received commands and saves them to a list.
  55. // The result of calls to Load() can be configured using SetLoadExpectation().
  56. class MockPersistentCookieStore : public CookieMonster::PersistentCookieStore {
  57. public:
  58. typedef std::vector<CookieStoreCommand> CommandList;
  59. MockPersistentCookieStore();
  60. MockPersistentCookieStore(const MockPersistentCookieStore&) = delete;
  61. MockPersistentCookieStore& operator=(const MockPersistentCookieStore&) =
  62. delete;
  63. // When set, Load() and LoadCookiesForKey() calls are store in the command
  64. // list, rather than being automatically executed. Defaults to false.
  65. void set_store_load_commands(bool store_load_commands) {
  66. store_load_commands_ = store_load_commands;
  67. }
  68. void SetLoadExpectation(bool return_value,
  69. std::vector<std::unique_ptr<CanonicalCookie>> result);
  70. const CommandList& commands() const { return commands_; }
  71. CommandList TakeCommands() { return std::move(commands_); }
  72. CookieMonster::PersistentCookieStore::LoadedCallback TakeCallbackAt(
  73. size_t i) {
  74. return std::move(commands_[i].loaded_callback);
  75. }
  76. void Load(LoadedCallback loaded_callback,
  77. const NetLogWithSource& net_log) override;
  78. void LoadCookiesForKey(const std::string& key,
  79. LoadedCallback loaded_callback) override;
  80. void AddCookie(const CanonicalCookie& cookie) override;
  81. void UpdateCookieAccessTime(const CanonicalCookie& cookie) override;
  82. void DeleteCookie(const CanonicalCookie& cookie) override;
  83. void SetForceKeepSessionState() override;
  84. void SetBeforeCommitCallback(base::RepeatingClosure callback) override;
  85. void Flush(base::OnceClosure callback) override;
  86. protected:
  87. ~MockPersistentCookieStore() override;
  88. private:
  89. CommandList commands_;
  90. bool store_load_commands_ = false;
  91. // Deferred result to use when Load() is called.
  92. bool load_return_value_ = true;
  93. std::vector<std::unique_ptr<CanonicalCookie>> load_result_;
  94. // Indicates if the store has been fully loaded to avoid returning duplicate
  95. // cookies.
  96. bool loaded_ = false;
  97. };
  98. // Helper to build a single CanonicalCookie.
  99. std::unique_ptr<CanonicalCookie> BuildCanonicalCookie(
  100. const GURL& url,
  101. const std::string& cookie_line,
  102. const base::Time& creation_time);
  103. // Helper to build a list of CanonicalCookie*s.
  104. void AddCookieToList(const GURL& url,
  105. const std::string& cookie_line,
  106. const base::Time& creation_time,
  107. std::vector<std::unique_ptr<CanonicalCookie>>* out_list);
  108. // Just act like a backing database. Keep cookie information from
  109. // Add/Update/Delete and regurgitate it when Load is called.
  110. class MockSimplePersistentCookieStore
  111. : public CookieMonster::PersistentCookieStore {
  112. public:
  113. MockSimplePersistentCookieStore();
  114. void Load(LoadedCallback loaded_callback,
  115. const NetLogWithSource& net_log) override;
  116. void LoadCookiesForKey(const std::string& key,
  117. LoadedCallback loaded_callback) override;
  118. void AddCookie(const CanonicalCookie& cookie) override;
  119. void UpdateCookieAccessTime(const CanonicalCookie& cookie) override;
  120. void DeleteCookie(const CanonicalCookie& cookie) override;
  121. void SetForceKeepSessionState() override;
  122. void SetBeforeCommitCallback(base::RepeatingClosure callback) override;
  123. void Flush(base::OnceClosure callback) override;
  124. protected:
  125. ~MockSimplePersistentCookieStore() override;
  126. private:
  127. typedef std::map<CanonicalCookie::UniqueCookieKey, CanonicalCookie>
  128. CanonicalCookieMap;
  129. CanonicalCookieMap cookies_;
  130. // Indicates if the store has been fully loaded to avoid return duplicate
  131. // cookies in subsequent load requests
  132. bool loaded_ = false;
  133. };
  134. // Helper function for creating a CookieMonster backed by a
  135. // MockSimplePersistentCookieStore for garbage collection testing.
  136. //
  137. // Fill the store through import with |num_*_cookies| cookies,
  138. // |num_old_*_cookies| with access time Now()-days_old, the rest with access
  139. // time Now(). Cookies made by |num_secure_cookies| and |num_non_secure_cookies|
  140. // will be marked secure and non-secure, respectively. Do two SetCookies().
  141. // Return whether each of the two SetCookies() took longer than |gc_perf_micros|
  142. // to complete, and how many cookie were left in the store afterwards.
  143. std::unique_ptr<CookieMonster> CreateMonsterFromStoreForGC(
  144. int num_secure_cookies,
  145. int num_old_secure_cookies,
  146. int num_non_secure_cookies,
  147. int num_old_non_secure_cookies,
  148. int days_old);
  149. } // namespace net
  150. #endif // NET_COOKIES_COOKIE_MONSTER_STORE_TEST_H_