package io.github.olivierlemasle.tests;

import static io.github.olivierlemasle.ca.CA.createCertificateAuthority;
import static io.github.olivierlemasle.ca.CA.createCsr;
import static io.github.olivierlemasle.ca.CA.dn;
import static io.github.olivierlemasle.ca.CA.loadCertificateAuthority;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import io.github.olivierlemasle.ca.CSR;
import io.github.olivierlemasle.ca.CaException;
import io.github.olivierlemasle.ca.CertificateAuthority;
import io.github.olivierlemasle.ca.DistinguishedName;

import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.cert.X509Certificate;

import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;

public class KeystoreExportTest {
  private static CertificateAuthority ca;
  private static CSR csr;
  private static BigInteger serialNumber;
  private static X509Certificate cert;

  @BeforeClass
  public static void setup() {
    final DistinguishedName caName = dn("CN=CA-Test");
    ca = createCertificateAuthority(caName).build();
    csr = createCsr().generateRequest(dn("CN=Test"));
    serialNumber = ca.generateRandomSerialNumber();
    cert = ca.signCsr(csr)
        .setSerialNumber(serialNumber)
        .sign();
  }

  @After
  public void clean() {
    try {
      Files.delete(Paths.get("test.p12"));
    } catch (final IOException e) {
      fail("Cannot delete keystore");
    }
  }

  @Test
  public void saveToKeystoreFileAndBack() {
    ca.exportPkcs12("test.p12", "password".toCharArray(), "ca");

    final CertificateAuthority ca2 = loadCertificateAuthority("test.p12",
        "password".toCharArray(), "ca");
    assertEquals(ca.getCaCertificate(), ca2.getCaCertificate());

    final X509Certificate cert2 = ca2.signCsr(csr)
        .setSerialNumber(serialNumber)
        .sign();
    assertEquals(cert, cert2);
  }

  @Test
  public void testInvalidKeystorePath() {
    ca.exportPkcs12("test.p12", "password".toCharArray(), "ca");

    try {
      loadCertificateAuthority("invalid", "password".toCharArray(), "ca");
      fail("CaException expected");
    } catch (final CaException expected) {
    }
  }

  @Test
  public void testInvalidKeystorePassword() {
    ca.exportPkcs12("test.p12", "password".toCharArray(), "ca");

    try {
      loadCertificateAuthority("test.p12", "incorrect".toCharArray(), "ca");
      fail("CaException expected");
    } catch (final CaException expected) {
    }
  }

  @Test
  public void testInvalidAlias() {
    ca.exportPkcs12("test.p12", "password".toCharArray(), "ca");

    try {
      loadCertificateAuthority("test.p12", "password".toCharArray(), "incorrect");
      fail("CaException expected");
    } catch (final CaException expected) {
    }
  }

}

@@ -720,16 +720,48 @@
 ificate;
+%0Aimport java.time.ZonedDateTime;
 %0A%0Aimport
@@ -908,16 +908,75 @@
 ity ca;%0A
+  private static ZonedDateTime time = ZonedDateTime.now();%0A
   privat
@@ -1393,32 +1393,89 @@
 r(serialNumber)%0A
+        .setNotBefore(time)%0A        .validDuringYears(1)%0A
         .sign();
@@ -1993,32 +1993,32 @@
 a2.signCsr(csr)%0A
-
         .setSeri
@@ -2032,32 +2032,89 @@
 r(serialNumber)%0A
+        .setNotBefore(time)%0A        .validDuringYears(1)%0A
         .sign();

Fix unit test