3.21.  Passwords

Overview

In general, you can perform all tests with both unprotected and password protected PDF documents. Passwords have to be declared with the tag <assertThat />. The general syntax looks like this:

<!-- Tags for tests with passwords: -->

<hasEncryptionLength />
<hasOwnerPassword    />
<hasUserPassword     />

<!-- Access to encrypted PDF documents: -->
<assertThat testDocument=".."    (required)
            testPassword=".."    (required if the PDF under test is encrypted)
            masterDocument=".."  (required if a master PDF is used)
            masterPassword=".."  (required if the master PDF is encrypted)
/>

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

Verifying Both Passwords

Opening a document with one password is already a test. But you can verify the other password using the tags <hasOwnerPassword /> or <hasUserPassword />:

<!-- Verify the owner-password of the document: -->

<testcase name="hasOwnerPassword">
  <assertThat testDocument="content/diverseContentOnMultiplePages_encrypted.pdf"
              testPassword="user-password"              1
  >
    <hasOwnerPassword>owner-password</hasOwnerPassword> 2
  </assertThat>
</testcase>
<!-- Verify the user-password of the document: -->

<testcase name="hasUserPassword">
  <assertThat testDocument="content/diverseContentOnMultiplePages_encrypted.pdf"
              testPassword="owner-password"             1
  >
    <hasUserPassword>user-password</hasUserPassword>    2
  </assertThat>
</testcase>

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. Hard coded also means that the password never changes.

Verifying the Encryption Length

This example shows how to verify the encryption length:

<testcase name="hasEncryptionLength">
  <assertThat testDocument="content/diverseContentOnMultiplePages_encrypted.pdf"
              testPassword="user-password"
  >
    <hasEncryptionLength>128</hasEncryptionLength>
  </assertThat>
</testcase>