Debian Codegraph @

Source/UnitTests/Core/IOS/FS/FileSystemTest.cpp

raw symbols SCIP
1 // Copyright 2018 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include <algorithm>
5 #include <array>
6 #include <memory>
7 #include <optional>
8 #include <string>
9
10 #include <gtest/gtest.h>
11
12 #include "Common/ChunkFile.h"
13 #include "Common/CommonTypes.h"
14 #include "Common/FileUtil.h"
15 #include "Core/IOS/FS/FileSystem.h"
16 #include "Core/IOS/IOS.h"
17 #include "UICommon/UICommon.h"
18
19 using namespace IOS::HLE::FS;
20
21 constexpr Modes modes{Mode::ReadWrite, Mode::None, Mode::None};
22
23 class FileSystemTest : public testing::Test
24 {
25 protected:
26 FileSystemTest() : m_profile_path{File::CreateTempDir()}
27 {
28 if (UserDirectoryCreationFailed())
29 {
30 return;
31 }
32 UICommon::SetUserDirectory(m_profile_path);
33 m_fs = IOS::HLE::Kernel{}.GetFS();
34 }
35
36 ~FileSystemTest() override
37 {
38 if (UserDirectoryCreationFailed())
39 {
40 return;
41 }
42 m_fs.reset();
43 File::DeleteDirRecursively(m_profile_path);
44 }
45 void SetUp() override
46 {
47 if (UserDirectoryCreationFailed())
48 {
49 FAIL();
50 }
51 }
52 bool UserDirectoryCreationFailed() const { return m_profile_path.empty(); }
53
54 std::shared_ptr<FileSystem> m_fs;
55
56 private:
57 std::string m_profile_path;
58 };
59
60 TEST(FileSystem, BasicPathValidity)
61 {
62 EXPECT_TRUE(IsValidPath("/"));
63 EXPECT_FALSE(IsValidNonRootPath("/"));
64
65 EXPECT_TRUE(IsValidNonRootPath("/shared2/sys/SYSCONF"));
66 EXPECT_TRUE(IsValidNonRootPath("/shared2/sys"));
67 EXPECT_TRUE(IsValidNonRootPath("/shared2"));
68
69 // Paths must start with /.
70 EXPECT_FALSE(IsValidNonRootPath("\\test"));
71 // Paths must not end with /.
72 EXPECT_FALSE(IsValidNonRootPath("/shared2/sys/"));
73 // Paths must not be longer than 64 characters.
74 EXPECT_FALSE(IsValidPath(
75 "/abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
76 }
77
78 TEST(FileSystem, PathSplitting)
79 {
80 SplitPathResult result;
81
82 result = {"/shared1", "00000042.app"};
83 EXPECT_EQ(SplitPathAndBasename("/shared1/00000042.app"), result);
84
85 result = {"/shared2/sys", "SYSCONF"};
86 EXPECT_EQ(SplitPathAndBasename("/shared2/sys/SYSCONF"), result);
87
88 result = {"/shared2", "sys"};
89 EXPECT_EQ(SplitPathAndBasename("/shared2/sys"), result);
90
91 result = {"/", "shared2"};
92 EXPECT_EQ(SplitPathAndBasename("/shared2"), result);
93 }
94
95 TEST_F(FileSystemTest, EssentialDirectories)
96 {
97 for (const std::string path :
98 {"/sys", "/ticket", "/title", "/shared1", "/shared2", "/tmp", "/import", "/meta"})
99 {
100 EXPECT_TRUE(m_fs->ReadDirectory(Uid{0}, Gid{0}, path).has_value()) << path;
101 }
102 }
103
104 TEST_F(FileSystemTest, CreateFile)
105 {
106 const std::string PATH = "/tmp/f";
107
108 constexpr u8 ArbitraryAttribute = 0xE1;
109
110 ASSERT_EQ(m_fs->CreateFile(Uid{0}, Gid{0}, PATH, ArbitraryAttribute, modes), ResultCode::Success);
111
112 const Result<Metadata> stats = m_fs->GetMetadata(Uid{0}, Gid{0}, PATH);
113 ASSERT_TRUE(stats.has_value());
114 EXPECT_TRUE(stats->is_file);
115 EXPECT_EQ(stats->size, 0u);
116 EXPECT_EQ(stats->uid, 0u);
117 EXPECT_EQ(stats->gid, 0);
118 EXPECT_EQ(stats->modes, modes);
119 EXPECT_EQ(stats->attribute, ArbitraryAttribute);
120
121 ASSERT_EQ(m_fs->CreateFile(Uid{0}, Gid{0}, PATH, 0, modes), ResultCode::AlreadyExists);
122
123 const Result<std::vector<std::string>> tmp_files = m_fs->ReadDirectory(Uid{0}, Gid{0}, "/tmp");
124 ASSERT_TRUE(tmp_files.has_value());
125 EXPECT_EQ(std::ranges::count(*tmp_files, "f"), 1u);
126
127 // Test invalid paths
128 // Unprintable characters
129 EXPECT_EQ(m_fs->CreateFile(Uid{0}, Gid{0}, "/tmp/tes\1t", 0, modes), ResultCode::Invalid);
130 EXPECT_EQ(m_fs->CreateFile(Uid{0}, Gid{0}, "/tmp/te\x7fst", 0, modes), ResultCode::Invalid);
131 // Paths with too many components are not rejected for files.
132 EXPECT_EQ(m_fs->CreateFile(Uid{0}, Gid{0}, "/1/2/3/4/5/6/7/8/9", 0, modes), ResultCode::NotFound);
133 }
134
135 TEST_F(FileSystemTest, CreateDirectory)
136 {
137 const std::string PATH = "/tmp/d";
138
139 constexpr u8 ArbitraryAttribute = 0x20;
140
141 ASSERT_EQ(m_fs->CreateDirectory(Uid{0}, Gid{0}, PATH, ArbitraryAttribute, modes),
142 ResultCode::Success);
143
144 const Result<Metadata> stats = m_fs->GetMetadata(Uid{0}, Gid{0}, PATH);
145 ASSERT_TRUE(stats.has_value());
146 EXPECT_FALSE(stats->is_file);
147 EXPECT_EQ(stats->uid, 0u);
148 EXPECT_EQ(stats->gid, 0);
149 EXPECT_EQ(stats->modes, modes);
150 EXPECT_EQ(stats->attribute, ArbitraryAttribute);
151
152 const Result<std::vector<std::string>> children = m_fs->ReadDirectory(Uid{0}, Gid{0}, PATH);
153 ASSERT_TRUE(children.has_value());
154 EXPECT_TRUE(children->empty());
155
156 EXPECT_EQ(m_fs->CreateDirectory(Uid{0}, Gid{0}, PATH, 0, modes), ResultCode::AlreadyExists);
157
158 // Paths with too many components should be rejected.
159 EXPECT_EQ(m_fs->CreateDirectory(Uid{0}, Gid{0}, "/1/2/3/4/5/6/7/8/9", 0, modes),
160 ResultCode::TooManyPathComponents);
161 }
162
163 TEST_F(FileSystemTest, Delete)
164 {
165 EXPECT_TRUE(m_fs->ReadDirectory(Uid{0}, Gid{0}, "/tmp").has_value());
166 EXPECT_EQ(m_fs->Delete(Uid{0}, Gid{0}, "/tmp"), ResultCode::Success);
167 EXPECT_EQ(m_fs->ReadDirectory(Uid{0}, Gid{0}, "/tmp").error(), ResultCode::NotFound);
168
169 // Test recursive directory deletion.
170 ASSERT_EQ(m_fs->CreateDirectory(Uid{0}, Gid{0}, "/sys/1", 0, modes), ResultCode::Success);
171 ASSERT_EQ(m_fs->CreateDirectory(Uid{0}, Gid{0}, "/sys/1/2", 0, modes), ResultCode::Success);
172 ASSERT_EQ(m_fs->CreateFile(Uid{0}, Gid{0}, "/sys/1/2/3", 0, modes), ResultCode::Success);
173 ASSERT_EQ(m_fs->CreateFile(Uid{0}, Gid{0}, "/sys/1/2/4", 0, modes), ResultCode::Success);
174
175 // Leave a file open. Deletion should fail while the file is in use.
176 auto handle = std::make_optional(m_fs->OpenFile(Uid{0}, Gid{0}, "/sys/1/2/3", Mode::Read));
177 ASSERT_TRUE(handle->has_value());
178 EXPECT_EQ(m_fs->Delete(Uid{0}, Gid{0}, "/sys/1/2/3"), ResultCode::InUse);
179 // A directory that contains a file that is in use is considered to be in use,
180 // so this should fail too.
181 EXPECT_EQ(m_fs->Delete(Uid{0}, Gid{0}, "/sys/1"), ResultCode::InUse);
182
183 // With the handle closed, both of these should work:
184 handle.reset();
185 EXPECT_EQ(m_fs->Delete(Uid{0}, Gid{0}, "/sys/1/2/3"), ResultCode::Success);
186 EXPECT_EQ(m_fs->Delete(Uid{0}, Gid{0}, "/sys/1"), ResultCode::Success);
187 }
188
189 TEST_F(FileSystemTest, Rename)
190 {
191 EXPECT_TRUE(m_fs->ReadDirectory(Uid{0}, Gid{0}, "/tmp").has_value());
192
193 EXPECT_EQ(m_fs->Rename(Uid{0}, Gid{0}, "/tmp", "/test"), ResultCode::Success);
194
195 EXPECT_EQ(m_fs->ReadDirectory(Uid{0}, Gid{0}, "/tmp").error(), ResultCode::NotFound);
196 EXPECT_TRUE(m_fs->ReadDirectory(Uid{0}, Gid{0}, "/test").has_value());
197
198 // Rename /test back to /tmp.
199 EXPECT_EQ(m_fs->Rename(Uid{0}, Gid{0}, "/test", "/tmp"), ResultCode::Success);
200
201 // Create a file called /tmp/f1, and rename it to /tmp/f2.
202 // This should not work; file name changes are not allowed for files.
203 ASSERT_EQ(m_fs->CreateFile(Uid{0}, Gid{0}, "/tmp/f1", 0, modes), ResultCode::Success);
204 EXPECT_EQ(m_fs->Rename(Uid{0}, Gid{0}, "/tmp/f1", "/tmp/f2"), ResultCode::Invalid);
205 }
206
207 TEST_F(FileSystemTest, RenameWithExistingTargetDirectory)
208 {
209 // Test directory -> existing, non-empty directory.
210 // IOS's FS sysmodule is not POSIX compliant and will remove the existing directory
211 // if it exists, even when there are files in it.
212 ASSERT_EQ(m_fs->CreateDirectory(Uid{0}, Gid{0}, "/tmp/d", 0, modes), ResultCode::Success);
213 ASSERT_EQ(m_fs->CreateDirectory(Uid{0}, Gid{0}, "/tmp/d2", 0, modes), ResultCode::Success);
214 ASSERT_EQ(m_fs->CreateFile(Uid{0}, Gid{0}, "/tmp/d2/file", 0, modes), ResultCode::Success);
215 EXPECT_EQ(m_fs->Rename(Uid{0}, Gid{0}, "/tmp/d", "/tmp/d2"), ResultCode::Success);
216
217 EXPECT_EQ(m_fs->ReadDirectory(Uid{0}, Gid{0}, "/tmp/d").error(), ResultCode::NotFound);
218 const Result<std::vector<std::string>> children = m_fs->ReadDirectory(Uid{0}, Gid{0}, "/tmp/d2");
219 ASSERT_TRUE(children.has_value());
220 EXPECT_TRUE(children->empty());
221 }
222
223 TEST_F(FileSystemTest, RenameWithExistingTargetFile)
224 {
225 const std::string source_path = "/sys/f2";
226 const std::string dest_path = "/tmp/f2";
227
228 // Create the test source file and write some data (so that we can check its size later on).
229 ASSERT_EQ(m_fs->CreateFile(Uid{0}, Gid{0}, source_path, 0, modes), ResultCode::Success);
230 const std::vector<u8> TEST_DATA{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}};
231 std::vector<u8> read_buffer(TEST_DATA.size());
232 {
233 const Result<FileHandle> file = m_fs->OpenFile(Uid{0}, Gid{0}, source_path, Mode::ReadWrite);
234 ASSERT_TRUE(file.has_value());
235 ASSERT_TRUE(file->Write(TEST_DATA.data(), TEST_DATA.size()).has_value());
236 }
237
238 // Create the test target file and leave it empty.
239 ASSERT_EQ(m_fs->CreateFile(Uid{0}, Gid{0}, dest_path, 0, modes), ResultCode::Success);
240
241 // Rename /sys/f2 to /tmp/f2 and check that f1 replaced f2.
242 EXPECT_EQ(m_fs->Rename(Uid{0}, Gid{0}, source_path, dest_path), ResultCode::Success);
243
244 ASSERT_FALSE(m_fs->GetMetadata(Uid{0}, Gid{0}, source_path).has_value());
245 EXPECT_EQ(m_fs->GetMetadata(Uid{0}, Gid{0}, source_path).error(), ResultCode::NotFound);
246
247 const Result<Metadata> metadata = m_fs->GetMetadata(Uid{0}, Gid{0}, dest_path);
248 ASSERT_TRUE(metadata.has_value());
249 EXPECT_TRUE(metadata->is_file);
250 EXPECT_EQ(metadata->size, TEST_DATA.size());
251 }
252
253 TEST_F(FileSystemTest, GetDirectoryStats)
254 {
255 auto check_stats = [this](const u32 clusters, const u32 inodes) {
256 const Result<DirectoryStats> stats = m_fs->GetDirectoryStats("/tmp");
257 ASSERT_TRUE(stats.has_value());
258 EXPECT_EQ(stats->used_clusters, clusters);
259 EXPECT_EQ(stats->used_inodes, inodes);
260 };
261
262 check_stats(0u, 1u);
263
264 EXPECT_EQ(m_fs->CreateFile(Uid{0}, Gid{0}, "/tmp/file", 0, modes), ResultCode::Success);
265 // Still no clusters (because the file is empty), but 2 inodes now.
266 check_stats(0u, 2u);
267
268 {
269 const Result<FileHandle> file = m_fs->OpenFile(Uid{0}, Gid{0}, "/tmp/file", Mode::Write);
270 (void)file->Write(std::vector<u8>(20).data(), 20);
271 }
272 // The file should now take up one cluster.
273 check_stats(1u, 2u);
274 }
275
276 // Files need to be explicitly created using CreateFile or CreateDirectory.
277 // Automatically creating them on first use would be a bug.
278 TEST_F(FileSystemTest, NonExistingFiles)
279 {
280 const Result<Metadata> metadata = m_fs->GetMetadata(Uid{0}, Gid{0}, "/tmp/foo");
281 ASSERT_FALSE(metadata.has_value());
282 EXPECT_EQ(metadata.error(), ResultCode::NotFound);
283
284 const Result<FileHandle> file = m_fs->OpenFile(Uid{0}, Gid{0}, "/tmp/foo", Mode::Read);
285 ASSERT_FALSE(file.has_value());
286 EXPECT_EQ(file.error(), ResultCode::NotFound);
287
288 const Result<std::vector<std::string>> children = m_fs->ReadDirectory(Uid{0}, Gid{0}, "/foo");
289 ASSERT_FALSE(children.has_value());
290 EXPECT_EQ(children.error(), ResultCode::NotFound);
291 }
292
293 TEST_F(FileSystemTest, Seek)
294 {
295 const std::vector<u8> TEST_DATA(10);
296
297 ASSERT_EQ(m_fs->CreateFile(Uid{0}, Gid{0}, "/tmp/f", 0, modes), ResultCode::Success);
298
299 const Result<FileHandle> file = m_fs->OpenFile(Uid{0}, Gid{0}, "/tmp/f", Mode::ReadWrite);
300 ASSERT_TRUE(file.has_value());
301
302 // An empty file should have a size of exactly 0 bytes.
303 EXPECT_EQ(file->GetStatus()->size, 0u);
304 // The file position should be set to the start right after an open.
305 EXPECT_EQ(file->GetStatus()->offset, 0u);
306
307 // Write some dummy data.
308 ASSERT_TRUE(file->Write(TEST_DATA.data(), TEST_DATA.size()).has_value());
309 EXPECT_EQ(file->GetStatus()->size, TEST_DATA.size());
310 EXPECT_EQ(file->GetStatus()->offset, TEST_DATA.size());
311
312 auto seek_and_check = [&file](const u32 offset, const SeekMode mode,
313 const u32 expected_position) {
314 const Result<u32> new_offset = file->Seek(offset, mode);
315 ASSERT_TRUE(new_offset.has_value());
316 EXPECT_EQ(*new_offset, expected_position);
317 EXPECT_EQ(file->GetStatus()->offset, expected_position);
318 };
319
320 seek_and_check(0, SeekMode::Set, 0);
321 seek_and_check(5, SeekMode::Set, 5);
322 seek_and_check(0, SeekMode::Current, 5);
323 seek_and_check(2, SeekMode::Current, 7);
324 seek_and_check(0, SeekMode::End, 10);
325
326 // Test past-EOF seeks.
327 const Result<u32> new_position = file->Seek(11, SeekMode::Set);
328 ASSERT_FALSE(new_position.has_value());
329 EXPECT_EQ(new_position.error(), ResultCode::Invalid);
330 }
331
332 TEST_F(FileSystemTest, WriteAndSimpleReadback)
333 {
334 const std::vector<u8> TEST_DATA{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}};
335 std::vector<u8> read_buffer(TEST_DATA.size());
336
337 ASSERT_EQ(m_fs->CreateFile(Uid{0}, Gid{0}, "/tmp/f", 0, modes), ResultCode::Success);
338
339 const Result<FileHandle> file = m_fs->OpenFile(Uid{0}, Gid{0}, "/tmp/f", Mode::ReadWrite);
340 ASSERT_TRUE(file.has_value());
341
342 // Write some test data.
343 ASSERT_TRUE(file->Write(TEST_DATA.data(), TEST_DATA.size()).has_value());
344
345 // Now read it back and make sure it is identical.
346 ASSERT_TRUE(file->Seek(0, SeekMode::Set).has_value());
347 ASSERT_TRUE(file->Read(read_buffer.data(), read_buffer.size()).has_value());
348 EXPECT_EQ(TEST_DATA, read_buffer);
349 }
350
351 TEST_F(FileSystemTest, WriteAndRead)
352 {
353 const std::vector<u8> TEST_DATA{{0xf, 1, 2, 3, 4, 5, 6, 7, 8, 9}};
354 const u32 TEST_DATA_SIZE = static_cast<u32>(TEST_DATA.size());
355 std::vector<u8> buffer(TEST_DATA_SIZE);
356
357 ASSERT_EQ(m_fs->CreateFile(Uid{0}, Gid{0}, "/tmp/f", 0, modes), ResultCode::Success);
358
359 Result<FileHandle> tmp_handle = m_fs->OpenFile(Uid{0}, Gid{0}, "/tmp/f", Mode::ReadWrite);
360 ASSERT_TRUE(tmp_handle.has_value());
361 const Fd fd = tmp_handle->Release();
362
363 // Try to read from an empty file. This should do nothing.
364 // See https://github.com/dolphin-emu/dolphin/pull/4942
365 Result<u32> read_result = m_fs->ReadBytesFromFile(fd, buffer.data(), TEST_DATA_SIZE);
366 EXPECT_TRUE(read_result.has_value());
367 EXPECT_EQ(*read_result, 0u);
368 EXPECT_EQ(m_fs->GetFileStatus(fd)->offset, 0u);
369
370 ASSERT_TRUE(m_fs->WriteBytesToFile(fd, TEST_DATA.data(), TEST_DATA_SIZE).has_value());
371 EXPECT_EQ(m_fs->GetFileStatus(fd)->offset, TEST_DATA.size());
372
373 // Try to read past EOF while we are at the end of the file. This should do nothing too.
374 read_result = m_fs->ReadBytesFromFile(fd, buffer.data(), TEST_DATA_SIZE);
375 EXPECT_TRUE(read_result.has_value());
376 EXPECT_EQ(*read_result, 0u);
377 EXPECT_EQ(m_fs->GetFileStatus(fd)->offset, TEST_DATA.size());
378
379 // Go back to the start and try to read past EOF. This should read the entire file until EOF.
380 ASSERT_TRUE(m_fs->SeekFile(fd, 0, SeekMode::Set).has_value());
381 const u32 LARGER_TEST_DATA_SIZE = TEST_DATA_SIZE + 10;
382 std::vector<u8> larger_buffer(LARGER_TEST_DATA_SIZE);
383 read_result = m_fs->ReadBytesFromFile(fd, larger_buffer.data(), LARGER_TEST_DATA_SIZE);
384 EXPECT_TRUE(read_result.has_value());
385 EXPECT_EQ(*read_result, TEST_DATA.size());
386 EXPECT_EQ(m_fs->GetFileStatus(fd)->offset, TEST_DATA.size());
387 }
388
389 TEST_F(FileSystemTest, MultipleHandles)
390 {
391 ASSERT_EQ(m_fs->CreateFile(Uid{0}, Gid{0}, "/tmp/f", 0, modes), ResultCode::Success);
392
393 {
394 const Result<FileHandle> file = m_fs->OpenFile(Uid{0}, Gid{0}, "/tmp/f", Mode::ReadWrite);
395 ASSERT_TRUE(file.has_value());
396 // Fill it with 10 zeroes.
397 ASSERT_TRUE(file->Write(std::vector<u8>(10).data(), 10).has_value());
398 }
399
400 const Result<FileHandle> file1 = m_fs->OpenFile(Uid{0}, Gid{0}, "/tmp/f", Mode::ReadWrite);
401 const Result<FileHandle> file2 = m_fs->OpenFile(Uid{0}, Gid{0}, "/tmp/f", Mode::ReadWrite);
402 ASSERT_TRUE(file1.has_value());
403 ASSERT_TRUE(file2.has_value());
404
405 // Write some test data using one handle and make sure the data is seen by the other handle
406 // (see issue 2917, 5232 and 8702 and https://github.com/dolphin-emu/dolphin/pull/2649).
407 // Also make sure the file offsets are independent for each handle.
408
409 const std::vector<u8> TEST_DATA{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}};
410 EXPECT_EQ(file1->GetStatus()->offset, 0u);
411 ASSERT_TRUE(file1->Write(TEST_DATA.data(), TEST_DATA.size()).has_value());
412 EXPECT_EQ(file1->GetStatus()->offset, 10u);
413
414 std::vector<u8> read_buffer(TEST_DATA.size());
415 EXPECT_EQ(file2->GetStatus()->offset, 0u);
416 ASSERT_TRUE(file2->Read(read_buffer.data(), read_buffer.size()).has_value());
417 EXPECT_EQ(file2->GetStatus()->offset, 10u);
418 EXPECT_EQ(TEST_DATA, read_buffer);
419 }
420
421 // ReadDirectory is used by official titles to determine whether a path is a file.
422 // If it is not a file, ResultCode::Invalid must be returned.
423 TEST_F(FileSystemTest, ReadDirectoryOnFile)
424 {
425 ASSERT_EQ(m_fs->CreateFile(Uid{0}, Gid{0}, "/tmp/f", 0, modes), ResultCode::Success);
426
427 const Result<std::vector<std::string>> result = m_fs->ReadDirectory(Uid{0}, Gid{0}, "/tmp/f");
428 ASSERT_FALSE(result.has_value());
429 EXPECT_EQ(result.error(), ResultCode::Invalid);
430 }
431
432 TEST_F(FileSystemTest, ReadDirectoryOrdering)
433 {
434 ASSERT_EQ(m_fs->CreateDirectory(Uid{0}, Gid{0}, "/tmp/o", 0, modes), ResultCode::Success);
435
436 // Randomly generated file names in no particular order.
437 const std::array<std::string, 5> file_names{{
438 "Rkj62lGwHp",
439 "XGDQTDJMea",
440 "1z5M43WeFw",
441 "YAY39VuMRd",
442 "hxJ86nkoBX",
443 }};
444 // Create the files.
445 for (const auto& name : file_names)
446 ASSERT_EQ(m_fs->CreateFile(Uid{0}, Gid{0}, "/tmp/o/" + name, 0, modes), ResultCode::Success);
447
448 // Verify that ReadDirectory returns a file list that is ordered by descending creation date
449 // (issue 10234).
450 const Result<std::vector<std::string>> result = m_fs->ReadDirectory(Uid{0}, Gid{0}, "/tmp/o");
451 ASSERT_TRUE(result.has_value());
452 ASSERT_EQ(result->size(), file_names.size());
453 EXPECT_TRUE(std::equal(result->begin(), result->end(), file_names.rbegin()));
454 }
455
456 TEST_F(FileSystemTest, CreateFullPath)
457 {
458 ASSERT_EQ(m_fs->CreateFullPath(Uid{0}, Gid{0}, "/tmp/a/b/c/d", 0, modes), ResultCode::Success);
459
460 // Parent directories should be created by CreateFullPath.
461 for (const std::string path : {"/tmp", "/tmp/a", "/tmp/a/b", "/tmp/a/b/c"})
462 EXPECT_TRUE(m_fs->ReadDirectory(Uid{0}, Gid{0}, path).has_value());
463
464 // If parent directories already exist, the call should still succeed.
465 EXPECT_EQ(m_fs->CreateFullPath(Uid{0}, Gid{0}, "/tmp/a/b/c/d", 0, modes), ResultCode::Success);
466
467 // If parent directories already exist and are owned by a different user,
468 // CreateFullPath should still succeed.
469 // See https://github.com/dolphin-emu/dolphin/pull/8593
470 EXPECT_EQ(m_fs->CreateFullPath(Uid{0x1000}, Gid{1}, "/shared2/wc24/mbox/Readme.txt", 0, modes),
471 ResultCode::Success);
472 }
473
474 TEST_F(FileSystemTest, DoState)
475 {
476 const std::string TEST_DATA_1 = "123";
477 const std::string TEST_DATA_2 = "4567";
478
479 std::array<u8, 4> read_buffer;
480
481 ASSERT_EQ(m_fs->CreateDirectory(Uid{1}, Gid{2}, "/tmp/a", 0, modes), ResultCode::Success);
482 ASSERT_EQ(m_fs->CreateDirectory(Uid{1}, Gid{2}, "/tmp/a/b", 0, modes), ResultCode::Success);
483 ASSERT_EQ(m_fs->CreateDirectory(Uid{0}, Gid{0}, "/tmp/a/c", 0, modes), ResultCode::Success);
484
485 ASSERT_EQ(m_fs->CreateFile(Uid{1}, Gid{2}, "/tmp/a/d", 0, modes), ResultCode::Success);
486 ASSERT_EQ(m_fs->CreateFile(Uid{3}, Gid{4}, "/tmp/e", 0, modes), ResultCode::Success);
487
488 {
489 Result<FileHandle> file1 = m_fs->OpenFile(Uid{1}, Gid{2}, "/tmp/a/d", Mode::ReadWrite);
490 ASSERT_TRUE(file1.has_value());
491 ASSERT_TRUE(file1->Write(TEST_DATA_1.data(), TEST_DATA_1.size()).has_value());
492 }
493
494 std::array<u8, 1024> state_buffer;
495 size_t state_size;
496
497 {
498 Result<FileHandle> file2 = m_fs->OpenFile(Uid{3}, Gid{4}, "/tmp/e", Mode::ReadWrite);
499 ASSERT_TRUE(file2.has_value());
500 ASSERT_TRUE(file2->Write(TEST_DATA_2.data(), TEST_DATA_2.size()).has_value());
501
502 u8* state_pointer = state_buffer.data();
503 PointerWrap p(&state_pointer, state_buffer.size(), PointerWrap::Mode::Write);
504 m_fs->DoState(p);
505 ASSERT_TRUE(p.IsWriteMode());
506
507 ASSERT_TRUE(file2->Seek(2, SeekMode::Set).has_value());
508 ASSERT_TRUE(file2->Write("_", 1).has_value());
509
510 Fd fd = file2->Release();
511 p.Do(fd);
512 ASSERT_TRUE(p.IsWriteMode());
513
514 state_size = state_pointer - state_buffer.data();
515 }
516
517 ASSERT_EQ(m_fs->Delete(Uid{0}, Gid{0}, "/tmp/a"), ResultCode::Success);
518 ASSERT_EQ(m_fs->GetMetadata(Uid{0}, Gid{0}, "/tmp/a").error(), ResultCode::NotFound);
519 ASSERT_EQ(m_fs->GetMetadata(Uid{0}, Gid{0}, "/tmp/a/b").error(), ResultCode::NotFound);
520 ASSERT_EQ(m_fs->GetMetadata(Uid{0}, Gid{0}, "/tmp/a/c").error(), ResultCode::NotFound);
521 ASSERT_EQ(m_fs->GetMetadata(Uid{0}, Gid{0}, "/tmp/a/d").error(), ResultCode::NotFound);
522
523 ASSERT_EQ(m_fs->CreateFile(Uid{5}, Gid{6}, "/tmp/f", 0, modes), ResultCode::Success);
524
525 ASSERT_EQ(m_fs->CreateDirectory(Uid{7}, Gid{8}, "/tmp/g", 0, modes), ResultCode::Success);
526
527 u8* state_pointer = state_buffer.data();
528 PointerWrap p(&state_pointer, state_size, PointerWrap::Mode::Read);
529 m_fs->DoState(p);
530 ASSERT_TRUE(p.IsReadMode());
531
532 constexpr auto check_directory_metadata = [](const Result<Metadata>& metadata, Uid uid, Gid gid) {
533 ASSERT_TRUE(metadata.has_value());
534 ASSERT_EQ(metadata->uid, uid);
535 ASSERT_EQ(metadata->gid, gid);
536 ASSERT_FALSE(metadata->is_file);
537 };
538
539 constexpr auto check_file_metadata = [](const Result<Metadata>& metadata, Uid uid, Gid gid,
540 u32 size) {
541 ASSERT_TRUE(metadata.has_value());
542 ASSERT_EQ(metadata->uid, uid);
543 ASSERT_EQ(metadata->gid, gid);
544 ASSERT_TRUE(metadata->is_file);
545 ASSERT_EQ(metadata->size, size);
546 };
547
548 check_directory_metadata(m_fs->GetMetadata(Uid{0}, Gid{0}, "/tmp/a"), Uid{1}, Gid{2});
549 check_directory_metadata(m_fs->GetMetadata(Uid{0}, Gid{0}, "/tmp/a/b"), Uid{1}, Gid{2});
550 check_directory_metadata(m_fs->GetMetadata(Uid{0}, Gid{0}, "/tmp/a/c"), Uid{0}, Gid{0});
551
552 check_file_metadata(m_fs->GetMetadata(Uid{0}, Gid{0}, "/tmp/a/d"), Uid{1}, Gid{2}, 3);
553 check_file_metadata(m_fs->GetMetadata(Uid{0}, Gid{0}, "/tmp/e"), Uid{3}, Gid{4}, 4);
554
555 ASSERT_EQ(m_fs->GetMetadata(Uid{0}, Gid{0}, "/tmp/f").error(), ResultCode::NotFound);
556 ASSERT_EQ(m_fs->GetMetadata(Uid{0}, Gid{0}, "/tmp/g").error(), ResultCode::NotFound);
557
558 Fd fd{};
559 p.Do(fd);
560 ASSERT_TRUE(p.IsReadMode());
561
562 {
563 FileHandle file2(m_fs.get(), fd);
564 ASSERT_EQ(file2.GetStatus()->offset, 4u);
565 ASSERT_TRUE(file2.Seek(0, SeekMode::Set).has_value());
566 ASSERT_TRUE(file2.Read(read_buffer.data(), TEST_DATA_2.size()).has_value());
567 for (size_t i = 0; i < TEST_DATA_2.size(); ++i)
568 ASSERT_EQ(read_buffer[i], TEST_DATA_2[i]);
569 }
570
571 {
572 Result<FileHandle> file1 = m_fs->OpenFile(Uid{5}, Gid{6}, "/tmp/a/d", Mode::Read);
573 ASSERT_TRUE(file1->Read(read_buffer.data(), TEST_DATA_1.size()).has_value());
574 for (size_t i = 0; i < TEST_DATA_1.size(); ++i)
575 ASSERT_EQ(read_buffer[i], TEST_DATA_1[i]);
576 }
577 }