test_fakeopensslclasses.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. '''
  2. Unit tests for the FakeOpenSSL classes
  3. '''
  4. import os
  5. import subprocess
  6. import sys
  7. import pytest
  8. MODULE_PATH = os.path.realpath(os.path.join(__file__, os.pardir, os.pardir, 'library'))
  9. sys.path.insert(1, MODULE_PATH)
  10. # pylint: disable=import-error,wrong-import-position,missing-docstring
  11. # pylint: disable=invalid-name,redefined-outer-name
  12. from openshift_cert_expiry import FakeOpenSSLCertificate # noqa: E402
  13. @pytest.fixture(scope='module')
  14. def fake_valid_cert(valid_cert):
  15. cmd = ['openssl', 'x509', '-in', str(valid_cert['cert_file']), '-text',
  16. '-nameopt', 'oneline']
  17. cert = subprocess.check_output(cmd)
  18. return FakeOpenSSLCertificate(cert.decode('utf8'))
  19. def test_not_after(valid_cert, fake_valid_cert):
  20. ''' Validate value returned back from get_notAfter() '''
  21. real_cert = valid_cert['cert']
  22. # Internal representation of pyOpenSSL is bytes, while FakeOpenSSLCertificate
  23. # is text, so decode the result from pyOpenSSL prior to comparing
  24. assert real_cert.get_notAfter().decode('utf8') == fake_valid_cert.get_notAfter()
  25. def test_serial(valid_cert, fake_valid_cert):
  26. ''' Validate value returned back form get_serialnumber() '''
  27. real_cert = valid_cert['cert']
  28. assert real_cert.get_serial_number() == fake_valid_cert.get_serial_number()
  29. def test_get_subject(valid_cert, fake_valid_cert):
  30. ''' Validate the certificate subject '''
  31. # Gather the subject components and create a list of colon separated strings.
  32. # Since the internal representation of pyOpenSSL uses bytes, we need to decode
  33. # the results before comparing.
  34. c_subjects = valid_cert['cert'].get_subject().get_components()
  35. c_subj = ', '.join(['{}:{}'.format(x.decode('utf8'), y.decode('utf8')) for x, y in c_subjects])
  36. f_subjects = fake_valid_cert.get_subject().get_components()
  37. f_subj = ', '.join(['{}:{}'.format(x, y) for x, y in f_subjects])
  38. assert c_subj == f_subj
  39. def get_san_extension(cert):
  40. # Internal representation of pyOpenSSL is bytes, while FakeOpenSSLCertificate
  41. # is text, so we need to set the value to search for accordingly.
  42. if isinstance(cert, FakeOpenSSLCertificate):
  43. san_short_name = 'subjectAltName'
  44. else:
  45. san_short_name = b'subjectAltName'
  46. for i in range(cert.get_extension_count()):
  47. ext = cert.get_extension(i)
  48. if ext.get_short_name() == san_short_name:
  49. # return the string representation to compare the actual SAN
  50. # values instead of the data types
  51. return str(ext)
  52. return None
  53. def test_subject_alt_names(valid_cert, fake_valid_cert):
  54. real_cert = valid_cert['cert']
  55. san = get_san_extension(real_cert)
  56. f_san = get_san_extension(fake_valid_cert)
  57. assert san == f_san
  58. # If there are either dns or ip sans defined, verify common_name present
  59. if valid_cert['ip'] or valid_cert['dns']:
  60. assert 'DNS:' + valid_cert['common_name'] in f_san
  61. # Verify all ip sans are present
  62. for ip in valid_cert['ip']:
  63. assert 'IP Address:' + ip in f_san
  64. # Verify all dns sans are present
  65. for name in valid_cert['dns']:
  66. assert 'DNS:' + name in f_san