blob: ccb25d333e2b8367949b680cdd20edddee6ea1e6 [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
Matt Menkeb0f4c7c2018-07-06 19:25:305#include <stdio.h>
6
7#include <map>
8
Yutaka Hiranobc3b4ddb2022-04-29 14:15:449#include "base/files/file_enumerator.h"
thestigd8df0332014-09-04 06:33:2910#include "base/files/file_util.h"
Yutaka Hiranobc3b4ddb2022-04-29 14:15:4411#include "base/files/safe_base_name.h"
[email protected]ea1a3f62012-11-16 20:34:2312#include "base/files/scoped_temp_dir.h"
Yutaka Hiranobc3b4ddb2022-04-29 14:15:4413#include "base/run_loop.h"
shivanigithub19b2f342020-12-30 01:57:4314#include "base/strings/string_number_conversions.h"
Yutaka Hiranobc3b4ddb2022-04-29 14:15:4415#include "base/test/bind.h"
shivanigithub19b2f342020-12-30 01:57:4316#include "base/test/scoped_feature_list.h"
Yutaka Hiranobc3b4ddb2022-04-29 14:15:4417#include "base/test/task_environment.h"
18#include "base/threading/platform_thread.h"
Yuta Hijikata101ed2a2020-11-18 07:50:3919#include "build/chromeos_buildflags.h"
[email protected]e0785902011-05-19 23:34:1720#include "net/disk_cache/cache_util.h"
[email protected]831a1a762010-10-12 05:57:0821#include "testing/gtest/include/gtest/gtest.h"
22#include "testing/platform_test.h"
23
24namespace disk_cache {
25
26class CacheUtilTest : public PlatformTest {
27 public:
dcheng67be2b1f2014-10-27 21:47:2928 void SetUp() override {
[email protected]831a1a762010-10-12 05:57:0829 PlatformTest::SetUp();
30 ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir());
vabrb8582322016-09-09 08:05:3731 cache_dir_ = tmp_dir_.GetPath().Append(FILE_PATH_LITERAL("Cache"));
[email protected]6cdfd7f2013-02-08 20:40:1532 file1_ = base::FilePath(cache_dir_.Append(FILE_PATH_LITERAL("file01")));
33 file2_ = base::FilePath(cache_dir_.Append(FILE_PATH_LITERAL(".file02")));
34 dir1_ = base::FilePath(cache_dir_.Append(FILE_PATH_LITERAL("dir01")));
[email protected]76ed979ee2013-07-17 15:50:4535 file3_ = base::FilePath(dir1_.Append(FILE_PATH_LITERAL("file03")));
[email protected]426d1c92013-12-03 20:08:5436 ASSERT_TRUE(base::CreateDirectory(cache_dir_));
[email protected]7600d0b2013-12-08 21:43:3037 FILE *fp = base::OpenFile(file1_, "w");
Raul Tambre94493c652019-03-11 17:18:3538 ASSERT_TRUE(fp != nullptr);
[email protected]7600d0b2013-12-08 21:43:3039 base::CloseFile(fp);
40 fp = base::OpenFile(file2_, "w");
Raul Tambre94493c652019-03-11 17:18:3541 ASSERT_TRUE(fp != nullptr);
[email protected]7600d0b2013-12-08 21:43:3042 base::CloseFile(fp);
[email protected]426d1c92013-12-03 20:08:5443 ASSERT_TRUE(base::CreateDirectory(dir1_));
[email protected]7600d0b2013-12-08 21:43:3044 fp = base::OpenFile(file3_, "w");
Raul Tambre94493c652019-03-11 17:18:3545 ASSERT_TRUE(fp != nullptr);
[email protected]7600d0b2013-12-08 21:43:3046 base::CloseFile(fp);
vabrb8582322016-09-09 08:05:3747 dest_dir_ = tmp_dir_.GetPath().Append(FILE_PATH_LITERAL("old_Cache_001"));
[email protected]6cdfd7f2013-02-08 20:40:1548 dest_file1_ = base::FilePath(dest_dir_.Append(FILE_PATH_LITERAL("file01")));
49 dest_file2_ =
50 base::FilePath(dest_dir_.Append(FILE_PATH_LITERAL(".file02")));
51 dest_dir1_ = base::FilePath(dest_dir_.Append(FILE_PATH_LITERAL("dir01")));
[email protected]831a1a762010-10-12 05:57:0852 }
53
54 protected:
[email protected]ea1a3f62012-11-16 20:34:2355 base::ScopedTempDir tmp_dir_;
[email protected]6cdfd7f2013-02-08 20:40:1556 base::FilePath cache_dir_;
57 base::FilePath file1_;
58 base::FilePath file2_;
59 base::FilePath dir1_;
[email protected]76ed979ee2013-07-17 15:50:4560 base::FilePath file3_;
[email protected]6cdfd7f2013-02-08 20:40:1561 base::FilePath dest_dir_;
62 base::FilePath dest_file1_;
63 base::FilePath dest_file2_;
64 base::FilePath dest_dir1_;
Yutaka Hiranobc3b4ddb2022-04-29 14:15:4465
66 base::test::TaskEnvironment task_environment_;
[email protected]831a1a762010-10-12 05:57:0867};
68
69TEST_F(CacheUtilTest, MoveCache) {
70 EXPECT_TRUE(disk_cache::MoveCache(cache_dir_, dest_dir_));
[email protected]7567484142013-07-11 17:36:0771 EXPECT_TRUE(base::PathExists(dest_dir_));
72 EXPECT_TRUE(base::PathExists(dest_file1_));
73 EXPECT_TRUE(base::PathExists(dest_file2_));
74 EXPECT_TRUE(base::PathExists(dest_dir1_));
Yuta Hijikata101ed2a2020-11-18 07:50:3975#if BUILDFLAG(IS_CHROMEOS_ASH)
[email protected]7567484142013-07-11 17:36:0776 EXPECT_TRUE(base::PathExists(cache_dir_)); // old cache dir stays
[email protected]831a1a762010-10-12 05:57:0877#else
[email protected]7567484142013-07-11 17:36:0778 EXPECT_FALSE(base::PathExists(cache_dir_)); // old cache is gone
[email protected]831a1a762010-10-12 05:57:0879#endif
[email protected]7567484142013-07-11 17:36:0780 EXPECT_FALSE(base::PathExists(file1_));
81 EXPECT_FALSE(base::PathExists(file2_));
82 EXPECT_FALSE(base::PathExists(dir1_));
[email protected]831a1a762010-10-12 05:57:0883}
84
85TEST_F(CacheUtilTest, DeleteCache) {
[email protected]831a1a762010-10-12 05:57:0886 disk_cache::DeleteCache(cache_dir_, false);
[email protected]7567484142013-07-11 17:36:0787 EXPECT_TRUE(base::PathExists(cache_dir_)); // cache dir stays
[email protected]76ed979ee2013-07-17 15:50:4588 EXPECT_FALSE(base::PathExists(dir1_));
[email protected]7567484142013-07-11 17:36:0789 EXPECT_FALSE(base::PathExists(file1_));
90 EXPECT_FALSE(base::PathExists(file2_));
[email protected]76ed979ee2013-07-17 15:50:4591 EXPECT_FALSE(base::PathExists(file3_));
[email protected]831a1a762010-10-12 05:57:0892}
93
94TEST_F(CacheUtilTest, DeleteCacheAndDir) {
[email protected]831a1a762010-10-12 05:57:0895 disk_cache::DeleteCache(cache_dir_, true);
[email protected]7567484142013-07-11 17:36:0796 EXPECT_FALSE(base::PathExists(cache_dir_)); // cache dir is gone
[email protected]76ed979ee2013-07-17 15:50:4597 EXPECT_FALSE(base::PathExists(dir1_));
[email protected]7567484142013-07-11 17:36:0798 EXPECT_FALSE(base::PathExists(file1_));
99 EXPECT_FALSE(base::PathExists(file2_));
[email protected]76ed979ee2013-07-17 15:50:45100 EXPECT_FALSE(base::PathExists(file3_));
[email protected]831a1a762010-10-12 05:57:08101}
102
Yutaka Hiranobc3b4ddb2022-04-29 14:15:44103TEST_F(CacheUtilTest, CleanupDirectory) {
104 base::RunLoop run_loop;
105 disk_cache::CleanupDirectory(cache_dir_,
106 base::BindLambdaForTesting([&](bool result) {
107 EXPECT_TRUE(result);
108 run_loop.Quit();
109 }));
110 run_loop.Run();
111
112 while (true) {
113 base::FileEnumerator enumerator(tmp_dir_.GetPath(), /*recursive=*/false,
114 /*file_type=*/base::FileEnumerator::FILES |
115 base::FileEnumerator::DIRECTORIES);
116 bool found = false;
117 while (true) {
118 base::FilePath path = enumerator.Next();
119 if (path.empty()) {
120 break;
121 }
122 // We're not sure if we see an entry in the directory because it depends
123 // on timing, but if we do, it must be "old_Cache_000".
124 // Caveat: On ChromeOS, we leave the top-level directory ("Cache") so
125 // it must be "Cache" or "old_Cache_000".
126 const base::FilePath dirname = path.DirName();
127 absl::optional<base::SafeBaseName> basename =
128 base::SafeBaseName::Create(path);
129 ASSERT_EQ(dirname, tmp_dir_.GetPath());
130 ASSERT_TRUE(basename.has_value());
131#if BUILDFLAG(IS_CHROMEOS_ASH)
132 if (basename->path().value() == FILE_PATH_LITERAL("Cache")) {
133 // See the comment above.
134 ASSERT_TRUE(base::IsDirectoryEmpty(dirname.Append(*basename)));
135 continue;
136 }
137#endif
138 ASSERT_EQ(basename->path().value(), FILE_PATH_LITERAL("old_Cache_000"));
139 found = true;
140 }
141 if (!found) {
142 break;
143 }
144
145 base::PlatformThread::Sleep(base::Milliseconds(10));
146 }
147}
148
149#if BUILDFLAG(IS_POSIX)
150TEST_F(CacheUtilTest, CleanupDirectoryFailsWhenParentDirectoryIsInaccessible) {
151 base::RunLoop run_loop;
152
153 ASSERT_TRUE(base::SetPosixFilePermissions(tmp_dir_.GetPath(), /*mode=*/0));
154 disk_cache::CleanupDirectory(cache_dir_,
155 base::BindLambdaForTesting([&](bool result) {
156 EXPECT_FALSE(result);
157 run_loop.Quit();
158 }));
159 run_loop.Run();
160}
161
162TEST_F(CacheUtilTest,
163 CleanupDirectorySucceedsWhenTargetDirectoryIsInaccessible) {
164 base::RunLoop run_loop;
165
166 ASSERT_TRUE(base::SetPosixFilePermissions(cache_dir_, /*mode=*/0));
167 disk_cache::CleanupDirectory(cache_dir_,
168 base::BindLambdaForTesting([&](bool result) {
169 EXPECT_TRUE(result);
170 run_loop.Quit();
171 }));
172 run_loop.Run();
173}
174#endif
175
Matt Menkeb0f4c7c2018-07-06 19:25:30176TEST_F(CacheUtilTest, PreferredCacheSize) {
177 const struct TestCase {
178 int64_t available;
shivanigithub19b2f342020-12-30 01:57:43179 int expected_without_trial;
180 int expected_with_200_trial;
181 int expected_with_250_trial;
182 int expected_with_300_trial;
Matt Menkeb0f4c7c2018-07-06 19:25:30183 } kTestCases[] = {
Maks Orlovicha789a6a42019-12-03 19:06:26184 // Weird negative value for available --- return the "default"
shivanigithub19b2f342020-12-30 01:57:43185 {-1000LL, 80 * 1024 * 1024, 160 * 1024 * 1024, 200 * 1024 * 1024,
186 240 * 1024 * 1024},
187 {-1LL, 80 * 1024 * 1024, 160 * 1024 * 1024, 200 * 1024 * 1024,
188 240 * 1024 * 1024},
Maks Orlovicha789a6a42019-12-03 19:06:26189
190 // 0 produces 0.
shivanigithub19b2f342020-12-30 01:57:43191 {0LL, 0, 0, 0, 0},
Maks Orlovicha789a6a42019-12-03 19:06:26192
Matt Menkeb0f4c7c2018-07-06 19:25:30193 // Cache is 80% of available space, when default cache size is larger than
194 // 80% of available space..
shivanigithub19b2f342020-12-30 01:57:43195 {50 * 1024 * 1024LL, 40 * 1024 * 1024, 40 * 1024 * 1024, 40 * 1024 * 1024,
196 40 * 1024 * 1024},
Matt Menkeb0f4c7c2018-07-06 19:25:30197 // Cache is default size, when default size is 10% to 80% of available
198 // space.
shivanigithub19b2f342020-12-30 01:57:43199 {100 * 1024 * 1024LL, 80 * 1024 * 1024, 80 * 1024 * 1024,
200 80 * 1024 * 1024, 80 * 1024 * 1024},
201 {200 * 1024 * 1024LL, 80 * 1024 * 1024, 80 * 1024 * 1024,
202 80 * 1024 * 1024, 80 * 1024 * 1024},
Matt Menkeb0f4c7c2018-07-06 19:25:30203 // Cache is 10% of available space if 2.5 * default size is more than 10%
204 // of available space.
shivanigithub19b2f342020-12-30 01:57:43205 {1000 * 1024 * 1024LL, 100 * 1024 * 1024, 200 * 1024 * 1024,
206 200 * 1024 * 1024, 200 * 1024 * 1024},
207 {2000 * 1024 * 1024LL, 200 * 1024 * 1024, 400 * 1024 * 1024,
208 400 * 1024 * 1024, 400 * 1024 * 1024},
Matt Menkeb0f4c7c2018-07-06 19:25:30209 // Cache is 2.5 * kDefaultCacheSize if 2.5 * kDefaultCacheSize uses from
210 // 1% to 10% of available space.
shivanigithub19b2f342020-12-30 01:57:43211 {10000 * 1024 * 1024LL, 200 * 1024 * 1024, 400 * 1024 * 1024,
212 500 * 1024 * 1024, 600 * 1024 * 1024},
Matt Menkeb0f4c7c2018-07-06 19:25:30213 // Otherwise, cache is 1% of available space.
shivanigithub19b2f342020-12-30 01:57:43214 {20000 * 1024 * 1024LL, 200 * 1024 * 1024, 400 * 1024 * 1024,
215 500 * 1024 * 1024, 600 * 1024 * 1024},
Matt Menkeb0f4c7c2018-07-06 19:25:30216 // Until it runs into the cache size cap.
shivanigithub19b2f342020-12-30 01:57:43217 {32000 * 1024 * 1024LL, 320 * 1024 * 1024, 640 * 1024 * 1024,
218 800 * 1024 * 1024, 960 * 1024 * 1024},
219 {50000 * 1024 * 1024LL, 320 * 1024 * 1024, 640 * 1024 * 1024,
220 800 * 1024 * 1024, 960 * 1024 * 1024},
Matt Menkeb0f4c7c2018-07-06 19:25:30221 };
222
223 for (const auto& test_case : kTestCases) {
shivanigithub19b2f342020-12-30 01:57:43224 EXPECT_EQ(test_case.expected_without_trial,
225 PreferredCacheSize(test_case.available))
Maks Orlovicha789a6a42019-12-03 19:06:26226 << test_case.available;
Seth Brenithddc50352021-08-05 15:05:45227
228 // Preferred size for WebUI code cache matches expected_without_trial but
229 // should never be more than 5 MB.
230 int expected_webui_code_cache_size =
231 std::min(5 * 1024 * 1024, test_case.expected_without_trial);
232 EXPECT_EQ(expected_webui_code_cache_size,
233 PreferredCacheSize(test_case.available,
234 net::GENERATED_WEBUI_BYTE_CODE_CACHE))
235 << test_case.available;
Matt Menkeb0f4c7c2018-07-06 19:25:30236 }
237
Bill Budge1bb18a942019-12-02 22:19:54238 // Check that the cache size cap is 50% higher for native code caches.
239 EXPECT_EQ(((320 * 1024 * 1024) / 2) * 3,
240 PreferredCacheSize(50000 * 1024 * 1024LL,
241 net::GENERATED_NATIVE_CODE_CACHE));
shivanigithub19b2f342020-12-30 01:57:43242
243 for (int cache_size_exeriment : {100, 200, 250, 300}) {
244 base::test::ScopedFeatureList scoped_feature_list;
245 std::map<std::string, std::string> field_trial_params;
246 field_trial_params["percent_relative_size"] =
247 base::NumberToString(cache_size_exeriment);
248 scoped_feature_list.InitAndEnableFeatureWithParameters(
249 disk_cache::kChangeDiskCacheSizeExperiment, field_trial_params);
250
251 for (const auto& test_case : kTestCases) {
252 int expected = 0;
253 switch (cache_size_exeriment) {
254 case 100:
255 expected = test_case.expected_without_trial;
256 break;
257 case 200:
258 expected = test_case.expected_with_200_trial;
259 break;
260 case 250:
261 expected = test_case.expected_with_250_trial;
262 break;
263 case 300:
264 expected = test_case.expected_with_300_trial;
265 break;
266 }
267
268 EXPECT_EQ(expected, PreferredCacheSize(test_case.available));
269
270 // For caches other than disk cache, the size is not scaled.
271 EXPECT_EQ(test_case.expected_without_trial,
272 PreferredCacheSize(test_case.available,
273 net::GENERATED_BYTE_CODE_CACHE));
Seth Brenithddc50352021-08-05 15:05:45274
275 // Preferred size for WebUI code cache is not scaled by the trial, and
276 // should never be more than 5 MB.
277 int expected_webui_code_cache_size =
278 std::min(5 * 1024 * 1024, test_case.expected_without_trial);
279 EXPECT_EQ(expected_webui_code_cache_size,
280 PreferredCacheSize(test_case.available,
281 net::GENERATED_WEBUI_BYTE_CODE_CACHE))
282 << test_case.available;
shivanigithub19b2f342020-12-30 01:57:43283 }
284
285 // Check that the cache size cap is 50% higher for native code caches but is
286 // not scaled for the experiment.
287 EXPECT_EQ(((320 * 1024 * 1024) / 2) * 3,
288 PreferredCacheSize(50000 * 1024 * 1024LL,
289 net::GENERATED_NATIVE_CODE_CACHE));
290 }
291
292 // Check no "percent_relative_size" matches default behavior.
293 {
294 base::test::ScopedFeatureList scoped_feature_list;
295 scoped_feature_list.InitAndEnableFeature(
296 disk_cache::kChangeDiskCacheSizeExperiment);
297 for (const auto& test_case : kTestCases) {
298 EXPECT_EQ(test_case.expected_without_trial,
299 PreferredCacheSize(test_case.available));
300 }
301 // Check that the cache size cap is 50% higher for native code caches.
302 EXPECT_EQ(((320 * 1024 * 1024) / 2) * 3,
303 PreferredCacheSize(50000 * 1024 * 1024LL,
304 net::GENERATED_NATIVE_CODE_CACHE));
305 }
Matt Menkeb0f4c7c2018-07-06 19:25:30306}
307
[email protected]831a1a762010-10-12 05:57:08308} // namespace disk_cache