Files
Aaru/Aaru.Tests/Images/TapeMediaImageTest.cs

179 lines
6.2 KiB
C#
Raw Normal View History

using System;
using System.IO;
using Aaru.Checksums;
using Aaru.CommonTypes;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
using FluentAssertions;
using FluentAssertions.Execution;
using NUnit.Framework;
namespace Aaru.Tests.Images;
2022-03-06 13:29:38 +00:00
public abstract class TapeMediaImageTest : BaseMediaImageTest
{
2022-03-06 13:29:38 +00:00
// How many sectors to read at once
const uint SECTORS_TO_READ = 256;
2022-03-06 13:29:38 +00:00
public abstract TapeImageTestExpected[] Tests { get; }
2022-03-06 13:29:38 +00:00
[Test]
public void Tape()
{
Environment.CurrentDirectory = DataFolder;
2022-03-06 13:29:38 +00:00
Assert.Multiple(() =>
{
foreach(TapeImageTestExpected 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;
2022-03-06 13:29:38 +00:00
var filtersList = new FiltersList();
IFilter filter = filtersList.GetFilter(testFile);
filter.Open(testFile);
2022-03-15 01:37:37 +00:00
var image = Activator.CreateInstance(Plugin.GetType()) as ITapeImage;
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;
Assert.AreEqual(true, image.IsTape, string.Format(Localization.Is_tape_0, testFile));
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(() =>
{
image.Files.Should().
BeEquivalentTo(test.Files, string.Format(Localization.Tape_files_0, testFile));
image.TapePartitions.Should().
BeEquivalentTo(test.Partitions, string.Format(Localization.Tape_partitions_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 Info()
{
Environment.CurrentDirectory = DataFolder;
2022-03-06 13:29:38 +00:00
Assert.Multiple(() =>
{
foreach(TapeImageTestExpected 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;
2022-03-06 13:29:38 +00:00
var filtersList = new FiltersList();
IFilter filter = filtersList.GetFilter(testFile);
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(TapeImageTestExpected 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;
2022-03-06 13:29:38 +00:00
var filtersList = new FiltersList();
IFilter filter = filtersList.GetFilter(testFile);
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;
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);
}
2022-03-06 13:29:38 +00:00
Assert.AreEqual(test.Md5, ctx.End(), string.Format(Localization.Hash_0, testFile));
2022-03-06 13:29:38 +00:00
}
});
}
}