Skip to content

Commit

Permalink
Reduced MaxLengthTempDirectoryThatVeraPdfFitsIn
Browse files Browse the repository at this point in the history
  • Loading branch information
stesee committed Sep 30, 2023
1 parent f57320a commit db4c6ac
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 47 deletions.
47 changes: 19 additions & 28 deletions PdfAValidator/PdfAValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -209,6 +210,11 @@ public async Task<Report> ValidateWithDetailedReportAsync(string pathToPdfFile,
/// <returns></returns>
public async Task<Report> ValidateBatchWithDetailedReportAsync(IEnumerable<string> pathsToPdfFiles, string commandLineArguments)
{
if (pathsToPdfFiles == null)
{
throw new ArgumentNullException(nameof(pathsToPdfFiles));
}

await IntiPathToVeraPdfBinAndJava().ConfigureAwait(false);

if (!IsSingleFolder(pathsToPdfFiles))
Expand Down Expand Up @@ -301,10 +307,17 @@ private static void WaitAndReceiveOutput(Process process, out string outputResul

private static bool IsSingleFolder(IEnumerable<string> 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;
Expand Down Expand Up @@ -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;
Expand All @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion PdfAValidator/PdfAValidator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.10.0.77988">
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.11.0.78383">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
20 changes: 10 additions & 10 deletions PdfAValidatorTest/PdfAValidatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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" };
Expand All @@ -301,7 +301,7 @@ public static async Task ShouldThrowPathTooLongException()
}
finally
{
Environment.SetEnvironmentVariable("TMP", initialEnvornmentTmpValue);
Environment.SetEnvironmentVariable("TMP", initialEnvironmentTmpValue);
}
}

Expand Down Expand Up @@ -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<VeraPdfException>(async () =>
Expand All @@ -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<VeraPdfException>(async () =>
Expand Down
16 changes: 8 additions & 8 deletions PdfAValidatorTest/PdfAValidatorTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
</ItemGroup>

<ItemGroup>
<None Remove="TestExecuteables\exitcode0.bat" />
<None Remove="TestExecuteables\exitcode0.sh" />
<None Remove="TestExecuteables\exitcode2.bat" />
<None Remove="TestExecuteables\exitcode2.sh" />
<None Remove="TestExecutables\exitcode0.bat" />
<None Remove="TestExecutables\exitcode0.sh" />
<None Remove="TestExecutables\exitcode2.bat" />
<None Remove="TestExecutables\exitcode2.sh" />
<None Remove="TestPdfFiles\Encrypted.pdf" />
<None Remove="TestPdfFiles\FontsNotEmbedded.pdf" />
<None Remove="TestPdfFiles\FromLibreOffice.pdf" />
Expand All @@ -31,10 +31,10 @@
</ItemGroup>

<ItemGroup>
<Content Include="TestExecuteables\exitcode2.sh">
<Content Include="TestExecutables\exitcode2.sh">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestExecuteables\exitcode0.sh">
<Content Include="TestExecutables\exitcode0.sh">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestPdfFiles\FromLibreOffice.pdf">
Expand All @@ -52,10 +52,10 @@
<Content Include="TestPdfFiles\FontsNotEmbedded.pdf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestExecuteables\exitcode2.bat">
<Content Include="TestExecutables\exitcode2.bat">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestExecuteables\exitcode0.bat">
<Content Include="TestExecutables\exitcode0.bat">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestPdfFiles\PdfNonCompliant.pdf">
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit db4c6ac

Please sign in to comment.