2020-01-28 08:55:39 +01:00
|
|
|
|
using System;
|
|
|
|
|
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 IFileRead_BytesReadContainsNumberOfBytesRead()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2021-07-19 22:16:11 +02:00
|
|
|
|
fs.CreateFile("/file", 100, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-01-20 03:49:58 +01:00
|
|
|
|
byte[] buffer = new byte[20];
|
2021-07-19 22:16:11 +02:00
|
|
|
|
fs.OpenFile(out IFile file, "/file", 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, 50, buffer, ReadOption.None));
|
2020-01-28 08:55:39 +01:00
|
|
|
|
Assert.Equal(20, bytesRead);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void IFileRead_OffsetPastEndOfFile_ReturnsOutOfRange()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2021-07-19 22:16:11 +02:00
|
|
|
|
fs.CreateFile("/file", 0, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-01-20 03:49:58 +01:00
|
|
|
|
byte[] buffer = new byte[10];
|
2021-07-19 22:16:11 +02:00
|
|
|
|
fs.OpenFile(out IFile file, "/file", OpenMode.Read);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
using (file)
|
|
|
|
|
{
|
2020-06-08 00:23:51 +02:00
|
|
|
|
Result rc = file.Read(out _, 1, buffer, ReadOption.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 IFileRead_OpenModeNoRead_ReturnsInvalidOpenModeForRead()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2021-07-19 22:16:11 +02:00
|
|
|
|
fs.CreateFile("/file", 0, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-01-20 03:49:58 +01:00
|
|
|
|
byte[] buffer = new byte[10];
|
2021-07-19 22:16:11 +02:00
|
|
|
|
fs.OpenFile(out IFile file, "/file", OpenMode.Write);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
using (file)
|
|
|
|
|
{
|
2020-06-08 00:23:51 +02:00
|
|
|
|
Result rc = file.Read(out _, 0, buffer, ReadOption.None);
|
2021-01-01 20:29:33 +01:00
|
|
|
|
Assert.Result(ResultFs.ReadUnpermitted, rc);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void IFileRead_NegativeOffset_ReturnsOutOfRange()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2021-07-19 22:16:11 +02:00
|
|
|
|
fs.CreateFile("/file", 0, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-01-20 03:49:58 +01:00
|
|
|
|
byte[] buffer = new byte[10];
|
2021-07-19 22:16:11 +02:00
|
|
|
|
fs.OpenFile(out IFile file, "/file", OpenMode.Write);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
using (file)
|
|
|
|
|
{
|
2020-06-08 00:23:51 +02:00
|
|
|
|
Result rc = file.Read(out _, -5, buffer, ReadOption.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 IFileRead_OffsetPlusSizeOverflows_ReturnsOutOfRange()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2021-07-19 22:16:11 +02:00
|
|
|
|
fs.CreateFile("/file", 0, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-01-20 03:49:58 +01:00
|
|
|
|
byte[] buffer = new byte[10];
|
2021-07-19 22:16:11 +02:00
|
|
|
|
fs.OpenFile(out IFile file, "/file", OpenMode.Write);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
using (file)
|
|
|
|
|
{
|
2020-06-08 00:23:51 +02:00
|
|
|
|
Result rc = file.Read(out _, long.MaxValue - 5, buffer, ReadOption.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 IFileRead_FileTooSmallToFillBuffer_BytesReadContainsAvailableByteCount()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2021-07-19 22:16:11 +02:00
|
|
|
|
fs.CreateFile("/file", 100, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-01-20 03:49:58 +01:00
|
|
|
|
byte[] buffer = new byte[200];
|
2021-07-19 22:16:11 +02:00
|
|
|
|
fs.OpenFile(out IFile file, "/file", 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, 90, buffer, ReadOption.None));
|
2020-01-28 08:55:39 +01:00
|
|
|
|
Assert.Equal(10, bytesRead);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void IFileRead_FileTooSmallToFillBuffer_DoesPartialRead()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2021-07-19 22:16:11 +02:00
|
|
|
|
fs.CreateFile("/file", 100, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2020-02-12 10:18:22 +01:00
|
|
|
|
// The contents of a created file are undefined, so zero the file
|
2021-07-19 22:16:11 +02:00
|
|
|
|
fs.OpenFile(out IFile file, "/file", OpenMode.Write);
|
2020-02-12 10:18:22 +01:00
|
|
|
|
using (file)
|
|
|
|
|
{
|
2020-06-08 00:23:51 +02:00
|
|
|
|
file.Write(0, new byte[100], WriteOption.None);
|
2020-02-12 10:18:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-20 03:49:58 +01:00
|
|
|
|
byte[] bufferExpected = new byte[200];
|
2020-01-28 08:55:39 +01:00
|
|
|
|
bufferExpected.AsSpan(10).Fill(0xCC);
|
|
|
|
|
|
2021-01-20 03:49:58 +01:00
|
|
|
|
byte[] buffer = new byte[200];
|
2020-01-28 08:55:39 +01:00
|
|
|
|
buffer.AsSpan().Fill(0xCC);
|
|
|
|
|
|
2021-07-19 22:16:11 +02:00
|
|
|
|
fs.OpenFile(out file, "/file", 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 _, 90, buffer, ReadOption.None));
|
2020-01-28 08:55:39 +01:00
|
|
|
|
Assert.Equal(bufferExpected, buffer);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|