]> Cypherpunks.ru repositories - pyderasn.git/blob - tests/test_crl.py
317355213365e389c5d17f5bc3a31857df8e3dd2
[pyderasn.git] / tests / test_crl.py
1 # coding: utf-8
2 # PyDERASN -- Python ASN.1 DER codec with abstract structures
3 # Copyright (C) 2017-2020 Sergey Matveev <stargrave@stargrave.org>
4 #
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.
8 #
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.
13 #
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
18 """
19
20 from os.path import exists
21 from time import time
22 from unittest import skipIf
23 from unittest import TestCase
24
25 from pyderasn import BitString
26 from pyderasn import encode_cer
27 from pyderasn import Sequence
28 from pyderasn import SequenceOf
29 from pyderasn import tag_ctxc
30
31 from tests.test_crts import AlgorithmIdentifier
32 from tests.test_crts import CertificateSerialNumber
33 from tests.test_crts import Extensions
34 from tests.test_crts import Name
35 from tests.test_crts import Time
36 from tests.test_crts import Version
37
38
39 class RevokedCertificate(Sequence):
40     schema = (
41         ("userCertificate", CertificateSerialNumber()),
42         ("revocationDate", Time()),
43         ("crlEntryExtensions", Extensions(optional=True)),
44     )
45
46
47 class RevokedCertificates(SequenceOf):
48     schema = RevokedCertificate()
49
50
51 class TBSCertList(Sequence):
52     schema = (
53         ("version", Version(optional=True)),
54         ("signature", AlgorithmIdentifier()),
55         ("issuer", Name()),
56         ("thisUpdate", Time()),
57         ("nextUpdate", Time(optional=True)),
58         ("revokedCertificates", RevokedCertificates(optional=True)),
59         ("crlExtensions", Extensions(expl=tag_ctxc(0), optional=True)),
60     )
61
62
63 class CertificateList(Sequence):
64     schema = (
65         ("tbsCertList", TBSCertList()),
66         ("signatureAlgorithm", AlgorithmIdentifier()),
67         ("signatureValue", BitString()),
68     )
69
70 @skipIf(not exists("revoke.crl"), "CACert's revoke.crl not found")
71 class TestCACert(TestCase):
72     def test_cer(self):
73         with open("revoke.crl", "rb") as fd:
74             raw = fd.read()
75         print("DER read")
76         start = time()
77         crl1 = CertificateList().decod(raw)
78         print("DER decoded", time() - start)
79         start = time()
80         cer_raw = encode_cer(crl1)
81         print("CER encoded", time() - start)
82         start = time()
83         crl2 = CertificateList().decod(cer_raw, ctx={"bered": True})
84         print("CER decoded", time() - start)
85         self.assertEqual(crl2, crl1)
86         start = time()
87         der_raw = crl2.encode()
88         print("DER encoded", time() - start)
89         self.assertSequenceEqual(der_raw, raw)