diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/pom.xml b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/pom.xml
index d891e03194..a5dde4a1bd 100644
--- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/pom.xml
+++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/pom.xml
@@ -167,6 +167,16 @@
h2-database-engine
test
+
+ org.powermock
+ powermock-api-mockito
+ test
+
+
+ org.powermock
+ powermock-module-testng
+ 1.7.0
+
org.wso2.carbon
org.wso2.carbon.logging
diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/common/BaseDeviceManagementCertificateTest.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/common/BaseDeviceManagementCertificateTest.java
index b1a09026b1..6ca1b5c383 100644
--- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/common/BaseDeviceManagementCertificateTest.java
+++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/common/BaseDeviceManagementCertificateTest.java
@@ -3,6 +3,7 @@ package org.wso2.carbon.certificate.mgt.core.common;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tomcat.jdbc.pool.PoolProperties;
+import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeClass;
@@ -26,7 +27,7 @@ import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
-public abstract class BaseDeviceManagementCertificateTest {
+public abstract class BaseDeviceManagementCertificateTest{
private DataSource dataSource;
private static final Log log = LogFactory.getLog(BaseDeviceManagementCertificateTest.class);
@@ -75,7 +76,7 @@ public abstract class BaseDeviceManagementCertificateTest {
Statement stmt = null;
try {
conn = this.getDataSource().getConnection();
- stmt = conn.createStatement();
+ stmt = conn.createStatement();
stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/sql/h2.sql'");
} finally {
TestUtils.cleanupResources(conn, stmt, null);
@@ -148,6 +149,7 @@ public abstract class BaseDeviceManagementCertificateTest {
}
public DataSource getDataSource() {
+
return dataSource;
}
diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateManagementServiceImplTests.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateManagementServiceImplTests.java
index ad144b7f9c..61d63ac0ec 100644
--- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateManagementServiceImplTests.java
+++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateManagementServiceImplTests.java
@@ -1,24 +1,14 @@
package org.wso2.carbon.certificate.mgt.core.impl;
+import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.apache.commons.io.FileUtils;
-import org.apache.woden.tool.converter.Convert;
-import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.cert.jcajce.JcaCertStore;
-import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.bouncycastle.cms.CMSAbsentContent;
import org.bouncycastle.cms.CMSException;
import org.bouncycastle.cms.CMSSignedData;
import org.bouncycastle.cms.CMSSignedDataGenerator;
-import org.bouncycastle.openssl.PEMReader;
-import org.bouncycastle.operator.ContentSigner;
-import org.bouncycastle.operator.OperatorCreationException;
-import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
import org.bouncycastle.pkcs.PKCS10CertificationRequest;
-import org.bouncycastle.pkcs.PKCS10CertificationRequestBuilder;
-import org.bouncycastle.util.io.pem.PemReader;
-import org.eclipse.wst.xml.core.internal.contenttype.ByteReader;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@@ -34,27 +24,25 @@ import org.wso2.carbon.certificate.mgt.core.util.CSRGenerator;
import org.wso2.carbon.certificate.mgt.core.util.CertificateManagementConstants;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
-import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
-
-import javax.xml.bind.DatatypeConverter;
+import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.IOException;
-import java.io.StringReader;
+import java.math.BigInteger;
import java.security.KeyPair;
-import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.Security;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
-import java.security.interfaces.RSAPublicKey;
-import java.util.ArrayDeque;
import java.util.ArrayList;
-import java.util.Base64;
import java.util.List;
+//@RunWith(PowerMockRunner.class)
+
+//@PowerMockIgnore({"javax.xml.*","java.sql.*", "org.xml.sax.*", "org.w3c.dom.*", "org.springframework.context.*", "org.apache.log4j.*"})
+//@PrepareForTest(org.wso2.carbon.certificate.mgt.core.util.CommonUtil.class)
public class CertificateManagementServiceImplTests extends BaseDeviceManagementCertificateTest {
private static Log log = LogFactory.getLog(CertificateManagementServiceImplTests.class);
@@ -191,19 +179,12 @@ public class CertificateManagementServiceImplTests extends BaseDeviceManagementC
// Generate key pair
KeyPair keyPair = csrGeneration.generateKeyPair("RSA", 1024);
byte[] csrData = csrGeneration.generateCSR("SHA256WithRSA", keyPair);
-
- //remove headers from csr
- String csrString = new String(csrData);
- csrString = csrString.replace("-----BEGIN NEW CERTIFICATE REQUEST-----", "")
- .replace("-----END NEW CERTIFICATE REQUEST-----", "");
-
- byte[] byteArrayBst = DatatypeConverter.parseBase64Binary(csrString);
PKCS10CertificationRequest certificationRequest;
try {
PrivateKey privateKeyCA = keyStoreReader.getCAPrivateKey();
X509Certificate certCA = (X509Certificate) keyStoreReader.getCACertificate();
- certificationRequest = new PKCS10CertificationRequest(byteArrayBst);
+ certificationRequest = new PKCS10CertificationRequest(csrData);
X509Certificate x509Certificate = managementService.generateCertificateFromCSR(privateKeyCA,
certificationRequest, certCA.getIssuerX500Principal().getName());
@@ -319,18 +300,28 @@ public class CertificateManagementServiceImplTests extends BaseDeviceManagementC
}
- @Test
- public void testVerifyPEMSignature() {
- }
+
+// public void testVerifyPEMSignature() throws KeystoreException, DeviceManagementException {
+//
+// DeviceConfigurationManager.getInstance().initConfig();
+// X509Certificate x509Certificate = managementService.generateX509Certificate();
+//
+// PowerMockito.mockStatic(CommonUtil.class);
+// PowerMockito.when(CommonUtil.generateSerialNumber()).thenReturn(new BigInteger("12345"));
+// CertificateResponse certificateResponse = managementService.verifyPEMSignature(x509Certificate);
+// Assert.assertNotNull(certificateResponse);
+//
+// }
@Test
public void testVerifySubjectDN() {
try {
DeviceConfigurationManager.getInstance().initConfig();
X509Certificate x509Certificate = managementService.generateX509Certificate();
- log.info(x509Certificate.getIssuerDN().getName());
+ log.info(x509Certificate.getIssuerX500Principal().getName());
+
managementService.verifySubjectDN(x509Certificate.getIssuerDN().getName());
} catch (KeystoreException e) {
@@ -340,6 +331,86 @@ public class CertificateManagementServiceImplTests extends BaseDeviceManagementC
}
}
+ @Test
+ public void testRetrieveCertificate(){
+ try {
+ X509Certificate x509Certificate = managementService.generateX509Certificate();
+ CertificateResponse certificateResponse = managementService.retrieveCertificate(x509Certificate.getSerialNumber().toString());
+ Assert.assertNotNull(certificateResponse);
+ Assert.assertEquals(x509Certificate.getSerialNumber(),certificateResponse.getCertificateserial());
+
+ } catch (KeystoreException e) {
+ e.printStackTrace();
+ } catch (CertificateManagementException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ @Test
+ public void testGetAllCertificates() throws CertificateManagementException {
+ managementService.getAllCertificates(1,1);
+ }
+
+ @Test
+ public void testGetCertificates(){
+ try{
+ List certificatesBefore = managementService.getCertificates();
+ X509Certificate x509Certificate1 = managementService.generateX509Certificate();
+ X509Certificate x509Certificate2 = managementService.generateX509Certificate();
+ List certificatesAfter = managementService.getCertificates();
+ Assert.assertNotNull(certificatesBefore);
+ Assert.assertNotNull(certificatesAfter);
+ Assert.assertEquals((certificatesBefore.size() + 2),certificatesAfter.size());
+
+
+ } catch (CertificateManagementException e) {
+ e.printStackTrace();
+ } catch (KeystoreException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Test
+ public void testGetCertificatesWithParams(){
+ try {
+
+ X509Certificate x509Certificate = managementService.generateX509Certificate();
+ List certificates = managementService.getCertificates();
+
+ int size = certificates.size();
+ boolean removed = managementService.removeCertificate(x509Certificate.getSerialNumber().toString());
+ certificates = managementService.getCertificates();
+ int sizeAfter = certificates.size();
+
+ Assert.assertNotNull(removed);
+ Assert.assertTrue(removed);
+ Assert.assertEquals((size-1),sizeAfter);
+
+ } catch (CertificateManagementException e) {
+ e.printStackTrace();
+ } catch (KeystoreException e) {
+ e.printStackTrace();
+ }
+ }
+
+
+ @Test
+ public void testSearchCertificates(){
+ try {
+ X509Certificate x509Certificate = managementService.generateX509Certificate();
+ List certificateResponses = managementService.searchCertificates(x509Certificate.getSerialNumber().toString());
+ Assert.assertNotNull(certificateResponses);
+ Assert.assertEquals(1,certificateResponses.size());
+ Assert.assertEquals(certificateResponses.get(0).getSerialNumber(),x509Certificate.getSerialNumber().toString());
+
+ } catch (KeystoreException e) {
+ e.printStackTrace();
+ } catch (CertificateManagementException e) {
+ e.printStackTrace();
+ }
+ }
+
@BeforeClass
@Override
diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/util/CSRGenerator.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/util/CSRGenerator.java
index 76dd9e2d0e..019d5a9c93 100644
--- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/util/CSRGenerator.java
+++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/util/CSRGenerator.java
@@ -1,14 +1,18 @@
package org.wso2.carbon.certificate.mgt.core.util;
-import sun.security.pkcs10.PKCS10;
-import sun.security.x509.X500Name;
+import org.bouncycastle.operator.ContentSigner;
+import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
+import org.bouncycastle.pkcs.PKCS10CertificationRequest;
+import org.bouncycastle.pkcs.PKCS10CertificationRequestBuilder;
+import org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequestBuilder;
+import javax.security.auth.x500.X500Principal;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
-import java.security.Signature;
+
public class CSRGenerator {
@@ -24,19 +28,14 @@ public class CSRGenerator {
PrintStream printStream = new PrintStream(outStream);
try {
- X500Name x500Name = new X500Name("C=DE,O=Organiztion,CN=WSO2");
-
- Signature sig = Signature.getInstance(sigAlg);
-
- sig.initSign(keyPair.getPrivate());
-
- PKCS10 pkcs10 = new PKCS10(keyPair.getPublic());
- pkcs10.encodeAndSign(x500Name, sig); // For Java 7 and Java 8
- pkcs10.print(printStream);
- byte[] csrBytes = outStream.toByteArray();
+ PKCS10CertificationRequestBuilder p10Builder = new JcaPKCS10CertificationRequestBuilder(
+ new X500Principal("CN=Requested Test Certificate"), keyPair.getPublic());
+ JcaContentSignerBuilder csBuilder = new JcaContentSignerBuilder("SHA256withRSA");
+ ContentSigner signer = csBuilder.build(keyPair.getPrivate());
+ PKCS10CertificationRequest csr = p10Builder.build(signer);
- return csrBytes;
+ return csr.getEncoded();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/resources/testng.xml b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/resources/testng.xml
index 56ec9fc569..f96d8b329d 100644
--- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/resources/testng.xml
+++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/resources/testng.xml
@@ -1,6 +1,7 @@
+