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