Files
Aaru/Aaru.Tests/Filesystems/FilesystemTest.cs

266 lines
9.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using Aaru.CommonTypes;
using Aaru.CommonTypes.AaruMetadata;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
using Aaru.Core;
using NUnit.Framework;
using File = System.IO.File;
using Partition = Aaru.CommonTypes.Partition;
namespace Aaru.Tests.Filesystems;
2023-10-04 08:29:56 +01:00
public abstract class FilesystemTest(string fileSystemType)
{
2023-10-04 08:29:56 +01:00
protected FilesystemTest() : this(null) {}
2022-03-06 13:29:38 +00:00
public abstract string DataFolder { get; }
public abstract IFilesystem Plugin { get; }
public abstract bool Partitions { get; }
2022-03-06 13:29:38 +00:00
public abstract FileSystemTest[] Tests { get; }
[OneTimeSetUp]
public void InitTest() => PluginBase.Init();
2022-03-06 13:29:38 +00:00
[Test]
public void Detect()
{
Environment.CurrentDirectory = DataFolder;
2022-03-06 13:29:38 +00:00
Assert.Multiple(() =>
{
foreach(FileSystemTest test in Tests)
{
2022-03-06 13:29:38 +00:00
string testFile = test.TestFile;
2022-03-06 13:29:38 +00:00
bool exists = File.Exists(testFile);
Assert.True(exists, string.Format(Localization._0_not_found, testFile));
2022-03-06 13:29:38 +00:00
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
// It arrives here...
2024-05-01 04:05:22 +01:00
if(!exists) continue;
IFilter inputFilter = PluginRegister.Singleton.GetFilter(testFile);
Assert.IsNotNull(inputFilter, string.Format(Localization.Filter_0, testFile));
2022-03-07 07:36:44 +00:00
var image = ImageFormat.Detect(inputFilter) as IMediaImage;
Assert.IsNotNull(image, string.Format(Localization.Image_format_0, testFile));
2024-05-01 04:05:22 +01:00
Assert.AreEqual(ErrorNumber.NoError,
image.Open(inputFilter),
string.Format(Localization.Cannot_open_image_for_0, testFile));
2022-03-06 13:29:38 +00:00
List<string> idPlugins;
2022-03-06 13:29:38 +00:00
if(Partitions)
{
List<Partition> partitionsList = Core.Partitions.GetAll(image);
2024-05-01 04:05:22 +01:00
Assert.Greater(partitionsList.Count,
0,
string.Format(Localization.No_partitions_found_for_0, testFile));
2023-10-03 23:44:33 +01:00
var found = false;
2022-03-06 13:29:38 +00:00
foreach(Partition p in partitionsList)
{
Core.Filesystems.Identify(image, out idPlugins, p, true);
2024-05-01 04:05:22 +01:00
if(idPlugins.Count == 0) continue;
2024-05-01 04:05:22 +01:00
if(!idPlugins.Contains(Plugin.Id.ToString())) continue;
2022-03-06 13:29:38 +00:00
found = true;
2022-03-06 13:29:38 +00:00
break;
}
2022-03-06 13:29:38 +00:00
Assert.True(found, string.Format(Localization.Filesystem_not_identified_for_0, testFile));
2022-03-06 13:29:38 +00:00
}
else
{
var wholePart = new Partition
{
2022-03-06 13:29:38 +00:00
Name = "Whole device",
Length = image.Info.Sectors,
Size = image.Info.Sectors * image.Info.SectorSize
};
Core.Filesystems.Identify(image, out idPlugins, wholePart, true);
2024-05-01 04:05:22 +01:00
Assert.Greater(idPlugins.Count,
0,
string.Format(Localization.No_filesystems_found_for_0, testFile));
Assert.True(idPlugins.Contains(Plugin.Id.ToString()),
string.Format(Localization.Not_identified_for_0, testFile));
}
2022-03-06 13:29:38 +00:00
}
});
}
2022-03-06 13:29:38 +00:00
[Test]
public void ImageInfo()
{
Environment.CurrentDirectory = DataFolder;
2022-03-06 13:29:38 +00:00
Assert.Multiple(() =>
{
foreach(FileSystemTest test in Tests)
{
2022-03-06 13:29:38 +00:00
string testFile = test.TestFile;
2022-03-06 13:29:38 +00:00
bool exists = File.Exists(testFile);
Assert.True(exists, string.Format(Localization._0_not_found, testFile));
2022-03-06 13:29:38 +00:00
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
// It arrives here...
2024-05-01 04:05:22 +01:00
if(!exists) continue;
IFilter inputFilter = PluginRegister.Singleton.GetFilter(testFile);
Assert.IsNotNull(inputFilter, string.Format(Localization.Filter_0, testFile));
2022-03-07 07:36:44 +00:00
var image = ImageFormat.Detect(inputFilter) as IMediaImage;
Assert.IsNotNull(image, string.Format(Localization.Image_format_0, testFile));
2024-05-01 04:05:22 +01:00
Assert.AreEqual(ErrorNumber.NoError,
image.Open(inputFilter),
string.Format(Localization.Cannot_open_image_for_0, testFile));
2023-10-03 23:44:33 +01:00
Assert.AreEqual(test.MediaType, image.Info.MediaType, testFile);
Assert.AreEqual(test.Sectors, image.Info.Sectors, testFile);
2022-03-06 13:29:38 +00:00
Assert.AreEqual(test.SectorSize, image.Info.SectorSize, testFile);
}
});
}
2022-03-06 13:29:38 +00:00
[Test]
public void Info()
{
Environment.CurrentDirectory = DataFolder;
2022-03-06 13:29:38 +00:00
Assert.Multiple(() =>
{
foreach(FileSystemTest test in Tests)
{
2022-03-06 13:29:38 +00:00
string testFile = test.TestFile;
2023-10-03 23:44:33 +01:00
var found = false;
2022-03-06 13:29:38 +00:00
var partition = new Partition();
2022-03-06 13:29:38 +00:00
bool exists = File.Exists(testFile);
Assert.True(exists, string.Format(Localization._0_not_found, testFile));
2022-03-06 13:29:38 +00:00
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
// It arrives here...
2024-05-01 04:05:22 +01:00
if(!exists) continue;
IFilter inputFilter = PluginRegister.Singleton.GetFilter(testFile);
Assert.IsNotNull(inputFilter, string.Format(Localization.Filter_0, testFile));
2022-03-07 07:36:44 +00:00
var image = ImageFormat.Detect(inputFilter) as IMediaImage;
Assert.IsNotNull(image, string.Format(Localization.Image_format_0, testFile));
2024-05-01 04:05:22 +01:00
Assert.AreEqual(ErrorNumber.NoError,
image.Open(inputFilter),
string.Format(Localization.Cannot_open_image_for_0, testFile));
2022-03-06 13:29:38 +00:00
List<string> idPlugins;
2022-03-06 13:29:38 +00:00
if(Partitions)
{
List<Partition> partitionsList = Core.Partitions.GetAll(image);
2024-05-01 04:05:22 +01:00
Assert.Greater(partitionsList.Count,
0,
string.Format(Localization.No_partitions_found_for_0, testFile));
2022-03-06 13:29:38 +00:00
// In reverse to skip boot partitions we're not interested in
for(int index = partitionsList.Count - 1; index >= 0; index--)
{
Core.Filesystems.Identify(image, out idPlugins, partitionsList[index], true);
2024-05-01 04:05:22 +01:00
if(idPlugins.Count == 0) continue;
2024-05-01 04:05:22 +01:00
if(!idPlugins.Contains(Plugin.Id.ToString())) continue;
2022-03-06 13:29:38 +00:00
found = true;
partition = partitionsList[index];
2022-03-06 13:29:38 +00:00
break;
}
2022-03-06 13:29:38 +00:00
}
else
{
partition = new Partition
{
2022-03-06 13:29:38 +00:00
Name = "Whole device",
Length = image.Info.Sectors,
Size = image.Info.Sectors * image.Info.SectorSize
};
Core.Filesystems.Identify(image, out idPlugins, partition, true);
2024-05-01 04:05:22 +01:00
Assert.Greater(idPlugins.Count,
0,
string.Format(Localization.No_filesystems_found_for_0, testFile));
2022-03-06 13:29:38 +00:00
found = idPlugins.Contains(Plugin.Id.ToString());
}
Assert.True(found, string.Format(Localization.Filesystem_not_identified_for_0, testFile));
2022-03-06 13:29:38 +00:00
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
// It is not the case, it changes
2024-05-01 04:05:22 +01:00
if(!found) continue;
2022-03-06 13:29:38 +00:00
var fs = Activator.CreateInstance(Plugin.GetType()) as IFilesystem;
Assert.NotNull(fs, string.Format(Localization.Could_not_instantiate_filesystem_for_0, testFile));
fs.GetInformation(image, partition, null, out _, out FileSystem fsMetadata);
2022-03-06 13:29:38 +00:00
if(test.ApplicationId != null)
2023-10-03 23:44:33 +01:00
{
2024-05-01 04:05:22 +01:00
Assert.AreEqual(test.ApplicationId,
fsMetadata.ApplicationIdentifier,
string.Format(Localization.Application_ID_0, testFile));
2023-10-03 23:44:33 +01:00
}
Assert.AreEqual(test.Bootable, fsMetadata.Bootable, string.Format(Localization.Bootable_0, testFile));
Assert.AreEqual(test.Clusters, fsMetadata.Clusters, string.Format(Localization.Clusters_0, testFile));
2024-05-01 04:05:22 +01:00
Assert.AreEqual(test.ClusterSize,
fsMetadata.ClusterSize,
string.Format(Localization.Cluster_size_0, testFile));
2022-03-06 13:29:38 +00:00
if(test.SystemId != null)
2023-10-03 23:44:33 +01:00
{
2024-05-01 04:05:22 +01:00
Assert.AreEqual(test.SystemId,
fsMetadata.SystemIdentifier,
string.Format(Localization.System_ID_0, testFile));
2023-10-03 23:44:33 +01:00
}
2024-05-01 04:05:22 +01:00
Assert.AreEqual(fileSystemType ?? test.Type,
fsMetadata.Type,
string.Format(Localization.Filesystem_type_0, testFile));
2024-05-01 04:05:22 +01:00
Assert.AreEqual(test.VolumeName,
fsMetadata.VolumeName,
string.Format(Localization.Volume_name_0, testFile));
2024-05-01 04:05:22 +01:00
Assert.AreEqual(test.VolumeSerial,
fsMetadata.VolumeSerial,
string.Format(Localization.Volume_serial_0, testFile));
2022-03-06 13:29:38 +00:00
}
});
}
}