2021-03-07 17:23:23 +00:00
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using Aaru.Checksums;
|
|
|
|
|
using Aaru.CommonTypes;
|
2021-09-19 21:16:47 +01:00
|
|
|
using Aaru.CommonTypes.Enums;
|
2021-03-07 17:23:23 +00:00
|
|
|
using Aaru.CommonTypes.Interfaces;
|
|
|
|
|
using Aaru.Core;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
namespace Aaru.Tests.Issues;
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <summary>This class will test an issue that happens when reading an image completely, from start to end, crashes.</summary>
|
|
|
|
|
public abstract class ImageReadIssueTest
|
2021-03-07 17:23:23 +00:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
const uint SECTORS_TO_READ = 256;
|
|
|
|
|
public abstract string DataFolder { get; }
|
|
|
|
|
public abstract string TestFile { get; }
|
|
|
|
|
|
2023-10-05 16:00:38 +01:00
|
|
|
[OneTimeSetUp]
|
|
|
|
|
public void InitTest() => PluginBase.Init();
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
[Test]
|
|
|
|
|
public void Test()
|
2021-03-07 17:23:23 +00:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
Environment.CurrentDirectory = DataFolder;
|
2021-03-07 17:23:23 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
bool exists = File.Exists(TestFile);
|
2022-11-29 10:33:40 +00:00
|
|
|
Assert.True(exists, Localization.Test_file_not_found);
|
2021-03-07 17:23:23 +00:00
|
|
|
|
2023-10-05 16:00:38 +01:00
|
|
|
IFilter inputFilter = PluginRegister.Singleton.GetFilter(TestFile);
|
2021-03-07 17:23:23 +00:00
|
|
|
|
2022-11-29 10:33:40 +00:00
|
|
|
Assert.IsNotNull(inputFilter, Localization.Filter_for_test_file_is_not_detected);
|
2021-03-07 17:23:23 +00:00
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
var image = ImageFormat.Detect(inputFilter) as IMediaImage;
|
2021-03-07 17:23:23 +00:00
|
|
|
|
2022-11-29 10:33:40 +00:00
|
|
|
Assert.IsNotNull(image, Localization.Image_format_for_test_file_is_not_detected);
|
2021-03-07 17:23:23 +00:00
|
|
|
|
2022-11-29 10:33:40 +00:00
|
|
|
Assert.AreEqual(ErrorNumber.NoError, image.Open(inputFilter), Localization.Cannot_open_image_for_test_file);
|
2021-03-07 17:23:23 +00:00
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
ulong doneSectors = 0;
|
|
|
|
|
var ctx = new Crc32Context();
|
2021-03-07 17:23:23 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
while(doneSectors < image.Info.Sectors)
|
|
|
|
|
{
|
|
|
|
|
byte[] sector;
|
2021-03-07 17:23:23 +00:00
|
|
|
|
2022-11-13 19:16:14 +00:00
|
|
|
ErrorNumber errno;
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(image.Info.Sectors - doneSectors >= SECTORS_TO_READ)
|
2021-03-07 17:23:23 +00:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
errno = image.ReadSectors(doneSectors, SECTORS_TO_READ, out sector);
|
|
|
|
|
doneSectors += SECTORS_TO_READ;
|
2021-03-07 17:23:23 +00:00
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
errno = image.ReadSectors(doneSectors, (uint)(image.Info.Sectors - doneSectors), out sector);
|
|
|
|
|
doneSectors += image.Info.Sectors - doneSectors;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(ErrorNumber.NoError, errno);
|
|
|
|
|
|
|
|
|
|
ctx.Update(sector);
|
2021-03-07 17:23:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|