From b2eff249188db667fa053c2a50d0302e357a2858 Mon Sep 17 00:00:00 2001 From: Miepee Date: Tue, 24 Jan 2023 00:37:16 +0100 Subject: [PATCH] Add tests for invalid paths --- AM2RPortHelperLib/RawMods.cs | 21 ++++++++++++++++++- AM2RPortHelperTests/RawModsTests.cs | 32 +++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/AM2RPortHelperLib/RawMods.cs b/AM2RPortHelperLib/RawMods.cs index db49da0..5d38163 100644 --- a/AM2RPortHelperLib/RawMods.cs +++ b/AM2RPortHelperLib/RawMods.cs @@ -26,6 +26,12 @@ public abstract class RawMods : ModsBase /// The OS for which the zip was made for could not be determined. public static ModOS GetModOSOfRawZip(string inputRawZipPath) { + if (inputRawZipPath is null) + throw new ArgumentNullException(nameof(inputRawZipPath) + " cannot be null!"); + + if (!File.Exists(inputRawZipPath)) + throw new FileNotFoundException(nameof(inputRawZipPath) + " does not exist!"); + ZipArchive archive = ZipFile.OpenRead(inputRawZipPath); // Since exe's can be differently named, we'll search for exactly one exe in no subdirectories. var exeList = archive.Entries.Where(f => f.FullName.EndsWith(".exe")).ToList(); @@ -94,6 +100,8 @@ public abstract class RawMods : ModsBase /// The raw mod zip was made for an OS that can't be determined. public static void PortToWindows(string inputRawZipPath, string outputRawZipPath, OutputHandlerDelegate outputDelegate = null) { + CheckIfOutputPathIsNull(outputRawZipPath); + ModOS currentOS = GetModOSOfRawZip(inputRawZipPath); outputDelegate.SendOutput("Zip Recognized as " + currentOS); @@ -160,6 +168,8 @@ public abstract class RawMods : ModsBase public static void PortToLinux(string inputRawZipPath, string outputRawZipPath, string pathToIcon = null, string pathToSplashScreen = null, OutputHandlerDelegate outputDelegate = null) { + CheckIfOutputPathIsNull(outputRawZipPath); + ModOS currentOS = GetModOSOfRawZip(inputRawZipPath); outputDelegate.SendOutput("Zip Recognized as " + currentOS); @@ -235,6 +245,8 @@ public abstract class RawMods : ModsBase public static void PortToAndroid(string inputRawZipPath, string outputRawApkPath, string pathToIcon = null, string pathToSplashScreen = null, bool useCustomSaveDirectory = false, bool usesInternet = false, OutputHandlerDelegate outputDelegate = null) { + CheckIfOutputPathIsNull(outputRawApkPath); + ModOS currentOS = GetModOSOfRawZip(inputRawZipPath); outputDelegate.SendOutput("Zip Recognized as " + currentOS); @@ -432,6 +444,8 @@ public abstract class RawMods : ModsBase public static void PortToMac(string inputRawZipPath, string outputRawZipPath, string pathToIcon = null, string pathToSplashScreen = null, OutputHandlerDelegate outputDelegate = null) { + CheckIfOutputPathIsNull(outputRawZipPath); + ModOS currentOS = GetModOSOfRawZip(inputRawZipPath); outputDelegate.SendOutput("Zip Recognized as " + currentOS); @@ -541,10 +555,15 @@ public abstract class RawMods : ModsBase //zip the result outputDelegate.SendOutput("Creating Mac zip..."); ZipFile.CreateFromDirectory(baseTempDirectory, outputRawZipPath); - + // Clean up Directory.Delete(TempDir, true); } + + private static void CheckIfOutputPathIsNull(string outputPath) + { + if (outputPath is null || String.IsNullOrWhiteSpace(outputPath)) throw new ArgumentOutOfRangeException(nameof(outputPath) + " cannot be null!"); + } /// /// Converts a GameMaker data file to bytecode version 16 diff --git a/AM2RPortHelperTests/RawModsTests.cs b/AM2RPortHelperTests/RawModsTests.cs index baa45ba..1be124b 100644 --- a/AM2RPortHelperTests/RawModsTests.cs +++ b/AM2RPortHelperTests/RawModsTests.cs @@ -267,6 +267,10 @@ public class RawModsTests Assert.True(File.Exists(newExtract + "/assets/" + deepSuffix.ToLower() + origInput.ToLower())); } + #endregion + + #region PortToMac + [Theory] [InlineData("./GameWin.zip", false, false)] [InlineData("./GameLin.zip", false, false)] @@ -367,7 +371,35 @@ public class RawModsTests Assert.Equal("English.lproj", new DirectoryInfo(newExtract + "/" + appDir.Name + "/Contents/Resources").GetDirectories().First(d => d.Name == "English.lproj").Name); Assert.True(File.Exists(newExtract + "/" + appDir.Name + "/Contents/Resources/" + deepSuffix.ToLower() + origInput.ToLower())); } + + #endregion + #region PortInvalidZips + + [Fact] + public void PortInvalidZipToWindows() + { + Assert.Throws(() => RawMods.PortToWindows(null, "/foo")); + Assert.Throws(() => RawMods.PortToWindows("/foo", "/foo")); + Assert.Throws(() => RawMods.PortToWindows("./GameLin.zip", null)); + } + + [Fact] + public void PortInvalidZipToLinux() + { + Assert.Throws(() => RawMods.PortToLinux(null, "/foo")); + Assert.Throws(() => RawMods.PortToLinux("/foo", "/foo")); + Assert.Throws(() => RawMods.PortToLinux("./GameLin.zip", null)); + } + + [Fact] + public void PortInvalidZipToMac() + { + Assert.Throws(() => RawMods.PortToMac(null, "/foo")); + Assert.Throws(() => RawMods.PortToMac("/foo", "/foo")); + Assert.Throws(() => RawMods.PortToMac("./GameLin.zip", null)); + } + #endregion // TODO: write tests for porttoandroid