2021-03-01 19:34:30 +00:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2021-03-05 16:36:51 +00:00
|
|
|
using System.IO;
|
2021-03-01 19:34:30 +00:00
|
|
|
using Aaru.CommonTypes;
|
|
|
|
|
using Aaru.CommonTypes.Interfaces;
|
|
|
|
|
using Aaru.Core;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
|
|
|
|
namespace Aaru.Tests.Filesystems
|
|
|
|
|
{
|
|
|
|
|
public abstract class FilesystemTest
|
|
|
|
|
{
|
|
|
|
|
readonly string _fileSystemType;
|
|
|
|
|
|
2021-03-01 21:33:58 +00:00
|
|
|
public FilesystemTest() => _fileSystemType = null;
|
|
|
|
|
|
2021-03-01 19:34:30 +00:00
|
|
|
public FilesystemTest(string fileSystemType) => _fileSystemType = fileSystemType;
|
|
|
|
|
|
2021-03-01 21:33:58 +00:00
|
|
|
public abstract string DataFolder { get; }
|
|
|
|
|
public abstract IFilesystem Plugin { get; }
|
|
|
|
|
public abstract bool Partitions { get; }
|
2021-03-01 21:18:02 +00:00
|
|
|
|
|
|
|
|
public abstract FileSystemTest[] Tests { get; }
|
2021-03-01 19:34:30 +00:00
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Detect()
|
|
|
|
|
{
|
2021-03-01 21:33:58 +00:00
|
|
|
Environment.CurrentDirectory = DataFolder;
|
2021-03-01 19:34:30 +00:00
|
|
|
|
|
|
|
|
Assert.Multiple(() =>
|
|
|
|
|
{
|
2021-03-01 21:18:02 +00:00
|
|
|
foreach(FileSystemTest test in Tests)
|
2021-03-01 19:34:30 +00:00
|
|
|
{
|
2021-03-05 16:36:51 +00:00
|
|
|
string testFile = test.TestFile;
|
|
|
|
|
|
|
|
|
|
bool exists = File.Exists(testFile);
|
|
|
|
|
Assert.True(exists, $"{testFile} not found");
|
|
|
|
|
|
|
|
|
|
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
|
|
|
|
|
// It arrives here...
|
|
|
|
|
if(!exists)
|
|
|
|
|
continue;
|
|
|
|
|
|
2021-03-01 19:34:30 +00:00
|
|
|
var filtersList = new FiltersList();
|
|
|
|
|
IFilter inputFilter = filtersList.GetFilter(testFile);
|
|
|
|
|
|
|
|
|
|
Assert.IsNotNull(inputFilter, $"Filter: {testFile}");
|
|
|
|
|
|
|
|
|
|
IMediaImage image = ImageFormat.Detect(inputFilter);
|
|
|
|
|
|
|
|
|
|
Assert.IsNotNull(image, $"Image format: {testFile}");
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(true, image.Open(inputFilter), $"Cannot open image for {testFile}");
|
|
|
|
|
|
|
|
|
|
List<string> idPlugins;
|
|
|
|
|
|
2021-03-01 21:33:58 +00:00
|
|
|
if(Partitions)
|
2021-03-01 19:34:30 +00:00
|
|
|
{
|
|
|
|
|
List<Partition> partitionsList = Core.Partitions.GetAll(image);
|
|
|
|
|
|
|
|
|
|
Assert.Greater(partitionsList.Count, 0, $"No partitions found for {testFile}");
|
|
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
|
|
foreach(Partition p in partitionsList)
|
|
|
|
|
{
|
|
|
|
|
Core.Filesystems.Identify(image, out idPlugins, p, true);
|
|
|
|
|
|
|
|
|
|
if(idPlugins.Count == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
2021-03-01 21:33:58 +00:00
|
|
|
if(!idPlugins.Contains(Plugin.Id.ToString()))
|
2021-03-01 19:34:30 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
found = true;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Assert.True(found, $"Filesystem not identified for {testFile}");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var wholePart = new Partition
|
|
|
|
|
{
|
|
|
|
|
Name = "Whole device",
|
|
|
|
|
Length = image.Info.Sectors,
|
|
|
|
|
Size = image.Info.Sectors * image.Info.SectorSize
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Core.Filesystems.Identify(image, out idPlugins, wholePart, true);
|
|
|
|
|
|
|
|
|
|
Assert.Greater(idPlugins.Count, 0, $"No filesystems found for {testFile}");
|
|
|
|
|
|
2021-03-01 21:33:58 +00:00
|
|
|
Assert.True(idPlugins.Contains(Plugin.Id.ToString()), $"Not identified for {testFile}");
|
2021-03-01 19:34:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void ImageInfo()
|
|
|
|
|
{
|
2021-03-01 21:33:58 +00:00
|
|
|
Environment.CurrentDirectory = DataFolder;
|
2021-03-01 19:34:30 +00:00
|
|
|
|
|
|
|
|
Assert.Multiple(() =>
|
|
|
|
|
{
|
2021-03-01 21:18:02 +00:00
|
|
|
foreach(FileSystemTest test in Tests)
|
2021-03-01 19:34:30 +00:00
|
|
|
{
|
2021-03-05 16:36:51 +00:00
|
|
|
string testFile = test.TestFile;
|
|
|
|
|
|
|
|
|
|
bool exists = File.Exists(testFile);
|
|
|
|
|
Assert.True(exists, $"{testFile} not found");
|
|
|
|
|
|
|
|
|
|
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
|
|
|
|
|
// It arrives here...
|
|
|
|
|
if(!exists)
|
|
|
|
|
continue;
|
|
|
|
|
|
2021-03-01 19:34:30 +00:00
|
|
|
var filtersList = new FiltersList();
|
|
|
|
|
IFilter inputFilter = filtersList.GetFilter(testFile);
|
|
|
|
|
|
|
|
|
|
Assert.IsNotNull(inputFilter, $"Filter: {testFile}");
|
|
|
|
|
|
|
|
|
|
IMediaImage image = ImageFormat.Detect(inputFilter);
|
|
|
|
|
|
|
|
|
|
Assert.IsNotNull(image, $"Image format: {testFile}");
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(true, image.Open(inputFilter), $"Cannot open image for {testFile}");
|
|
|
|
|
|
2021-03-01 21:18:02 +00:00
|
|
|
Assert.AreEqual(test.MediaType, image.Info.MediaType, testFile);
|
|
|
|
|
Assert.AreEqual(test.Sectors, image.Info.Sectors, testFile);
|
|
|
|
|
Assert.AreEqual(test.SectorSize, image.Info.SectorSize, testFile);
|
2021-03-01 19:34:30 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Info()
|
|
|
|
|
{
|
2021-03-01 21:33:58 +00:00
|
|
|
Environment.CurrentDirectory = DataFolder;
|
2021-03-01 19:34:30 +00:00
|
|
|
|
|
|
|
|
Assert.Multiple(() =>
|
|
|
|
|
{
|
2021-03-01 21:18:02 +00:00
|
|
|
foreach(FileSystemTest test in Tests)
|
2021-03-01 19:34:30 +00:00
|
|
|
{
|
2021-03-01 21:18:02 +00:00
|
|
|
string testFile = test.TestFile;
|
|
|
|
|
bool found = false;
|
|
|
|
|
var partition = new Partition();
|
2021-03-01 19:34:30 +00:00
|
|
|
|
2021-03-05 16:36:51 +00:00
|
|
|
bool exists = File.Exists(testFile);
|
|
|
|
|
Assert.True(exists, $"{testFile} not found");
|
|
|
|
|
|
|
|
|
|
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
|
|
|
|
|
// It arrives here...
|
|
|
|
|
if(!exists)
|
|
|
|
|
continue;
|
|
|
|
|
|
2021-03-01 19:34:30 +00:00
|
|
|
var filtersList = new FiltersList();
|
2021-03-01 21:18:02 +00:00
|
|
|
IFilter inputFilter = filtersList.GetFilter(testFile);
|
2021-03-01 19:34:30 +00:00
|
|
|
|
2021-03-01 21:18:02 +00:00
|
|
|
Assert.IsNotNull(inputFilter, $"Filter: {testFile}");
|
2021-03-01 19:34:30 +00:00
|
|
|
|
|
|
|
|
IMediaImage image = ImageFormat.Detect(inputFilter);
|
|
|
|
|
|
2021-03-01 21:18:02 +00:00
|
|
|
Assert.IsNotNull(image, $"Image format: {testFile}");
|
2021-03-01 19:34:30 +00:00
|
|
|
|
2021-03-01 21:18:02 +00:00
|
|
|
Assert.AreEqual(true, image.Open(inputFilter), $"Cannot open image for {testFile}");
|
2021-03-01 19:34:30 +00:00
|
|
|
|
|
|
|
|
List<string> idPlugins;
|
|
|
|
|
|
2021-03-01 21:33:58 +00:00
|
|
|
if(Partitions)
|
2021-03-01 19:34:30 +00:00
|
|
|
{
|
|
|
|
|
List<Partition> partitionsList = Core.Partitions.GetAll(image);
|
|
|
|
|
|
2021-03-01 21:18:02 +00:00
|
|
|
Assert.Greater(partitionsList.Count, 0, $"No partitions found for {testFile}");
|
2021-03-01 19:34:30 +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);
|
|
|
|
|
|
|
|
|
|
if(idPlugins.Count == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
2021-03-01 21:33:58 +00:00
|
|
|
if(!idPlugins.Contains(Plugin.Id.ToString()))
|
2021-03-01 19:34:30 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
found = true;
|
|
|
|
|
partition = partitionsList[index];
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
partition = new Partition
|
|
|
|
|
{
|
|
|
|
|
Name = "Whole device",
|
|
|
|
|
Length = image.Info.Sectors,
|
|
|
|
|
Size = image.Info.Sectors * image.Info.SectorSize
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Core.Filesystems.Identify(image, out idPlugins, partition, true);
|
|
|
|
|
|
2021-03-01 21:18:02 +00:00
|
|
|
Assert.Greater(idPlugins.Count, 0, $"No filesystems found for {testFile}");
|
2021-03-01 19:34:30 +00:00
|
|
|
|
2021-03-01 21:33:58 +00:00
|
|
|
found = idPlugins.Contains(Plugin.Id.ToString());
|
2021-03-01 19:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
2021-03-01 21:18:02 +00:00
|
|
|
Assert.True(found, $"Filesystem not identified for {testFile}");
|
2021-03-01 19:34:30 +00:00
|
|
|
|
|
|
|
|
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
|
|
|
|
|
// It is not the case, it changes
|
|
|
|
|
if(!found)
|
|
|
|
|
continue;
|
|
|
|
|
|
2021-03-01 21:33:58 +00:00
|
|
|
var fs = Activator.CreateInstance(Plugin.GetType()) as IFilesystem;
|
2021-03-01 19:34:30 +00:00
|
|
|
|
2021-03-01 21:18:02 +00:00
|
|
|
Assert.NotNull(fs, $"Could not instantiate filesystem for {testFile}");
|
2021-03-01 19:34:30 +00:00
|
|
|
|
|
|
|
|
fs.GetInformation(image, partition, out _, null);
|
|
|
|
|
|
2021-03-01 21:18:02 +00:00
|
|
|
if(test.ApplicationId != null)
|
|
|
|
|
Assert.AreEqual(test.ApplicationId, fs.XmlFsType.ApplicationIdentifier,
|
|
|
|
|
$"Application ID: {testFile}");
|
2021-03-01 19:34:30 +00:00
|
|
|
|
2021-03-01 21:18:02 +00:00
|
|
|
Assert.AreEqual(test.Bootable, fs.XmlFsType.Bootable, $"Bootable: {testFile}");
|
|
|
|
|
Assert.AreEqual(test.Clusters, fs.XmlFsType.Clusters, $"Clusters: {testFile}");
|
|
|
|
|
Assert.AreEqual(test.ClusterSize, fs.XmlFsType.ClusterSize, $"Cluster size: {testFile}");
|
2021-03-01 19:34:30 +00:00
|
|
|
|
2021-03-01 21:18:02 +00:00
|
|
|
if(test.SystemId != null)
|
|
|
|
|
Assert.AreEqual(test.SystemId, fs.XmlFsType.SystemIdentifier, $"System ID: {testFile}");
|
2021-03-01 19:34:30 +00:00
|
|
|
|
2021-03-01 21:18:02 +00:00
|
|
|
Assert.AreEqual(_fileSystemType ?? test.Type, fs.XmlFsType.Type, $"Filesystem type: {testFile}");
|
2021-03-01 19:34:30 +00:00
|
|
|
|
2021-03-01 21:18:02 +00:00
|
|
|
Assert.AreEqual(test.VolumeName, fs.XmlFsType.VolumeName, $"Volume name: {testFile}");
|
|
|
|
|
Assert.AreEqual(test.VolumeSerial, fs.XmlFsType.VolumeSerial, $"Volume serial: {testFile}");
|
2021-03-01 19:34:30 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|