X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=tests%2Ftest_pyderasn.py;h=15f54f90d43dd611b8239c5aa43e4490daf5554d;hb=038b5d9eab3e5dc2a203f064f22caa854f5b68ae;hp=f655d973dcb39cd7b3c80e68b1876daeacb8af98;hpb=eb67733960022e82168120c03b5c0e81272ddb2b;p=pyderasn.git diff --git a/tests/test_pyderasn.py b/tests/test_pyderasn.py index f655d97..15f54f9 100644 --- a/tests/test_pyderasn.py +++ b/tests/test_pyderasn.py @@ -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 @@ -42,12 +43,14 @@ from hypothesis.strategies import sets from hypothesis.strategies import text from hypothesis.strategies import tuples from six import assertRaisesRegex +from six import binary_type from six import byte2int from six import indexbytes 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 @@ -60,6 +63,7 @@ from pyderasn import Choice from pyderasn import DecodeError from pyderasn import DecodePathDefBy from pyderasn import Enumerated +from pyderasn import EOC from pyderasn import GeneralizedTime from pyderasn import GeneralString from pyderasn import GraphicString @@ -106,11 +110,11 @@ from pyderasn import VideotexString from pyderasn import VisibleString -settings.register_profile('local', settings( +settings.register_profile("local", settings( deadline=5000, perform_health_check=False, )) -settings.load_profile('local') +settings.load_profile("local") LONG_TEST_MAX_EXAMPLES = settings().max_examples * 4 tag_classes = sampled_from(( @@ -283,7 +287,7 @@ class CommonMixin(object): with self.assertRaises(ValueError): self.base_klass(impl=b"whatever", expl=b"whenever") - @given(binary(), integers(), integers(), integers()) + @given(binary(min_size=1), integers(), integers(), integers()) def test_decoded(self, impl, offset, llen, vlen): obj = self.base_klass(impl=impl, _decoded=(offset, llen, vlen)) self.assertEqual(obj.offset, offset) @@ -292,7 +296,7 @@ class CommonMixin(object): self.assertEqual(obj.tlen, len(impl)) self.assertEqual(obj.tlvlen, obj.tlen + obj.llen + obj.vlen) - @given(binary()) + @given(binary(min_size=1)) def test_impl_inherited(self, impl_tag): class Inherited(self.base_klass): impl = impl_tag @@ -585,13 +589,23 @@ class TestBoolean(CommonMixin, TestCase): ))) @given(integers(min_value=0 + 1, max_value=255 - 1)) - def test_invalid_value(self, value): + def test_ber_value(self, value): with assertRaisesRegex(self, DecodeError, "unacceptable Boolean value"): Boolean().decode(b"".join(( Boolean.tag_default, len_encode(1), int2byte(value), ))) + obj, _ = Boolean().decode( + b"".join(( + Boolean.tag_default, + len_encode(1), + int2byte(value), + )), + ctx={"bered": True}, + ) + self.assertTrue(bool(obj)) + self.assertTrue(obj.bered) @composite @@ -1136,8 +1150,8 @@ class TestBitString(CommonMixin, TestCase): @given( tuples(integers(min_value=0), binary()), tuples(integers(min_value=0), binary()), - binary(), - binary(), + binary(min_size=1), + binary(min_size=1), ) def test_comparison(self, value1, value2, tag1, tag2): for klass in (BitString, BitStringInherited): @@ -1384,6 +1398,86 @@ class TestBitString(CommonMixin, TestCase): self.assertTrue(obj[9]) self.assertFalse(obj[17]) + @given( + integers(min_value=1, max_value=30), + lists( + one_of( + binary(min_size=1, max_size=5), + lists( + binary(min_size=1, max_size=5), + min_size=1, + max_size=3, + ), + ), + min_size=0, + max_size=3, + ), + lists(booleans(), min_size=1), + ) + def test_constructed(self, impl, chunk_inputs, chunk_last_bits): + def chunk_constructed(contents): + return ( + tag_encode(form=TagFormConstructed, num=3) + + b"\x80" + + b"".join(BitString(content).encode() for content in contents) + + EOC + ) + chunks = [] + payload_expected = b"" + bit_len_expected = 0 + for chunk_input in chunk_inputs: + if isinstance(chunk_input, binary_type): + chunks.append(BitString(chunk_input).encode()) + payload_expected += chunk_input + bit_len_expected += len(chunk_input) * 8 + else: + chunks.append(chunk_constructed(chunk_input)) + payload = b"".join(chunk_input) + payload_expected += payload + bit_len_expected += len(payload) * 8 + chunk_last = BitString("'%s'B" % "".join( + "1" if bit else "0" for bit in chunk_last_bits + )) + payload_expected += bytes(chunk_last) + bit_len_expected += chunk_last.bit_len + encoded_indefinite = ( + tag_encode(form=TagFormConstructed, num=impl) + + b"\x80" + + b"".join(chunks) + + chunk_last.encode() + + EOC + ) + encoded_definite = ( + tag_encode(form=TagFormConstructed, num=impl) + + len_encode(len(b"".join(chunks) + chunk_last.encode())) + + b"".join(chunks) + + chunk_last.encode() + ) + with assertRaisesRegex(self, DecodeError, "unallowed BER"): + BitString(impl=tag_encode(impl)).decode(encoded_indefinite) + for encoded in (encoded_indefinite, encoded_definite): + obj, tail = BitString(impl=tag_encode(impl)).decode( + encoded, ctx={"bered": True} + ) + self.assertSequenceEqual(tail, b"") + self.assertEqual(obj.bit_len, bit_len_expected) + self.assertSequenceEqual(bytes(obj), payload_expected) + self.assertTrue(obj.bered) + self.assertEqual(len(encoded), obj.tlvlen) + + def test_x690_vector(self): + vector_payload = hexdec("0A3B5F291CD0") + vector = BitString((len(vector_payload) * 8 - 4, vector_payload)) + obj, tail = BitString().decode(hexdec("0307040A3B5F291CD0")) + self.assertSequenceEqual(tail, b"") + self.assertEqual(obj, vector) + obj, tail = BitString().decode( + hexdec("23800303000A3B0305045F291CD00000"), + ctx={"bered": True}, + ) + self.assertSequenceEqual(tail, b"") + self.assertEqual(obj, vector) + @composite def octet_string_values_strategy(draw, do_expl=False): @@ -1449,7 +1543,7 @@ class TestOctetString(CommonMixin, TestCase): repr(obj) pprint(obj) - @given(binary(), binary(), binary(), binary()) + @given(binary(), binary(), binary(min_size=1), binary(min_size=1)) def test_comparison(self, value1, value2, tag1, tag2): for klass in (OctetString, OctetStringInherited): obj1 = klass(value1) @@ -1695,6 +1789,61 @@ class TestOctetString(CommonMixin, TestCase): ) self.assertEqual(obj_decoded.expl_offset, offset) + @given( + integers(min_value=1, max_value=30), + lists( + one_of( + binary(min_size=1, max_size=5), + lists( + binary(min_size=1, max_size=5), + min_size=1, + max_size=3, + ), + ), + min_size=1, + max_size=3, + ), + ) + def test_constructed(self, impl, chunk_inputs): + def chunk_constructed(contents): + return ( + tag_encode(form=TagFormConstructed, num=4) + + b"\x80" + + b"".join(OctetString(content).encode() for content in contents) + + EOC + ) + chunks = [] + payload_expected = b"" + for chunk_input in chunk_inputs: + if isinstance(chunk_input, binary_type): + chunks.append(OctetString(chunk_input).encode()) + payload_expected += chunk_input + else: + chunks.append(chunk_constructed(chunk_input)) + payload = b"".join(chunk_input) + payload_expected += payload + encoded_indefinite = ( + tag_encode(form=TagFormConstructed, num=impl) + + b"\x80" + + b"".join(chunks) + + EOC + ) + encoded_definite = ( + tag_encode(form=TagFormConstructed, num=impl) + + len_encode(len(b"".join(chunks))) + + b"".join(chunks) + ) + with assertRaisesRegex(self, DecodeError, "unallowed BER"): + OctetString(impl=tag_encode(impl)).decode(encoded_indefinite) + for encoded in (encoded_indefinite, encoded_definite): + obj, tail = OctetString(impl=tag_encode(impl)).decode( + encoded, ctx={"bered": True} + ) + self.assertSequenceEqual(tail, b"") + self.assertSequenceEqual(bytes(obj), payload_expected) + self.assertTrue(obj.bered) + self.assertEqual(len(encoded), obj.tlvlen) + @composite def null_values_strategy(draw, do_expl=False): @@ -2238,6 +2387,12 @@ class TestObjectIdentifier(CommonMixin, TestCase): data, ))) + def test_x690_vector(self): + self.assertEqual( + ObjectIdentifier().decode(hexdec("0603883703"))[0], + ObjectIdentifier((2, 999, 3)), + ) + @composite def enumerated_values_strategy(draw, schema=None, do_expl=False): @@ -2553,8 +2708,8 @@ class StringMixin(object): def test_comparison(self, d): value1 = d.draw(text(alphabet=self.text_alphabet())) value2 = d.draw(text(alphabet=self.text_alphabet())) - tag1 = d.draw(binary()) - tag2 = d.draw(binary()) + tag1 = d.draw(binary(min_size=1)) + tag2 = d.draw(binary(min_size=1)) obj1 = self.base_klass(value1) obj2 = self.base_klass(value2) self.assertEqual(obj1 == obj2, value1 == value2) @@ -2802,35 +2957,130 @@ 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 + def test_x690_vector(self): + self.assertEqual( + str(VisibleString().decode(hexdec("1A054A6F6E6573"))[0]), + "Jones", + ) + self.assertEqual( + str(VisibleString().decode( + hexdec("3A0904034A6F6E04026573"), + ctx={"bered": True}, + )[0]), + "Jones", + ) + self.assertEqual( + str(VisibleString().decode( + hexdec("3A8004034A6F6E040265730000"), + ctx={"bered": True}, + )[0]), + "Jones", + ) + -class TestGeneralString(StringMixin, CommonMixin, TestCase): +class TestGeneralString( + UnicodeDecodeErrorMixin, + StringMixin, + CommonMixin, + TestCase, +): base_klass = GeneralString @@ -2916,8 +3166,8 @@ class TimeMixin(object): min_value=self.min_datetime, max_value=self.max_datetime, )) - tag1 = d.draw(binary()) - tag2 = d.draw(binary()) + tag1 = d.draw(binary(min_size=1)) + tag2 = d.draw(binary(min_size=1)) if self.omit_ms: value1 = value1.replace(microsecond=0) value2 = value2.replace(microsecond=0) @@ -3695,6 +3945,26 @@ class TestChoice(CommonMixin, TestCase): with self.assertRaises(TagMismatch): obj.decode(int_encoded) + def test_tag_mismatch_underlying(self): + class SeqOfBoolean(SequenceOf): + schema = Boolean() + + class SeqOfInteger(SequenceOf): + schema = Integer() + + class Wahl(Choice): + schema = ( + ("erste", SeqOfBoolean()), + ) + + int_encoded = SeqOfInteger((Integer(123),)).encode() + bool_encoded = SeqOfBoolean((Boolean(False),)).encode() + obj = Wahl() + obj.decode(bool_encoded) + with self.assertRaises(TagMismatch) as err: + obj.decode(int_encoded) + self.assertEqual(err.exception.decode_path, ("erste", "0")) + @composite def seq_values_strategy(draw, seq_klass, do_expl=False): @@ -3870,7 +4140,7 @@ def sequences_strategy(draw, seq_klass): class SeqMixing(object): def test_invalid_value_type(self): with self.assertRaises(InvalidValueType) as err: - self.base_klass((1, 2, 3)) + self.base_klass(123) repr(err.exception) def test_invalid_value_type_set(self): @@ -4272,6 +4542,16 @@ class TestSequence(SeqMixing, CommonMixin, TestCase): seq[missing] = Boolean() repr(err.exception) + def test_x690_vector(self): + class Seq(Sequence): + schema = ( + ("name", IA5String()), + ("ok", Boolean()), + ) + seq = Seq().decode(hexdec("300A1605536d6974680101FF"))[0] + self.assertEqual(seq["name"], "Smith") + self.assertEqual(seq["ok"], True) + class TestSet(SeqMixing, CommonMixin, TestCase): base_klass = Set @@ -5188,3 +5468,42 @@ class TestStrictDefaultExistence(TestCase): seq.decode(raw) with assertRaisesRegex(self, DecodeError, "DEFAULT value met"): seq.decode(raw, ctx={"strict_default_existence": True}) + + +class TestX690PrefixedType(TestCase): + def runTest(self): + self.assertSequenceEqual( + VisibleString("Jones").encode(), + hexdec("1A054A6F6E6573"), + ) + self.assertSequenceEqual( + VisibleString( + "Jones", + impl=tag_encode(3, klass=TagClassApplication), + ).encode(), + hexdec("43054A6F6E6573"), + ) + self.assertSequenceEqual( + Any( + VisibleString( + "Jones", + impl=tag_encode(3, klass=TagClassApplication), + ), + expl=tag_ctxc(2), + ).encode(), + hexdec("A20743054A6F6E6573"), + ) + self.assertSequenceEqual( + OctetString( + VisibleString( + "Jones", + impl=tag_encode(3, klass=TagClassApplication), + ).encode(), + impl=tag_encode(7, form=TagFormConstructed, klass=TagClassApplication), + ).encode(), + hexdec("670743054A6F6E6573"), + ) + self.assertSequenceEqual( + VisibleString("Jones", impl=tag_ctxp(2)).encode(), + hexdec("82054A6F6E6573"), + )