X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=tests%2Ftest_pyderasn.py;h=412f012657bb51ed8819f0a5112314148eb3767c;hb=12689a9a8c74c290fb59854c178a074026386d57;hp=4edf0608f61f544f9ca5a91d9e9bd9c714e77c39;hpb=6ffa181faaed35d38e9057c473363b5b6e44efd6;p=pyderasn.git diff --git a/tests/test_pyderasn.py b/tests/test_pyderasn.py index 4edf060..412f012 100644 --- a/tests/test_pyderasn.py +++ b/tests/test_pyderasn.py @@ -1,6 +1,6 @@ # coding: utf-8 # PyDERASN -- Python ASN.1 DER/CER/BER codec with abstract structures -# Copyright (C) 2017-2021 Sergey Matveev +# Copyright (C) 2017-2022 Sergey Matveev # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as @@ -32,6 +32,7 @@ from string import whitespace from time import mktime from time import time from unittest import TestCase +from unittest.mock import patch from dateutil.tz import UTC from hypothesis import assume @@ -122,6 +123,7 @@ from pyderasn import UTCTime from pyderasn import UTF8String from pyderasn import VideotexString from pyderasn import VisibleString +import pyderasn max_examples = environ.get("MAX_EXAMPLES") @@ -677,7 +679,7 @@ class TestBoolean(CommonMixin, TestCase): repr(obj) list(obj.pps()) - @given(integers(min_value=2)) + @given(integers(min_value=2, max_value=10)) def test_invalid_len(self, l): with self.assertRaises(InvalidLength): Boolean().decode(b"".join(( @@ -1270,6 +1272,8 @@ def bit_string_values_strategy(draw, schema=None, value_required=False, do_expl= if generation_choice == 2 or draw(booleans()): return draw(binary(max_size=len(schema) // 8)) if generation_choice == 3 or draw(booleans()): + if len(schema) == 0: + return () return tuple(draw(lists(sampled_from([name for name, _ in schema])))) return None value = _value(value_required) @@ -2676,8 +2680,8 @@ def oid_strategy(draw): if first_arc in (0, 1): second_arc = draw(integers(min_value=0, max_value=39)) else: - second_arc = draw(integers(min_value=0)) - other_arcs = draw(lists(integers(min_value=0))) + second_arc = draw(integers(min_value=0, max_value=1 << 63)) + other_arcs = draw(lists(integers(min_value=0, max_value=1 << 63))) return tuple([first_arc, second_arc] + other_arcs) @@ -2908,21 +2912,27 @@ class TestObjectIdentifier(CommonMixin, TestCase): with self.assertRaisesRegex(DecodeError, "unfinished OID"): obj.decode(data) - @given(integers(min_value=0)) + @given(integers(min_value=0, max_value=1 << 63)) def test_invalid_short(self, value): with self.assertRaises(InvalidOID): ObjectIdentifier((value,)) with self.assertRaises(InvalidOID): ObjectIdentifier("%d" % value) - @given(integers(min_value=3), integers(min_value=0)) + @given( + integers(min_value=3, max_value=1 << 63), + integers(min_value=0, max_value=1 << 63), + ) def test_invalid_first_arc(self, first_arc, second_arc): with self.assertRaises(InvalidOID): ObjectIdentifier((first_arc, second_arc)) with self.assertRaises(InvalidOID): ObjectIdentifier("%d.%d" % (first_arc, second_arc)) - @given(integers(min_value=0, max_value=1), integers(min_value=40)) + @given( + integers(min_value=0, max_value=1), + integers(min_value=40, max_value=1 << 63), + ) def test_invalid_second_arc(self, first_arc, second_arc): with self.assertRaises(InvalidOID): ObjectIdentifier((first_arc, second_arc)) @@ -5061,6 +5071,14 @@ class TestUTCTime(TimeMixin, CommonMixin, TestCase): with self.assertRaisesRegex(ValueError, "only naive"): UTCTime(datetime(2000, 1, 1, 1, tzinfo=UTC)) + def test_raises_if_no_dateutil(self): + with patch("pyderasn.tzUTC", new="missing"): + with self.assertRaisesRegex(NotImplementedError, "dateutil"): + UTCTime(datetime.now()).totzdatetime() + + def test_tzinfo_gives_datetime_with_tzutc_tzinfo(self): + self.assertEqual(UTCTime(datetime.now()).totzdatetime().tzinfo, UTC) + @composite def tlv_value_strategy(draw): @@ -5936,7 +5954,7 @@ def sequences_strategy(draw, seq_klass): return seq_outer, expect_outers -class SeqMixing(object): +class SeqMixin(object): def test_invalid_value_type(self): with self.assertRaises(InvalidValueType) as err: self.base_klass(123) @@ -6450,7 +6468,7 @@ class SeqMixing(object): self.assertTrue(decoded.bered) -class TestSequence(SeqMixing, CommonMixin, TestCase): +class TestSequence(SeqMixin, CommonMixin, TestCase): base_klass = Sequence @given( @@ -6496,7 +6514,7 @@ class TestSequence(SeqMixing, CommonMixin, TestCase): self.assertEqual(seq["ok"], True) -class TestSet(SeqMixing, CommonMixin, TestCase): +class TestSet(SeqMixin, CommonMixin, TestCase): base_klass = Set @settings(max_examples=LONG_TEST_MAX_EXAMPLES) @@ -6611,7 +6629,7 @@ def seqof_values_strategy(draw, schema=None, do_expl=False): ) -class SeqOfMixing(object): +class SeqOfMixin(object): def test_invalid_value_type(self): with self.assertRaises(InvalidValueType) as err: self.base_klass(123) @@ -6713,7 +6731,7 @@ 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(False)] * d.draw(integers(max_value=bound_min - 1)) + value = [Boolean(False)] * d.draw(integers(min_value=0, max_value=bound_min - 1)) with self.assertRaises(BoundsError) as err: SeqOf(value=value, bounds=(bound_min, bound_max)) repr(err.exception) @@ -7081,7 +7099,7 @@ class SeqOfMixing(object): self.assertTrue(decoded.bered) -class TestSequenceOf(SeqOfMixing, CommonMixin, TestCase): +class TestSequenceOf(SeqOfMixin, CommonMixin, TestCase): class SeqOf(SequenceOf): schema = "whatever" base_klass = SeqOf @@ -7165,7 +7183,7 @@ class TestSequenceOf(SeqOfMixing, CommonMixin, TestCase): self.assertFalse(seqof.ready) -class TestSetOf(SeqOfMixing, CommonMixin, TestCase): +class TestSetOf(SeqOfMixin, CommonMixin, TestCase): class SeqOf(SetOf): schema = "whatever" base_klass = SeqOf @@ -7369,10 +7387,10 @@ class TestGoMarshalVectors(TestCase): seq["erste"] = PrintableString("test") self.assertSequenceEqual(seq.encode(), hexdec("3006130474657374")) # Asterisk is actually not allowable - PrintableString._allowable_chars |= set(b"*") + pyderasn.PRINTABLE_ALLOWABLE_CHARS |= set(b"*") seq["erste"] = PrintableString("test*") self.assertSequenceEqual(seq.encode(), hexdec("30071305746573742a")) - PrintableString._allowable_chars -= set(b"*") + pyderasn.PRINTABLE_ALLOWABLE_CHARS -= set(b"*") class Seq(Sequence): schema = ( @@ -7418,7 +7436,11 @@ class TestPP(TestCase): def test_oid_printing(self, d): oids = { str(ObjectIdentifier(k)): v * 2 - for k, v in d.draw(dictionaries(oid_strategy(), text_letters())).items() + for k, v in d.draw(dictionaries( + oid_strategy(), + text_letters(), + min_size=1, + )).items() } chosen = d.draw(sampled_from(sorted(oids))) chosen_id = oids[chosen]