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;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace LibHac.Tests.Fs.IFileSystemTestBase
|
|
|
|
|
{
|
|
|
|
|
public abstract partial class IFileSystemTests
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public void IFileRead_BytesReadContainsNumberOfBytesRead()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.CreateFile("/file".ToU8Span(), 100, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
|
|
|
|
var buffer = new byte[20];
|
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-06 23:18:03 +02:00
|
|
|
|
Assert.True(file.Read(out long bytesRead, 50, buffer, ReadOptionFlag.None).IsSuccess());
|
2020-01-28 08:55:39 +01:00
|
|
|
|
Assert.Equal(20, bytesRead);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void IFileRead_OffsetPastEndOfFile_ReturnsOutOfRange()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.CreateFile("/file".ToU8Span(), 0, 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-06 23:18:03 +02:00
|
|
|
|
Result rc = file.Read(out _, 1, buffer, ReadOptionFlag.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
Assert.Equal(ResultFs.OutOfRange.Value, rc);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void IFileRead_OpenModeNoRead_ReturnsInvalidOpenModeForRead()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.CreateFile("/file".ToU8Span(), 0, 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-06 23:18:03 +02:00
|
|
|
|
Result rc = file.Read(out _, 0, buffer, ReadOptionFlag.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
Assert.Equal(ResultFs.InvalidOpenModeForRead.Value, rc);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void IFileRead_NegativeOffset_ReturnsOutOfRange()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.CreateFile("/file".ToU8Span(), 0, 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-06 23:18:03 +02:00
|
|
|
|
Result rc = file.Read(out _, -5, buffer, ReadOptionFlag.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
Assert.Equal(ResultFs.OutOfRange.Value, rc);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void IFileRead_OffsetPlusSizeOverflows_ReturnsOutOfRange()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.CreateFile("/file".ToU8Span(), 0, 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-06 23:18:03 +02:00
|
|
|
|
Result rc = file.Read(out _, long.MaxValue - 5, buffer, ReadOptionFlag.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
Assert.Equal(ResultFs.OutOfRange.Value, rc);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void IFileRead_FileTooSmallToFillBuffer_BytesReadContainsAvailableByteCount()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.CreateFile("/file".ToU8Span(), 100, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
|
|
|
|
var buffer = new byte[200];
|
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-06 23:18:03 +02:00
|
|
|
|
Assert.True(file.Read(out long bytesRead, 90, buffer, ReadOptionFlag.None).IsSuccess());
|
2020-01-28 08:55:39 +01:00
|
|
|
|
Assert.Equal(10, bytesRead);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void IFileRead_FileTooSmallToFillBuffer_DoesPartialRead()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.CreateFile("/file".ToU8Span(), 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
|
2020-03-08 09:00:02 +01:00
|
|
|
|
fs.OpenFile(out IFile file, "/file".ToU8Span(), OpenMode.Write);
|
2020-02-12 10:18:22 +01:00
|
|
|
|
using (file)
|
|
|
|
|
{
|
2020-06-06 23:18:03 +02:00
|
|
|
|
file.Write(0, new byte[100], WriteOptionFlag.None);
|
2020-02-12 10:18:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-28 08:55:39 +01:00
|
|
|
|
var bufferExpected = new byte[200];
|
|
|
|
|
bufferExpected.AsSpan(10).Fill(0xCC);
|
|
|
|
|
|
|
|
|
|
var buffer = new byte[200];
|
|
|
|
|
buffer.AsSpan().Fill(0xCC);
|
|
|
|
|
|
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-06 23:18:03 +02:00
|
|
|
|
Assert.True(file.Read(out _, 90, buffer, ReadOptionFlag.None).IsSuccess());
|
2020-01-28 08:55:39 +01:00
|
|
|
|
Assert.Equal(bufferExpected, buffer);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|