Files
Aaru/Aaru.Tests/Images/BlockMediaImageTest.cs

326 lines
14 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
using Aaru.Checksums;
using Aaru.CommonTypes;
2021-09-16 04:42:14 +01:00
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
using Aaru.Core;
using Aaru.Tests.Filesystems;
using FluentAssertions.Execution;
using NUnit.Framework;
namespace Aaru.Tests.Images;
2022-03-06 13:29:38 +00:00
public abstract class BlockMediaImageTest : BaseMediaImageTest
{
2022-03-06 13:29:38 +00:00
// How many sectors to read at once
const uint SECTORS_TO_READ = 256;
public abstract BlockImageTestExpected[] Tests { get; }
[OneTimeSetUp]
public void InitTest() => PluginBase.Init();
2022-03-06 13:29:38 +00:00
[Test]
public void Info()
{
2022-03-06 13:29:38 +00:00
Environment.CurrentDirectory = DataFolder;
2022-03-06 13:29:38 +00:00
Assert.Multiple(() =>
{
2022-03-06 13:29:38 +00:00
foreach(BlockImageTestExpected 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...
if(!exists)
continue;
IFilter filter = PluginRegister.Singleton.GetFilter(testFile);
2022-03-06 13:29:38 +00:00
filter.Open(testFile);
2022-03-15 01:37:37 +00:00
var image = Activator.CreateInstance(Plugin.GetType()) as IMediaImage;
Assert.NotNull(image, string.Format(Localization.Could_not_instantiate_filesystem_for_0, testFile));
2022-03-06 13:29:38 +00:00
ErrorNumber opened = image.Open(filter);
Assert.AreEqual(ErrorNumber.NoError, opened, string.Format(Localization.Open_0, testFile));
2022-03-06 13:29:38 +00:00
if(opened != ErrorNumber.NoError)
continue;
2022-03-06 13:29:38 +00:00
using(new AssertionScope())
2023-10-03 23:44:33 +01:00
{
2022-03-06 13:29:38 +00:00
Assert.Multiple(() =>
{
Assert.AreEqual(test.Sectors, image.Info.Sectors,
string.Format(Localization.Sectors_0, testFile));
Assert.AreEqual(test.SectorSize, image.Info.SectorSize,
string.Format(Localization.Sector_size_0, testFile));
Assert.AreEqual(test.MediaType, image.Info.MediaType,
string.Format(Localization.Media_type_0, testFile));
2022-03-06 13:29:38 +00:00
});
2023-10-03 23:44:33 +01:00
}
2022-03-06 13:29:38 +00:00
}
});
}
2022-03-06 13:29:38 +00:00
[Test]
public void Hashes()
{
Environment.CurrentDirectory = DataFolder;
ErrorNumber errno;
2022-03-06 13:29:38 +00:00
Assert.Multiple(() =>
{
foreach(BlockImageTestExpected 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...
if(!exists)
continue;
IFilter filter = PluginRegister.Singleton.GetFilter(testFile);
2022-03-06 13:29:38 +00:00
filter.Open(testFile);
2022-03-15 01:37:37 +00:00
var image = Activator.CreateInstance(Plugin.GetType()) as IMediaImage;
Assert.NotNull(image, string.Format(Localization.Could_not_instantiate_filesystem_for_0, testFile));
2022-03-06 13:29:38 +00:00
ErrorNumber opened = image.Open(filter);
Assert.AreEqual(ErrorNumber.NoError, opened, string.Format(Localization.Open_0, testFile));
2022-03-06 13:29:38 +00:00
if(opened != ErrorNumber.NoError)
continue;
2022-03-06 13:29:38 +00:00
ulong doneSectors = 0;
var ctx = new Md5Context();
2022-03-06 13:29:38 +00:00
while(doneSectors < image.Info.Sectors)
{
byte[] sector;
2022-03-06 13:29:38 +00:00
if(image.Info.Sectors - doneSectors >= SECTORS_TO_READ)
{
errno = image.ReadSectors(doneSectors, SECTORS_TO_READ, out sector);
doneSectors += SECTORS_TO_READ;
}
else
{
2022-03-07 07:36:44 +00:00
errno = image.ReadSectors(doneSectors, (uint)(image.Info.Sectors - doneSectors), out sector);
2022-03-06 13:29:38 +00:00
doneSectors += image.Info.Sectors - doneSectors;
}
2022-03-06 13:29:38 +00:00
Assert.AreEqual(ErrorNumber.NoError, errno);
ctx.Update(sector);
}
Assert.AreEqual(test.Md5, ctx.End(), string.Format(Localization.Hash_0, testFile));
2022-03-06 13:29:38 +00:00
}
});
}
2022-03-06 13:29:38 +00:00
[Test]
public void Contents()
{
Environment.CurrentDirectory = DataFolder;
PluginRegister plugins = PluginRegister.Singleton;
2022-03-06 13:29:38 +00:00
Assert.Multiple(() =>
{
foreach(BlockImageTestExpected test in Tests)
{
2022-03-06 13:29:38 +00:00
if(test.Partitions is null)
continue;
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...
if(!exists)
continue;
IFilter filter = PluginRegister.Singleton.GetFilter(testFile);
2022-03-06 13:29:38 +00:00
filter.Open(testFile);
2022-03-15 01:37:37 +00:00
var image = Activator.CreateInstance(Plugin.GetType()) as IMediaImage;
Assert.NotNull(image, string.Format(Localization.Could_not_instantiate_filesystem_for_0, testFile));
2022-03-06 13:29:38 +00:00
ErrorNumber opened = image.Open(filter);
Assert.AreEqual(ErrorNumber.NoError, opened, string.Format(Localization.Open_0, testFile));
2022-03-06 13:29:38 +00:00
if(opened != ErrorNumber.NoError)
continue;
List<Partition> partitions = Core.Partitions.GetAll(image);
2022-03-06 13:29:38 +00:00
if(partitions.Count == 0)
2023-10-03 23:44:33 +01:00
{
2022-03-06 13:29:38 +00:00
partitions.Add(new Partition
{
2022-03-06 13:29:38 +00:00
Description = "Whole device",
Length = image.Info.Sectors,
Offset = 0,
Size = image.Info.SectorSize * image.Info.Sectors,
Sequence = 1,
Start = 0
});
2023-10-03 23:44:33 +01:00
}
2022-03-06 13:29:38 +00:00
Assert.AreEqual(test.Partitions.Length, partitions.Count,
string.Format(Localization.Expected_0_partitions_in_1_but_found_2,
test.Partitions.Length, testFile, partitions.Count));
2022-03-06 13:29:38 +00:00
using(new AssertionScope())
2023-10-03 23:44:33 +01:00
{
2022-03-06 13:29:38 +00:00
Assert.Multiple(() =>
{
2023-10-03 23:44:33 +01:00
for(var i = 0; i < test.Partitions.Length; i++)
{
2022-03-06 13:29:38 +00:00
BlockPartitionVolumes expectedPartition = test.Partitions[i];
Partition foundPartition = partitions[i];
2022-03-06 13:29:38 +00:00
Assert.AreEqual(expectedPartition.Start, foundPartition.Start,
string.
Format(Localization.Expected_partition_0_to_start_at_sector_1_but_found_it_starts_at_2_in_3,
i, expectedPartition.Start, foundPartition.Start, testFile));
2022-03-06 13:29:38 +00:00
Assert.AreEqual(expectedPartition.Length, foundPartition.Length,
string.
Format(Localization.Expected_partition_0_to_have_1_sectors_but_found_it_has_2_sectors_in_3,
i, expectedPartition.Length, foundPartition.Length, testFile));
2023-10-03 23:44:33 +01:00
var expectedDataFilename = $"{testFile}.contents.partition{i}.json";
2022-03-06 13:29:38 +00:00
if(!File.Exists(expectedDataFilename))
continue;
var serializerOptions = new JsonSerializerOptions
2022-03-06 13:29:38 +00:00
{
Converters =
{
new JsonStringEnumConverter()
},
MaxDepth = 1536, // More than this an we get a StackOverflowException
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
PropertyNameCaseInsensitive = true,
IncludeFields = true
2022-03-06 13:29:38 +00:00
};
var sr = new FileStream(expectedDataFilename, FileMode.Open);
VolumeData[] expectedData = JsonSerializer.Deserialize<VolumeData[]>(sr, serializerOptions);
sr.Close();
2022-03-06 13:29:38 +00:00
Assert.NotNull(expectedData);
Core.Filesystems.Identify(image, out List<string> idPlugins, partitions[i]);
2022-03-06 13:29:38 +00:00
if(expectedData.Length != idPlugins.Count)
continue;
2022-03-07 07:36:44 +00:00
// Uncomment to generate JSON file
/*
2022-03-06 13:29:38 +00:00
expectedData = new VolumeData[idPlugins.Count];
2022-03-06 13:29:38 +00:00
for(int j = 0; j < idPlugins.Count; j++)
{
string pluginName = idPlugins[j];
2022-03-06 13:29:38 +00:00
if(!plugins.ReadOnlyFilesystems.TryGetValue(pluginName,
out IReadOnlyFilesystem fs))
2022-03-06 13:29:38 +00:00
continue;
Assert.IsNotNull(fs, string.Format(Localization.Could_not_instantiate_filesystem_0, pluginName));
ErrorNumber error = fs.Mount(image, partitions[i], null, null, null);
2022-03-06 13:29:38 +00:00
Assert.AreEqual(ErrorNumber.NoError, error,
string.Format(Localization.Could_not_mount_0_in_partition_1, pluginName, i));
2022-03-06 13:29:38 +00:00
if(error != ErrorNumber.NoError)
continue;
2022-03-06 13:29:38 +00:00
expectedData[j] = new VolumeData
{
Files = ReadOnlyFilesystemTest.BuildDirectory(fs, "/", 0)
2022-03-06 13:29:38 +00:00
};
}
var sw = new FileStream(expectedDataFilename, FileMode.Create);
JsonSerializer.Serialize(sw, expectedData, serializerOptions);
2022-03-06 13:29:38 +00:00
sw.Close();
*/
2022-03-06 13:29:38 +00:00
if(idPlugins.Count == 0)
continue;
2022-03-06 13:29:38 +00:00
Assert.AreEqual(expectedData.Length, idPlugins.Count,
$"Expected {expectedData.Length} filesystems identified in partition {i
} but found {idPlugins.Count} in {testFile}");
2023-10-03 23:44:33 +01:00
for(var j = 0; j < idPlugins.Count; j++)
2022-03-06 13:29:38 +00:00
{
string pluginName = idPlugins[j];
if(!plugins.ReadOnlyFilesystems.TryGetValue(pluginName, out IReadOnlyFilesystem fs))
2022-03-06 13:29:38 +00:00
continue;
2022-03-07 07:36:44 +00:00
Assert.IsNotNull(fs, $"Could not instantiate filesystem {pluginName} in {testFile}");
2022-03-06 13:29:38 +00:00
ErrorNumber error = fs.Mount(image, partitions[i], null, null, null);
2022-03-06 13:29:38 +00:00
Assert.AreEqual(ErrorNumber.NoError, error,
$"Could not mount {pluginName} in partition {i} in {testFile}.");
2022-03-06 13:29:38 +00:00
if(error != ErrorNumber.NoError)
continue;
2022-03-06 13:29:38 +00:00
VolumeData volumeData = expectedData[j];
2023-10-03 23:44:33 +01:00
var currentDepth = 0;
ReadOnlyFilesystemTest.TestDirectory(fs, "/", volumeData.Files, testFile, true,
out List<ReadOnlyFilesystemTest.NextLevel>
currentLevel, currentDepth);
while(currentLevel.Count > 0)
{
currentDepth++;
List<ReadOnlyFilesystemTest.NextLevel> nextLevels = new();
foreach(ReadOnlyFilesystemTest.NextLevel subLevel in currentLevel)
{
ReadOnlyFilesystemTest.TestDirectory(fs, subLevel.Path, subLevel.Children,
testFile, true,
out List<ReadOnlyFilesystemTest.NextLevel>
nextLevel, currentDepth);
nextLevels.AddRange(nextLevel);
}
currentLevel = nextLevels;
}
}
2022-03-06 13:29:38 +00:00
}
});
2023-10-03 23:44:33 +01:00
}
2022-03-06 13:29:38 +00:00
}
});
}
}