simple_test_util.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #include "net/disk_cache/simple/simple_test_util.h"
  5. #include "base/files/file.h"
  6. #include "base/files/file_path.h"
  7. #include "net/base/hash_value.h"
  8. #include "net/disk_cache/simple/simple_entry_format.h"
  9. #include "net/disk_cache/simple/simple_util.h"
  10. namespace disk_cache::simple_util {
  11. using base::File;
  12. using base::FilePath;
  13. bool CreateCorruptFileForTests(const std::string& key,
  14. const FilePath& cache_path) {
  15. FilePath entry_file_path = cache_path.AppendASCII(
  16. disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0));
  17. int flags = File::FLAG_CREATE_ALWAYS | File::FLAG_WRITE;
  18. File entry_file(entry_file_path, flags);
  19. if (!entry_file.IsValid())
  20. return false;
  21. return entry_file.Write(0, "dummy", 1) == 1;
  22. }
  23. bool RemoveKeySHA256FromEntry(const std::string& key,
  24. const FilePath& cache_path) {
  25. FilePath entry_file_path = cache_path.AppendASCII(
  26. disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0));
  27. int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE;
  28. File entry_file(entry_file_path, flags);
  29. if (!entry_file.IsValid())
  30. return false;
  31. int64_t file_length = entry_file.GetLength();
  32. SimpleFileEOF eof_record;
  33. if (file_length < static_cast<int64_t>(sizeof(eof_record)))
  34. return false;
  35. if (entry_file.Read(file_length - sizeof(eof_record),
  36. reinterpret_cast<char*>(&eof_record),
  37. sizeof(eof_record)) != sizeof(eof_record)) {
  38. return false;
  39. }
  40. if (eof_record.final_magic_number != disk_cache::kSimpleFinalMagicNumber ||
  41. (eof_record.flags & SimpleFileEOF::FLAG_HAS_KEY_SHA256) !=
  42. SimpleFileEOF::FLAG_HAS_KEY_SHA256) {
  43. return false;
  44. }
  45. // Remove the key SHA256 flag, and rewrite the header on top of the
  46. // SHA256. Truncate the file afterwards, and we have an identical entry
  47. // lacking a key SHA256.
  48. eof_record.flags &= ~SimpleFileEOF::FLAG_HAS_KEY_SHA256;
  49. if (entry_file.Write(
  50. file_length - sizeof(eof_record) - sizeof(net::SHA256HashValue),
  51. reinterpret_cast<char*>(&eof_record),
  52. sizeof(eof_record)) != sizeof(eof_record)) {
  53. return false;
  54. }
  55. if (!entry_file.SetLength(file_length - sizeof(net::SHA256HashValue))) {
  56. return false;
  57. }
  58. return true;
  59. }
  60. bool CorruptKeySHA256FromEntry(const std::string& key,
  61. const base::FilePath& cache_path) {
  62. FilePath entry_file_path = cache_path.AppendASCII(
  63. disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0));
  64. int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE;
  65. File entry_file(entry_file_path, flags);
  66. if (!entry_file.IsValid())
  67. return false;
  68. int64_t file_length = entry_file.GetLength();
  69. SimpleFileEOF eof_record;
  70. if (file_length < static_cast<int64_t>(sizeof(eof_record)))
  71. return false;
  72. if (entry_file.Read(file_length - sizeof(eof_record),
  73. reinterpret_cast<char*>(&eof_record),
  74. sizeof(eof_record)) != sizeof(eof_record)) {
  75. return false;
  76. }
  77. if (eof_record.final_magic_number != disk_cache::kSimpleFinalMagicNumber ||
  78. (eof_record.flags & SimpleFileEOF::FLAG_HAS_KEY_SHA256) !=
  79. SimpleFileEOF::FLAG_HAS_KEY_SHA256) {
  80. return false;
  81. }
  82. const char corrupt_data[] = "corrupt data";
  83. static_assert(sizeof(corrupt_data) <= sizeof(net::SHA256HashValue),
  84. "corrupt data should not be larger than a SHA-256");
  85. if (entry_file.Write(
  86. file_length - sizeof(eof_record) - sizeof(net::SHA256HashValue),
  87. corrupt_data, sizeof(corrupt_data)) != sizeof(corrupt_data)) {
  88. return false;
  89. }
  90. return true;
  91. }
  92. bool CorruptStream0LengthFromEntry(const std::string& key,
  93. const base::FilePath& cache_path) {
  94. FilePath entry_file_path = cache_path.AppendASCII(
  95. disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0));
  96. int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE;
  97. File entry_file(entry_file_path, flags);
  98. if (!entry_file.IsValid())
  99. return false;
  100. int64_t file_length = entry_file.GetLength();
  101. SimpleFileEOF eof_record;
  102. if (file_length < static_cast<int64_t>(sizeof(eof_record)))
  103. return false;
  104. if (entry_file.Read(file_length - sizeof(eof_record),
  105. reinterpret_cast<char*>(&eof_record),
  106. sizeof(eof_record)) != sizeof(eof_record)) {
  107. return false;
  108. }
  109. if (eof_record.final_magic_number != disk_cache::kSimpleFinalMagicNumber)
  110. return false;
  111. // Set the stream size to a clearly invalidly large value.
  112. eof_record.stream_size = std::numeric_limits<uint32_t>::max() - 50;
  113. if (entry_file.Write(file_length - sizeof(eof_record),
  114. reinterpret_cast<char*>(&eof_record),
  115. sizeof(eof_record)) != sizeof(eof_record)) {
  116. return false;
  117. }
  118. return true;
  119. }
  120. } // namespace disk_cache::simple_util