doc/news.rst | 2 ++ pyderasn.py | 20 ++++++++++++++++++-- tests/test_pyderasn.py | 10 +++++++++- diff --git a/doc/news.rst b/doc/news.rst index da701741eaf202d2f3b1fc5e461ddc1144d75c1586fcafcbfba146ed3d60aa58..dd2b6815bcfbc90ed968c1328ddb6c49dd67e5964b49dd7c31343757dc301204 100644 --- a/doc/news.rst +++ b/doc/news.rst @@ -10,6 +10,8 @@ objects now * Copies made previously with ``.copy()`` lacked ``.defined`` field, now they are not * All objects are friendly to ``pickle`` libraries +* ``PrintableString`` has ``allow_asterisk`` and ``allow_ampersand`` + property .. _release5.6: diff --git a/pyderasn.py b/pyderasn.py index e96924da3601b138e495ef3c27eec328f5487f1e5075b0a6eb6f38b4cd6ac175..a8cc49091a4d4776451ff0bde48ed07e141655f4b9ffe5c662172b577fcc6504 100755 --- a/pyderasn.py +++ b/pyderasn.py @@ -3991,6 +3991,10 @@ Its value is properly sanitized: see X.680 41.4 table 10. >>> PrintableString().allowable_chars frozenset([' ', "'", ..., 'z']) + >>> obj = PrintableString("foo*bar", allow_asterisk=True) + PrintableString PrintableString foo*bar + >>> obj.allow_asterisk, obj.allow_ampersand + (True, False) """ __slots__ = () tag_default = tag_encode(19) @@ -4026,6 +4030,18 @@ super(PrintableString, self).__init__( value, bounds, impl, expl, default, optional, _decoded, ) + @property + def allow_asterisk(self): + """Is asterisk character allowed? + """ + return self._asterisk <= self._allowable_chars + + @property + def allow_ampersand(self): + """Is ampersand character allowed? + """ + return self._ampersand <= self._allowable_chars + def _value_sanitize(self, value): value = super(PrintableString, self)._value_sanitize(value) if not frozenset(value) <= self._allowable_chars: @@ -4061,8 +4077,8 @@ impl=self.tag if impl is None else impl, expl=self._expl if expl is None else expl, default=self.default if default is None else default, optional=self.optional if optional is None else optional, - allow_asterisk=self._asterisk <= self._allowable_chars, - allow_ampersand=self._ampersand <= self._allowable_chars, + allow_asterisk=self.allow_asterisk, + allow_ampersand=self.allow_ampersand, ) diff --git a/tests/test_pyderasn.py b/tests/test_pyderasn.py index e52a4c648b9cd3d9d01e6f7cf2dddac7880b6510db18c7d86872f019768d577e..81585daed1266fe9546b95c3fb4c5c048b6ed5f4e93f3a22297299dfa39872e1 100644 --- a/tests/test_pyderasn.py +++ b/tests/test_pyderasn.py @@ -3540,14 +3540,22 @@ ("*", {"allow_asterisk": True}), ("&", {"allow_ampersand": True}), ("&*", {"allow_asterisk": True, "allow_ampersand": True}), ): - s = "hello invalid " + c + s = "hello invalid" + obj = self.base_klass(s) + for prop in kwargs.keys(): + self.assertFalse(getattr(obj, prop)) + s += c with assertRaisesRegex(self, DecodeError, "non-printable"): self.base_klass(s) self.base_klass(s, **kwargs) klass = self.base_klass(**kwargs) obj = klass(s) + for prop in kwargs.keys(): + self.assertTrue(getattr(obj, prop)) obj = copy(obj) obj(s) + for prop in kwargs.keys(): + self.assertTrue(getattr(obj, prop)) class TestTeletexString(