2 # PyDERASN -- Python ASN.1 DER/CER/BER codec with abstract structures
3 # Copyright (C) 2017-2020 Sergey Matveev <stargrave@stargrave.org>
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Lesser General Public License as
7 # published by the Free Software Foundation, version 3 of the License.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this program. If not, see
16 # <http://www.gnu.org/licenses/>.
17 """CRL related schemas, just to test the performance with them
20 from os.path import exists
22 from unittest import skipIf
23 from unittest import TestCase
27 from pyderasn import BitString
28 from pyderasn import encode_cer
29 from pyderasn import file_mmaped
30 from pyderasn import Sequence
31 from pyderasn import SequenceOf
32 from pyderasn import tag_ctxc
34 from tests.test_crts import AlgorithmIdentifier
35 from tests.test_crts import CertificateSerialNumber
36 from tests.test_crts import Extensions
37 from tests.test_crts import Name
38 from tests.test_crts import Time
39 from tests.test_crts import Version
42 class RevokedCertificate(Sequence):
44 ("userCertificate", CertificateSerialNumber()),
45 ("revocationDate", Time()),
46 ("crlEntryExtensions", Extensions(optional=True)),
50 class RevokedCertificates(SequenceOf):
51 schema = RevokedCertificate()
54 class TBSCertList(Sequence):
56 ("version", Version(optional=True)),
57 ("signature", AlgorithmIdentifier()),
59 ("thisUpdate", Time()),
60 ("nextUpdate", Time(optional=True)),
61 ("revokedCertificates", RevokedCertificates(optional=True)),
62 ("crlExtensions", Extensions(expl=tag_ctxc(0), optional=True)),
66 class CertificateList(Sequence):
68 ("tbsCertList", TBSCertList()),
69 ("signatureAlgorithm", AlgorithmIdentifier()),
70 ("signatureValue", BitString()),
74 CRL_PATH = "revoke.crl"
77 @skipIf(not exists(CRL_PATH), "CACert's revoke.crl not found")
78 class TestCACert(TestCase):
80 with open(CRL_PATH, "rb") as fd:
84 crl1 = CertificateList().decod(raw)
85 print("DER decoded", time() - start)
87 cer_raw = encode_cer(crl1)
88 print("CER encoded", time() - start)
90 crl2 = CertificateList().decod(cer_raw, ctx={"bered": True})
91 print("CER decoded", time() - start)
92 self.assertEqual(crl2, crl1)
94 der_raw = crl2.encode()
95 print("DER encoded", time() - start)
96 self.assertSequenceEqual(der_raw, raw)
98 @skipIf(PY2, "Py27 mmap does not implement buffer protocol")
99 def test_mmaped(self):
100 fd = open(CRL_PATH, "rb")
102 CertificateList().decod(file_mmaped(fd))
103 print("DER decoded", time() - start)
105 def test_evgens(self):
106 fd = open(CRL_PATH, "rb")
107 raw = memoryview(fd.read()) if PY2 else file_mmaped(fd)
110 revoked_certs_count = 0
112 for decode_path, _, _ in CertificateList().decode_evgen(raw):
115 len(decode_path) == 3 and
116 decode_path[:2] == ("tbsCertList", "revokedCertificates")
118 revoked_certs_count += 1
119 print("CRL parsed", time() - start)
120 evgens_upto_count = 0
122 for decode_path, _, _ in CertificateList().decode_evgen(raw, ctx={
124 (("tbsCertList", "revokedCertificates", any), True),
127 evgens_upto_count += 1
128 print("CRL upto parsed", time() - start)
130 float(evgens_count - evgens_upto_count) / revoked_certs_count,