Add second set of tests

revert-70aa11f8
Sameera Wickramasekara 7 years ago
parent a9fd6b3f1c
commit 5c9ad2a3d2

@ -167,6 +167,16 @@
<artifactId>h2-database-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-testng</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.logging</artifactId>

@ -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;
@ -148,6 +149,7 @@ public abstract class BaseDeviceManagementCertificateTest {
}
public DataSource getDataSource() {
return dataSource;
}

@ -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<CertificateResponse> certificatesBefore = managementService.getCertificates();
X509Certificate x509Certificate1 = managementService.generateX509Certificate();
X509Certificate x509Certificate2 = managementService.generateX509Certificate();
List<CertificateResponse> 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<CertificateResponse> 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<CertificateResponse> 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

@ -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 {

@ -1,6 +1,7 @@
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="CertificateManagementServiceImplTests">
<test name="Operatoin Bean validation tests" preserve-order="true">
<classes>
<class name="org.wso2.carbon.certificate.mgt.core.common.BaseDeviceManagementCertificateTest"/>

Loading…
Cancel
Save