diff options
author | ohemorange <ebportnoy@gmail.com> | 2019-11-27 02:25:41 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-27 02:25:41 +0300 |
commit | d2b65b47f2e2968df62e0feea5ebf28bfdd3e4b2 (patch) | |
tree | 9c686c839091476d244237c75e58e90d818ef5a7 /acme/tests/test_util.py | |
parent | b624172f6827b35214bc3a2df076df0bc524cba2 (diff) |
Refactor tests out of packaged module for acme plugin (#7600)
* Move acme tests to tests/ directory outside of acme module
* Fix call to messages_test in client_test
* Move test_util.py and testdata/ into tests/
* Update manifest to package tests
* Exclude pycache and .py[cod]
Diffstat (limited to 'acme/tests/test_util.py')
-rw-r--r-- | acme/tests/test_util.py | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/acme/tests/test_util.py b/acme/tests/test_util.py new file mode 100644 index 000000000..6737bff4e --- /dev/null +++ b/acme/tests/test_util.py @@ -0,0 +1,68 @@ +"""Test utilities. + +.. warning:: This module is not part of the public API. + +""" +import os +import pkg_resources + +from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives import serialization +import josepy as jose +from OpenSSL import crypto + + +def load_vector(*names): + """Load contents of a test vector.""" + # luckily, resource_string opens file in binary mode + return pkg_resources.resource_string( + __name__, os.path.join('testdata', *names)) + + +def _guess_loader(filename, loader_pem, loader_der): + _, ext = os.path.splitext(filename) + if ext.lower() == '.pem': + return loader_pem + elif ext.lower() == '.der': + return loader_der + else: # pragma: no cover + raise ValueError("Loader could not be recognized based on extension") + + +def load_cert(*names): + """Load certificate.""" + loader = _guess_loader( + names[-1], crypto.FILETYPE_PEM, crypto.FILETYPE_ASN1) + return crypto.load_certificate(loader, load_vector(*names)) + + +def load_comparable_cert(*names): + """Load ComparableX509 cert.""" + return jose.ComparableX509(load_cert(*names)) + + +def load_csr(*names): + """Load certificate request.""" + loader = _guess_loader( + names[-1], crypto.FILETYPE_PEM, crypto.FILETYPE_ASN1) + return crypto.load_certificate_request(loader, load_vector(*names)) + + +def load_comparable_csr(*names): + """Load ComparableX509 certificate request.""" + return jose.ComparableX509(load_csr(*names)) + + +def load_rsa_private_key(*names): + """Load RSA private key.""" + loader = _guess_loader(names[-1], serialization.load_pem_private_key, + serialization.load_der_private_key) + return jose.ComparableRSAKey(loader( + load_vector(*names), password=None, backend=default_backend())) + + +def load_pyopenssl_private_key(*names): + """Load pyOpenSSL private key.""" + loader = _guess_loader( + names[-1], crypto.FILETYPE_PEM, crypto.FILETYPE_ASN1) + return crypto.load_privatekey(loader, load_vector(*names)) |