]> Cypherpunks.ru repositories - pyderasn.git/blobdiff - tests/test_pyderasn.py
Fix double encoded values decoding in SET
[pyderasn.git] / tests / test_pyderasn.py
index b7ee9a9da9571a1f29b2a8feac0ae9ac9166a11a..3161102997daf57449b26c20194b1081613f8b65 100644 (file)
@@ -20,6 +20,7 @@ from copy import deepcopy
 from datetime import datetime
 from datetime import timedelta
 from importlib import import_module
+from os import environ
 from random import random
 from string import ascii_letters
 from string import digits
@@ -126,8 +127,10 @@ from pyderasn import VideotexString
 from pyderasn import VisibleString
 
 
+max_examples = environ.get("MAX_EXAMPLES")
 settings.register_profile("local", settings(
     deadline=5000,
+    **({"max_examples": int(max_examples)} if max_examples else {})
 ))
 settings.load_profile("local")
 LONG_TEST_MAX_EXAMPLES = settings().max_examples * 4
@@ -343,7 +346,7 @@ class CommonMixin(object):
         self.assertSequenceEqual(obj.impl, impl_tag)
         self.assertFalse(obj.expled)
 
-    @given(binary())
+    @given(binary(min_size=1))
     def test_expl_inherited(self, expl_tag):
         class Inherited(self.base_klass):
             expl = expl_tag
@@ -414,7 +417,7 @@ class TestBoolean(CommonMixin, TestCase):
         list(obj.pps())
         pprint(obj, big_blobs=True, with_decode_path=True)
 
-    @given(booleans(), booleans(), binary(), binary())
+    @given(booleans(), booleans(), binary(min_size=1), binary(min_size=1))
     def test_comparison(self, value1, value2, tag1, tag2):
         for klass in (Boolean, BooleanInherited):
             obj1 = klass(value1)
@@ -833,7 +836,7 @@ class TestInteger(CommonMixin, TestCase):
         pprint(obj, big_blobs=True, with_decode_path=True)
         hash(obj)
 
-    @given(integers(), integers(), binary(), binary())
+    @given(integers(), integers(), binary(min_size=1), binary(min_size=1))
     def test_comparison(self, value1, value2, tag1, tag2):
         for klass in (Integer, IntegerInherited):
             obj1 = klass(value1)
@@ -2273,7 +2276,7 @@ class TestNull(CommonMixin, TestCase):
         list(obj.pps())
         pprint(obj, big_blobs=True, with_decode_path=True)
 
-    @given(binary(), binary())
+    @given(binary(min_size=1), binary(min_size=1))
     def test_comparison(self, tag1, tag2):
         for klass in (Null, NullInherited):
             obj1 = klass(impl=tag1)
@@ -2503,7 +2506,7 @@ class TestObjectIdentifier(CommonMixin, TestCase):
         pprint(obj, big_blobs=True, with_decode_path=True)
         hash(obj)
 
-    @given(oid_strategy(), oid_strategy(), binary(), binary())
+    @given(oid_strategy(), oid_strategy(), binary(min_size=1), binary(min_size=1))
     def test_comparison(self, value1, value2, tag1, tag2):
         for klass in (ObjectIdentifier, ObjectIdentifierInherited):
             obj1 = klass(value1)
@@ -2973,7 +2976,7 @@ class TestEnumerated(CommonMixin, TestCase):
         list(obj.pps())
         pprint(obj, big_blobs=True, with_decode_path=True)
 
-    @given(integers(), integers(), binary(), binary())
+    @given(integers(), integers(), binary(min_size=1), binary(min_size=1))
     def test_comparison(self, value1, value2, tag1, tag2):
         class E(Enumerated):
             schema = (
@@ -4305,6 +4308,14 @@ class TestGeneralizedTime(TimeMixin, CommonMixin, TestCase):
                 b"200001020304+5Z",
                 b"20000102030405.+6Z",
                 b"20000102030405.-6Z",
+                b"_2000102030405Z",
+                b"2000_102030405Z",
+                b"200001_2030405Z",
+                b"20000102_30405Z",
+                b"2000010203_405Z",
+                b"200001020304_5Z",
+                b"20000102030405._6Z",
+                b"20000102030405.6_Z",
                 b" 2000102030405Z",
                 b"2000 102030405Z",
                 b"200001 2030405Z",
@@ -4406,6 +4417,13 @@ class TestUTCTime(TimeMixin, CommonMixin, TestCase):
                 b"000102+30405Z",
                 b"00010203+405Z",
                 b"0001020304+5Z",
+                b"_10102030405Z",
+                b"00_102030405Z",
+                b"0001_2030405Z",
+                b"000102_30405Z",
+                b"00010203_405Z",
+                b"0001020304_5Z",
+                b"00010203045_Z",
                 b" 10102030405Z",
                 b"00 102030405Z",
                 b"0001 2030405Z",
@@ -4715,7 +4733,7 @@ class TestAny(CommonMixin, TestCase):
             pprint(obj, big_blobs=True, with_decode_path=True)
             self.assertSequenceEqual(obj.encode(), integer_encoded)
 
-    @given(binary(), binary())
+    @given(binary(min_size=1), binary(min_size=1))
     def test_comparison(self, value1, value2):
         for klass in (Any, AnyInherited):
             obj1 = klass(value1)
@@ -5836,7 +5854,7 @@ class SeqMixing(object):
             min_size=1,
         )).items())
         tags = [tag_encode(tag) for tag in d.draw(sets(
-            integers(min_value=0),
+            integers(min_value=1),
             min_size=len(_schema),
             max_size=len(_schema),
         ))]
@@ -5874,7 +5892,7 @@ class SeqMixing(object):
     def test_missing_from_spec(self, d):
         names = list(d.draw(sets(text_letters(), min_size=2)))
         tags = [tag_encode(tag) for tag in d.draw(sets(
-            integers(min_value=0),
+            integers(min_value=1),
             min_size=len(names),
             max_size=len(names),
         ))]
@@ -6031,6 +6049,22 @@ class TestSet(SeqMixing, CommonMixin, TestCase):
                 tags,
             )
 
+    def test_same_value_twice(self):
+        class Seq(Set):
+            schema = (
+                ("bool", Boolean()),
+                ("int", Integer()),
+            )
+
+        encoded = b"".join((
+            Integer(123).encode(),
+            Integer(234).encode(),
+            Boolean(True).encode(),
+        ))
+        encoded = Seq.tag_default + len_encode(len(encoded)) + encoded
+        with self.assertRaises(TagMismatch):
+            Seq().decod(encoded, ctx={"allow_unordered_set": True})
+
 
 @composite
 def seqof_values_strategy(draw, schema=None, do_expl=False):
@@ -6093,7 +6127,7 @@ class SeqOfMixing(object):
         with assertRaisesRegex(self, ValueError, "schema must be specified"):
             self.base_klass.__mro__[1]()
 
-    @given(booleans(), booleans(), binary(), binary())
+    @given(booleans(), booleans(), binary(min_size=1), binary(min_size=1))
     def test_comparison(self, value1, value2, tag1, tag2):
         class SeqOf(self.base_klass):
             schema = Boolean()