2020-03-08 09:00:02 +01: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;
|
|
|
|
|
|
|
|
|
|
namespace LibHac.Tests.Fs.IFileSystemTestBase
|
|
|
|
|
{
|
|
|
|
|
public abstract partial class IFileSystemTests
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public void RenameFile_SameParentDirectory_EntryIsRenamed()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.CreateFile("/file1".ToU8Span(), 0, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2020-07-06 17:37:34 +02:00
|
|
|
|
Assert.Success(fs.RenameFile("/file1".ToU8Span(), "/file2".ToU8Span()));
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2020-07-06 17:37:34 +02:00
|
|
|
|
Assert.Success(fs.GetEntryType(out DirectoryEntryType type, "/file2".ToU8Span()));
|
2020-03-08 09:00:02 +01:00
|
|
|
|
Result rc = fs.GetEntryType(out _, "/file1".ToU8Span());
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
|
|
|
|
Assert.Equal(DirectoryEntryType.File, type);
|
2020-07-06 17:37:34 +02:00
|
|
|
|
Assert.Result(ResultFs.PathNotFound, rc);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
}
|
|
|
|
|
[Fact]
|
|
|
|
|
public void RenameFile_DifferentParentDirectory_EntryIsRenamed()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.CreateFile("/file1".ToU8Span(), 0, CreateFileOptions.None);
|
|
|
|
|
fs.CreateDirectory("/dir".ToU8Span());
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2020-07-06 17:37:34 +02:00
|
|
|
|
Assert.Success(fs.RenameFile("/file1".ToU8Span(), "/dir/file2".ToU8Span()));
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2020-07-06 17:37:34 +02:00
|
|
|
|
Assert.Success(fs.GetEntryType(out DirectoryEntryType type, "/dir/file2".ToU8Span()));
|
2020-03-08 09:00:02 +01:00
|
|
|
|
Result rc = fs.GetEntryType(out _, "/file1".ToU8Span());
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
|
|
|
|
Assert.Equal(DirectoryEntryType.File, type);
|
2020-07-06 17:37:34 +02:00
|
|
|
|
Assert.Result(ResultFs.PathNotFound, rc);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void RenameFile_DestExistsAsFile_ReturnsPathAlreadyExists()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.CreateFile("/file1".ToU8Span(), 0, CreateFileOptions.None);
|
|
|
|
|
fs.CreateFile("/file2".ToU8Span(), 0, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
Result rc = fs.RenameFile("/file1".ToU8Span(), "/file2".ToU8Span());
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2020-07-06 17:37:34 +02:00
|
|
|
|
Assert.Result(ResultFs.PathAlreadyExists, rc);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void RenameFile_DestExistsAsDirectory_ReturnsPathAlreadyExists()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.CreateFile("/file".ToU8Span(), 0, CreateFileOptions.None);
|
|
|
|
|
fs.CreateDirectory("/dir".ToU8Span());
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
Result rc = fs.RenameFile("/file".ToU8Span(), "/dir".ToU8Span());
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2020-07-06 17:37:34 +02:00
|
|
|
|
Assert.Result(ResultFs.PathAlreadyExists, rc);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void RenameFile_DestExistsAsFile_FileSizesDoNotChange()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.CreateFile("/file1".ToU8Span(), 54321, CreateFileOptions.None);
|
|
|
|
|
fs.CreateFile("/file2".ToU8Span(), 12345, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.RenameFile("/file1".ToU8Span(), "/file2".ToU8Span());
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2020-07-06 17:37:34 +02:00
|
|
|
|
Assert.Success(fs.OpenFile(out IFile file1, "/file1".ToU8Span(), OpenMode.Read));
|
|
|
|
|
Assert.Success(fs.OpenFile(out IFile file2, "/file2".ToU8Span(), OpenMode.Read));
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
|
|
|
|
using (file1)
|
|
|
|
|
using (file2)
|
|
|
|
|
{
|
2020-07-06 17:37:34 +02:00
|
|
|
|
Assert.Success(file1.GetSize(out long file1Size));
|
|
|
|
|
Assert.Success(file2.GetSize(out long file2Size));
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
|
|
|
|
Assert.Equal(54321, file1Size);
|
|
|
|
|
Assert.Equal(12345, file2Size);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void RenameFile_DataIsUnmodified()
|
|
|
|
|
{
|
|
|
|
|
var data = new byte[] { 7, 4, 1, 0, 8, 5, 2, 9, 6, 3 };
|
|
|
|
|
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.CreateFile("/file".ToU8Span(), data.Length, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.OpenFile(out IFile file, "/file".ToU8Span(), OpenMode.Write);
|
2020-06-08 00:23:51 +02:00
|
|
|
|
file.Write(0, data, WriteOption.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
file.Dispose();
|
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.RenameFile("/file".ToU8Span(), "/renamed".ToU8Span());
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
|
|
|
|
var readData = new byte[data.Length];
|
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.OpenFile(out file, "/renamed".ToU8Span(), OpenMode.Read);
|
2020-06-08 00:23:51 +02:00
|
|
|
|
Result rc = file.Read(out long bytesRead, 0, readData, ReadOption.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
file.Dispose();
|
|
|
|
|
|
2020-07-06 17:37:34 +02:00
|
|
|
|
Assert.Success(rc);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
Assert.Equal(data.Length, bytesRead);
|
|
|
|
|
Assert.Equal(data, readData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|