OLD | NEW |
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 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 ASSERT_EQ(FILE_ERROR_OK, cache_->GetFile(id_pinned, &pinned_path)); | 166 ASSERT_EQ(FILE_ERROR_OK, cache_->GetFile(id_pinned, &pinned_path)); |
167 | 167 |
168 // Call FreeDiskSpaceIfNeededFor(). | 168 // Call FreeDiskSpaceIfNeededFor(). |
169 fake_free_disk_space_getter_->set_default_value(test_util::kLotsOfSpace); | 169 fake_free_disk_space_getter_->set_default_value(test_util::kLotsOfSpace); |
170 fake_free_disk_space_getter_->PushFakeValue(0); | 170 fake_free_disk_space_getter_->PushFakeValue(0); |
171 const int64 kNeededBytes = 1; | 171 const int64 kNeededBytes = 1; |
172 EXPECT_TRUE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); | 172 EXPECT_TRUE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); |
173 | 173 |
174 // Only 'temporary' file gets removed. | 174 // Only 'temporary' file gets removed. |
175 FileCacheEntry entry; | 175 FileCacheEntry entry; |
176 EXPECT_FALSE(cache_->GetCacheEntry(id_tmp, &entry)); | 176 EXPECT_EQ(FILE_ERROR_NOT_FOUND, cache_->GetCacheEntry(id_tmp, &entry)); |
177 EXPECT_FALSE(base::PathExists(tmp_path)); | 177 EXPECT_FALSE(base::PathExists(tmp_path)); |
178 | 178 |
179 EXPECT_TRUE(cache_->GetCacheEntry(id_pinned, &entry)); | 179 EXPECT_EQ(FILE_ERROR_OK, cache_->GetCacheEntry(id_pinned, &entry)); |
180 EXPECT_TRUE(base::PathExists(pinned_path)); | 180 EXPECT_TRUE(base::PathExists(pinned_path)); |
181 | 181 |
182 // Returns false when disk space cannot be freed. | 182 // Returns false when disk space cannot be freed. |
183 fake_free_disk_space_getter_->set_default_value(0); | 183 fake_free_disk_space_getter_->set_default_value(0); |
184 EXPECT_FALSE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); | 184 EXPECT_FALSE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); |
185 } | 185 } |
186 | 186 |
187 TEST_F(FileCacheTest, GetFile) { | 187 TEST_F(FileCacheTest, GetFile) { |
188 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat"); | 188 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat"); |
189 const std::string src_contents = "test"; | 189 const std::string src_contents = "test"; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, | 238 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, |
239 src_contents)); | 239 src_contents)); |
240 std::string id("id"); | 240 std::string id("id"); |
241 std::string md5(base::MD5String(src_contents)); | 241 std::string md5(base::MD5String(src_contents)); |
242 | 242 |
243 // Store a file. | 243 // Store a file. |
244 EXPECT_EQ(FILE_ERROR_OK, cache_->Store( | 244 EXPECT_EQ(FILE_ERROR_OK, cache_->Store( |
245 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY)); | 245 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY)); |
246 | 246 |
247 FileCacheEntry cache_entry; | 247 FileCacheEntry cache_entry; |
248 EXPECT_TRUE(cache_->GetCacheEntry(id, &cache_entry)); | 248 EXPECT_EQ(FILE_ERROR_OK, cache_->GetCacheEntry(id, &cache_entry)); |
249 EXPECT_TRUE(cache_entry.is_present()); | 249 EXPECT_TRUE(cache_entry.is_present()); |
250 EXPECT_EQ(md5, cache_entry.md5()); | 250 EXPECT_EQ(md5, cache_entry.md5()); |
251 | 251 |
252 base::FilePath cache_file_path; | 252 base::FilePath cache_file_path; |
253 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(id, &cache_file_path)); | 253 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(id, &cache_file_path)); |
254 EXPECT_TRUE(base::ContentsEqual(src_file_path, cache_file_path)); | 254 EXPECT_TRUE(base::ContentsEqual(src_file_path, cache_file_path)); |
255 | 255 |
256 // Store a non-existent file. | 256 // Store a non-existent file. |
257 EXPECT_EQ(FILE_ERROR_FAILED, cache_->Store( | 257 EXPECT_EQ(FILE_ERROR_FAILED, cache_->Store( |
258 id, md5, base::FilePath::FromUTF8Unsafe("non_existent_file"), | 258 id, md5, base::FilePath::FromUTF8Unsafe("non_existent_file"), |
259 FileCache::FILE_OPERATION_COPY)); | 259 FileCache::FILE_OPERATION_COPY)); |
260 | 260 |
261 // Passing empty MD5 marks the entry as dirty. | 261 // Passing empty MD5 marks the entry as dirty. |
262 EXPECT_EQ(FILE_ERROR_OK, cache_->Store( | 262 EXPECT_EQ(FILE_ERROR_OK, cache_->Store( |
263 id, std::string(), src_file_path, FileCache::FILE_OPERATION_COPY)); | 263 id, std::string(), src_file_path, FileCache::FILE_OPERATION_COPY)); |
264 | 264 |
265 EXPECT_TRUE(cache_->GetCacheEntry(id, &cache_entry)); | 265 EXPECT_EQ(FILE_ERROR_OK, cache_->GetCacheEntry(id, &cache_entry)); |
266 EXPECT_TRUE(cache_entry.is_present()); | 266 EXPECT_TRUE(cache_entry.is_present()); |
267 EXPECT_TRUE(cache_entry.md5().empty()); | 267 EXPECT_TRUE(cache_entry.md5().empty()); |
268 EXPECT_TRUE(cache_entry.is_dirty()); | 268 EXPECT_TRUE(cache_entry.is_dirty()); |
269 | 269 |
270 // No free space available. | 270 // No free space available. |
271 fake_free_disk_space_getter_->set_default_value(0); | 271 fake_free_disk_space_getter_->set_default_value(0); |
272 | 272 |
273 EXPECT_EQ(FILE_ERROR_NO_LOCAL_SPACE, cache_->Store( | 273 EXPECT_EQ(FILE_ERROR_NO_LOCAL_SPACE, cache_->Store( |
274 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY)); | 274 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY)); |
275 } | 275 } |
276 | 276 |
277 TEST_F(FileCacheTest, PinAndUnpin) { | 277 TEST_F(FileCacheTest, PinAndUnpin) { |
278 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat"); | 278 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat"); |
279 const std::string src_contents = "test"; | 279 const std::string src_contents = "test"; |
280 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, | 280 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, |
281 src_contents)); | 281 src_contents)); |
282 std::string id("id_present"); | 282 std::string id("id_present"); |
283 std::string md5(base::MD5String(src_contents)); | 283 std::string md5(base::MD5String(src_contents)); |
284 | 284 |
285 // Store a file. | 285 // Store a file. |
286 EXPECT_EQ(FILE_ERROR_OK, cache_->Store( | 286 EXPECT_EQ(FILE_ERROR_OK, cache_->Store( |
287 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY)); | 287 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY)); |
288 | 288 |
289 FileCacheEntry cache_entry; | 289 FileCacheEntry cache_entry; |
290 EXPECT_TRUE(cache_->GetCacheEntry(id, &cache_entry)); | 290 EXPECT_EQ(FILE_ERROR_OK, cache_->GetCacheEntry(id, &cache_entry)); |
291 EXPECT_FALSE(cache_entry.is_pinned()); | 291 EXPECT_FALSE(cache_entry.is_pinned()); |
292 | 292 |
293 // Pin the existing file. | 293 // Pin the existing file. |
294 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(id)); | 294 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(id)); |
295 | 295 |
296 EXPECT_TRUE(cache_->GetCacheEntry(id, &cache_entry)); | 296 EXPECT_EQ(FILE_ERROR_OK, cache_->GetCacheEntry(id, &cache_entry)); |
297 EXPECT_TRUE(cache_entry.is_pinned()); | 297 EXPECT_TRUE(cache_entry.is_pinned()); |
298 | 298 |
299 // Unpin the file. | 299 // Unpin the file. |
300 EXPECT_EQ(FILE_ERROR_OK, cache_->Unpin(id)); | 300 EXPECT_EQ(FILE_ERROR_OK, cache_->Unpin(id)); |
301 | 301 |
302 EXPECT_TRUE(cache_->GetCacheEntry(id, &cache_entry)); | 302 EXPECT_EQ(FILE_ERROR_OK, cache_->GetCacheEntry(id, &cache_entry)); |
303 EXPECT_FALSE(cache_entry.is_pinned()); | 303 EXPECT_FALSE(cache_entry.is_pinned()); |
304 | 304 |
305 // Pin a non-present file. | 305 // Pin a non-present file. |
306 std::string id_non_present = "id_non_present"; | 306 std::string id_non_present = "id_non_present"; |
307 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(id_non_present)); | 307 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(id_non_present)); |
308 | 308 |
309 EXPECT_TRUE(cache_->GetCacheEntry(id_non_present, &cache_entry)); | 309 EXPECT_EQ(FILE_ERROR_OK, cache_->GetCacheEntry(id_non_present, &cache_entry)); |
310 EXPECT_TRUE(cache_entry.is_pinned()); | 310 EXPECT_TRUE(cache_entry.is_pinned()); |
311 | 311 |
312 // Unpin the previously pinned non-existent file. | 312 // Unpin the previously pinned non-existent file. |
313 EXPECT_EQ(FILE_ERROR_OK, cache_->Unpin(id_non_present)); | 313 EXPECT_EQ(FILE_ERROR_OK, cache_->Unpin(id_non_present)); |
314 | 314 |
315 EXPECT_FALSE(cache_->GetCacheEntry(id_non_present, &cache_entry)); | 315 EXPECT_EQ(FILE_ERROR_NOT_FOUND, |
| 316 cache_->GetCacheEntry(id_non_present, &cache_entry)); |
316 | 317 |
317 // Unpin a file that doesn't exist in cache and is not pinned. | 318 // Unpin a file that doesn't exist in cache and is not pinned. |
318 EXPECT_EQ(FILE_ERROR_NOT_FOUND, cache_->Unpin("id_non_existent")); | 319 EXPECT_EQ(FILE_ERROR_NOT_FOUND, cache_->Unpin("id_non_existent")); |
319 } | 320 } |
320 | 321 |
321 TEST_F(FileCacheTest, MountUnmount) { | 322 TEST_F(FileCacheTest, MountUnmount) { |
322 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat"); | 323 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat"); |
323 const std::string src_contents = "test"; | 324 const std::string src_contents = "test"; |
324 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, | 325 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, |
325 src_contents)); | 326 src_contents)); |
(...skipping 23 matching lines...) Expand all Loading... |
349 base::FilePath src_file; | 350 base::FilePath src_file; |
350 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); | 351 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); |
351 | 352 |
352 const std::string id = "id"; | 353 const std::string id = "id"; |
353 ASSERT_EQ(FILE_ERROR_OK, cache_->Store(id, "md5", src_file, | 354 ASSERT_EQ(FILE_ERROR_OK, cache_->Store(id, "md5", src_file, |
354 FileCache::FILE_OPERATION_COPY)); | 355 FileCache::FILE_OPERATION_COPY)); |
355 | 356 |
356 // Entry is not dirty nor opened. | 357 // Entry is not dirty nor opened. |
357 EXPECT_FALSE(cache_->IsOpenedForWrite(id)); | 358 EXPECT_FALSE(cache_->IsOpenedForWrite(id)); |
358 FileCacheEntry entry; | 359 FileCacheEntry entry; |
359 EXPECT_TRUE(cache_->GetCacheEntry(id, &entry)); | 360 EXPECT_EQ(FILE_ERROR_OK, cache_->GetCacheEntry(id, &entry)); |
360 EXPECT_FALSE(entry.is_dirty()); | 361 EXPECT_FALSE(entry.is_dirty()); |
361 | 362 |
362 // Open (1). | 363 // Open (1). |
363 scoped_ptr<base::ScopedClosureRunner> file_closer1; | 364 scoped_ptr<base::ScopedClosureRunner> file_closer1; |
364 EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer1)); | 365 EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer1)); |
365 EXPECT_TRUE(cache_->IsOpenedForWrite(id)); | 366 EXPECT_TRUE(cache_->IsOpenedForWrite(id)); |
366 | 367 |
367 // Entry is dirty. | 368 // Entry is dirty. |
368 EXPECT_TRUE(cache_->GetCacheEntry(id, &entry)); | 369 EXPECT_EQ(FILE_ERROR_OK, cache_->GetCacheEntry(id, &entry)); |
369 EXPECT_TRUE(entry.is_dirty()); | 370 EXPECT_TRUE(entry.is_dirty()); |
370 | 371 |
371 // Open (2). | 372 // Open (2). |
372 scoped_ptr<base::ScopedClosureRunner> file_closer2; | 373 scoped_ptr<base::ScopedClosureRunner> file_closer2; |
373 EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer2)); | 374 EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer2)); |
374 EXPECT_TRUE(cache_->IsOpenedForWrite(id)); | 375 EXPECT_TRUE(cache_->IsOpenedForWrite(id)); |
375 | 376 |
376 // Close (1). | 377 // Close (1). |
377 file_closer1.reset(); | 378 file_closer1.reset(); |
378 EXPECT_TRUE(cache_->IsOpenedForWrite(id)); | 379 EXPECT_TRUE(cache_->IsOpenedForWrite(id)); |
(...skipping 28 matching lines...) Expand all Loading... |
407 contents_after)); | 408 contents_after)); |
408 | 409 |
409 // Cannot update MD5 of an opend file. | 410 // Cannot update MD5 of an opend file. |
410 EXPECT_EQ(FILE_ERROR_IN_USE, cache_->UpdateMd5(id)); | 411 EXPECT_EQ(FILE_ERROR_IN_USE, cache_->UpdateMd5(id)); |
411 | 412 |
412 // Close file. | 413 // Close file. |
413 file_closer.reset(); | 414 file_closer.reset(); |
414 | 415 |
415 // MD5 was cleared by OpenForWrite(). | 416 // MD5 was cleared by OpenForWrite(). |
416 FileCacheEntry entry; | 417 FileCacheEntry entry; |
417 EXPECT_TRUE(cache_->GetCacheEntry(id, &entry)); | 418 EXPECT_EQ(FILE_ERROR_OK, cache_->GetCacheEntry(id, &entry)); |
418 EXPECT_TRUE(entry.md5().empty()); | 419 EXPECT_TRUE(entry.md5().empty()); |
419 | 420 |
420 // Update MD5. | 421 // Update MD5. |
421 EXPECT_EQ(FILE_ERROR_OK, cache_->UpdateMd5(id)); | 422 EXPECT_EQ(FILE_ERROR_OK, cache_->UpdateMd5(id)); |
422 EXPECT_TRUE(cache_->GetCacheEntry(id, &entry)); | 423 EXPECT_EQ(FILE_ERROR_OK, cache_->GetCacheEntry(id, &entry)); |
423 EXPECT_EQ(base::MD5String(contents_after), entry.md5()); | 424 EXPECT_EQ(base::MD5String(contents_after), entry.md5()); |
424 } | 425 } |
425 | 426 |
426 TEST_F(FileCacheTest, ClearDirty) { | 427 TEST_F(FileCacheTest, ClearDirty) { |
427 // Prepare a file. | 428 // Prepare a file. |
428 base::FilePath src_file; | 429 base::FilePath src_file; |
429 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); | 430 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); |
430 | 431 |
431 const std::string id = "id"; | 432 const std::string id = "id"; |
432 ASSERT_EQ(FILE_ERROR_OK, cache_->Store(id, "md5", src_file, | 433 ASSERT_EQ(FILE_ERROR_OK, cache_->Store(id, "md5", src_file, |
433 FileCache::FILE_OPERATION_COPY)); | 434 FileCache::FILE_OPERATION_COPY)); |
434 | 435 |
435 // Open the file. | 436 // Open the file. |
436 scoped_ptr<base::ScopedClosureRunner> file_closer; | 437 scoped_ptr<base::ScopedClosureRunner> file_closer; |
437 EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer)); | 438 EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer)); |
438 | 439 |
439 // Entry is dirty. | 440 // Entry is dirty. |
440 FileCacheEntry entry; | 441 FileCacheEntry entry; |
441 EXPECT_TRUE(cache_->GetCacheEntry(id, &entry)); | 442 EXPECT_EQ(FILE_ERROR_OK, cache_->GetCacheEntry(id, &entry)); |
442 EXPECT_TRUE(entry.is_dirty()); | 443 EXPECT_TRUE(entry.is_dirty()); |
443 | 444 |
444 // Cannot clear the dirty bit of an opened entry. | 445 // Cannot clear the dirty bit of an opened entry. |
445 EXPECT_EQ(FILE_ERROR_IN_USE, cache_->ClearDirty(id)); | 446 EXPECT_EQ(FILE_ERROR_IN_USE, cache_->ClearDirty(id)); |
446 | 447 |
447 // Close the file and clear the dirty bit. | 448 // Close the file and clear the dirty bit. |
448 file_closer.reset(); | 449 file_closer.reset(); |
449 EXPECT_EQ(FILE_ERROR_OK, cache_->ClearDirty(id)); | 450 EXPECT_EQ(FILE_ERROR_OK, cache_->ClearDirty(id)); |
450 | 451 |
451 // Entry is not dirty. | 452 // Entry is not dirty. |
452 EXPECT_TRUE(cache_->GetCacheEntry(id, &entry)); | 453 EXPECT_EQ(FILE_ERROR_OK, cache_->GetCacheEntry(id, &entry)); |
453 EXPECT_FALSE(entry.is_dirty()); | 454 EXPECT_FALSE(entry.is_dirty()); |
454 } | 455 } |
455 | 456 |
456 TEST_F(FileCacheTest, Remove) { | 457 TEST_F(FileCacheTest, Remove) { |
457 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat"); | 458 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat"); |
458 const std::string src_contents = "test"; | 459 const std::string src_contents = "test"; |
459 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, | 460 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, |
460 src_contents)); | 461 src_contents)); |
461 std::string id("id"); | 462 std::string id("id"); |
462 std::string md5(base::MD5String(src_contents)); | 463 std::string md5(base::MD5String(src_contents)); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 const std::string md5("abcdef0123456789"); | 520 const std::string md5("abcdef0123456789"); |
520 | 521 |
521 // Store an existing file. | 522 // Store an existing file. |
522 base::FilePath src_file; | 523 base::FilePath src_file; |
523 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); | 524 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); |
524 ASSERT_EQ(FILE_ERROR_OK, | 525 ASSERT_EQ(FILE_ERROR_OK, |
525 cache_->Store(id, md5, src_file, FileCache::FILE_OPERATION_COPY)); | 526 cache_->Store(id, md5, src_file, FileCache::FILE_OPERATION_COPY)); |
526 | 527 |
527 // Verify that the cache entry is created. | 528 // Verify that the cache entry is created. |
528 FileCacheEntry cache_entry; | 529 FileCacheEntry cache_entry; |
529 ASSERT_TRUE(cache_->GetCacheEntry(id, &cache_entry)); | 530 ASSERT_EQ(FILE_ERROR_OK, cache_->GetCacheEntry(id, &cache_entry)); |
530 | 531 |
531 // Clear cache. | 532 // Clear cache. |
532 EXPECT_TRUE(cache_->ClearAll()); | 533 EXPECT_TRUE(cache_->ClearAll()); |
533 | 534 |
534 // Verify that the cache is removed. | 535 // Verify that the cache is removed. |
535 EXPECT_FALSE(cache_->GetCacheEntry(id, &cache_entry)); | 536 EXPECT_EQ(FILE_ERROR_NOT_FOUND, cache_->GetCacheEntry(id, &cache_entry)); |
536 EXPECT_TRUE(base::IsDirectoryEmpty(cache_files_dir_)); | 537 EXPECT_TRUE(base::IsDirectoryEmpty(cache_files_dir_)); |
537 } | 538 } |
538 | 539 |
539 } // namespace internal | 540 } // namespace internal |
540 } // namespace drive | 541 } // namespace drive |
OLD | NEW |