2020-01-28 08:55:39 +01:00
|
|
|
|
using System;
|
2021-08-10 23:44:58 +02:00
|
|
|
|
using LibHac.Common;
|
2020-01-28 08:55:39 +01:00
|
|
|
|
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 IFileWrite_CanReadBackWrittenData()
|
2020-01-28 08:55:39 +01:00
|
|
|
|
{
|
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
|
|
|
|
byte[] readData = new byte[data.Length];
|
2022-12-03 20:53:30 +01:00
|
|
|
|
fs.OpenFile(ref file.Ref, "/file", OpenMode.Read);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Success(file.Get.Read(out long bytesRead, 0, readData, ReadOption.None));
|
|
|
|
|
Assert.Equal(data.Length, bytesRead);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Equal(data, readData);
|
|
|
|
|
}
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
[Fact]
|
|
|
|
|
public void IFileWrite_WritePastEndOfFileWithNoAppend_ReturnsFileExtensionWithoutOpenModeAllowAppend()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
fs.CreateFile("/file", 10, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
byte[] buffer = new byte[10];
|
|
|
|
|
using var file = new UniqueRef<IFile>();
|
2022-12-03 20:53:30 +01:00
|
|
|
|
fs.OpenFile(ref file.Ref, "/file", OpenMode.Write);
|
2021-08-10 23:44:58 +02:00
|
|
|
|
|
2022-11-10 05:33:27 +01:00
|
|
|
|
Result res = file.Get.Write(5, buffer, WriteOption.None);
|
|
|
|
|
Assert.Result(ResultFs.FileExtensionWithoutOpenModeAllowAppend, 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 IFileWrite_OpenModeNoWrite_ReturnsInvalidOpenModeForWrite()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
fs.CreateFile("/file", 10, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
byte[] buffer = new byte[10];
|
|
|
|
|
using var file = new UniqueRef<IFile>();
|
2022-12-03 20:53:30 +01:00
|
|
|
|
fs.OpenFile(ref file.Ref, "/file", OpenMode.Read);
|
2021-08-10 23:44:58 +02:00
|
|
|
|
|
2022-11-10 05:33:27 +01:00
|
|
|
|
Result res = file.Get.Write(5, buffer, WriteOption.None);
|
|
|
|
|
Assert.Result(ResultFs.WriteUnpermitted, 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 IFileWrite_NegativeOffset_ReturnsOutOfRange()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
fs.CreateFile("/file", 10, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
byte[] buffer = new byte[10];
|
|
|
|
|
using var file = new UniqueRef<IFile>();
|
2022-12-03 20:53:30 +01:00
|
|
|
|
fs.OpenFile(ref file.Ref, "/file", OpenMode.Read);
|
2021-08-10 23:44:58 +02:00
|
|
|
|
|
2022-11-10 05:33:27 +01:00
|
|
|
|
Result res = file.Get.Write(-5, buffer, WriteOption.None);
|
|
|
|
|
Assert.Result(ResultFs.OutOfRange, 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 IFileWrite_OffsetPlusSizeOverflows_ReturnsOutOfRange()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
fs.CreateFile("/file", 10, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
byte[] buffer = new byte[10];
|
|
|
|
|
using var file = new UniqueRef<IFile>();
|
2022-12-03 20:53:30 +01:00
|
|
|
|
fs.OpenFile(ref file.Ref, "/file", OpenMode.Read);
|
2021-08-10 23:44:58 +02:00
|
|
|
|
|
2022-11-10 05:33:27 +01:00
|
|
|
|
Result res = file.Get.Write(long.MaxValue - 5, buffer, WriteOption.None);
|
|
|
|
|
Assert.Result(ResultFs.OutOfRange, 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 IFileWrite_WritePartiallyPastEndOfFileAppendAllowed_FileIsExtended()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
fs.CreateFile("/file", 10, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
byte[] buffer = new byte[10];
|
|
|
|
|
using var file = new UniqueRef<IFile>();
|
2022-12-03 20:53:30 +01:00
|
|
|
|
fs.OpenFile(ref file.Ref, "/file", OpenMode.All);
|
2021-08-10 23:44:58 +02:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Success(file.Get.Write(5, buffer, WriteOption.None));
|
2021-08-10 23:44:58 +02:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
file.Get.GetSize(out long newSize);
|
|
|
|
|
Assert.Equal(15, newSize);
|
|
|
|
|
}
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
[Fact]
|
|
|
|
|
public void IFileWrite_WritePastEndOfFileAppendAllowed_FileIsExtended()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
fs.CreateFile("/file", 10, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
byte[] buffer = new byte[10];
|
|
|
|
|
using var file = new UniqueRef<IFile>();
|
2022-12-03 20:53:30 +01:00
|
|
|
|
fs.OpenFile(ref file.Ref, "/file", OpenMode.All);
|
2021-08-10 23:44:58 +02:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Success(file.Get.Write(15, buffer, WriteOption.None));
|
2021-08-10 23:44:58 +02:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
file.Get.GetSize(out long newSize);
|
|
|
|
|
Assert.Equal(25, newSize);
|
|
|
|
|
}
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
[Fact]
|
|
|
|
|
public void IFileWrite_WritePastEndOfFileAppendAllowed_DataIsWritten()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
fs.CreateFile("/file", 10, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
byte[] bufferExpected = new byte[25];
|
|
|
|
|
bufferExpected.AsSpan(15).Fill(0xCC);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
byte[] writeBuffer = new byte[10];
|
|
|
|
|
writeBuffer.AsSpan().Fill(0xCC);
|
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.All);
|
2020-02-12 10:18:22 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Success(file.Get.Write(15, writeBuffer, WriteOption.None));
|
2021-08-10 23:44:58 +02:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
// Unwritten portions of new files are undefined, so write to the other portions
|
|
|
|
|
file.Get.Write(0, new byte[15], WriteOption.None);
|
|
|
|
|
file.Reset();
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
byte[] readBuffer = new byte[25];
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2022-12-03 20:53:30 +01:00
|
|
|
|
fs.OpenFile(ref file.Ref, "/file", OpenMode.Read);
|
2021-08-10 23:44:58 +02:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
file.Get.Read(out _, 0, readBuffer, ReadOption.None);
|
|
|
|
|
Assert.Equal(bufferExpected, readBuffer);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
}
|
2022-11-12 02:21:07 +01:00
|
|
|
|
}
|