From: Sergey Matveev Date: Sun, 20 May 2018 16:03:04 +0000 (+0300) Subject: DRY decode_path strategy X-Git-Tag: 3.7~1 X-Git-Url: http://www.git.cypherpunks.ru/?p=pyderasn.git;a=commitdiff_plain;h=62c83afbcea277bcaec2a70597e3cfdc0991410b DRY decode_path strategy --- diff --git a/tests/test_pyderasn.py b/tests/test_pyderasn.py index 8b93827..ef5f79e 100644 --- a/tests/test_pyderasn.py +++ b/tests/test_pyderasn.py @@ -125,6 +125,9 @@ tag_classes = sampled_from(( TagClassUniversal, )) tag_forms = sampled_from((TagFormConstructed, TagFormPrimitive)) +decode_path_strat = lists(integers(), max_size=3).map( + lambda decode_path: tuple(str(dp) for dp in decode_path) +) class TestHex(TestCase): @@ -463,10 +466,9 @@ class TestBoolean(CommonMixin, TestCase): @given( integers(min_value=31), integers(min_value=0), - lists(integers()), + decode_path_strat, ) def test_bad_tag(self, tag, offset, decode_path): - decode_path = tuple(str(i) for i in decode_path) with self.assertRaises(DecodeError) as err: Boolean().decode( tag_encode(tag)[:-1], @@ -480,10 +482,9 @@ class TestBoolean(CommonMixin, TestCase): @given( integers(min_value=31), integers(min_value=0), - lists(integers()), + decode_path_strat, ) def test_bad_expl_tag(self, tag, offset, decode_path): - decode_path = tuple(str(i) for i in decode_path) with self.assertRaises(DecodeError) as err: Boolean(expl=Boolean.tag_default).decode( tag_encode(tag)[:-1], @@ -497,10 +498,9 @@ class TestBoolean(CommonMixin, TestCase): @given( integers(min_value=128), integers(min_value=0), - lists(integers()), + decode_path_strat, ) def test_bad_len(self, l, offset, decode_path): - decode_path = tuple(str(i) for i in decode_path) with self.assertRaises(DecodeError) as err: Boolean().decode( Boolean.tag_default + len_encode(l)[:-1], @@ -514,10 +514,9 @@ class TestBoolean(CommonMixin, TestCase): @given( integers(min_value=128), integers(min_value=0), - lists(integers()), + decode_path_strat, ) def test_bad_expl_len(self, l, offset, decode_path): - decode_path = tuple(str(i) for i in decode_path) with self.assertRaises(DecodeError) as err: Boolean(expl=Boolean.tag_default).decode( Boolean.tag_default + len_encode(l)[:-1], @@ -913,10 +912,9 @@ class TestInteger(CommonMixin, TestCase): @given( integers(min_value=31), integers(min_value=0), - lists(integers()), + decode_path_strat, ) def test_bad_tag(self, tag, offset, decode_path): - decode_path = tuple(str(i) for i in decode_path) with self.assertRaises(DecodeError) as err: Integer().decode( tag_encode(tag)[:-1], @@ -930,10 +928,9 @@ class TestInteger(CommonMixin, TestCase): @given( integers(min_value=128), integers(min_value=0), - lists(integers()), + decode_path_strat, ) def test_bad_len(self, l, offset, decode_path): - decode_path = tuple(str(i) for i in decode_path) with self.assertRaises(DecodeError) as err: Integer().decode( Integer.tag_default + len_encode(l)[:-1], @@ -947,10 +944,9 @@ class TestInteger(CommonMixin, TestCase): @given( sets(integers(), min_size=2, max_size=2), integers(min_value=0), - lists(integers()), + decode_path_strat, ) 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 Int(Integer): @@ -1307,10 +1303,9 @@ class TestBitString(CommonMixin, TestCase): @given( integers(min_value=31), integers(min_value=0), - lists(integers()), + decode_path_strat, ) def test_bad_tag(self, tag, offset, decode_path): - decode_path = tuple(str(i) for i in decode_path) with self.assertRaises(DecodeError) as err: BitString().decode( tag_encode(tag)[:-1], @@ -1324,10 +1319,9 @@ class TestBitString(CommonMixin, TestCase): @given( integers(min_value=128), integers(min_value=0), - lists(integers()), + decode_path_strat, ) def test_bad_len(self, l, offset, decode_path): - decode_path = tuple(str(i) for i in decode_path) with self.assertRaises(DecodeError) as err: BitString().decode( BitString.tag_default + len_encode(l)[:-1], @@ -1739,10 +1733,9 @@ class TestOctetString(CommonMixin, TestCase): @given( integers(min_value=31), integers(min_value=0), - lists(integers()), + decode_path_strat, ) def test_bad_tag(self, tag, offset, decode_path): - decode_path = tuple(str(i) for i in decode_path) with self.assertRaises(DecodeError) as err: OctetString().decode( tag_encode(tag)[:-1], @@ -1756,10 +1749,9 @@ class TestOctetString(CommonMixin, TestCase): @given( integers(min_value=128), integers(min_value=0), - lists(integers()), + decode_path_strat, ) def test_bad_len(self, l, offset, decode_path): - decode_path = tuple(str(i) for i in decode_path) with self.assertRaises(DecodeError) as err: OctetString().decode( OctetString.tag_default + len_encode(l)[:-1], @@ -1773,10 +1765,9 @@ class TestOctetString(CommonMixin, TestCase): @given( sets(integers(min_value=0, max_value=10), min_size=2, max_size=2), integers(min_value=0), - lists(integers()), + decode_path_strat, ) 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(OctetString): @@ -1998,10 +1989,9 @@ class TestNull(CommonMixin, TestCase): @given( integers(min_value=31), integers(min_value=0), - lists(integers()), + decode_path_strat, ) def test_bad_tag(self, tag, offset, decode_path): - decode_path = tuple(str(i) for i in decode_path) with self.assertRaises(DecodeError) as err: Null().decode( tag_encode(tag)[:-1], @@ -2015,10 +2005,9 @@ class TestNull(CommonMixin, TestCase): @given( integers(min_value=128), integers(min_value=0), - lists(integers()), + decode_path_strat, ) def test_bad_len(self, l, offset, decode_path): - decode_path = tuple(str(i) for i in decode_path) with self.assertRaises(DecodeError) as err: Null().decode( Null.tag_default + len_encode(l)[:-1], @@ -2270,10 +2259,9 @@ class TestObjectIdentifier(CommonMixin, TestCase): @given( integers(min_value=31), integers(min_value=0), - lists(integers()), + decode_path_strat, ) def test_bad_tag(self, tag, offset, decode_path): - decode_path = tuple(str(i) for i in decode_path) with self.assertRaises(DecodeError) as err: ObjectIdentifier().decode( tag_encode(tag)[:-1], @@ -2287,10 +2275,9 @@ class TestObjectIdentifier(CommonMixin, TestCase): @given( integers(min_value=128), integers(min_value=0), - lists(integers()), + decode_path_strat, ) def test_bad_len(self, l, offset, decode_path): - decode_path = tuple(str(i) for i in decode_path) with self.assertRaises(DecodeError) as err: ObjectIdentifier().decode( ObjectIdentifier.tag_default + len_encode(l)[:-1], @@ -2905,10 +2892,9 @@ class StringMixin(object): @given( integers(min_value=31), integers(min_value=0), - lists(integers()), + decode_path_strat, ) def test_bad_tag(self, tag, offset, decode_path): - decode_path = tuple(str(i) for i in decode_path) with self.assertRaises(DecodeError) as err: self.base_klass().decode( tag_encode(tag)[:-1], @@ -2922,10 +2908,9 @@ class StringMixin(object): @given( integers(min_value=128), integers(min_value=0), - lists(integers()), + decode_path_strat, ) def test_bad_len(self, l, offset, decode_path): - decode_path = tuple(str(i) for i in decode_path) with self.assertRaises(DecodeError) as err: self.base_klass().decode( self.base_klass.tag_default + len_encode(l)[:-1], @@ -2939,10 +2924,9 @@ class StringMixin(object): @given( sets(integers(min_value=0, max_value=10), min_size=2, max_size=2), integers(min_value=0), - lists(integers()), + decode_path_strat, ) 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): @@ -3040,10 +3024,9 @@ class TestNumericString(StringMixin, CommonMixin, TestCase): @given( sets(integers(min_value=0, max_value=10), min_size=2, max_size=2), integers(min_value=0), - lists(integers()), + decode_path_strat, ) 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): @@ -3638,10 +3621,9 @@ class TestAny(CommonMixin, TestCase): @given( integers(min_value=31), integers(min_value=0), - lists(integers()), + decode_path_strat, ) def test_bad_tag(self, tag, offset, decode_path): - decode_path = tuple(str(i) for i in decode_path) with self.assertRaises(DecodeError) as err: Any().decode( tag_encode(tag)[:-1], @@ -3655,10 +3637,9 @@ class TestAny(CommonMixin, TestCase): @given( integers(min_value=128), integers(min_value=0), - lists(integers()), + decode_path_strat, ) def test_bad_len(self, l, offset, decode_path): - decode_path = tuple(str(i) for i in decode_path) with self.assertRaises(DecodeError) as err: Any().decode( Any.tag_default + len_encode(l)[:-1], @@ -4384,10 +4365,9 @@ class SeqMixing(object): @given( integers(min_value=31), integers(min_value=0), - lists(integers()), + decode_path_strat, ) def test_bad_tag(self, tag, offset, decode_path): - decode_path = tuple(str(i) for i in decode_path) with self.assertRaises(DecodeError) as err: self.base_klass().decode( tag_encode(tag)[:-1], @@ -4401,10 +4381,9 @@ class SeqMixing(object): @given( integers(min_value=128), integers(min_value=0), - lists(integers()), + decode_path_strat, ) def test_bad_len(self, l, offset, decode_path): - decode_path = tuple(str(i) for i in decode_path) with self.assertRaises(DecodeError) as err: self.base_klass().decode( self.base_klass.tag_default + len_encode(l)[:-1], @@ -4937,10 +4916,9 @@ class SeqOfMixing(object): @given( integers(min_value=31), integers(min_value=0), - lists(integers()), + decode_path_strat, ) def test_bad_tag(self, tag, offset, decode_path): - decode_path = tuple(str(i) for i in decode_path) with self.assertRaises(DecodeError) as err: self.base_klass().decode( tag_encode(tag)[:-1], @@ -4954,10 +4932,9 @@ class SeqOfMixing(object): @given( integers(min_value=128), integers(min_value=0), - lists(integers()), + decode_path_strat, ) def test_bad_len(self, l, offset, decode_path): - decode_path = tuple(str(i) for i in decode_path) with self.assertRaises(DecodeError) as err: self.base_klass().decode( self.base_klass.tag_default + len_encode(l)[:-1],