OLD | NEW |
1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2010 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
9 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 9 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
10 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 FilePath path = GetCacheFilePath(); | 188 FilePath path = GetCacheFilePath(); |
189 ASSERT_TRUE(DeleteCache(path)); | 189 ASSERT_TRUE(DeleteCache(path)); |
190 base::Thread cache_thread("CacheThread"); | 190 base::Thread cache_thread("CacheThread"); |
191 ASSERT_TRUE(cache_thread.StartWithOptions( | 191 ASSERT_TRUE(cache_thread.StartWithOptions( |
192 base::Thread::Options(MessageLoop::TYPE_IO, 0))); | 192 base::Thread::Options(MessageLoop::TYPE_IO, 0))); |
193 | 193 |
194 // Test the private factory methods. | 194 // Test the private factory methods. |
195 disk_cache::Backend* cache = NULL; | 195 disk_cache::Backend* cache = NULL; |
196 int rv = disk_cache::BackendImpl::CreateBackend( | 196 int rv = disk_cache::BackendImpl::CreateBackend( |
197 path, false, 0, net::DISK_CACHE, disk_cache::kNoRandom, | 197 path, false, 0, net::DISK_CACHE, disk_cache::kNoRandom, |
198 cache_thread.message_loop_proxy(), &cache, &cb); | 198 cache_thread.message_loop_proxy(), NULL, &cache, &cb); |
199 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 199 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
200 ASSERT_TRUE(cache); | 200 ASSERT_TRUE(cache); |
201 delete cache; | 201 delete cache; |
202 | 202 |
203 cache = disk_cache::MemBackendImpl::CreateBackend(0); | 203 cache = disk_cache::MemBackendImpl::CreateBackend(0); |
204 ASSERT_TRUE(cache); | 204 ASSERT_TRUE(cache); |
205 delete cache; | 205 delete cache; |
206 cache = NULL; | 206 cache = NULL; |
207 | 207 |
208 // Now test the public API. | 208 // Now test the public API. |
209 rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, path, 0, false, | 209 rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, path, 0, false, |
210 cache_thread.message_loop_proxy(), | 210 cache_thread.message_loop_proxy(), |
211 &cache, &cb); | 211 NULL, &cache, &cb); |
212 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 212 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
213 ASSERT_TRUE(cache); | 213 ASSERT_TRUE(cache); |
214 delete cache; | 214 delete cache; |
215 cache = NULL; | 215 cache = NULL; |
216 | 216 |
217 rv = disk_cache::CreateCacheBackend(net::MEMORY_CACHE, FilePath(), 0, false, | 217 rv = disk_cache::CreateCacheBackend(net::MEMORY_CACHE, FilePath(), 0, false, |
218 NULL, &cache, &cb); | 218 NULL, NULL, &cache, &cb); |
219 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 219 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
220 ASSERT_TRUE(cache); | 220 ASSERT_TRUE(cache); |
221 delete cache; | 221 delete cache; |
222 } | 222 } |
223 | 223 |
224 MessageLoop::current()->RunAllPending(); | 224 MessageLoop::current()->RunAllPending(); |
225 } | 225 } |
226 | 226 |
227 TEST_F(DiskCacheBackendTest, ExternalFiles) { | 227 TEST_F(DiskCacheBackendTest, ExternalFiles) { |
228 InitCache(); | 228 InitCache(); |
(...skipping 24 matching lines...) Expand all Loading... |
253 { | 253 { |
254 FilePath path = GetCacheFilePath(); | 254 FilePath path = GetCacheFilePath(); |
255 ASSERT_TRUE(DeleteCache(path)); | 255 ASSERT_TRUE(DeleteCache(path)); |
256 base::Thread cache_thread("CacheThread"); | 256 base::Thread cache_thread("CacheThread"); |
257 ASSERT_TRUE(cache_thread.StartWithOptions( | 257 ASSERT_TRUE(cache_thread.StartWithOptions( |
258 base::Thread::Options(MessageLoop::TYPE_IO, 0))); | 258 base::Thread::Options(MessageLoop::TYPE_IO, 0))); |
259 | 259 |
260 disk_cache::Backend* cache; | 260 disk_cache::Backend* cache; |
261 int rv = disk_cache::BackendImpl::CreateBackend( | 261 int rv = disk_cache::BackendImpl::CreateBackend( |
262 path, false, 0, net::DISK_CACHE, disk_cache::kNoRandom, | 262 path, false, 0, net::DISK_CACHE, disk_cache::kNoRandom, |
263 base::MessageLoopProxy::CreateForCurrentThread(), &cache, &cb); | 263 base::MessageLoopProxy::CreateForCurrentThread(), NULL, |
| 264 &cache, &cb); |
264 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 265 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
265 | 266 |
266 disk_cache::EntryImpl* entry; | 267 disk_cache::EntryImpl* entry; |
267 rv = cache->CreateEntry("some key", | 268 rv = cache->CreateEntry("some key", |
268 reinterpret_cast<disk_cache::Entry**>(&entry), &cb); | 269 reinterpret_cast<disk_cache::Entry**>(&entry), &cb); |
269 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 270 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
270 | 271 |
271 const int kSize = 25000; | 272 const int kSize = 25000; |
272 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); | 273 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); |
273 CacheTestFillBuffer(buffer->data(), kSize, false); | 274 CacheTestFillBuffer(buffer->data(), kSize, false); |
(...skipping 30 matching lines...) Expand all Loading... |
304 { | 305 { |
305 FilePath path = GetCacheFilePath(); | 306 FilePath path = GetCacheFilePath(); |
306 ASSERT_TRUE(DeleteCache(path)); | 307 ASSERT_TRUE(DeleteCache(path)); |
307 base::Thread cache_thread("CacheThread"); | 308 base::Thread cache_thread("CacheThread"); |
308 ASSERT_TRUE(cache_thread.StartWithOptions( | 309 ASSERT_TRUE(cache_thread.StartWithOptions( |
309 base::Thread::Options(MessageLoop::TYPE_IO, 0))); | 310 base::Thread::Options(MessageLoop::TYPE_IO, 0))); |
310 | 311 |
311 disk_cache::Backend* cache; | 312 disk_cache::Backend* cache; |
312 int rv = disk_cache::BackendImpl::CreateBackend( | 313 int rv = disk_cache::BackendImpl::CreateBackend( |
313 path, false, 0, net::DISK_CACHE, disk_cache::kNoRandom, | 314 path, false, 0, net::DISK_CACHE, disk_cache::kNoRandom, |
314 cache_thread.message_loop_proxy(), &cache, &cb); | 315 cache_thread.message_loop_proxy(), NULL, &cache, &cb); |
315 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 316 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
316 | 317 |
317 disk_cache::Entry* entry; | 318 disk_cache::Entry* entry; |
318 rv = cache->CreateEntry("some key", &entry, &cb); | 319 rv = cache->CreateEntry("some key", &entry, &cb); |
319 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 320 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
320 | 321 |
321 const int kSize = 25000; | 322 const int kSize = 25000; |
322 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); | 323 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); |
323 CacheTestFillBuffer(buffer->data(), kSize, false); | 324 CacheTestFillBuffer(buffer->data(), kSize, false); |
324 | 325 |
(...skipping 16 matching lines...) Expand all Loading... |
341 ASSERT_EQ(5, file_util::WriteFile(index, "hello", 5)); | 342 ASSERT_EQ(5, file_util::WriteFile(index, "hello", 5)); |
342 | 343 |
343 base::Thread cache_thread("CacheThread"); | 344 base::Thread cache_thread("CacheThread"); |
344 ASSERT_TRUE(cache_thread.StartWithOptions( | 345 ASSERT_TRUE(cache_thread.StartWithOptions( |
345 base::Thread::Options(MessageLoop::TYPE_IO, 0))); | 346 base::Thread::Options(MessageLoop::TYPE_IO, 0))); |
346 TestCompletionCallback cb; | 347 TestCompletionCallback cb; |
347 | 348 |
348 disk_cache::Backend* backend = NULL; | 349 disk_cache::Backend* backend = NULL; |
349 int rv = disk_cache::BackendImpl::CreateBackend( | 350 int rv = disk_cache::BackendImpl::CreateBackend( |
350 path, false, 0, net::DISK_CACHE, disk_cache::kNone, | 351 path, false, 0, net::DISK_CACHE, disk_cache::kNone, |
351 cache_thread.message_loop_proxy(), &backend, &cb); | 352 cache_thread.message_loop_proxy(), NULL, &backend, &cb); |
352 ASSERT_NE(net::OK, cb.GetResult(rv)); | 353 ASSERT_NE(net::OK, cb.GetResult(rv)); |
353 | 354 |
354 ASSERT_TRUE(backend == NULL); | 355 ASSERT_TRUE(backend == NULL); |
355 delete backend; | 356 delete backend; |
356 } | 357 } |
357 | 358 |
358 void DiskCacheBackendTest::BackendSetSize() { | 359 void DiskCacheBackendTest::BackendSetSize() { |
359 SetDirectMode(); | 360 SetDirectMode(); |
360 const int cache_size = 0x10000; // 64 kB | 361 const int cache_size = 0x10000; // 64 kB |
361 SetMaxSize(cache_size); | 362 SetMaxSize(cache_size); |
(...skipping 909 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1271 ASSERT_TRUE(CopyTestCache("wrong_version")); | 1272 ASSERT_TRUE(CopyTestCache("wrong_version")); |
1272 FilePath path = GetCacheFilePath(); | 1273 FilePath path = GetCacheFilePath(); |
1273 base::Thread cache_thread("CacheThread"); | 1274 base::Thread cache_thread("CacheThread"); |
1274 ASSERT_TRUE(cache_thread.StartWithOptions( | 1275 ASSERT_TRUE(cache_thread.StartWithOptions( |
1275 base::Thread::Options(MessageLoop::TYPE_IO, 0))); | 1276 base::Thread::Options(MessageLoop::TYPE_IO, 0))); |
1276 TestCompletionCallback cb; | 1277 TestCompletionCallback cb; |
1277 | 1278 |
1278 disk_cache::Backend* cache; | 1279 disk_cache::Backend* cache; |
1279 int rv = disk_cache::BackendImpl::CreateBackend( | 1280 int rv = disk_cache::BackendImpl::CreateBackend( |
1280 path, true, 0, net::DISK_CACHE, disk_cache::kNoRandom, | 1281 path, true, 0, net::DISK_CACHE, disk_cache::kNoRandom, |
1281 cache_thread.message_loop_proxy(), &cache, &cb); | 1282 cache_thread.message_loop_proxy(), NULL, &cache, &cb); |
1282 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 1283 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
1283 | 1284 |
1284 MessageLoopHelper helper; | 1285 MessageLoopHelper helper; |
1285 | 1286 |
1286 ASSERT_TRUE(NULL != cache); | 1287 ASSERT_TRUE(NULL != cache); |
1287 ASSERT_EQ(0, cache->GetEntryCount()); | 1288 ASSERT_EQ(0, cache->GetEntryCount()); |
1288 | 1289 |
1289 delete cache; | 1290 delete cache; |
1290 } | 1291 } |
1291 | 1292 |
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1644 BackendDisable4(); | 1645 BackendDisable4(); |
1645 } | 1646 } |
1646 | 1647 |
1647 TEST_F(DiskCacheTest, Backend_UsageStats) { | 1648 TEST_F(DiskCacheTest, Backend_UsageStats) { |
1648 MessageLoopHelper helper; | 1649 MessageLoopHelper helper; |
1649 | 1650 |
1650 FilePath path = GetCacheFilePath(); | 1651 FilePath path = GetCacheFilePath(); |
1651 ASSERT_TRUE(DeleteCache(path)); | 1652 ASSERT_TRUE(DeleteCache(path)); |
1652 scoped_ptr<disk_cache::BackendImpl> cache; | 1653 scoped_ptr<disk_cache::BackendImpl> cache; |
1653 cache.reset(new disk_cache::BackendImpl( | 1654 cache.reset(new disk_cache::BackendImpl( |
1654 path, base::MessageLoopProxy::CreateForCurrentThread())); | 1655 path, base::MessageLoopProxy::CreateForCurrentThread(), |
| 1656 NULL)); |
1655 ASSERT_TRUE(NULL != cache.get()); | 1657 ASSERT_TRUE(NULL != cache.get()); |
1656 cache->SetUnitTestMode(); | 1658 cache->SetUnitTestMode(); |
1657 ASSERT_EQ(net::OK, cache->SyncInit()); | 1659 ASSERT_EQ(net::OK, cache->SyncInit()); |
1658 | 1660 |
1659 // Wait for a callback that never comes... about 2 secs :). The message loop | 1661 // Wait for a callback that never comes... about 2 secs :). The message loop |
1660 // has to run to allow invocation of the usage timer. | 1662 // has to run to allow invocation of the usage timer. |
1661 helper.WaitUntilCacheIoFinished(1); | 1663 helper.WaitUntilCacheIoFinished(1); |
1662 } | 1664 } |
1663 | 1665 |
1664 void DiskCacheBackendTest::BackendDoomAll() { | 1666 void DiskCacheBackendTest::BackendDoomAll() { |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1762 base::Thread cache_thread("CacheThread"); | 1764 base::Thread cache_thread("CacheThread"); |
1763 ASSERT_TRUE(cache_thread.StartWithOptions( | 1765 ASSERT_TRUE(cache_thread.StartWithOptions( |
1764 base::Thread::Options(MessageLoop::TYPE_IO, 0))); | 1766 base::Thread::Options(MessageLoop::TYPE_IO, 0))); |
1765 TestCompletionCallback cb; | 1767 TestCompletionCallback cb; |
1766 | 1768 |
1767 const int kNumberOfCaches = 2; | 1769 const int kNumberOfCaches = 2; |
1768 disk_cache::Backend* cache[kNumberOfCaches]; | 1770 disk_cache::Backend* cache[kNumberOfCaches]; |
1769 | 1771 |
1770 int rv = disk_cache::BackendImpl::CreateBackend( | 1772 int rv = disk_cache::BackendImpl::CreateBackend( |
1771 store1.path(), false, 0, net::DISK_CACHE, disk_cache::kNone, | 1773 store1.path(), false, 0, net::DISK_CACHE, disk_cache::kNone, |
1772 cache_thread.message_loop_proxy(), &cache[0], &cb); | 1774 cache_thread.message_loop_proxy(), NULL, &cache[0], &cb); |
1773 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 1775 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
1774 rv = disk_cache::BackendImpl::CreateBackend( | 1776 rv = disk_cache::BackendImpl::CreateBackend( |
1775 store2.path(), false, 0, net::MEDIA_CACHE, disk_cache::kNone, | 1777 store2.path(), false, 0, net::MEDIA_CACHE, disk_cache::kNone, |
1776 cache_thread.message_loop_proxy(), &cache[1], &cb); | 1778 cache_thread.message_loop_proxy(), NULL, &cache[1], &cb); |
1777 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 1779 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
1778 | 1780 |
1779 ASSERT_TRUE(cache[0] != NULL && cache[1] != NULL); | 1781 ASSERT_TRUE(cache[0] != NULL && cache[1] != NULL); |
1780 | 1782 |
1781 std::string key("the first key"); | 1783 std::string key("the first key"); |
1782 disk_cache::Entry* entry; | 1784 disk_cache::Entry* entry; |
1783 for (int i = 0; i < kNumberOfCaches; i++) { | 1785 for (int i = 0; i < kNumberOfCaches; i++) { |
1784 rv = cache[i]->CreateEntry(key, &entry, &cb); | 1786 rv = cache[i]->CreateEntry(key, &entry, &cb); |
1785 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 1787 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
1786 entry->Close(); | 1788 entry->Close(); |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1944 char buffer1[kSize]; | 1946 char buffer1[kSize]; |
1945 char buffer2[kSize]; | 1947 char buffer2[kSize]; |
1946 memset(buffer1, 't', kSize); | 1948 memset(buffer1, 't', kSize); |
1947 memset(buffer2, 0, kSize); | 1949 memset(buffer2, 0, kSize); |
1948 EXPECT_TRUE(file->Write(buffer1, kSize, 0)); | 1950 EXPECT_TRUE(file->Write(buffer1, kSize, 0)); |
1949 EXPECT_TRUE(file->Read(buffer2, kSize, 0)); | 1951 EXPECT_TRUE(file->Read(buffer2, kSize, 0)); |
1950 EXPECT_EQ(0, memcmp(buffer1, buffer2, kSize)); | 1952 EXPECT_EQ(0, memcmp(buffer1, buffer2, kSize)); |
1951 | 1953 |
1952 EXPECT_TRUE(disk_cache::DeleteCacheFile(name)); | 1954 EXPECT_TRUE(disk_cache::DeleteCacheFile(name)); |
1953 } | 1955 } |
OLD | NEW |