Changeset - a7af49f9e2c3
[Not reviewed]
0 1 0
Branko Majic (branko) - 4 years ago 2020-07-13 17:03:07
branko@majic.rs
GC-37: Parametrise a couple of storage test functions to cover ECDSA keys as well.
1 file changed with 19 insertions and 9 deletions:
0 comments (0 inline, 0 general)
tests/test_storage.py
Show inline comments
 
@@ -25,12 +25,14 @@ import cryptography
 

	
 
import gimmecert.commands
 
import gimmecert.crypto
 
import gimmecert.storage
 
import gimmecert.utils
 

	
 
import pytest
 

	
 

	
 
def test_initialise_storage(tmpdir):
 
    tmpdir.chdir()
 

	
 
    gimmecert.storage.initialise_storage(tmpdir.strpath)
 

	
 
@@ -101,23 +103,27 @@ def test_is_initialised_returns_true_if_directory_is_initialised(tmpdir):
 
def test_is_initialised_returns_false_if_directory_is_not_initialised(tmpdir):
 
    tmpdir.chdir()
 

	
 
    assert gimmecert.storage.is_initialised(tmpdir.strpath) is False
 

	
 

	
 
def test_read_ca_hierarchy_returns_list_of_ca_private_key_and_certificate_pairs_for_single_ca(tmpdir):
 
@pytest.mark.parametrize("key_specification, private_key_instance_type", [
 
    [("rsa", 1024), cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKey],
 
    [("ecdsa", cryptography.hazmat.primitives.asymmetric.ec.SECP192R1), cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey],
 
])
 
def test_read_ca_hierarchy_returns_list_of_ca_private_key_and_certificate_pairs_for_single_ca(tmpdir, key_specification, private_key_instance_type):
 
    tmpdir.chdir()
 
    gimmecert.commands.init(io.StringIO(), io.StringIO(), tmpdir.strpath, 'My Project', 1, ("rsa", 2048))
 
    gimmecert.commands.init(io.StringIO(), io.StringIO(), tmpdir.strpath, 'My Project', 1, key_specification)
 

	
 
    ca_hierarchy = gimmecert.storage.read_ca_hierarchy(tmpdir.join('.gimmecert', 'ca').strpath)
 

	
 
    assert len(ca_hierarchy) == 1
 

	
 
    private_key, certificate = ca_hierarchy[0]
 

	
 
    assert isinstance(private_key, cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKey)
 
    assert isinstance(private_key, private_key_instance_type)
 
    assert isinstance(certificate, cryptography.x509.Certificate)
 

	
 

	
 
def test_read_private_key_returns_private_key(tmpdir):
 
    private_key_path = tmpdir.join('private.key.pem').strpath
 
    private_key = gimmecert.crypto.KeyGenerator('rsa', 2048)()
 
@@ -141,35 +147,39 @@ def test_read_certificate_returns_certificate(tmpdir):
 
    my_certificate = gimmecert.storage.read_certificate(certificate_path)
 

	
 
    assert isinstance(my_certificate, cryptography.x509.Certificate)
 
    assert my_certificate == certificate
 

	
 

	
 
def test_read_ca_hierarchy_returns_list_of_ca_private_key_and_certificate_pairs_in_hierarchy_order_for_multiple_cas(tmpdir):
 
@pytest.mark.parametrize("key_specification, private_key_instance_type", [
 
    [("rsa", 1024), cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKey],
 
    [("ecdsa", cryptography.hazmat.primitives.asymmetric.ec.SECP192R1), cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey],
 
])
 
def test_read_ca_hierarchy_returns_list_of_ca_private_key_and_certificate_pairs_in_hierarchy_order_for_multiple_cas(tmpdir, key_specification, private_key_instance_type):
 
    tmpdir.chdir()
 
    gimmecert.commands.init(io.StringIO(), io.StringIO(), tmpdir.strpath, 'My Project', 4, ("rsa", 2048))
 
    gimmecert.commands.init(io.StringIO(), io.StringIO(), tmpdir.strpath, 'My Project', 4, key_specification)
 

	
 
    ca_hierarchy = gimmecert.storage.read_ca_hierarchy(tmpdir.join('.gimmecert', 'ca').strpath)
 

	
 
    assert len(ca_hierarchy) == 4
 

	
 
    private_key_1, certificate_1 = ca_hierarchy[0]
 
    private_key_2, certificate_2 = ca_hierarchy[1]
 
    private_key_3, certificate_3 = ca_hierarchy[2]
 
    private_key_4, certificate_4 = ca_hierarchy[3]
 

	
 
    assert isinstance(private_key_1, cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKey)
 
    assert isinstance(private_key_1, private_key_instance_type)
 
    assert isinstance(certificate_1, cryptography.x509.Certificate)
 
    assert certificate_1.subject == gimmecert.crypto.get_dn("My Project Level 1 CA")
 
    assert isinstance(private_key_2, cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKey)
 
    assert isinstance(private_key_2, private_key_instance_type)
 
    assert isinstance(certificate_2, cryptography.x509.Certificate)
 
    assert certificate_2.subject == gimmecert.crypto.get_dn("My Project Level 2 CA")
 
    assert isinstance(private_key_3, cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKey)
 
    assert isinstance(private_key_3, private_key_instance_type)
 
    assert isinstance(certificate_3, cryptography.x509.Certificate)
 
    assert certificate_3.subject == gimmecert.crypto.get_dn("My Project Level 3 CA")
 
    assert isinstance(private_key_4, cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKey)
 
    assert isinstance(private_key_4, private_key_instance_type)
 
    assert isinstance(certificate_4, cryptography.x509.Certificate)
 
    assert certificate_4.subject == gimmecert.crypto.get_dn("My Project Level 4 CA")
 

	
 

	
 
def test_write_csr(tmpdir):
 
    csr_file = tmpdir.join('test.csr.pem')
0 comments (0 inline, 0 general)