Make SaveDataManagement functions public and add tests

This commit is contained in:
Alex Barney 2021-05-30 16:29:42 -07:00
parent 3056c5c296
commit b346280bc9
3 changed files with 60 additions and 9 deletions

View file

@ -67,7 +67,7 @@ namespace LibHac.Fs.Shim
[SkipLocalsInit]
public static class SaveDataManagement
{
internal static Result ReadSaveDataFileSystemExtraData(this FileSystemClientImpl fs,
public static Result ReadSaveDataFileSystemExtraData(this FileSystemClientImpl fs,
out SaveDataExtraData extraData, ulong saveDataId)
{
UnsafeHelpers.SkipParamInit(out extraData);
@ -80,7 +80,7 @@ namespace LibHac.Fs.Shim
return Result.Success;
}
internal static Result ReadSaveDataFileSystemExtraData(this FileSystemClientImpl fs,
public static Result ReadSaveDataFileSystemExtraData(this FileSystemClientImpl fs,
out SaveDataExtraData extraData, SaveDataSpaceId spaceId, ulong saveDataId)
{
UnsafeHelpers.SkipParamInit(out extraData);
@ -94,7 +94,7 @@ namespace LibHac.Fs.Shim
return Result.Success;
}
internal static Result ReadSaveDataFileSystemExtraData(this FileSystemClientImpl fs,
public static Result ReadSaveDataFileSystemExtraData(this FileSystemClientImpl fs,
out SaveDataExtraData extraData, SaveDataSpaceId spaceId, in SaveDataAttribute attribute)
{
UnsafeHelpers.SkipParamInit(out extraData);
@ -108,7 +108,7 @@ namespace LibHac.Fs.Shim
return Result.Success;
}
internal static Result ReadSaveDataFileSystemExtraData(this FileSystemClientImpl fs,
public static Result ReadSaveDataFileSystemExtraData(this FileSystemClientImpl fs,
out SaveDataExtraData extraData, SaveDataSpaceId spaceId, in SaveDataAttribute attribute,
in SaveDataExtraData extraDataMask)
{
@ -123,7 +123,7 @@ namespace LibHac.Fs.Shim
return Result.Success;
}
internal static Result WriteSaveDataFileSystemExtraData(this FileSystemClientImpl fs, SaveDataSpaceId spaceId,
public static Result WriteSaveDataFileSystemExtraData(this FileSystemClientImpl fs, SaveDataSpaceId spaceId,
ulong saveDataId, in SaveDataExtraData extraData)
{
using ReferenceCountedDisposable<IFileSystemProxy> fsProxy = fs.GetFileSystemProxyServiceObject();
@ -134,7 +134,7 @@ namespace LibHac.Fs.Shim
return rc;
}
internal static Result WriteSaveDataFileSystemExtraData(this FileSystemClientImpl fs, SaveDataSpaceId spaceId,
public static Result WriteSaveDataFileSystemExtraData(this FileSystemClientImpl fs, SaveDataSpaceId spaceId,
ulong saveDataId, in SaveDataExtraData extraData, in SaveDataExtraData extraDataMask)
{
using ReferenceCountedDisposable<IFileSystemProxy> fsProxy = fs.GetFileSystemProxyServiceObject();
@ -145,7 +145,7 @@ namespace LibHac.Fs.Shim
return rc;
}
internal static Result WriteSaveDataFileSystemExtraData(this FileSystemClientImpl fs, SaveDataSpaceId spaceId,
public static Result WriteSaveDataFileSystemExtraData(this FileSystemClientImpl fs, SaveDataSpaceId spaceId,
in SaveDataAttribute attribute, in SaveDataExtraData extraData, in SaveDataExtraData extraDataMask)
{
using ReferenceCountedDisposable<IFileSystemProxy> fsProxy = fs.GetFileSystemProxyServiceObject();
@ -156,7 +156,7 @@ namespace LibHac.Fs.Shim
return rc;
}
internal static Result FindSaveDataWithFilter(this FileSystemClientImpl fs, out SaveDataInfo saveInfo,
public static Result FindSaveDataWithFilter(this FileSystemClientImpl fs, out SaveDataInfo saveInfo,
SaveDataSpaceId spaceId, in SaveDataFilter filter)
{
UnsafeHelpers.SkipParamInit(out saveInfo);

View file

@ -7,7 +7,7 @@ namespace LibHac.Tests.Fs.FileSystemClientTests
{
public static class FileSystemServerFactory
{
private static FileSystemClient CreateClientImpl(bool sdCardInserted, out IFileSystem rootFs)
private static Horizon CreateHorizonImpl(bool sdCardInserted, out IFileSystem rootFs)
{
rootFs = new InMemoryFileSystem();
var keySet = new KeySet();
@ -27,6 +27,12 @@ namespace LibHac.Tests.Fs.FileSystemClientTests
config.ExternalKeySet = new ExternalKeySet();
FileSystemServerInitializer.InitializeWithConfig(fsServerClient, fsServer, config);
return horizon;
}
private static FileSystemClient CreateClientImpl(bool sdCardInserted, out IFileSystem rootFs)
{
Horizon horizon = CreateHorizonImpl(sdCardInserted, out rootFs);
HorizonClient horizonClient = horizon.CreatePrivilegedHorizonClient();
@ -42,5 +48,10 @@ namespace LibHac.Tests.Fs.FileSystemClientTests
{
return CreateClientImpl(false, out rootFs);
}
public static Horizon CreateHorizonServer()
{
return CreateHorizonImpl(true, out _);
}
}
}

View file

@ -3,6 +3,8 @@ using System.Linq;
using LibHac.Common;
using LibHac.Fs;
using LibHac.Fs.Shim;
using LibHac.FsSrv.Impl;
using LibHac.Ncm;
using LibHac.Time;
using Xunit;
@ -244,6 +246,44 @@ namespace LibHac.Tests.Fs.FileSystemClientTests.ShimTests
Assert.Equal(journalSize, actualJournalSize);
}
[Fact]
public void CreateSaveData_FromSubProgram_CreatesSaveDataForMainProgram()
{
Horizon hos = FileSystemServerFactory.CreateHorizonServer();
Span<ProgramIndexMapInfo> mapInfo = stackalloc ProgramIndexMapInfo[5];
var mainProgramId = new ProgramId(0x123456);
var programId = new ProgramId(mainProgramId.Value + 2);
for (int i = 0; i < mapInfo.Length; i++)
{
mapInfo[i].MainProgramId = mainProgramId;
mapInfo[i].ProgramId = new ProgramId(mainProgramId.Value + (uint)i);
mapInfo[i].ProgramIndex = (byte)i;
}
HorizonClient client = hos.CreatePrivilegedHorizonClient();
HorizonClient subProgramClient =
hos.CreateHorizonClient(new ProgramLocation(programId, StorageId.BuiltInUser),
AccessControlBits.Bits.CreateSaveData);
Assert.Success(client.Fs.RegisterProgramIndexMapInfo(mapInfo));
Assert.Success(subProgramClient.Fs.CreateSaveData(Ncm.ApplicationId.InvalidId, UserId.InvalidId, 0, 0x4000,
0x4000, SaveDataFlags.None));
// Get the created save data's ID
Assert.Success(client.Fs.OpenSaveDataIterator(out SaveDataIterator iterator, SaveDataSpaceId.User));
var info = new SaveDataInfo[2];
iterator.ReadSaveDataInfo(out long entriesRead, info);
Assert.Equal(1, entriesRead);
Assert.Equal(mainProgramId, info[0].ProgramId);
}
[Fact]
public void DeleteSaveData_DoesNotExist_ReturnsTargetNotFound()
{