OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/disk_cache/simple/simple_test_util.h" | 5 #include "net/disk_cache/simple/simple_test_util.h" |
6 | 6 |
7 #include "base/files/file.h" | 7 #include "base/files/file.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "net/base/hash_value.h" |
| 10 #include "net/disk_cache/simple/simple_entry_format.h" |
9 #include "net/disk_cache/simple/simple_util.h" | 11 #include "net/disk_cache/simple/simple_util.h" |
10 | 12 |
11 namespace disk_cache { | 13 namespace disk_cache { |
12 namespace simple_util { | 14 namespace simple_util { |
13 | 15 |
| 16 using base::File; |
| 17 using base::FilePath; |
| 18 |
14 bool CreateCorruptFileForTests(const std::string& key, | 19 bool CreateCorruptFileForTests(const std::string& key, |
15 const base::FilePath& cache_path) { | 20 const FilePath& cache_path) { |
16 base::FilePath entry_file_path = cache_path.AppendASCII( | 21 FilePath entry_file_path = cache_path.AppendASCII( |
17 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0)); | 22 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0)); |
18 int flags = base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE; | 23 int flags = File::FLAG_CREATE_ALWAYS | File::FLAG_WRITE; |
19 base::File entry_file(entry_file_path, flags); | 24 File entry_file(entry_file_path, flags); |
20 | 25 |
21 if (!entry_file.IsValid()) | 26 if (!entry_file.IsValid()) |
22 return false; | 27 return false; |
23 | 28 |
24 return entry_file.Write(0, "dummy", 1) == 1; | 29 return entry_file.Write(0, "dummy", 1) == 1; |
25 } | 30 } |
26 | 31 |
| 32 bool RemoveKeySHA256FromEntry(const std::string& key, |
| 33 const FilePath& cache_path) { |
| 34 FilePath entry_file_path = cache_path.AppendASCII( |
| 35 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0)); |
| 36 int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE; |
| 37 File entry_file(entry_file_path, flags); |
| 38 if (!entry_file.IsValid()) |
| 39 return false; |
| 40 int file_length = entry_file.GetLength(); |
| 41 SimpleFileEOF eof_record; |
| 42 if (entry_file.Read(file_length - sizeof(eof_record), |
| 43 reinterpret_cast<char*>(&eof_record), |
| 44 sizeof(eof_record)) != sizeof(eof_record)) { |
| 45 return false; |
| 46 } |
| 47 if (eof_record.final_magic_number != disk_cache::kSimpleFinalMagicNumber || |
| 48 (eof_record.flags & SimpleFileEOF::FLAG_HAS_KEY_SHA256) != |
| 49 SimpleFileEOF::FLAG_HAS_KEY_SHA256) { |
| 50 return false; |
| 51 } |
| 52 // Remove the key SHA256 flag, and rewrite the header on top of the |
| 53 // SHA256. Truncate the file afterwards, and we have an identical entry |
| 54 // lacking a key SHA256. |
| 55 eof_record.flags &= ~SimpleFileEOF::FLAG_HAS_KEY_SHA256; |
| 56 if (entry_file.Write( |
| 57 file_length - sizeof(eof_record) - sizeof(net::SHA256HashValue), |
| 58 reinterpret_cast<char*>(&eof_record), |
| 59 sizeof(eof_record)) != sizeof(eof_record)) { |
| 60 return false; |
| 61 } |
| 62 if (!entry_file.SetLength(file_length - sizeof(net::SHA256HashValue))) { |
| 63 return false; |
| 64 } |
| 65 return true; |
| 66 } |
| 67 |
| 68 bool CorruptKeySHA256FromEntry(const std::string& key, |
| 69 const base::FilePath& cache_path) { |
| 70 FilePath entry_file_path = cache_path.AppendASCII( |
| 71 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0)); |
| 72 int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE; |
| 73 File entry_file(entry_file_path, flags); |
| 74 if (!entry_file.IsValid()) |
| 75 return false; |
| 76 int file_length = entry_file.GetLength(); |
| 77 SimpleFileEOF eof_record; |
| 78 if (entry_file.Read(file_length - sizeof(eof_record), |
| 79 reinterpret_cast<char*>(&eof_record), |
| 80 sizeof(eof_record)) != sizeof(eof_record)) { |
| 81 return false; |
| 82 } |
| 83 if (eof_record.final_magic_number != disk_cache::kSimpleFinalMagicNumber || |
| 84 (eof_record.flags & SimpleFileEOF::FLAG_HAS_KEY_SHA256) != |
| 85 SimpleFileEOF::FLAG_HAS_KEY_SHA256) { |
| 86 return false; |
| 87 } |
| 88 |
| 89 const char corrupt_data[] = "corrupt data"; |
| 90 static_assert(sizeof(corrupt_data) <= sizeof(net::SHA256HashValue), |
| 91 "corrupt data should not be larger than a SHA-256"); |
| 92 if (entry_file.Write( |
| 93 file_length - sizeof(eof_record) - sizeof(net::SHA256HashValue), |
| 94 corrupt_data, sizeof(corrupt_data)) != sizeof(corrupt_data)) { |
| 95 return false; |
| 96 } |
| 97 return true; |
| 98 } |
| 99 |
27 } // namespace simple_util | 100 } // namespace simple_util |
28 } // namespace disk_cache | 101 } // namespace disk_cache |
OLD | NEW |