Skip to content

Commit 00376b9

Browse files
committed
more test coverage for der parsers
1 parent 3803135 commit 00376b9

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

src/ecdsa/test_der.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
remove_bitstring,
2222
remove_object,
2323
encode_oid,
24+
remove_constructed,
25+
remove_octet_string,
26+
remove_sequence,
2427
)
2528

2629

@@ -71,6 +74,18 @@ def test_encoding_of_128(self):
7174
self.assertEqual(val, 128)
7275
self.assertFalse(rem)
7376

77+
def test_wrong_tag(self):
78+
with self.assertRaises(UnexpectedDER) as e:
79+
remove_integer(b"\x01\x02\x00\x80")
80+
81+
self.assertIn("wanted type 'integer'", str(e.exception))
82+
83+
def test_wrong_length(self):
84+
with self.assertRaises(UnexpectedDER) as e:
85+
remove_integer(b"\x02\x03\x00\x80")
86+
87+
self.assertIn("Length longer", str(e.exception))
88+
7489

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

370385

386+
class TestRemoveConstructed(unittest.TestCase):
387+
def test_simple(self):
388+
data = b"\xa1\x02\xff\xaa"
389+
390+
tag, body, rest = remove_constructed(data)
391+
392+
self.assertEqual(tag, 0x01)
393+
self.assertEqual(body, b"\xff\xaa")
394+
self.assertEqual(rest, b"")
395+
396+
def test_with_malformed_tag(self):
397+
data = b"\x01\x02\xff\xaa"
398+
399+
with self.assertRaises(UnexpectedDER) as e:
400+
remove_constructed(data)
401+
402+
self.assertIn("constructed tag", str(e.exception))
403+
404+
405+
class TestRemoveOctetString(unittest.TestCase):
406+
def test_simple(self):
407+
data = b"\x04\x03\xaa\xbb\xcc"
408+
body, rest = remove_octet_string(data)
409+
self.assertEqual(body, b"\xaa\xbb\xcc")
410+
self.assertEqual(rest, b"")
411+
412+
def test_with_malformed_tag(self):
413+
data = b"\x03\x03\xaa\xbb\xcc"
414+
with self.assertRaises(UnexpectedDER) as e:
415+
remove_octet_string(data)
416+
417+
self.assertIn("octetstring", str(e.exception))
418+
419+
420+
class TestRemoveSequence(unittest.TestCase):
421+
def test_simple(self):
422+
data = b"\x30\x02\xff\xaa"
423+
body, rest = remove_sequence(data)
424+
self.assertEqual(body, b"\xff\xaa")
425+
self.assertEqual(rest, b"")
426+
427+
def test_with_empty_string(self):
428+
with self.assertRaises(UnexpectedDER) as e:
429+
remove_sequence(b"")
430+
431+
self.assertIn("Empty string", str(e.exception))
432+
433+
def test_with_wrong_tag(self):
434+
data = b"\x20\x02\xff\xaa"
435+
436+
with self.assertRaises(UnexpectedDER) as e:
437+
remove_sequence(data)
438+
439+
self.assertIn("wanted type 'sequence'", str(e.exception))
440+
441+
def test_with_wrong_length(self):
442+
data = b"\x30\x03\xff\xaa"
443+
444+
with self.assertRaises(UnexpectedDER) as e:
445+
remove_sequence(data)
446+
447+
self.assertIn("Length longer", str(e.exception))
448+
449+
371450
@st.composite
372451
def st_oid(draw, max_value=2 ** 512, max_size=50):
373452
"""

0 commit comments

Comments
 (0)