]> Cypherpunks.ru repositories - pyderasn.git/blobdiff - tests/test_crl.py
README update and CER/BER mentioning
[pyderasn.git] / tests / test_crl.py
index 317355213365e389c5d17f5bc3a31857df8e3dd2..fcfcb9e379ab92d001dad67ceacc235e5735b0aa 100644 (file)
@@ -1,5 +1,5 @@
 # coding: utf-8
-# PyDERASN -- Python ASN.1 DER codec with abstract structures
+# PyDERASN -- Python ASN.1 DER/CER/BER codec with abstract structures
 # Copyright (C) 2017-2020 Sergey Matveev <stargrave@stargrave.org>
 #
 # This program is free software: you can redistribute it and/or modify
@@ -22,8 +22,11 @@ from time import time
 from unittest import skipIf
 from unittest import TestCase
 
+from six import PY2
+
 from pyderasn import BitString
 from pyderasn import encode_cer
+from pyderasn import file_mmaped
 from pyderasn import Sequence
 from pyderasn import SequenceOf
 from pyderasn import tag_ctxc
@@ -67,10 +70,14 @@ class CertificateList(Sequence):
         ("signatureValue", BitString()),
     )
 
-@skipIf(not exists("revoke.crl"), "CACert's revoke.crl not found")
+
+CRL_PATH = "revoke.crl"
+
+
+@skipIf(not exists(CRL_PATH), "CACert's revoke.crl not found")
 class TestCACert(TestCase):
     def test_cer(self):
-        with open("revoke.crl", "rb") as fd:
+        with open(CRL_PATH, "rb") as fd:
             raw = fd.read()
         print("DER read")
         start = time()
@@ -87,3 +94,39 @@ class TestCACert(TestCase):
         der_raw = crl2.encode()
         print("DER encoded", time() - start)
         self.assertSequenceEqual(der_raw, raw)
+
+    @skipIf(PY2, "Py27 mmap does not implement buffer protocol")
+    def test_mmaped(self):
+        fd = open(CRL_PATH, "rb")
+        start = time()
+        CertificateList().decod(file_mmaped(fd))
+        print("DER decoded", time() - start)
+
+    def test_evgens(self):
+        fd = open(CRL_PATH, "rb")
+        raw = memoryview(fd.read()) if PY2 else file_mmaped(fd)
+        print("CRL opened")
+        evgens_count = 0
+        revoked_certs_count = 0
+        start = time()
+        for decode_path, _, _ in CertificateList().decode_evgen(raw):
+            evgens_count += 1
+            if (
+                    len(decode_path) == 3 and
+                    decode_path[:2] == ("tbsCertList", "revokedCertificates")
+            ):
+                revoked_certs_count += 1
+        print("CRL parsed", time() - start)
+        evgens_upto_count = 0
+        start = time()
+        for decode_path, _, _ in CertificateList().decode_evgen(raw, ctx={
+                "evgen_mode_upto": (
+                    (("tbsCertList", "revokedCertificates", any), True),
+                ),
+        }):
+            evgens_upto_count += 1
+        print("CRL upto parsed", time() - start)
+        self.assertEqual(
+            float(evgens_count - evgens_upto_count) / revoked_certs_count,
+            3,
+        )