2021-08-10 23:44:58 +02:00
|
|
|
|
using LibHac.Common;
|
|
|
|
|
using LibHac.Fs;
|
2020-06-10 07:55:59 +02:00
|
|
|
|
using LibHac.Fs.Fsa;
|
2020-01-28 08:55:39 +01:00
|
|
|
|
using Xunit;
|
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
namespace LibHac.Tests.Fs.IFileSystemTestBase;
|
|
|
|
|
|
|
|
|
|
public abstract partial class IFileSystemTests
|
2020-01-28 08:55:39 +01:00
|
|
|
|
{
|
2021-11-14 20:08:57 +01:00
|
|
|
|
[Fact]
|
|
|
|
|
public void RenameFile_SameParentDirectory_EntryIsRenamed()
|
2020-01-28 08:55:39 +01:00
|
|
|
|
{
|
2021-11-14 20:08:57 +01:00
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
fs.CreateFile("/file1", 0, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Success(fs.RenameFile("/file1", "/file2"));
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Success(fs.GetEntryType(out DirectoryEntryType type, "/file2"));
|
2022-11-10 05:33:27 +01:00
|
|
|
|
Result res = fs.GetEntryType(out _, "/file1");
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Equal(DirectoryEntryType.File, type);
|
2022-11-10 05:33:27 +01:00
|
|
|
|
Assert.Result(ResultFs.PathNotFound, res);
|
2021-11-14 20:08:57 +01:00
|
|
|
|
}
|
2022-11-12 02:21:07 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
[Fact]
|
|
|
|
|
public void RenameFile_DifferentParentDirectory_EntryIsRenamed()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
fs.CreateFile("/file1", 0, CreateFileOptions.None);
|
|
|
|
|
fs.CreateDirectory("/dir");
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Success(fs.RenameFile("/file1", "/dir/file2"));
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Success(fs.GetEntryType(out DirectoryEntryType type, "/dir/file2"));
|
2022-11-10 05:33:27 +01:00
|
|
|
|
Result res = fs.GetEntryType(out _, "/file1");
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Equal(DirectoryEntryType.File, type);
|
2022-11-10 05:33:27 +01:00
|
|
|
|
Assert.Result(ResultFs.PathNotFound, res);
|
2021-11-14 20:08:57 +01:00
|
|
|
|
}
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
[Fact]
|
|
|
|
|
public void RenameFile_DestExistsAsFile_ReturnsPathAlreadyExists()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
fs.CreateFile("/file1", 0, CreateFileOptions.None);
|
|
|
|
|
fs.CreateFile("/file2", 0, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2022-11-10 05:33:27 +01:00
|
|
|
|
Result res = fs.RenameFile("/file1", "/file2");
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2022-11-10 05:33:27 +01:00
|
|
|
|
Assert.Result(ResultFs.PathAlreadyExists, res);
|
2021-11-14 20:08:57 +01:00
|
|
|
|
}
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
[Fact]
|
|
|
|
|
public void RenameFile_DestExistsAsDirectory_ReturnsPathAlreadyExists()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
fs.CreateFile("/file", 0, CreateFileOptions.None);
|
|
|
|
|
fs.CreateDirectory("/dir");
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2022-11-10 05:33:27 +01:00
|
|
|
|
Result res = fs.RenameFile("/file", "/dir");
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2022-11-10 05:33:27 +01:00
|
|
|
|
Assert.Result(ResultFs.PathAlreadyExists, res);
|
2021-11-14 20:08:57 +01:00
|
|
|
|
}
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
[Fact]
|
|
|
|
|
public void RenameFile_DestExistsAsFile_FileSizesDoNotChange()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
fs.CreateFile("/file1", 54321, CreateFileOptions.None);
|
|
|
|
|
fs.CreateFile("/file2", 12345, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
fs.RenameFile("/file1", "/file2");
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
using var file1 = new UniqueRef<IFile>();
|
|
|
|
|
using var file2 = new UniqueRef<IFile>();
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2022-12-03 20:53:30 +01:00
|
|
|
|
Assert.Success(fs.OpenFile(ref file1.Ref, "/file1", OpenMode.Read));
|
|
|
|
|
Assert.Success(fs.OpenFile(ref file2.Ref, "/file2", OpenMode.Read));
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Success(file1.Get.GetSize(out long file1Size));
|
|
|
|
|
Assert.Success(file2.Get.GetSize(out long file2Size));
|
2021-08-10 23:44:58 +02:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Equal(54321, file1Size);
|
|
|
|
|
Assert.Equal(12345, file2Size);
|
|
|
|
|
}
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
[Fact]
|
|
|
|
|
public void RenameFile_DataIsUnmodified()
|
|
|
|
|
{
|
2023-12-10 02:48:56 +01:00
|
|
|
|
byte[] data = [7, 4, 1, 0, 8, 5, 2, 9, 6, 3];
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
fs.CreateFile("/file", data.Length, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
using var file = new UniqueRef<IFile>();
|
2022-12-03 20:53:30 +01:00
|
|
|
|
fs.OpenFile(ref file.Ref, "/file", OpenMode.Write);
|
2021-11-14 20:08:57 +01:00
|
|
|
|
file.Get.Write(0, data, WriteOption.None);
|
|
|
|
|
file.Reset();
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
fs.RenameFile("/file", "/renamed");
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
byte[] readData = new byte[data.Length];
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2022-12-03 20:53:30 +01:00
|
|
|
|
fs.OpenFile(ref file.Ref, "/renamed", OpenMode.Read);
|
2022-11-10 05:33:27 +01:00
|
|
|
|
Result res = file.Get.Read(out long bytesRead, 0, readData, ReadOption.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2022-11-10 05:33:27 +01:00
|
|
|
|
Assert.Success(res);
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Equal(data.Length, bytesRead);
|
|
|
|
|
Assert.Equal(data, readData);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
}
|
2022-11-12 02:21:07 +01:00
|
|
|
|
}
|