3.24.  Passwords

Overview

In general, you can perform all tests with both unprotected and password protected PDF documents. The syntax differs slightly in the instantiation method, a second parameter is required representing the password:

// Access to password protected PDF
AssertThat.document(filename, ownerPassword) 1 2

// Test methods:
.hasEncryptionLength(..) 
.hasOwnerPassword(..) 
.hasUserPassword(..)

1

If the document is not protected, use only the first parameter.

2

If the second parameter is used, the document is considered protected.

This syntax is the same for user password and owner password.

Verifying Both Passwords

Opening a document with a password is already a test. But you can verify the second password with the methods hasOwnerPassword(..) or hasUserPassword(..):

// Verify the owner-password of the document:
@Test
public void hasOwnerPassword() throws Exception {
  String filename = "documentUnderTest.pdf";
  String userPassword = "user-password";
  
  AssertThat.document(filename, userPassword)    1
            .hasOwnerPassword("owner-password")  2
  ;
}
// Verify the user-password of the document:
@Test
public void hasUserPassword() throws Exception {
  String filename = "documentUnderTest.pdf";
  String ownerPassword = "owner-password";
  
  AssertThat.document(filename, ownerPassword)   1
            .hasUserPassword("user-password")    2
  ;
}

1 1

Open the file with one password

2 2

Verify the other password

Usually it's bad practice to hard code passwords in the source code, but it's OK for test passwords in test environments.

Verifying the Password Encryption Length

This example shows how to verify the encryption length:

@Test
public void hasEncryptionLength() throws Exception {
  String filename = "documentUnderTest.pdf";
  String userPassword = "user-password";
  
  AssertThat.document(filename, userPassword)
            .hasEncryptionLength(128)
  ;
}