]> Cypherpunks.ru repositories - pyderasn.git/blobdiff - tests/test_pyderasn.py
NumericString sanitizes its value against numbers
[pyderasn.git] / tests / test_pyderasn.py
index f655d973dcb39cd7b3c80e68b1876daeacb8af98..f8377e8881e6e4454758372c28196edac0f57c80 100644 (file)
@@ -18,6 +18,7 @@
 
 from datetime import datetime
 from string import ascii_letters
+from string import digits
 from string import printable
 from string import whitespace
 from unittest import TestCase
@@ -48,6 +49,7 @@ from six import int2byte
 from six import iterbytes
 from six import PY2
 from six import text_type
+from six import unichr as six_unichr
 
 from pyderasn import _pp
 from pyderasn import abs_decode_path
@@ -2802,35 +2804,110 @@ class TestUTF8String(StringMixin, CommonMixin, TestCase):
     base_klass = UTF8String
 
 
+class UnicodeDecodeErrorMixin(object):
+    @given(text(
+        alphabet=''.join(six_unichr(i) for i in list(range(0x0410, 0x044f + 1))),
+        min_size=1,
+        max_size=5,
+    ))
+    def test_unicode_decode_error(self, cyrillic_text):
+        with self.assertRaises(DecodeError):
+            self.base_klass(cyrillic_text)
+
+
 class TestNumericString(StringMixin, CommonMixin, TestCase):
     base_klass = NumericString
 
+    def text_alphabet(self):
+        return digits
+
+    @given(text(alphabet=ascii_letters, min_size=1, max_size=5))
+    def test_non_numeric(self, cyrillic_text):
+        with assertRaisesRegex(self, DecodeError, "non-numeric"):
+            self.base_klass(cyrillic_text)
+
+    @given(
+        sets(integers(min_value=0, max_value=10), min_size=2, max_size=2),
+        integers(min_value=0),
+        lists(integers()),
+    )
+    def test_invalid_bounds_while_decoding(self, ints, offset, decode_path):
+        decode_path = tuple(str(i) for i in decode_path)
+        value, bound_min = list(sorted(ints))
+
+        class String(self.base_klass):
+            bounds = (bound_min, bound_min)
+        with self.assertRaises(DecodeError) as err:
+            String().decode(
+                self.base_klass(b"1" * value).encode(),
+                offset=offset,
+                decode_path=decode_path,
+            )
+        repr(err.exception)
+        self.assertEqual(err.exception.offset, offset)
+        self.assertEqual(err.exception.decode_path, decode_path)
+
 
-class TestPrintableString(StringMixin, CommonMixin, TestCase):
+class TestPrintableString(
+        UnicodeDecodeErrorMixin,
+        StringMixin,
+        CommonMixin,
+        TestCase,
+):
     base_klass = PrintableString
 
 
-class TestTeletexString(StringMixin, CommonMixin, TestCase):
+class TestTeletexString(
+        UnicodeDecodeErrorMixin,
+        StringMixin,
+        CommonMixin,
+        TestCase,
+):
     base_klass = TeletexString
 
 
-class TestVideotexString(StringMixin, CommonMixin, TestCase):
+class TestVideotexString(
+        UnicodeDecodeErrorMixin,
+        StringMixin,
+        CommonMixin,
+        TestCase,
+):
     base_klass = VideotexString
 
 
-class TestIA5String(StringMixin, CommonMixin, TestCase):
+class TestIA5String(
+        UnicodeDecodeErrorMixin,
+        StringMixin,
+        CommonMixin,
+        TestCase,
+):
     base_klass = IA5String
 
 
-class TestGraphicString(StringMixin, CommonMixin, TestCase):
+class TestGraphicString(
+        UnicodeDecodeErrorMixin,
+        StringMixin,
+        CommonMixin,
+        TestCase,
+):
     base_klass = GraphicString
 
 
-class TestVisibleString(StringMixin, CommonMixin, TestCase):
+class TestVisibleString(
+        UnicodeDecodeErrorMixin,
+        StringMixin,
+        CommonMixin,
+        TestCase,
+):
     base_klass = VisibleString
 
 
-class TestGeneralString(StringMixin, CommonMixin, TestCase):
+class TestGeneralString(
+        UnicodeDecodeErrorMixin,
+        StringMixin,
+        CommonMixin,
+        TestCase,
+):
     base_klass = GeneralString