Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TST: Improve encryption/decryption test #1009

Merged
merged 1 commit into from
Jun 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,20 +265,35 @@ def test_fill_form():
writer.write(output_stream)


def test_encrypt():
@pytest.mark.parametrize(
"use_128bit",
[(True), (False)],
)
def test_encrypt(use_128bit):
reader = PdfReader(os.path.join(RESOURCE_ROOT, "form.pdf"))
writer = PdfWriter()

page = reader.pages[0]
orig_text = page.extract_text()

writer.add_page(page)
writer.encrypt(user_pwd="userpwd", owner_pwd="ownerpwd", use_128bit=False)
writer.encrypt(user_pwd="userpwd", owner_pwd="ownerpwd", use_128bit=use_128bit)

# write "output" to PyPDF2-output.pdf
tmp_filename = "dont_commit_encrypted.pdf"
with open(tmp_filename, "wb") as output_stream:
writer.write(output_stream)

with open(tmp_filename, "rb") as input_stream:
data = input_stream.read()

assert b"foo" not in data

reader = PdfReader(tmp_filename, password="userpwd")
new_text = reader.pages[0].extract_text()

assert new_text == orig_text

# Cleanup
os.remove(tmp_filename)

Expand Down