From: Sergey Matveev Date: Mon, 23 Jul 2018 18:46:44 +0000 (+0300) Subject: Fix SequenceOf/SetOf BoundsError raising X-Git-Tag: 3.12^0 X-Git-Url: http://www.git.cypherpunks.ru/?p=pyderasn.git;a=commitdiff_plain;h=a862bd86ae10ebed1bc42e172b89b74e091e20f5 Fix SequenceOf/SetOf BoundsError raising --- diff --git a/doc/news.rst b/doc/news.rst index c1c069a..8fccd21 100644 --- a/doc/news.rst +++ b/doc/news.rst @@ -6,6 +6,7 @@ News 3.12 ---- * Fix possible uncaught TypeError in Py2 with zero bytes inside the value. +* Fix SequenceOf/SetOf raising BoundsError instead of DecodeError. .. _release3.11: diff --git a/pyderasn.py b/pyderasn.py index b9bc9da..38b0548 100755 --- a/pyderasn.py +++ b/pyderasn.py @@ -5025,16 +5025,24 @@ class SequenceOf(Obj): vlen += value_len v = v_tail _value.append(value) - obj = self.__class__( - value=_value, - schema=spec, - bounds=(self._bound_min, self._bound_max), - impl=self.tag, - expl=self._expl, - default=self.default, - optional=self.optional, - _decoded=(offset, llen, vlen + (EOC_LEN if lenindef else 0)), - ) + try: + obj = self.__class__( + value=_value, + schema=spec, + bounds=(self._bound_min, self._bound_max), + impl=self.tag, + expl=self._expl, + default=self.default, + optional=self.optional, + _decoded=(offset, llen, vlen + (EOC_LEN if lenindef else 0)), + ) + except BoundsError as err: + raise DecodeError( + msg=str(err), + klass=self.__class__, + decode_path=decode_path, + offset=offset, + ) if lenindef: if v[:EOC_LEN].tobytes() != EOC: raise DecodeError( diff --git a/tests/test_pyderasn.py b/tests/test_pyderasn.py index 147e3cd..c85a90a 100644 --- a/tests/test_pyderasn.py +++ b/tests/test_pyderasn.py @@ -809,9 +809,19 @@ class TestInteger(CommonMixin, TestCase): with self.assertRaises(BoundsError) as err: Integer(value=values[0], bounds=(values[1], values[2])) repr(err.exception) + with assertRaisesRegex(self, DecodeError, "bounds") as err: + Integer(bounds=(values[1], values[2])).decode( + Integer(values[0]).encode() + ) + repr(err.exception) with self.assertRaises(BoundsError) as err: Integer(value=values[2], bounds=(values[0], values[1])) repr(err.exception) + with assertRaisesRegex(self, DecodeError, "bounds") as err: + Integer(bounds=(values[0], values[1])).decode( + Integer(values[2]).encode() + ) + repr(err.exception) @given(data_strategy()) def test_call(self, d): @@ -1762,10 +1772,20 @@ class TestOctetString(CommonMixin, TestCase): with self.assertRaises(BoundsError) as err: OctetString(value=value, bounds=(bound_min, bound_max)) repr(err.exception) + with assertRaisesRegex(self, DecodeError, "bounds") as err: + OctetString(bounds=(bound_min, bound_max)).decode( + OctetString(value).encode() + ) + repr(err.exception) value = d.draw(binary(min_size=bound_max + 1)) with self.assertRaises(BoundsError) as err: OctetString(value=value, bounds=(bound_min, bound_max)) repr(err.exception) + with assertRaisesRegex(self, DecodeError, "bounds") as err: + OctetString(bounds=(bound_min, bound_max)).decode( + OctetString(value).encode() + ) + repr(err.exception) @given(data_strategy()) def test_call(self, d): @@ -2978,10 +2998,20 @@ class StringMixin(object): with self.assertRaises(BoundsError) as err: self.base_klass(value=value, bounds=(bound_min, bound_max)) repr(err.exception) + with assertRaisesRegex(self, DecodeError, "bounds") as err: + self.base_klass(bounds=(bound_min, bound_max)).decode( + self.base_klass(value).encode() + ) + repr(err.exception) value = d.draw(text(alphabet=self.text_alphabet(), min_size=bound_max + 1)) with self.assertRaises(BoundsError) as err: self.base_klass(value=value, bounds=(bound_min, bound_max)) repr(err.exception) + with assertRaisesRegex(self, DecodeError, "bounds") as err: + self.base_klass(bounds=(bound_min, bound_max)).decode( + self.base_klass(value).encode() + ) + repr(err.exception) @given(data_strategy()) def test_call(self, d): @@ -5080,17 +5110,27 @@ class SeqOfMixing(object): schema = Boolean() bound_min = d.draw(integers(min_value=1, max_value=1 << 7)) bound_max = d.draw(integers(min_value=bound_min, max_value=1 << 7)) - value = [Boolean()] * d.draw(integers(max_value=bound_min - 1)) + value = [Boolean(False)] * d.draw(integers(max_value=bound_min - 1)) with self.assertRaises(BoundsError) as err: SeqOf(value=value, bounds=(bound_min, bound_max)) repr(err.exception) - value = [Boolean()] * d.draw(integers( + with assertRaisesRegex(self, DecodeError, "bounds") as err: + SeqOf(bounds=(bound_min, bound_max)).decode( + SeqOf(value).encode() + ) + repr(err.exception) + value = [Boolean(True)] * d.draw(integers( min_value=bound_max + 1, max_value=bound_max + 10, )) with self.assertRaises(BoundsError) as err: SeqOf(value=value, bounds=(bound_min, bound_max)) repr(err.exception) + with assertRaisesRegex(self, DecodeError, "bounds") as err: + SeqOf(bounds=(bound_min, bound_max)).decode( + SeqOf(value).encode() + ) + repr(err.exception) @given(integers(min_value=1, max_value=10)) def test_out_of_bounds(self, bound_max):