Add tests around CleanRip helpers

This commit is contained in:
Matt Nadareski
2024-12-03 16:55:14 -05:00
parent 011ca670e0
commit cc7116a623
5 changed files with 168 additions and 21 deletions

View File

@@ -8,6 +8,7 @@
- Ensure SS.bin only required for DVD
- Update tests to account for new file count
- Add tests around some Aaru helpers
- Add tests around CleanRip helpers
### 3.2.4 (2024-11-24)

View File

@@ -153,5 +153,137 @@ namespace MPF.Processors.Test
}
#endregion
#region GenerateCleanripDatafile
[Fact]
public void GenerateCleanripDatafile_NoIsoNoDumpinfo_Null()
{
string iso = "INVALID";
string dumpinfo = "INVALID";
var actual = CleanRip.GenerateCleanripDatafile(iso, dumpinfo);
Assert.Null(actual);
}
[Fact]
public void GenerateCleanripDatafile_IsoOnly_Filled()
{
string iso = Path.Combine(Environment.CurrentDirectory, "TestData", "CleanRip", "DVD", "test.iso");
string dumpinfo = "INVALID";
var actual = CleanRip.GenerateCleanripDatafile(iso, dumpinfo);
Assert.NotNull(actual);
Assert.NotNull(actual.Game);
var game = Assert.Single(actual.Game);
Assert.NotNull(game.Rom);
var rom = Assert.Single(game.Rom);
Assert.Equal("9", rom.Size);
Assert.Equal("560b9f59", rom.CRC);
Assert.Equal("edbb6676247e65c2245dd4883ed9fc24", rom.MD5);
Assert.Equal("1b33ad54d78085be5ecb1cf1b3e9da821e708075", rom.SHA1);
}
[Fact]
public void GenerateCleanripDatafile_DumpinfoOnly_Filled()
{
string iso = "INVALID";
string dumpinfo = Path.Combine(Environment.CurrentDirectory, "TestData", "CleanRip", "DVD", "test-dumpinfo.txt");
var actual = CleanRip.GenerateCleanripDatafile(iso, dumpinfo);
Assert.NotNull(actual);
Assert.NotNull(actual.Game);
var game = Assert.Single(actual.Game);
Assert.NotNull(game.Rom);
var rom = Assert.Single(game.Rom);
Assert.Equal("-1", rom.Size);
Assert.Equal("00000000", rom.CRC);
Assert.Equal("d41d8cd98f00b204e9800998ecf8427e", rom.MD5);
Assert.Equal("da39a3ee5e6b4b0d3255bfef95601890afd80709", rom.SHA1);
}
[Fact]
public void GenerateCleanripDatafile_BothValid_Filled()
{
string iso = Path.Combine(Environment.CurrentDirectory, "TestData", "CleanRip", "DVD", "test.iso");
string dumpinfo = Path.Combine(Environment.CurrentDirectory, "TestData", "CleanRip", "DVD", "test-dumpinfo.txt");
var actual = CleanRip.GenerateCleanripDatafile(iso, dumpinfo);
Assert.NotNull(actual);
Assert.NotNull(actual.Game);
var game = Assert.Single(actual.Game);
Assert.NotNull(game.Rom);
var rom = Assert.Single(game.Rom);
Assert.Equal("9", rom.Size);
Assert.Equal("00000000", rom.CRC);
Assert.Equal("d41d8cd98f00b204e9800998ecf8427e", rom.MD5);
Assert.Equal("da39a3ee5e6b4b0d3255bfef95601890afd80709", rom.SHA1);
}
#endregion
#region GetBCA
[Fact]
public void GetBCA_InvalidPath_Null()
{
string bcaPath = "INVALID";
string? actual = CleanRip.GetBCA(bcaPath);
Assert.Null(actual);
}
[Fact]
public void GetBCA_ValidPath_Formatted()
{
string expected = "0001 0203 0405 0607 0809 0A0B 0C0D 0E0F\n0001 0203 0405 0607 0809 0A0B 0C0D 0E0F\n0001 0203 0405 0607 0809 0A0B 0C0D 0E0F\n0001 0203 0405 0607 0809 0A0B 0C0D 0E0F\n0001 0203 0405 0607 0809 0A0B 0C0D 0E0F\n0001 0203 0405 0607 0809 0A0B 0C0D 0E0F\n0001 0203 0405 0607 0809 0A0B 0C0D 0E0F\n0001 0203 0405 0607 0809 0A0B 0C0D 0E0F\n0001 0203 0405 0607 0809 0A0B 0C0D 0E0F\n0001 0203 0405 0607 0809 0A0B 0C0D 0E0F\n";
string bcaPath = Path.Combine(Environment.CurrentDirectory, "TestData", "CleanRip", "DVD", "test.bca");
string? actual = CleanRip.GetBCA(bcaPath);
Assert.NotNull(actual);
Assert.Equal(expected, actual);
}
#endregion
#region GetGameCubeWiiInformation
[Fact]
public void GetGameCubeWiiInformation_NoFile_False()
{
string dumpinfo = string.Empty;
bool actual = CleanRip.GetGameCubeWiiInformation(dumpinfo, out Region? region, out string? version, out string? name, out string? serial);
Assert.False(actual);
Assert.Null(region);
Assert.Null(version);
Assert.Null(name);
Assert.Null(serial);
}
[Fact]
public void GetGameCubeWiiInformation_Filled_True()
{
Region? expectedRegion = Region.World;
string? expectedVersion = "version";
string? expectedName = "name";
string? expectedSerial = "000A00";
string dumpinfo = Path.Combine(Environment.CurrentDirectory, "TestData", "CleanRip", "DVD", "test-dumpinfo.txt");
bool actual = CleanRip.GetGameCubeWiiInformation(dumpinfo, out Region? region, out string? version, out string? name, out string? serial);
Assert.True(actual);
Assert.Equal(expectedRegion, region);
Assert.Equal(expectedVersion, version);
Assert.Equal(expectedName, name);
Assert.Equal(expectedSerial, serial);
}
#endregion
}
}

View File

@@ -1 +1,7 @@
TEST DATA
--File Generated by CleanRip
CRC32: 00000000
MD5: d41d8cd98f00b204e9800998ecf8427e
SHA-1: da39a3ee5e6b4b0d3255bfef95601890afd80709
Version: version
Internal Name: name
Filename: 000A00

View File

@@ -100,13 +100,14 @@ namespace MPF.Processors
/// <param name="iso">Path to ISO file</param>
/// <param name="dumpinfo">Path to discinfo file</param>
/// <returns></returns>
private static Datafile? GenerateCleanripDatafile(string iso, string dumpinfo)
internal static Datafile? GenerateCleanripDatafile(string iso, string dumpinfo)
{
// If the files don't exist, we can't get info from it
if (!File.Exists(iso) || !File.Exists(dumpinfo))
return null;
// Get the size from the ISO, if possible
long size = iso.Length > 0 && File.Exists(iso)
? new FileInfo(iso).Length
: -1;
long size = new FileInfo(iso).Length;
// Setup the hashes
string crc = string.Empty;
string md5 = string.Empty;
string sha1 = string.Empty;
@@ -114,22 +115,25 @@ namespace MPF.Processors
try
{
// Make sure this file is a dumpinfo
using var sr = File.OpenText(dumpinfo);
if (sr.ReadLine()?.Contains("--File Generated by CleanRip") != true)
return null;
// Read all lines and gather dat information
while (!sr.EndOfStream)
if (dumpinfo.Length > 0 && File.Exists(dumpinfo))
{
var line = sr.ReadLine()?.Trim();
if (string.IsNullOrEmpty(line))
continue;
else if (line!.StartsWith("CRC32"))
crc = line.Substring(7).ToLowerInvariant();
else if (line.StartsWith("MD5"))
md5 = line.Substring(5);
else if (line.StartsWith("SHA-1"))
sha1 = line.Substring(7);
using var sr = File.OpenText(dumpinfo);
if (sr.ReadLine()?.Contains("--File Generated by CleanRip") != true)
return null;
// Read all lines and gather dat information
while (!sr.EndOfStream)
{
var line = sr.ReadLine()?.Trim();
if (string.IsNullOrEmpty(line))
continue;
else if (line!.StartsWith("CRC32"))
crc = line.Substring(7).ToLowerInvariant();
else if (line.StartsWith("MD5"))
md5 = line.Substring(5);
else if (line.StartsWith("SHA-1"))
sha1 = line.Substring(7);
}
}
// Ensure all checksums were found in log
@@ -143,6 +147,10 @@ namespace MPF.Processors
}
}
// If no information could be found
if (size == -1 && crc == string.Empty && md5 == string.Empty && sha1 == string.Empty)
return null;
return new Datafile
{
Game = [new Game() { Rom = [new Rom { Name = Path.GetFileName(iso), Size = size.ToString(), CRC = crc, MD5 = md5, SHA1 = sha1 }] }]