2020-01-28 08:55:39 +01:00
|
|
|
|
using System;
|
2020-03-08 09:00:02 +01: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;
|
|
|
|
|
|
|
|
|
|
namespace LibHac.Tests.Fs.IFileSystemTestBase
|
|
|
|
|
{
|
|
|
|
|
public abstract partial class IFileSystemTests
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public void IFileWrite_CanReadBackWrittenData()
|
|
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
var readData = new byte[data.Length];
|
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.OpenFile(out file, "/file".ToU8Span(), OpenMode.Read);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
using (file)
|
|
|
|
|
{
|
2020-07-06 17:37:34 +02:00
|
|
|
|
Assert.Success(file.Read(out long bytesRead, 0, readData, ReadOption.None));
|
2020-01-28 08:55:39 +01:00
|
|
|
|
Assert.Equal(data.Length, bytesRead);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Assert.Equal(data, readData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void IFileWrite_WritePastEndOfFileWithNoAppend_ReturnsFileExtensionWithoutOpenModeAllowAppend()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.CreateFile("/file".ToU8Span(), 10, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
|
|
|
|
var buffer = new byte[10];
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.OpenFile(out IFile file, "/file".ToU8Span(), OpenMode.Write);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
using (file)
|
|
|
|
|
{
|
2020-06-08 00:23:51 +02:00
|
|
|
|
Result rc = file.Write(5, buffer, WriteOption.None);
|
2020-07-06 17:37:34 +02:00
|
|
|
|
Assert.Result(ResultFs.FileExtensionWithoutOpenModeAllowAppend, rc);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void IFileWrite_OpenModeNoWrite_ReturnsInvalidOpenModeForWrite()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.CreateFile("/file".ToU8Span(), 10, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
|
|
|
|
var buffer = new byte[10];
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.OpenFile(out IFile file, "/file".ToU8Span(), OpenMode.Read);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
using (file)
|
|
|
|
|
{
|
2020-06-08 00:23:51 +02:00
|
|
|
|
Result rc = file.Write(5, buffer, WriteOption.None);
|
2020-07-06 17:37:34 +02:00
|
|
|
|
Assert.Result(ResultFs.InvalidOpenModeForWrite, rc);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void IFileWrite_NegativeOffset_ReturnsOutOfRange()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.CreateFile("/file".ToU8Span(), 10, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
|
|
|
|
var buffer = new byte[10];
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.OpenFile(out IFile file, "/file".ToU8Span(), OpenMode.Read);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
using (file)
|
|
|
|
|
{
|
2020-06-08 00:23:51 +02:00
|
|
|
|
Result rc = file.Write(-5, buffer, WriteOption.None);
|
2020-07-06 17:37:34 +02:00
|
|
|
|
Assert.Result(ResultFs.OutOfRange, rc);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void IFileWrite_OffsetPlusSizeOverflows_ReturnsOutOfRange()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.CreateFile("/file".ToU8Span(), 10, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
|
|
|
|
var buffer = new byte[10];
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.OpenFile(out IFile file, "/file".ToU8Span(), OpenMode.Read);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
using (file)
|
|
|
|
|
{
|
2020-06-08 00:23:51 +02:00
|
|
|
|
Result rc = file.Write(long.MaxValue - 5, buffer, WriteOption.None);
|
2020-07-06 17:37:34 +02:00
|
|
|
|
Assert.Result(ResultFs.OutOfRange, rc);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void IFileWrite_WritePartiallyPastEndOfFileAppendAllowed_FileIsExtended()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.CreateFile("/file".ToU8Span(), 10, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
|
|
|
|
var buffer = new byte[10];
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.OpenFile(out IFile file, "/file".ToU8Span(), OpenMode.All);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
using (file)
|
|
|
|
|
{
|
2020-07-06 17:37:34 +02:00
|
|
|
|
Assert.Success(file.Write(5, buffer, WriteOption.None));
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
|
|
|
|
file.GetSize(out long newSize);
|
|
|
|
|
Assert.Equal(15, newSize);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void IFileWrite_WritePastEndOfFileAppendAllowed_FileIsExtended()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.CreateFile("/file".ToU8Span(), 10, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
|
|
|
|
var buffer = new byte[10];
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.OpenFile(out IFile file, "/file".ToU8Span(), OpenMode.All);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
using (file)
|
|
|
|
|
{
|
2020-07-06 17:37:34 +02:00
|
|
|
|
Assert.Success(file.Write(15, buffer, WriteOption.None));
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
|
|
|
|
file.GetSize(out long newSize);
|
|
|
|
|
Assert.Equal(25, newSize);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void IFileWrite_WritePastEndOfFileAppendAllowed_DataIsWritten()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.CreateFile("/file".ToU8Span(), 10, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
|
|
|
|
var bufferExpected = new byte[25];
|
|
|
|
|
bufferExpected.AsSpan(15).Fill(0xCC);
|
|
|
|
|
|
|
|
|
|
var writeBuffer = new byte[10];
|
|
|
|
|
writeBuffer.AsSpan().Fill(0xCC);
|
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.OpenFile(out IFile file, "/file".ToU8Span(), OpenMode.All);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
using (file)
|
|
|
|
|
{
|
2020-07-06 17:37:34 +02:00
|
|
|
|
Assert.Success(file.Write(15, writeBuffer, WriteOption.None));
|
2020-02-12 10:18:22 +01:00
|
|
|
|
|
|
|
|
|
// Unwritten portions of new files are undefined, so write to the other portions
|
2020-06-08 00:23:51 +02:00
|
|
|
|
file.Write(0, new byte[15], WriteOption.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var readBuffer = new byte[25];
|
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.OpenFile(out file, "/file".ToU8Span(), OpenMode.Read);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
using (file)
|
|
|
|
|
{
|
2020-06-08 00:23:51 +02:00
|
|
|
|
file.Read(out _, 0, readBuffer, ReadOption.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
Assert.Equal(bufferExpected, readBuffer);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|