Skip to content

Commit 1095194

Browse files
committed
more test coverage for der parsers
1 parent 6786041 commit 1095194

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

src/ecdsa/test_der.py

+79
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
remove_bitstring,
2323
remove_object,
2424
encode_oid,
25+
remove_constructed,
26+
remove_octet_string,
27+
remove_sequence,
2528
)
2629

2730

@@ -72,6 +75,18 @@ def test_encoding_of_128(self):
7275
self.assertEqual(val, 128)
7376
self.assertFalse(rem)
7477

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

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

371386

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

0 commit comments

Comments
 (0)