summaryrefslogtreecommitdiff
path: root/Source/UnitTests/Common/FileUtilTest.cpp
blob: bfdc9128f6b01ef6cbb30bfd1d91e49516ca428d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Copyright 2020 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <algorithm>
#include <array>
#include <latch>
#include <thread>

#include <gtest/gtest.h>

#include "Common/BitUtils.h"
#include "Common/DirectIOFile.h"
#include "Common/FileUtil.h"

class FileUtilTest : public testing::Test
{
protected:
  FileUtilTest()
      : m_parent_directory(File::CreateTempDir()), m_file_path(m_parent_directory + "/file.txt"),
        m_directory_path(m_parent_directory + "/dir"),
        m_invalid_path(m_parent_directory + "/invalid.txt")
  {
  }

  ~FileUtilTest() override
  {
    if (!m_parent_directory.empty())
    {
      File::DeleteDirRecursively(m_parent_directory);
    }
  }

  void SetUp() override
  {
    if (m_parent_directory.empty())
    {
      FAIL();
    }
  }

  constexpr static std::array<File::IfAbsentBehavior, 2> s_warning_behaviors = {
      File::IfAbsentBehavior::ConsoleWarning,
      File::IfAbsentBehavior::NoConsoleWarning,
  };

  const std::string m_parent_directory;
  const std::string m_file_path;
  const std::string m_directory_path;
  const std::string m_invalid_path;
};

static void DeleteShouldNotRemoveDirectory(const std::string& path, File::IfAbsentBehavior behavior)
{
  File::CreateDir(path);
  EXPECT_FALSE(File::Delete(path, behavior));
  File::DeleteDir(path, behavior);
}

static void DeleteShouldRemoveFile(const std::string& path, File::IfAbsentBehavior behavior)
{
  File::CreateEmptyFile(path);
  EXPECT_TRUE(File::Delete(path, behavior));
}

static void DeleteShouldReturnTrueForInvalidPath(const std::string& path,
                                                 File::IfAbsentBehavior behavior)
{
  EXPECT_TRUE(File::Delete(path, behavior));
}

static void DeleteDirShouldRemoveDirectory(const std::string& path, File::IfAbsentBehavior behavior)
{
  File::CreateDir(path);
  EXPECT_TRUE(File::DeleteDir(path, behavior));
}

static void DeleteDirShouldNotRemoveFile(const std::string& path, File::IfAbsentBehavior behavior)
{
  File::CreateEmptyFile(path);
  EXPECT_FALSE(File::DeleteDir(path, behavior));
  File::Delete(path, behavior);
}

static void DeleteDirShouldReturnTrueForInvalidPath(const std::string& path,
                                                    File::IfAbsentBehavior behavior)
{
  EXPECT_TRUE(File::DeleteDir(path, behavior));
}

TEST_F(FileUtilTest, Delete)
{
  for (const auto behavior : s_warning_behaviors)
  {
    DeleteShouldNotRemoveDirectory(m_directory_path, behavior);
    DeleteShouldRemoveFile(m_file_path, behavior);
    DeleteShouldReturnTrueForInvalidPath(m_invalid_path, behavior);
  }
}

TEST_F(FileUtilTest, DeleteDir)
{
  for (const auto behavior : s_warning_behaviors)
  {
    DeleteDirShouldRemoveDirectory(m_directory_path, behavior);
    DeleteDirShouldNotRemoveFile(m_file_path, behavior);
    DeleteDirShouldReturnTrueForInvalidPath(m_invalid_path, behavior);
  }
}

TEST_F(FileUtilTest, CreateFullPath)
{
  ASSERT_TRUE(!m_directory_path.ends_with('/'));
  File::CreateDir(m_directory_path);

  // should try to create the directory at m_directory_path, which already exists
  EXPECT_TRUE(File::IsDirectory(m_directory_path));
  EXPECT_TRUE(File::CreateFullPath(m_directory_path + "/"));

  // should create the directory (one level)
  std::string p1 = m_directory_path + "/p1";
  EXPECT_TRUE(!File::Exists(p1));
  EXPECT_TRUE(File::CreateFullPath(p1 + "/"));
  EXPECT_TRUE(File::IsDirectory(p1));

  // should create the directories (multiple levels)
  std::string p2 = m_directory_path + "/p2";
  std::string p2a = m_directory_path + "/p2/a";
  std::string p2b = m_directory_path + "/p2/a/b";
  std::string p2c = m_directory_path + "/p2/a/b/c";
  EXPECT_TRUE(!File::Exists(p2));
  EXPECT_TRUE(!File::Exists(p2a));
  EXPECT_TRUE(!File::Exists(p2b));
  EXPECT_TRUE(!File::Exists(p2c));
  EXPECT_TRUE(File::CreateFullPath(p2c + "/"));
  EXPECT_TRUE(File::IsDirectory(p2));
  EXPECT_TRUE(File::IsDirectory(p2a));
  EXPECT_TRUE(File::IsDirectory(p2b));
  EXPECT_TRUE(File::IsDirectory(p2c));

  // if the given path ends in a file, the file should be ignored
  std::string p3 = m_directory_path + "/p3";
  std::string p3file = m_directory_path + "/p3/test.bin";
  EXPECT_TRUE(!File::Exists(p3));
  EXPECT_TRUE(!File::Exists(p3file));
  EXPECT_TRUE(File::CreateFullPath(p3file));
  EXPECT_TRUE(File::IsDirectory(p3));
  EXPECT_TRUE(!File::Exists(p3file));

  // if we try to create a directory where a file already exists, we expect it to fail and leave the
  // file alone
  EXPECT_TRUE(File::CreateEmptyFile(p3file));
  EXPECT_TRUE(File::IsFile(p3file));
  EXPECT_FALSE(File::CreateFullPath(p3file + "/"));
  EXPECT_TRUE(File::IsFile(p3file));
}

TEST_F(FileUtilTest, DirectIOFile)
{
  static constexpr std::array<u8, 3> u8_test_data = {42, 7, 99};
  static constexpr std::array<u16, 3> u16_test_data = {0xdead, 0xbeef, 0xf00d};

  static constexpr int u16_data_offset = 73;

  File::DirectIOFile file;
  EXPECT_FALSE(file.IsOpen());

  // Read mode fails with a non-existing file.
  EXPECT_FALSE(file.Open(m_file_path, File::AccessMode::Read));

  std::array<u8, u8_test_data.size()> u8_buffer = {};

  // Everything fails when a file isn't open.
  EXPECT_FALSE(file.Write(u8_buffer));
  EXPECT_FALSE(file.Read(u8_buffer));
  EXPECT_FALSE(file.Flush());
  EXPECT_FALSE(file.Seek(12, File::SeekOrigin::Begin));
  EXPECT_EQ(file.Tell(), 0);
  EXPECT_EQ(file.GetSize(), 0);

  // Fail to open non-existing file.
  EXPECT_FALSE(file.Open(m_file_path, File::AccessMode::ReadAndWrite));
  EXPECT_FALSE(file.Open(m_file_path, File::AccessMode::Read, File::OpenMode::Existing));
  EXPECT_FALSE(file.Open(m_file_path, File::AccessMode::Write, File::OpenMode::Existing));

  // Read mode can create files
  EXPECT_TRUE(file.Open(m_file_path, File::AccessMode::Read, File::OpenMode::Create));
  EXPECT_TRUE(file.Close());

  // Open a file for writing.
  EXPECT_TRUE(file.Open(m_file_path, File::AccessMode::Write, File::OpenMode::Always));
  EXPECT_TRUE(file.Flush());
  EXPECT_TRUE(file.IsOpen());

  // Note: Double Open() currently ASSERTs. It's not obvious if that should succeed or fail.

  EXPECT_TRUE(file.Close());
  EXPECT_FALSE(file.Open(m_file_path, File::AccessMode::Write, File::OpenMode::Create));

  EXPECT_TRUE(file.Open(m_file_path, File::AccessMode::Write, File::OpenMode::Existing));
  EXPECT_TRUE(file.Close());

  // Rename, Resize, and Delete fail with a closed handle.
  EXPECT_FALSE(Rename(file, m_file_path, "fail"));
  EXPECT_FALSE(Resize(file, 72));
  EXPECT_FALSE(Delete(file, m_file_path));
  EXPECT_TRUE(File::Exists(m_file_path));

  EXPECT_TRUE(file.Open(m_file_path, File::AccessMode::Write));

  EXPECT_TRUE(file.Write(u8_buffer.data(), 0));  // write of 0 succeeds.
  EXPECT_FALSE(file.Read(u8_buffer.data(), 0));  // read of 0 (in write mode) fails.

  // Resize through handle works.
  EXPECT_TRUE(Resize(file, 1));
  EXPECT_EQ(file.GetSize(), 1);
  file.Close();

  // Write truncates by default.
  EXPECT_TRUE(file.Open(m_file_path, File::AccessMode::Write));
  EXPECT_EQ(file.GetSize(), 0);

  EXPECT_TRUE(file.Write(u8_test_data));
  EXPECT_EQ(file.GetSize(), u8_test_data.size());  // size changed
  EXPECT_EQ(file.Tell(), u8_test_data.size());     // file position changed

  // Relative seek works.
  EXPECT_TRUE(file.Seek(0, File::SeekOrigin::End));
  EXPECT_EQ(file.Tell(), file.GetSize());
  EXPECT_TRUE(file.Seek(-int(u8_test_data.size()), File::SeekOrigin::Current));
  EXPECT_EQ(file.Tell(), 0);

  // Read while in "write mode" fails
  EXPECT_FALSE(file.Read(u8_buffer));
  EXPECT_EQ(file.Tell(), 0);

  // Seeking past the end works.
  EXPECT_TRUE(file.Seek(u16_data_offset, File::SeekOrigin::Begin));
  EXPECT_EQ(file.Tell(), u16_data_offset);
  EXPECT_EQ(file.GetSize(), u8_test_data.size());  // no change in size

  static constexpr u64 final_file_size = u16_data_offset + sizeof(u16_test_data);

  // Size changes after write.
  EXPECT_TRUE(file.Write(Common::AsU8Span(u16_test_data)));
  EXPECT_EQ(file.GetSize(), final_file_size);

  // Seek before begin fails.
  EXPECT_FALSE(file.Seek(-1, File::SeekOrigin::Begin));
  EXPECT_EQ(file.Tell(), file.GetSize());  // unchanged position

  EXPECT_TRUE(file.Close());
  EXPECT_FALSE(file.IsOpen());

  EXPECT_EQ(file.Tell(), 0);
  EXPECT_EQ(file.GetSize(), 0);

  EXPECT_FALSE(file.Close());  // Double close fails.

  // Open file for reading.
  EXPECT_TRUE(file.Open(m_file_path, File::AccessMode::Read, File::OpenMode::Always));
  EXPECT_TRUE(file.Close());
  EXPECT_TRUE(file.Open(m_file_path, File::AccessMode::Read));

  static constexpr int big_offset = 999;

  // We can seek beyond the end.
  EXPECT_TRUE(file.Seek(big_offset, File::SeekOrigin::End));
  EXPECT_EQ(file.Tell(), file.GetSize() + big_offset);

  // Resize through handle fails when in read mode.
  EXPECT_FALSE(Resize(file, big_offset));
  EXPECT_EQ(file.GetSize(), final_file_size);

  constexpr size_t thread_count = 4;

  File::DirectIOFile closed_file;

  std::latch do_reads{1};

  EXPECT_TRUE(file.Seek(u16_data_offset, File::SeekOrigin::Begin));

  // Concurrent access with copied handles works.
  std::vector<std::thread> threads(thread_count);
  for (auto& t : threads)
  {
    t = std::thread{[&do_reads, file, closed_file]() mutable {
      EXPECT_FALSE(closed_file.IsOpen());  // a copied closed file is still closed

      EXPECT_EQ(file.Tell(), u16_data_offset);  // current position is copied

      std::array<u8, 1> one_byte{};

      constexpr u64 small_offset = 3;

      do_reads.wait();

      EXPECT_TRUE(file.Seek(small_offset, File::SeekOrigin::Begin));
      EXPECT_EQ(file.Tell(), small_offset);

      // writes fail in read mode.
      EXPECT_FALSE(file.Write(u8_test_data));
      EXPECT_FALSE(file.OffsetWrite(0, u8_test_data.data(), 0));
      EXPECT_EQ(file.Tell(), small_offset);

      EXPECT_TRUE(file.Read(one_byte.data(), 0));  // read of zero succeeds.
      EXPECT_EQ(file.Tell(), small_offset);        // unchanged position

      // Reading at an explicit offset doesn't change the position
      EXPECT_TRUE(file.OffsetRead(u8_test_data.size() - 1, one_byte));
      EXPECT_EQ(file.Tell(), small_offset);
      EXPECT_EQ(one_byte[0], u8_test_data.back());

      EXPECT_EQ(file.GetSize(), final_file_size);
      EXPECT_TRUE(file.Seek(-int(sizeof(u16_test_data)), File::SeekOrigin::End));

      // We can't read beyond the end of the file.
      EXPECT_FALSE(file.OffsetRead(big_offset, one_byte));
      // A read of zero beyond the end works.
      EXPECT_TRUE(file.OffsetRead(big_offset, one_byte.data(), 0));

      // Reading the previously written data works.
      std::array<u16, u16_test_data.size()> u16_buffer = {};
      EXPECT_TRUE(file.Read(Common::AsWritableU8Span(u16_buffer)));
      EXPECT_TRUE(std::ranges::equal(u16_buffer, u16_test_data));

      // We can't read at the end of the file.
      EXPECT_FALSE(file.Read(one_byte));
      EXPECT_EQ(file.Tell(), file.GetSize());  // The position is unchanged.
    }};
  }

  // We may close the file on our end before the other threads use it.
  file.Close();

  // ReadAndWrite does not truncate existing files.
  EXPECT_TRUE(file.Open(m_file_path, File::AccessMode::ReadAndWrite));
  EXPECT_EQ(file.GetSize(), final_file_size);
  file.Close();

  // Write doesn't truncate when the mode is changed.
  EXPECT_TRUE(file.Open(m_file_path, File::AccessMode::Write, File::OpenMode::Existing));
  EXPECT_EQ(file.GetSize(), final_file_size);

  // Open a new handle to the same file.
  File::DirectIOFile second_handle(m_file_path, File::AccessMode::Write, File::OpenMode::Always);
  EXPECT_TRUE(second_handle.IsOpen());

  const std::string destination_path_1 = m_file_path + ".dest_1";

  // Rename through handle works when opened elsewhere.
  EXPECT_TRUE(Rename(file, m_file_path, destination_path_1));
  EXPECT_FALSE(File::Exists(m_file_path));
  EXPECT_TRUE(File::Exists(destination_path_1));

  const std::string destination_path_2 = m_file_path + ".dest_2";

  // Note: Windows fails the next `Rename` if this file is kept open.
  // I don't know if there is a nice way to make that work.
  {
    File::DirectIOFile another_file{destination_path_2, File::AccessMode::Write};
    EXPECT_TRUE(another_file.IsOpen());
  }

  // Rename overwrites existing files.
  EXPECT_TRUE(Rename(file, destination_path_1, destination_path_2));
  EXPECT_FALSE(File::Exists(destination_path_1));
  EXPECT_EQ(File::GetSize(destination_path_2), final_file_size);

  const std::string destination_path_3 = m_file_path + ".dest_3";

  // Truncate fails in Read mode.
  File::Copy(destination_path_2, destination_path_3);
  EXPECT_EQ(File::GetSize(destination_path_3), final_file_size);
  {
    File::DirectIOFile tmp{destination_path_3, File::AccessMode::Read, File::OpenMode::Truncate};
    EXPECT_FALSE(tmp.IsOpen());
    EXPECT_EQ(File::GetSize(destination_path_3), final_file_size);
  }

  // Truncate works with ReadAndWrite.
  EXPECT_EQ(File::GetSize(destination_path_3), final_file_size);
  {
    File::DirectIOFile tmp{destination_path_3, File::AccessMode::ReadAndWrite,
                           File::OpenMode::Truncate};
    EXPECT_EQ(tmp.GetSize(), 0);
    Delete(tmp, destination_path_3);
  }

  // Truncate works with Write.
  File::Copy(destination_path_2, destination_path_3);
  EXPECT_EQ(File::GetSize(destination_path_3), final_file_size);
  {
    File::DirectIOFile tmp{destination_path_3, File::AccessMode::Write, File::OpenMode::Truncate};
    EXPECT_EQ(tmp.GetSize(), 0);
    Delete(tmp, destination_path_3);
  }

  // Delete through handle works.
  EXPECT_TRUE(Delete(file, destination_path_2));
  EXPECT_FALSE(File::Exists(destination_path_2));

  // The threads can read even after deleting everything.
  do_reads.count_down();
  std::ranges::for_each(threads, &std::thread::join);

  file.Close();
  EXPECT_TRUE(file.Open(destination_path_1, File::AccessMode::Read, File::OpenMode::Always));

  // Required on Windows for the below to succeed.
  second_handle.Close();

  file.Close();
  EXPECT_TRUE(file.Open(destination_path_2, File::AccessMode::Write, File::OpenMode::Always));
}