From 969ad2a4b2cbd9d56e4aec6aa40c59eace008825 Mon Sep 17 00:00:00 2001 From: RedProkofiev Date: Tue, 12 Dec 2023 13:21:04 +0000 Subject: [PATCH] Unit test for get_subject_components --- ssm/crypto.py | 3 +-- test/test_crypto.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/ssm/crypto.py b/ssm/crypto.py index 4d872403..8e1eef2f 100644 --- a/ssm/crypto.py +++ b/ssm/crypto.py @@ -297,8 +297,7 @@ def verify_cert_path(certpath, capath, check_crls=True): return verify_cert(certstring, capath, check_crls) def get_subject_components(subject_x509name): - - # Undergoing testing + """RegEx to strip a keyname into a separated list.""" subject = "".join("/{:s}={:s}".format(name.decode(), value.decode()) for name, value in subject_x509name.get_components()) diff --git a/test/test_crypto.py b/test/test_crypto.py index 0dcb4001..e011dd80 100644 --- a/test/test_crypto.py +++ b/test/test_crypto.py @@ -3,6 +3,7 @@ import unittest import logging import os +import OpenSSL from subprocess import call, Popen, PIPE import tempfile import quopri @@ -15,6 +16,7 @@ decrypt, \ verify, \ verify_cert, \ + get_subject_components, \ CryptoException logging.basicConfig() @@ -172,6 +174,22 @@ def test_verify(self): self.assertRaises(CryptoException, verify, 'Bibbly bobbly', None, False) self.assertRaises(CryptoException, verify, None, 'not a path', False) + def test_get_subject_components(self): + ''' + Check that the correct DN is extracted from the certstring. + ''' + # Still a valid certificate + with open(TEST_CERT_FILE, 'r') as test_cert: + cert_string = test_cert.read() + + subject_x509name = OpenSSL.crypto.load_certificate( + OpenSSL.crypto.FILETYPE_PEM, + cert_string + ).get_subject() + + if not get_subject_components(subject_x509name) == TEST_CERT_DN: + self.fail("Didn't retrieve correct DN from cert.") + def test_get_certificate_subject(self): ''' Check that the correct DN is extracted from the cert.