Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andersonhc committed Aug 2, 2023
1 parent 73c6db5 commit 525a7d5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
Binary file not shown.
38 changes: 33 additions & 5 deletions test/encryption/test_encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,32 @@ def fixed_iv(size):
assert_pdf_equal(pdf, HERE / "encryption_aes256.pdf", tmp_path)


def test_encryption_aes256_with_user_password(tmp_path):
pdf = FPDF()

def custom_file_id():
return pdf._default_file_id(bytearray([0xFF]))

pdf.file_id = custom_file_id

def fixed_iv(size):
return bytearray(size)

pdf.set_author("author")
pdf.set_subject("string to be encrypted")
pdf.add_page()
pdf.set_font("helvetica", size=12)
pdf.cell(txt="hello world")
pdf.set_encryption(
owner_password="fpdf2",
user_password="1" * 1000,
encryption_method=EncryptionMethod.AES_256,
permissions=AccessPermission.all(),
)
pdf._security_handler.get_random_bytes = fixed_iv
assert_pdf_equal(pdf, HERE / "encryption_aes256_user_password.pdf", tmp_path)


def test_blank_owner_password(tmp_path):
pdf = FPDF()
pdf.set_encryption(
Expand All @@ -250,11 +276,12 @@ def test_blank_owner_password(tmp_path):


def test_password_prep():
# The PDF standard requires the passwords to be prepared using the stringprep algorithm
# using the SASLprep as per RFC 4013
# https://datatracker.ietf.org/doc/html/rfc4013
# Those assertions are bases on the examples section of the RFC
#
"""
The PDF standard requires the passwords to be prepared using the stringprep algorithm
using the SASLprep as per RFC 4013
https://datatracker.ietf.org/doc/html/rfc4013
Those assertions are bases on the examples section of the RFC
"""
assert sh.prepare_string("I\xadX") == b"IX" # SOFT HYPHEN mapped to nothing
assert sh.prepare_string("user") == b"user" # no transformation
assert sh.prepare_string("USER") == b"USER" # case preserved
Expand All @@ -265,3 +292,4 @@ def test_password_prep():
assert str(e.value) == "The password  contains prohibited characters"
with pytest.raises(FPDFException) as e:
sh.prepare_string("\u0627\x31") # Error - bidirectional check
assert sh.prepare_string("A" * 300) == b"A" * 127 # test cap 127 chars

0 comments on commit 525a7d5

Please sign in to comment.