Skip to content

Commit 525a7d5

Browse files
committed
add tests
1 parent 73c6db5 commit 525a7d5

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed
Binary file not shown.

test/encryption/test_encryption.py

+33-5
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,32 @@ def fixed_iv(size):
237237
assert_pdf_equal(pdf, HERE / "encryption_aes256.pdf", tmp_path)
238238

239239

240+
def test_encryption_aes256_with_user_password(tmp_path):
241+
pdf = FPDF()
242+
243+
def custom_file_id():
244+
return pdf._default_file_id(bytearray([0xFF]))
245+
246+
pdf.file_id = custom_file_id
247+
248+
def fixed_iv(size):
249+
return bytearray(size)
250+
251+
pdf.set_author("author")
252+
pdf.set_subject("string to be encrypted")
253+
pdf.add_page()
254+
pdf.set_font("helvetica", size=12)
255+
pdf.cell(txt="hello world")
256+
pdf.set_encryption(
257+
owner_password="fpdf2",
258+
user_password="1" * 1000,
259+
encryption_method=EncryptionMethod.AES_256,
260+
permissions=AccessPermission.all(),
261+
)
262+
pdf._security_handler.get_random_bytes = fixed_iv
263+
assert_pdf_equal(pdf, HERE / "encryption_aes256_user_password.pdf", tmp_path)
264+
265+
240266
def test_blank_owner_password(tmp_path):
241267
pdf = FPDF()
242268
pdf.set_encryption(
@@ -250,11 +276,12 @@ def test_blank_owner_password(tmp_path):
250276

251277

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

0 commit comments

Comments
 (0)