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

Side by Side Diff: chrome/browser/chromeos/drive/file_cache_unittest.cc

Issue 296463002: drive: Stop using FileCacheEntry related methods in FileCache (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Do not overwrite cache state with RefreshEntry Created 6 years, 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/chromeos/drive/file_cache.h" 5 #include "chrome/browser/chromeos/drive/file_cache.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/callback_helpers.h" 10 #include "base/callback_helpers.h"
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 cache_files_dir_, 53 cache_files_dir_,
54 base::MessageLoopProxy::current().get(), 54 base::MessageLoopProxy::current().get(),
55 fake_free_disk_space_getter_.get())); 55 fake_free_disk_space_getter_.get()));
56 ASSERT_TRUE(cache_->Initialize()); 56 ASSERT_TRUE(cache_->Initialize());
57 } 57 }
58 58
59 static bool RenameCacheFilesToNewFormat(FileCache* cache) { 59 static bool RenameCacheFilesToNewFormat(FileCache* cache) {
60 return cache->RenameCacheFilesToNewFormat(); 60 return cache->RenameCacheFilesToNewFormat();
61 } 61 }
62 62
63 FileError GetCacheEntry(FileCache* cache,
64 const std::string& id,
65 FileCacheEntry* cache_entry) {
66 return cache->storage_->GetCacheEntry(id, cache_entry);
67 }
68
69 content::TestBrowserThreadBundle thread_bundle_; 63 content::TestBrowserThreadBundle thread_bundle_;
70 base::ScopedTempDir temp_dir_; 64 base::ScopedTempDir temp_dir_;
71 base::FilePath cache_files_dir_; 65 base::FilePath cache_files_dir_;
72 66
73 scoped_ptr<ResourceMetadataStorage, test_util::DestroyHelperForTests> 67 scoped_ptr<ResourceMetadataStorage, test_util::DestroyHelperForTests>
74 metadata_storage_; 68 metadata_storage_;
75 scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_; 69 scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_;
76 scoped_ptr<FakeFreeDiskSpaceGetter> fake_free_disk_space_getter_; 70 scoped_ptr<FakeFreeDiskSpaceGetter> fake_free_disk_space_getter_;
77 }; 71 };
78 72
79 TEST_F(FileCacheTest, RecoverFilesFromCacheDirectory) { 73 TEST_F(FileCacheTest, RecoverFilesFromCacheDirectory) {
80 base::FilePath dir_source_root; 74 base::FilePath dir_source_root;
81 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &dir_source_root)); 75 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &dir_source_root));
82 const base::FilePath src_path = 76 const base::FilePath src_path =
83 dir_source_root.AppendASCII("chrome/test/data/chromeos/drive/image.png"); 77 dir_source_root.AppendASCII("chrome/test/data/chromeos/drive/image.png");
84 78
85 // Store files. This file should not be moved. 79 // Store files. This file should not be moved.
80 ResourceEntry entry;
81 entry.set_local_id("id_foo");
82 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
86 EXPECT_EQ(FILE_ERROR_OK, cache_->Store("id_foo", "md5", src_path, 83 EXPECT_EQ(FILE_ERROR_OK, cache_->Store("id_foo", "md5", src_path,
87 FileCache::FILE_OPERATION_COPY)); 84 FileCache::FILE_OPERATION_COPY));
88 85
89 // Set up files in the cache directory. These files should be moved. 86 // Set up files in the cache directory. These files should be moved.
90 const base::FilePath file_directory = 87 const base::FilePath file_directory =
91 temp_dir_.path().AppendASCII(kCacheFileDirectory); 88 temp_dir_.path().AppendASCII(kCacheFileDirectory);
92 ASSERT_TRUE(base::CopyFile(src_path, file_directory.AppendASCII("id_bar"))); 89 ASSERT_TRUE(base::CopyFile(src_path, file_directory.AppendASCII("id_bar")));
93 ASSERT_TRUE(base::CopyFile(src_path, file_directory.AppendASCII("id_baz"))); 90 ASSERT_TRUE(base::CopyFile(src_path, file_directory.AppendASCII("id_baz")));
94 91
95 // Insert a dirty entry with "id_baz" to |recovered_cache_info|. 92 // Insert a dirty entry with "id_baz" to |recovered_cache_info|.
(...skipping 28 matching lines...) Expand all
124 EXPECT_FALSE(base::PathExists( 121 EXPECT_FALSE(base::PathExists(
125 dest_directory.AppendASCII("image00000003.png"))); 122 dest_directory.AppendASCII("image00000003.png")));
126 } 123 }
127 124
128 TEST_F(FileCacheTest, FreeDiskSpaceIfNeededFor) { 125 TEST_F(FileCacheTest, FreeDiskSpaceIfNeededFor) {
129 base::FilePath src_file; 126 base::FilePath src_file;
130 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); 127 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file));
131 128
132 // Store a file as a 'temporary' file and remember the path. 129 // Store a file as a 'temporary' file and remember the path.
133 const std::string id_tmp = "id_tmp", md5_tmp = "md5_tmp"; 130 const std::string id_tmp = "id_tmp", md5_tmp = "md5_tmp";
131
132 ResourceEntry entry;
133 entry.set_local_id(id_tmp);
134 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
134 ASSERT_EQ(FILE_ERROR_OK, 135 ASSERT_EQ(FILE_ERROR_OK,
135 cache_->Store(id_tmp, md5_tmp, src_file, 136 cache_->Store(id_tmp, md5_tmp, src_file,
136 FileCache::FILE_OPERATION_COPY)); 137 FileCache::FILE_OPERATION_COPY));
137 base::FilePath tmp_path; 138 base::FilePath tmp_path;
138 ASSERT_EQ(FILE_ERROR_OK, cache_->GetFile(id_tmp, &tmp_path)); 139 ASSERT_EQ(FILE_ERROR_OK, cache_->GetFile(id_tmp, &tmp_path));
139 140
140 // Store a file as a pinned file and remember the path. 141 // Store a file as a pinned file and remember the path.
141 const std::string id_pinned = "id_pinned", md5_pinned = "md5_pinned"; 142 const std::string id_pinned = "id_pinned", md5_pinned = "md5_pinned";
143 entry.Clear();
144 entry.set_local_id(id_pinned);
145 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
142 ASSERT_EQ(FILE_ERROR_OK, 146 ASSERT_EQ(FILE_ERROR_OK,
143 cache_->Store(id_pinned, md5_pinned, src_file, 147 cache_->Store(id_pinned, md5_pinned, src_file,
144 FileCache::FILE_OPERATION_COPY)); 148 FileCache::FILE_OPERATION_COPY));
145 ASSERT_EQ(FILE_ERROR_OK, cache_->Pin(id_pinned)); 149 ASSERT_EQ(FILE_ERROR_OK, cache_->Pin(id_pinned));
146 base::FilePath pinned_path; 150 base::FilePath pinned_path;
147 ASSERT_EQ(FILE_ERROR_OK, cache_->GetFile(id_pinned, &pinned_path)); 151 ASSERT_EQ(FILE_ERROR_OK, cache_->GetFile(id_pinned, &pinned_path));
148 152
149 // Call FreeDiskSpaceIfNeededFor(). 153 // Call FreeDiskSpaceIfNeededFor().
150 fake_free_disk_space_getter_->set_default_value(test_util::kLotsOfSpace); 154 fake_free_disk_space_getter_->set_default_value(test_util::kLotsOfSpace);
151 fake_free_disk_space_getter_->PushFakeValue(0); 155 fake_free_disk_space_getter_->PushFakeValue(0);
152 const int64 kNeededBytes = 1; 156 const int64 kNeededBytes = 1;
153 EXPECT_TRUE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); 157 EXPECT_TRUE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes));
154 158
155 // Only 'temporary' file gets removed. 159 // Only 'temporary' file gets removed.
156 FileCacheEntry entry; 160 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_tmp, &entry));
157 EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetCacheEntry(cache_.get(), id_tmp, &entry)); 161 EXPECT_FALSE(entry.file_specific_info().cache_state().is_present());
158 EXPECT_FALSE(base::PathExists(tmp_path)); 162 EXPECT_FALSE(base::PathExists(tmp_path));
159 163
160 EXPECT_EQ(FILE_ERROR_OK, GetCacheEntry(cache_.get(), id_pinned, &entry)); 164 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_pinned, &entry));
165 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
161 EXPECT_TRUE(base::PathExists(pinned_path)); 166 EXPECT_TRUE(base::PathExists(pinned_path));
162 167
163 // Returns false when disk space cannot be freed. 168 // Returns false when disk space cannot be freed.
164 fake_free_disk_space_getter_->set_default_value(0); 169 fake_free_disk_space_getter_->set_default_value(0);
165 EXPECT_FALSE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); 170 EXPECT_FALSE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes));
166 } 171 }
167 172
168 TEST_F(FileCacheTest, GetFile) { 173 TEST_F(FileCacheTest, GetFile) {
169 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat"); 174 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat");
170 const std::string src_contents = "test"; 175 const std::string src_contents = "test";
171 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, 176 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path,
172 src_contents)); 177 src_contents));
173 std::string id("id1"); 178 std::string id("id1");
174 std::string md5(base::MD5String(src_contents)); 179 std::string md5(base::MD5String(src_contents));
175 180
176 const base::FilePath cache_file_directory = 181 const base::FilePath cache_file_directory =
177 temp_dir_.path().AppendASCII(kCacheFileDirectory); 182 temp_dir_.path().AppendASCII(kCacheFileDirectory);
178 183
179 // Try to get an existing file from cache. 184 // Try to get an existing file from cache.
185 ResourceEntry entry;
186 entry.set_local_id(id);
187 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
180 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(id, md5, src_file_path, 188 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(id, md5, src_file_path,
181 FileCache::FILE_OPERATION_COPY)); 189 FileCache::FILE_OPERATION_COPY));
182 base::FilePath cache_file_path; 190 base::FilePath cache_file_path;
183 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(id, &cache_file_path)); 191 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(id, &cache_file_path));
184 EXPECT_EQ( 192 EXPECT_EQ(
185 cache_file_directory.AppendASCII(util::EscapeCacheFileName(id)).value(), 193 cache_file_directory.AppendASCII(util::EscapeCacheFileName(id)).value(),
186 cache_file_path.value()); 194 cache_file_path.value());
187 195
188 std::string contents; 196 std::string contents;
189 EXPECT_TRUE(base::ReadFileToString(cache_file_path, &contents)); 197 EXPECT_TRUE(base::ReadFileToString(cache_file_path, &contents));
190 EXPECT_EQ(src_contents, contents); 198 EXPECT_EQ(src_contents, contents);
191 199
192 // Get file from cache with different id. 200 // Get file from cache with different id.
193 id = "id2"; 201 id = "id2";
202 entry.Clear();
203 entry.set_local_id(id);
204 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
194 EXPECT_EQ(FILE_ERROR_NOT_FOUND, cache_->GetFile(id, &cache_file_path)); 205 EXPECT_EQ(FILE_ERROR_NOT_FOUND, cache_->GetFile(id, &cache_file_path));
195 206
196 // Pin a non-existent file. 207 // Pin a non-existent file.
197 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(id)); 208 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(id));
198 209
199 // Get the non-existent pinned file from cache. 210 // Get the non-existent pinned file from cache.
200 EXPECT_EQ(FILE_ERROR_NOT_FOUND, cache_->GetFile(id, &cache_file_path)); 211 EXPECT_EQ(FILE_ERROR_NOT_FOUND, cache_->GetFile(id, &cache_file_path));
201 212
202 // Get a previously pinned and stored file from cache. 213 // Get a previously pinned and stored file from cache.
203 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(id, md5, src_file_path, 214 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(id, md5, src_file_path,
(...skipping 11 matching lines...) Expand all
215 226
216 TEST_F(FileCacheTest, Store) { 227 TEST_F(FileCacheTest, Store) {
217 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat"); 228 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat");
218 const std::string src_contents = "test"; 229 const std::string src_contents = "test";
219 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, 230 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path,
220 src_contents)); 231 src_contents));
221 std::string id("id"); 232 std::string id("id");
222 std::string md5(base::MD5String(src_contents)); 233 std::string md5(base::MD5String(src_contents));
223 234
224 // Store a file. 235 // Store a file.
236 ResourceEntry entry;
237 entry.set_local_id(id);
238 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
225 EXPECT_EQ(FILE_ERROR_OK, cache_->Store( 239 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(
226 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY)); 240 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY));
227 241
228 FileCacheEntry cache_entry; 242 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
229 EXPECT_EQ(FILE_ERROR_OK, GetCacheEntry(cache_.get(), id, &cache_entry)); 243 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
230 EXPECT_TRUE(cache_entry.is_present()); 244 EXPECT_EQ(md5, entry.file_specific_info().cache_state().md5());
231 EXPECT_EQ(md5, cache_entry.md5());
232 245
233 base::FilePath cache_file_path; 246 base::FilePath cache_file_path;
234 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(id, &cache_file_path)); 247 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(id, &cache_file_path));
235 EXPECT_TRUE(base::ContentsEqual(src_file_path, cache_file_path)); 248 EXPECT_TRUE(base::ContentsEqual(src_file_path, cache_file_path));
236 249
237 // Store a non-existent file. 250 // Store a non-existent file.
238 EXPECT_EQ(FILE_ERROR_FAILED, cache_->Store( 251 EXPECT_EQ(FILE_ERROR_FAILED, cache_->Store(
239 id, md5, base::FilePath::FromUTF8Unsafe("non_existent_file"), 252 id, md5, base::FilePath::FromUTF8Unsafe("non_existent_file"),
240 FileCache::FILE_OPERATION_COPY)); 253 FileCache::FILE_OPERATION_COPY));
241 254
242 // Passing empty MD5 marks the entry as dirty. 255 // Passing empty MD5 marks the entry as dirty.
243 EXPECT_EQ(FILE_ERROR_OK, cache_->Store( 256 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(
244 id, std::string(), src_file_path, FileCache::FILE_OPERATION_COPY)); 257 id, std::string(), src_file_path, FileCache::FILE_OPERATION_COPY));
245 258
246 EXPECT_EQ(FILE_ERROR_OK, GetCacheEntry(cache_.get(), id, &cache_entry)); 259 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
247 EXPECT_TRUE(cache_entry.is_present()); 260 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
248 EXPECT_TRUE(cache_entry.md5().empty()); 261 EXPECT_TRUE(entry.file_specific_info().cache_state().md5().empty());
249 EXPECT_TRUE(cache_entry.is_dirty()); 262 EXPECT_TRUE(entry.file_specific_info().cache_state().is_dirty());
250 263
251 // No free space available. 264 // No free space available.
252 fake_free_disk_space_getter_->set_default_value(0); 265 fake_free_disk_space_getter_->set_default_value(0);
253 266
254 EXPECT_EQ(FILE_ERROR_NO_LOCAL_SPACE, cache_->Store( 267 EXPECT_EQ(FILE_ERROR_NO_LOCAL_SPACE, cache_->Store(
255 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY)); 268 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY));
256 } 269 }
257 270
258 TEST_F(FileCacheTest, PinAndUnpin) { 271 TEST_F(FileCacheTest, PinAndUnpin) {
259 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat"); 272 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat");
260 const std::string src_contents = "test"; 273 const std::string src_contents = "test";
261 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, 274 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path,
262 src_contents)); 275 src_contents));
263 std::string id("id_present"); 276 std::string id("id_present");
264 std::string md5(base::MD5String(src_contents)); 277 std::string md5(base::MD5String(src_contents));
265 278
266 // Store a file. 279 // Store a file.
280 ResourceEntry entry;
281 entry.set_local_id(id);
282 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
267 EXPECT_EQ(FILE_ERROR_OK, cache_->Store( 283 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(
268 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY)); 284 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY));
269 285
270 FileCacheEntry cache_entry; 286 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
271 EXPECT_EQ(FILE_ERROR_OK, GetCacheEntry(cache_.get(), id, &cache_entry)); 287 EXPECT_FALSE(entry.file_specific_info().cache_state().is_pinned());
272 EXPECT_FALSE(cache_entry.is_pinned());
273 288
274 // Pin the existing file. 289 // Pin the existing file.
275 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(id)); 290 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(id));
276 291
277 EXPECT_EQ(FILE_ERROR_OK, GetCacheEntry(cache_.get(), id, &cache_entry)); 292 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
278 EXPECT_TRUE(cache_entry.is_pinned()); 293 EXPECT_TRUE(entry.file_specific_info().cache_state().is_pinned());
279 294
280 // Unpin the file. 295 // Unpin the file.
281 EXPECT_EQ(FILE_ERROR_OK, cache_->Unpin(id)); 296 EXPECT_EQ(FILE_ERROR_OK, cache_->Unpin(id));
282 297
283 EXPECT_EQ(FILE_ERROR_OK, GetCacheEntry(cache_.get(), id, &cache_entry)); 298 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
284 EXPECT_FALSE(cache_entry.is_pinned()); 299 EXPECT_FALSE(entry.file_specific_info().cache_state().is_pinned());
285 300
286 // Pin a non-present file. 301 // Pin a non-present file.
287 std::string id_non_present = "id_non_present"; 302 std::string id_non_present = "id_non_present";
303 entry.Clear();
304 entry.set_local_id(id_non_present);
305 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
288 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(id_non_present)); 306 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(id_non_present));
289 307
290 EXPECT_EQ(FILE_ERROR_OK, 308 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_non_present, &entry));
291 GetCacheEntry(cache_.get(), id_non_present, &cache_entry)); 309 EXPECT_TRUE(entry.file_specific_info().cache_state().is_pinned());
292 EXPECT_TRUE(cache_entry.is_pinned());
293 310
294 // Unpin the previously pinned non-existent file. 311 // Unpin the previously pinned non-existent file.
295 EXPECT_EQ(FILE_ERROR_OK, cache_->Unpin(id_non_present)); 312 EXPECT_EQ(FILE_ERROR_OK, cache_->Unpin(id_non_present));
296 313
297 EXPECT_EQ(FILE_ERROR_NOT_FOUND, 314 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_non_present, &entry));
298 GetCacheEntry(cache_.get(), id_non_present, &cache_entry)); 315 EXPECT_FALSE(entry.file_specific_info().has_cache_state());
299 316
300 // Unpin a file that doesn't exist in cache and is not pinned. 317 // Unpin a file that doesn't exist in cache and is not pinned.
301 EXPECT_EQ(FILE_ERROR_NOT_FOUND, cache_->Unpin("id_non_existent")); 318 EXPECT_EQ(FILE_ERROR_NOT_FOUND, cache_->Unpin("id_non_existent"));
302 } 319 }
303 320
304 TEST_F(FileCacheTest, MountUnmount) { 321 TEST_F(FileCacheTest, MountUnmount) {
305 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat"); 322 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat");
306 const std::string src_contents = "test"; 323 const std::string src_contents = "test";
307 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, 324 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path,
308 src_contents)); 325 src_contents));
309 std::string id("id_present"); 326 std::string id("id_present");
310 std::string md5(base::MD5String(src_contents)); 327 std::string md5(base::MD5String(src_contents));
311 328
312 // Store a file. 329 // Store a file.
330 ResourceEntry entry;
331 entry.set_local_id(id);
332 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
313 EXPECT_EQ(FILE_ERROR_OK, cache_->Store( 333 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(
314 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY)); 334 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY));
315 335
316 // Mark the file mounted. 336 // Mark the file mounted.
317 base::FilePath cache_file_path; 337 base::FilePath cache_file_path;
318 EXPECT_EQ(FILE_ERROR_OK, cache_->MarkAsMounted(id, &cache_file_path)); 338 EXPECT_EQ(FILE_ERROR_OK, cache_->MarkAsMounted(id, &cache_file_path));
319 339
320 // Try to remove it. 340 // Try to remove it.
321 EXPECT_EQ(FILE_ERROR_IN_USE, cache_->Remove(id)); 341 EXPECT_EQ(FILE_ERROR_IN_USE, cache_->Remove(id));
322 342
323 // Clear mounted state of the file. 343 // Clear mounted state of the file.
324 EXPECT_EQ(FILE_ERROR_OK, cache_->MarkAsUnmounted(cache_file_path)); 344 EXPECT_EQ(FILE_ERROR_OK, cache_->MarkAsUnmounted(cache_file_path));
325 345
326 // Try to remove again. 346 // Try to remove again.
327 EXPECT_EQ(FILE_ERROR_OK, cache_->Remove(id)); 347 EXPECT_EQ(FILE_ERROR_OK, cache_->Remove(id));
328 } 348 }
329 349
330 TEST_F(FileCacheTest, OpenForWrite) { 350 TEST_F(FileCacheTest, OpenForWrite) {
331 // Prepare a file. 351 // Prepare a file.
332 base::FilePath src_file; 352 base::FilePath src_file;
333 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); 353 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file));
334 354
335 const std::string id = "id"; 355 const std::string id = "id";
356 ResourceEntry entry;
357 entry.set_local_id(id);
358 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
336 ASSERT_EQ(FILE_ERROR_OK, cache_->Store(id, "md5", src_file, 359 ASSERT_EQ(FILE_ERROR_OK, cache_->Store(id, "md5", src_file,
337 FileCache::FILE_OPERATION_COPY)); 360 FileCache::FILE_OPERATION_COPY));
338 361
339 // Entry is not dirty nor opened. 362 // Entry is not dirty nor opened.
340 EXPECT_FALSE(cache_->IsOpenedForWrite(id)); 363 EXPECT_FALSE(cache_->IsOpenedForWrite(id));
341 FileCacheEntry entry; 364 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
342 EXPECT_EQ(FILE_ERROR_OK, GetCacheEntry(cache_.get(), id, &entry)); 365 EXPECT_FALSE(entry.file_specific_info().cache_state().is_dirty());
343 EXPECT_FALSE(entry.is_dirty());
344 366
345 // Open (1). 367 // Open (1).
346 scoped_ptr<base::ScopedClosureRunner> file_closer1; 368 scoped_ptr<base::ScopedClosureRunner> file_closer1;
347 EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer1)); 369 EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer1));
348 EXPECT_TRUE(cache_->IsOpenedForWrite(id)); 370 EXPECT_TRUE(cache_->IsOpenedForWrite(id));
349 371
350 // Entry is dirty. 372 // Entry is dirty.
351 EXPECT_EQ(FILE_ERROR_OK, GetCacheEntry(cache_.get(), id, &entry)); 373 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
352 EXPECT_TRUE(entry.is_dirty()); 374 EXPECT_TRUE(entry.file_specific_info().cache_state().is_dirty());
353 375
354 // Open (2). 376 // Open (2).
355 scoped_ptr<base::ScopedClosureRunner> file_closer2; 377 scoped_ptr<base::ScopedClosureRunner> file_closer2;
356 EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer2)); 378 EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer2));
357 EXPECT_TRUE(cache_->IsOpenedForWrite(id)); 379 EXPECT_TRUE(cache_->IsOpenedForWrite(id));
358 380
359 // Close (1). 381 // Close (1).
360 file_closer1.reset(); 382 file_closer1.reset();
361 EXPECT_TRUE(cache_->IsOpenedForWrite(id)); 383 EXPECT_TRUE(cache_->IsOpenedForWrite(id));
362 384
363 // Close (2). 385 // Close (2).
364 file_closer2.reset(); 386 file_closer2.reset();
365 EXPECT_FALSE(cache_->IsOpenedForWrite(id)); 387 EXPECT_FALSE(cache_->IsOpenedForWrite(id));
366 388
367 // Try to open non-existent file. 389 // Try to open non-existent file.
368 EXPECT_EQ(FILE_ERROR_NOT_FOUND, 390 EXPECT_EQ(FILE_ERROR_NOT_FOUND,
369 cache_->OpenForWrite("nonexistent_id", &file_closer1)); 391 cache_->OpenForWrite("nonexistent_id", &file_closer1));
370 } 392 }
371 393
372 TEST_F(FileCacheTest, UpdateMd5) { 394 TEST_F(FileCacheTest, UpdateMd5) {
373 // Store test data. 395 // Store test data.
374 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat"); 396 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat");
375 const std::string contents_before = "before"; 397 const std::string contents_before = "before";
376 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, 398 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path,
377 contents_before)); 399 contents_before));
378 std::string id("id1"); 400 std::string id("id1");
401 ResourceEntry entry;
402 entry.set_local_id(id);
403 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
379 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(id, base::MD5String(contents_before), 404 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(id, base::MD5String(contents_before),
380 src_file_path, 405 src_file_path,
381 FileCache::FILE_OPERATION_COPY)); 406 FileCache::FILE_OPERATION_COPY));
382 407
383 // Modify the cache file. 408 // Modify the cache file.
384 scoped_ptr<base::ScopedClosureRunner> file_closer; 409 scoped_ptr<base::ScopedClosureRunner> file_closer;
385 EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer)); 410 EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer));
386 base::FilePath cache_file_path; 411 base::FilePath cache_file_path;
387 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(id, &cache_file_path)); 412 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(id, &cache_file_path));
388 const std::string contents_after = "after"; 413 const std::string contents_after = "after";
389 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(cache_file_path, 414 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(cache_file_path,
390 contents_after)); 415 contents_after));
391 416
392 // Cannot update MD5 of an opend file. 417 // Cannot update MD5 of an opend file.
393 EXPECT_EQ(FILE_ERROR_IN_USE, cache_->UpdateMd5(id)); 418 EXPECT_EQ(FILE_ERROR_IN_USE, cache_->UpdateMd5(id));
394 419
395 // Close file. 420 // Close file.
396 file_closer.reset(); 421 file_closer.reset();
397 422
398 // MD5 was cleared by OpenForWrite(). 423 // MD5 was cleared by OpenForWrite().
399 FileCacheEntry entry; 424 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
400 EXPECT_EQ(FILE_ERROR_OK, GetCacheEntry(cache_.get(), id, &entry)); 425 EXPECT_TRUE(entry.file_specific_info().cache_state().md5().empty());
401 EXPECT_TRUE(entry.md5().empty());
402 426
403 // Update MD5. 427 // Update MD5.
404 EXPECT_EQ(FILE_ERROR_OK, cache_->UpdateMd5(id)); 428 EXPECT_EQ(FILE_ERROR_OK, cache_->UpdateMd5(id));
405 EXPECT_EQ(FILE_ERROR_OK, GetCacheEntry(cache_.get(), id, &entry)); 429 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
406 EXPECT_EQ(base::MD5String(contents_after), entry.md5()); 430 EXPECT_EQ(base::MD5String(contents_after),
431 entry.file_specific_info().cache_state().md5());
407 } 432 }
408 433
409 TEST_F(FileCacheTest, ClearDirty) { 434 TEST_F(FileCacheTest, ClearDirty) {
410 // Prepare a file. 435 // Prepare a file.
411 base::FilePath src_file; 436 base::FilePath src_file;
412 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); 437 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file));
413 438
414 const std::string id = "id"; 439 const std::string id = "id";
440 ResourceEntry entry;
441 entry.set_local_id(id);
442 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
415 ASSERT_EQ(FILE_ERROR_OK, cache_->Store(id, "md5", src_file, 443 ASSERT_EQ(FILE_ERROR_OK, cache_->Store(id, "md5", src_file,
416 FileCache::FILE_OPERATION_COPY)); 444 FileCache::FILE_OPERATION_COPY));
417 445
418 // Open the file. 446 // Open the file.
419 scoped_ptr<base::ScopedClosureRunner> file_closer; 447 scoped_ptr<base::ScopedClosureRunner> file_closer;
420 EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer)); 448 EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer));
421 449
422 // Entry is dirty. 450 // Entry is dirty.
423 FileCacheEntry entry; 451 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
424 EXPECT_EQ(FILE_ERROR_OK, GetCacheEntry(cache_.get(), id, &entry)); 452 EXPECT_TRUE(entry.file_specific_info().cache_state().is_dirty());
425 EXPECT_TRUE(entry.is_dirty());
426 453
427 // Cannot clear the dirty bit of an opened entry. 454 // Cannot clear the dirty bit of an opened entry.
428 EXPECT_EQ(FILE_ERROR_IN_USE, cache_->ClearDirty(id)); 455 EXPECT_EQ(FILE_ERROR_IN_USE, cache_->ClearDirty(id));
429 456
430 // Close the file and clear the dirty bit. 457 // Close the file and clear the dirty bit.
431 file_closer.reset(); 458 file_closer.reset();
432 EXPECT_EQ(FILE_ERROR_OK, cache_->ClearDirty(id)); 459 EXPECT_EQ(FILE_ERROR_OK, cache_->ClearDirty(id));
433 460
434 // Entry is not dirty. 461 // Entry is not dirty.
435 EXPECT_EQ(FILE_ERROR_OK, GetCacheEntry(cache_.get(), id, &entry)); 462 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
436 EXPECT_FALSE(entry.is_dirty()); 463 EXPECT_FALSE(entry.file_specific_info().cache_state().is_dirty());
437 } 464 }
438 465
439 TEST_F(FileCacheTest, Remove) { 466 TEST_F(FileCacheTest, Remove) {
440 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat"); 467 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat");
441 const std::string src_contents = "test"; 468 const std::string src_contents = "test";
442 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, 469 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path,
443 src_contents)); 470 src_contents));
444 std::string id("id"); 471 std::string id("id");
445 std::string md5(base::MD5String(src_contents)); 472 std::string md5(base::MD5String(src_contents));
446 473
447 // First store a file to cache. 474 // First store a file to cache.
475 ResourceEntry entry;
476 entry.set_local_id(id);
477 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
448 base::FilePath src_file; 478 base::FilePath src_file;
449 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); 479 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file));
450 EXPECT_EQ(FILE_ERROR_OK, cache_->Store( 480 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(
451 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY)); 481 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY));
452 482
453 base::FilePath cache_file_path; 483 base::FilePath cache_file_path;
454 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(id, &cache_file_path)); 484 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(id, &cache_file_path));
455 485
456 // Then try to remove existing file from cache. 486 // Then try to remove existing file from cache.
457 EXPECT_EQ(FILE_ERROR_OK, cache_->Remove(id)); 487 EXPECT_EQ(FILE_ERROR_OK, cache_->Remove(id));
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
495 EXPECT_TRUE(base::ReadFileToString(file_directory.AppendASCII("id_kyu"), 525 EXPECT_TRUE(base::ReadFileToString(file_directory.AppendASCII("id_kyu"),
496 &contents)); 526 &contents));
497 EXPECT_EQ("kyu", contents); 527 EXPECT_EQ("kyu", contents);
498 } 528 }
499 529
500 TEST_F(FileCacheTest, ClearAll) { 530 TEST_F(FileCacheTest, ClearAll) {
501 const std::string id("pdf:1a2b"); 531 const std::string id("pdf:1a2b");
502 const std::string md5("abcdef0123456789"); 532 const std::string md5("abcdef0123456789");
503 533
504 // Store an existing file. 534 // Store an existing file.
535 ResourceEntry entry;
536 entry.set_local_id(id);
537 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
505 base::FilePath src_file; 538 base::FilePath src_file;
506 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); 539 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file));
507 ASSERT_EQ(FILE_ERROR_OK, 540 ASSERT_EQ(FILE_ERROR_OK,
508 cache_->Store(id, md5, src_file, FileCache::FILE_OPERATION_COPY)); 541 cache_->Store(id, md5, src_file, FileCache::FILE_OPERATION_COPY));
509 542
510 // Verify that the cache entry is created.
511 FileCacheEntry cache_entry;
512 ASSERT_EQ(FILE_ERROR_OK, GetCacheEntry(cache_.get(), id, &cache_entry));
513
514 // Clear cache. 543 // Clear cache.
515 EXPECT_TRUE(cache_->ClearAll()); 544 EXPECT_TRUE(cache_->ClearAll());
516 545
517 // Verify that the cache is removed. 546 // Verify that the cache is removed.
518 EXPECT_EQ(FILE_ERROR_NOT_FOUND,
519 GetCacheEntry(cache_.get(), id, &cache_entry));
520 EXPECT_TRUE(base::IsDirectoryEmpty(cache_files_dir_)); 547 EXPECT_TRUE(base::IsDirectoryEmpty(cache_files_dir_));
521 } 548 }
522 549
523 } // namespace internal 550 } // namespace internal
524 } // namespace drive 551 } // namespace drive
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/drive/file_cache.cc ('k') | chrome/browser/chromeos/drive/resource_metadata.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698