File: persistent_cache_collection_unittest.cc

package info (click to toggle)
chromium 144.0.7559.109-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,915,868 kB
  • sloc: cpp: 35,866,215; ansic: 7,599,035; javascript: 3,623,761; python: 1,639,407; xml: 833,084; asm: 716,173; pascal: 185,323; sh: 88,763; perl: 88,699; objc: 79,984; sql: 58,217; cs: 42,430; fortran: 24,101; makefile: 20,747; tcl: 15,277; php: 14,022; yacc: 9,059; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (476 lines) | stat: -rw-r--r-- 18,466 bytes parent folder | download | duplicates (5)
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// Copyright 2025 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/persistent_cache/persistent_cache_collection.h"

#include <algorithm>
#include <optional>
#include <string>
#include <vector>

#include "base/containers/span.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/gmock_expected_support.h"
#include "base/test/gtest_util.h"
#include "base/test/task_environment.h"
#include "base/threading/platform_thread.h"
#include "base/time/time.h"
#include "components/persistent_cache/backend.h"
#include "components/persistent_cache/mock/mock_backend.h"
#include "components/persistent_cache/mock/mock_backend_storage_delegate.h"
#include "components/persistent_cache/pending_backend.h"
#include "components/persistent_cache/persistent_cache.h"
#include "components/persistent_cache/sqlite/constants.h"
#include "components/persistent_cache/sqlite/sqlite_backend_impl.h"
#include "components/persistent_cache/sqlite/vfs/sqlite_database_vfs_file_set.h"
#include "components/persistent_cache/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

std::vector<base::FilePath> GetPathsInDir(const base::FilePath& directory) {
  std::vector<base::FilePath> paths;
  base::FileEnumerator(directory, /*recursive=*/false,
                       base::FileEnumerator::FILES)
      .ForEach([&paths](const base::FilePath& file_path) {
        paths.push_back(file_path);
      });
  return paths;
}

}  // namespace

namespace persistent_cache {

using base::test::ErrorIs;
using base::test::HasValue;
using base::test::ValueIs;
using testing::_;
using testing::AnyNumber;
using testing::Eq;
using testing::IsEmpty;
using testing::IsTrue;
using testing::Ne;
using testing::Optional;
using testing::Property;
using testing::ResultOf;
using testing::Return;
using testing::StrEq;
using testing::UnorderedElementsAre;

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

  static constexpr int64_t kTargetFootprint = 20000;
  base::ScopedTempDir temp_dir_;
  base::test::SingleThreadTaskEnvironment task_environment_;
};

// Two operations with the same cache_id operate on the same database.
TEST_F(PersistentCacheCollectionTest, CreateAndUse) {
  PersistentCacheCollection collection(temp_dir_.GetPath(), kTargetFootprint);

  std::string cache_id("cache_id");
  std::string key("key");
  EXPECT_THAT(collection.Insert(cache_id, key, base::as_byte_span(key)),
              HasValue());
  ASSERT_THAT(FindEntry(collection, cache_id, key),
              ValueIs(Optional(ContentEq(base::as_byte_span(key)))));
}

TEST_F(PersistentCacheCollectionTest, DeleteAllFiles) {
  PersistentCacheCollection collection(temp_dir_.GetPath(), kTargetFootprint);

  std::string cache_id("cache_id");
  std::string key("key");
  EXPECT_THAT(collection.Insert(cache_id, key, base::as_byte_span(key)),
              HasValue());

  // Inserting an entry should have created at least one file.
  EXPECT_FALSE(base::IsDirectoryEmpty(temp_dir_.GetPath()));

  collection.DeleteAllFiles();
  EXPECT_TRUE(base::IsDirectoryEmpty(temp_dir_.GetPath()));
}

static constexpr int64_t kOneHundredMiB = 100 * 1024 * 1024;

TEST_F(PersistentCacheCollectionTest, Retrieval) {
  PersistentCacheCollection collection(temp_dir_.GetPath(), kOneHundredMiB);

  constexpr char first_cache_id[] = "first_cache_id";
  constexpr char second_cache_id[] = "second_cache_id";

  constexpr char first_key[] = "first_key";
  constexpr char second_key[] = "second_key";

  constexpr const char first_content[] = "first_content";

  // At first there is nothing in the collection.
  EXPECT_THAT(FindEntry(collection, first_cache_id, first_key),
              ValueIs(Eq(std::nullopt)));
  EXPECT_THAT(FindEntry(collection, first_cache_id, second_key),
              ValueIs(Eq(std::nullopt)));
  EXPECT_THAT(FindEntry(collection, second_cache_id, first_key),
              ValueIs(Eq(std::nullopt)));
  EXPECT_THAT(FindEntry(collection, second_cache_id, second_key),
              ValueIs(Eq(std::nullopt)));

  // Inserting for a certain cache id allows retrieval for this id and this id
  // only.
  EXPECT_THAT(collection.Insert(first_cache_id, first_key,
                                base::byte_span_from_cstring(first_content)),
              HasValue());
  ASSERT_THAT(FindEntry(collection, first_cache_id, first_key),
              ValueIs(Optional(
                  ContentEq(base::byte_span_from_cstring(first_content)))));

  EXPECT_THAT(FindEntry(collection, second_cache_id, first_key),
              ValueIs(Eq(std::nullopt)));
}

TEST_F(PersistentCacheCollectionTest, RetrievalAfterClear) {
  PersistentCacheCollection collection(temp_dir_.GetPath(), kOneHundredMiB);

  std::string first_cache_id = "first_cache_id";
  std::string first_key = "first_key";
  constexpr const char first_content[] = "first_content";

  // Test basic retrieval.
  EXPECT_THAT(FindEntry(collection, first_cache_id, first_key),
              ValueIs(Eq(std::nullopt)));

  EXPECT_THAT(collection.Insert(first_cache_id, first_key,
                                base::byte_span_from_cstring(first_content)),
              HasValue());

  EXPECT_THAT(FindEntry(collection, first_cache_id, first_key),
              ValueIs(Ne(std::nullopt)));

  collection.Clear();

  // Retrieval still works after clear because data persistence is unaffected by
  // lifetime of PersistentCache instances.
  EXPECT_THAT(FindEntry(collection, first_cache_id, first_key),
              ValueIs(Ne(std::nullopt)));
}

TEST_F(PersistentCacheCollectionTest, ContinuousFootPrintReduction) {
  constexpr int64_t kSmallFootprint = 128;

  PersistentCacheCollection collection(temp_dir_.GetPath(), kSmallFootprint);

  int i = 0;
  int64_t added_footprint = 0;

  // Add things right up to the limit where files start to be deleted.
  while (added_footprint < kSmallFootprint) {
    std::string number = base::NumberToString(i);

    // Account for size of key and value.
    int64_t footprint_after_insertion = added_footprint + (number.length() * 2);

    if (footprint_after_insertion < kSmallFootprint) {
      int64_t directory_size_before =
          base::ComputeDirectorySize(temp_dir_.GetPath());

      EXPECT_THAT(collection.Insert(number, number, base::as_byte_span(number)),
                  HasValue());

      int64_t directory_size_after =
          base::ComputeDirectorySize(temp_dir_.GetPath());

      // If there's no footprint reduction and the new values are being stored
      // then directory size is just going up.
      EXPECT_GT(directory_size_after, directory_size_before);
    }

    added_footprint = footprint_after_insertion;
    ++i;
  }

  // If `kSmallFootprint` is not large enough to trigger at least two successful
  // insertions into the cache the test does not provide sufficient coverage.
  ASSERT_GT(i, 2);

  int64_t directory_size_before =
      base::ComputeDirectorySize(temp_dir_.GetPath());

  // Since no footprint reduction should have been triggered all values added
  // should still be available.
  for (int j = 0; j < i - 1; ++j) {
    std::string number = base::NumberToString(j);
    EXPECT_THAT(FindEntry(collection, number, number),
                ValueIs(Ne(std::nullopt)));
  }

  // Add one more item which should bring things over the limit.
  std::string number = base::NumberToString(i + 1);
  EXPECT_THAT(collection.Insert(number, number, base::as_byte_span(number)),
              HasValue());

  int64_t directory_size_after =
      base::ComputeDirectorySize(temp_dir_.GetPath());

  // Footprint reduction happened automatically. Note that's it's not possible
  // to specifically know what the current footprint is since the last insert
  // took place after the footprint reduction.
  EXPECT_LT(directory_size_after, directory_size_before);
}

TEST_F(PersistentCacheCollectionTest, BaseNameFromCacheId) {
  // Invalid tokens results in empty string and not a crash.
  EXPECT_EQ(PersistentCacheCollection::BaseNameFromCacheId("`"),
            base::FilePath());
  EXPECT_EQ(PersistentCacheCollection::BaseNameFromCacheId("``"),
            base::FilePath());

  // Verify file name is obfuscated.
  std::string cache_id("devs_first_db");
  base::FilePath base_name(
      PersistentCacheCollection::BaseNameFromCacheId(cache_id));
  EXPECT_EQ(base_name.value().find(base::FilePath::FromASCII(cache_id).value()),
            std::string::npos);
  EXPECT_EQ(base_name.value().find(FILE_PATH_LITERAL("devs")),
            std::string::npos);
  EXPECT_EQ(base_name.value().find(FILE_PATH_LITERAL("first")),
            std::string::npos);
}

TEST_F(PersistentCacheCollectionTest, FullAllowedCharacterSetHandled) {
  PersistentCacheCollection collection(temp_dir_.GetPath(), kOneHundredMiB);

  std::string all_chars_key =
      PersistentCacheCollection::GetAllAllowedCharactersInCacheIds();
  std::string number("number");
  EXPECT_THAT(
      collection.Insert(all_chars_key, number, base::as_byte_span(number)),
      HasValue());
  EXPECT_THAT(FindEntry(collection, all_chars_key, number),
              ValueIs(Ne(std::nullopt)));
}

TEST_F(PersistentCacheCollectionTest, InstancesAbandonnedOnLRUEviction) {
  static constexpr char kKey[] = "KEY";
  static constexpr size_t kLruCacheCapacity = 5;
  PersistentCacheCollection collection(temp_dir_.GetPath(), kOneHundredMiB,
                                       kLruCacheCapacity);

  std::vector<std::unique_ptr<PersistentCache>> caches;
  size_t cache_id = 0;
  auto get_increasing_cache_id = [&cache_id]() {
    return base::NumberToString(cache_id++);
  };

  // Creates caches exactly up to capacity.
  for (size_t i = 0; i < kLruCacheCapacity; ++i) {
    ASSERT_OK_AND_ASSIGN(
        auto pending_backend,
        collection.ShareReadWriteConnection(get_increasing_cache_id()));
    caches.push_back(PersistentCache::Bind(std::move(pending_backend)));
  }
  ASSERT_NE(caches.front(), nullptr);

  // Find succeeds since the instance is not evicted yet.
  EXPECT_THAT(FindEntry(*caches.front(), kKey), HasValue());

  // Create one more cache which goes over the limit.
  ASSERT_OK_AND_ASSIGN(
      auto pending_backend,
      collection.ShareReadWriteConnection(get_increasing_cache_id()));
  caches.emplace_back(PersistentCache::Bind(std::move(pending_backend)));

  // The first cache has now been evicted and is abandoned.
  EXPECT_THAT(FindEntry(*caches.front(), kKey),
              ErrorIs(TransactionError::kConnectionError));
}

TEST_F(PersistentCacheCollectionTest, InstancesAbandonnedOnClear) {
  PersistentCacheCollection collection(temp_dir_.GetPath(), kOneHundredMiB);

  std::string key("key");
  ASSERT_OK_AND_ASSIGN(auto pending_backend,
                       collection.ShareReadWriteConnection(key));
  auto cache = PersistentCache::Bind(std::move(pending_backend));

  collection.Clear();
  EXPECT_THAT(FindEntry(*cache, key),
              ErrorIs(TransactionError::kConnectionError));
}

TEST_F(PersistentCacheCollectionTest, AbandonnedErrorsDoNotCauseDeletions) {
  PersistentCacheCollection collection(temp_dir_.GetPath(), kOneHundredMiB);

  std::string first_cache_id = "first_cache_id";
  std::string first_key = "first_key";
  constexpr const char first_content[] = "first_content";

  EXPECT_THAT(collection.Insert(first_cache_id, first_key,
                                base::byte_span_from_cstring(first_content)),
              HasValue());
  EXPECT_THAT(
      GetPathsInDir(temp_dir_.GetPath()),
      UnorderedElementsAre(
          Property(&base::FilePath::Extension, StrEq(sqlite::kDbFileExtension)),
          Property(&base::FilePath::Extension,
                   StrEq(sqlite::kJournalFileExtension))));

  ASSERT_OK_AND_ASSIGN(auto pending_backend,
                       collection.ShareReadWriteConnection(first_cache_id));
  auto cache = PersistentCache::Bind(std::move(pending_backend));
  EXPECT_EQ(cache->Abandon(), LockState::kNotHeld);

  EXPECT_THAT(FindEntry(*cache, first_key),
              ErrorIs(TransactionError::kConnectionError));
  EXPECT_THAT(FindEntry(collection, first_cache_id, first_key),
              ErrorIs(TransactionError::kConnectionError));

  // Files are still there.
  EXPECT_THAT(
      GetPathsInDir(temp_dir_.GetPath()),
      UnorderedElementsAre(
          Property(&base::FilePath::Extension, StrEq(sqlite::kDbFileExtension)),
          Property(&base::FilePath::Extension,
                   StrEq(sqlite::kJournalFileExtension))));
}

TEST_F(PersistentCacheCollectionTest, EvictWhileLockedDeletesFiles) {
  auto mock_delegate =
      std::make_unique<testing::NiceMock<MockBackendStorageDelegate>>();
  auto backend = std::make_unique<testing::NiceMock<MockBackend>>();

  // Backend default behavior.
  ON_CALL(*backend, IsReadOnly()).WillByDefault(Return(false));

  // Simulates the fact that readers are left over on abandonment.
  EXPECT_CALL(*backend, Abandon()).WillOnce(Return(LockState::kReading));

  // Return the mock backend from the BackendStorage::Delegate when requested,
  // and remember the cache base name.
  base::FilePath saved_base_name;
  EXPECT_CALL(*mock_delegate, MakeBackend(temp_dir_.GetPath(), _, false, false))
      .WillOnce([&](const base::FilePath& directory,
                    const base::FilePath& base_name, bool single_connection,
                    bool journal_mode_wal) {
        saved_base_name = base_name;
        return (std::move(backend));
      });

  // This call only takes place as a reaction to the reader being left over
  // after abandonment.
  EXPECT_CALL(
      *mock_delegate,
      DeleteFiles(temp_dir_.GetPath(),
                  ResultOf(
                      [&saved_base_name](const base::FilePath& base_name) {
                        return base_name == saved_base_name;
                      },
                      IsTrue())));

  PersistentCacheCollection collection(temp_dir_.GetPath(), kOneHundredMiB,
                                       std::move(mock_delegate));
  std::string first_cache_id = "first_cache_id";
  // `ExportReadWriteBackendParams` called to force the collection to create a
  // `PersistentCache`.
  collection.ShareReadWriteConnection(first_cache_id);
  collection.Clear();
}

TEST_F(PersistentCacheCollectionTest,
       BackendStorageCreationAfterDeleteSucceedsWithHeldFiles) {
  PersistentCacheCollection collection(temp_dir_.GetPath(), kOneHundredMiB);
  std::string first_cache_id = "first_cache_id";

  // Files exists after creating a params since `ExportReadWriteBackendParams`
  // forces the creation of a `PersistentCache`.
  ASSERT_THAT(collection.ShareReadWriteConnection(first_cache_id),
              Ne(std::nullopt));
  EXPECT_THAT(
      GetPathsInDir(temp_dir_.GetPath()),
      UnorderedElementsAre(
          Property(&base::FilePath::Extension, StrEq(sqlite::kDbFileExtension)),
          Property(&base::FilePath::Extension,
                   StrEq(sqlite::kJournalFileExtension))));

  // No more files after delete.
  collection.DeleteAllFiles();
  EXPECT_THAT(GetPathsInDir(temp_dir_.GetPath()), IsEmpty());

  // It's possible to recreate params/files with the same cache_id.
  ASSERT_OK_AND_ASSIGN(auto other_pending_backend,
                       collection.ShareReadWriteConnection(first_cache_id));
  EXPECT_THAT(
      GetPathsInDir(temp_dir_.GetPath()),
      UnorderedElementsAre(
          Property(&base::FilePath::Extension, StrEq(sqlite::kDbFileExtension)),
          Property(&base::FilePath::Extension,
                   StrEq(sqlite::kJournalFileExtension))));
}

TEST_F(PersistentCacheCollectionTest, PermanentErrorCausesDeletion) {
  PersistentCacheCollection collection(temp_dir_.GetPath(), kOneHundredMiB);

  std::string first_cache_id = "first_cache_id";
  std::string first_key = "first_key";
  static constexpr char first_content[] = "first_content";

  EXPECT_THAT(collection.Insert(first_cache_id, first_key,
                                base::byte_span_from_cstring(first_content)),
              HasValue());
  EXPECT_THAT(
      GetPathsInDir(temp_dir_.GetPath()),
      UnorderedElementsAre(
          Property(&base::FilePath::Extension, StrEq(sqlite::kDbFileExtension)),
          Property(&base::FilePath::Extension,
                   StrEq(sqlite::kJournalFileExtension))));

  // TODO(https://crbug.com/377475540): Instead of triggering an error in a
  // backend specific way PersistentCacheCollection should have a way to inject
  // mock BackendStorage::Delegate.
  base::FileEnumerator(temp_dir_.GetPath(), /*recursive=*/false,
                       base::FileEnumerator::FILES)
      .ForEach([](const base::FilePath& file_path) {
        base::File file;
        file.Initialize(file_path, base::File::FLAG_WRITE |
                                       base::File::FLAG_OPEN |
                                       base::File::FLAG_CAN_DELETE_ON_CLOSE |
                                       base::File::FLAG_WIN_SHARE_DELETE);

        // Truncate which will cause future requests to start failing.
        CHECK(file.IsValid());
        file.SetLength(0);
      });

  // Permanent error because there are no more valid files.
  EXPECT_THAT(FindEntry(collection, first_cache_id, first_key),
              ErrorIs(TransactionError::kPermanent));

  // TODO(https://crbug.com/377475540): As in previous item once we use mocking
  // to trigger failures we should validate that transient errors are handled
  // properly in a backend agnostic way.

  // Files got deleted on permanent error.
  EXPECT_THAT(GetPathsInDir(temp_dir_.GetPath()), IsEmpty());
}

using PersistentCacheCollectionDeathTest = PersistentCacheCollectionTest;

// Tests that trying to operate on a cache in a collection crashes if an
// invalid cache_id is used.
TEST_F(PersistentCacheCollectionDeathTest, BadKeysCrash) {
  EXPECT_CHECK_DEATH({
    // There is no expectation for the return value we can test since death is
    // expected
    std::ignore = PersistentCacheCollection(temp_dir_.GetPath(), kOneHundredMiB)
                      .Insert(std::string("BADKEY"), "key",
                              base::byte_span_from_cstring("value"));
  });
}

}  // namespace persistent_cache