From db4c6ac8c8043728022aee3bba0a9bd6329eb18a Mon Sep 17 00:00:00 2001 From: Stefan Seeland <168659+stesee@users.noreply.github.com> Date: Sat, 30 Sep 2023 19:15:34 +0200 Subject: [PATCH] Reduced MaxLengthTempDirectoryThatVeraPdfFitsIn --- PdfAValidator/PdfAValidator.cs | 47 ++++++++----------- PdfAValidator/PdfAValidator.csproj | 2 +- PdfAValidatorTest/PdfAValidatorTest.cs | 20 ++++---- PdfAValidatorTest/PdfAValidatorTest.csproj | 16 +++---- .../exitcode0.bat | 0 .../exitcode0.sh | 0 .../exitcode2.bat | 0 .../exitcode2.sh | 0 8 files changed, 38 insertions(+), 47 deletions(-) rename PdfAValidatorTest/{TestExecuteables => TestExecutables}/exitcode0.bat (100%) rename PdfAValidatorTest/{TestExecuteables => TestExecutables}/exitcode0.sh (100%) mode change 100755 => 100644 rename PdfAValidatorTest/{TestExecuteables => TestExecutables}/exitcode2.bat (100%) rename PdfAValidatorTest/{TestExecuteables => TestExecutables}/exitcode2.sh (100%) mode change 100755 => 100644 diff --git a/PdfAValidator/PdfAValidator.cs b/PdfAValidator/PdfAValidator.cs index a1d5b3e..63987b2 100644 --- a/PdfAValidator/PdfAValidator.cs +++ b/PdfAValidator/PdfAValidator.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Globalization; using System.IO; using System.IO.Compression; using System.Linq; @@ -157,16 +158,16 @@ private void InitRuntimeSpecificMaxLengthTempdirectoryThatVeraPdfFitsIn() { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { - MaxLengthTempDirectoryThatVeraPdfFitsIn = 197; + MaxLengthTempDirectoryThatVeraPdfFitsIn = 201; } else { MaxLengthTempDirectoryThatVeraPdfFitsIn = 260; } - if (RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework")) + if (RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework", true, CultureInfo.InvariantCulture)) { - MaxLengthTempDirectoryThatVeraPdfFitsIn = 76; + MaxLengthTempDirectoryThatVeraPdfFitsIn = 55; } } @@ -209,6 +210,11 @@ public async Task ValidateWithDetailedReportAsync(string pathToPdfFile, /// public async Task ValidateBatchWithDetailedReportAsync(IEnumerable pathsToPdfFiles, string commandLineArguments) { + if (pathsToPdfFiles == null) + { + throw new ArgumentNullException(nameof(pathsToPdfFiles)); + } + await IntiPathToVeraPdfBinAndJava().ConfigureAwait(false); if (!IsSingleFolder(pathsToPdfFiles)) @@ -301,10 +307,17 @@ private static void WaitAndReceiveOutput(Process process, out string outputResul private static bool IsSingleFolder(IEnumerable pathsToPdfFiles) { - var isSingle = pathsToPdfFiles.Count() == 1; + if (pathsToPdfFiles == null) + { + throw new ArgumentNullException(nameof(pathsToPdfFiles)); + } + + var enumeratedPathsToPdfFiles = pathsToPdfFiles.ToArray(); + + var isSingle = enumeratedPathsToPdfFiles.Length == 1; if (isSingle) { - var absolutePath = Path.GetFullPath(pathsToPdfFiles.First()); + var absolutePath = Path.GetFullPath(enumeratedPathsToPdfFiles[0]); return Directory.Exists(absolutePath); } return false; @@ -385,11 +398,10 @@ private async Task IntiPathToVeraPdfBinAndJava() Directory.CreateDirectory(pathVeraPdfDirectory); await ExtractBinaryFromManifest("Codeuctivity.VeraPdf.zip").ConfigureAwait(false); VeraPdfStartScript = Path.Combine(pathVeraPdfDirectory, "verapdf"); - SetLinuxFileExecutable(VeraPdfStartScript); } else { - throw new NotImplementedException(Resources.OsNotSupportedMessage); + throw new NotSupportedException(Resources.OsNotSupportedMessage); } IsInitialized = true; @@ -400,27 +412,6 @@ private async Task IntiPathToVeraPdfBinAndJava() } } - private static void SetLinuxFileExecutable(string filePath) - { - var chmodCmd = "chmod 700 " + filePath; - var escapedArgs = chmodCmd.Replace(maskedQuote, "\\\""); - - using var process = new Process - { - StartInfo = new ProcessStartInfo - { - RedirectStandardOutput = true, - UseShellExecute = false, - CreateNoWindow = true, - WindowStyle = ProcessWindowStyle.Hidden, - FileName = "/bin/bash", - Arguments = $"-c \"{escapedArgs}\"" - } - }; - process.Start(); - process.WaitForExit(); - } - private async Task ExtractBinaryFromManifest(string resourceName) { var pathZipVeraPdf = Path.Combine(pathVeraPdfDirectory, $"{Guid.NewGuid()}.zip"); diff --git a/PdfAValidator/PdfAValidator.csproj b/PdfAValidator/PdfAValidator.csproj index 576aa19..153cde4 100644 --- a/PdfAValidator/PdfAValidator.csproj +++ b/PdfAValidator/PdfAValidator.csproj @@ -56,7 +56,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/PdfAValidatorTest/PdfAValidatorTest.cs b/PdfAValidatorTest/PdfAValidatorTest.cs index 6c1dafe..d1c0eed 100644 --- a/PdfAValidatorTest/PdfAValidatorTest.cs +++ b/PdfAValidatorTest/PdfAValidatorTest.cs @@ -284,13 +284,13 @@ public static async Task ShouldThrowPathTooLongException() return; } - var initialEnvornmentTmpValue = Environment.GetEnvironmentVariable("TMP"); + var initialEnvironmentTmpValue = Environment.GetEnvironmentVariable("TMP"); try { - var realyLongPath = Path.Combine(initialEnvornmentTmpValue!, "RealyLongRealyLongRealyLongRealyLongRealyLongRealyLongRealyLongRealyLongRealyLongRealyLongRealyLongRealyLongRealyRealyLongRealy", Guid.NewGuid().ToString()); + var reallyLongPath = Path.Combine(initialEnvironmentTmpValue!, "ReallyLongReallyLongReallyLongReallyLongReallyLongReallyLongReallyLongReallyLongReallyLongReallyLongReallyLongReallyLongReallyLong", Guid.NewGuid().ToString()); - Environment.SetEnvironmentVariable("TMP", realyLongPath); + Environment.SetEnvironmentVariable("TMP", reallyLongPath); using var pdfAValidator = new PdfAValidator(); var files = new[] { "./TestPdfFiles/FromLibreOffice.pdf", "./TestPdfFiles/FromLibreOfficeNonPdfA.pdf" }; @@ -301,7 +301,7 @@ public static async Task ShouldThrowPathTooLongException() } finally { - Environment.SetEnvironmentVariable("TMP", initialEnvornmentTmpValue); + Environment.SetEnvironmentVariable("TMP", initialEnvironmentTmpValue); } } @@ -392,13 +392,13 @@ public static async Task ShouldSafeGuardToAKnownMaxTemporaryPathLengthWhichStill } [Fact] - public static async Task ShouldFailGracefullWithUnrecognicedVeraPdfOutput() + public static async Task ShouldFailGracefulWithUnrecognizedVeraPdfOutput() { - var somethingThatReturnsExitcode0 = "./TestExecuteables/exitcode0.bat"; + var somethingThatReturnsExitcode0 = "./TestExecutables/exitcode0.bat"; if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { - somethingThatReturnsExitcode0 = "TestExecuteables/exitcode0.sh"; + somethingThatReturnsExitcode0 = "TestExecutables/exitcode0.sh"; } var veraPdfException = await Assert.ThrowsAsync(async () => @@ -412,13 +412,13 @@ public static async Task ShouldFailGracefullWithUnrecognicedVeraPdfOutput() } [Fact] - public static async Task ShouldFailGracefullWithExitcode2() + public static async Task ShouldFailGracefullyWithExitcode2() { - var somethingThatReturnsExitcode2 = "./TestExecuteables/exitcode2.bat"; + var somethingThatReturnsExitcode2 = "./TestExecutables/exitcode2.bat"; if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { - somethingThatReturnsExitcode2 = "TestExecuteables/exitcode2.sh"; + somethingThatReturnsExitcode2 = "TestExecutables/exitcode2.sh"; } var veraPdfException = await Assert.ThrowsAsync(async () => diff --git a/PdfAValidatorTest/PdfAValidatorTest.csproj b/PdfAValidatorTest/PdfAValidatorTest.csproj index 05b43e2..cc8a756 100644 --- a/PdfAValidatorTest/PdfAValidatorTest.csproj +++ b/PdfAValidatorTest/PdfAValidatorTest.csproj @@ -16,10 +16,10 @@ - - - - + + + + @@ -31,10 +31,10 @@ - + PreserveNewest - + PreserveNewest @@ -52,10 +52,10 @@ PreserveNewest - + PreserveNewest - + PreserveNewest diff --git a/PdfAValidatorTest/TestExecuteables/exitcode0.bat b/PdfAValidatorTest/TestExecutables/exitcode0.bat similarity index 100% rename from PdfAValidatorTest/TestExecuteables/exitcode0.bat rename to PdfAValidatorTest/TestExecutables/exitcode0.bat diff --git a/PdfAValidatorTest/TestExecuteables/exitcode0.sh b/PdfAValidatorTest/TestExecutables/exitcode0.sh old mode 100755 new mode 100644 similarity index 100% rename from PdfAValidatorTest/TestExecuteables/exitcode0.sh rename to PdfAValidatorTest/TestExecutables/exitcode0.sh diff --git a/PdfAValidatorTest/TestExecuteables/exitcode2.bat b/PdfAValidatorTest/TestExecutables/exitcode2.bat similarity index 100% rename from PdfAValidatorTest/TestExecuteables/exitcode2.bat rename to PdfAValidatorTest/TestExecutables/exitcode2.bat diff --git a/PdfAValidatorTest/TestExecuteables/exitcode2.sh b/PdfAValidatorTest/TestExecutables/exitcode2.sh old mode 100755 new mode 100644 similarity index 100% rename from PdfAValidatorTest/TestExecuteables/exitcode2.sh rename to PdfAValidatorTest/TestExecutables/exitcode2.sh