From 8eb02642c77e2508180ceabf58bc105072b719c2 Mon Sep 17 00:00:00 2001 From: Sameera Wickramasekara Date: Mon, 2 Oct 2017 12:43:16 +0530 Subject: [PATCH] Fixed test case issues --- .../BaseDeviceManagementCertificateTest.java | 4 - .../mgt/core/common/DataSourceConfig.java | 3 + ...CertificateManagementServiceImplTests.java | 29 +- .../mgt/core/util/CSRGenerator.java | 37 +- .../mgt/core/util/DummyCertificate.java | 22 +- .../carbon-home/repository/conf/carbon.xml | 655 ++++++++++++++++++ .../src/test/resources/sql/h2.sql | 17 + 7 files changed, 716 insertions(+), 51 deletions(-) create mode 100644 components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/resources/carbon-home/repository/conf/carbon.xml 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 59e83ca5c62..38a16a7fa07 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 @@ -44,7 +44,6 @@ import java.sql.Statement; public abstract class BaseDeviceManagementCertificateTest { private DataSource dataSource; private static final Log log = LogFactory.getLog(BaseDeviceManagementCertificateTest.class); - private static final String DATASOURCE_LOCATION = "src/test/resources/data-source-config.xml"; @BeforeSuite @@ -78,7 +77,6 @@ public abstract class BaseDeviceManagementCertificateTest { return new org.apache.tomcat.jdbc.pool.DataSource(properties); } - private DataSourceConfig readDataSourceConfig() throws DeviceManagementException { try { File file = new File(DATASOURCE_LOCATION); @@ -123,14 +121,12 @@ public abstract class BaseDeviceManagementCertificateTest { System.setProperty("carbon.home", file.getAbsolutePath()); } } - PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants .SUPER_TENANT_DOMAIN_NAME); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID); } 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/common/DataSourceConfig.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/common/DataSourceConfig.java index 869660ee794..f3154d3bdc8 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/common/DataSourceConfig.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/common/DataSourceConfig.java @@ -20,6 +20,9 @@ package org.wso2.carbon.certificate.mgt.core.common; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; +/** + * Model class for DataSourceConfig. + */ @XmlRootElement(name = "DataSourceConfig") public class DataSourceConfig { 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 f8023644a20..a7e0629e5d2 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 @@ -56,26 +56,27 @@ import java.security.cert.*; import java.util.ArrayList; import java.util.List; +/** + * Test cases for for CertificateManagementServiceImpl class methods. + * + */ public class CertificateManagementServiceImplTests extends BaseDeviceManagementCertificateTest { private static Log log = LogFactory.getLog(CertificateManagementServiceImplTests.class); private static final String CA_CERT_PEM = "src/test/resources/ca_cert.pem"; private static final String RA_CERT_PEM = "src/test/resources/ra_cert.pem"; - CertificateManagementServiceImpl managementService = null; - - @Test(description = "This test case tests initialization of CertificateManagementServiceImpl instance") - public void testGetInstance() { - CertificateManagementServiceImpl instance = CertificateManagementServiceImpl.getInstance(); - Assert.assertNotNull(instance); - log.info("getInstance Test Successful"); - } + private CertificateManagementServiceImpl managementService; @BeforeClass - public void initCertificateManagementService() throws DeviceManagementException { - //save certificatemanagementservice instance as class variable - managementService = CertificateManagementServiceImpl.getInstance(); + public void init() throws Exception { + initDataSource(); + CertificateManagementDAOFactory.init(this.getDataSource()); //set Bouncycastle as a provider for testing Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); + //save certificatemanagementservice instance as class variable + managementService = CertificateManagementServiceImpl.getInstance(); + Assert.assertNotNull(managementService); + log.info("getInstance Test Successful"); } @Test(description = "This test case tests retrieval of CA Certificate from the keystore") @@ -329,9 +330,5 @@ public class CertificateManagementServiceImplTests extends BaseDeviceManagementC log.info("ExtractCertificateFromSignature Test Successful"); } - @BeforeClass - public void init() throws Exception { - initDataSource(); - CertificateManagementDAOFactory.init(this.getDataSource()); - } + } 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 1ee50d04ad9..5fa4f7dabd4 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 @@ -18,7 +18,10 @@ package org.wso2.carbon.certificate.mgt.core.util; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; 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; @@ -30,8 +33,13 @@ import java.io.IOException; import java.io.PrintStream; import java.security.KeyPair; import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +/** + * Test class for generating a mock CSR request. + */ public class CSRGenerator { + private static Log log = LogFactory.getLog(CSRGenerator.class); /** * Generate the desired CSR for signing @@ -43,32 +51,18 @@ public class CSRGenerator { public byte[] generateCSR(String sigAlg, KeyPair keyPair) { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); PrintStream printStream = new PrintStream(outStream); - try { - 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 csr.getEncoded(); - } catch (Exception ex) { - ex.printStackTrace(); - } finally { - if (null != outStream) { - try { - outStream.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - if (null != printStream) { - printStream.close(); - } + } catch (OperatorCreationException ex) { + log.error("Error while Key generation operation", ex); + } catch (IOException ex) { + log.error("Error while generating CSR,ex"); } - return new byte[0]; } @@ -83,14 +77,11 @@ public class CSRGenerator { try { KeyPairGenerator keyPairGenerator = null; keyPairGenerator = KeyPairGenerator.getInstance(alg); - keyPairGenerator.initialize(keySize); - return keyPairGenerator.generateKeyPair(); - } catch (Exception ex) { - ex.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + log.error("The provided algorithm is not found ",e); } - return null; } } diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/util/DummyCertificate.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/util/DummyCertificate.java index a89c4e0a7e5..f276df46256 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/util/DummyCertificate.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/java/org/wso2/carbon/certificate/mgt/core/util/DummyCertificate.java @@ -21,25 +21,34 @@ package org.wso2.carbon.certificate.mgt.core.util; import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; import java.math.BigInteger; -import java.security.*; -import java.security.cert.*; +import java.security.Principal; +import java.security.PublicKey; +import java.security.cert.CertificateEncodingException; +import java.security.cert.CertificateExpiredException; +import java.security.cert.CertificateNotYetValidException; +import java.security.cert.CertificateException; +import java.security.NoSuchAlgorithmException; +import java.security.SignatureException; +import java.security.NoSuchProviderException; +import java.security.cert.X509Certificate; +import java.security.InvalidKeyException; import java.util.Date; import java.util.Set; +/** + * A Mock Certificate class with an extension provided.(Used only to test the extension) + */ public class DummyCertificate extends X509Certificate { public static final String EXT = "Dummy extension"; public static final String DN = "O=WSO2,OU=Mobile,C=LK,CN=123456789"; - @Override public void checkValidity() throws CertificateExpiredException, CertificateNotYetValidException { - } @Override public void checkValidity(Date date) throws CertificateExpiredException, CertificateNotYetValidException { - } @Override @@ -65,7 +74,6 @@ public class DummyCertificate extends X509Certificate { return DN; } }; - } @Override @@ -130,12 +138,10 @@ public class DummyCertificate extends X509Certificate { @Override public void verify(PublicKey key) throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException { - } @Override public void verify(PublicKey key, String sigProvider) throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException { - } @Override diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/resources/carbon-home/repository/conf/carbon.xml b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/resources/carbon-home/repository/conf/carbon.xml new file mode 100644 index 00000000000..7f976a58cf5 --- /dev/null +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/resources/carbon-home/repository/conf/carbon.xml @@ -0,0 +1,655 @@ + + + + + + + + ${product.name} + + + ${product.key} + + + ${product.version} + + + + + + + + + local:/${carbon.context}/services/ + + + + + + + ${default.server.role} + + + + + + + org.wso2.carbon + + + / + + + + + + + + + 15 + + + + + + + + + 0 + + + + + 9999 + + 11111 + + + + + + 10389 + + 8000 + + + + + + 10500 + + + + + + + org.wso2.carbon.tomcat.jndi.CarbonJavaURLContextFactory + + + + + + + + + java + + + + + + + + + + false + + + false + + + 600 + + + + false + + + + + + + + 30 + + + + + + + + + 15 + + + + + + ${carbon.home}/repository/deployment/server/ + + + 15 + + + ${carbon.home}/repository/conf/axis2/axis2.xml + + + 30000 + + + ${carbon.home}/repository/deployment/client/ + + ${carbon.home}/repository/conf/axis2/axis2_client.xml + + true + + + + + + + + + + admin + Default Administrator Role + + + user + Default User Role + + + + + + + + + + + + ${carbon.home}/repository/resources/security/wso2carbon.jks + + JKS + + wso2carbon + + wso2carbon + + wso2carbon + + + + + + ${carbon.home}/repository/resources/security/client-truststore.jks + + JKS + + wso2carbon + + + + + + + + + + + + + + + + + + + UserManager + + + false + + + + + + + ${carbon.home}/tmp/work + + + + + + true + + + 10 + + + 30 + + + + + + 100 + + + + keystore + certificate + * + + org.wso2.carbon.ui.transports.fileupload.AnyFileUploadExecutor + + + + + jarZip + + org.wso2.carbon.ui.transports.fileupload.JarZipUploadExecutor + + + + dbs + + org.wso2.carbon.ui.transports.fileupload.DBSFileUploadExecutor + + + + tools + + org.wso2.carbon.ui.transports.fileupload.ToolsFileUploadExecutor + + + + toolsAny + + org.wso2.carbon.ui.transports.fileupload.ToolsAnyFileUploadExecutor + + + + + + + info + org.wso2.carbon.core.transports.util.InfoProcessor + + + wsdl + org.wso2.carbon.core.transports.util.Wsdl11Processor + + + wsdl2 + org.wso2.carbon.core.transports.util.Wsdl20Processor + + + xsd + org.wso2.carbon.core.transports.util.XsdProcessor + + + + + + false + false + true + svn + http://svnrepo.example.com/repos/ + username + password + true + + + + + + + + + + + + + + + ${require.carbon.servlet} + + + + + true + + + + + + + default repository + ${p2.repo.url} + + + + + + + + true + + + + + + true + + diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/resources/sql/h2.sql b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/resources/sql/h2.sql index 6705b595f0e..044898f9128 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/resources/sql/h2.sql +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/test/resources/sql/h2.sql @@ -1,3 +1,20 @@ +-- +-- Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +-- +-- WSO2 Inc. licenses this file to you under the Apache License, +-- Version 2.0 (the "License"); you may not use this file except +-- in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. + CREATE TABLE IF NOT EXISTS DM_DEVICE_CERTIFICATE ( ID INTEGER auto_increment NOT NULL, SERIAL_NUMBER VARCHAR(500) DEFAULT NULL,