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: Add test for filters.ASCIIHexDecode #822

Merged
merged 3 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
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
8 changes: 6 additions & 2 deletions .github/workflows/github-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ jobs:
- name: Test with pytest
run: |
python -m coverage run --parallel-mode -m pytest Tests -vv
if: matrix.python-version != '3.10.1'
- name: Test with pytest
if: matrix.python-version != '3.10.1' && matrix.python-version != 2.7
- name: Test with pytest (2.7)
run: |
python -m coverage run --parallel-mode -m pytest Tests -vv -m "not no_py27"
if: matrix.python-version == 2.7
- name: Test with pytest (OO flag)
run: |
python -OO -m coverage run --parallel-mode -m pytest Tests -vv
if: matrix.python-version == '3.10.1'
Expand Down
16 changes: 15 additions & 1 deletion PyPDF2/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from PyPDF2.constants import ImageAttributes as IA
from PyPDF2.constants import LzwFilterParameters as LZW
from PyPDF2.constants import StreamAttributes as SA
from PyPDF2.errors import PdfReadError
from PyPDF2.errors import PdfReadError, PdfStreamError
from PyPDF2.utils import ord_, paethPredictor

if version_info < ( 3, 0 ):
Expand Down Expand Up @@ -194,12 +194,26 @@ def encode(data):


class ASCIIHexDecode(object):
"""
The ASCIIHexDecode filter decodes data that has been encoded in ASCII
hexadecimal form into a base-7 ASCII format.
"""

@staticmethod
def decode(data, decodeParms=None):
"""
:param data: a str sequence of hexadecimal-encoded values to be
converted into a base-7 ASCII string
:param decodeParms:
:return: a string conversion in base-7 ASCII, where each of its values
v is such that 0 <= ord(v) <= 127.
"""
retval = ""
char = ""
x = 0
while True:
if x >= len(data):
raise PdfStreamError("Unexpected EOD in ASCIIHexDecode")
c = data[x]
if c == ">":
break
Expand Down
59 changes: 59 additions & 0 deletions Tests/test_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from PyPDF2.filters import ASCIIHexDecode
import string
from PyPDF2.errors import PdfStreamError
import pytest


@pytest.mark.parametrize(
"input,expected",
[
(">", ""),
(
"6162636465666768696a6b6c6d6e6f707172737475767778797a>",
string.ascii_lowercase,
),
(
"4142434445464748494a4b4c4d4e4f505152535455565758595a>",
string.ascii_uppercase,
),
(
"6162636465666768696a6b6c6d6e6f707172737475767778797a4142434445464748494a4b4c4d4e4f505152535455565758595a>",
string.ascii_letters,
),
("30313233343536373839>", string.digits),
(
"3 031323334353637 3839>",
string.digits,
), # Same as previous, but whitespaced
("30313233343536373839616263646566414243444546>", string.hexdigits),
("20090a0d0b0c>", string.whitespace),
],
ids=[
"empty",
"ascii_lowercase",
"ascii_uppercase",
"ascii_letters",
"digits",
"digits_whitespace",
"hexdigits",
"whitespace",
],
)
@pytest.mark.no_py27
def test_expected_results(input, expected):
"""
Feeds a bunch of values to ASCIIHexDecode.decode() and ensures the
correct output is returned.
TO-DO What is decode() supposed to do for such inputs as ">>", ">>>" or
any other not terminated by ">"? (For the latter case, an exception
is currently raised.)
"""

assert ASCIIHexDecode.decode(input) == expected


def test_no_eod():
"""Ensuring an exception is raised when no EOD character is present"""
with pytest.raises(PdfStreamError) as exc:
ASCIIHexDecode.decode("")
assert exc.value.args[0] == "Unexpected EOD in ASCIIHexDecode"
3 changes: 3 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
markers =
no_py27: Flag for tests that fail under Python 2.7 only