Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(191)

Side by Side Diff: net/http/http_cache_unittest.cc

Issue 19747: URLRequestContext and disk cache for media files (Closed)
Patch Set: signed and unsigned... Created 11 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/http/http_cache.cc ('k') | net/http/http_network_layer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/http/http_cache.h" 5 #include "net/http/http_cache.h"
6 6
7 #include "base/hash_tables.h" 7 #include "base/hash_tables.h"
8 #include "base/message_loop.h" 8 #include "base/message_loop.h"
9 #include "base/platform_file.h"
9 #include "base/string_util.h" 10 #include "base/string_util.h"
10 #include "net/base/net_errors.h" 11 #include "net/base/net_errors.h"
11 #include "net/base/load_flags.h" 12 #include "net/base/load_flags.h"
12 #include "net/disk_cache/disk_cache.h" 13 #include "net/disk_cache/disk_cache.h"
13 #include "net/http/http_request_info.h" 14 #include "net/http/http_request_info.h"
14 #include "net/http/http_response_info.h" 15 #include "net/http/http_response_info.h"
15 #include "net/http/http_transaction.h" 16 #include "net/http/http_transaction.h"
16 #include "net/http/http_transaction_unittest.h" 17 #include "net/http/http_transaction_unittest.h"
17 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
18 19
19 using base::Time; 20 using base::Time;
20 21
21 namespace { 22 namespace {
22 23
23 //----------------------------------------------------------------------------- 24 //-----------------------------------------------------------------------------
24 // mock disk cache (a very basic memory cache implementation) 25 // mock disk cache (a very basic memory cache implementation)
25 26
26 class MockDiskEntry : public disk_cache::Entry, 27 class MockDiskEntry : public disk_cache::Entry,
27 public base::RefCounted<MockDiskEntry> { 28 public base::RefCounted<MockDiskEntry> {
28 public: 29 public:
29 MockDiskEntry() : test_mode_(0), doomed_(false) { 30 MockDiskEntry()
31 : test_mode_(0), doomed_(false), platform_file_(global_platform_file_) {
30 } 32 }
31 33
32 MockDiskEntry(const std::string& key) : key_(key), doomed_(false) { 34 MockDiskEntry(const std::string& key)
35 : key_(key), doomed_(false), platform_file_(global_platform_file_) {
33 const MockTransaction* t = FindMockTransaction(GURL(key)); 36 const MockTransaction* t = FindMockTransaction(GURL(key));
34 DCHECK(t); 37 DCHECK(t);
35 test_mode_ = t->test_mode; 38 test_mode_ = t->test_mode;
36 } 39 }
37 40
38 ~MockDiskEntry() { 41 ~MockDiskEntry() {
39 } 42 }
40 43
41 bool is_doomed() const { return doomed_; } 44 bool is_doomed() const { return doomed_; }
42 45
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 94
92 if (offset < 0 || offset > static_cast<int>(data_[index].size())) 95 if (offset < 0 || offset > static_cast<int>(data_[index].size()))
93 return net::ERR_FAILED; 96 return net::ERR_FAILED;
94 97
95 data_[index].resize(offset + buf_len); 98 data_[index].resize(offset + buf_len);
96 if (buf_len) 99 if (buf_len)
97 memcpy(&data_[index][offset], buf->data(), buf_len); 100 memcpy(&data_[index][offset], buf->data(), buf_len);
98 return buf_len; 101 return buf_len;
99 } 102 }
100 103
104 base::PlatformFile UseExternalFile(int index) {
105 return platform_file_;
106 }
107
108 base::PlatformFile GetPlatformFile(int index) {
109 return platform_file_;
110 }
111
112 static void set_global_platform_file(base::PlatformFile platform_file) {
113 global_platform_file_ = platform_file;
114 }
115
101 private: 116 private:
102 // Unlike the callbacks for MockHttpTransaction, we want this one to run even 117 // Unlike the callbacks for MockHttpTransaction, we want this one to run even
103 // if the consumer called Close on the MockDiskEntry. We achieve that by 118 // if the consumer called Close on the MockDiskEntry. We achieve that by
104 // leveraging the fact that this class is reference counted. 119 // leveraging the fact that this class is reference counted.
105 void CallbackLater(net::CompletionCallback* callback, int result) { 120 void CallbackLater(net::CompletionCallback* callback, int result) {
106 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(this, 121 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(this,
107 &MockDiskEntry::RunCallback, callback, result)); 122 &MockDiskEntry::RunCallback, callback, result));
108 } 123 }
109 void RunCallback(net::CompletionCallback* callback, int result) { 124 void RunCallback(net::CompletionCallback* callback, int result) {
110 callback->Run(result); 125 callback->Run(result);
111 } 126 }
112 127
113 std::string key_; 128 std::string key_;
114 std::vector<char> data_[2]; 129 std::vector<char> data_[2];
115 int test_mode_; 130 int test_mode_;
116 bool doomed_; 131 bool doomed_;
132 base::PlatformFile platform_file_;
133 static base::PlatformFile global_platform_file_;
117 }; 134 };
118 135
136 base::PlatformFile MockDiskEntry::global_platform_file_ =
137 base::kInvalidPlatformFileValue;
138
119 class MockDiskCache : public disk_cache::Backend { 139 class MockDiskCache : public disk_cache::Backend {
120 public: 140 public:
121 MockDiskCache() : open_count_(0), create_count_(0), fail_requests_(0) { 141 MockDiskCache() : open_count_(0), create_count_(0), fail_requests_(0) {
122 } 142 }
123 143
124 ~MockDiskCache() { 144 ~MockDiskCache() {
125 EntryMap::iterator it = entries_.begin(); 145 EntryMap::iterator it = entries_.begin();
126 for (; it != entries_.end(); ++it) 146 for (; it != entries_.end(); ++it)
127 it->second->Release(); 147 it->second->Release();
128 } 148 }
(...skipping 1048 matching lines...) Expand 10 before | Expand all | Expand 10 after
1177 } 1197 }
1178 1198
1179 // Ensure that we don't crash by if left-behind transactions. 1199 // Ensure that we don't crash by if left-behind transactions.
1180 TEST(HttpCache, OutlivedTransactions) { 1200 TEST(HttpCache, OutlivedTransactions) {
1181 MockHttpCache* cache = new MockHttpCache; 1201 MockHttpCache* cache = new MockHttpCache;
1182 1202
1183 net::HttpTransaction* trans = cache->http_cache()->CreateTransaction(); 1203 net::HttpTransaction* trans = cache->http_cache()->CreateTransaction();
1184 delete cache; 1204 delete cache;
1185 delete trans; 1205 delete trans;
1186 } 1206 }
1207
1208 // Make sure Entry::UseExternalFile is called when a new entry is created in
1209 // a HttpCache with MEDIA type. Also make sure Entry::GetPlatformFile is called
1210 // when an entry is loaded from a HttpCache with MEDIA type. Also confirm we
1211 // will receive a file handle in ResponseInfo from a media cache.
1212 TEST(HttpCache, SimpleGET_MediaCache) {
1213 // Initialize the HttpCache with MEDIA type.
1214 MockHttpCache cache;
1215 cache.http_cache()->set_type(net::HttpCache::MEDIA);
1216
1217 // Define some fake file handles for testing.
1218 base::PlatformFile kFakePlatformFile1, kFakePlatformFile2;
1219 #if defined(OS_WIN)
1220 kFakePlatformFile1 = reinterpret_cast<base::PlatformFile>(1);
1221 kFakePlatformFile2 = reinterpret_cast<base::PlatformFile>(2);
1222 #else
1223 kFakePlatformFile1 = 1;
1224 kFakePlatformFile2 = 2;
1225 #endif
1226
1227 ScopedMockTransaction trans_info(kSimpleGET_Transaction);
1228 TestCompletionCallback callback;
1229
1230 {
1231 // Set the fake file handle to MockDiskEntry so cache is written with an
1232 // entry created with our fake file handle.
1233 MockDiskEntry::set_global_platform_file(kFakePlatformFile1);
1234
1235 scoped_ptr<net::HttpTransaction> trans(
1236 cache.http_cache()->CreateTransaction());
1237 ASSERT_TRUE(trans.get());
1238
1239 MockHttpRequest request(trans_info);
1240
1241 int rv = trans->Start(&request, &callback);
1242 if (rv == net::ERR_IO_PENDING)
1243 rv = callback.WaitForResult();
1244 ASSERT_EQ(net::OK, rv);
1245
1246 const net::HttpResponseInfo* response = trans->GetResponseInfo();
1247 ASSERT_TRUE(response);
1248
1249 ASSERT_EQ(kFakePlatformFile1, response->response_data_file);
1250
1251 ReadAndVerifyTransaction(trans.get(), trans_info);
1252 }
1253
1254 // Load only from cache so we would get the same file handle.
1255 trans_info.load_flags |= net::LOAD_ONLY_FROM_CACHE;
1256
1257 {
1258 // Set a different file handle value to MockDiskEntry so any new entry
1259 // created in the cache won't have the same file handle value.
1260 MockDiskEntry::set_global_platform_file(kFakePlatformFile2);
1261
1262 scoped_ptr<net::HttpTransaction> trans(
1263 cache.http_cache()->CreateTransaction());
1264 ASSERT_TRUE(trans.get());
1265
1266 MockHttpRequest request(trans_info);
1267
1268 int rv = trans->Start(&request, &callback);
1269 if (rv == net::ERR_IO_PENDING)
1270 rv = callback.WaitForResult();
1271 ASSERT_EQ(net::OK, rv);
1272
1273 const net::HttpResponseInfo* response = trans->GetResponseInfo();
1274 ASSERT_TRUE(response);
1275
1276 // Make sure we get the same file handle as in the first request.
1277 ASSERT_EQ(kFakePlatformFile1, response->response_data_file);
1278
1279 ReadAndVerifyTransaction(trans.get(), trans_info);
1280 }
1281 }
OLDNEW
« no previous file with comments | « net/http/http_cache.cc ('k') | net/http/http_network_layer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698