blob: b7558155f5fc5e6024cafeb5e7a668b9f209a505 [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]831a1a762010-10-12 05:57:082// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
thestigd8df0332014-09-04 06:33:295#include "base/files/file_util.h"
[email protected]ea1a3f62012-11-16 20:34:236#include "base/files/scoped_temp_dir.h"
[email protected]e0785902011-05-19 23:34:177#include "net/disk_cache/cache_util.h"
[email protected]831a1a762010-10-12 05:57:088#include "testing/gtest/include/gtest/gtest.h"
9#include "testing/platform_test.h"
10
11namespace disk_cache {
12
13class CacheUtilTest : public PlatformTest {
14 public:
[email protected]46fadfd2013-02-06 09:40:1615 virtual void SetUp() {
[email protected]831a1a762010-10-12 05:57:0816 PlatformTest::SetUp();
17 ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir());
18 cache_dir_ = tmp_dir_.path().Append(FILE_PATH_LITERAL("Cache"));
[email protected]6cdfd7f2013-02-08 20:40:1519 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]76ed979ee2013-07-17 15:50:4522 file3_ = base::FilePath(dir1_.Append(FILE_PATH_LITERAL("file03")));
[email protected]426d1c92013-12-03 20:08:5423 ASSERT_TRUE(base::CreateDirectory(cache_dir_));
[email protected]7600d0b2013-12-08 21:43:3024 FILE *fp = base::OpenFile(file1_, "w");
[email protected]831a1a762010-10-12 05:57:0825 ASSERT_TRUE(fp != NULL);
[email protected]7600d0b2013-12-08 21:43:3026 base::CloseFile(fp);
27 fp = base::OpenFile(file2_, "w");
[email protected]831a1a762010-10-12 05:57:0828 ASSERT_TRUE(fp != NULL);
[email protected]7600d0b2013-12-08 21:43:3029 base::CloseFile(fp);
[email protected]426d1c92013-12-03 20:08:5430 ASSERT_TRUE(base::CreateDirectory(dir1_));
[email protected]7600d0b2013-12-08 21:43:3031 fp = base::OpenFile(file3_, "w");
[email protected]76ed979ee2013-07-17 15:50:4532 ASSERT_TRUE(fp != NULL);
[email protected]7600d0b2013-12-08 21:43:3033 base::CloseFile(fp);
[email protected]831a1a762010-10-12 05:57:0834 dest_dir_ = tmp_dir_.path().Append(FILE_PATH_LITERAL("old_Cache_001"));
[email protected]6cdfd7f2013-02-08 20:40:1535 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]831a1a762010-10-12 05:57:0839 }
40
41 protected:
[email protected]ea1a3f62012-11-16 20:34:2342 base::ScopedTempDir tmp_dir_;
[email protected]6cdfd7f2013-02-08 20:40:1543 base::FilePath cache_dir_;
44 base::FilePath file1_;
45 base::FilePath file2_;
46 base::FilePath dir1_;
[email protected]76ed979ee2013-07-17 15:50:4547 base::FilePath file3_;
[email protected]6cdfd7f2013-02-08 20:40:1548 base::FilePath dest_dir_;
49 base::FilePath dest_file1_;
50 base::FilePath dest_file2_;
51 base::FilePath dest_dir1_;
[email protected]831a1a762010-10-12 05:57:0852};
53
54TEST_F(CacheUtilTest, MoveCache) {
55 EXPECT_TRUE(disk_cache::MoveCache(cache_dir_, dest_dir_));
[email protected]7567484142013-07-11 17:36:0756 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]831a1a762010-10-12 05:57:0860#if defined(OS_CHROMEOS)
[email protected]7567484142013-07-11 17:36:0761 EXPECT_TRUE(base::PathExists(cache_dir_)); // old cache dir stays
[email protected]831a1a762010-10-12 05:57:0862#else
[email protected]7567484142013-07-11 17:36:0763 EXPECT_FALSE(base::PathExists(cache_dir_)); // old cache is gone
[email protected]831a1a762010-10-12 05:57:0864#endif
[email protected]7567484142013-07-11 17:36:0765 EXPECT_FALSE(base::PathExists(file1_));
66 EXPECT_FALSE(base::PathExists(file2_));
67 EXPECT_FALSE(base::PathExists(dir1_));
[email protected]831a1a762010-10-12 05:57:0868}
69
70TEST_F(CacheUtilTest, DeleteCache) {
[email protected]831a1a762010-10-12 05:57:0871 disk_cache::DeleteCache(cache_dir_, false);
[email protected]7567484142013-07-11 17:36:0772 EXPECT_TRUE(base::PathExists(cache_dir_)); // cache dir stays
[email protected]76ed979ee2013-07-17 15:50:4573 EXPECT_FALSE(base::PathExists(dir1_));
[email protected]7567484142013-07-11 17:36:0774 EXPECT_FALSE(base::PathExists(file1_));
75 EXPECT_FALSE(base::PathExists(file2_));
[email protected]76ed979ee2013-07-17 15:50:4576 EXPECT_FALSE(base::PathExists(file3_));
[email protected]831a1a762010-10-12 05:57:0877}
78
79TEST_F(CacheUtilTest, DeleteCacheAndDir) {
[email protected]831a1a762010-10-12 05:57:0880 disk_cache::DeleteCache(cache_dir_, true);
[email protected]7567484142013-07-11 17:36:0781 EXPECT_FALSE(base::PathExists(cache_dir_)); // cache dir is gone
[email protected]76ed979ee2013-07-17 15:50:4582 EXPECT_FALSE(base::PathExists(dir1_));
[email protected]7567484142013-07-11 17:36:0783 EXPECT_FALSE(base::PathExists(file1_));
84 EXPECT_FALSE(base::PathExists(file2_));
[email protected]76ed979ee2013-07-17 15:50:4585 EXPECT_FALSE(base::PathExists(file3_));
[email protected]831a1a762010-10-12 05:57:0886}
87
88TEST_F(CacheUtilTest, DeleteCacheFile) {
89 EXPECT_TRUE(disk_cache::DeleteCacheFile(file1_));
[email protected]7567484142013-07-11 17:36:0790 EXPECT_FALSE(base::PathExists(file1_));
91 EXPECT_TRUE(base::PathExists(cache_dir_)); // cache dir stays
[email protected]76ed979ee2013-07-17 15:50:4592 EXPECT_TRUE(base::PathExists(dir1_));
93 EXPECT_TRUE(base::PathExists(file3_));
[email protected]831a1a762010-10-12 05:57:0894}
95
96} // namespace disk_cache