// Path: src/test/java/org/fbsks/certservices/BaseTest.java
// @RunWith(SpringRunner.class)
// @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
// @Transactional
// public abstract class BaseTest {
// 
// }
// 
// Path: src/main/java/org/fbsks/certservices/model/PKI.java
// @Entity
// public class PKI extends AbstractPersistable<Long> {
// 
// 	private static final long serialVersionUID = -140537791349423216L;
// 
// 	@OneToMany
// 	private List<CertificateAuthority> cas; 
// 	
// 	@JsonView(PKISummary.class)
// 	private String name;
// 	
// 	protected PKI() {}
// 	
// 	public PKI(String name, List<CertificateAuthority> cas) {
// 		this.name = name;
// 		this.cas = cas;
// 	}
// 	
// 	public PKI(String name, CertificateAuthority ca) {
// 		this.name = name;
// 		
// 		this.cas = new ArrayList<CertificateAuthority>();
// 		this.cas.add(ca);
// 	}
// 
// 	public List<CertificateAuthority> getCas() {
// 		return cas;
// 	}
// 
// 	public String getName() {
// 		return name;
// 	}
// 	
// 	public void setCas(List<CertificateAuthority> cas) {
// 		this.cas = cas;
// 	}
// 
// 	public void setName(String name) {
// 		this.name = name;
// 	}
// }
// 
// Path: src/main/java/org/fbsks/certservices/utils/GeneralUtils.java
// public class GeneralUtils {
// 	
// 	public static final String CN = "CN=";
// 	public static final String EMPTY_STRING = "";
// 	public static final String COMMA = ",";
// 	
// 	public static String getParsedCertificateCommonName(X509CertificateHolder certificate) {
// 		String caCN = certificate.getSubject().toString();
// 		caCN = caCN.substring(0, caCN.indexOf(COMMA));
// 		caCN = caCN.replace(CN, EMPTY_STRING);
// 
// 		return caCN;
// 	}
// 	
// 	public static String getParsedCRLX500Principal(X509CRL crl) {
// 		String crlName = crl.getIssuerX500Principal().toString();
// 		crlName = crlName.replace(CN, EMPTY_STRING);
// 		
// 		return crlName;
// 	}
// }


import static org.junit.Assert.assertEquals;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.cert.CRLException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509CRL;
import java.util.ArrayList;
import java.util.List;
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.cms.CMSException;
import org.bouncycastle.cms.CMSSignedData;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Base64;
import org.fbsks.certservices.BaseTest;
import org.fbsks.certservices.model.PKI;
import org.fbsks.certservices.utils.GeneralUtils;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
	
	@Test
	public void shouldGenerateP12() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, NoSuchProviderException {
		ResponseEntity<byte[]> p12response = pkiController.newPKICertificate("testPKI", "testSubject", "1234");
		
		KeyStore keyStore = KeyStore.getInstance("PKCS12", BouncyCastleProvider.PROVIDER_NAME);
		
		keyStore.load(new ByteArrayInputStream(Base64.decode(p12response.getBody())), "1234".toCharArray());
		assertEquals(1, keyStore.size());
	}
	
	@Test
	public void shouldGetCRLs() throws CRLException, CertificateException {		
		List<PKI> pkiList = pkiController.listPKIs();
		String caName = pkiList.get(0).getCas().get(0).getName();
		ResponseEntity<byte[]> crls = pkiController.getCRLs(caName);
		
		X509CRL crl = (X509CRL) CertificateFactory.getInstance("X.509").generateCRL(new ByteArrayInputStream(Base64.decode(crls.getBody())));
		assertEquals(caName, GeneralUtils.getParsedCRLX500Principal(crl));
	}
	
	@Test
	public void shouldAccessAIA() throws CRLException, IOException, CMSException {
		ResponseEntity<byte[]> aia = pkiController.getAIA("testPKI");
		
		CMSSignedData p7b = new CMSSignedData(Base64.decode(aia.getBody()));
		
		@SuppressWarnings("unchecked")
		List<X509CertificateHolder> certificates = new ArrayList<X509CertificateHolder>(p7b.getCertificates().getMatches(null));
		
// Path: src/test/java/org/fbsks/certservices/BaseTest.java
// @RunWith(SpringRunner.class)
// @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
// @Transactional
// public abstract class BaseTest {
// 
// }
// 
// Path: src/main/java/org/fbsks/certservices/model/PKI.java
// @Entity
// public class PKI extends AbstractPersistable<Long> {
// 
// 	private static final long serialVersionUID = -140537791349423216L;
// 
// 	@OneToMany
// 	private List<CertificateAuthority> cas; 
// 	
// 	@JsonView(PKISummary.class)
// 	private String name;
// 	
// 	protected PKI() {}
// 	
// 	public PKI(String name, List<CertificateAuthority> cas) {
// 		this.name = name;
// 		this.cas = cas;
// 	}
// 	
// 	public PKI(String name, CertificateAuthority ca) {
// 		this.name = name;
// 		
// 		this.cas = new ArrayList<CertificateAuthority>();
// 		this.cas.add(ca);
// 	}
// 
// 	public List<CertificateAuthority> getCas() {
// 		return cas;
// 	}
// 
// 	public String getName() {
// 		return name;
// 	}
// 	
// 	public void setCas(List<CertificateAuthority> cas) {
// 		this.cas = cas;
// 	}
// 
// 	public void setName(String name) {
// 		this.name = name;
// 	}
// }
// 
// Path: src/main/java/org/fbsks/certservices/utils/GeneralUtils.java
// public class GeneralUtils {
// 	
// 	public static final String CN = "CN=";
// 	public static final String EMPTY_STRING = "";
// 	public static final String COMMA = ",";
// 	
// 	public static String getParsedCertificateCommonName(X509CertificateHolder certificate) {
// 		String caCN = certificate.getSubject().toString();
// 		caCN = caCN.substring(0, caCN.indexOf(COMMA));
// 		caCN = caCN.replace(CN, EMPTY_STRING);
// 
// 		return caCN;
// 	}
// 	
// 	public static String getParsedCRLX500Principal(X509CRL crl) {
// 		String crlName = crl.getIssuerX500Principal().toString();
// 		crlName = crlName.replace(CN, EMPTY_STRING);
// 		
// 		return crlName;
// 	}
// }


// Path: src/test/java/org/fbsks/certservices/controller/rest/PKIControllerTest.java
import static org.junit.Assert.assertEquals;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.cert.CRLException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509CRL;
import java.util.ArrayList;
import java.util.List;
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.cms.CMSException;
import org.bouncycastle.cms.CMSSignedData;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Base64;
import org.fbsks.certservices.BaseTest;
import org.fbsks.certservices.model.PKI;
import org.fbsks.certservices.utils.GeneralUtils;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;

	
	@Test
	public void shouldGenerateP12() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, NoSuchProviderException {
		ResponseEntity<byte[]> p12response = pkiController.newPKICertificate("testPKI", "testSubject", "1234");
		
		KeyStore keyStore = KeyStore.getInstance("PKCS12", BouncyCastleProvider.PROVIDER_NAME);
		
		keyStore.load(new ByteArrayInputStream(Base64.decode(p12response.getBody())), "1234".toCharArray());
		assertEquals(1, keyStore.size());
	}
	
	@Test
	public void shouldGetCRLs() throws CRLException, CertificateException {		
		List<PKI> pkiList = pkiController.listPKIs();
		String caName = pkiList.get(0).getCas().get(0).getName();
		ResponseEntity<byte[]> crls = pkiController.getCRLs(caName);
		
		X509CRL crl = (X509CRL) CertificateFactory.getInstance("X.509").generateCRL(new ByteArrayInputStream(Base64.decode(crls.getBody())));
		assertEquals(caName, GeneralUtils.getParsedCRLX500Principal(crl));
	}
	
	@Test
	public void shouldAccessAIA() throws CRLException, IOException, CMSException {
		ResponseEntity<byte[]> aia = pkiController.getAIA("testPKI");
		
		CMSSignedData p7b = new CMSSignedData(Base64.decode(aia.getBody()));
		
		@SuppressWarnings("unchecked")
		List<X509CertificateHolder> certificates = new ArrayList<X509CertificateHolder>(p7b.getCertificates().getMatches(null));
		
		List<PKI> pkiList = pkiController.listPKIs();