Skip to content

Commit

Permalink
Unit test for get_subject_components
Browse files Browse the repository at this point in the history
  • Loading branch information
RedProkofiev committed Dec 12, 2023
1 parent 1f5eea2 commit 969ad2a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 1 addition & 2 deletions ssm/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
18 changes: 18 additions & 0 deletions test/test_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import unittest
import logging
import os
import OpenSSL
from subprocess import call, Popen, PIPE
import tempfile
import quopri
Expand All @@ -15,6 +16,7 @@
decrypt, \
verify, \
verify_cert, \
get_subject_components, \
CryptoException

logging.basicConfig()
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 969ad2a

Please sign in to comment.