| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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 "webkit/browser/fileapi/quota/quota_backend_impl.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/files/scoped_temp_dir.h" | |
| 10 #include "base/message_loop/message_loop.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" | |
| 13 #include "third_party/leveldatabase/src/include/leveldb/env.h" | |
| 14 #include "webkit/browser/fileapi/file_system_usage_cache.h" | |
| 15 #include "webkit/browser/fileapi/obfuscated_file_util.h" | |
| 16 #include "webkit/browser/quota/quota_manager_proxy.h" | |
| 17 | |
| 18 namespace fileapi { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 const char kOrigin[] = "http://example.com"; | |
| 23 | |
| 24 bool DidReserveQuota(bool accepted, | |
| 25 base::File::Error* error_out, | |
| 26 int64* delta_out, | |
| 27 base::File::Error error, | |
| 28 int64 delta) { | |
| 29 DCHECK(error_out); | |
| 30 DCHECK(delta_out); | |
| 31 *error_out = error; | |
| 32 *delta_out = delta; | |
| 33 return accepted; | |
| 34 } | |
| 35 | |
| 36 class MockQuotaManagerProxy : public quota::QuotaManagerProxy { | |
| 37 public: | |
| 38 MockQuotaManagerProxy() | |
| 39 : QuotaManagerProxy(NULL, NULL), | |
| 40 storage_modified_count_(0), | |
| 41 usage_(0), quota_(0) {} | |
| 42 | |
| 43 // We don't mock them. | |
| 44 virtual void NotifyOriginInUse(const GURL& origin) OVERRIDE {} | |
| 45 virtual void NotifyOriginNoLongerInUse(const GURL& origin) OVERRIDE {} | |
| 46 virtual void SetUsageCacheEnabled(quota::QuotaClient::ID client_id, | |
| 47 const GURL& origin, | |
| 48 quota::StorageType type, | |
| 49 bool enabled) OVERRIDE {} | |
| 50 | |
| 51 virtual void NotifyStorageModified( | |
| 52 quota::QuotaClient::ID client_id, | |
| 53 const GURL& origin, | |
| 54 quota::StorageType type, | |
| 55 int64 delta) OVERRIDE { | |
| 56 ++storage_modified_count_; | |
| 57 usage_ += delta; | |
| 58 ASSERT_LE(usage_, quota_); | |
| 59 } | |
| 60 | |
| 61 virtual void GetUsageAndQuota( | |
| 62 base::SequencedTaskRunner* original_task_runner, | |
| 63 const GURL& origin, | |
| 64 quota::StorageType type, | |
| 65 const GetUsageAndQuotaCallback& callback) OVERRIDE { | |
| 66 callback.Run(quota::kQuotaStatusOk, usage_, quota_); | |
| 67 } | |
| 68 | |
| 69 int storage_modified_count() { return storage_modified_count_; } | |
| 70 int64 usage() { return usage_; } | |
| 71 void set_usage(int64 usage) { usage_ = usage; } | |
| 72 void set_quota(int64 quota) { quota_ = quota; } | |
| 73 | |
| 74 protected: | |
| 75 virtual ~MockQuotaManagerProxy() {} | |
| 76 | |
| 77 private: | |
| 78 int storage_modified_count_; | |
| 79 int64 usage_; | |
| 80 int64 quota_; | |
| 81 | |
| 82 DISALLOW_COPY_AND_ASSIGN(MockQuotaManagerProxy); | |
| 83 }; | |
| 84 | |
| 85 } // namespace | |
| 86 | |
| 87 class QuotaBackendImplTest : public testing::Test { | |
| 88 public: | |
| 89 QuotaBackendImplTest() | |
| 90 : file_system_usage_cache_(file_task_runner()), | |
| 91 quota_manager_proxy_(new MockQuotaManagerProxy) {} | |
| 92 | |
| 93 virtual void SetUp() OVERRIDE { | |
| 94 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | |
| 95 in_memory_env_.reset(leveldb::NewMemEnv(leveldb::Env::Default())); | |
| 96 file_util_.reset(ObfuscatedFileUtil::CreateForTesting( | |
| 97 NULL, data_dir_.path(), in_memory_env_.get(), file_task_runner())); | |
| 98 backend_.reset(new QuotaBackendImpl(file_task_runner(), | |
| 99 file_util_.get(), | |
| 100 &file_system_usage_cache_, | |
| 101 quota_manager_proxy_.get())); | |
| 102 } | |
| 103 | |
| 104 virtual void TearDown() OVERRIDE { | |
| 105 backend_.reset(); | |
| 106 quota_manager_proxy_ = NULL; | |
| 107 file_util_.reset(); | |
| 108 message_loop_.RunUntilIdle(); | |
| 109 } | |
| 110 | |
| 111 protected: | |
| 112 void InitializeForOriginAndType(const GURL& origin, FileSystemType type) { | |
| 113 ASSERT_TRUE(file_util_->InitOriginDatabase(origin, true /* create */)); | |
| 114 ASSERT_TRUE(file_util_->origin_database_ != NULL); | |
| 115 | |
| 116 std::string type_string = | |
| 117 SandboxFileSystemBackendDelegate::GetTypeString(type); | |
| 118 base::File::Error error = base::File::FILE_ERROR_FAILED; | |
| 119 base::FilePath path = file_util_->GetDirectoryForOriginAndType( | |
| 120 origin, type_string, true /* create */, &error); | |
| 121 ASSERT_EQ(base::File::FILE_OK, error); | |
| 122 | |
| 123 ASSERT_TRUE(file_system_usage_cache_.UpdateUsage( | |
| 124 GetUsageCachePath(origin, type), 0)); | |
| 125 } | |
| 126 | |
| 127 base::SequencedTaskRunner* file_task_runner() { | |
| 128 return base::MessageLoopProxy::current().get(); | |
| 129 } | |
| 130 | |
| 131 base::FilePath GetUsageCachePath(const GURL& origin, FileSystemType type) { | |
| 132 base::FilePath path; | |
| 133 base::File::Error error = | |
| 134 backend_->GetUsageCachePath(origin, type, &path); | |
| 135 EXPECT_EQ(base::File::FILE_OK, error); | |
| 136 EXPECT_FALSE(path.empty()); | |
| 137 return path; | |
| 138 } | |
| 139 | |
| 140 base::MessageLoop message_loop_; | |
| 141 base::ScopedTempDir data_dir_; | |
| 142 scoped_ptr<leveldb::Env> in_memory_env_; | |
| 143 scoped_ptr<ObfuscatedFileUtil> file_util_; | |
| 144 FileSystemUsageCache file_system_usage_cache_; | |
| 145 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; | |
| 146 scoped_ptr<QuotaBackendImpl> backend_; | |
| 147 | |
| 148 private: | |
| 149 DISALLOW_COPY_AND_ASSIGN(QuotaBackendImplTest); | |
| 150 }; | |
| 151 | |
| 152 TEST_F(QuotaBackendImplTest, ReserveQuota_Basic) { | |
| 153 FileSystemType type = fileapi::kFileSystemTypeTemporary; | |
| 154 InitializeForOriginAndType(GURL(kOrigin), type); | |
| 155 quota_manager_proxy_->set_quota(10000); | |
| 156 | |
| 157 int64 delta = 0; | |
| 158 | |
| 159 const int64 kDelta1 = 1000; | |
| 160 base::File::Error error = base::File::FILE_ERROR_FAILED; | |
| 161 backend_->ReserveQuota(GURL(kOrigin), type, kDelta1, | |
| 162 base::Bind(&DidReserveQuota, true, &error, &delta)); | |
| 163 EXPECT_EQ(base::File::FILE_OK, error); | |
| 164 EXPECT_EQ(kDelta1, delta); | |
| 165 EXPECT_EQ(kDelta1, quota_manager_proxy_->usage()); | |
| 166 | |
| 167 const int64 kDelta2 = -300; | |
| 168 error = base::File::FILE_ERROR_FAILED; | |
| 169 backend_->ReserveQuota(GURL(kOrigin), type, kDelta2, | |
| 170 base::Bind(&DidReserveQuota, true, &error, &delta)); | |
| 171 EXPECT_EQ(base::File::FILE_OK, error); | |
| 172 EXPECT_EQ(kDelta2, delta); | |
| 173 EXPECT_EQ(kDelta1 + kDelta2, quota_manager_proxy_->usage()); | |
| 174 | |
| 175 EXPECT_EQ(2, quota_manager_proxy_->storage_modified_count()); | |
| 176 } | |
| 177 | |
| 178 TEST_F(QuotaBackendImplTest, ReserveQuota_NoSpace) { | |
| 179 FileSystemType type = fileapi::kFileSystemTypeTemporary; | |
| 180 InitializeForOriginAndType(GURL(kOrigin), type); | |
| 181 quota_manager_proxy_->set_quota(100); | |
| 182 | |
| 183 int64 delta = 0; | |
| 184 | |
| 185 const int64 kDelta = 1000; | |
| 186 base::File::Error error = base::File::FILE_ERROR_FAILED; | |
| 187 backend_->ReserveQuota(GURL(kOrigin), type, kDelta, | |
| 188 base::Bind(&DidReserveQuota, true, &error, &delta)); | |
| 189 EXPECT_EQ(base::File::FILE_OK, error); | |
| 190 EXPECT_EQ(100, delta); | |
| 191 EXPECT_EQ(100, quota_manager_proxy_->usage()); | |
| 192 | |
| 193 EXPECT_EQ(1, quota_manager_proxy_->storage_modified_count()); | |
| 194 } | |
| 195 | |
| 196 TEST_F(QuotaBackendImplTest, ReserveQuota_Revert) { | |
| 197 FileSystemType type = fileapi::kFileSystemTypeTemporary; | |
| 198 InitializeForOriginAndType(GURL(kOrigin), type); | |
| 199 quota_manager_proxy_->set_quota(10000); | |
| 200 | |
| 201 int64 delta = 0; | |
| 202 | |
| 203 const int64 kDelta = 1000; | |
| 204 base::File::Error error = base::File::FILE_ERROR_FAILED; | |
| 205 backend_->ReserveQuota(GURL(kOrigin), type, kDelta, | |
| 206 base::Bind(&DidReserveQuota, false, &error, &delta)); | |
| 207 EXPECT_EQ(base::File::FILE_OK, error); | |
| 208 EXPECT_EQ(kDelta, delta); | |
| 209 EXPECT_EQ(0, quota_manager_proxy_->usage()); | |
| 210 | |
| 211 EXPECT_EQ(2, quota_manager_proxy_->storage_modified_count()); | |
| 212 } | |
| 213 | |
| 214 TEST_F(QuotaBackendImplTest, ReleaseReservedQuota) { | |
| 215 FileSystemType type = fileapi::kFileSystemTypeTemporary; | |
| 216 InitializeForOriginAndType(GURL(kOrigin), type); | |
| 217 const int64 kInitialUsage = 2000; | |
| 218 quota_manager_proxy_->set_usage(kInitialUsage); | |
| 219 quota_manager_proxy_->set_quota(10000); | |
| 220 | |
| 221 const int64 kSize = 1000; | |
| 222 backend_->ReleaseReservedQuota(GURL(kOrigin), type, kSize); | |
| 223 EXPECT_EQ(kInitialUsage - kSize, quota_manager_proxy_->usage()); | |
| 224 | |
| 225 EXPECT_EQ(1, quota_manager_proxy_->storage_modified_count()); | |
| 226 } | |
| 227 | |
| 228 TEST_F(QuotaBackendImplTest, CommitQuotaUsage) { | |
| 229 FileSystemType type = fileapi::kFileSystemTypeTemporary; | |
| 230 InitializeForOriginAndType(GURL(kOrigin), type); | |
| 231 quota_manager_proxy_->set_quota(10000); | |
| 232 base::FilePath path = GetUsageCachePath(GURL(kOrigin), type); | |
| 233 | |
| 234 const int64 kDelta1 = 1000; | |
| 235 backend_->CommitQuotaUsage(GURL(kOrigin), type, kDelta1); | |
| 236 EXPECT_EQ(kDelta1, quota_manager_proxy_->usage()); | |
| 237 int64 usage = 0; | |
| 238 EXPECT_TRUE(file_system_usage_cache_.GetUsage(path, &usage)); | |
| 239 EXPECT_EQ(kDelta1, usage); | |
| 240 | |
| 241 const int64 kDelta2 = -300; | |
| 242 backend_->CommitQuotaUsage(GURL(kOrigin), type, kDelta2); | |
| 243 EXPECT_EQ(kDelta1 + kDelta2, quota_manager_proxy_->usage()); | |
| 244 usage = 0; | |
| 245 EXPECT_TRUE(file_system_usage_cache_.GetUsage(path, &usage)); | |
| 246 EXPECT_EQ(kDelta1 + kDelta2, usage); | |
| 247 | |
| 248 EXPECT_EQ(2, quota_manager_proxy_->storage_modified_count()); | |
| 249 } | |
| 250 | |
| 251 TEST_F(QuotaBackendImplTest, DirtyCount) { | |
| 252 FileSystemType type = fileapi::kFileSystemTypeTemporary; | |
| 253 InitializeForOriginAndType(GURL(kOrigin), type); | |
| 254 base::FilePath path = GetUsageCachePath(GURL(kOrigin), type); | |
| 255 | |
| 256 backend_->IncrementDirtyCount(GURL(kOrigin), type); | |
| 257 uint32 dirty = 0; | |
| 258 ASSERT_TRUE(file_system_usage_cache_.GetDirty(path, &dirty)); | |
| 259 EXPECT_EQ(1u, dirty); | |
| 260 | |
| 261 backend_->DecrementDirtyCount(GURL(kOrigin), type); | |
| 262 ASSERT_TRUE(file_system_usage_cache_.GetDirty(path, &dirty)); | |
| 263 EXPECT_EQ(0u, dirty); | |
| 264 } | |
| 265 | |
| 266 } // namespace fileapi | |
| OLD | NEW |