[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 831a1a76 | 2010-10-12 05:57:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
thestig | d8df033 | 2014-09-04 06:33:29 | [diff] [blame^] | 5 | #include "base/files/file_util.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 6 | #include "base/files/scoped_temp_dir.h" |
[email protected] | e078590 | 2011-05-19 23:34:17 | [diff] [blame] | 7 | #include "net/disk_cache/cache_util.h" |
[email protected] | 831a1a76 | 2010-10-12 05:57:08 | [diff] [blame] | 8 | #include "testing/gtest/include/gtest/gtest.h" |
| 9 | #include "testing/platform_test.h" |
| 10 | |
| 11 | namespace disk_cache { |
| 12 | |
| 13 | class CacheUtilTest : public PlatformTest { |
| 14 | public: |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 15 | virtual void SetUp() { |
[email protected] | 831a1a76 | 2010-10-12 05:57:08 | [diff] [blame] | 16 | PlatformTest::SetUp(); |
| 17 | ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir()); |
| 18 | cache_dir_ = tmp_dir_.path().Append(FILE_PATH_LITERAL("Cache")); |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 19 | file1_ = base::FilePath(cache_dir_.Append(FILE_PATH_LITERAL("file01"))); |
| 20 | file2_ = base::FilePath(cache_dir_.Append(FILE_PATH_LITERAL(".file02"))); |
| 21 | dir1_ = base::FilePath(cache_dir_.Append(FILE_PATH_LITERAL("dir01"))); |
[email protected] | 76ed979ee | 2013-07-17 15:50:45 | [diff] [blame] | 22 | file3_ = base::FilePath(dir1_.Append(FILE_PATH_LITERAL("file03"))); |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 23 | ASSERT_TRUE(base::CreateDirectory(cache_dir_)); |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 24 | FILE *fp = base::OpenFile(file1_, "w"); |
[email protected] | 831a1a76 | 2010-10-12 05:57:08 | [diff] [blame] | 25 | ASSERT_TRUE(fp != NULL); |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 26 | base::CloseFile(fp); |
| 27 | fp = base::OpenFile(file2_, "w"); |
[email protected] | 831a1a76 | 2010-10-12 05:57:08 | [diff] [blame] | 28 | ASSERT_TRUE(fp != NULL); |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 29 | base::CloseFile(fp); |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 30 | ASSERT_TRUE(base::CreateDirectory(dir1_)); |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 31 | fp = base::OpenFile(file3_, "w"); |
[email protected] | 76ed979ee | 2013-07-17 15:50:45 | [diff] [blame] | 32 | ASSERT_TRUE(fp != NULL); |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 33 | base::CloseFile(fp); |
[email protected] | 831a1a76 | 2010-10-12 05:57:08 | [diff] [blame] | 34 | dest_dir_ = tmp_dir_.path().Append(FILE_PATH_LITERAL("old_Cache_001")); |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 35 | dest_file1_ = base::FilePath(dest_dir_.Append(FILE_PATH_LITERAL("file01"))); |
| 36 | dest_file2_ = |
| 37 | base::FilePath(dest_dir_.Append(FILE_PATH_LITERAL(".file02"))); |
| 38 | dest_dir1_ = base::FilePath(dest_dir_.Append(FILE_PATH_LITERAL("dir01"))); |
[email protected] | 831a1a76 | 2010-10-12 05:57:08 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | protected: |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 42 | base::ScopedTempDir tmp_dir_; |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 43 | base::FilePath cache_dir_; |
| 44 | base::FilePath file1_; |
| 45 | base::FilePath file2_; |
| 46 | base::FilePath dir1_; |
[email protected] | 76ed979ee | 2013-07-17 15:50:45 | [diff] [blame] | 47 | base::FilePath file3_; |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 48 | base::FilePath dest_dir_; |
| 49 | base::FilePath dest_file1_; |
| 50 | base::FilePath dest_file2_; |
| 51 | base::FilePath dest_dir1_; |
[email protected] | 831a1a76 | 2010-10-12 05:57:08 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | TEST_F(CacheUtilTest, MoveCache) { |
| 55 | EXPECT_TRUE(disk_cache::MoveCache(cache_dir_, dest_dir_)); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 56 | EXPECT_TRUE(base::PathExists(dest_dir_)); |
| 57 | EXPECT_TRUE(base::PathExists(dest_file1_)); |
| 58 | EXPECT_TRUE(base::PathExists(dest_file2_)); |
| 59 | EXPECT_TRUE(base::PathExists(dest_dir1_)); |
[email protected] | 831a1a76 | 2010-10-12 05:57:08 | [diff] [blame] | 60 | #if defined(OS_CHROMEOS) |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 61 | EXPECT_TRUE(base::PathExists(cache_dir_)); // old cache dir stays |
[email protected] | 831a1a76 | 2010-10-12 05:57:08 | [diff] [blame] | 62 | #else |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 63 | EXPECT_FALSE(base::PathExists(cache_dir_)); // old cache is gone |
[email protected] | 831a1a76 | 2010-10-12 05:57:08 | [diff] [blame] | 64 | #endif |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 65 | EXPECT_FALSE(base::PathExists(file1_)); |
| 66 | EXPECT_FALSE(base::PathExists(file2_)); |
| 67 | EXPECT_FALSE(base::PathExists(dir1_)); |
[email protected] | 831a1a76 | 2010-10-12 05:57:08 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | TEST_F(CacheUtilTest, DeleteCache) { |
[email protected] | 831a1a76 | 2010-10-12 05:57:08 | [diff] [blame] | 71 | disk_cache::DeleteCache(cache_dir_, false); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 72 | EXPECT_TRUE(base::PathExists(cache_dir_)); // cache dir stays |
[email protected] | 76ed979ee | 2013-07-17 15:50:45 | [diff] [blame] | 73 | EXPECT_FALSE(base::PathExists(dir1_)); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 74 | EXPECT_FALSE(base::PathExists(file1_)); |
| 75 | EXPECT_FALSE(base::PathExists(file2_)); |
[email protected] | 76ed979ee | 2013-07-17 15:50:45 | [diff] [blame] | 76 | EXPECT_FALSE(base::PathExists(file3_)); |
[email protected] | 831a1a76 | 2010-10-12 05:57:08 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | TEST_F(CacheUtilTest, DeleteCacheAndDir) { |
[email protected] | 831a1a76 | 2010-10-12 05:57:08 | [diff] [blame] | 80 | disk_cache::DeleteCache(cache_dir_, true); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 81 | EXPECT_FALSE(base::PathExists(cache_dir_)); // cache dir is gone |
[email protected] | 76ed979ee | 2013-07-17 15:50:45 | [diff] [blame] | 82 | EXPECT_FALSE(base::PathExists(dir1_)); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 83 | EXPECT_FALSE(base::PathExists(file1_)); |
| 84 | EXPECT_FALSE(base::PathExists(file2_)); |
[email protected] | 76ed979ee | 2013-07-17 15:50:45 | [diff] [blame] | 85 | EXPECT_FALSE(base::PathExists(file3_)); |
[email protected] | 831a1a76 | 2010-10-12 05:57:08 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | TEST_F(CacheUtilTest, DeleteCacheFile) { |
| 89 | EXPECT_TRUE(disk_cache::DeleteCacheFile(file1_)); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 90 | EXPECT_FALSE(base::PathExists(file1_)); |
| 91 | EXPECT_TRUE(base::PathExists(cache_dir_)); // cache dir stays |
[email protected] | 76ed979ee | 2013-07-17 15:50:45 | [diff] [blame] | 92 | EXPECT_TRUE(base::PathExists(dir1_)); |
| 93 | EXPECT_TRUE(base::PathExists(file3_)); |
[email protected] | 831a1a76 | 2010-10-12 05:57:08 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | } // namespace disk_cache |