import os
import shutil

from certauth.certauth import main, CertificateAuthority
import tempfile

def setup_module():
    global TEST_CA_DIR
    TEST_CA_DIR = tempfile.mkdtemp()

    global TEST_CA_ROOT
    TEST_CA_ROOT = os.path.join(TEST_CA_DIR, 'certauth_test_ca.pem')

def teardown_module():
    shutil.rmtree(TEST_CA_DIR)
    assert not os.path.isdir(TEST_CA_DIR)
    assert not os.path.isfile(TEST_CA_ROOT)

def test_create_root():
    ret = main([TEST_CA_ROOT, '-c', 'Test Root Cert'])
    assert ret == 0

def test_create_host_cert():
    ret = main([TEST_CA_ROOT, '-d', TEST_CA_DIR, '-n', 'example.com'])
    assert ret == 0
    certfile = os.path.join(TEST_CA_DIR, 'example.com.pem')
    assert os.path.isfile(certfile)

def test_create_wildcard_host_cert_force_overwrite():
    ret = main([TEST_CA_ROOT, '-d', TEST_CA_DIR, '--hostname', 'example.com', '-w', '-f'])
    assert ret == 0
    certfile = os.path.join(TEST_CA_DIR, 'example.com.pem')
    assert os.path.isfile(certfile)

def test_explicit_wildcard():
    ca = CertificateAuthority(TEST_CA_ROOT, TEST_CA_DIR, 'Test CA')
    filename = ca.get_wildcard_cert('test.example.proxy')
    certfile = os.path.join(TEST_CA_DIR, 'example.proxy.pem')
    assert filename == certfile
    assert os.path.isfile(certfile)
    os.remove(certfile)

def test_create_already_exists():
    ret = main([TEST_CA_ROOT, '-d', TEST_CA_DIR, '-n', 'example.com', '-w'])
    assert ret == 1
    certfile = os.path.join(TEST_CA_DIR, 'example.com.pem')
    assert os.path.isfile(certfile)
    # remove now
    os.remove(certfile)

def test_create_root_already_exists():
    ret = main([TEST_CA_ROOT])
    # not created, already exists
    assert ret == 1
    # remove now
    os.remove(TEST_CA_ROOT)

def test_create_root_subdir():
    # create a new cert in a subdirectory
    subdir = os.path.join(TEST_CA_DIR, 'subdir')

    ca_file = os.path.join(subdir, 'certauth_test_ca.pem')

    ca = CertificateAuthority(ca_file, subdir, 'Test CA',
                             cert_start=-60 * 60,
                             cert_duration=60 * 60 * 24 * 3)

    assert os.path.isdir(subdir)
    assert os.path.isfile(ca_file)

    buff = ca.get_root_PKCS12()
    assert len(buff) > 0

@@ -90,16 +90,59 @@
 tempfile
+%0Afrom OpenSSL import crypto%0Aimport datetime
 %0A%0Adef se
@@ -2259,8 +2259,642 @@
 ff) %3E 0%0A
+%0A    expected_not_before = datetime.datetime.utcnow() - datetime.timedelta(seconds=60 * 60)%0A    expected_not_after = expected_not_before + datetime.timedelta(seconds=60 * 60 * 24 * 3)%0A%0A    cert = crypto.load_pkcs12(buff).get_certificate()%0A%0A    actual_not_before = datetime.datetime.strptime(%0A            cert.get_notBefore().decode('ascii'), '%25Y%25m%25d%25H%25M%25SZ')%0A    actual_not_after = datetime.datetime.strptime(%0A            cert.get_notAfter().decode('ascii'), '%25Y%25m%25d%25H%25M%25SZ')%0A%0A    assert abs((actual_not_before - expected_not_before).total_seconds()) %3C 10%0A    assert abs((actual_not_after - expected_not_after).total_seconds()) %3C 10%0A%0A

check that cert values notBefore and notAfter are set correctly from cert_start and cert_duration parameters