2020-02-19 07:12:23 +01:00
|
|
|
|
using LibHac.Account;
|
2021-08-21 00:33:26 +02:00
|
|
|
|
using LibHac.Common;
|
2020-02-19 07:12:23 +01:00
|
|
|
|
using LibHac.Fs;
|
|
|
|
|
using LibHac.Fs.Shim;
|
|
|
|
|
using LibHac.Ns;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
using static LibHac.Fs.ApplicationSaveDataManagement;
|
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
namespace LibHac.Tests.Fs.FileSystemClientTests;
|
|
|
|
|
|
|
|
|
|
public class ApplicationSaveDataManagementTests
|
2020-02-19 07:12:23 +01:00
|
|
|
|
{
|
2021-11-14 20:08:57 +01:00
|
|
|
|
[Fact]
|
|
|
|
|
public static void EnsureApplicationSaveData_CreatesAccountSaveData()
|
2020-02-19 07:12:23 +01:00
|
|
|
|
{
|
2021-11-14 20:08:57 +01:00
|
|
|
|
FileSystemClient fs = FileSystemServerFactory.CreateClient(true);
|
|
|
|
|
|
|
|
|
|
var applicationId = new Ncm.ApplicationId(11);
|
|
|
|
|
var userId = new Uid(2, 3);
|
|
|
|
|
|
|
|
|
|
var nacp = new ApplicationControlProperty
|
2020-02-19 07:12:23 +01:00
|
|
|
|
{
|
2021-11-14 20:08:57 +01:00
|
|
|
|
UserAccountSaveDataSize = 0x1000,
|
|
|
|
|
UserAccountSaveDataJournalSize = 0x1000
|
|
|
|
|
};
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Success(EnsureApplicationSaveData(fs, out _, applicationId, ref nacp, ref userId));
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
using var iterator = new UniqueRef<SaveDataIterator>();
|
|
|
|
|
fs.OpenSaveDataIterator(ref iterator.Ref(), SaveDataSpaceId.User);
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
var info = new SaveDataInfo[2];
|
|
|
|
|
Assert.Success(iterator.Get.ReadSaveDataInfo(out long entriesRead, info));
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Equal(1, entriesRead);
|
|
|
|
|
Assert.Equal(applicationId, info[0].ProgramId);
|
2022-01-19 04:45:48 +01:00
|
|
|
|
Assert.Equal(Utility.ConvertAccountUidToFsUserId(userId), info[0].UserId);
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Equal(SaveDataType.Account, info[0].Type);
|
|
|
|
|
}
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
[Fact]
|
|
|
|
|
public static void EnsureApplicationSaveData_CreatesDeviceSaveData()
|
|
|
|
|
{
|
|
|
|
|
FileSystemClient fs = FileSystemServerFactory.CreateClient(true);
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
var applicationId = new Ncm.ApplicationId(11);
|
|
|
|
|
var userId = new Uid(2, 3);
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
var nacp = new ApplicationControlProperty
|
2020-02-19 07:12:23 +01:00
|
|
|
|
{
|
2021-11-14 20:08:57 +01:00
|
|
|
|
DeviceSaveDataSize = 0x1000,
|
|
|
|
|
DeviceSaveDataJournalSize = 0x1000
|
|
|
|
|
};
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Success(EnsureApplicationSaveData(fs, out _, applicationId, ref nacp, ref userId));
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
using var iterator = new UniqueRef<SaveDataIterator>();
|
|
|
|
|
fs.OpenSaveDataIterator(ref iterator.Ref(), SaveDataSpaceId.User);
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
var info = new SaveDataInfo[2];
|
|
|
|
|
Assert.Success(iterator.Get.ReadSaveDataInfo(out long entriesRead, info));
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Equal(1, entriesRead);
|
|
|
|
|
Assert.Equal(applicationId, info[0].ProgramId);
|
|
|
|
|
Assert.Equal(UserId.InvalidId, info[0].UserId);
|
|
|
|
|
Assert.Equal(SaveDataType.Device, info[0].Type);
|
|
|
|
|
}
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
[Fact]
|
|
|
|
|
public static void EnsureApplicationSaveData_CreatesBcatCacheStorage()
|
|
|
|
|
{
|
|
|
|
|
FileSystemClient fs = FileSystemServerFactory.CreateClient(true);
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
var applicationId = new Ncm.ApplicationId(11);
|
|
|
|
|
var userId = new Uid(2, 3);
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
var nacp = new ApplicationControlProperty
|
2020-02-19 07:12:23 +01:00
|
|
|
|
{
|
2021-11-14 20:08:57 +01:00
|
|
|
|
BcatDeliveryCacheStorageSize = 0x1000
|
|
|
|
|
};
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Success(EnsureApplicationSaveData(fs, out _, applicationId, ref nacp, ref userId));
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
using var iterator = new UniqueRef<SaveDataIterator>();
|
|
|
|
|
fs.OpenSaveDataIterator(ref iterator.Ref(), SaveDataSpaceId.User);
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
var info = new SaveDataInfo[2];
|
|
|
|
|
Assert.Success(iterator.Get.ReadSaveDataInfo(out long entriesRead, info));
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Equal(1, entriesRead);
|
|
|
|
|
Assert.Equal(applicationId, info[0].ProgramId);
|
|
|
|
|
Assert.Equal(UserId.InvalidId, info[0].UserId);
|
|
|
|
|
Assert.Equal(SaveDataType.Bcat, info[0].Type);
|
|
|
|
|
}
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
[Fact]
|
|
|
|
|
public static void EnsureApplicationSaveData_CreatesTemporaryStorage()
|
|
|
|
|
{
|
|
|
|
|
FileSystemClient fs = FileSystemServerFactory.CreateClient(true);
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
var applicationId = new Ncm.ApplicationId(11);
|
|
|
|
|
var userId = new Uid(2, 3);
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
var nacp = new ApplicationControlProperty
|
2020-02-19 07:12:23 +01:00
|
|
|
|
{
|
2021-11-14 20:08:57 +01:00
|
|
|
|
TemporaryStorageSize = 0x1000
|
|
|
|
|
};
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Success(EnsureApplicationSaveData(fs, out _, applicationId, ref nacp, ref userId));
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
using var iterator = new UniqueRef<SaveDataIterator>();
|
|
|
|
|
fs.OpenSaveDataIterator(ref iterator.Ref(), SaveDataSpaceId.Temporary);
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
var info = new SaveDataInfo[2];
|
|
|
|
|
Assert.Success(iterator.Get.ReadSaveDataInfo(out long entriesRead, info));
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Equal(1, entriesRead);
|
|
|
|
|
Assert.Equal(applicationId, info[0].ProgramId);
|
|
|
|
|
Assert.Equal(UserId.InvalidId, info[0].UserId);
|
|
|
|
|
Assert.Equal(SaveDataType.Temporary, info[0].Type);
|
|
|
|
|
}
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
[Fact]
|
|
|
|
|
public static void EnsureApplicationCacheStorage_SdCardAvailable_CreatesCacheStorageOnSd()
|
|
|
|
|
{
|
|
|
|
|
FileSystemClient fs = FileSystemServerFactory.CreateClient(true);
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
var applicationId = new Ncm.ApplicationId(11);
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
var nacp = new ApplicationControlProperty
|
2020-02-19 07:12:23 +01:00
|
|
|
|
{
|
2021-11-14 20:08:57 +01:00
|
|
|
|
CacheStorageSize = 0x1000,
|
|
|
|
|
CacheStorageJournalSize = 0x1000
|
|
|
|
|
};
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Success(fs.EnsureApplicationCacheStorage(out _, out CacheStorageTargetMedia target, applicationId,
|
|
|
|
|
ref nacp));
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Equal(CacheStorageTargetMedia.SdCard, target);
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
using var iterator = new UniqueRef<SaveDataIterator>();
|
2021-12-29 18:13:42 +01:00
|
|
|
|
fs.OpenSaveDataIterator(ref iterator.Ref(), SaveDataSpaceId.SdUser);
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
var info = new SaveDataInfo[2];
|
|
|
|
|
Assert.Success(iterator.Get.ReadSaveDataInfo(out long entriesRead, info));
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Equal(1, entriesRead);
|
|
|
|
|
Assert.Equal(applicationId, info[0].ProgramId);
|
|
|
|
|
Assert.Equal(SaveDataType.Cache, info[0].Type);
|
|
|
|
|
}
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
[Fact]
|
|
|
|
|
public static void EnsureApplicationCacheStorage_SdCardNotAvailable_CreatesCacheStorageOnBis()
|
|
|
|
|
{
|
|
|
|
|
FileSystemClient fs = FileSystemServerFactory.CreateClient(false);
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
var applicationId = new Ncm.ApplicationId(11);
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
var nacp = new ApplicationControlProperty
|
2020-02-19 07:12:23 +01:00
|
|
|
|
{
|
2021-11-14 20:08:57 +01:00
|
|
|
|
CacheStorageSize = 0x1000,
|
|
|
|
|
CacheStorageJournalSize = 0x1000
|
|
|
|
|
};
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Success(fs.EnsureApplicationCacheStorage(out _, out CacheStorageTargetMedia target, applicationId,
|
|
|
|
|
ref nacp));
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Equal(CacheStorageTargetMedia.Nand, target);
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
using var iterator = new UniqueRef<SaveDataIterator>();
|
|
|
|
|
fs.OpenSaveDataIterator(ref iterator.Ref(), SaveDataSpaceId.User);
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
var info = new SaveDataInfo[2];
|
|
|
|
|
Assert.Success(iterator.Get.ReadSaveDataInfo(out long entriesRead, info));
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Equal(1, entriesRead);
|
|
|
|
|
Assert.Equal(applicationId, info[0].ProgramId);
|
|
|
|
|
Assert.Equal(SaveDataType.Cache, info[0].Type);
|
|
|
|
|
}
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(true)]
|
|
|
|
|
[InlineData(false)]
|
|
|
|
|
public static void GetCacheStorageTargetMedia_ReturnsTargetOfNewCacheStorage(bool isSdCardInserted)
|
|
|
|
|
{
|
|
|
|
|
FileSystemClient fs = FileSystemServerFactory.CreateClient(isSdCardInserted);
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
var applicationId = new Ncm.ApplicationId(11);
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
var nacp = new ApplicationControlProperty
|
2020-02-19 07:12:23 +01:00
|
|
|
|
{
|
2021-11-14 20:08:57 +01:00
|
|
|
|
CacheStorageSize = 0x1000,
|
|
|
|
|
CacheStorageJournalSize = 0x1000
|
|
|
|
|
};
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
fs.EnsureApplicationCacheStorage(out _, out CacheStorageTargetMedia targetFromCreation, applicationId, ref nacp);
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Success(fs.GetCacheStorageTargetMedia(out CacheStorageTargetMedia target, applicationId));
|
|
|
|
|
Assert.Equal(targetFromCreation, target);
|
|
|
|
|
}
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
[Fact]
|
|
|
|
|
public static void GetCacheStorageTargetMedia_CacheStorageDoesNotExist_ReturnsNone()
|
|
|
|
|
{
|
|
|
|
|
FileSystemClient fs = FileSystemServerFactory.CreateClient(true);
|
2020-02-19 07:12:23 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Success(fs.GetCacheStorageTargetMedia(out CacheStorageTargetMedia target, new Ncm.ApplicationId(11)));
|
|
|
|
|
Assert.Equal(CacheStorageTargetMedia.None, target);
|
2020-02-19 07:12:23 +01:00
|
|
|
|
}
|
2021-12-29 18:13:42 +01:00
|
|
|
|
}
|