File: resource_cache_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (259 lines) | stat: -rw-r--r-- 9,028 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/policy/core/common/cloud/resource_cache.h"

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/task_environment.h"
#include "base/test/test_simple_task_runner.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace policy {

namespace {

const char kKey1[] = "key 1";
const char kKey2[] = "key 2";
const char kKey3[] = "key 3";
const char kSubA[] = "a";
const char kSubB[] = "bb";
const char kSubC[] = "ccc";
const char kSubD[] = "dddd";
const char kSubE[] = "eeeee";

const char kData0[] = "{ \"key\": \"value\" }";
const char kData1[] = "{}";

const int kMaxCacheSize = 1024 * 10;
const std::string kData1Kb = std::string(1024, ' ');
const std::string kData2Kb = std::string(1024 * 2, ' ');
const std::string kData9Kb = std::string(1024 * 9, ' ');
const std::string kData10Kb = std::string(1024 * 10, ' ');
const std::string kData9KbUpdated = std::string(1024 * 9, '*');

bool Matches(const std::string& expected, const std::string& subkey) {
  return subkey == expected;
}

}  // namespace

class ResourceCacheTest : public testing::Test {
 protected:
  void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); }

  void TearDown() override { task_environment_.RunUntilIdle(); }

  base::test::TaskEnvironment task_environment_;
  base::ScopedTempDir temp_dir_;
};

TEST_F(ResourceCacheTest, StoreAndLoad) {
  ResourceCache cache(temp_dir_.GetPath(),
                      base::SingleThreadTaskRunner::GetCurrentDefault(),
                      /* max_cache_size */ std::nullopt);

  // No data initially.
  std::string data;
  EXPECT_TRUE(cache.Load(kKey1, kSubA, &data).empty());

  // Store some data and load it.
  base::FilePath file_path = cache.Store(kKey1, kSubA, kData0);
  EXPECT_FALSE(file_path.empty());
  std::string file_content;
  EXPECT_TRUE(base::ReadFileToString(file_path, &file_content));
  EXPECT_EQ(kData0, file_content);

  file_path = cache.Load(kKey1, kSubA, &data);
  EXPECT_FALSE(file_path.empty());
  file_content.clear();
  EXPECT_TRUE(base::ReadFileToString(file_path, &file_content));
  EXPECT_EQ(kData0, file_content);
  EXPECT_EQ(kData0, data);

  // Store more data in another subkey.
  EXPECT_FALSE(cache.Store(kKey1, kSubB, kData1).empty());

  // Write subkeys to two other keys.
  EXPECT_FALSE(cache.Store(kKey2, kSubA, kData0).empty());
  EXPECT_FALSE(cache.Store(kKey2, kSubB, kData1).empty());
  EXPECT_FALSE(cache.Store(kKey3, kSubA, kData0).empty());
  EXPECT_FALSE(cache.Store(kKey3, kSubB, kData1).empty());

  // Enumerate all the subkeys.
  std::map<std::string, std::string> contents;
  cache.LoadAllSubkeys(kKey1, &contents);
  EXPECT_EQ(2u, contents.size());
  EXPECT_EQ(kData0, contents[kSubA]);
  EXPECT_EQ(kData1, contents[kSubB]);

  // Store more subkeys.
  EXPECT_FALSE(cache.Store(kKey1, kSubC, kData1).empty());
  EXPECT_FALSE(cache.Store(kKey1, kSubD, kData1).empty());
  EXPECT_FALSE(cache.Store(kKey1, kSubE, kData1).empty());

  // Now purge some of them.
  std::set<std::string> keep;
  keep.insert(kSubB);
  keep.insert(kSubD);
  cache.PurgeOtherSubkeys(kKey1, keep);

  // Enumerate all the remaining subkeys.
  cache.LoadAllSubkeys(kKey1, &contents);
  EXPECT_EQ(2u, contents.size());
  EXPECT_EQ(kData1, contents[kSubB]);
  EXPECT_EQ(kData1, contents[kSubD]);

  // Delete subkeys directly.
  cache.Delete(kKey1, kSubB);
  cache.Delete(kKey1, kSubD);
  cache.LoadAllSubkeys(kKey1, &contents);
  EXPECT_EQ(0u, contents.size());

  // The other two keys were not affected.
  cache.LoadAllSubkeys(kKey2, &contents);
  EXPECT_EQ(2u, contents.size());
  EXPECT_EQ(kData0, contents[kSubA]);
  EXPECT_EQ(kData1, contents[kSubB]);
  cache.LoadAllSubkeys(kKey3, &contents);
  EXPECT_EQ(2u, contents.size());
  EXPECT_EQ(kData0, contents[kSubA]);
  EXPECT_EQ(kData1, contents[kSubB]);

  // Now purge all keys except the third.
  keep.clear();
  keep.insert(kKey3);
  cache.PurgeOtherKeys(keep);

  // The first two keys are empty.
  cache.LoadAllSubkeys(kKey1, &contents);
  EXPECT_EQ(0u, contents.size());
  cache.LoadAllSubkeys(kKey1, &contents);
  EXPECT_EQ(0u, contents.size());

  // The third key is unaffected.
  cache.LoadAllSubkeys(kKey3, &contents);
  EXPECT_EQ(2u, contents.size());
  EXPECT_EQ(kData0, contents[kSubA]);
  EXPECT_EQ(kData1, contents[kSubB]);
}

TEST_F(ResourceCacheTest, FilterSubkeys) {
  ResourceCache cache(temp_dir_.GetPath(),
                      base::SingleThreadTaskRunner::GetCurrentDefault(),
                      /* max_cache_size */ std::nullopt);

  // Store some data.
  EXPECT_FALSE(cache.Store(kKey1, kSubA, kData0).empty());
  EXPECT_FALSE(cache.Store(kKey1, kSubB, kData1).empty());
  EXPECT_FALSE(cache.Store(kKey1, kSubC, kData0).empty());
  EXPECT_FALSE(cache.Store(kKey2, kSubA, kData0).empty());
  EXPECT_FALSE(cache.Store(kKey2, kSubB, kData1).empty());
  EXPECT_FALSE(cache.Store(kKey3, kSubA, kData0).empty());
  EXPECT_FALSE(cache.Store(kKey3, kSubB, kData1).empty());

  // Check the contents of kKey1.
  std::map<std::string, std::string> contents;
  cache.LoadAllSubkeys(kKey1, &contents);
  EXPECT_EQ(3u, contents.size());
  EXPECT_EQ(kData0, contents[kSubA]);
  EXPECT_EQ(kData1, contents[kSubB]);
  EXPECT_EQ(kData0, contents[kSubC]);

  // Filter some subkeys.
  cache.FilterSubkeys(kKey1, base::BindRepeating(&Matches, kSubA));

  // Check the contents of kKey1 again.
  cache.LoadAllSubkeys(kKey1, &contents);
  EXPECT_EQ(2u, contents.size());
  EXPECT_EQ(kData1, contents[kSubB]);
  EXPECT_EQ(kData0, contents[kSubC]);

  // Other keys weren't affected.
  cache.LoadAllSubkeys(kKey2, &contents);
  EXPECT_EQ(2u, contents.size());
  cache.LoadAllSubkeys(kKey3, &contents);
  EXPECT_EQ(2u, contents.size());
}

TEST_F(ResourceCacheTest, StoreWithEnabledCacheLimit) {
  ResourceCache cache(temp_dir_.GetPath(),
                      base::SingleThreadTaskRunner::GetCurrentDefault(),
                      kMaxCacheSize);
  task_environment_.RunUntilIdle();

  // Put first subkey with 9Kb data in cache.
  EXPECT_FALSE(cache.Store(kKey1, kSubA, kData9Kb).empty());
  // Try to put second subkey with 2Kb data in cache, expected to fail while
  // total size exceeds 10Kb.
  EXPECT_TRUE(cache.Store(kKey2, kSubB, kData2Kb).empty());
  // Put second subkey with 1Kb data in cache.
  EXPECT_FALSE(cache.Store(kKey2, kSubC, kData1Kb).empty());
  // Try to put third subkey with 2 bytes data in cache, expected to fail while
  // total size exceeds 10Kb.
  EXPECT_TRUE(cache.Store(kKey1, kSubB, kData1).empty());

  // Remove keys with all subkeys.
  cache.Clear(kKey1);
  cache.Clear(kKey2);

  // Put first subkey with 9Kb data in cache.
  EXPECT_FALSE(cache.Store(kKey3, kSubA, kData9Kb).empty());
  // Put second subkey with 1Kb data in cache.
  EXPECT_FALSE(cache.Store(kKey3, kSubB, kData1Kb).empty());
  // Try to put third subkey with 2 bytes data in cache, expected to fail while
  // total size exceeds 10Kb.
  EXPECT_TRUE(cache.Store(kKey1, kSubB, kData1).empty());

  // Replace data in first subkey with another 9Kb data.
  EXPECT_FALSE(cache.Store(kKey3, kSubA, kData9KbUpdated).empty());

  // Remove this key with 9Kb data.
  cache.Delete(kKey3, kSubA);

  // Put second subkey with 2 bytes data in cache.
  EXPECT_FALSE(cache.Store(kKey1, kSubB, kData1).empty());
}

#if BUILDFLAG(IS_POSIX)  // Because of symbolic links.

TEST_F(ResourceCacheTest, StoreInDirectoryWithCycleSymlinks) {
  base::FilePath inner_dir = temp_dir_.GetPath().AppendASCII("inner");
  ASSERT_TRUE(base::CreateDirectory(inner_dir));
  base::FilePath symlink_to_parent = inner_dir.AppendASCII("symlink");
  ASSERT_TRUE(base::CreateSymbolicLink(temp_dir_.GetPath(), symlink_to_parent));

  ResourceCache cache(temp_dir_.GetPath(),
                      base::SingleThreadTaskRunner::GetCurrentDefault(),
                      kMaxCacheSize);
  task_environment_.RunUntilIdle();

  // Check if the cache is empty
  EXPECT_FALSE(cache.Store(kKey1, kSubA, kData10Kb).empty());
}

TEST_F(ResourceCacheTest, StoreInDirectoryWithSymlinkToRoot) {
  base::FilePath inner_dir = temp_dir_.GetPath().AppendASCII("inner");
  ASSERT_TRUE(base::CreateDirectory(inner_dir));
  base::FilePath root_path(FILE_PATH_LITERAL("/"));
  base::FilePath symlink_to_root = temp_dir_.GetPath().AppendASCII("symlink");
  ASSERT_TRUE(base::CreateSymbolicLink(root_path, symlink_to_root));

  ResourceCache cache(temp_dir_.GetPath(),
                      base::SingleThreadTaskRunner::GetCurrentDefault(),
                      kMaxCacheSize);
  task_environment_.RunUntilIdle();

  // Check if the cache is empty
  EXPECT_FALSE(cache.Store(kKey1, kSubA, kData10Kb).empty());
}

#endif  // BUILDFLAG(IS_POSIX)

}  // namespace policy