From b346280bc930bfbb489c31dccb63b8b9aed92b6c Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Sun, 30 May 2021 16:29:42 -0700 Subject: [PATCH] Make SaveDataManagement functions public and add tests --- src/LibHac/Fs/Shim/SaveDataManagement.cs | 16 ++++---- .../FileSystemServerFactory.cs | 13 +++++- .../ShimTests/SaveDataManagement.cs | 40 +++++++++++++++++++ 3 files changed, 60 insertions(+), 9 deletions(-) diff --git a/src/LibHac/Fs/Shim/SaveDataManagement.cs b/src/LibHac/Fs/Shim/SaveDataManagement.cs index d0ce841a..be722d48 100644 --- a/src/LibHac/Fs/Shim/SaveDataManagement.cs +++ b/src/LibHac/Fs/Shim/SaveDataManagement.cs @@ -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 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 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 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); diff --git a/tests/LibHac.Tests/Fs/FileSystemClientTests/FileSystemServerFactory.cs b/tests/LibHac.Tests/Fs/FileSystemClientTests/FileSystemServerFactory.cs index adb4633b..ef38f2bc 100644 --- a/tests/LibHac.Tests/Fs/FileSystemClientTests/FileSystemServerFactory.cs +++ b/tests/LibHac.Tests/Fs/FileSystemClientTests/FileSystemServerFactory.cs @@ -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 _); + } } } diff --git a/tests/LibHac.Tests/Fs/FileSystemClientTests/ShimTests/SaveDataManagement.cs b/tests/LibHac.Tests/Fs/FileSystemClientTests/ShimTests/SaveDataManagement.cs index 3b52b6be..e6a26a97 100644 --- a/tests/LibHac.Tests/Fs/FileSystemClientTests/ShimTests/SaveDataManagement.cs +++ b/tests/LibHac.Tests/Fs/FileSystemClientTests/ShimTests/SaveDataManagement.cs @@ -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 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() {