| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2014 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 #include "base/bind.h" |
| 6 #include "base/file_util.h" |
| 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/message_loop/message_loop_proxy.h" |
| 9 #include "base/run_loop.h" |
| 10 #include "net/base/io_buffer.h" |
| 11 #include "net/base/net_errors.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "webkit/browser/appcache/appcache_disk_cache.h" |
| 14 |
| 15 namespace appcache { |
| 16 |
| 17 class AppCacheDiskCacheTest : public testing::Test { |
| 18 public: |
| 19 AppCacheDiskCacheTest() {} |
| 20 |
| 21 virtual void SetUp() OVERRIDE { |
| 22 // Use the current thread for the DiskCache's cache_thread. |
| 23 message_loop_.reset(new base::MessageLoopForIO()); |
| 24 cache_thread_ = base::MessageLoopProxy::current(); |
| 25 ASSERT_TRUE(directory_.CreateUniqueTempDir()); |
| 26 completion_callback_ = base::Bind( |
| 27 &AppCacheDiskCacheTest::OnComplete, |
| 28 base::Unretained(this)); |
| 29 } |
| 30 |
| 31 virtual void TearDown() OVERRIDE { |
| 32 base::RunLoop().RunUntilIdle(); |
| 33 message_loop_.reset(NULL); |
| 34 } |
| 35 |
| 36 void FlushCacheTasks() { |
| 37 base::RunLoop().RunUntilIdle(); |
| 38 } |
| 39 |
| 40 void OnComplete(int err) { |
| 41 completion_results_.push_back(err); |
| 42 } |
| 43 |
| 44 base::ScopedTempDir directory_; |
| 45 scoped_ptr<base::MessageLoop> message_loop_; |
| 46 scoped_refptr<base::MessageLoopProxy> cache_thread_; |
| 47 net::CompletionCallback completion_callback_; |
| 48 std::vector<int> completion_results_; |
| 49 |
| 50 static const int k10MBytes = 10 * 1024 * 1024; |
| 51 }; |
| 52 |
| 53 TEST_F(AppCacheDiskCacheTest, DisablePriorToInitCompletion) { |
| 54 AppCacheDiskCache::Entry* entry = NULL; |
| 55 |
| 56 // Create an instance and start it initializing, queue up |
| 57 // one of each kind of "entry" function. |
| 58 scoped_ptr<AppCacheDiskCache> disk_cache(new AppCacheDiskCache); |
| 59 EXPECT_FALSE(disk_cache->is_disabled()); |
| 60 disk_cache->InitWithDiskBackend( |
| 61 directory_.path(), k10MBytes, false, cache_thread_, |
| 62 completion_callback_); |
| 63 disk_cache->CreateEntry(1, &entry, completion_callback_); |
| 64 disk_cache->OpenEntry(2, &entry, completion_callback_); |
| 65 disk_cache->DoomEntry(3, completion_callback_); |
| 66 |
| 67 // Pull the plug on all that. |
| 68 EXPECT_FALSE(disk_cache->is_disabled()); |
| 69 disk_cache->Disable(); |
| 70 EXPECT_TRUE(disk_cache->is_disabled()); |
| 71 |
| 72 FlushCacheTasks(); |
| 73 |
| 74 EXPECT_EQ(NULL, entry); |
| 75 EXPECT_EQ(4u, completion_results_.size()); |
| 76 for (std::vector<int>::const_iterator iter = completion_results_.begin(); |
| 77 iter < completion_results_.end(); ++iter) { |
| 78 EXPECT_EQ(net::ERR_ABORTED, *iter); |
| 79 } |
| 80 |
| 81 // Ensure the directory can be deleted at this point. |
| 82 EXPECT_TRUE(base::DirectoryExists(directory_.path())); |
| 83 EXPECT_FALSE(base::IsDirectoryEmpty(directory_.path())); |
| 84 EXPECT_TRUE(base::DeleteFile(directory_.path(), true)); |
| 85 EXPECT_FALSE(base::DirectoryExists(directory_.path())); |
| 86 } |
| 87 |
| 88 TEST_F(AppCacheDiskCacheTest, DisableAfterInitted) { |
| 89 // Create an instance and let it fully init. |
| 90 scoped_ptr<AppCacheDiskCache> disk_cache(new AppCacheDiskCache); |
| 91 EXPECT_FALSE(disk_cache->is_disabled()); |
| 92 disk_cache->InitWithDiskBackend( |
| 93 directory_.path(), k10MBytes, false, cache_thread_, |
| 94 completion_callback_); |
| 95 FlushCacheTasks(); |
| 96 EXPECT_EQ(1u, completion_results_.size()); |
| 97 EXPECT_EQ(net::OK, completion_results_[0]); |
| 98 |
| 99 // Pull the plug |
| 100 disk_cache->Disable(); |
| 101 FlushCacheTasks(); |
| 102 |
| 103 // Ensure the directory can be deleted at this point. |
| 104 EXPECT_TRUE(base::DirectoryExists(directory_.path())); |
| 105 EXPECT_FALSE(base::IsDirectoryEmpty(directory_.path())); |
| 106 EXPECT_TRUE(base::DeleteFile(directory_.path(), true)); |
| 107 EXPECT_FALSE(base::DirectoryExists(directory_.path())); |
| 108 |
| 109 // Methods should return immediately when disabled and not invoke |
| 110 // the callback at all. |
| 111 AppCacheDiskCache::Entry* entry = NULL; |
| 112 completion_results_.clear(); |
| 113 EXPECT_EQ(net::ERR_ABORTED, |
| 114 disk_cache->CreateEntry(1, &entry, completion_callback_)); |
| 115 EXPECT_EQ(net::ERR_ABORTED, |
| 116 disk_cache->OpenEntry(2, &entry, completion_callback_)); |
| 117 EXPECT_EQ(net::ERR_ABORTED, |
| 118 disk_cache->DoomEntry(3, completion_callback_)); |
| 119 FlushCacheTasks(); |
| 120 EXPECT_TRUE(completion_results_.empty()); |
| 121 } |
| 122 |
| 123 TEST_F(AppCacheDiskCacheTest, DisableWithEntriesOpen) { |
| 124 // Create an instance and let it fully init. |
| 125 scoped_ptr<AppCacheDiskCache> disk_cache(new AppCacheDiskCache); |
| 126 EXPECT_FALSE(disk_cache->is_disabled()); |
| 127 disk_cache->InitWithDiskBackend( |
| 128 directory_.path(), k10MBytes, false, cache_thread_, |
| 129 completion_callback_); |
| 130 FlushCacheTasks(); |
| 131 EXPECT_EQ(1u, completion_results_.size()); |
| 132 EXPECT_EQ(net::OK, completion_results_[0]); |
| 133 |
| 134 // Note: We don't have detailed expectations of the DiskCache |
| 135 // operations because on android it's really SimpleCache which |
| 136 // does behave differently. |
| 137 // |
| 138 // What matters for the corruption handling and service reinitiazation |
| 139 // is that the directory can be deleted after the calling Disable() method, |
| 140 // and we do have expectations about that. |
| 141 |
| 142 // Create/open some entries. |
| 143 AppCacheDiskCache::Entry* entry1 = NULL; |
| 144 AppCacheDiskCache::Entry* entry2 = NULL; |
| 145 disk_cache->CreateEntry(1, &entry1, completion_callback_); |
| 146 disk_cache->CreateEntry(2, &entry2, completion_callback_); |
| 147 FlushCacheTasks(); |
| 148 EXPECT_TRUE(entry1); |
| 149 EXPECT_TRUE(entry2); |
| 150 |
| 151 // Write something to one of the entries and flush it. |
| 152 const char* kData = "Hello"; |
| 153 const int kDataLen = strlen(kData) + 1; |
| 154 scoped_refptr<net::IOBuffer> write_buf(new net::WrappedIOBuffer(kData)); |
| 155 entry1->Write(0, 0, write_buf, kDataLen, completion_callback_); |
| 156 FlushCacheTasks(); |
| 157 |
| 158 // Queue up a read and a write. |
| 159 scoped_refptr<net::IOBuffer> read_buf = new net::IOBuffer(kDataLen); |
| 160 entry1->Read(0, 0, read_buf.get(), kDataLen, completion_callback_); |
| 161 entry2->Write(0, 0, write_buf.get(), kDataLen, completion_callback_); |
| 162 |
| 163 // Pull the plug |
| 164 disk_cache->Disable(); |
| 165 FlushCacheTasks(); |
| 166 |
| 167 // Ensure the directory can be deleted at this point. |
| 168 EXPECT_TRUE(base::DirectoryExists(directory_.path())); |
| 169 EXPECT_FALSE(base::IsDirectoryEmpty(directory_.path())); |
| 170 EXPECT_TRUE(base::DeleteFile(directory_.path(), true)); |
| 171 EXPECT_FALSE(base::DirectoryExists(directory_.path())); |
| 172 |
| 173 disk_cache.reset(NULL); |
| 174 |
| 175 // Also, new IO operations should fail immediately. |
| 176 EXPECT_EQ( |
| 177 net::ERR_ABORTED, |
| 178 entry1->Read(0, 0, read_buf.get(), kDataLen, completion_callback_)); |
| 179 entry1->Close(); |
| 180 entry2->Close(); |
| 181 |
| 182 FlushCacheTasks(); |
| 183 } |
| 184 |
| 185 } // namespace appcache |
| OLD | NEW |