LibHac/tests/LibHac.Tests/FsSrv/ProgramIndexMapInfoTests.cs

196 lines
6.7 KiB
C#
Raw Normal View History

using System;
using LibHac.Common;
using LibHac.Fs;
using LibHac.Fs.Shim;
using LibHac.FsSrv.Impl;
using LibHac.Ncm;
using LibHac.Util;
using Xunit;
2021-11-14 20:08:57 +01:00
namespace LibHac.Tests.FsSrv;
public class ProgramIndexMapInfoTests
{
2021-11-14 20:08:57 +01:00
[Fact]
public void GetProgramIndexForAccessLog_IsMultiProgram_ReturnsCorrectIndex()
{
2021-11-14 20:08:57 +01:00
const int count = 7;
2021-11-14 20:08:57 +01:00
Horizon hos = HorizonFactory.CreateBasicHorizon();
2021-11-14 20:08:57 +01:00
var programs = new HorizonClient[count];
2021-11-14 20:08:57 +01:00
programs[0] = hos.CreateHorizonClient(new ProgramLocation(new ProgramId(1), StorageId.BuiltInSystem),
AccessControlBits.Bits.RegisterProgramIndexMapInfo);
2021-11-14 20:08:57 +01:00
for (int i = 1; i < programs.Length; i++)
{
programs[i] =
hos.CreateHorizonClient(new ProgramLocation(new ProgramId((ulong)(i + 1)), StorageId.BuiltInSystem),
AccessControlBits.Bits.None);
}
2021-11-14 20:08:57 +01:00
var map = new ProgramIndexMapInfo[count];
2021-11-14 20:08:57 +01:00
for (int i = 0; i < map.Length; i++)
{
map[i].MainProgramId = new Ncm.ApplicationId(1);
2021-11-14 20:08:57 +01:00
map[i].ProgramId = new ProgramId((ulong)(i + 1));
map[i].ProgramIndex = (byte)i;
}
2021-11-14 20:08:57 +01:00
Assert.Success(programs[0].Fs.RegisterProgramIndexMapInfo(map));
2021-11-14 20:08:57 +01:00
for (int i = 0; i < programs.Length; i++)
{
using SharedRef<LibHac.FsSrv.Sf.IFileSystemProxy> fsProxy =
programs[i].Fs.Impl.GetFileSystemProxyServiceObject();
Assert.Success(fsProxy.Get.GetProgramIndexForAccessLog(out int programIndex, out int programCount));
2021-11-14 20:08:57 +01:00
Assert.Equal(i, programIndex);
Assert.Equal(count, programCount);
}
2021-11-14 20:08:57 +01:00
}
2021-11-14 20:08:57 +01:00
private ProgramIndexMapInfoManager CreatePopulatedManager(int count, Func<int, long> idCreator)
{
var manager = new ProgramIndexMapInfoManager();
2021-11-14 20:08:57 +01:00
var map = new ProgramIndexMapInfo[count];
2021-11-14 20:08:57 +01:00
for (int i = 0; i < map.Length; i++)
{
map[i].MainProgramId = new Ncm.ApplicationId((ulong)idCreator(0));
2021-11-14 20:08:57 +01:00
map[i].ProgramId = new ProgramId((ulong)idCreator(i));
map[i].ProgramIndex = (byte)i;
}
2021-11-14 20:08:57 +01:00
Assert.Success(manager.Reset(map));
2021-11-14 20:08:57 +01:00
return manager;
}
2021-11-14 20:08:57 +01:00
[Fact]
public void Get_IdDoesNotExist_ReturnsNull()
{
const int count = 5;
2021-11-14 20:08:57 +01:00
ProgramIndexMapInfoManager manager = CreatePopulatedManager(count, x => x + 3);
2021-11-14 20:08:57 +01:00
Assert.False(manager.Get(new ProgramId(0)).HasValue);
Assert.False(manager.Get(new ProgramId(2)).HasValue);
Assert.False(manager.Get(new ProgramId(8)).HasValue);
Assert.False(manager.Get(new ProgramId(9001)).HasValue);
}
2021-11-14 20:08:57 +01:00
[Fact]
public void Get_IdExists_ReturnsCorrectMapInfo()
{
const int count = 5;
ProgramIndexMapInfoManager manager = CreatePopulatedManager(count, x => x + 1);
// ReSharper disable PossibleInvalidOperationException
Optional<ProgramIndexMapInfo> map = manager.Get(new ProgramId(1));
Assert.True(map.HasValue);
Assert.Equal(new ProgramId(1), map.Value.MainProgramId);
Assert.Equal(new ProgramId(1), map.Value.ProgramId);
Assert.Equal(0, map.Value.ProgramIndex);
map = manager.Get(new ProgramId(4));
Assert.True(map.HasValue);
Assert.Equal(new ProgramId(1), map.Value.MainProgramId);
Assert.Equal(new ProgramId(4), map.Value.ProgramId);
Assert.Equal(3, map.Value.ProgramIndex);
// ReSharper restore PossibleInvalidOperationException
}
2021-11-14 20:08:57 +01:00
[Fact]
public void GetProgramId_WithIndex_ReturnsProgramIdOfSpecifiedIndex()
{
const int count = 5;
ProgramIndexMapInfoManager manager = CreatePopulatedManager(count, x => (x + 1) * 5);
// Check that the program ID can be retrieved using any program in the set
Assert.Equal(new ProgramId(20), manager.GetProgramId(new ProgramId(5), 3));
Assert.Equal(new ProgramId(20), manager.GetProgramId(new ProgramId(10), 3));
Assert.Equal(new ProgramId(20), manager.GetProgramId(new ProgramId(15), 3));
Assert.Equal(new ProgramId(20), manager.GetProgramId(new ProgramId(20), 3));
Assert.Equal(new ProgramId(20), manager.GetProgramId(new ProgramId(25), 3));
// Check that any index can be used
Assert.Equal(new ProgramId(5), manager.GetProgramId(new ProgramId(10), 0));
Assert.Equal(new ProgramId(10), manager.GetProgramId(new ProgramId(10), 1));
Assert.Equal(new ProgramId(15), manager.GetProgramId(new ProgramId(10), 2));
Assert.Equal(new ProgramId(20), manager.GetProgramId(new ProgramId(10), 3));
Assert.Equal(new ProgramId(25), manager.GetProgramId(new ProgramId(10), 4));
}
2021-11-14 20:08:57 +01:00
[Fact]
public void GetProgramId_InputIdDoesNotExist_ReturnsInvalidId()
{
const int count = 5;
2021-11-14 20:08:57 +01:00
ProgramIndexMapInfoManager manager = CreatePopulatedManager(count, x => x + 1);
2021-11-14 20:08:57 +01:00
Assert.Equal(ProgramId.InvalidId, manager.GetProgramId(new ProgramId(0), 3));
Assert.Equal(ProgramId.InvalidId, manager.GetProgramId(new ProgramId(666), 3));
Assert.Equal(ProgramId.InvalidId, manager.GetProgramId(new ProgramId(777), 3));
}
2021-11-14 20:08:57 +01:00
[Fact]
public void GetProgramId_InputIndexDoesNotExist_ReturnsInvalidId()
{
const int count = 5;
2021-11-14 20:08:57 +01:00
ProgramIndexMapInfoManager manager = CreatePopulatedManager(count, x => x + 1);
2021-11-14 20:08:57 +01:00
Assert.Equal(ProgramId.InvalidId, manager.GetProgramId(new ProgramId(2), 5));
Assert.Equal(ProgramId.InvalidId, manager.GetProgramId(new ProgramId(2), 12));
Assert.Equal(ProgramId.InvalidId, manager.GetProgramId(new ProgramId(2), 255));
}
2021-11-14 20:08:57 +01:00
[Fact]
public void GetProgramCount_MapIsEmpty_ReturnsZero()
{
var manager = new ProgramIndexMapInfoManager();
2021-11-14 20:08:57 +01:00
Assert.Equal(0, manager.GetProgramCount());
}
2021-11-14 20:08:57 +01:00
[Fact]
public void GetProgramCount_MapHasEntries_ReturnsCorrectCount()
{
ProgramIndexMapInfoManager manager = CreatePopulatedManager(1, x => x + 1);
Assert.Equal(1, manager.GetProgramCount());
2021-11-14 20:08:57 +01:00
manager = CreatePopulatedManager(10, x => x + 1);
Assert.Equal(10, manager.GetProgramCount());
2021-11-14 20:08:57 +01:00
manager = CreatePopulatedManager(255, x => x + 1);
Assert.Equal(255, manager.GetProgramCount());
}
2021-11-14 20:08:57 +01:00
[Fact]
public void Clear_MapHasEntries_CountIsZero()
{
const int count = 5;
ProgramIndexMapInfoManager manager = CreatePopulatedManager(count, x => x + 1);
2021-11-14 20:08:57 +01:00
manager.Clear();
2021-11-14 20:08:57 +01:00
Assert.Equal(0, manager.GetProgramCount());
}
2021-11-14 20:08:57 +01:00
[Fact]
public void Clear_MapHasEntries_CannotGetOldEntry()
{
const int count = 5;
ProgramIndexMapInfoManager manager = CreatePopulatedManager(count, x => x + 1);
2021-11-14 20:08:57 +01:00
manager.Clear();
2021-11-14 20:08:57 +01:00
Assert.False(manager.Get(new ProgramId(2)).HasValue);
}
2022-01-13 00:29:42 +01:00
}