2021-03-02 00:35:16 +00:00
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Aaru.Checksums;
|
|
|
|
|
using Aaru.CommonTypes;
|
|
|
|
|
using Aaru.CommonTypes.Enums;
|
|
|
|
|
using Aaru.CommonTypes.Interfaces;
|
|
|
|
|
using Aaru.CommonTypes.Structs;
|
|
|
|
|
using FluentAssertions;
|
|
|
|
|
using FluentAssertions.Execution;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
|
|
|
|
namespace Aaru.Tests.Images
|
|
|
|
|
{
|
2021-03-02 02:59:20 +00:00
|
|
|
public abstract class OpticalMediaImageTest : BaseMediaImageTest
|
2021-03-02 00:35:16 +00:00
|
|
|
{
|
2021-03-02 02:59:20 +00:00
|
|
|
const uint SECTORS_TO_READ = 256;
|
|
|
|
|
public abstract OpticalImageTestExpected[] Tests { get; }
|
2021-03-02 00:35:16 +00:00
|
|
|
|
|
|
|
|
[Test]
|
2021-03-02 02:59:20 +00:00
|
|
|
public void Info()
|
2021-03-02 00:35:16 +00:00
|
|
|
{
|
|
|
|
|
Environment.CurrentDirectory = _dataFolder;
|
|
|
|
|
|
|
|
|
|
Assert.Multiple(() =>
|
|
|
|
|
{
|
2021-03-02 02:59:20 +00:00
|
|
|
foreach(OpticalImageTestExpected test in Tests)
|
2021-03-02 00:35:16 +00:00
|
|
|
{
|
2021-03-02 02:59:20 +00:00
|
|
|
string testFile = test.TestFile;
|
2021-03-02 00:35:16 +00:00
|
|
|
var filtersList = new FiltersList();
|
2021-03-02 02:59:20 +00:00
|
|
|
IFilter filter = filtersList.GetFilter(testFile);
|
|
|
|
|
filter.Open(testFile);
|
2021-03-02 00:35:16 +00:00
|
|
|
|
|
|
|
|
var image = Activator.CreateInstance(_plugin.GetType()) as IOpticalMediaImage;
|
2021-03-02 02:59:20 +00:00
|
|
|
Assert.NotNull(image, $"Could not instantiate filesystem for {testFile}");
|
2021-03-02 00:35:16 +00:00
|
|
|
|
|
|
|
|
bool opened = image.Open(filter);
|
2021-03-02 02:59:20 +00:00
|
|
|
Assert.AreEqual(true, opened, $"Open: {testFile}");
|
2021-03-02 00:35:16 +00:00
|
|
|
|
|
|
|
|
if(!opened)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
using(new AssertionScope())
|
|
|
|
|
{
|
|
|
|
|
Assert.Multiple(() =>
|
|
|
|
|
{
|
2021-03-02 02:59:20 +00:00
|
|
|
Assert.AreEqual(test.Sectors, image.Info.Sectors, $"Sectors: {testFile}");
|
2021-03-02 00:35:16 +00:00
|
|
|
|
2021-03-02 02:59:20 +00:00
|
|
|
if((test.SectorSize > 0) != null)
|
|
|
|
|
Assert.AreEqual(test.SectorSize, image.Info.SectorSize, $"Sector size: {testFile}");
|
2021-03-02 00:35:16 +00:00
|
|
|
|
2021-03-02 02:59:20 +00:00
|
|
|
Assert.AreEqual(test.MediaType, image.Info.MediaType, $"Media type: {testFile}");
|
2021-03-02 00:35:16 +00:00
|
|
|
|
|
|
|
|
if(image.Info.XmlMediaType != XmlMediaType.OpticalDisc)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-03-02 02:59:20 +00:00
|
|
|
Assert.AreEqual(test.Tracks, image.Tracks.Count, $"Tracks: {testFile}");
|
2021-03-02 00:35:16 +00:00
|
|
|
|
|
|
|
|
image.Tracks.Select(t => t.TrackSession).Should().
|
2021-03-02 02:59:20 +00:00
|
|
|
BeEquivalentTo(test.Tracks.Select(s => s.Session), $"Track session: {testFile}");
|
2021-03-02 00:35:16 +00:00
|
|
|
|
|
|
|
|
image.Tracks.Select(t => t.TrackStartSector).Should().
|
2021-03-02 02:59:20 +00:00
|
|
|
BeEquivalentTo(test.Tracks.Select(s => s.Start), $"Track start: {testFile}");
|
2021-03-02 00:35:16 +00:00
|
|
|
|
|
|
|
|
image.Tracks.Select(t => t.TrackEndSector).Should().
|
2021-03-02 02:59:20 +00:00
|
|
|
BeEquivalentTo(test.Tracks.Select(s => s.End), $"Track end: {testFile}");
|
2021-03-02 00:35:16 +00:00
|
|
|
|
|
|
|
|
image.Tracks.Select(t => t.TrackPregap).Should().
|
2021-03-02 02:59:20 +00:00
|
|
|
BeEquivalentTo(test.Tracks.Select(s => s.Pregap), $"Track pregap: {testFile}");
|
2021-03-02 00:35:16 +00:00
|
|
|
|
|
|
|
|
int trackNo = 0;
|
|
|
|
|
|
|
|
|
|
byte[] flags = new byte[image.Tracks.Count];
|
|
|
|
|
|
|
|
|
|
foreach(Track currentTrack in image.Tracks)
|
|
|
|
|
{
|
|
|
|
|
if(image.Info.ReadableSectorTags.Contains(SectorTagType.CdTrackFlags))
|
|
|
|
|
flags[trackNo] = image.ReadSectorTag(currentTrack.TrackSequence,
|
|
|
|
|
SectorTagType.CdTrackFlags)[0];
|
|
|
|
|
|
|
|
|
|
trackNo++;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-02 02:59:20 +00:00
|
|
|
flags.Should().BeEquivalentTo(test.Tracks.Select(s => s.Flags), $"Track flags: {testFile}");
|
2021-03-02 00:35:16 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2021-03-02 02:59:20 +00:00
|
|
|
public void Hashes()
|
2021-03-02 00:35:16 +00:00
|
|
|
{
|
|
|
|
|
Environment.CurrentDirectory = Environment.CurrentDirectory = _dataFolder;
|
|
|
|
|
|
|
|
|
|
Assert.Multiple(() =>
|
|
|
|
|
{
|
2021-03-02 02:59:20 +00:00
|
|
|
Parallel.For(0L, Tests.Length, (i, state) =>
|
2021-03-02 00:35:16 +00:00
|
|
|
{
|
2021-03-02 02:59:20 +00:00
|
|
|
string testFile = Tests[i].TestFile;
|
2021-03-02 00:35:16 +00:00
|
|
|
var filtersList = new FiltersList();
|
2021-03-02 02:59:20 +00:00
|
|
|
IFilter filter = filtersList.GetFilter(testFile);
|
|
|
|
|
filter.Open(testFile);
|
2021-03-02 00:35:16 +00:00
|
|
|
|
|
|
|
|
var image = Activator.CreateInstance(_plugin.GetType()) as IOpticalMediaImage;
|
2021-03-02 02:59:20 +00:00
|
|
|
Assert.NotNull(image, $"Could not instantiate filesystem for {testFile}");
|
2021-03-02 00:35:16 +00:00
|
|
|
|
|
|
|
|
bool opened = image.Open(filter);
|
2021-03-02 02:59:20 +00:00
|
|
|
Assert.AreEqual(true, opened, $"Open: {testFile}");
|
2021-03-02 00:35:16 +00:00
|
|
|
|
|
|
|
|
if(!opened)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Md5Context ctx;
|
|
|
|
|
|
|
|
|
|
if(image.Info.XmlMediaType == XmlMediaType.OpticalDisc)
|
|
|
|
|
{
|
|
|
|
|
foreach(bool @long in new[]
|
|
|
|
|
{
|
|
|
|
|
false, true
|
|
|
|
|
})
|
|
|
|
|
{
|
|
|
|
|
ctx = new Md5Context();
|
|
|
|
|
|
|
|
|
|
foreach(Track currentTrack in image.Tracks)
|
|
|
|
|
{
|
|
|
|
|
ulong sectors = currentTrack.TrackEndSector - currentTrack.TrackStartSector + 1;
|
|
|
|
|
ulong doneSectors = 0;
|
|
|
|
|
|
|
|
|
|
while(doneSectors < sectors)
|
|
|
|
|
{
|
|
|
|
|
byte[] sector;
|
|
|
|
|
|
|
|
|
|
if(sectors - doneSectors >= SECTORS_TO_READ)
|
|
|
|
|
{
|
|
|
|
|
sector =
|
|
|
|
|
@long ? image.ReadSectorsLong(doneSectors, SECTORS_TO_READ,
|
|
|
|
|
currentTrack.TrackSequence)
|
|
|
|
|
: image.ReadSectors(doneSectors, SECTORS_TO_READ,
|
|
|
|
|
currentTrack.TrackSequence);
|
|
|
|
|
|
|
|
|
|
doneSectors += SECTORS_TO_READ;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
sector =
|
|
|
|
|
@long ? image.ReadSectorsLong(doneSectors, (uint)(sectors - doneSectors),
|
|
|
|
|
currentTrack.TrackSequence)
|
|
|
|
|
: image.ReadSectors(doneSectors, (uint)(sectors - doneSectors),
|
|
|
|
|
currentTrack.TrackSequence);
|
|
|
|
|
|
|
|
|
|
doneSectors += sectors - doneSectors;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.Update(sector);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-02 02:59:20 +00:00
|
|
|
Assert.AreEqual(@long ? Tests[i].LongMD5 : Tests[i].MD5, ctx.End(),
|
|
|
|
|
$"{(@long ? "Long hash" : "Hash")}: {testFile}");
|
2021-03-02 00:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!image.Info.ReadableSectorTags.Contains(SectorTagType.CdSectorSubchannel))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
ctx = new Md5Context();
|
|
|
|
|
|
|
|
|
|
foreach(Track currentTrack in image.Tracks)
|
|
|
|
|
{
|
|
|
|
|
ulong sectors = currentTrack.TrackEndSector - currentTrack.TrackStartSector + 1;
|
|
|
|
|
ulong doneSectors = 0;
|
|
|
|
|
|
|
|
|
|
while(doneSectors < sectors)
|
|
|
|
|
{
|
|
|
|
|
byte[] sector;
|
|
|
|
|
|
|
|
|
|
if(sectors - doneSectors >= SECTORS_TO_READ)
|
|
|
|
|
{
|
|
|
|
|
sector = image.ReadSectorsTag(doneSectors, SECTORS_TO_READ,
|
|
|
|
|
currentTrack.TrackSequence,
|
|
|
|
|
SectorTagType.CdSectorSubchannel);
|
|
|
|
|
|
|
|
|
|
doneSectors += SECTORS_TO_READ;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
sector = image.ReadSectorsTag(doneSectors, (uint)(sectors - doneSectors),
|
|
|
|
|
currentTrack.TrackSequence,
|
|
|
|
|
SectorTagType.CdSectorSubchannel);
|
|
|
|
|
|
|
|
|
|
doneSectors += sectors - doneSectors;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.Update(sector);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-02 02:59:20 +00:00
|
|
|
Assert.AreEqual(Tests[i].SubchannelMD5, ctx.End(), $"Subchannel hash: {testFile}");
|
2021-03-02 00:35:16 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ctx = new Md5Context();
|
|
|
|
|
ulong doneSectors = 0;
|
|
|
|
|
|
|
|
|
|
while(doneSectors < image.Info.Sectors)
|
|
|
|
|
{
|
|
|
|
|
byte[] sector;
|
|
|
|
|
|
|
|
|
|
if(image.Info.Sectors - doneSectors >= SECTORS_TO_READ)
|
|
|
|
|
{
|
|
|
|
|
sector = image.ReadSectors(doneSectors, SECTORS_TO_READ);
|
|
|
|
|
doneSectors += SECTORS_TO_READ;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
sector = image.ReadSectors(doneSectors, (uint)(image.Info.Sectors - doneSectors));
|
|
|
|
|
doneSectors += image.Info.Sectors - doneSectors;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.Update(sector);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-02 02:59:20 +00:00
|
|
|
Assert.AreEqual(Tests[i].MD5, ctx.End(), $"Hash: {testFile}");
|
2021-03-02 00:35:16 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|