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 eof_record.flags &= ~SimpleFileEOF::FLAG_HAS_KEY_SHA256; | |
53 if (entry_file.Write( | |
54 file_length - sizeof(eof_record) - sizeof(net::SHA256HashValue), | |
Randy Smith (Not in Mondays)
2016/05/19 19:38:50
Comment calling out that record is being moved bac
gavinp
2016/05/20 01:42:32
Done.
| |
55 reinterpret_cast<char*>(&eof_record), | |
56 sizeof(eof_record)) != sizeof(eof_record)) { | |
57 return false; | |
58 } | |
59 if (!entry_file.SetLength(file_length - sizeof(net::SHA256HashValue))) { | |
60 return false; | |
61 } | |
62 return true; | |
63 } | |
64 | |
65 bool CorruptKeySHA256FromEntry(const std::string& key, | |
66 const base::FilePath& cache_path) { | |
67 FilePath entry_file_path = cache_path.AppendASCII( | |
68 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0)); | |
69 int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE; | |
70 File entry_file(entry_file_path, flags); | |
71 if (!entry_file.IsValid()) | |
72 return false; | |
73 int file_length = entry_file.GetLength(); | |
74 SimpleFileEOF eof_record; | |
75 if (entry_file.Read(file_length - sizeof(eof_record), | |
76 reinterpret_cast<char*>(&eof_record), | |
77 sizeof(eof_record)) != sizeof(eof_record)) { | |
78 return false; | |
79 } | |
80 if (eof_record.final_magic_number != disk_cache::kSimpleFinalMagicNumber || | |
81 (eof_record.flags & SimpleFileEOF::FLAG_HAS_KEY_SHA256) != | |
82 SimpleFileEOF::FLAG_HAS_KEY_SHA256) { | |
83 return false; | |
84 } | |
85 | |
86 const char corrupt_data[] = "corrupt data"; | |
87 static_assert(sizeof(corrupt_data) <= sizeof(net::SHA256HashValue), | |
88 "corrupt data should not be larger than a SHA-256"); | |
89 if (entry_file.Write( | |
90 file_length - sizeof(eof_record) - sizeof(net::SHA256HashValue), | |
91 corrupt_data, sizeof(corrupt_data)) != sizeof(corrupt_data)) { | |
92 return false; | |
93 } | |
94 return true; | |
95 } | |
96 | |
27 } // namespace simple_util | 97 } // namespace simple_util |
28 } // namespace disk_cache | 98 } // namespace disk_cache |
OLD | NEW |