1 // Copyright 2011 The Chromium Authors
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 <stdio.h>
6
7 #include <map>
8
9 #include "base/files/file_enumerator.h"
10 #include "base/files/file_util.h"
11 #include "base/files/safe_base_name.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/run_loop.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/test/bind.h"
16 #include "base/test/scoped_feature_list.h"
17 #include "base/test/task_environment.h"
18 #include "base/threading/platform_thread.h"
19 #include "build/chromeos_buildflags.h"
20 #include "net/disk_cache/cache_util.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "testing/platform_test.h"
23
24 namespace disk_cache {
25
26 class CacheUtilTest : public PlatformTest {
27 public:
SetUp()28 void SetUp() override {
29 PlatformTest::SetUp();
30 ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir());
31 cache_dir_ = tmp_dir_.GetPath().Append(FILE_PATH_LITERAL("Cache"));
32 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")));
35 file3_ = base::FilePath(dir1_.Append(FILE_PATH_LITERAL("file03")));
36 ASSERT_TRUE(base::CreateDirectory(cache_dir_));
37 FILE *fp = base::OpenFile(file1_, "w");
38 ASSERT_TRUE(fp != nullptr);
39 base::CloseFile(fp);
40 fp = base::OpenFile(file2_, "w");
41 ASSERT_TRUE(fp != nullptr);
42 base::CloseFile(fp);
43 ASSERT_TRUE(base::CreateDirectory(dir1_));
44 fp = base::OpenFile(file3_, "w");
45 ASSERT_TRUE(fp != nullptr);
46 base::CloseFile(fp);
47 dest_dir_ = tmp_dir_.GetPath().Append(FILE_PATH_LITERAL("old_Cache_001"));
48 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")));
52 }
53
54 protected:
55 base::ScopedTempDir tmp_dir_;
56 base::FilePath cache_dir_;
57 base::FilePath file1_;
58 base::FilePath file2_;
59 base::FilePath dir1_;
60 base::FilePath file3_;
61 base::FilePath dest_dir_;
62 base::FilePath dest_file1_;
63 base::FilePath dest_file2_;
64 base::FilePath dest_dir1_;
65
66 base::test::TaskEnvironment task_environment_;
67 };
68
TEST_F(CacheUtilTest,MoveCache)69 TEST_F(CacheUtilTest, MoveCache) {
70 EXPECT_TRUE(disk_cache::MoveCache(cache_dir_, dest_dir_));
71 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_));
75 #if BUILDFLAG(IS_CHROMEOS_ASH)
76 EXPECT_TRUE(base::PathExists(cache_dir_)); // old cache dir stays
77 #else
78 EXPECT_FALSE(base::PathExists(cache_dir_)); // old cache is gone
79 #endif
80 EXPECT_FALSE(base::PathExists(file1_));
81 EXPECT_FALSE(base::PathExists(file2_));
82 EXPECT_FALSE(base::PathExists(dir1_));
83 }
84
TEST_F(CacheUtilTest,DeleteCache)85 TEST_F(CacheUtilTest, DeleteCache) {
86 disk_cache::DeleteCache(cache_dir_, false);
87 EXPECT_TRUE(base::PathExists(cache_dir_)); // cache dir stays
88 EXPECT_FALSE(base::PathExists(dir1_));
89 EXPECT_FALSE(base::PathExists(file1_));
90 EXPECT_FALSE(base::PathExists(file2_));
91 EXPECT_FALSE(base::PathExists(file3_));
92 }
93
TEST_F(CacheUtilTest,DeleteCacheAndDir)94 TEST_F(CacheUtilTest, DeleteCacheAndDir) {
95 disk_cache::DeleteCache(cache_dir_, true);
96 EXPECT_FALSE(base::PathExists(cache_dir_)); // cache dir is gone
97 EXPECT_FALSE(base::PathExists(dir1_));
98 EXPECT_FALSE(base::PathExists(file1_));
99 EXPECT_FALSE(base::PathExists(file2_));
100 EXPECT_FALSE(base::PathExists(file3_));
101 }
102
TEST_F(CacheUtilTest,CleanupDirectory)103 TEST_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 std::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)
TEST_F(CacheUtilTest,CleanupDirectoryFailsWhenParentDirectoryIsInaccessible)150 TEST_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
TEST_F(CacheUtilTest,CleanupDirectorySucceedsWhenTargetDirectoryIsInaccessible)162 TEST_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
TEST_F(CacheUtilTest,PreferredCacheSize)176 TEST_F(CacheUtilTest, PreferredCacheSize) {
177 const struct TestCase {
178 int64_t available;
179 int expected_without_trial;
180 int expected_with_200_trial;
181 int expected_with_250_trial;
182 int expected_with_300_trial;
183 } kTestCases[] = {
184 // Weird negative value for available --- return the "default"
185 {-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},
189
190 // 0 produces 0.
191 {0LL, 0, 0, 0, 0},
192
193 // Cache is 80% of available space, when default cache size is larger than
194 // 80% of available space..
195 {50 * 1024 * 1024LL, 40 * 1024 * 1024, 40 * 1024 * 1024, 40 * 1024 * 1024,
196 40 * 1024 * 1024},
197 // Cache is default size, when default size is 10% to 80% of available
198 // space.
199 {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},
203 // Cache is 10% of available space if 2.5 * default size is more than 10%
204 // of available space.
205 {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},
209 // Cache is 2.5 * kDefaultCacheSize if 2.5 * kDefaultCacheSize uses from
210 // 1% to 10% of available space.
211 {10000 * 1024 * 1024LL, 200 * 1024 * 1024, 400 * 1024 * 1024,
212 500 * 1024 * 1024, 600 * 1024 * 1024},
213 // Otherwise, cache is 1% of available space.
214 {20000 * 1024 * 1024LL, 200 * 1024 * 1024, 400 * 1024 * 1024,
215 500 * 1024 * 1024, 600 * 1024 * 1024},
216 // Until it runs into the cache size cap.
217 {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},
221 };
222
223 for (const auto& test_case : kTestCases) {
224 EXPECT_EQ(test_case.expected_without_trial,
225 PreferredCacheSize(test_case.available))
226 << test_case.available;
227
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;
236 }
237
238 // 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));
242
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));
274
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;
283 }
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 }
306 }
307
308 } // namespace disk_cache
309