From 11fce9a8706a34bb29701f8e00c41da76b71606f Mon Sep 17 00:00:00 2001 From: Joona Hoikkala Date: Fri, 20 Dec 2019 20:19:00 +0200 Subject: [PATCH] Add a crypto_util test and mark few lines as no cover --- certbot/certbot/_internal/ocsp.py | 4 ++-- certbot/certbot/crypto_util.py | 2 ++ certbot/tests/crypto_util_test.py | 18 ++++++++++++++++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/certbot/certbot/_internal/ocsp.py b/certbot/certbot/_internal/ocsp.py index c8a614d6d..49a50060c 100644 --- a/certbot/certbot/_internal/ocsp.py +++ b/certbot/certbot/_internal/ocsp.py @@ -112,7 +112,7 @@ class RevocationChecker(object): "-verify_other", chain_path, "-trust_other", "-header"] + self.host_args(host) - if response_file: + if response_file: # pragma: no cover cmd += ["-respout", response_file] logger.debug("Querying OCSP for %s", cert_path) logger.debug(" ".join(cmd)) @@ -176,7 +176,7 @@ def _check_ocsp_cryptography(cert_path, chain_path, url, response_file=None): logger.info("OCSP check failed for %s (HTTP status: %d)", cert_path, response.status_code) return False - if response_file: + if response_file: # pragma: no cover with open(response_file, 'wb') as fh: fh.write(response.content) diff --git a/certbot/certbot/crypto_util.py b/certbot/certbot/crypto_util.py index fe46242c4..e6c063eed 100644 --- a/certbot/certbot/crypto_util.py +++ b/certbot/certbot/crypto_util.py @@ -225,7 +225,9 @@ def verify_renewable_cert(renewable_cert): def load_cert(cert_path): """Reads the certificate PEM file and returns a cryptography.x509 object + :param str cert_path: Path to the certificate + :rtype `cryptography.x509`: :returns: x509 certificate object """ diff --git a/certbot/tests/crypto_util_test.py b/certbot/tests/crypto_util_test.py index 1d642ae9e..4b8b4934d 100644 --- a/certbot/tests/crypto_util_test.py +++ b/certbot/tests/crypto_util_test.py @@ -25,6 +25,7 @@ P256_KEY = test_util.load_vector('nistp256_key.pem') P256_CERT_PATH = test_util.vector_path('cert-nosans_nistp256.pem') P256_CERT = test_util.load_vector('cert-nosans_nistp256.pem') + class InitSaveKeyTest(test_util.TempDirTestCase): """Tests for certbot.crypto_util.init_save_key.""" def setUp(self): @@ -369,8 +370,10 @@ class Sha256sumTest(unittest.TestCase): """Tests for certbot.crypto_util.notAfter""" def test_sha256sum(self): from certbot.crypto_util import sha256sum - self.assertEqual(sha256sum(CERT_PATH), - '914ffed8daf9e2c99d90ac95c77d54f32cbd556672facac380f0c063498df84e') + self.assertEqual( + sha256sum(CERT_PATH), + '914ffed8daf9e2c99d90ac95c77d54f32cbd556672facac380f0c063498df84e' + ) class CertAndChainFromFullchainTest(unittest.TestCase): @@ -388,5 +391,16 @@ class CertAndChainFromFullchainTest(unittest.TestCase): self.assertEqual(chain_out, chain_pem) +class CertFingerprintTest(unittest.TestCase): + """Tests for certbot.crypto_util.cert_sha1_fingerprint""" + + def test_cert_sha1_fingerprint(self): + from certbot.crypto_util import cert_sha1_fingerprint + self.assertEqual( + cert_sha1_fingerprint(CERT_PATH), + b'\t\xf8\xce\x01E\r(\x84g\xc32j\xc0E~5\x199\xc7.' + ) + + if __name__ == '__main__': unittest.main() # pragma: no cover