LibHac/tests/LibHac.Tests/Fs/IFileSystemTestBase/IFileSystemTests.IFile.Read.cs

126 lines
3.8 KiB
C#
Raw Normal View History

using System;
using LibHac.Common;
using LibHac.Fs;
using LibHac.Fs.Fsa;
using Xunit;
2021-11-14 20:08:57 +01:00
namespace LibHac.Tests.Fs.IFileSystemTestBase;
public abstract partial class IFileSystemTests
{
2021-11-14 20:08:57 +01:00
[Fact]
public void IFileRead_BytesReadContainsNumberOfBytesRead()
{
2021-11-14 20:08:57 +01:00
IFileSystem fs = CreateFileSystem();
2021-11-14 20:08:57 +01:00
fs.CreateFile("/file", 100, CreateFileOptions.None);
2021-11-14 20:08:57 +01:00
byte[] buffer = new byte[20];
using var file = new UniqueRef<IFile>();
fs.OpenFile(ref file.Ref(), "/file", OpenMode.Read);
2021-11-14 20:08:57 +01:00
Assert.Success(file.Get.Read(out long bytesRead, 50, buffer, ReadOption.None));
Assert.Equal(20, bytesRead);
}
2021-11-14 20:08:57 +01:00
[Fact]
public void IFileRead_OffsetPastEndOfFile_ReturnsOutOfRange()
{
IFileSystem fs = CreateFileSystem();
2021-11-14 20:08:57 +01:00
fs.CreateFile("/file", 0, CreateFileOptions.None);
2021-11-14 20:08:57 +01:00
byte[] buffer = new byte[10];
using var file = new UniqueRef<IFile>();
fs.OpenFile(ref file.Ref(), "/file", OpenMode.Read);
Result res = file.Get.Read(out _, 1, buffer, ReadOption.None);
Assert.Result(ResultFs.OutOfRange, res);
2021-11-14 20:08:57 +01:00
}
2021-11-14 20:08:57 +01:00
[Fact]
public void IFileRead_OpenModeNoRead_ReturnsInvalidOpenModeForRead()
{
IFileSystem fs = CreateFileSystem();
2021-11-14 20:08:57 +01:00
fs.CreateFile("/file", 0, CreateFileOptions.None);
2021-11-14 20:08:57 +01:00
byte[] buffer = new byte[10];
using var file = new UniqueRef<IFile>();
fs.OpenFile(ref file.Ref(), "/file", OpenMode.Write);
Result res = file.Get.Read(out _, 0, buffer, ReadOption.None);
Assert.Result(ResultFs.ReadUnpermitted, res);
2021-11-14 20:08:57 +01:00
}
2021-11-14 20:08:57 +01:00
[Fact]
public void IFileRead_NegativeOffset_ReturnsOutOfRange()
{
IFileSystem fs = CreateFileSystem();
2021-11-14 20:08:57 +01:00
fs.CreateFile("/file", 0, CreateFileOptions.None);
2021-11-14 20:08:57 +01:00
byte[] buffer = new byte[10];
using var file = new UniqueRef<IFile>();
fs.OpenFile(ref file.Ref(), "/file", OpenMode.Write);
Result res = file.Get.Read(out _, -5, buffer, ReadOption.None);
Assert.Result(ResultFs.OutOfRange, res);
2021-11-14 20:08:57 +01:00
}
2021-11-14 20:08:57 +01:00
[Fact]
public void IFileRead_OffsetPlusSizeOverflows_ReturnsOutOfRange()
{
IFileSystem fs = CreateFileSystem();
2021-11-14 20:08:57 +01:00
fs.CreateFile("/file", 0, CreateFileOptions.None);
2021-11-14 20:08:57 +01:00
byte[] buffer = new byte[10];
using var file = new UniqueRef<IFile>();
fs.OpenFile(ref file.Ref(), "/file", OpenMode.Write);
Result res = file.Get.Read(out _, long.MaxValue - 5, buffer, ReadOption.None);
Assert.Result(ResultFs.OutOfRange, res);
2021-11-14 20:08:57 +01:00
}
2021-11-14 20:08:57 +01:00
[Fact]
public void IFileRead_FileTooSmallToFillBuffer_BytesReadContainsAvailableByteCount()
{
IFileSystem fs = CreateFileSystem();
2021-11-14 20:08:57 +01:00
fs.CreateFile("/file", 100, CreateFileOptions.None);
2021-11-14 20:08:57 +01:00
byte[] buffer = new byte[200];
using var file = new UniqueRef<IFile>();
fs.OpenFile(ref file.Ref(), "/file", OpenMode.Read);
2021-11-14 20:08:57 +01:00
Assert.Success(file.Get.Read(out long bytesRead, 90, buffer, ReadOption.None));
Assert.Equal(10, bytesRead);
}
2021-11-14 20:08:57 +01:00
[Fact]
public void IFileRead_FileTooSmallToFillBuffer_DoesPartialRead()
{
IFileSystem fs = CreateFileSystem();
2021-11-14 20:08:57 +01:00
fs.CreateFile("/file", 100, CreateFileOptions.None);
2021-11-14 20:08:57 +01:00
// The contents of a created file are undefined, so zero the file
using var file = new UniqueRef<IFile>();
fs.OpenFile(ref file.Ref(), "/file", OpenMode.Write);
file.Get.Write(0, new byte[100], WriteOption.None);
file.Reset();
2020-02-12 10:18:22 +01:00
2021-11-14 20:08:57 +01:00
byte[] bufferExpected = new byte[200];
bufferExpected.AsSpan(10).Fill(0xCC);
2021-11-14 20:08:57 +01:00
byte[] buffer = new byte[200];
buffer.AsSpan().Fill(0xCC);
2021-11-14 20:08:57 +01:00
fs.OpenFile(ref file.Ref(), "/file", OpenMode.Read);
2021-11-14 20:08:57 +01:00
Assert.Success(file.Get.Read(out _, 90, buffer, ReadOption.None));
Assert.Equal(bufferExpected, buffer);
}
2021-11-14 20:08:57 +01:00
}