xref: /aosp_15_r20/external/cronet/net/disk_cache/simple/simple_test_util.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2013 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 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_TEST_UTIL_H_
6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_TEST_UTIL_H_
7 
8 #include <stddef.h>
9 
10 #include <string>
11 
12 #include "base/functional/callback.h"
13 
14 namespace base {
15 class FilePath;
16 }
17 
18 namespace disk_cache::simple_util {
19 
20 // Immutable array with compile-time bound-checking.
21 template <typename T, size_t Size>
22 class ImmutableArray {
23  public:
24   static const size_t size = Size;
25 
ImmutableArray(const base::RepeatingCallback<T (size_t index)> & initializer)26   explicit ImmutableArray(
27       const base::RepeatingCallback<T(size_t index)>& initializer) {
28     for (size_t i = 0; i < size; ++i)
29       data_[i] = initializer.Run(i);
30   }
31 
32   template <size_t Index>
at()33   const T& at() const {
34     static_assert(Index < size, "array out of bounds");
35     return data_[Index];
36   }
37 
38  private:
39   T data_[size];
40 };
41 
42 // Creates a corrupt file to be used in tests.
43 bool CreateCorruptFileForTests(const std::string& key,
44                                const base::FilePath& cache_path);
45 
46 // Removes the key SHA256 from an entry.
47 bool RemoveKeySHA256FromEntry(const std::string& key,
48                               const base::FilePath& cache_path);
49 
50 // Modifies the key SHA256 from an entry so that it is corrupt.
51 bool CorruptKeySHA256FromEntry(const std::string& key,
52                                const base::FilePath& cache_path);
53 
54 // Modifies the stream 0 length field from an entry so it is invalid.
55 bool CorruptStream0LengthFromEntry(const std::string& key,
56                                    const base::FilePath& cache_path);
57 
58 }  // namespace disk_cache::simple_util
59 
60 #endif  // NET_DISK_CACHE_SIMPLE_SIMPLE_TEST_UTIL_H_
61