2022-07-05 18:59:55 -07:00
|
|
|
import os
|
|
|
|
import unittest
|
|
|
|
import json
|
|
|
|
import jc.parsers.x509_cert
|
|
|
|
|
|
|
|
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
|
|
|
|
|
|
|
class MyTests(unittest.TestCase):
|
|
|
|
|
2022-09-23 14:02:27 -07:00
|
|
|
# input
|
|
|
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/x509-ca-cert.der'), 'rb') as f:
|
|
|
|
x509_ca_cert = f.read()
|
2022-07-05 18:59:55 -07:00
|
|
|
|
2022-09-23 14:02:27 -07:00
|
|
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/x509-cert-and-key.pem'), 'r', encoding='utf-8') as f:
|
|
|
|
x509_cert_and_key_pem = f.read()
|
2022-07-05 18:59:55 -07:00
|
|
|
|
2022-09-23 14:02:27 -07:00
|
|
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/x509-letsencrypt.pem'), 'r', encoding='utf-8') as f:
|
|
|
|
x509_letsencrypt = f.read()
|
2022-07-05 18:59:55 -07:00
|
|
|
|
2022-09-23 14:02:27 -07:00
|
|
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/x509-multi-cert.pem'), 'r', encoding='utf-8') as f:
|
|
|
|
x509_multi_cert = f.read()
|
2022-07-05 18:59:55 -07:00
|
|
|
|
2022-12-28 21:59:38 -08:00
|
|
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/x509-string-serialnumber.der'), 'rb') as f:
|
|
|
|
x509_string_serialnumber = f.read()
|
|
|
|
|
2022-09-23 14:02:27 -07:00
|
|
|
# output
|
|
|
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/x509-ca-cert.json'), 'r', encoding='utf-8') as f:
|
|
|
|
x509_ca_cert_json = json.loads(f.read())
|
2022-07-05 18:59:55 -07:00
|
|
|
|
2022-09-23 14:02:27 -07:00
|
|
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/x509-cert-and-key.json'), 'r', encoding='utf-8') as f:
|
|
|
|
x509_cert_and_key_pem_json = json.loads(f.read())
|
2022-07-05 18:59:55 -07:00
|
|
|
|
2022-09-23 14:02:27 -07:00
|
|
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/x509-letsencrypt.json'), 'r', encoding='utf-8') as f:
|
|
|
|
x509_letsencrypt_json = json.loads(f.read())
|
2022-07-05 18:59:55 -07:00
|
|
|
|
2022-09-23 14:02:27 -07:00
|
|
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/x509-multi-cert.json'), 'r', encoding='utf-8') as f:
|
|
|
|
x509_multi_cert_json = json.loads(f.read())
|
2022-07-05 18:59:55 -07:00
|
|
|
|
2022-12-28 21:59:38 -08:00
|
|
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/x509-string-serialnumber.json'), 'r', encoding='utf-8') as f:
|
|
|
|
x509_string_serialnumber_json = json.loads(f.read())
|
|
|
|
|
2022-07-05 18:59:55 -07:00
|
|
|
|
|
|
|
def test_x509_cert_nodata(self):
|
|
|
|
"""
|
|
|
|
Test 'x509_cert' with no data
|
|
|
|
"""
|
|
|
|
self.assertEqual(jc.parsers.x509_cert.parse('', quiet=True), [])
|
|
|
|
|
|
|
|
def test_x509_ca_cert(self):
|
|
|
|
"""
|
|
|
|
Test 'cat x509-ca-cert.der' (CA cert in DER format)
|
|
|
|
"""
|
|
|
|
self.assertEqual(jc.parsers.x509_cert.parse(self.x509_ca_cert, quiet=True), self.x509_ca_cert_json)
|
|
|
|
|
|
|
|
def test_x509_cert_and_key(self):
|
|
|
|
"""
|
|
|
|
Test 'cat x509-cert-and-key.pem' (combo cert and key file in PEM format)
|
|
|
|
"""
|
|
|
|
self.assertEqual(jc.parsers.x509_cert.parse(self.x509_cert_and_key_pem, quiet=True), self.x509_cert_and_key_pem_json)
|
|
|
|
|
|
|
|
def test_x509_letsencrypt(self):
|
|
|
|
"""
|
|
|
|
Test 'cat x509-letsencrypt.pem' (letsencrypt cert in PEM format)
|
|
|
|
"""
|
|
|
|
self.assertEqual(jc.parsers.x509_cert.parse(self.x509_letsencrypt, quiet=True), self.x509_letsencrypt_json)
|
|
|
|
|
|
|
|
def test_x509_multi_cert(self):
|
|
|
|
"""
|
|
|
|
Test 'cat x509-multi-cert.pem' (PEM file with multiple certificates)
|
|
|
|
"""
|
2022-12-28 21:59:38 -08:00
|
|
|
self.assertEqual(jc.parsers.x509_cert.parse(self.x509_multi_cert, quiet=True), self.x509_multi_cert_json)
|
|
|
|
|
|
|
|
def test_x509_string_serialnumber(self):
|
|
|
|
"""
|
|
|
|
Test 'cat x509-string-serialnumber.der' (DER file with string serial numbers)
|
|
|
|
"""
|
|
|
|
self.assertEqual(jc.parsers.x509_cert.parse(self.x509_multi_cert, quiet=True), self.x509_multi_cert_json)
|
2022-07-05 18:59:55 -07:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|