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" | 9 #include "net/base/hash_value.h" |
10 #include "net/disk_cache/simple/simple_entry_format.h" | 10 #include "net/disk_cache/simple/simple_entry_format.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 bool RemoveKeySHA256FromEntry(const std::string& key, | 32 bool RemoveKeySHA256FromEntry(const std::string& key, |
33 const FilePath& cache_path) { | 33 const FilePath& cache_path) { |
34 FilePath entry_file_path = cache_path.AppendASCII( | 34 FilePath entry_file_path = cache_path.AppendASCII( |
35 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0)); | 35 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0)); |
36 int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE; | 36 int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE; |
37 File entry_file(entry_file_path, flags); | 37 File entry_file(entry_file_path, flags); |
38 if (!entry_file.IsValid()) | 38 if (!entry_file.IsValid()) |
39 return false; | 39 return false; |
40 int file_length = entry_file.GetLength(); | 40 int file_length = entry_file.GetLength(); |
41 SimpleFileEOF eof_record; | 41 SimpleFileEOF eof_record; |
| 42 if (file_length < sizeof(eof_record)) |
| 43 return false; |
42 if (entry_file.Read(file_length - sizeof(eof_record), | 44 if (entry_file.Read(file_length - sizeof(eof_record), |
43 reinterpret_cast<char*>(&eof_record), | 45 reinterpret_cast<char*>(&eof_record), |
44 sizeof(eof_record)) != sizeof(eof_record)) { | 46 sizeof(eof_record)) != sizeof(eof_record)) { |
45 return false; | 47 return false; |
46 } | 48 } |
47 if (eof_record.final_magic_number != disk_cache::kSimpleFinalMagicNumber || | 49 if (eof_record.final_magic_number != disk_cache::kSimpleFinalMagicNumber || |
48 (eof_record.flags & SimpleFileEOF::FLAG_HAS_KEY_SHA256) != | 50 (eof_record.flags & SimpleFileEOF::FLAG_HAS_KEY_SHA256) != |
49 SimpleFileEOF::FLAG_HAS_KEY_SHA256) { | 51 SimpleFileEOF::FLAG_HAS_KEY_SHA256) { |
50 return false; | 52 return false; |
51 } | 53 } |
(...skipping 14 matching lines...) Expand all Loading... |
66 } | 68 } |
67 | 69 |
68 bool CorruptKeySHA256FromEntry(const std::string& key, | 70 bool CorruptKeySHA256FromEntry(const std::string& key, |
69 const base::FilePath& cache_path) { | 71 const base::FilePath& cache_path) { |
70 FilePath entry_file_path = cache_path.AppendASCII( | 72 FilePath entry_file_path = cache_path.AppendASCII( |
71 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0)); | 73 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0)); |
72 int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE; | 74 int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE; |
73 File entry_file(entry_file_path, flags); | 75 File entry_file(entry_file_path, flags); |
74 if (!entry_file.IsValid()) | 76 if (!entry_file.IsValid()) |
75 return false; | 77 return false; |
76 int file_length = entry_file.GetLength(); | 78 int64_t file_length = entry_file.GetLength(); |
77 SimpleFileEOF eof_record; | 79 SimpleFileEOF eof_record; |
| 80 if (file_length < sizeof(eof_record)) |
| 81 return false; |
78 if (entry_file.Read(file_length - sizeof(eof_record), | 82 if (entry_file.Read(file_length - sizeof(eof_record), |
79 reinterpret_cast<char*>(&eof_record), | 83 reinterpret_cast<char*>(&eof_record), |
80 sizeof(eof_record)) != sizeof(eof_record)) { | 84 sizeof(eof_record)) != sizeof(eof_record)) { |
81 return false; | 85 return false; |
82 } | 86 } |
83 if (eof_record.final_magic_number != disk_cache::kSimpleFinalMagicNumber || | 87 if (eof_record.final_magic_number != disk_cache::kSimpleFinalMagicNumber || |
84 (eof_record.flags & SimpleFileEOF::FLAG_HAS_KEY_SHA256) != | 88 (eof_record.flags & SimpleFileEOF::FLAG_HAS_KEY_SHA256) != |
85 SimpleFileEOF::FLAG_HAS_KEY_SHA256) { | 89 SimpleFileEOF::FLAG_HAS_KEY_SHA256) { |
86 return false; | 90 return false; |
87 } | 91 } |
(...skipping 10 matching lines...) Expand all Loading... |
98 } | 102 } |
99 | 103 |
100 bool CorruptStream0LengthFromEntry(const std::string& key, | 104 bool CorruptStream0LengthFromEntry(const std::string& key, |
101 const base::FilePath& cache_path) { | 105 const base::FilePath& cache_path) { |
102 FilePath entry_file_path = cache_path.AppendASCII( | 106 FilePath entry_file_path = cache_path.AppendASCII( |
103 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0)); | 107 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0)); |
104 int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE; | 108 int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE; |
105 File entry_file(entry_file_path, flags); | 109 File entry_file(entry_file_path, flags); |
106 if (!entry_file.IsValid()) | 110 if (!entry_file.IsValid()) |
107 return false; | 111 return false; |
108 int file_length = entry_file.GetLength(); | 112 int64_t file_length = entry_file.GetLength(); |
109 SimpleFileEOF eof_record; | 113 SimpleFileEOF eof_record; |
| 114 if (file_length < sizeof(eof_record)) |
| 115 return false; |
110 if (entry_file.Read(file_length - sizeof(eof_record), | 116 if (entry_file.Read(file_length - sizeof(eof_record), |
111 reinterpret_cast<char*>(&eof_record), | 117 reinterpret_cast<char*>(&eof_record), |
112 sizeof(eof_record)) != sizeof(eof_record)) { | 118 sizeof(eof_record)) != sizeof(eof_record)) { |
113 return false; | 119 return false; |
114 } | 120 } |
115 if (eof_record.final_magic_number != disk_cache::kSimpleFinalMagicNumber) | 121 if (eof_record.final_magic_number != disk_cache::kSimpleFinalMagicNumber) |
116 return false; | 122 return false; |
117 | 123 |
118 // Set the stream size to a clearly invalidly large value. | 124 // Set the stream size to a clearly invalidly large value. |
119 eof_record.stream_size = std::numeric_limits<uint32_t>::max() - 50; | 125 eof_record.stream_size = std::numeric_limits<uint32_t>::max() - 50; |
120 if (entry_file.Write(file_length - sizeof(eof_record), | 126 if (entry_file.Write(file_length - sizeof(eof_record), |
121 reinterpret_cast<char*>(&eof_record), | 127 reinterpret_cast<char*>(&eof_record), |
122 sizeof(eof_record)) != sizeof(eof_record)) { | 128 sizeof(eof_record)) != sizeof(eof_record)) { |
123 return false; | 129 return false; |
124 } | 130 } |
125 return true; | 131 return true; |
126 } | 132 } |
127 | 133 |
128 } // namespace simple_util | 134 } // namespace simple_util |
129 } // namespace disk_cache | 135 } // namespace disk_cache |
OLD | NEW |