Fixed formatting of unit tests

revert-70aa11f8
Sameera Wickramasekara 7 years ago
parent b5facd468a
commit 32ee88852c

@ -46,7 +46,6 @@ import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
import sun.misc.BASE64Encoder; import sun.misc.BASE64Encoder;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -66,17 +65,9 @@ public class CertificateManagementServiceImplTests extends BaseDeviceManagementC
@Test(description = "This test case tests initialization of CertificateManagementServiceImpl instance") @Test(description = "This test case tests initialization of CertificateManagementServiceImpl instance")
public void testGetInstance() { public void testGetInstance() {
try { CertificateManagementServiceImpl instance = CertificateManagementServiceImpl.getInstance();
CertificateManagementServiceImpl instance = CertificateManagementServiceImpl.getInstance(); Assert.assertNotNull(instance);
Assert.assertNotNull(instance); log.info("getInstance Test Successful");
log.info("getInstance Test Successful");
} catch (NullPointerException e) {
log.error("Error while initializing CertificateManagementService", e);
Assert.fail();
}
} }
@BeforeClass @BeforeClass
@ -85,510 +76,262 @@ public class CertificateManagementServiceImplTests extends BaseDeviceManagementC
managementService = CertificateManagementServiceImpl.getInstance(); managementService = CertificateManagementServiceImpl.getInstance();
//set Bouncycastle as a provider for testing //set Bouncycastle as a provider for testing
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
} }
@Test(description = "This test case tests retrieval of CA Certificate from the keystore") @Test(description = "This test case tests retrieval of CA Certificate from the keystore")
public void testGetCACertificate() { public void testGetCACertificate() throws KeystoreException {
try { CertificateManagementServiceImpl instance = CertificateManagementServiceImpl.getInstance();
CertificateManagementServiceImpl instance = CertificateManagementServiceImpl.getInstance(); Certificate caCertificate = instance.getCACertificate();
Certificate caCertificate = instance.getCACertificate(); Assert.assertNotNull(caCertificate);
Assert.assertNotNull(caCertificate); Assert.assertEquals(caCertificate.getType(), CertificateManagementConstants.X_509);
Assert.assertEquals(caCertificate.getType(), CertificateManagementConstants.X_509); log.info("GetCACertificate Test Successful");
log.info("GetCACertificate Test Successful");
} catch (KeystoreException e) {
String msg = "Error while getting the CA Certificate";
log.error(msg, e);
Assert.fail(msg, e);
}
} }
@Test(description = "This test case tests retrieval of RA Certificate from the keystore") @Test(description = "This test case tests retrieval of RA Certificate from the keystore")
public void testGetRACertificate() { public void testGetRACertificate() throws KeystoreException {
try { Certificate raCertificate = managementService.getRACertificate();
Certificate raCertificate = managementService.getRACertificate(); Assert.assertNotNull(raCertificate);
Assert.assertNotNull(raCertificate); Assert.assertEquals(raCertificate.getType(), CertificateManagementConstants.X_509);
Assert.assertEquals(raCertificate.getType(), CertificateManagementConstants.X_509); log.info("GetRACertificate Test Successful");
log.info("GetRACertificate Test Successful");
} catch (KeystoreException e) {
String msg = "Error while getting the RA Certificate";
log.error(msg, e);
Assert.fail(msg, e);
}
} }
@Test(description = "This test case test generation of root certificates") @Test(description = "This test case test generation of root certificates")
public void testGetRootCertificate() { public void testGetRootCertificate() throws IOException, KeystoreException {
File caCert = new File(CA_CERT_PEM); File caCert = new File(CA_CERT_PEM);
File raCert = new File(RA_CERT_PEM); File raCert = new File(RA_CERT_PEM);
//read file to byte arrays
try { byte[] caBytes = FileUtils.readFileToByteArray(caCert);
//read file to byte arrays byte[] raBytes = FileUtils.readFileToByteArray(raCert);
byte[] caBytes = FileUtils.readFileToByteArray(caCert); List<X509Certificate> rootCertificates = managementService.getRootCertificates(caBytes, raBytes);
byte[] raBytes = FileUtils.readFileToByteArray(raCert); Assert.assertNotNull(rootCertificates);
Assert.assertEquals(rootCertificates.get(0).getType(), CertificateManagementConstants.X_509);
List<X509Certificate> rootCertificates = managementService.getRootCertificates(caBytes, raBytes); Assert.assertEquals(rootCertificates.get(1).getType(), CertificateManagementConstants.X_509);
Assert.assertNotNull(rootCertificates); log.info("GetRootCertificate Test Successful");
Assert.assertEquals(rootCertificates.get(0).getType(), CertificateManagementConstants.X_509);
Assert.assertEquals(rootCertificates.get(1).getType(), CertificateManagementConstants.X_509);
log.info("GetRootCertificate Test Successful");
} catch (IOException e) {
String msg = "Error reading byte streams";
log.error(msg, e);
Assert.fail(msg, e);
} catch (KeystoreException e) {
String msg = "Error retrieving root certificates";
log.error(msg, e);
Assert.fail(msg, e);
}
} }
@Test(description = "This test case tests generation of X509Certificate") @Test(description = "This test case tests generation of X509Certificate")
public void testGenerateX509Certificate() { public void testGenerateX509Certificate() throws KeystoreException {
try { X509Certificate x509Certificate = managementService.generateX509Certificate();
Assert.assertNotNull(x509Certificate);
X509Certificate x509Certificate = managementService.generateX509Certificate(); Assert.assertEquals(x509Certificate.getType(), CertificateManagementConstants.X_509);
Assert.assertNotNull(x509Certificate); log.info("GenerateX509Certificate Test Successful");
Assert.assertEquals(x509Certificate.getType(), CertificateManagementConstants.X_509);
log.info("GenerateX509Certificate Test Successful");
} catch (KeystoreException e) {
String msg = "Error while generating X509 Certificate";
log.error(msg, e);
Assert.fail(msg, e);
}
} }
@Test(description = "This test case tests retrieving SCEP CA Certificate") @Test(description = "This test case tests retrieving SCEP CA Certificate")
public void testGetCACertSCEP() { public void testGetCACertSCEP() throws KeystoreException {
try { SCEPResponse caCertSCEP = managementService.getCACertSCEP();
Assert.assertNotNull(caCertSCEP);
SCEPResponse caCertSCEP = managementService.getCACertSCEP(); Assert.assertEquals(caCertSCEP.getResultCriteria(), CAStatus.CA_RA_CERT_RECEIVED);
Assert.assertNotNull(caCertSCEP); log.info("GetCACertSCEP Test Successful");
Assert.assertEquals(caCertSCEP.getResultCriteria(), CAStatus.CA_RA_CERT_RECEIVED);
log.info("GetCACertSCEP Test Successful");
} catch (KeystoreException e) {
String msg = "Error while Retrieving CA Certificate";
log.error(msg, e);
Assert.fail(msg, e);
}
} }
@Test @Test
public void testGetCACapsSCEP() { public void testGetCACapsSCEP() {
byte[] caCapsSCEP = managementService.getCACapsSCEP(); byte[] caCapsSCEP = managementService.getCACapsSCEP();
Assert.assertNotNull(caCapsSCEP); Assert.assertNotNull(caCapsSCEP);
Assert.assertEquals(caCapsSCEP, CertificateManagementConstants.POST_BODY_CA_CAPS.getBytes()); Assert.assertEquals(caCapsSCEP, CertificateManagementConstants.POST_BODY_CA_CAPS.getBytes());
log.info("GetCACapsSCEP Test Successful"); log.info("GetCACapsSCEP Test Successful");
} }
@Test(description = "This test case tests generation of a X509Certificate from a CSR") @Test(description = "This test case tests generation of a X509Certificate from a CSR")
public void testGenerateCertificateFromCSR() { public void testGenerateCertificateFromCSR() throws KeystoreException, IOException {
CSRGenerator csrGeneration = new CSRGenerator(); CSRGenerator csrGeneration = new CSRGenerator();
KeyStoreReader keyStoreReader = new KeyStoreReader(); KeyStoreReader keyStoreReader = new KeyStoreReader();
// Generate key pair // Generate key pair
KeyPair keyPair = csrGeneration.generateKeyPair("RSA", 1024); KeyPair keyPair = csrGeneration.generateKeyPair("RSA", 1024);
byte[] csrData = csrGeneration.generateCSR("SHA256WithRSA", keyPair); byte[] csrData = csrGeneration.generateCSR("SHA256WithRSA", keyPair);
PKCS10CertificationRequest certificationRequest; PKCS10CertificationRequest certificationRequest;
PrivateKey privateKeyCA = keyStoreReader.getCAPrivateKey();
try { X509Certificate certCA = (X509Certificate) keyStoreReader.getCACertificate();
PrivateKey privateKeyCA = keyStoreReader.getCAPrivateKey(); certificationRequest = new PKCS10CertificationRequest(csrData);
X509Certificate certCA = (X509Certificate) keyStoreReader.getCACertificate(); X509Certificate x509Certificate = managementService.generateCertificateFromCSR(privateKeyCA,
certificationRequest = new PKCS10CertificationRequest(csrData); certificationRequest, certCA.getIssuerX500Principal().getName());
X509Certificate x509Certificate = managementService.generateCertificateFromCSR(privateKeyCA, Assert.assertNotNull(x509Certificate);
certificationRequest, certCA.getIssuerX500Principal().getName()); Assert.assertEquals(x509Certificate.getType(), CertificateManagementConstants.X_509);
log.info("GenerateCertificateFromCSR Test Successful");
Assert.assertNotNull(x509Certificate);
Assert.assertEquals(x509Certificate.getType(), CertificateManagementConstants.X_509);
log.info("GenerateCertificateFromCSR Test Successful");
} catch (KeystoreException e) {
String msg = "Error while reading Certificates from the keystore";
log.error(msg, e);
Assert.fail(msg, e);
} catch (IOException e) {
String msg = "Error while reading byte streams";
log.error(msg, e);
Assert.fail(msg, e);
}
} }
@Test(description = "This test case tests retrieval of a Certificate from the keystore from the Serial Number") @Test(description = "This test case tests retrieval of a Certificate from the keystore from the Serial Number")
public void testGetCertificateBySerial() { public void testGetCertificateBySerial() throws KeystoreException, DeviceManagementException {
X509Certificate x509Certificate = null; X509Certificate x509Certificate = null;
try { //generate and save a certificate
//generate and save a certificate x509Certificate = managementService.generateX509Certificate();
x509Certificate = managementService.generateX509Certificate(); //initialize DeviceConfigurationManager
//initialize DeviceConfigurationManager DeviceConfigurationManager.getInstance().initConfig();
DeviceConfigurationManager.getInstance().initConfig(); CertificateResponse certificateBySerial = managementService.getCertificateBySerial(x509Certificate.getSerialNumber().toString());
CertificateResponse certificateBySerial = managementService.getCertificateBySerial(x509Certificate.getSerialNumber().toString()); Assert.assertNotNull(certificateBySerial);
Assert.assertEquals(certificateBySerial.getSerialNumber(), x509Certificate.getSerialNumber().toString());
Assert.assertNotNull(certificateBySerial); log.info("GetCertificateBySerial Test Successful");
Assert.assertEquals(certificateBySerial.getSerialNumber(), x509Certificate.getSerialNumber().toString());
log.info("GetCertificateBySerial Test Successful");
} catch (KeystoreException e) {
String msg = "Error while receiving the certificate";
log.error(msg, e);
Assert.fail(msg, e);
} catch (DeviceManagementException e) {
String msg = "Error while initilizing DeviceConfigurationManager";
log.error(msg, e);
}
} }
@Test(description = "This test case tests retrieval of a Certificate from the keystore from the Alias") @Test(description = "This test case tests retrieval of a Certificate from the keystore from the Alias")
public void testGetCertificateByAlias() { public void testGetCertificateByAlias() throws KeystoreException, DeviceManagementException {
X509Certificate x509Certificate = null; X509Certificate x509Certificate = null;
try { //generate and save a certificate
//generate and save a certificate x509Certificate = managementService.generateX509Certificate();
x509Certificate = managementService.generateX509Certificate(); //initialize DeviceConfigurationManager
DeviceConfigurationManager.getInstance().initConfig();
//initialize DeviceConfigurationManager Certificate certificateByAlias = managementService.getCertificateByAlias(x509Certificate.getSerialNumber().toString());
DeviceConfigurationManager.getInstance().initConfig(); Assert.assertNotNull(certificateByAlias);
Certificate certificateByAlias = managementService.getCertificateByAlias(x509Certificate.getSerialNumber().toString()); Assert.assertEquals(certificateByAlias.getType(), CertificateManagementConstants.X_509);
log.info("GetCertificateByAlias Test Successful");
Assert.assertNotNull(certificateByAlias);
Assert.assertEquals(certificateByAlias.getType(), CertificateManagementConstants.X_509);
log.info("GetCertificateByAlias Test Successful");
} catch (KeystoreException e) {
String msg = "Error while receiving the certificate";
log.error(msg, e);
Assert.fail(msg, e);
} catch (DeviceManagementException e) {
String msg = "Error while initilizing DeviceConfigurationManager";
log.error(msg, e);
}
} }
@Test(description = "This test case tests Signature verification of a Certificate against the keystore") @Test(description = "This test case tests Signature verification of a Certificate against the keystore")
public void testVerifySignature() { public void testVerifySignature() throws KeystoreException, CertificateEncodingException, CMSException, IOException {
BASE64Encoder encoder = new BASE64Encoder(); BASE64Encoder encoder = new BASE64Encoder();
//generate and save a certificate in the keystore
try { X509Certificate x509Certificate = managementService.generateX509Certificate();
//generate and save a certificate in the keystore //Generate CMSdata
X509Certificate x509Certificate = managementService.generateX509Certificate(); CMSSignedDataGenerator generator = new CMSSignedDataGenerator();
List<X509Certificate> list = new ArrayList<>();
//Generate CMSdata list.add(x509Certificate);
CMSSignedDataGenerator generator = new CMSSignedDataGenerator(); JcaCertStore store = new JcaCertStore(list);
List<X509Certificate> list = new ArrayList<>(); generator.addCertificates(store);
list.add(x509Certificate); CMSSignedData degenerateSd = generator.generate(new CMSAbsentContent());
JcaCertStore store = new JcaCertStore(list); byte[] signature = degenerateSd.getEncoded();
generator.addCertificates(store); boolean verifySignature = managementService.verifySignature(encoder.encode(signature));
CMSSignedData degenerateSd = generator.generate(new CMSAbsentContent()); Assert.assertNotNull(verifySignature);
byte[] signature = degenerateSd.getEncoded(); Assert.assertTrue(verifySignature);
log.info("VerifySignature Test Successful");
boolean verifySignature = managementService.verifySignature(encoder.encode(signature));
Assert.assertNotNull(verifySignature);
Assert.assertTrue(verifySignature);
log.info("VerifySignature Test Successful");
} catch (CertificateEncodingException e) {
String msg = "Error in Certificate encoding";
log.error(msg, e);
Assert.fail(msg, e);
} catch (IOException e) {
String msg = "Error reading encoded signature";
log.error(msg, e);
Assert.fail(msg, e);
} catch (CMSException e) {
String msg = "Error Adding certificates";
log.error(msg, e);
Assert.fail(msg, e);
} catch (KeystoreException e) {
String msg = "Error while accessing the keystore";
log.error(msg, e);
Assert.fail(msg, e);
}
} }
@Test(description = "This test case tests DN verification of a Certificate against the keystore") @Test(description = "This test case tests DN verification of a Certificate against the keystore")
public void testVerifySubjectDN() { public void testVerifySubjectDN() throws DeviceManagementException, KeystoreException {
try { DeviceConfigurationManager.getInstance().initConfig();
DeviceConfigurationManager.getInstance().initConfig(); X509Certificate x509Certificate = managementService.generateX509Certificate();
X509Certificate x509Certificate = managementService.generateX509Certificate(); log.info(x509Certificate.getIssuerX500Principal().getName());
log.info(x509Certificate.getIssuerX500Principal().getName()); managementService.verifySubjectDN(x509Certificate.getIssuerDN().getName());
managementService.verifySubjectDN(x509Certificate.getIssuerDN().getName());
} catch (KeystoreException e) {
String msg = "Error while accessing the keystore";
log.error(msg, e);
Assert.fail(msg, e);
} catch (DeviceManagementException e) {
String msg = "Error while initilizing DeviceConfigurationManager";
log.error(msg, e);
}
} }
@Test(description = "This test case tests retrieval of a Certificate from the keystore from the Serial") @Test(description = "This test case tests retrieval of a Certificate from the keystore from the Serial")
public void testRetrieveCertificate() { public void testRetrieveCertificate() throws KeystoreException, CertificateManagementException {
try { X509Certificate x509Certificate = managementService.generateX509Certificate();
X509Certificate x509Certificate = managementService.generateX509Certificate(); CertificateResponse certificateResponse = managementService.retrieveCertificate(x509Certificate.getSerialNumber().toString());
CertificateResponse certificateResponse = managementService.retrieveCertificate(x509Certificate.getSerialNumber().toString()); Assert.assertNotNull(certificateResponse);
Assert.assertNotNull(certificateResponse); Assert.assertEquals(x509Certificate.getSerialNumber(), certificateResponse.getCertificateserial());
Assert.assertEquals(x509Certificate.getSerialNumber(), certificateResponse.getCertificateserial());
} catch (KeystoreException e) {
String msg = "Error while accessing the keystore";
log.error(msg, e);
Assert.fail(msg, e);
} catch (CertificateManagementException e) {
String msg = " Error occurred while looking up for the certificate in the keystore";
log.error(msg, e);
Assert.fail(msg, e);
}
} }
@Test(description = "This test case tests the retrieval of Certificates from keystore in desired pagination") @Test(description = "This test case tests the retrieval of Certificates from keystore in desired pagination")
public void testGetAllCertificatesPaginated() throws CertificateManagementException { public void testGetAllCertificatesPaginated() throws CertificateManagementException, KeystoreException {
try { managementService.generateX509Certificate();
managementService.generateX509Certificate(); managementService.generateX509Certificate();
managementService.generateX509Certificate(); PaginationResult allCertificates = managementService.getAllCertificates(0, 2);
PaginationResult allCertificates = managementService.getAllCertificates(0, 2); Assert.assertEquals(allCertificates.getData().size(), 2);
Assert.assertEquals(allCertificates.getData().size(), 2); log.info("GetAllCertificatesPaginated Test Successful");
log.info("GetAllCertificatesPaginated Test Successful");
} catch (KeystoreException e) {
String msg = "Error while accessing the keystore";
log.error(msg, e);
Assert.fail(msg, e);
}
} }
@Test(description = "This test casae tests retrieval of all Certificates from keystore") @Test(description = "This test casae tests retrieval of all Certificates from keystore")
public void testGetCertificates() throws CertificateManagementException { public void testGetCertificates() throws CertificateManagementException, KeystoreException {
try { List<CertificateResponse> certificatesBefore = managementService.getCertificates();
List<CertificateResponse> certificatesBefore = managementService.getCertificates(); managementService.generateX509Certificate();
managementService.generateX509Certificate(); managementService.generateX509Certificate();
managementService.generateX509Certificate(); List<CertificateResponse> certificatesAfter = managementService.getCertificates();
List<CertificateResponse> certificatesAfter = managementService.getCertificates(); Assert.assertNotNull(certificatesBefore);
Assert.assertNotNull(certificatesBefore); Assert.assertNotNull(certificatesAfter);
Assert.assertNotNull(certificatesAfter); Assert.assertEquals((certificatesBefore.size() + 2), certificatesAfter.size());
Assert.assertEquals((certificatesBefore.size() + 2), certificatesAfter.size()); log.info("GetCertificates Test Successful");
log.info("GetCertificates Test Successful");
} catch (KeystoreException e) {
String msg = "Error while accessing the keystore";
log.error(msg, e);
Assert.fail(msg, e);
}
} }
@Test(description = "This test case tests deleting Certificate from the keystore") @Test(description = "This test case tests deleting Certificate from the keystore")
public void testRemoveCertificate() throws CertificateManagementException { public void testRemoveCertificate() throws CertificateManagementException, KeystoreException {
try { X509Certificate x509Certificate = managementService.generateX509Certificate();
List<CertificateResponse> certificates = managementService.getCertificates();
X509Certificate x509Certificate = managementService.generateX509Certificate(); int size = certificates.size();
List<CertificateResponse> certificates = managementService.getCertificates(); boolean removed = managementService.removeCertificate(x509Certificate.getSerialNumber().toString());
certificates = managementService.getCertificates();
int size = certificates.size(); int sizeAfter = certificates.size();
boolean removed = managementService.removeCertificate(x509Certificate.getSerialNumber().toString()); Assert.assertNotNull(removed);
certificates = managementService.getCertificates(); Assert.assertTrue(removed);
int sizeAfter = certificates.size(); Assert.assertEquals((size - 1), sizeAfter);
log.info("RemoveCertificate Test Successful");
Assert.assertNotNull(removed);
Assert.assertTrue(removed);
Assert.assertEquals((size - 1), sizeAfter);
log.info("RemoveCertificate Test Successful");
} catch (KeystoreException e) {
String msg = "Error while accessing the keystore";
log.error(msg, e);
Assert.fail(msg, e);
}
} }
@Test(description = "This test case tests searching for a list of certificates by the serial number") @Test(description = "This test case tests searching for a list of certificates by the serial number")
public void testSearchCertificates() throws CertificateManagementException { public void testSearchCertificates() throws CertificateManagementException, KeystoreException {
try { X509Certificate x509Certificate = managementService.generateX509Certificate();
X509Certificate x509Certificate = managementService.generateX509Certificate(); List<CertificateResponse> certificateResponses = managementService.searchCertificates(x509Certificate.getSerialNumber().toString());
List<CertificateResponse> certificateResponses = managementService.searchCertificates(x509Certificate.getSerialNumber().toString()); Assert.assertNotNull(certificateResponses);
Assert.assertNotNull(certificateResponses); Assert.assertEquals(1, certificateResponses.size());
Assert.assertEquals(1, certificateResponses.size()); Assert.assertEquals(certificateResponses.get(0).getSerialNumber(), x509Certificate.getSerialNumber().toString());
Assert.assertEquals(certificateResponses.get(0).getSerialNumber(), x509Certificate.getSerialNumber().toString()); log.info("SearchCertificates Test Successful");
log.info("SearchCertificates Test Successful");
} catch (KeystoreException e) {
String msg = "Error while accessing the keystore";
log.error(msg, e);
Assert.fail(msg, e);
}
} }
@Test(description = "This test case tests generation of signed Certificate from a CSR") @Test(description = "This test case tests generation of signed Certificate from a CSR")
public void testGetSignedCertificateFromCSR() { public void testGetSignedCertificateFromCSR() throws KeystoreException {
CSRGenerator csrGeneration = new CSRGenerator(); CSRGenerator csrGeneration = new CSRGenerator();
BASE64Encoder encoder = new BASE64Encoder(); BASE64Encoder encoder = new BASE64Encoder();
// Generate key pair // Generate key pair
KeyPair keyPair = csrGeneration.generateKeyPair("RSA", 1024); KeyPair keyPair = csrGeneration.generateKeyPair("RSA", 1024);
byte[] csrData = csrGeneration.generateCSR("SHA256WithRSA", keyPair); byte[] csrData = csrGeneration.generateCSR("SHA256WithRSA", keyPair);
try { X509Certificate signedCertificateFromCSR = managementService.getSignedCertificateFromCSR(encoder.encode(csrData));
X509Certificate signedCertificateFromCSR = managementService.getSignedCertificateFromCSR(encoder.encode(csrData)); Assert.assertNotNull(signedCertificateFromCSR);
Assert.assertNotNull(signedCertificateFromCSR); Assert.assertEquals(signedCertificateFromCSR.getType(), CertificateManagementConstants.X_509);
Assert.assertEquals(signedCertificateFromCSR.getType(), CertificateManagementConstants.X_509); log.info("GetSignedCertificateFromCSR Test Successful");
log.info("GetSignedCertificateFromCSR Test Successful");
} catch (KeystoreException e) {
String msg = "Error while accessing the keystore";
log.error(msg, e);
Assert.fail(msg, e);
}
} }
@Test(description = "This test case tests the extraction of Challenge token from a Certificate") @Test(description = "This test case tests the extraction of Challenge token from a Certificate")
public void testExtractChallengeToken() throws KeystoreException { public void testExtractChallengeToken() throws KeystoreException {
X509Certificate x509Certificate1 = new DummyCertificate(); X509Certificate x509Certificate1 = new DummyCertificate();
String token = managementService.extractChallengeToken(x509Certificate1); String token = managementService.extractChallengeToken(x509Certificate1);
Assert.assertNotNull(token); Assert.assertNotNull(token);
Assert.assertEquals(token, DummyCertificate.EXT); Assert.assertEquals(token, DummyCertificate.EXT);
log.info("extractChallengeToken Test Successful"); log.info("extractChallengeToken Test Successful");
} }
@Test(description = "This test case tests saving a list of Certificates in the keystore") @Test(description = "This test case tests saving a list of Certificates in the keystore")
public void testSaveCertificate() throws CertificateManagementException { public void testSaveCertificate() throws CertificateManagementException, IOException, CertificateException, KeystoreException {
File caCert = new File(CA_CERT_PEM); File caCert = new File(CA_CERT_PEM);
try { int before = managementService.getCertificates().size();
int before = managementService.getCertificates().size(); byte[] caBytes = FileUtils.readFileToByteArray(caCert);
byte[] caBytes = FileUtils.readFileToByteArray(caCert); CertificateFactory cf = CertificateFactory.getInstance("X.509");
CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(caBytes));
X509Certificate cert = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(caBytes)); List<org.wso2.carbon.certificate.mgt.core.bean.Certificate> certificates = new ArrayList<>();
org.wso2.carbon.certificate.mgt.core.bean.Certificate certificateToStore =
List<org.wso2.carbon.certificate.mgt.core.bean.Certificate> certificates = new ArrayList<>(); new org.wso2.carbon.certificate.mgt.core.bean.Certificate();
org.wso2.carbon.certificate.mgt.core.bean.Certificate certificateToStore = certificateToStore.setTenantId(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
new org.wso2.carbon.certificate.mgt.core.bean.Certificate(); certificateToStore.setCertificate(cert);
certificateToStore.setTenantId(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); certificates.add(certificateToStore);
certificateToStore.setCertificate(cert); managementService.saveCertificate(certificates);
certificates.add(certificateToStore); int after = managementService.getCertificates().size();
Assert.assertEquals((before + 1), after);
managementService.saveCertificate(certificates); log.info("SaveCertificate Test Successful");
int after = managementService.getCertificates().size();
Assert.assertEquals((before + 1), after);
log.info("SaveCertificate Test Successful");
} catch (IOException e) {
String msg = "Error while reading Pem file from the file";
log.error(msg, e);
Assert.fail(msg, e);
} catch (CertificateException e) {
String msg = "Error while Converting Pem file to X509 Certificate";
log.error(msg, e);
Assert.fail(msg, e);
} catch (KeystoreException e) {
String msg = "Error while accessing the keystore";
log.error(msg, e);
Assert.fail(msg, e);
;
}
} }
@Test(description = "This test case tests converting a pem file to X509 Certificate") @Test(description = "This test case tests converting a pem file to X509 Certificate")
public void testPemToX509Certificate() { public void testPemToX509Certificate() throws IOException, KeystoreException {
File caCert = new File(CA_CERT_PEM); File caCert = new File(CA_CERT_PEM);
BASE64Encoder encoder = new BASE64Encoder(); BASE64Encoder encoder = new BASE64Encoder();
try { byte[] caBytes = FileUtils.readFileToByteArray(caCert);
byte[] caBytes = FileUtils.readFileToByteArray(caCert); X509Certificate certificate = managementService.pemToX509Certificate(encoder.encode(caBytes));
X509Certificate certificate = managementService.pemToX509Certificate(encoder.encode(caBytes)); Assert.assertNotNull(certificate);
Assert.assertNotNull(certificate); Assert.assertEquals(certificate.getType(), CertificateManagementConstants.X_509);
Assert.assertEquals(certificate.getType(), CertificateManagementConstants.X_509); log.info("PemToX509Certificate Test Successful");
log.info("PemToX509Certificate Test Successful");
} catch (IOException e) {
String msg = "Error while reading Pem file from the file";
log.error(msg, e);
Assert.fail(msg, e);
} catch (KeystoreException e) {
String msg = "Error while accessing the keystore";
log.error(msg, e);
Assert.fail(msg, e);
}
} }
@Test(description = "This test case tests extracting Certificate from the header Signature") @Test(description = "This test case tests extracting Certificate from the header Signature")
public void testExtractCertificateFromSignature() { public void testExtractCertificateFromSignature() throws KeystoreException, CertificateEncodingException, CMSException, IOException {
BASE64Encoder encoder = new BASE64Encoder(); BASE64Encoder encoder = new BASE64Encoder();
//generate and save a certificate in the keystore
try { X509Certificate x509Certificate = managementService.generateX509Certificate();
//generate and save a certificate in the keystore //Generate CMSdata
X509Certificate x509Certificate = managementService.generateX509Certificate(); CMSSignedDataGenerator generator = new CMSSignedDataGenerator();
List<X509Certificate> list = new ArrayList<>();
//Generate CMSdata list.add(x509Certificate);
CMSSignedDataGenerator generator = new CMSSignedDataGenerator(); JcaCertStore store = new JcaCertStore(list);
List<X509Certificate> list = new ArrayList<>(); generator.addCertificates(store);
list.add(x509Certificate); CMSSignedData degenerateSd = generator.generate(new CMSAbsentContent());
JcaCertStore store = new JcaCertStore(list); byte[] signature = degenerateSd.getEncoded();
generator.addCertificates(store); X509Certificate certificate = managementService.extractCertificateFromSignature(encoder.encode(signature));
CMSSignedData degenerateSd = generator.generate(new CMSAbsentContent()); Assert.assertNotNull(certificate);
byte[] signature = degenerateSd.getEncoded(); Assert.assertEquals(certificate.getType(), CertificateManagementConstants.X_509);
log.info("ExtractCertificateFromSignature Test Successful");
X509Certificate certificate = managementService.extractCertificateFromSignature(encoder.encode(signature));
Assert.assertNotNull(certificate);
Assert.assertEquals(certificate.getType(), CertificateManagementConstants.X_509);
log.info("ExtractCertificateFromSignature Test Successful");
} catch (CertificateEncodingException e) {
String msg = "Error in Certificate encoding";
log.error(msg, e);
Assert.fail(msg, e);
} catch (IOException e) {
String msg = "Error reading encoded signature";
log.error(msg, e);
Assert.fail(msg, e);
} catch (CMSException e) {
String msg = "Error Adding certificates";
log.error(msg, e);
Assert.fail(msg, e);
} catch (KeystoreException e) {
String msg = "Error while accessing the keystore";
log.error(msg, e);
Assert.fail(msg, e);
}
} }
@BeforeClass @BeforeClass
public void init() throws Exception { public void init() throws Exception {
initDataSource(); initDataSource();
CertificateManagementDAOFactory.init(this.getDataSource()); CertificateManagementDAOFactory.init(this.getDataSource());
} }
} }

Loading…
Cancel
Save