Debian Codegraph @

Source/UnitTests/Common/FileUtilTest.cpp

raw symbols SCIP
1 // Copyright 2020 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include <algorithm>
5 #include <array>
6 #include <latch>
7 #include <thread>
8
9 #include <gtest/gtest.h>
10
11 #include "Common/BitUtils.h"
12 #include "Common/DirectIOFile.h"
13 #include "Common/FileUtil.h"
14
15 class FileUtilTest : public testing::Test
16 {
17 protected:
18 FileUtilTest()
19 : m_parent_directory(File::CreateTempDir()), m_file_path(m_parent_directory + "/file.txt"),
20 m_directory_path(m_parent_directory + "/dir"),
21 m_invalid_path(m_parent_directory + "/invalid.txt")
22 {
23 }
24
25 ~FileUtilTest() override
26 {
27 if (!m_parent_directory.empty())
28 {
29 File::DeleteDirRecursively(m_parent_directory);
30 }
31 }
32
33 void SetUp() override
34 {
35 if (m_parent_directory.empty())
36 {
37 FAIL();
38 }
39 }
40
41 constexpr static std::array<File::IfAbsentBehavior, 2> s_warning_behaviors = {
42 File::IfAbsentBehavior::ConsoleWarning,
43 File::IfAbsentBehavior::NoConsoleWarning,
44 };
45
46 const std::string m_parent_directory;
47 const std::string m_file_path;
48 const std::string m_directory_path;
49 const std::string m_invalid_path;
50 };
51
52 static void DeleteShouldNotRemoveDirectory(const std::string& path, File::IfAbsentBehavior behavior)
53 {
54 File::CreateDir(path);
55 EXPECT_FALSE(File::Delete(path, behavior));
56 File::DeleteDir(path, behavior);
57 }
58
59 static void DeleteShouldRemoveFile(const std::string& path, File::IfAbsentBehavior behavior)
60 {
61 File::CreateEmptyFile(path);
62 EXPECT_TRUE(File::Delete(path, behavior));
63 }
64
65 static void DeleteShouldReturnTrueForInvalidPath(const std::string& path,
66 File::IfAbsentBehavior behavior)
67 {
68 EXPECT_TRUE(File::Delete(path, behavior));
69 }
70
71 static void DeleteDirShouldRemoveDirectory(const std::string& path, File::IfAbsentBehavior behavior)
72 {
73 File::CreateDir(path);
74 EXPECT_TRUE(File::DeleteDir(path, behavior));
75 }
76
77 static void DeleteDirShouldNotRemoveFile(const std::string& path, File::IfAbsentBehavior behavior)
78 {
79 File::CreateEmptyFile(path);
80 EXPECT_FALSE(File::DeleteDir(path, behavior));
81 File::Delete(path, behavior);
82 }
83
84 static void DeleteDirShouldReturnTrueForInvalidPath(const std::string& path,
85 File::IfAbsentBehavior behavior)
86 {
87 EXPECT_TRUE(File::DeleteDir(path, behavior));
88 }
89
90 TEST_F(FileUtilTest, Delete)
91 {
92 for (const auto behavior : s_warning_behaviors)
93 {
94 DeleteShouldNotRemoveDirectory(m_directory_path, behavior);
95 DeleteShouldRemoveFile(m_file_path, behavior);
96 DeleteShouldReturnTrueForInvalidPath(m_invalid_path, behavior);
97 }
98 }
99
100 TEST_F(FileUtilTest, DeleteDir)
101 {
102 for (const auto behavior : s_warning_behaviors)
103 {
104 DeleteDirShouldRemoveDirectory(m_directory_path, behavior);
105 DeleteDirShouldNotRemoveFile(m_file_path, behavior);
106 DeleteDirShouldReturnTrueForInvalidPath(m_invalid_path, behavior);
107 }
108 }
109
110 TEST_F(FileUtilTest, CreateFullPath)
111 {
112 ASSERT_TRUE(!m_directory_path.ends_with('/'));
113 File::CreateDir(m_directory_path);
114
115 // should try to create the directory at m_directory_path, which already exists
116 EXPECT_TRUE(File::IsDirectory(m_directory_path));
117 EXPECT_TRUE(File::CreateFullPath(m_directory_path + "/"));
118
119 // should create the directory (one level)
120 std::string p1 = m_directory_path + "/p1";
121 EXPECT_TRUE(!File::Exists(p1));
122 EXPECT_TRUE(File::CreateFullPath(p1 + "/"));
123 EXPECT_TRUE(File::IsDirectory(p1));
124
125 // should create the directories (multiple levels)
126 std::string p2 = m_directory_path + "/p2";
127 std::string p2a = m_directory_path + "/p2/a";
128 std::string p2b = m_directory_path + "/p2/a/b";
129 std::string p2c = m_directory_path + "/p2/a/b/c";
130 EXPECT_TRUE(!File::Exists(p2));
131 EXPECT_TRUE(!File::Exists(p2a));
132 EXPECT_TRUE(!File::Exists(p2b));
133 EXPECT_TRUE(!File::Exists(p2c));
134 EXPECT_TRUE(File::CreateFullPath(p2c + "/"));
135 EXPECT_TRUE(File::IsDirectory(p2));
136 EXPECT_TRUE(File::IsDirectory(p2a));
137 EXPECT_TRUE(File::IsDirectory(p2b));
138 EXPECT_TRUE(File::IsDirectory(p2c));
139
140 // if the given path ends in a file, the file should be ignored
141 std::string p3 = m_directory_path + "/p3";
142 std::string p3file = m_directory_path + "/p3/test.bin";
143 EXPECT_TRUE(!File::Exists(p3));
144 EXPECT_TRUE(!File::Exists(p3file));
145 EXPECT_TRUE(File::CreateFullPath(p3file));
146 EXPECT_TRUE(File::IsDirectory(p3));
147 EXPECT_TRUE(!File::Exists(p3file));
148
149 // if we try to create a directory where a file already exists, we expect it to fail and leave the
150 // file alone
151 EXPECT_TRUE(File::CreateEmptyFile(p3file));
152 EXPECT_TRUE(File::IsFile(p3file));
153 EXPECT_FALSE(File::CreateFullPath(p3file + "/"));
154 EXPECT_TRUE(File::IsFile(p3file));
155 }
156
157 TEST_F(FileUtilTest, DirectIOFile)
158 {
159 static constexpr std::array<u8, 3> u8_test_data = {42, 7, 99};
160 static constexpr std::array<u16, 3> u16_test_data = {0xdead, 0xbeef, 0xf00d};
161
162 static constexpr int u16_data_offset = 73;
163
164 File::DirectIOFile file;
165 EXPECT_FALSE(file.IsOpen());
166
167 // Read mode fails with a non-existing file.
168 EXPECT_FALSE(file.Open(m_file_path, File::AccessMode::Read));
169
170 std::array<u8, u8_test_data.size()> u8_buffer = {};
171
172 // Everything fails when a file isn't open.
173 EXPECT_FALSE(file.Write(u8_buffer));
174 EXPECT_FALSE(file.Read(u8_buffer));
175 EXPECT_FALSE(file.Flush());
176 EXPECT_FALSE(file.Seek(12, File::SeekOrigin::Begin));
177 EXPECT_EQ(file.Tell(), 0);
178 EXPECT_EQ(file.GetSize(), 0);
179
180 // Fail to open non-existing file.
181 EXPECT_FALSE(file.Open(m_file_path, File::AccessMode::ReadAndWrite));
182 EXPECT_FALSE(file.Open(m_file_path, File::AccessMode::Read, File::OpenMode::Existing));
183 EXPECT_FALSE(file.Open(m_file_path, File::AccessMode::Write, File::OpenMode::Existing));
184
185 // Read mode can create files
186 EXPECT_TRUE(file.Open(m_file_path, File::AccessMode::Read, File::OpenMode::Create));
187 EXPECT_TRUE(file.Close());
188
189 // Open a file for writing.
190 EXPECT_TRUE(file.Open(m_file_path, File::AccessMode::Write, File::OpenMode::Always));
191 EXPECT_TRUE(file.Flush());
192 EXPECT_TRUE(file.IsOpen());
193
194 // Note: Double Open() currently ASSERTs. It's not obvious if that should succeed or fail.
195
196 EXPECT_TRUE(file.Close());
197 EXPECT_FALSE(file.Open(m_file_path, File::AccessMode::Write, File::OpenMode::Create));
198
199 EXPECT_TRUE(file.Open(m_file_path, File::AccessMode::Write, File::OpenMode::Existing));
200 EXPECT_TRUE(file.Close());
201
202 // Rename, Resize, and Delete fail with a closed handle.
203 EXPECT_FALSE(Rename(file, m_file_path, "fail"));
204 EXPECT_FALSE(Resize(file, 72));
205 EXPECT_FALSE(Delete(file, m_file_path));
206 EXPECT_TRUE(File::Exists(m_file_path));
207
208 EXPECT_TRUE(file.Open(m_file_path, File::AccessMode::Write));
209
210 EXPECT_TRUE(file.Write(u8_buffer.data(), 0)); // write of 0 succeeds.
211 EXPECT_FALSE(file.Read(u8_buffer.data(), 0)); // read of 0 (in write mode) fails.
212
213 // Resize through handle works.
214 EXPECT_TRUE(Resize(file, 1));
215 EXPECT_EQ(file.GetSize(), 1);
216 file.Close();
217
218 // Write truncates by default.
219 EXPECT_TRUE(file.Open(m_file_path, File::AccessMode::Write));
220 EXPECT_EQ(file.GetSize(), 0);
221
222 EXPECT_TRUE(file.Write(u8_test_data));
223 EXPECT_EQ(file.GetSize(), u8_test_data.size()); // size changed
224 EXPECT_EQ(file.Tell(), u8_test_data.size()); // file position changed
225
226 // Relative seek works.
227 EXPECT_TRUE(file.Seek(0, File::SeekOrigin::End));
228 EXPECT_EQ(file.Tell(), file.GetSize());
229 EXPECT_TRUE(file.Seek(-int(u8_test_data.size()), File::SeekOrigin::Current));
230 EXPECT_EQ(file.Tell(), 0);
231
232 // Read while in "write mode" fails
233 EXPECT_FALSE(file.Read(u8_buffer));
234 EXPECT_EQ(file.Tell(), 0);
235
236 // Seeking past the end works.
237 EXPECT_TRUE(file.Seek(u16_data_offset, File::SeekOrigin::Begin));
238 EXPECT_EQ(file.Tell(), u16_data_offset);
239 EXPECT_EQ(file.GetSize(), u8_test_data.size()); // no change in size
240
241 static constexpr u64 final_file_size = u16_data_offset + sizeof(u16_test_data);
242
243 // Size changes after write.
244 EXPECT_TRUE(file.Write(Common::AsU8Span(u16_test_data)));
245 EXPECT_EQ(file.GetSize(), final_file_size);
246
247 // Seek before begin fails.
248 EXPECT_FALSE(file.Seek(-1, File::SeekOrigin::Begin));
249 EXPECT_EQ(file.Tell(), file.GetSize()); // unchanged position
250
251 EXPECT_TRUE(file.Close());
252 EXPECT_FALSE(file.IsOpen());
253
254 EXPECT_EQ(file.Tell(), 0);
255 EXPECT_EQ(file.GetSize(), 0);
256
257 EXPECT_FALSE(file.Close()); // Double close fails.
258
259 // Open file for reading.
260 EXPECT_TRUE(file.Open(m_file_path, File::AccessMode::Read, File::OpenMode::Always));
261 EXPECT_TRUE(file.Close());
262 EXPECT_TRUE(file.Open(m_file_path, File::AccessMode::Read));
263
264 static constexpr int big_offset = 999;
265
266 // We can seek beyond the end.
267 EXPECT_TRUE(file.Seek(big_offset, File::SeekOrigin::End));
268 EXPECT_EQ(file.Tell(), file.GetSize() + big_offset);
269
270 // Resize through handle fails when in read mode.
271 EXPECT_FALSE(Resize(file, big_offset));
272 EXPECT_EQ(file.GetSize(), final_file_size);
273
274 constexpr size_t thread_count = 4;
275
276 File::DirectIOFile closed_file;
277
278 std::latch do_reads{1};
279
280 EXPECT_TRUE(file.Seek(u16_data_offset, File::SeekOrigin::Begin));
281
282 // Concurrent access with copied handles works.
283 std::vector<std::thread> threads(thread_count);
284 for (auto& t : threads)
285 {
286 t = std::thread{[&do_reads, file, closed_file]() mutable {
287 EXPECT_FALSE(closed_file.IsOpen()); // a copied closed file is still closed
288
289 EXPECT_EQ(file.Tell(), u16_data_offset); // current position is copied
290
291 std::array<u8, 1> one_byte{};
292
293 constexpr u64 small_offset = 3;
294
295 do_reads.wait();
296
297 EXPECT_TRUE(file.Seek(small_offset, File::SeekOrigin::Begin));
298 EXPECT_EQ(file.Tell(), small_offset);
299
300 // writes fail in read mode.
301 EXPECT_FALSE(file.Write(u8_test_data));
302 EXPECT_FALSE(file.OffsetWrite(0, u8_test_data.data(), 0));
303 EXPECT_EQ(file.Tell(), small_offset);
304
305 EXPECT_TRUE(file.Read(one_byte.data(), 0)); // read of zero succeeds.
306 EXPECT_EQ(file.Tell(), small_offset); // unchanged position
307
308 // Reading at an explicit offset doesn't change the position
309 EXPECT_TRUE(file.OffsetRead(u8_test_data.size() - 1, one_byte));
310 EXPECT_EQ(file.Tell(), small_offset);
311 EXPECT_EQ(one_byte[0], u8_test_data.back());
312
313 EXPECT_EQ(file.GetSize(), final_file_size);
314 EXPECT_TRUE(file.Seek(-int(sizeof(u16_test_data)), File::SeekOrigin::End));
315
316 // We can't read beyond the end of the file.
317 EXPECT_FALSE(file.OffsetRead(big_offset, one_byte));
318 // A read of zero beyond the end works.
319 EXPECT_TRUE(file.OffsetRead(big_offset, one_byte.data(), 0));
320
321 // Reading the previously written data works.
322 std::array<u16, u16_test_data.size()> u16_buffer = {};
323 EXPECT_TRUE(file.Read(Common::AsWritableU8Span(u16_buffer)));
324 EXPECT_TRUE(std::ranges::equal(u16_buffer, u16_test_data));
325
326 // We can't read at the end of the file.
327 EXPECT_FALSE(file.Read(one_byte));
328 EXPECT_EQ(file.Tell(), file.GetSize()); // The position is unchanged.
329 }};
330 }
331
332 // We may close the file on our end before the other threads use it.
333 file.Close();
334
335 // ReadAndWrite does not truncate existing files.
336 EXPECT_TRUE(file.Open(m_file_path, File::AccessMode::ReadAndWrite));
337 EXPECT_EQ(file.GetSize(), final_file_size);
338 file.Close();
339
340 // Write doesn't truncate when the mode is changed.
341 EXPECT_TRUE(file.Open(m_file_path, File::AccessMode::Write, File::OpenMode::Existing));
342 EXPECT_EQ(file.GetSize(), final_file_size);
343
344 // Open a new handle to the same file.
345 File::DirectIOFile second_handle(m_file_path, File::AccessMode::Write, File::OpenMode::Always);
346 EXPECT_TRUE(second_handle.IsOpen());
347
348 const std::string destination_path_1 = m_file_path + ".dest_1";
349
350 // Rename through handle works when opened elsewhere.
351 EXPECT_TRUE(Rename(file, m_file_path, destination_path_1));
352 EXPECT_FALSE(File::Exists(m_file_path));
353 EXPECT_TRUE(File::Exists(destination_path_1));
354
355 const std::string destination_path_2 = m_file_path + ".dest_2";
356
357 // Note: Windows fails the next `Rename` if this file is kept open.
358 // I don't know if there is a nice way to make that work.
359 {
360 File::DirectIOFile another_file{destination_path_2, File::AccessMode::Write};
361 EXPECT_TRUE(another_file.IsOpen());
362 }
363
364 // Rename overwrites existing files.
365 EXPECT_TRUE(Rename(file, destination_path_1, destination_path_2));
366 EXPECT_FALSE(File::Exists(destination_path_1));
367 EXPECT_EQ(File::GetSize(destination_path_2), final_file_size);
368
369 const std::string destination_path_3 = m_file_path + ".dest_3";
370
371 // Truncate fails in Read mode.
372 File::Copy(destination_path_2, destination_path_3);
373 EXPECT_EQ(File::GetSize(destination_path_3), final_file_size);
374 {
375 File::DirectIOFile tmp{destination_path_3, File::AccessMode::Read, File::OpenMode::Truncate};
376 EXPECT_FALSE(tmp.IsOpen());
377 EXPECT_EQ(File::GetSize(destination_path_3), final_file_size);
378 }
379
380 // Truncate works with ReadAndWrite.
381 EXPECT_EQ(File::GetSize(destination_path_3), final_file_size);
382 {
383 File::DirectIOFile tmp{destination_path_3, File::AccessMode::ReadAndWrite,
384 File::OpenMode::Truncate};
385 EXPECT_EQ(tmp.GetSize(), 0);
386 Delete(tmp, destination_path_3);
387 }
388
389 // Truncate works with Write.
390 File::Copy(destination_path_2, destination_path_3);
391 EXPECT_EQ(File::GetSize(destination_path_3), final_file_size);
392 {
393 File::DirectIOFile tmp{destination_path_3, File::AccessMode::Write, File::OpenMode::Truncate};
394 EXPECT_EQ(tmp.GetSize(), 0);
395 Delete(tmp, destination_path_3);
396 }
397
398 // Delete through handle works.
399 EXPECT_TRUE(Delete(file, destination_path_2));
400 EXPECT_FALSE(File::Exists(destination_path_2));
401
402 // The threads can read even after deleting everything.
403 do_reads.count_down();
404 std::ranges::for_each(threads, &std::thread::join);
405
406 file.Close();
407 EXPECT_TRUE(file.Open(destination_path_1, File::AccessMode::Read, File::OpenMode::Always));
408
409 // Required on Windows for the below to succeed.
410 second_handle.Close();
411
412 file.Close();
413 EXPECT_TRUE(file.Open(destination_path_2, File::AccessMode::Write, File::OpenMode::Always));
414 }