Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(584)

Unified Diff: net/disk_cache/simple/simple_test_util.cc

Issue 1977863003: SimpleCache: open with fewer seeks. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@open-speeder-macbetter
Patch Set: actually fix test Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/disk_cache/simple/simple_test_util.h ('k') | net/disk_cache/simple/simple_util.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/disk_cache/simple/simple_test_util.cc
diff --git a/net/disk_cache/simple/simple_test_util.cc b/net/disk_cache/simple/simple_test_util.cc
index 2d3edcb22a16e5576afb8257824ab112b3fed8cb..97982ff10b8d540407c4edd061e0e49c1dcdc667 100644
--- a/net/disk_cache/simple/simple_test_util.cc
+++ b/net/disk_cache/simple/simple_test_util.cc
@@ -6,17 +6,22 @@
#include "base/files/file.h"
#include "base/files/file_path.h"
+#include "net/base/hash_value.h"
+#include "net/disk_cache/simple/simple_entry_format.h"
#include "net/disk_cache/simple/simple_util.h"
namespace disk_cache {
namespace simple_util {
+using base::File;
+using base::FilePath;
+
bool CreateCorruptFileForTests(const std::string& key,
- const base::FilePath& cache_path) {
- base::FilePath entry_file_path = cache_path.AppendASCII(
+ const FilePath& cache_path) {
+ FilePath entry_file_path = cache_path.AppendASCII(
disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0));
- int flags = base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE;
- base::File entry_file(entry_file_path, flags);
+ int flags = File::FLAG_CREATE_ALWAYS | File::FLAG_WRITE;
+ File entry_file(entry_file_path, flags);
if (!entry_file.IsValid())
return false;
@@ -24,5 +29,73 @@ bool CreateCorruptFileForTests(const std::string& key,
return entry_file.Write(0, "dummy", 1) == 1;
}
+bool RemoveKeySHA256FromEntry(const std::string& key,
+ const FilePath& cache_path) {
+ FilePath entry_file_path = cache_path.AppendASCII(
+ disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0));
+ int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE;
+ File entry_file(entry_file_path, flags);
+ if (!entry_file.IsValid())
+ return false;
+ int file_length = entry_file.GetLength();
+ SimpleFileEOF eof_record;
+ if (entry_file.Read(file_length - sizeof(eof_record),
+ reinterpret_cast<char*>(&eof_record),
+ sizeof(eof_record)) != sizeof(eof_record)) {
+ return false;
+ }
+ if (eof_record.final_magic_number != disk_cache::kSimpleFinalMagicNumber ||
+ (eof_record.flags & SimpleFileEOF::FLAG_HAS_KEY_SHA256) !=
+ SimpleFileEOF::FLAG_HAS_KEY_SHA256) {
+ return false;
+ }
+ // Remove the key SHA256 flag, and rewrite the header on top of the
+ // SHA256. Truncate the file afterwards, and we have an identical entry
+ // lacking a key SHA256.
+ eof_record.flags &= ~SimpleFileEOF::FLAG_HAS_KEY_SHA256;
+ if (entry_file.Write(
+ file_length - sizeof(eof_record) - sizeof(net::SHA256HashValue),
+ reinterpret_cast<char*>(&eof_record),
+ sizeof(eof_record)) != sizeof(eof_record)) {
+ return false;
+ }
+ if (!entry_file.SetLength(file_length - sizeof(net::SHA256HashValue))) {
+ return false;
+ }
+ return true;
+}
+
+bool CorruptKeySHA256FromEntry(const std::string& key,
+ const base::FilePath& cache_path) {
+ FilePath entry_file_path = cache_path.AppendASCII(
+ disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0));
+ int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE;
+ File entry_file(entry_file_path, flags);
+ if (!entry_file.IsValid())
+ return false;
+ int file_length = entry_file.GetLength();
+ SimpleFileEOF eof_record;
+ if (entry_file.Read(file_length - sizeof(eof_record),
+ reinterpret_cast<char*>(&eof_record),
+ sizeof(eof_record)) != sizeof(eof_record)) {
+ return false;
+ }
+ if (eof_record.final_magic_number != disk_cache::kSimpleFinalMagicNumber ||
+ (eof_record.flags & SimpleFileEOF::FLAG_HAS_KEY_SHA256) !=
+ SimpleFileEOF::FLAG_HAS_KEY_SHA256) {
+ return false;
+ }
+
+ const char corrupt_data[] = "corrupt data";
+ static_assert(sizeof(corrupt_data) <= sizeof(net::SHA256HashValue),
+ "corrupt data should not be larger than a SHA-256");
+ if (entry_file.Write(
+ file_length - sizeof(eof_record) - sizeof(net::SHA256HashValue),
+ corrupt_data, sizeof(corrupt_data)) != sizeof(corrupt_data)) {
+ return false;
+ }
+ return true;
+}
+
} // namespace simple_util
} // namespace disk_cache
« no previous file with comments | « net/disk_cache/simple/simple_test_util.h ('k') | net/disk_cache/simple/simple_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698