OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 // This file contains test infrastructure for multiple files |
| 6 // (current cookie_monster_unittest.cc and cookie_monster_perftest.cc) |
| 7 // that need to test out CookieMonster interactions with the backing store. |
| 8 // It should only be included by test code. |
| 9 |
| 10 #include "base/time.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 // Describes a call to one of the 3 functions of PersistentCookieStore. |
| 16 struct CookieStoreCommand { |
| 17 enum Type { |
| 18 ADD, |
| 19 UPDATE_ACCESS_TIME, |
| 20 REMOVE, |
| 21 }; |
| 22 |
| 23 CookieStoreCommand(Type type, |
| 24 const std::string& key, |
| 25 const net::CookieMonster::CanonicalCookie& cookie) |
| 26 : type(type), key(key), cookie(cookie) {} |
| 27 |
| 28 Type type; |
| 29 std::string key; // Only applicable to the ADD command. |
| 30 net::CookieMonster::CanonicalCookie cookie; |
| 31 }; |
| 32 |
| 33 // Implementation of PersistentCookieStore that captures the |
| 34 // received commands and saves them to a list. |
| 35 // The result of calls to Load() can be configured using SetLoadExpectation(). |
| 36 class MockPersistentCookieStore |
| 37 : public net::CookieMonster::PersistentCookieStore { |
| 38 public: |
| 39 typedef std::vector<CookieStoreCommand> CommandList; |
| 40 |
| 41 MockPersistentCookieStore() : load_return_value_(true) { |
| 42 } |
| 43 |
| 44 virtual bool Load( |
| 45 std::vector<net::CookieMonster::KeyedCanonicalCookie>* out_cookies) { |
| 46 bool ok = load_return_value_; |
| 47 if (ok) |
| 48 *out_cookies = load_result_; |
| 49 return ok; |
| 50 } |
| 51 |
| 52 virtual void AddCookie(const std::string& key, |
| 53 const net::CookieMonster::CanonicalCookie& cookie) { |
| 54 commands_.push_back( |
| 55 CookieStoreCommand(CookieStoreCommand::ADD, key, cookie)); |
| 56 } |
| 57 |
| 58 virtual void UpdateCookieAccessTime( |
| 59 const net::CookieMonster::CanonicalCookie& cookie) { |
| 60 commands_.push_back(CookieStoreCommand( |
| 61 CookieStoreCommand::UPDATE_ACCESS_TIME, std::string(), cookie)); |
| 62 } |
| 63 |
| 64 virtual void DeleteCookie( |
| 65 const net::CookieMonster::CanonicalCookie& cookie) { |
| 66 commands_.push_back( |
| 67 CookieStoreCommand(CookieStoreCommand::REMOVE, std::string(), cookie)); |
| 68 } |
| 69 |
| 70 void SetLoadExpectation( |
| 71 bool return_value, |
| 72 const std::vector<net::CookieMonster::KeyedCanonicalCookie>& result) { |
| 73 load_return_value_ = return_value; |
| 74 load_result_ = result; |
| 75 } |
| 76 |
| 77 const CommandList& commands() const { |
| 78 return commands_; |
| 79 } |
| 80 |
| 81 private: |
| 82 CommandList commands_; |
| 83 |
| 84 // Deferred result to use when Load() is called. |
| 85 bool load_return_value_; |
| 86 std::vector<net::CookieMonster::KeyedCanonicalCookie> load_result_; |
| 87 |
| 88 DISALLOW_COPY_AND_ASSIGN(MockPersistentCookieStore); |
| 89 }; |
| 90 |
| 91 // Mock for CookieMonster::Delegate |
| 92 class MockCookieMonsterDelegate : public net::CookieMonster::Delegate { |
| 93 public: |
| 94 typedef std::pair<net::CookieMonster::CanonicalCookie, bool> |
| 95 CookieNotification; |
| 96 |
| 97 MockCookieMonsterDelegate() {} |
| 98 |
| 99 virtual void OnCookieChanged( |
| 100 const net::CookieMonster::CanonicalCookie& cookie, |
| 101 bool removed) { |
| 102 CookieNotification notification(cookie, removed); |
| 103 changes_.push_back(notification); |
| 104 } |
| 105 |
| 106 const std::vector<CookieNotification>& changes() const { return changes_; } |
| 107 |
| 108 void reset() { changes_.clear(); } |
| 109 |
| 110 private: |
| 111 virtual ~MockCookieMonsterDelegate() {} |
| 112 |
| 113 std::vector<CookieNotification> changes_; |
| 114 |
| 115 DISALLOW_COPY_AND_ASSIGN(MockCookieMonsterDelegate); |
| 116 }; |
| 117 |
| 118 // Helper to build a list of KeyedCanonicalCookies. |
| 119 static void AddKeyedCookieToList( |
| 120 const std::string& key, |
| 121 const std::string& cookie_line, |
| 122 const base::Time& creation_time, |
| 123 std::vector<net::CookieMonster::KeyedCanonicalCookie>* out_list) { |
| 124 |
| 125 // Parse the cookie line. |
| 126 net::CookieMonster::ParsedCookie pc(cookie_line); |
| 127 EXPECT_TRUE(pc.IsValid()); |
| 128 |
| 129 // This helper is simplistic in interpreting a parsed cookie, in order to |
| 130 // avoid duplicated CookieMonster's CanonPath() and CanonExpiration() |
| 131 // functions. Would be nice to export them, and re-use here. |
| 132 EXPECT_FALSE(pc.HasMaxAge()); |
| 133 EXPECT_TRUE(pc.HasPath()); |
| 134 base::Time cookie_expires = pc.HasExpires() ? |
| 135 net::CookieMonster::ParseCookieTime(pc.Expires()) : base::Time(); |
| 136 std::string cookie_path = pc.Path(); |
| 137 |
| 138 scoped_ptr<net::CookieMonster::CanonicalCookie> cookie( |
| 139 new net::CookieMonster::CanonicalCookie( |
| 140 pc.Name(), pc.Value(), key, cookie_path, |
| 141 pc.IsSecure(), pc.IsHttpOnly(), |
| 142 creation_time, creation_time, |
| 143 !cookie_expires.is_null(), |
| 144 cookie_expires)); |
| 145 |
| 146 out_list->push_back( |
| 147 net::CookieMonster::KeyedCanonicalCookie( |
| 148 key, cookie.release())); |
| 149 } |
| 150 |
| 151 } // namespace |
OLD | NEW |