Skip to content

Commit 074d80f

Browse files
committed
Test the new decoder with hypothesis.
1 parent 83e5e5b commit 074d80f

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

test/test_huffman.py

+25
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# -*- coding: utf-8 -*-
2+
from hpack.exceptions import HPACKDecodingError
23
from hpack.huffman_table import decode_huffman
34
from hpack.huffman import HuffmanEncoder
45
from hpack.huffman_constants import REQUEST_CODES, REQUEST_CODES_LENGTH
56

7+
from hypothesis import given, example
8+
from hypothesis.strategies import binary
9+
610

711
class TestHuffman(object):
812

@@ -18,3 +22,24 @@ def test_request_huffman_encode(self):
1822
assert encoder.encode(b"no-cache") == b'\xa8\xeb\x10d\x9c\xbf'
1923
assert encoder.encode(b"custom-key") == b'%\xa8I\xe9[\xa9}\x7f'
2024
assert encoder.encode(b"custom-value") == b'%\xa8I\xe9[\xb8\xe8\xb4\xbf'
25+
26+
27+
class TestHuffmanDecoder(object):
28+
@given(data=binary())
29+
@example(b'\xff')
30+
@example(b'\x5f\xff\xff\xff\xff')
31+
@example(b'\x00\x3f\xff\xff\xff')
32+
def test_huffman_decoder_properly_handles_all_bytestrings(self, data):
33+
"""
34+
When given random bytestrings, either we get HPACKDecodingError or we
35+
get a bytestring back.
36+
"""
37+
# The examples aren't special, they're just known to hit specific error
38+
# paths through the state machine. Basically, they are strings that are
39+
# definitely invalid.
40+
try:
41+
result = decode_huffman(data)
42+
except HPACKDecodingError:
43+
result = b''
44+
45+
assert isinstance(result, bytes)

0 commit comments

Comments
 (0)