Skip to content

Commit

Permalink
Test PDF name verifications
Browse files Browse the repository at this point in the history
  • Loading branch information
JorjMcKie committed Oct 8, 2023
1 parent 2cf54e9 commit 50d6eff
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion fitz/fitz.i
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ struct Document


FITZEXCEPTION(xref_set_key, !result)
%pythonprepend %{
%pythonprepend xref_set_key %{
"""Set the value of a PDF dictionary key."""
if self.is_closed or self.is_encrypted:
raise ValueError("document closed or encrypted")
Expand Down
36 changes: 36 additions & 0 deletions tests/test_object_manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,39 @@ def test_trailer():
trailer_keys = doc.xref_get_keys(-1)
assert "ID" in trailer_keys
assert "Root" in trailer_keys


def test_valid_name():
"""Verify correct PDF names in method xref_set_key."""
doc = fitz.open()
page = doc.new_page()

# testing name in "key": confirm correct spec is accepted
doc.xref_set_key(page.xref, "Rotate", "90")
assert page.rotation == 90

# check wrong spec is detected
error_generated = False
try:
# illegal char in name (white space)
doc.xref_set_key(page.xref, "my rotate", "90")
except ValueError as e:
assert str(e) == "bad 'key'"
error_generated = True
assert error_generated

# test name in "value": confirm correct spec is accepted
doc.xref_set_key(page.xref, "my_rotate/something", "90")
assert doc.xref_get_key(page.xref, "my_rotate/something") == ("int", "90")
doc.xref_set_key(page.xref, "my_rotate", "/90")
assert doc.xref_get_key(page.xref, "my_rotate") == ("name", "/90")

# check wrong spec is detected
error_generated = False
try:
# no slash inside name allowed
doc.xref_set_key(page.xref, "my_rotate", "/9/0")
except ValueError as e:
assert str(e) == "bad 'value'"
error_generated = True
assert error_generated

0 comments on commit 50d6eff

Please sign in to comment.