Skip to content

Commit

Permalink
more test coverage for der parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
tomato42 committed Dec 10, 2020
1 parent 3803135 commit de1b87c
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/ecdsa/test_der.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
remove_bitstring,
remove_object,
encode_oid,
remove_constructed,
remove_octet_string,
remove_sequence,
)


Expand Down Expand Up @@ -71,6 +74,18 @@ def test_encoding_of_128(self):
self.assertEqual(val, 128)
self.assertFalse(rem)

def test_wrong_tag(self):
with self.assertRaises(UnexpectedDER) as e:
remove_integer(b"\x01\x02\x00\x80")

self.assertIn("wanted type 'integer'", str(e.exception))

def test_wrong_length(self):
with self.assertRaises(UnexpectedDER) as e:
remove_integer(b"\x02\x03\x00\x80")

self.assertIn("Length longer", str(e.exception))


class TestReadLength(unittest.TestCase):
# DER requires the lengths between 0 and 127 to be encoded using the short
Expand Down Expand Up @@ -368,6 +383,70 @@ def test_with_too_long_length(self):
remove_object(b"\x06\x03\x88\x37")


class TestRemoveConstructed(unittest.TestCase):
def test_simple(self):
data = b"\xa1\x02\xff\xaa"

tag, body, rest = remove_constructed(data)

self.assertEqual(tag, 0x01)
self.assertEqual(body, b"\xff\xaa")
self.assertEqual(rest, b"")

def test_with_malformed_tag(self):
data = b"\x01\x02\xff\xaa"

with self.assertRaises(UnexpectedDER) as e:
tag, body, rest = remove_constructed(data)

self.assertIn("constructed tag", str(e.exception))


class TestRemoveOctetString(unittest.TestCase):
def test_simple(self):
data = b"\x04\x03\xaa\xbb\xcc"
body, rest = remove_octet_string(data)
self.assertEqual(body, b"\xaa\xbb\xcc")
self.assertEqual(rest, b"")

def test_with_malformed_tag(self):
data = b"\x03\x03\xaa\xbb\xcc"
with self.assertRaises(UnexpectedDER) as e:
body, rest = remove_octet_string(data)

self.assertIn("octetstring", str(e.exception))


class TestRemoveSequence(unittest.TestCase):
def test_simple(self):
data = b"\x30\x02\xff\xaa"
body, rest = remove_sequence(data)
self.assertEqual(body, b"\xff\xaa")
self.assertEqual(rest, b"")

def test_with_empty_string(self):
with self.assertRaises(UnexpectedDER) as e:
remove_sequence(b"")

self.assertIn("Empty string", str(e.exception))

def test_with_wrong_tag(self):
data = b"\x20\x02\xff\xaa"

with self.assertRaises(UnexpectedDER) as e:
remove_sequence(data)

self.assertIn("wanted type 'sequence'", str(e.exception))

def test_with_wrong_length(self):
data = b"\x30\x03\xff\xaa"

with self.assertRaises(UnexpectedDER) as e:
remove_sequence(data)

self.assertIn("Length longer", str(e.exception))


@st.composite
def st_oid(draw, max_value=2 ** 512, max_size=50):
"""
Expand Down

0 comments on commit de1b87c

Please sign in to comment.