Prev |  | Next

## 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)

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

| If the document is not protected, use only the first parameter.
| 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)
                .hasOwnerPassword("owner-password")
      ;
    }

    // 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)
                .hasUserPassword("user-password")
      ;
    }

| Open the file with one password
| 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)
      ;
    }

Prev | Up | Next
3.23. Page Numbers as Objectives | Home | 3.25. PDF/A