allow the user to supply multiple passwords for decryption

previously the only way to test if a password was correct was to supply a single password and throw if the value was incorrect. this was slow. now parsing options supports a list of passwords as well as a single password option (which is equivalent to a list with a single item). these passwords are all tested at the same time and an exception is only thrown once all passwords are tested.
This commit is contained in:
Eliot Jones
2019-12-20 15:11:05 +00:00
parent 5e68720495
commit 4d697e3669
6 changed files with 136 additions and 32 deletions

View File

@@ -0,0 +1,53 @@
namespace UglyToad.PdfPig.Tests.Integration
{
using System;
using System.Collections.Generic;
using Exceptions;
using Xunit;
public class EncryptedDocumentTests
{
private const string FileName = "encrypted-password-is-password.pdf";
private const string Password = "password";
[Fact]
public void NoPasswordThrows()
{
Action action = () => PdfDocument.Open(GetPath());
Assert.Throws<PdfDocumentEncryptedException>(action);
}
[Fact]
public void CanOpenDocumentAndGetPage()
{
using (var document = PdfDocument.Open(GetPath(), new ParsingOptions
{
Password = Password
}))
{
foreach (var page in document.GetPages())
{
Assert.NotNull(page.Text);
}
}
}
[Fact]
public void CanProvideMultiplePasswords()
{
using (var document = PdfDocument.Open(GetPath(), new ParsingOptions
{
Passwords = new List<string> { "pangolin", "harpsichord", Password }
}))
{
foreach (var page in document.GetPages())
{
Assert.NotNull(page.Text);
}
}
}
private static string GetPath() => IntegrationHelpers.GetSpecificTestDocumentPath(FileName);
}
}

View File

@@ -29,6 +29,9 @@
<Content Include="Integration\Documents\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Integration\SpecificTestDocuments\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
@@ -73,8 +76,6 @@
<Content Include="Fonts\Type1\CMBX12.pfa" />
<Content Include="Fonts\Type1\CMCSC10.pfa" />
<Content Include="Fonts\Type1\Raleway-Black.pfb" />
<Content Include="Integration\SpecificTestDocuments\invalid-xref-loop.pdf" />
<Content Include="Integration\SpecificTestDocuments\custom-properties.pdf" />
<Content Include="Parser\SimpleGoogleDocPageContent.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>