from datetime import datetime
from math import ceil
from os import environ
+from string import digits
from six import add_metaclass
from six import binary_type
optional=self.optional,
_decoded=(offset, llen, l),
)
+ except DecodeError as err:
+ raise DecodeError(
+ msg=err.msg,
+ klass=self.__class__,
+ decode_path=decode_path,
+ offset=offset,
+ )
except BoundsError as err:
raise DecodeError(
msg=str(err),
>>> PrintableString("привет мир")
Traceback (most recent call last):
- UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-5: ordinal not in range(128)
+ pyderasn.DecodeError: 'ascii' codec can't encode characters in position 0-5: ordinal not in range(128)
>>> BMPString("ада", bounds=(2, 2))
Traceback (most recent call last):
value_raw = value
else:
raise InvalidValueType((self.__class__, text_type, binary_type))
- value_raw = (
- value_decoded.encode(self.encoding)
- if value_raw is None else value_raw
- )
- value_decoded = (
- value_raw.decode(self.encoding)
- if value_decoded is None else value_decoded
- )
+ try:
+ value_raw = (
+ value_decoded.encode(self.encoding)
+ if value_raw is None else value_raw
+ )
+ value_decoded = (
+ value_raw.decode(self.encoding)
+ if value_decoded is None else value_decoded
+ )
+ except (UnicodeEncodeError, UnicodeDecodeError) as err:
+ raise DecodeError(str(err))
if not self._bound_min <= len(value_decoded) <= self._bound_max:
raise BoundsError(
self._bound_min,
tag_default = tag_encode(18)
encoding = "ascii"
asn1_type_name = "NumericString"
+ allowable_chars = set(digits.encode("ascii"))
+
+ def _value_sanitize(self, value):
+ value = super(NumericString, self)._value_sanitize(value)
+ if not set(value) <= self.allowable_chars:
+ raise DecodeError("non-numeric value")
+ return value
class PrintableString(CommonString):
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
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
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