X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=tests%2Ftest_crts.py;h=d8785294f1f6c6c0554b0105a0bab522819548e8;hb=521a4868199657f49e0b20973dab53730b93fd54;hp=9c6b76c87a3fbfdf97bb6e56f9dadd3c8e514968;hpb=78833daa9b9637827bfb570135e699b5871aefd3;p=pyderasn.git diff --git a/tests/test_crts.py b/tests/test_crts.py index 9c6b76c..d878529 100644 --- a/tests/test_crts.py +++ b/tests/test_crts.py @@ -1,11 +1,10 @@ # coding: utf-8 # PyDERASN -- Python ASN.1 DER codec with abstract structures -# Copyright (C) 2017-2018 Sergey Matveev +# Copyright (C) 2017-2020 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 -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. +# published by the Free Software Foundation, version 3 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -16,9 +15,14 @@ # License along with this program. If not, see # . +from copy import copy from datetime import datetime from unittest import TestCase +from six.moves.cPickle import dumps as pickle_dumps +from six.moves.cPickle import HIGHEST_PROTOCOL as pickle_proto +from six.moves.cPickle import loads as pickle_loads + from pyderasn import Any from pyderasn import BitString from pyderasn import Boolean @@ -91,8 +95,8 @@ class AttributeValue(Any): class OrganizationName(Choice): schema = ( - ('printableString', PrintableString()), - ('teletexString', TeletexString()), + ("printableString", PrintableString()), + ("teletexString", TeletexString()), ) @@ -206,8 +210,7 @@ class TestGoSelfSignedVector(TestCase): "ba3ca12568fdc6c7b4511cd40a7f659980402df2b998bb9a4a8cbeb34c0f0a78c", "f8d91ede14a5ed76bf116fe360aafa8821490435", ))) - crt, tail = Certificate().decode(raw) - self.assertSequenceEqual(tail, b"") + crt = Certificate().decod(raw) tbs = crt["tbsCertificate"] self.assertEqual(tbs["version"], 0) self.assertFalse(tbs["version"].decoded) @@ -221,12 +224,12 @@ class TestGoSelfSignedVector(TestCase): expect.encode(), ) assert_raw_equals(tbs["serialNumber"], Integer(10143011886257155224)) - algo_id = AlgorithmIdentifier() - algo_id["algorithm"] = ObjectIdentifier("1.2.840.113549.1.1.5") - algo_id["parameters"] = Any(Null()) + algo_id = AlgorithmIdentifier(( + ("algorithm", ObjectIdentifier("1.2.840.113549.1.1.5")), + ("parameters", Any(Null())), + )) self.assertEqual(tbs["signature"], algo_id) assert_raw_equals(tbs["signature"], algo_id) - issuer = Name() rdnSeq = RDNSequence() for oid, klass, text in ( ("2.5.4.6", PrintableString, "XX"), @@ -236,22 +239,25 @@ class TestGoSelfSignedVector(TestCase): ("2.5.4.3", PrintableString, "false.example.com"), ("1.2.840.113549.1.9.1", IA5String, "false@example.com"), ): - attr = AttributeTypeAndValue() - attr["type"] = AttributeType(oid) - attr["value"] = AttributeValue(klass(text)) - rdn = RelativeDistinguishedName() - rdn.append(attr) - rdnSeq.append(rdn) - issuer["rdnSequence"] = rdnSeq + rdnSeq.append( + RelativeDistinguishedName(( + AttributeTypeAndValue(( + ("type", AttributeType(oid)), + ("value", AttributeValue(klass(text))), + )), + )) + ) + issuer = Name(("rdnSequence", rdnSeq)) self.assertEqual(tbs["issuer"], issuer) assert_raw_equals(tbs["issuer"], issuer) - validity = Validity() - validity["notBefore"] = Time( - ("utcTime", UTCTime(datetime(2009, 10, 8, 0, 25, 53))) - ) - validity["notAfter"] = Time( - ("utcTime", UTCTime(datetime(2010, 10, 8, 0, 25, 53))) - ) + validity = Validity(( + ("notBefore", Time( + ("utcTime", UTCTime(datetime(2009, 10, 8, 0, 25, 53))) + )), + ("notAfter", Time( + ("utcTime", UTCTime(datetime(2010, 10, 8, 0, 25, 53))) + )), + )) self.assertEqual(tbs["validity"], validity) assert_raw_equals(tbs["validity"], validity) self.assertEqual(tbs["subject"], issuer) @@ -278,13 +284,15 @@ class TestGoSelfSignedVector(TestCase): self.assertSequenceEqual(crt.encode(), raw) pprint(crt) repr(crt) + pickle_loads(pickle_dumps(crt, pickle_proto)) tbs = TBSCertificate() tbs["serialNumber"] = CertificateSerialNumber(10143011886257155224) - sign_algo_id = AlgorithmIdentifier() - sign_algo_id["algorithm"] = ObjectIdentifier("1.2.840.113549.1.1.5") - sign_algo_id["parameters"] = Any(Null()) + sign_algo_id = AlgorithmIdentifier(( + ("algorithm", ObjectIdentifier("1.2.840.113549.1.1.5")), + ("parameters", Any(Null())), + )) tbs["signature"] = sign_algo_id rdnSeq = RDNSequence() @@ -296,24 +304,31 @@ class TestGoSelfSignedVector(TestCase): ("2.5.4.3", PrintableString, "false.example.com"), ("1.2.840.113549.1.9.1", IA5String, "false@example.com"), ): - attr = AttributeTypeAndValue() - attr["type"] = AttributeType(oid) - attr["value"] = AttributeValue(klass(text)) - rdn = RelativeDistinguishedName() - rdn.append(attr) - rdnSeq.append(rdn) + rdnSeq.append( + RelativeDistinguishedName(( + AttributeTypeAndValue(( + ("type", AttributeType(oid)), + ("value", AttributeValue(klass(text))), + )), + )) + ) issuer = Name() issuer["rdnSequence"] = rdnSeq tbs["issuer"] = issuer tbs["subject"] = issuer - validity = Validity() - validity["notBefore"] = Time(("utcTime", UTCTime(datetime(2009, 10, 8, 0, 25, 53)))) - validity["notAfter"] = Time(("utcTime", UTCTime(datetime(2010, 10, 8, 0, 25, 53)))) + validity = Validity(( + ("notBefore", Time( + ("utcTime", UTCTime(datetime(2009, 10, 8, 0, 25, 53)),), + )), + ("notAfter", Time( + ("utcTime", UTCTime(datetime(2010, 10, 8, 0, 25, 53)),), + )), + )) tbs["validity"] = validity spki = SubjectPublicKeyInfo() - spki_algo_id = sign_algo_id.copy() + spki_algo_id = copy(sign_algo_id) spki_algo_id["algorithm"] = ObjectIdentifier("1.2.840.113549.1.1.1") spki["algorithm"] = spki_algo_id spki["subjectPublicKey"] = BitString(hexdec("".join(( @@ -387,8 +402,8 @@ class TestGoPayPalVector(TestCase): "07ba44cce54a2d723f9847f626dc054605076321ab469b9c78d5545b3d0c1ec86", "48cb55023826fdbb8221c439607a8bb", ))) - crt, tail = Certificate().decode(raw) - self.assertSequenceEqual(tail, b"") + crt = Certificate().decod(raw) self.assertSequenceEqual(crt.encode(), raw) pprint(crt) repr(crt) + pickle_loads(pickle_dumps(crt, pickle_proto))