simple_test_util.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_TEST_UTIL_H_
  5. #define NET_DISK_CACHE_SIMPLE_SIMPLE_TEST_UTIL_H_
  6. #include <stddef.h>
  7. #include <string>
  8. #include "base/callback.h"
  9. namespace base {
  10. class FilePath;
  11. }
  12. namespace disk_cache::simple_util {
  13. // Immutable array with compile-time bound-checking.
  14. template <typename T, size_t Size>
  15. class ImmutableArray {
  16. public:
  17. static const size_t size = Size;
  18. explicit ImmutableArray(
  19. const base::RepeatingCallback<T(size_t index)>& initializer) {
  20. for (size_t i = 0; i < size; ++i)
  21. data_[i] = initializer.Run(i);
  22. }
  23. template <size_t Index>
  24. const T& at() const {
  25. static_assert(Index < size, "array out of bounds");
  26. return data_[Index];
  27. }
  28. private:
  29. T data_[size];
  30. };
  31. // Creates a corrupt file to be used in tests.
  32. bool CreateCorruptFileForTests(const std::string& key,
  33. const base::FilePath& cache_path);
  34. // Removes the key SHA256 from an entry.
  35. bool RemoveKeySHA256FromEntry(const std::string& key,
  36. const base::FilePath& cache_path);
  37. // Modifies the key SHA256 from an entry so that it is corrupt.
  38. bool CorruptKeySHA256FromEntry(const std::string& key,
  39. const base::FilePath& cache_path);
  40. // Modifies the stream 0 length field from an entry so it is invalid.
  41. bool CorruptStream0LengthFromEntry(const std::string& key,
  42. const base::FilePath& cache_path);
  43. } // namespace disk_cache::simple_util
  44. #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_TEST_UTIL_H_