1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/disk_cache/simple/simple_test_util.h"
#include "base/containers/span.h"
#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::simple_util {
using base::File;
using base::FilePath;
bool CreateCorruptFileForTests(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_CREATE_ALWAYS | File::FLAG_WRITE;
File entry_file(entry_file_path, flags);
if (!entry_file.IsValid())
return false;
return entry_file.WriteAndCheck(0, base::byte_span_from_cstring("d"));
}
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;
int64_t file_length = entry_file.GetLength();
SimpleFileEOF eof_record;
if (file_length < static_cast<int64_t>(sizeof(eof_record)))
return false;
if (!entry_file.ReadAndCheck(file_length - sizeof(eof_record),
base::byte_span_from_ref(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.WriteAndCheck(
file_length - sizeof(eof_record) - sizeof(net::SHA256HashValue),
base::byte_span_from_ref(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;
int64_t file_length = entry_file.GetLength();
SimpleFileEOF eof_record;
if (file_length < static_cast<int64_t>(sizeof(eof_record)))
return false;
if (!entry_file.ReadAndCheck(file_length - sizeof(eof_record),
base::byte_span_from_ref(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.WriteAndCheck(
file_length - sizeof(eof_record) - sizeof(net::SHA256HashValue),
base::byte_span_with_nul_from_cstring(corrupt_data))) {
return false;
}
return true;
}
bool CorruptStream0LengthFromEntry(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;
int64_t file_length = entry_file.GetLength();
SimpleFileEOF eof_record;
if (file_length < static_cast<int64_t>(sizeof(eof_record)))
return false;
if (!entry_file.ReadAndCheck(file_length - sizeof(eof_record),
base::byte_span_from_ref(eof_record))) {
return false;
}
if (eof_record.final_magic_number != disk_cache::kSimpleFinalMagicNumber)
return false;
// Set the stream size to a clearly invalidly large value.
eof_record.stream_size = std::numeric_limits<uint32_t>::max() - 50;
if (!entry_file.WriteAndCheck(file_length - sizeof(eof_record),
base::byte_span_from_ref(eof_record))) {
return false;
}
return true;
}
} // namespace disk_cache::simple_util
|