xref: /aosp_15_r20/external/cronet/net/disk_cache/simple/simple_test_util.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/disk_cache/simple/simple_test_util.h"
6 
7 #include "base/files/file.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"
11 #include "net/disk_cache/simple/simple_util.h"
12 
13 namespace disk_cache::simple_util {
14 
15 using base::File;
16 using base::FilePath;
17 
CreateCorruptFileForTests(const std::string & key,const FilePath & cache_path)18 bool CreateCorruptFileForTests(const std::string& key,
19                                const FilePath& cache_path) {
20   FilePath entry_file_path = cache_path.AppendASCII(
21       disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0));
22   int flags = File::FLAG_CREATE_ALWAYS | File::FLAG_WRITE;
23   File entry_file(entry_file_path, flags);
24 
25   if (!entry_file.IsValid())
26     return false;
27 
28   return entry_file.Write(0, "dummy", 1) == 1;
29 }
30 
RemoveKeySHA256FromEntry(const std::string & key,const FilePath & cache_path)31 bool RemoveKeySHA256FromEntry(const std::string& key,
32                               const FilePath& cache_path) {
33   FilePath entry_file_path = cache_path.AppendASCII(
34       disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0));
35   int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE;
36   File entry_file(entry_file_path, flags);
37   if (!entry_file.IsValid())
38     return false;
39   int64_t file_length = entry_file.GetLength();
40   SimpleFileEOF eof_record;
41   if (file_length < static_cast<int64_t>(sizeof(eof_record)))
42     return false;
43   if (entry_file.Read(file_length - sizeof(eof_record),
44                       reinterpret_cast<char*>(&eof_record),
45                       sizeof(eof_record)) != sizeof(eof_record)) {
46     return false;
47   }
48   if (eof_record.final_magic_number != disk_cache::kSimpleFinalMagicNumber ||
49       (eof_record.flags & SimpleFileEOF::FLAG_HAS_KEY_SHA256) !=
50           SimpleFileEOF::FLAG_HAS_KEY_SHA256) {
51     return false;
52   }
53   // Remove the key SHA256 flag, and rewrite the header on top of the
54   // SHA256. Truncate the file afterwards, and we have an identical entry
55   // lacking a key SHA256.
56   eof_record.flags &= ~SimpleFileEOF::FLAG_HAS_KEY_SHA256;
57   if (entry_file.Write(
58           file_length - sizeof(eof_record) - sizeof(net::SHA256HashValue),
59           reinterpret_cast<char*>(&eof_record),
60           sizeof(eof_record)) != sizeof(eof_record)) {
61     return false;
62   }
63   if (!entry_file.SetLength(file_length - sizeof(net::SHA256HashValue))) {
64     return false;
65   }
66   return true;
67 }
68 
CorruptKeySHA256FromEntry(const std::string & key,const base::FilePath & cache_path)69 bool CorruptKeySHA256FromEntry(const std::string& key,
70                                const base::FilePath& cache_path) {
71   FilePath entry_file_path = cache_path.AppendASCII(
72       disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0));
73   int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE;
74   File entry_file(entry_file_path, flags);
75   if (!entry_file.IsValid())
76     return false;
77   int64_t file_length = entry_file.GetLength();
78   SimpleFileEOF eof_record;
79   if (file_length < static_cast<int64_t>(sizeof(eof_record)))
80     return false;
81   if (entry_file.Read(file_length - sizeof(eof_record),
82                       reinterpret_cast<char*>(&eof_record),
83                       sizeof(eof_record)) != sizeof(eof_record)) {
84     return false;
85   }
86   if (eof_record.final_magic_number != disk_cache::kSimpleFinalMagicNumber ||
87       (eof_record.flags & SimpleFileEOF::FLAG_HAS_KEY_SHA256) !=
88           SimpleFileEOF::FLAG_HAS_KEY_SHA256) {
89     return false;
90   }
91 
92   const char corrupt_data[] = "corrupt data";
93   static_assert(sizeof(corrupt_data) <= sizeof(net::SHA256HashValue),
94                 "corrupt data should not be larger than a SHA-256");
95   if (entry_file.Write(
96           file_length - sizeof(eof_record) - sizeof(net::SHA256HashValue),
97           corrupt_data, sizeof(corrupt_data)) != sizeof(corrupt_data)) {
98     return false;
99   }
100   return true;
101 }
102 
CorruptStream0LengthFromEntry(const std::string & key,const base::FilePath & cache_path)103 bool CorruptStream0LengthFromEntry(const std::string& key,
104                                    const base::FilePath& cache_path) {
105   FilePath entry_file_path = cache_path.AppendASCII(
106       disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0));
107   int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE;
108   File entry_file(entry_file_path, flags);
109   if (!entry_file.IsValid())
110     return false;
111   int64_t file_length = entry_file.GetLength();
112   SimpleFileEOF eof_record;
113   if (file_length < static_cast<int64_t>(sizeof(eof_record)))
114     return false;
115   if (entry_file.Read(file_length - sizeof(eof_record),
116                       reinterpret_cast<char*>(&eof_record),
117                       sizeof(eof_record)) != sizeof(eof_record)) {
118     return false;
119   }
120   if (eof_record.final_magic_number != disk_cache::kSimpleFinalMagicNumber)
121     return false;
122 
123   // Set the stream size to a clearly invalidly large value.
124   eof_record.stream_size = std::numeric_limits<uint32_t>::max() - 50;
125   if (entry_file.Write(file_length - sizeof(eof_record),
126                        reinterpret_cast<char*>(&eof_record),
127                        sizeof(eof_record)) != sizeof(eof_record)) {
128     return false;
129   }
130   return true;
131 }
132 
133 }  // namespace disk_cache::simple_util
134