1 // Copyright (c) 2011 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 "net/disk_cache/cache_util.h"
6 #include "base/file_util.h"
7 #include "base/memory/scoped_temp_dir.h"
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:
SetUp()15 void SetUp() {
16 PlatformTest::SetUp();
17 ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir());
18 cache_dir_ = tmp_dir_.path().Append(FILE_PATH_LITERAL("Cache"));
19 file1_ = FilePath(cache_dir_.Append(FILE_PATH_LITERAL("file01")));
20 file2_ = FilePath(cache_dir_.Append(FILE_PATH_LITERAL(".file02")));
21 dir1_ = FilePath(cache_dir_.Append(FILE_PATH_LITERAL("dir01")));
22 ASSERT_TRUE(file_util::CreateDirectory(cache_dir_));
23 FILE *fp = file_util::OpenFile(file1_, "w");
24 ASSERT_TRUE(fp != NULL);
25 file_util::CloseFile(fp);
26 fp = file_util::OpenFile(file2_, "w");
27 ASSERT_TRUE(fp != NULL);
28 file_util::CloseFile(fp);
29 ASSERT_TRUE(file_util::CreateDirectory(dir1_));
30 dest_dir_ = tmp_dir_.path().Append(FILE_PATH_LITERAL("old_Cache_001"));
31 dest_file1_ = FilePath(dest_dir_.Append(FILE_PATH_LITERAL("file01")));
32 dest_file2_ = FilePath(dest_dir_.Append(FILE_PATH_LITERAL(".file02")));
33 dest_dir1_ = FilePath(dest_dir_.Append(FILE_PATH_LITERAL("dir01")));
34 }
35
36 protected:
37 ScopedTempDir tmp_dir_;
38 FilePath cache_dir_;
39 FilePath file1_;
40 FilePath file2_;
41 FilePath dir1_;
42 FilePath dest_dir_;
43 FilePath dest_file1_;
44 FilePath dest_file2_;
45 FilePath dest_dir1_;
46 };
47
TEST_F(CacheUtilTest,MoveCache)48 TEST_F(CacheUtilTest, MoveCache) {
49 EXPECT_TRUE(disk_cache::MoveCache(cache_dir_, dest_dir_));
50 EXPECT_TRUE(file_util::PathExists(dest_dir_));
51 EXPECT_TRUE(file_util::PathExists(dest_file1_));
52 EXPECT_TRUE(file_util::PathExists(dest_file2_));
53 EXPECT_TRUE(file_util::PathExists(dest_dir1_));
54 #if defined(OS_CHROMEOS)
55 EXPECT_TRUE(file_util::PathExists(cache_dir_)); // old cache dir stays
56 #else
57 EXPECT_FALSE(file_util::PathExists(cache_dir_)); // old cache is gone
58 #endif
59 EXPECT_FALSE(file_util::PathExists(file1_));
60 EXPECT_FALSE(file_util::PathExists(file2_));
61 EXPECT_FALSE(file_util::PathExists(dir1_));
62 }
63
TEST_F(CacheUtilTest,DeleteCache)64 TEST_F(CacheUtilTest, DeleteCache) {
65 // DeleteCache won't delete subdirs, so let's not start with this
66 // one around.
67 file_util::Delete(dir1_, false);
68 disk_cache::DeleteCache(cache_dir_, false);
69 EXPECT_TRUE(file_util::PathExists(cache_dir_)); // cache dir stays
70 EXPECT_FALSE(file_util::PathExists(file1_));
71 EXPECT_FALSE(file_util::PathExists(file2_));
72 }
73
TEST_F(CacheUtilTest,DeleteCacheAndDir)74 TEST_F(CacheUtilTest, DeleteCacheAndDir) {
75 // DeleteCache won't delete subdirs, so let's not start with this
76 // one around.
77 file_util::Delete(dir1_, false);
78 disk_cache::DeleteCache(cache_dir_, true);
79 EXPECT_FALSE(file_util::PathExists(cache_dir_)); // cache dir is gone
80 EXPECT_FALSE(file_util::PathExists(file1_));
81 EXPECT_FALSE(file_util::PathExists(file2_));
82 }
83
TEST_F(CacheUtilTest,DeleteCacheFile)84 TEST_F(CacheUtilTest, DeleteCacheFile) {
85 EXPECT_TRUE(disk_cache::DeleteCacheFile(file1_));
86 EXPECT_FALSE(file_util::PathExists(file1_));
87 EXPECT_TRUE(file_util::PathExists(cache_dir_)); // cache dir stays
88 }
89
90 } // namespace disk_cache
91