15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This is a mock of the http cache and related testing classes. To be fair, it
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// is not really a mock http cache given that it uses the real implementation of
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the http cache, but it has fake implementations of all required components,
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// so it is useful for unit tests at the http layer.
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef NET_HTTP_MOCK_HTTP_CACHE_H_
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define NET_HTTP_MOCK_HTTP_CACHE_H_
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/containers/hash_tables.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/disk_cache/disk_cache.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/http/http_cache.h"
16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "net/http/http_transaction_test_util.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//-----------------------------------------------------------------------------
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Mock disk cache (a very basic memory cache implementation).
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MockDiskEntry : public disk_cache::Entry,
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      public base::RefCounted<MockDiskEntry> {
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit MockDiskEntry(const std::string& key);
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_doomed() const { return doomed_; }
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Doom() OVERRIDE;
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Close() OVERRIDE;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual std::string GetKey() const OVERRIDE;
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual base::Time GetLastUsed() const OVERRIDE;
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual base::Time GetLastModified() const OVERRIDE;
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int32 GetDataSize(int index) const OVERRIDE;
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int ReadData(int index, int offset, net::IOBuffer* buf, int buf_len,
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       const net::CompletionCallback& callback) OVERRIDE;
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int WriteData(int index, int offset, net::IOBuffer* buf, int buf_len,
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        const net::CompletionCallback& callback,
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        bool truncate) OVERRIDE;
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int ReadSparseData(int64 offset, net::IOBuffer* buf, int buf_len,
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             const net::CompletionCallback& callback) OVERRIDE;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int WriteSparseData(
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      int64 offset, net::IOBuffer* buf, int buf_len,
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const net::CompletionCallback& callback) OVERRIDE;
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int GetAvailableRange(
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      int64 offset, int len, int64* start,
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const net::CompletionCallback& callback) OVERRIDE;
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool CouldBeSparse() const OVERRIDE;
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void CancelSparseIO() OVERRIDE;
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int ReadyForSparseIO(
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const net::CompletionCallback& completion_callback) OVERRIDE;
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Fail most subsequent requests.
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void set_fail_requests() { fail_requests_ = true; }
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void set_fail_sparse_requests() { fail_sparse_requests_ = true; }
5658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If |value| is true, don't deliver any completion callbacks until called
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // again with |value| set to false.  Caution: remember to enable callbacks
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // again or all subsequent tests will fail.
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void IgnoreCallbacks(bool value);
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class base::RefCounted<MockDiskEntry>;
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  struct CallbackInfo;
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~MockDiskEntry();
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Unlike the callbacks for MockHttpTransaction, we want this one to run even
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // if the consumer called Close on the MockDiskEntry.  We achieve that by
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // leveraging the fact that this class is reference counted.
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void CallbackLater(const net::CompletionCallback& callback, int result);
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RunCallback(const net::CompletionCallback& callback, int result);
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // When |store| is true, stores the callback to be delivered later; otherwise
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // delivers any callback previously stored.
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void StoreAndDeliverCallbacks(bool store, MockDiskEntry* entry,
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                       const net::CompletionCallback& callback,
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                       int result);
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const int kNumCacheEntryDataIndices = 3;
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string key_;
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<char> data_[kNumCacheEntryDataIndices];
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int test_mode_;
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool doomed_;
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool sparse_;
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool fail_requests_;
8958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool fail_sparse_requests_;
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool busy_;
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool delayed_;
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool cancel_;
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool ignore_callbacks_;
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MockDiskCache : public disk_cache::Backend {
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockDiskCache();
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~MockDiskCache();
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual net::CacheType GetCacheType() const OVERRIDE;
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int32 GetEntryCount() const OVERRIDE;
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int OpenEntry(const std::string& key, disk_cache::Entry** entry,
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        const net::CompletionCallback& callback) OVERRIDE;
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry,
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          const net::CompletionCallback& callback) OVERRIDE;
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int DoomEntry(const std::string& key,
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        const net::CompletionCallback& callback) OVERRIDE;
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int DoomAllEntries(const net::CompletionCallback& callback) OVERRIDE;
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int DoomEntriesBetween(
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Time initial_time,
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Time end_time,
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const net::CompletionCallback& callback) OVERRIDE;
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int DoomEntriesSince(
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Time initial_time,
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const net::CompletionCallback& callback) OVERRIDE;
1171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  virtual scoped_ptr<Iterator> CreateIterator() OVERRIDE;
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void GetStats(
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::vector<std::pair<std::string, std::string> >* stats) OVERRIDE;
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnExternalCacheHit(const std::string& key) OVERRIDE;
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns number of times a cache entry was successfully opened.
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int open_count() const { return open_count_; }
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns number of times a cache entry was successfully created.
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int create_count() const { return create_count_; }
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Fail any subsequent CreateEntry and OpenEntry.
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void set_fail_requests() { fail_requests_ = true; }
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Return entries that fail some of their requests.
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void set_soft_failures(bool value) { soft_failures_ = value; }
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Makes sure that CreateEntry is not called twice for a given key.
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void set_double_create_check(bool value) { double_create_check_ = value; }
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
13758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Makes all requests for data ranges to fail as not implemented.
13858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void set_fail_sparse_requests() { fail_sparse_requests_ = true; }
13958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ReleaseAll();
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef base::hash_map<std::string, MockDiskEntry*> EntryMap;
1441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  class NotImplementedIterator;
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void CallbackLater(const net::CompletionCallback& callback, int result);
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EntryMap entries_;
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int open_count_;
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int create_count_;
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool fail_requests_;
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool soft_failures_;
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool double_create_check_;
15458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool fail_sparse_requests_;
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MockBackendFactory : public net::HttpCache::BackendFactory {
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int CreateBackend(net::NetLog* net_log,
160a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                            scoped_ptr<disk_cache::Backend>* backend,
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            const net::CompletionCallback& callback) OVERRIDE;
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MockHttpCache {
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockHttpCache();
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit MockHttpCache(net::HttpCache::BackendFactory* disk_cache_factory);
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  net::HttpCache* http_cache() { return &http_cache_; }
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockNetworkLayer* network_layer() {
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return static_cast<MockNetworkLayer*>(http_cache_.network_layer());
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockDiskCache* disk_cache();
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Wrapper around http_cache()->CreateTransaction(net::DEFAULT_PRIORITY...)
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int CreateTransaction(scoped_ptr<net::HttpTransaction>* trans);
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
179116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Wrapper to bypass the cache lock for new transactions.
180116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void BypassCacheLock();
181116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper function for reading response info from the disk cache.
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool ReadResponseInfo(disk_cache::Entry* disk_entry,
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               net::HttpResponseInfo* response_info,
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               bool* response_truncated);
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper function for writing response info into the disk cache.
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool WriteResponseInfo(disk_cache::Entry* disk_entry,
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                const net::HttpResponseInfo* response_info,
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                bool skip_transient_headers,
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                bool response_truncated);
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper function to synchronously open a backend entry.
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool OpenBackendEntry(const std::string& key, disk_cache::Entry** entry);
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper function to synchronously create a backend entry.
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool CreateBackendEntry(const std::string& key, disk_cache::Entry** entry,
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          net::NetLog* net_log);
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the test mode after considering the global override.
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static int GetTestMode(int test_mode);
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overrides the test mode for a given operation. Remember to reset it after
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the test! (by setting test_mode to zero).
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void SetTestMode(int test_mode);
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  net::HttpCache http_cache_;
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This version of the disk cache doesn't invoke CreateEntry callbacks.
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MockDiskCacheNoCB : public MockDiskCache {
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry,
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          const net::CompletionCallback& callback) OVERRIDE;
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MockBackendNoCbFactory : public net::HttpCache::BackendFactory {
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int CreateBackend(net::NetLog* net_log,
220a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                            scoped_ptr<disk_cache::Backend>* backend,
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            const net::CompletionCallback& callback) OVERRIDE;
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This backend factory allows us to control the backend instantiation.
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MockBlockingBackendFactory : public net::HttpCache::BackendFactory {
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockBlockingBackendFactory();
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~MockBlockingBackendFactory();
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int CreateBackend(net::NetLog* net_log,
231a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                            scoped_ptr<disk_cache::Backend>* backend,
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            const net::CompletionCallback& callback) OVERRIDE;
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Completes the backend creation. Any blocked call will be notified via the
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // provided callback.
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void FinishCreation();
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
238a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  scoped_ptr<disk_cache::Backend>* backend() { return backend_; }
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void set_fail(bool fail) { fail_ = fail; }
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const net::CompletionCallback& callback() { return callback_; }
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int Result() { return fail_ ? net::ERR_FAILED : net::OK; }
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
246a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  scoped_ptr<disk_cache::Backend>* backend_;
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  net::CompletionCallback callback_;
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool block_;
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool fail_;
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // NET_HTTP_MOCK_HTTP_CACHE_H_
253