Files
Aaru/Aaru.Tests/Images/QEMU/VirtualPC.cs

174 lines
5.6 KiB
C#
Raw Normal View History

// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : VirtualPC.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Aaru unit testing.
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2021 Natalia Portillo
// ****************************************************************************/
using System;
using System.IO;
using Aaru.Checksums;
using Aaru.CommonTypes;
using Aaru.DiscImages;
using Aaru.Filters;
using FluentAssertions.Execution;
using NUnit.Framework;
namespace Aaru.Tests.Images.QEMU
{
[TestFixture]
public class VirtualPC
{
readonly string[] _testFiles =
{
"qemu_dynamic_250mb.vhd.lz",
"qemu_fixed_10mb.vhd.lz",
"virtualpc.vhd.lz"
};
readonly ulong[] _sectors =
{
// qemu_dynamic_250mb.vhd.lz"
512064,
// qemu_fixed_10mb.vhd.lz"
20536,
// virtualpc.vhd.lz
251940
};
readonly uint[] _sectorSize =
{
// qemu_dynamic_250mb.vhd.lz"
512,
// qemu_fixed_10mb.vhd.lz"
512,
// virtualpc.vhd.lz
512
};
readonly MediaType[] _mediaTypes =
{
// qemu_dynamic_250mb.vhd.lz"
MediaType.Unknown,
// qemu_fixed_10mb.vhd.lz"
MediaType.Unknown,
// virtualpc.vhd.lz
MediaType.Unknown
};
readonly string[] _md5S =
{
// qemu_dynamic_250mb.vhd.lz"
"0435d6781d14d34a32c6ac40f5e70d35",
// qemu_fixed_10mb.vhd.lz"
"adfad4fb019f157e868baa39e7753db7",
// virtualpc.vhd.lz
"6246bff640cb3a56d2611e7f8616384d"
};
readonly string _dataFolder = Path.Combine(Consts.TEST_FILES_ROOT, "Media image formats", "QEMU", "VirtualPC");
[Test]
public void Info()
{
Environment.CurrentDirectory = _dataFolder;
Assert.Multiple(() =>
{
for(int i = 0; i < _testFiles.Length; i++)
{
var filter = new LZip();
filter.Open(_testFiles[i]);
var image = new Vhd();
bool opened = image.Open(filter);
Assert.AreEqual(true, opened, $"Open: {_testFiles[i]}");
if(!opened)
continue;
using(new AssertionScope())
{
Assert.Multiple(() =>
{
Assert.AreEqual(_sectors[i], image.Info.Sectors, $"Sectors: {_testFiles[i]}");
Assert.AreEqual(_sectorSize[i], image.Info.SectorSize, $"Sector size: {_testFiles[i]}");
Assert.AreEqual(_mediaTypes[i], image.Info.MediaType, $"Media type: {_testFiles[i]}");
});
}
}
});
}
// How many sectors to read at once
const uint _sectorsToRead = 256;
[Test]
public void Hashes()
{
Environment.CurrentDirectory = _dataFolder;
Assert.Multiple(() =>
{
for(int i = 0; i < _testFiles.Length; i++)
{
var filter = new LZip();
filter.Open(_testFiles[i]);
var image = new Vhd();
bool opened = image.Open(filter);
ulong doneSectors = 0;
Assert.AreEqual(true, opened, $"Open: {_testFiles[i]}");
if(!opened)
continue;
var ctx = new Md5Context();
while(doneSectors < image.Info.Sectors)
{
byte[] sector;
if(image.Info.Sectors - doneSectors >= _sectorsToRead)
{
sector = image.ReadSectors(doneSectors, _sectorsToRead);
doneSectors += _sectorsToRead;
}
else
{
sector = image.ReadSectors(doneSectors, (uint)(image.Info.Sectors - doneSectors));
doneSectors += image.Info.Sectors - doneSectors;
}
ctx.Update(sector);
}
Assert.AreEqual(_md5S[i], ctx.End(), $"Hash: {_testFiles[i]}");
}
});
}
}
}