Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2011 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 "net/base/cookie_monster.h"
     11 
     12 namespace base {
     13 class Time;
     14 }
     15 
     16 namespace net {
     17 
     18 // Describes a call to one of the 3 functions of PersistentCookieStore.
     19 struct CookieStoreCommand {
     20   enum Type {
     21     ADD,
     22     UPDATE_ACCESS_TIME,
     23     REMOVE,
     24   };
     25 
     26   CookieStoreCommand(Type type,
     27                      const CookieMonster::CanonicalCookie& cookie)
     28       : type(type),
     29         cookie(cookie) {}
     30 
     31   Type type;
     32   CookieMonster::CanonicalCookie cookie;
     33 };
     34 
     35 // Implementation of PersistentCookieStore that captures the
     36 // received commands and saves them to a list.
     37 // The result of calls to Load() can be configured using SetLoadExpectation().
     38 class MockPersistentCookieStore
     39     : public CookieMonster::PersistentCookieStore {
     40  public:
     41   typedef std::vector<CookieStoreCommand> CommandList;
     42 
     43   MockPersistentCookieStore();
     44   virtual ~MockPersistentCookieStore();
     45 
     46   void SetLoadExpectation(
     47       bool return_value,
     48       const std::vector<CookieMonster::CanonicalCookie*>& result);
     49 
     50   const CommandList& commands() const {
     51     return commands_;
     52   }
     53 
     54   virtual bool Load(
     55       std::vector<CookieMonster::CanonicalCookie*>* out_cookies);
     56 
     57   virtual void AddCookie(const CookieMonster::CanonicalCookie& cookie);
     58 
     59   virtual void UpdateCookieAccessTime(
     60       const CookieMonster::CanonicalCookie& cookie);
     61 
     62   virtual void DeleteCookie(
     63       const CookieMonster::CanonicalCookie& cookie);
     64 
     65   virtual void Flush(Task* completion_task);
     66 
     67   // No files are created so nothing to clear either
     68   virtual void SetClearLocalStateOnExit(bool clear_local_state);
     69 
     70  private:
     71   CommandList commands_;
     72 
     73   // Deferred result to use when Load() is called.
     74   bool load_return_value_;
     75   std::vector<CookieMonster::CanonicalCookie*> load_result_;
     76 
     77   DISALLOW_COPY_AND_ASSIGN(MockPersistentCookieStore);
     78 };
     79 
     80 // Mock for CookieMonster::Delegate
     81 class MockCookieMonsterDelegate : public CookieMonster::Delegate {
     82  public:
     83   typedef std::pair<CookieMonster::CanonicalCookie, bool>
     84       CookieNotification;
     85 
     86   MockCookieMonsterDelegate();
     87 
     88   const std::vector<CookieNotification>& changes() const { return changes_; }
     89 
     90   void reset() { changes_.clear(); }
     91 
     92   virtual void OnCookieChanged(
     93       const CookieMonster::CanonicalCookie& cookie,
     94       bool removed,
     95       CookieMonster::Delegate::ChangeCause cause);
     96 
     97  private:
     98   virtual ~MockCookieMonsterDelegate();
     99 
    100   std::vector<CookieNotification> changes_;
    101 
    102   DISALLOW_COPY_AND_ASSIGN(MockCookieMonsterDelegate);
    103 };
    104 
    105 // Helper to build a list of CanonicalCookie*s.
    106 void AddCookieToList(
    107     const std::string& key,
    108     const std::string& cookie_line,
    109     const base::Time& creation_time,
    110     std::vector<CookieMonster::CanonicalCookie*>* out_list);
    111 
    112 // Just act like a backing database.  Keep cookie information from
    113 // Add/Update/Delete and regurgitate it when Load is called.
    114 class MockSimplePersistentCookieStore
    115     : public CookieMonster::PersistentCookieStore {
    116  public:
    117   MockSimplePersistentCookieStore();
    118   virtual ~MockSimplePersistentCookieStore();
    119 
    120   virtual bool Load(
    121       std::vector<CookieMonster::CanonicalCookie*>* out_cookies);
    122 
    123   virtual void AddCookie(
    124       const CookieMonster::CanonicalCookie& cookie);
    125 
    126   virtual void UpdateCookieAccessTime(
    127       const CookieMonster::CanonicalCookie& cookie);
    128 
    129   virtual void DeleteCookie(
    130       const CookieMonster::CanonicalCookie& cookie);
    131 
    132   virtual void Flush(Task* completion_task);
    133 
    134   virtual void SetClearLocalStateOnExit(bool clear_local_state);
    135 
    136  private:
    137   typedef std::map<int64, CookieMonster::CanonicalCookie>
    138       CanonicalCookieMap;
    139 
    140   CanonicalCookieMap cookies_;
    141 };
    142 
    143 // Helper function for creating a CookieMonster backed by a
    144 // MockSimplePersistentCookieStore for garbage collection testing.
    145 //
    146 // Fill the store through import with |num_cookies| cookies, |num_old_cookies|
    147 // with access time Now()-days_old, the rest with access time Now().
    148 // Do two SetCookies().  Return whether each of the two SetCookies() took
    149 // longer than |gc_perf_micros| to complete, and how many cookie were
    150 // left in the store afterwards.
    151 CookieMonster* CreateMonsterFromStoreForGC(
    152     int num_cookies,
    153     int num_old_cookies,
    154     int days_old);
    155 
    156 }  // namespace net
    157