From 011ca670e026c692edd48084ffc2fe7f5987d90a Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Tue, 3 Dec 2024 16:29:50 -0500 Subject: [PATCH] Add tests around some Aaru helpers --- CHANGELIST.md | 1 + MPF.Processors.Test/AaruTests.cs | 179 +++++++++++++++++++++++++++++++ MPF.Processors/Aaru.cs | 145 ++++++++++++++----------- 3 files changed, 261 insertions(+), 64 deletions(-) diff --git a/CHANGELIST.md b/CHANGELIST.md index 80b4839a..808a25ff 100644 --- a/CHANGELIST.md +++ b/CHANGELIST.md @@ -7,6 +7,7 @@ - Add processor base implementation tests - Ensure SS.bin only required for DVD - Update tests to account for new file count +- Add tests around some Aaru helpers ### 3.2.4 (2024-11-24) diff --git a/MPF.Processors.Test/AaruTests.cs b/MPF.Processors.Test/AaruTests.cs index ff894069..e3c91d13 100644 --- a/MPF.Processors.Test/AaruTests.cs +++ b/MPF.Processors.Test/AaruTests.cs @@ -1,10 +1,14 @@ using System; using System.IO; using SabreTools.RedumpLib.Data; +using Schemas; using Xunit; +#pragma warning disable CS0618 // Ignore "Type or member is obsolete" + namespace MPF.Processors.Test { + // TODO: Add tests around remaining helper methods public class AaruTests { #region GetOutputFiles @@ -164,5 +168,180 @@ namespace MPF.Processors.Test } #endregion + + #region GenerateCuesheet + + [Fact] + public void GenerateCuesheet_Null_Null() + { + CICMMetadataType? cicmSidecar = null; + string basePath = "test"; + string? actual = Aaru.GenerateCuesheet(cicmSidecar, basePath); + Assert.Null(actual); + } + + [Fact] + public void GenerateCuesheet_Empty_Null() + { + CICMMetadataType? cicmSidecar = new CICMMetadataType(); + string basePath = "test"; + string? actual = Aaru.GenerateCuesheet(cicmSidecar, basePath); + Assert.Null(actual); + } + + [Fact] + public void GenerateCuesheet_Valid_Filled() + { + TrackType trackType = new TrackType + { + BytesPerSector = 2352, + Flags = new TrackFlagsType { Quadraphonic = true }, + Indexes = [new TrackIndexType { index = 1, Value = 0 }], + ISRC = "isrc", + Sequence = new TrackSequenceType { TrackNumber = 1 }, + TrackType1 = TrackTypeTrackType.mode1, + }; + + OpticalDiscType opticalDiscType = new OpticalDiscType + { + DiscType = "CD-ROM", + MediaCatalogueNumber = "mcn", + Track = [trackType], + Tracks = [1], + }; + + CICMMetadataType? cicmSidecar = new CICMMetadataType + { + OpticalDisc = [opticalDiscType], + Performer = ["performer"], + }; + + string basePath = "test"; + string? actual = Aaru.GenerateCuesheet(cicmSidecar, basePath); + + // TODO: Unexpected outcome -- Non-null but empty cuesheet generated + // TODO: Add structure validation + Assert.NotNull(actual); + } + + #endregion + + #region GenerateDatafile + + [Fact] + public void GenerateDatafile_Null_Null() + { + CICMMetadataType? cicmSidecar = null; + string basePath = "test"; + var actual = Aaru.GenerateDatafile(cicmSidecar, basePath); + Assert.Null(actual); + } + + [Fact] + public void GenerateDatafile_Empty_Null() + { + CICMMetadataType? cicmSidecar = new CICMMetadataType(); + string basePath = "test"; + var actual = Aaru.GenerateDatafile(cicmSidecar, basePath); + Assert.Null(actual); + } + + [Fact] + public void GenerateDatafile_Valid_Filled() + { + TrackType trackType = new TrackType + { + Checksums = + [ + new ChecksumType { type = ChecksumTypeType.crc32, Value = "00000000" }, + new ChecksumType { type = ChecksumTypeType.md5, Value = "d41d8cd98f00b204e9800998ecf8427e" }, + new ChecksumType { type = ChecksumTypeType.sha1, Value = "da39a3ee5e6b4b0d3255bfef95601890afd80709" }, + ], + Sequence = new TrackSequenceType { TrackNumber = 1 }, + Size = 12345, + }; + + OpticalDiscType opticalDiscType = new OpticalDiscType + { + DiscType = "CD-ROM", + MediaCatalogueNumber = "mcn", + Track = [trackType], + Tracks = [1], + }; + + CICMMetadataType? cicmSidecar = new CICMMetadataType + { + OpticalDisc = [opticalDiscType], + }; + + string basePath = "test"; + var actual = Aaru.GenerateDatafile(cicmSidecar, basePath); + + // TODO: Add structure validation + Assert.NotNull(actual); + } + + #endregion + + #region GeneratePVD + + [Fact] + public void GeneratePVD_Null_Null() + { + CICMMetadataType? cicmSidecar = null; + var actual = Aaru.GeneratePVD(cicmSidecar); + Assert.Null(actual); + } + + [Fact] + public void GeneratePVD_Empty_Null() + { + CICMMetadataType? cicmSidecar = new CICMMetadataType(); + var actual = Aaru.GeneratePVD(cicmSidecar); + Assert.Null(actual); + } + + [Fact] + public void GeneratePVD_Valid_Filled() + { + FileSystemType fileSystemType = new FileSystemType + { + CreationDate = DateTime.UtcNow, + CreationDateSpecified = true, + ModificationDate = DateTime.UtcNow, + ModificationDateSpecified = true, + ExpirationDate = DateTime.UtcNow, + ExpirationDateSpecified = true, + EffectiveDate = DateTime.UtcNow, + EffectiveDateSpecified = true, + }; + + PartitionType partitionType = new PartitionType + { + FileSystems = [fileSystemType], + }; + + TrackType trackType = new TrackType + { + FileSystemInformation = [partitionType], + }; + + OpticalDiscType opticalDiscType = new OpticalDiscType + { + Track = [trackType], + }; + + CICMMetadataType? cicmSidecar = new CICMMetadataType + { + OpticalDisc = [opticalDiscType], + }; + + string? actual = Aaru.GeneratePVD(cicmSidecar); + + // TODO: Add structure validation + Assert.NotNull(actual); + } + + #endregion } } \ No newline at end of file diff --git a/MPF.Processors/Aaru.cs b/MPF.Processors/Aaru.cs index b699747c..ab863491 100644 --- a/MPF.Processors/Aaru.cs +++ b/MPF.Processors/Aaru.cs @@ -257,63 +257,7 @@ namespace MPF.Processors #endregion - #region Information Extraction Methods - - /// - /// Convert the TrackTypeTrackType value to a CueTrackDataType - /// - /// TrackTypeTrackType to convert - /// Sector size to help with specific subtypes - /// CueTrackDataType representing the input data - private static CueTrackDataType ConvertToDataType(TrackTypeTrackType trackType, uint bytesPerSector) - { - switch (trackType) - { - case TrackTypeTrackType.audio: - return CueTrackDataType.AUDIO; - - case TrackTypeTrackType.mode1: - if (bytesPerSector == 2048) - return CueTrackDataType.MODE1_2048; - else - return CueTrackDataType.MODE1_2352; - - case TrackTypeTrackType.mode2: - case TrackTypeTrackType.m2f1: - case TrackTypeTrackType.m2f2: - if (bytesPerSector == 2336) - return CueTrackDataType.MODE2_2336; - else - return CueTrackDataType.MODE2_2352; - - default: - return CueTrackDataType.MODE1_2352; - } - } - - /// - /// Convert the TrackFlagsType value to a CueTrackFlag - /// - /// TrackFlagsType containing flag data - /// CueTrackFlag representing the flags - private static CueTrackFlag ConvertToTrackFlag(TrackFlagsType trackFlagsType) - { - if (trackFlagsType == null) - return 0; - - CueTrackFlag flag = 0; - - if (trackFlagsType.CopyPermitted) - flag |= CueTrackFlag.DCP; - - if (trackFlagsType.Quadraphonic) - flag |= CueTrackFlag.FourCH; - - if (trackFlagsType.PreEmphasis) - flag |= CueTrackFlag.PRE; - - return flag; - } + #region Cuesheet Generation /// /// Generate a cuesheet string based on CICM sidecar data @@ -321,7 +265,7 @@ namespace MPF.Processors /// CICM Sidecar data generated by Aaru /// Base path for determining file names /// String containing the cuesheet, null on error - private static string? GenerateCuesheet(CICMMetadataType? cicmSidecar, string basePath) + internal static string? GenerateCuesheet(CICMMetadataType? cicmSidecar, string basePath) { // If the object is null, we can't get information from it if (cicmSidecar == null) @@ -455,20 +399,79 @@ namespace MPF.Processors return null; } + /// + /// Convert the TrackTypeTrackType value to a CueTrackDataType + /// + /// TrackTypeTrackType to convert + /// Sector size to help with specific subtypes + /// CueTrackDataType representing the input data + private static CueTrackDataType ConvertToDataType(TrackTypeTrackType trackType, uint bytesPerSector) + { + switch (trackType) + { + case TrackTypeTrackType.audio: + return CueTrackDataType.AUDIO; + + case TrackTypeTrackType.mode1: + if (bytesPerSector == 2048) + return CueTrackDataType.MODE1_2048; + else + return CueTrackDataType.MODE1_2352; + + case TrackTypeTrackType.mode2: + case TrackTypeTrackType.m2f1: + case TrackTypeTrackType.m2f2: + if (bytesPerSector == 2336) + return CueTrackDataType.MODE2_2336; + else + return CueTrackDataType.MODE2_2352; + + default: + return CueTrackDataType.MODE1_2352; + } + } + + /// + /// Convert the TrackFlagsType value to a CueTrackFlag + /// + /// TrackFlagsType containing flag data + /// CueTrackFlag representing the flags + private static CueTrackFlag ConvertToTrackFlag(TrackFlagsType trackFlagsType) + { + if (trackFlagsType == null) + return 0; + + CueTrackFlag flag = 0; + + if (trackFlagsType.CopyPermitted) + flag |= CueTrackFlag.DCP; + + if (trackFlagsType.Quadraphonic) + flag |= CueTrackFlag.FourCH; + + if (trackFlagsType.PreEmphasis) + flag |= CueTrackFlag.PRE; + + return flag; + } + + #endregion + + #region Datafile Generation + /// /// Generate a CMP XML datfile string based on CICM sidecar data /// /// CICM Sidecar data generated by Aaru /// Base path for determining file names /// Datafile containing the hash information, null on error - private static Datafile? GenerateDatafile(CICMMetadataType? cicmSidecar, string basePath) + internal static Datafile? GenerateDatafile(CICMMetadataType? cicmSidecar, string basePath) { // If the object is null, we can't get information from it if (cicmSidecar == null) return null; // Required variables - var datafile = new Datafile(); var roms = new List(); // Process OpticalDisc, if possible @@ -562,10 +565,16 @@ namespace MPF.Processors } // Assign the roms to a new game - datafile.Game = new Game[1]; - datafile.Game[0] = new Game { Rom = [.. roms] }; + if (roms.Count > 0) + { + var datafile = new Datafile + { + Game = [new Game { Rom = [.. roms] }] + }; + return datafile; + } - return datafile; + return null; } /// @@ -593,12 +602,16 @@ namespace MPF.Processors return trackName; } + #endregion + + #region PVD Generation + /// /// Generate a Redump-compatible PVD block based on CICM sidecar file /// /// CICM Sidecar data generated by Aaru /// String containing the PVD, null on error - private static string? GeneratePVD(CICMMetadataType? cicmSidecar) + internal static string? GeneratePVD(CICMMetadataType? cicmSidecar) { // If the object is null, we can't get information from it if (cicmSidecar == null) @@ -796,6 +809,10 @@ namespace MPF.Processors return pvdLine.ToString(); } + #endregion + + #region Information Extraction Methods + /// /// Read the CICM Sidecar as an object ///