mirror of
https://github.com/Thealexbarney/LibHac.git
synced 2024-11-14 10:49:41 +01:00
126 lines
3.9 KiB
C#
126 lines
3.9 KiB
C#
|
using System;
|
|||
|
using LibHac.Fs;
|
|||
|
using Xunit;
|
|||
|
|
|||
|
namespace LibHac.Tests.Fs.IFileSystemTestBase
|
|||
|
{
|
|||
|
public abstract partial class IFileSystemTests
|
|||
|
{
|
|||
|
[Fact]
|
|||
|
public void IFileRead_BytesReadContainsNumberOfBytesRead()
|
|||
|
{
|
|||
|
IFileSystem fs = CreateFileSystem();
|
|||
|
|
|||
|
fs.CreateFile("/file", 100, CreateFileOptions.None);
|
|||
|
|
|||
|
var buffer = new byte[20];
|
|||
|
fs.OpenFile(out IFile file, "/file", OpenMode.Read);
|
|||
|
using (file)
|
|||
|
{
|
|||
|
Assert.True(file.Read(out long bytesRead, 50, buffer, ReadOption.None).IsSuccess());
|
|||
|
Assert.Equal(20, bytesRead);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public void IFileRead_OffsetPastEndOfFile_ReturnsOutOfRange()
|
|||
|
{
|
|||
|
IFileSystem fs = CreateFileSystem();
|
|||
|
|
|||
|
fs.CreateFile("/file", 0, CreateFileOptions.None);
|
|||
|
|
|||
|
var buffer = new byte[10];
|
|||
|
fs.OpenFile(out IFile file, "/file", OpenMode.Read);
|
|||
|
using (file)
|
|||
|
{
|
|||
|
Result rc = file.Read(out _, 1, buffer, ReadOption.None);
|
|||
|
Assert.Equal(ResultFs.OutOfRange.Value, rc);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public void IFileRead_OpenModeNoRead_ReturnsInvalidOpenModeForRead()
|
|||
|
{
|
|||
|
IFileSystem fs = CreateFileSystem();
|
|||
|
|
|||
|
fs.CreateFile("/file", 0, CreateFileOptions.None);
|
|||
|
|
|||
|
var buffer = new byte[10];
|
|||
|
fs.OpenFile(out IFile file, "/file", OpenMode.Write);
|
|||
|
using (file)
|
|||
|
{
|
|||
|
Result rc = file.Read(out _, 0, buffer, ReadOption.None);
|
|||
|
Assert.Equal(ResultFs.InvalidOpenModeForRead.Value, rc);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public void IFileRead_NegativeOffset_ReturnsOutOfRange()
|
|||
|
{
|
|||
|
IFileSystem fs = CreateFileSystem();
|
|||
|
|
|||
|
fs.CreateFile("/file", 0, CreateFileOptions.None);
|
|||
|
|
|||
|
var buffer = new byte[10];
|
|||
|
fs.OpenFile(out IFile file, "/file", OpenMode.Write);
|
|||
|
using (file)
|
|||
|
{
|
|||
|
Result rc = file.Read(out _, -5, buffer, ReadOption.None);
|
|||
|
Assert.Equal(ResultFs.OutOfRange.Value, rc);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public void IFileRead_OffsetPlusSizeOverflows_ReturnsOutOfRange()
|
|||
|
{
|
|||
|
IFileSystem fs = CreateFileSystem();
|
|||
|
|
|||
|
fs.CreateFile("/file", 0, CreateFileOptions.None);
|
|||
|
|
|||
|
var buffer = new byte[10];
|
|||
|
fs.OpenFile(out IFile file, "/file", OpenMode.Write);
|
|||
|
using (file)
|
|||
|
{
|
|||
|
Result rc = file.Read(out _, long.MaxValue - 5, buffer, ReadOption.None);
|
|||
|
Assert.Equal(ResultFs.OutOfRange.Value, rc);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public void IFileRead_FileTooSmallToFillBuffer_BytesReadContainsAvailableByteCount()
|
|||
|
{
|
|||
|
IFileSystem fs = CreateFileSystem();
|
|||
|
|
|||
|
fs.CreateFile("/file", 100, CreateFileOptions.None);
|
|||
|
|
|||
|
var buffer = new byte[200];
|
|||
|
fs.OpenFile(out IFile file, "/file", OpenMode.Read);
|
|||
|
using (file)
|
|||
|
{
|
|||
|
Assert.True(file.Read(out long bytesRead, 90, buffer, ReadOption.None).IsSuccess());
|
|||
|
Assert.Equal(10, bytesRead);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public void IFileRead_FileTooSmallToFillBuffer_DoesPartialRead()
|
|||
|
{
|
|||
|
IFileSystem fs = CreateFileSystem();
|
|||
|
|
|||
|
fs.CreateFile("/file", 100, CreateFileOptions.None);
|
|||
|
|
|||
|
var bufferExpected = new byte[200];
|
|||
|
bufferExpected.AsSpan(10).Fill(0xCC);
|
|||
|
|
|||
|
var buffer = new byte[200];
|
|||
|
buffer.AsSpan().Fill(0xCC);
|
|||
|
|
|||
|
fs.OpenFile(out IFile file, "/file", OpenMode.Read);
|
|||
|
using (file)
|
|||
|
{
|
|||
|
Assert.True(file.Read(out _, 90, buffer, ReadOption.None).IsSuccess());
|
|||
|
Assert.Equal(bufferExpected, buffer);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|