summaryrefslogtreecommitdiffstats
path: root/certServiceClient/src/test
diff options
context:
space:
mode:
authorkjaniak <kornel.janiak@nokia.com>2020-11-13 15:03:17 +0100
committerkjaniak <kornel.janiak@nokia.com>2020-11-25 12:30:39 +0100
commit5a9b5d9d6911a82e50c7d3e15c2cc8b9ca14098a (patch)
tree1f78ba5e0862c970a91370e272d3740289942c66 /certServiceClient/src/test
parent6043d57de1232277456200a7e218cfa53e444c05 (diff)
[OOM cert-service-client] Add validation of email, ip and domain name
Added SanMapper to distinguish types of SANS. ExitableException changed to RuntimeException. Introcudced intermediate object San. Issue-ID: OOM-2559 Signed-off-by: kjaniak <kornel.janiak@nokia.com> Change-Id: I060de9869ab9fd737a474f683a251abd8431d224
Diffstat (limited to 'certServiceClient/src/test')
-rw-r--r--certServiceClient/src/test/java/org/onap/oom/certservice/client/certification/CsrFactoryTest.java24
-rw-r--r--certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/factory/ClientConfigurationFactoryTest.java20
-rw-r--r--certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/factory/CsrConfigurationFactoryTest.java38
-rw-r--r--certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/factory/SanMapperTest.java77
-rw-r--r--certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/client/ClientEnvsValueValidatorsTest.java (renamed from certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/BasicValidationFunctionsTest.java)44
-rw-r--r--certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/client/OutputTypeValidatorTest.java44
-rw-r--r--certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/csr/CommonNameValidatorTest.java (renamed from certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/ValidatorsFactoryTest.java)22
-rw-r--r--certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/csr/CsrEnvsValueValidatorsTest.java122
-rw-r--r--certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/csr/UriValidatorTest.java (renamed from certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/UriValidatorTest.java)30
9 files changed, 336 insertions, 85 deletions
diff --git a/certServiceClient/src/test/java/org/onap/oom/certservice/client/certification/CsrFactoryTest.java b/certServiceClient/src/test/java/org/onap/oom/certservice/client/certification/CsrFactoryTest.java
index ee7d210f..ab9fc921 100644
--- a/certServiceClient/src/test/java/org/onap/oom/certservice/client/certification/CsrFactoryTest.java
+++ b/certServiceClient/src/test/java/org/onap/oom/certservice/client/certification/CsrFactoryTest.java
@@ -20,31 +20,35 @@
package org.onap.oom.certservice.client.certification;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.security.KeyPair;
import java.util.List;
+import org.bouncycastle.asn1.x509.GeneralName;
import org.junit.jupiter.api.Test;
import org.onap.oom.certservice.client.certification.exception.CsrGenerationException;
import org.onap.oom.certservice.client.certification.exception.KeyPairGenerationException;
import org.onap.oom.certservice.client.configuration.model.CsrConfiguration;
-
-import java.security.KeyPair;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
+import org.onap.oom.certservice.client.configuration.model.San;
class CsrFactoryTest {
CsrConfiguration config = mock(CsrConfiguration.class);
-
@Test
- void createEncodedCsr_shouldSucceedWhenAllFieldsAreSetCorrectly() throws KeyPairGenerationException, CsrGenerationException {
+ void createEncodedCsr_shouldSucceedWhenAllFieldsAreSetCorrectly()
+ throws KeyPairGenerationException, CsrGenerationException {
KeyPair keyPair =
- new KeyPairFactory(EncryptionAlgorithmConstants.RSA_ENCRYPTION_ALGORITHM, EncryptionAlgorithmConstants.KEY_SIZE).create();
+ new KeyPairFactory(EncryptionAlgorithmConstants.RSA_ENCRYPTION_ALGORITHM,
+ EncryptionAlgorithmConstants.KEY_SIZE).create();
+ San san1 = new San("onapexample.com", GeneralName.dNSName);
+ San san2 = new San("onapexample.com.pl", GeneralName.dNSName);
when(config.getCommonName()).thenReturn("onap.org");
- when(config.getSans()).thenReturn(List.of("onapexample.com","onapexample.com.pl","onapexample.pl"));
+ when(config.getSans()).thenReturn(List.of(san1, san2));
when(config.getCountry()).thenReturn("US");
when(config.getLocation()).thenReturn("San-Francisco");
when(config.getOrganization()).thenReturn("Linux-Foundation");
diff --git a/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/factory/ClientConfigurationFactoryTest.java b/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/factory/ClientConfigurationFactoryTest.java
index 071e7551..166b1e60 100644
--- a/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/factory/ClientConfigurationFactoryTest.java
+++ b/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/factory/ClientConfigurationFactoryTest.java
@@ -27,7 +27,7 @@ import org.onap.oom.certservice.client.configuration.exception.ClientConfigurati
import org.onap.oom.certservice.client.configuration.model.ClientConfiguration;
import java.util.Optional;
-import org.onap.oom.certservice.client.configuration.validation.ValidatorsFactory;
+import org.onap.oom.certservice.client.configuration.validation.client.OutputTypeValidator;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -48,7 +48,7 @@ public class ClientConfigurationFactoryTest {
private static final String OUTPUT_TYPE_DEFAULT = "P12";
private EnvsForClient envsForClient = mock(EnvsForClient.class);
- private ValidatorsFactory validatorsFactory = new ValidatorsFactory();
+ private OutputTypeValidator outputTypeValidator = new OutputTypeValidator();
@Test
@@ -61,7 +61,7 @@ public class ClientConfigurationFactoryTest {
when(envsForClient.getOutputType()).thenReturn(Optional.of(OUTPUT_TYPE_VALID));
// when
- ClientConfiguration configuration = new ClientConfigurationFactory(envsForClient, validatorsFactory).create();
+ ClientConfiguration configuration = new ClientConfigurationFactory(envsForClient, outputTypeValidator).create();
System.out.println(configuration.toString());
// then
@@ -79,7 +79,7 @@ public class ClientConfigurationFactoryTest {
when(envsForClient.getOutputPath()).thenReturn(Optional.of(OUTPUT_PATH_VALID));
// when
- ClientConfiguration configuration = new ClientConfigurationFactory(envsForClient, validatorsFactory).create();
+ ClientConfiguration configuration = new ClientConfigurationFactory(envsForClient, outputTypeValidator).create();
// then
assertThat(configuration.getCaName()).isEqualTo(CA_NAME_VALID);
@@ -95,7 +95,8 @@ public class ClientConfigurationFactoryTest {
when(envsForClient.getOutputPath()).thenReturn(Optional.of(OUTPUT_PATH_VALID));
// when
- ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient, validatorsFactory);
+ ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient,
+ outputTypeValidator);
// then
assertThatExceptionOfType(ClientConfigurationException.class)
@@ -112,7 +113,8 @@ public class ClientConfigurationFactoryTest {
when(envsForClient.getUrlToCertService()).thenReturn(Optional.of(URL_TO_CERT_SERVICE_VALID));
// when
- ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient, validatorsFactory);
+ ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient,
+ outputTypeValidator);
// when/then
assertThatExceptionOfType(ClientConfigurationException.class)
@@ -129,7 +131,8 @@ public class ClientConfigurationFactoryTest {
when(envsForClient.getUrlToCertService()).thenReturn(Optional.of(URL_TO_CERT_SERVICE_VALID));
// when
- ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient, validatorsFactory);
+ ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient,
+ outputTypeValidator);
//then
assertThatExceptionOfType(ClientConfigurationException.class)
@@ -147,7 +150,8 @@ public class ClientConfigurationFactoryTest {
when(envsForClient.getOutputType()).thenReturn(Optional.of(OUTPUT_TYPE_INVALID));
// when
- ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient, validatorsFactory);
+ ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient,
+ outputTypeValidator);
//then
assertThatExceptionOfType(ClientConfigurationException.class)
diff --git a/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/factory/CsrConfigurationFactoryTest.java b/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/factory/CsrConfigurationFactoryTest.java
index cdcefe2e..158f0cf2 100644
--- a/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/factory/CsrConfigurationFactoryTest.java
+++ b/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/factory/CsrConfigurationFactoryTest.java
@@ -22,6 +22,7 @@ package org.onap.oom.certservice.client.configuration.factory;
import java.util.List;
import org.assertj.core.api.Condition;
+import org.bouncycastle.asn1.x509.GeneralName;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.onap.oom.certservice.client.configuration.CsrConfigurationEnvs;
@@ -30,7 +31,8 @@ import org.onap.oom.certservice.client.configuration.exception.CsrConfigurationE
import org.onap.oom.certservice.client.configuration.model.CsrConfiguration;
import java.util.Optional;
-import org.onap.oom.certservice.client.configuration.validation.ValidatorsFactory;
+import org.onap.oom.certservice.client.configuration.model.San;
+import org.onap.oom.certservice.client.configuration.validation.csr.CommonNameValidator;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -41,8 +43,9 @@ import static org.onap.oom.certservice.client.api.ExitStatus.CSR_CONFIGURATION_E
public class CsrConfigurationFactoryTest {
private static final String COMMON_NAME_VALID = "onap.org";
- private static final List<String> SANS_SPLITTED_VALID = List.of("test-name", "test-name-1");
- private static final String SANS_VALID = "test-name,test-name-1";
+ private static final String RAW_SAN1 = "ves-collector";
+ private static final String RAW_SAN2 = "ves";
+ private static final String RAW_SANS_VALID = String.format("%s,%s", RAW_SAN1, RAW_SAN2);
private static final String COUNTRY_VALID = "US";
private static final String LOCATION_VALID = "San-Francisco";
private static final String ORGANIZATION_VALID = "Linux-Foundation";
@@ -51,9 +54,11 @@ public class CsrConfigurationFactoryTest {
private static final String COMMON_NAME_INVALID = "onap.org*&";
private static final String COUNTRY_INVALID = "PLA";
private static final String ORGANIZATION_INVALID = "Linux?Foundation";
+ private static final String INVALID_SANS = "192.168.1.";
private EnvsForCsr envsForCsr = mock(EnvsForCsr.class);
- private ValidatorsFactory validatorsFactory = new ValidatorsFactory();
+ private CommonNameValidator commonNameValidator = new CommonNameValidator();
+ private SanMapper sanMapper = new SanMapper();
private CsrConfigurationFactory testedFactory;
private Condition<CsrConfigurationException> expectedExitCodeCondition = new Condition<>("Correct exit code") {
@Override
@@ -64,20 +69,23 @@ public class CsrConfigurationFactoryTest {
@BeforeEach
void setUp() {
- testedFactory = new CsrConfigurationFactory(envsForCsr, validatorsFactory);
+ testedFactory = new CsrConfigurationFactory(envsForCsr, commonNameValidator, sanMapper);
}
@Test
void shouldReturnCorrectConfiguration_WhenAllVariablesAreSetAndValid() throws CsrConfigurationException {
// given
mockEnvsWithAllValidParameters();
+ San san1 = new San(RAW_SAN1, GeneralName.dNSName);
+ San san2 = new San(RAW_SAN2, GeneralName.dNSName);
+ List<San> sans = List.of(san1, san2);
// when
CsrConfiguration configuration = testedFactory.create();
// then
assertThat(configuration.getCommonName()).isEqualTo(COMMON_NAME_VALID);
- assertThat(configuration.getSans()).isEqualTo(SANS_SPLITTED_VALID);
+ assertThat(configuration.getSans()).isEqualTo(sans);
assertThat(configuration.getCountry()).isEqualTo(COUNTRY_VALID);
assertThat(configuration.getLocation()).isEqualTo(LOCATION_VALID);
assertThat(configuration.getOrganization()).isEqualTo(ORGANIZATION_VALID);
@@ -150,6 +158,17 @@ public class CsrConfigurationFactoryTest {
.has(expectedExitCodeCondition);
}
+ @Test
+ void shouldThrowCsrConfigurationExceptionWhenSansInvalid() {
+ // given
+ mockEnvsWithInvalidSans();
+ // when/then
+ assertThatExceptionOfType(CsrConfigurationException.class)
+ .isThrownBy(testedFactory::create)
+ .withMessageContaining("SAN :" + INVALID_SANS + " does not match any requirements")
+ .has(expectedExitCodeCondition);
+ }
+
private void mockEnvsWithAllValidParameters() {
mockEnvsWithValidRequiredParameters();
mockEnvsWithValidOptionalParameters();
@@ -158,7 +177,7 @@ public class CsrConfigurationFactoryTest {
private void mockEnvsWithValidOptionalParameters() {
when(envsForCsr.getOrganizationUnit()).thenReturn(Optional.of(ORGANIZATION_UNIT_VALID));
when(envsForCsr.getLocation()).thenReturn(Optional.of(LOCATION_VALID));
- when(envsForCsr.getSubjectAlternativesName()).thenReturn(Optional.of(SANS_VALID));
+ when(envsForCsr.getSubjectAlternativesName()).thenReturn(Optional.of(RAW_SANS_VALID));
}
private void mockEnvsWithValidRequiredParameters() {
@@ -187,4 +206,9 @@ public class CsrConfigurationFactoryTest {
mockEnvsWithAllValidParameters();
when(envsForCsr.getState()).thenReturn(Optional.empty());
}
+
+ private void mockEnvsWithInvalidSans() {
+ mockEnvsWithAllValidParameters();
+ when(envsForCsr.getSubjectAlternativesName()).thenReturn(Optional.of(INVALID_SANS));
+ }
}
diff --git a/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/factory/SanMapperTest.java b/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/factory/SanMapperTest.java
new file mode 100644
index 00000000..9ec4c6e7
--- /dev/null
+++ b/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/factory/SanMapperTest.java
@@ -0,0 +1,77 @@
+/*
+ * ============LICENSE_START=======================================================
+ * oom-certservice-client
+ * ================================================================================
+ * Copyright (C) 2020 Nokia. All rights reserved.
+ * ================================================================================
+ * Licensed 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.oom.certservice.client.configuration.factory;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+
+import java.util.function.Function;
+import org.bouncycastle.asn1.x509.GeneralName;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+import org.onap.oom.certservice.client.configuration.exception.CsrConfigurationException;
+import org.onap.oom.certservice.client.configuration.model.San;
+
+class SanMapperTest {
+
+ private Function<String, San> sanMapper = new SanMapper();
+
+ @ParameterizedTest
+ @ValueSource(strings = {"192.178.2.3", "10.183.34.201", "ff:ff:ff:ff:ff:ff:ff:ff", "ff:ff::"})
+ void shouldCorrectlyMapIpAddress(String san) {
+ // when
+ San result = sanMapper.apply(san);
+ // then
+ assertThat(result.getValue()).isEqualTo(san);
+ assertThat(result.getType()).isEqualTo(GeneralName.iPAddress);
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"foo@bar.com", "sample@example.com", "onap@domain.pl", "alex.supertramp@onap.com",
+ "al.super^tramp@onap.org"})
+ void shouldCorrectlyMapEmailAddress(String san) {
+ // when
+ San result = sanMapper.apply(san);
+ // then
+ assertThat(result.getValue()).isEqualTo(san);
+ assertThat(result.getType()).isEqualTo(GeneralName.rfc822Name);
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"sample.com", "Sample.com", "onap.org", "SRI-NIC.ARPA", "ves-collector", "sample"})
+ void shouldCorrectlyMapDomain(String san) {
+ // when
+ San result = sanMapper.apply(san);
+ // then
+ assertThat(result.getValue()).isEqualTo(san);
+ assertThat(result.getType()).isEqualTo(GeneralName.dNSName);
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {" ", "", "192.168.0.", "10.183.34.201:8080", "incoreectdomaim@onap.ux", "<sample@example.com>",
+ "onap@domain"})
+ void shouldThrowExceptionOnIncorrectString(String san) {
+ // when, then
+ assertThatExceptionOfType(CsrConfigurationException.class)
+ .isThrownBy(() -> sanMapper.apply(san))
+ .withMessage("SAN :" + san + " does not match any requirements");
+ }
+}
diff --git a/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/BasicValidationFunctionsTest.java b/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/client/ClientEnvsValueValidatorsTest.java
index d552ba55..9ed46c1a 100644
--- a/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/BasicValidationFunctionsTest.java
+++ b/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/client/ClientEnvsValueValidatorsTest.java
@@ -18,17 +18,27 @@
* ============LICENSE_END=========================================================
*/
-package org.onap.oom.certservice.client.configuration.validation;
+package org.onap.oom.certservice.client.configuration.validation.client;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.onap.oom.certservice.client.configuration.validation.BasicValidationFunctions.isAlphaNumeric;
-import static org.onap.oom.certservice.client.configuration.validation.BasicValidationFunctions.isCountryValid;
-import static org.onap.oom.certservice.client.configuration.validation.BasicValidationFunctions.isPathValid;
+import static org.onap.oom.certservice.client.configuration.validation.client.ClientEnvsValueValidators.isAlphaNumeric;
+import static org.onap.oom.certservice.client.configuration.validation.client.ClientEnvsValueValidators.isPathValid;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
-class BasicValidationFunctionsTest {
+class ClientEnvsValueValidatorsTest {
+ @ParameterizedTest
+ @ValueSource(strings = {"caname", "caname1", "123caName", "ca1name"})
+ void shouldAcceptValidAlphanumeric(String caName) {
+ assertThat(isAlphaNumeric(caName)).isTrue();
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"44caname$", "#caname1", "1c_aname", "ca1-name"})
+ void shouldRejectInvalidAlphanumeric(String caName) {
+ assertThat(isAlphaNumeric(caName)).isFalse();
+ }
@ParameterizedTest
@ValueSource(strings = {"/var/log", "/", "/var/log/", "/second_var", "/second-var"})
@@ -42,28 +52,4 @@ class BasicValidationFunctionsTest {
assertThat(isPathValid(path)).isFalse();
}
- @ParameterizedTest
- @ValueSource(strings = {"PL", "DE", "PN", "US", "IO", "CA", "KH", "CO", "DK", "EC", "CZ", "CN", "BR", "BD", "BE"})
- void shouldAcceptValidCountryCode(String countryCode) {
- assertThat(isCountryValid(countryCode)).isTrue();
- }
-
- @ParameterizedTest
- @ValueSource(strings = {"", "QQ", "AFG", "D", "&*", "!", "ONAP", "p", "pl", "us", "afg"})
- void shouldRejectInvalidCountryCode(String countryCode) {
- assertThat(isCountryValid(countryCode)).isFalse();
- }
-
- @ParameterizedTest
- @ValueSource(strings = {"caname", "caname1", "123caName", "ca1name"})
- void shouldAcceptValidAlphanumeric(String caName) {
- assertThat(isAlphaNumeric(caName)).isTrue();
- }
-
- @ParameterizedTest
- @ValueSource(strings = {"44caname$", "#caname1", "1c_aname", "ca1-name"})
- void shouldRejectInvalidAlphanumeric(String caName) {
- assertThat(isAlphaNumeric(caName)).isFalse();
- }
-
}
diff --git a/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/client/OutputTypeValidatorTest.java b/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/client/OutputTypeValidatorTest.java
new file mode 100644
index 00000000..1b32c017
--- /dev/null
+++ b/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/client/OutputTypeValidatorTest.java
@@ -0,0 +1,44 @@
+/*
+ * ============LICENSE_START=======================================================
+ * oom-certservice-client
+ * ================================================================================
+ * Copyright (C) 2020 Nokia. All rights reserved.
+ * ================================================================================
+ * Licensed 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.oom.certservice.client.configuration.validation.client;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+class OutputTypeValidatorTest {
+
+ OutputTypeValidator cut = new OutputTypeValidator();
+
+ @ParameterizedTest
+ @ValueSource(strings = {"JKS", "P12", "PEM"})
+ void shouldAcceptValidOutputType(String outputType) {
+ assertThat(cut.test(outputType)).isTrue();
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"jks", "p12", "pem", "", "pass", "!@$#pp"})
+ void shouldRejectInvalidOutputType(String outputType) {
+ assertThat(cut.test(outputType)).isFalse();
+ }
+
+}
diff --git a/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/ValidatorsFactoryTest.java b/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/csr/CommonNameValidatorTest.java
index 3c14d30f..deb02cf3 100644
--- a/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/ValidatorsFactoryTest.java
+++ b/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/csr/CommonNameValidatorTest.java
@@ -18,39 +18,27 @@
* ============LICENSE_END=========================================================
*/
-package org.onap.oom.certservice.client.configuration.validation;
+package org.onap.oom.certservice.client.configuration.validation.csr;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
-class ValidatorsFactoryTest {
+class CommonNameValidatorTest {
- ValidatorsFactory cut = new ValidatorsFactory();
-
- @ParameterizedTest
- @ValueSource(strings = {"JKS", "P12", "PEM"})
- void shouldAcceptValidOutputType(String outputType) {
- assertThat(cut.outputTypeValidator().test(outputType)).isTrue();
- }
-
- @ParameterizedTest
- @ValueSource(strings = {"jks", "p12", "pem", "", "pass", "!@$#pp"})
- void shouldRejectInvalidOutputType(String outputType) {
- assertThat(cut.outputTypeValidator().test(outputType)).isFalse();
- }
+ CommonNameValidator cut = new CommonNameValidator();
@ParameterizedTest
@ValueSource(strings = {"example.com", "www.example.com"})
void shouldAcceptValidCommonName(String commonName) {
- assertThat(cut.commonNameValidator().test(commonName)).isTrue();
+ assertThat(cut.test(commonName)).isTrue();
}
@ParameterizedTest
@ValueSource(strings = {"https://example.com", "http://example.com", "example.com:8080", "0.0.0.0", "@#$%.com"})
void shouldRejectInvalidCommonName(String commonName) {
- assertThat(cut.commonNameValidator().test(commonName)).isFalse();
+ assertThat(cut.test(commonName)).isFalse();
}
}
diff --git a/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/csr/CsrEnvsValueValidatorsTest.java b/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/csr/CsrEnvsValueValidatorsTest.java
new file mode 100644
index 00000000..d1d075a4
--- /dev/null
+++ b/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/csr/CsrEnvsValueValidatorsTest.java
@@ -0,0 +1,122 @@
+/*
+ * ============LICENSE_START=======================================================
+ * oom-certservice-client
+ * ================================================================================
+ * Copyright (C) 2020 Nokia. All rights reserved.
+ * ================================================================================
+ * Licensed 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.oom.certservice.client.configuration.validation.csr;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.onap.oom.certservice.client.configuration.validation.client.ClientEnvsValueValidators.isPathValid;
+import static org.onap.oom.certservice.client.configuration.validation.csr.CsrEnvsValueValidators.isCountryValid;
+import static org.onap.oom.certservice.client.configuration.validation.csr.CsrEnvsValueValidators.isDomainNameValid;
+import static org.onap.oom.certservice.client.configuration.validation.csr.CsrEnvsValueValidators.isEmailAddressValid;
+import static org.onap.oom.certservice.client.configuration.validation.csr.CsrEnvsValueValidators.isIpAddressValid;
+import static org.onap.oom.certservice.client.configuration.validation.csr.CsrEnvsValueValidators.isSpecialCharPresent;
+import static org.onap.oom.certservice.client.configuration.validation.csr.CsrEnvsValueValidators.isUriValid;
+
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+class CsrEnvsValueValidatorsTest {
+
+ @ParameterizedTest
+ @ValueSource(strings = {"/var/log", "/", "/var/log/", "/second_var", "/second-var"})
+ void shouldAcceptValidPath(String path) {
+ assertThat(isPathValid(path)).isTrue();
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"/var/log?", "", "var_", "var", "//", "/var//log"})
+ void shouldRejectInvalidPath(String path) {
+ assertThat(isPathValid(path)).isFalse();
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"PL", "DE", "PN", "US", "IO", "CA", "KH", "CO", "DK", "EC", "CZ", "CN", "BR", "BD", "BE"})
+ void shouldAcceptValidCountryCode(String countryCode) {
+ assertThat(isCountryValid(countryCode)).isTrue();
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"", "QQ", "AFG", "D", "&*", "!", "ONAP", "p", "pl", "us", "afg"})
+ void shouldRejectInvalidCountryCode(String countryCode) {
+ assertThat(isCountryValid(countryCode)).isFalse();
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"sample@example.com", "onap@domain.pl", "alex.supertramp@onap.com",
+ "al.super^tramp@onap.org"})
+ void shouldAcceptValidEmailAddr(String emailAddr) {
+ assertThat(isEmailAddressValid(emailAddr)).isTrue();
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"<sample@example.com>", "onap@domain", "(mailto)user@onap.com", "mailto:axe@axe.de",
+ "incoreectdomaim@onap.ux"})
+ void shouldRejectInvalidEmailAddr(String address) {
+ assertThat(isEmailAddressValid(address)).isFalse();
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"192.168.0.1", "10.183.34.201", "ff:ff:ff:ff::", "ff:ff:ff:ff:ff:ff:ff:ff"})
+ void shouldAcceptValidIpAddress(String address) {
+ assertThat(isIpAddressValid(address)).isTrue();
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"192.168.0.", "ff:ff:ee:a1:", "fg:ff:ff:ff::", "http://10.183.34.201",
+ "10.183.34.201:8080"})
+ void shouldRejectInvalidIpAddress(String address) {
+ assertThat(isIpAddressValid(address)).isFalse();
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"sample.com", "Sample.com", "onap.org", "SRI-NIC.ARPA", "ves-collector", "sample"})
+ void shouldAcceptValidDomainName(String domain) {
+ assertThat(isDomainNameValid(domain)).isTrue();
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {" ", "", "sample@onap.org", "192.168.0.1", "http://sample.com"})
+ void shouldRejectInvalidDomainNames(String domain) {
+ assertThat(isDomainNameValid(domain)).isFalse();
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"http://sample.com/path", "ftp://sample.com/path/file.txt", "https://ves.pl"})
+ void shouldBeTrueForValidUris(String text) {
+ assertThat(isUriValid(text)).isTrue();
+ }
+ @ParameterizedTest
+ @ValueSource(strings = {"3http://sample.com", "192.168.0.1", "www.example.com"})
+ void shouldBeFalseForInvalidUris(String text) {
+ assertThat(isUriValid(text)).isFalse();
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"/text~", "/text#", "/text@", "/text*","/text$", "/text+", "/text%", "/text!", "/text(",
+ "/text)", "/text?", "/text|", "/text_", "/text^"})
+ void shouldBeTrueForStringsWithSpecialChars(String text) {
+ assertThat(isSpecialCharPresent(text)).isTrue();
+ }
+ @ParameterizedTest
+ @ValueSource(strings = {"text", ""})
+ void shouldBeFalseForStringsWithoutSpecialChars(String text) {
+ assertThat(isSpecialCharPresent(text)).isFalse();
+ }
+}
diff --git a/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/UriValidatorTest.java b/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/csr/UriValidatorTest.java
index d7024d4e..addc4603 100644
--- a/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/UriValidatorTest.java
+++ b/certServiceClient/src/test/java/org/onap/oom/certservice/client/configuration/validation/csr/UriValidatorTest.java
@@ -18,9 +18,10 @@
* ============LICENSE_END=========================================================
*/
-package org.onap.oom.certservice.client.configuration.validation;
+package org.onap.oom.certservice.client.configuration.validation.csr;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.onap.oom.certservice.client.configuration.validation.csr.UriValidator.isValid;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
@@ -34,19 +35,19 @@ class UriValidatorTest {
@ParameterizedTest
@ValueSource(strings = {"http:/", "http:", "http://", "h4ttp://"})
void shouldTrueForValidScheme(String uri) {
- assertThat(UriValidator.isValidUri(uri)).isTrue();
+ assertThat(isValid(uri)).isTrue();
}
@ParameterizedTest
@ValueSource(strings = {"example.com", "www.example.com", "0.0.0.0", "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]"})
void shouldFalseForUriWithoutScheme(String uri) {
- assertThat(UriValidator.isValidUri(uri)).isFalse();
+ assertThat(isValid(uri)).isFalse();
}
@ParameterizedTest
@ValueSource(strings = {"*http://", "_http://", "?http://", "4http://"})
void shouldFalseForUriWithInvalidScheme(String uri) {
- assertThat(UriValidator.isValidUri(uri)).isFalse();
+ assertThat(isValid(uri)).isFalse();
}
/**
@@ -64,7 +65,7 @@ class UriValidatorTest {
"http://user:password:test@example.com",
"http://user-info:password@example.com"})
void shouldTrueForValidUserInAuthority(String uri) {
- assertThat(UriValidator.isValidUri(uri)).isTrue();
+ assertThat(isValid(uri)).isTrue();
}
@ParameterizedTest
@@ -72,7 +73,7 @@ class UriValidatorTest {
"http://user:password",
"http://user:password:test:"})
void shouldFalseForMissingHostInAuthority(String uri) {
- assertThat(UriValidator.isValidUri(uri)).isFalse();
+ assertThat(isValid(uri)).isFalse();
}
@ParameterizedTest
@@ -82,7 +83,7 @@ class UriValidatorTest {
"http://8.8.8.8/",
"http://8.8.8.8/test"})
void shouldTrueForUriContainsIP(String uri) {
- assertThat(UriValidator.isValidUri(uri)).isTrue();
+ assertThat(isValid(uri)).isTrue();
}
@ParameterizedTest
@@ -92,7 +93,7 @@ class UriValidatorTest {
"http://8.8.8.8:8080/test",
"https://8.8.8.8:443/"})
void shouldTrueForUriContainsIPAndPort(String uri) {
- assertThat(UriValidator.isValidUri(uri)).isTrue();
+ assertThat(isValid(uri)).isTrue();
}
@ParameterizedTest
@@ -101,7 +102,7 @@ class UriValidatorTest {
"http:/file",
"http:/ptah/to/file"})
void shouldTrueForMissingAuthority(String uri) {
- assertThat(UriValidator.isValidUri(uri)).isTrue();
+ assertThat(isValid(uri)).isTrue();
}
/**
@@ -114,7 +115,7 @@ class UriValidatorTest {
"http://example.com/path",
"http://example.com/",})
void shouldTrueForPathWithAuthority(String uri) {
- assertThat(UriValidator.isValidUri(uri)).isTrue();
+ assertThat(isValid(uri)).isTrue();
}
@ParameterizedTest
@@ -123,7 +124,7 @@ class UriValidatorTest {
"http:/path",
"http:/",})
void shouldTrueForPathWithoutAuthority(String uri) {
- assertThat(UriValidator.isValidUri(uri)).isTrue();
+ assertThat(isValid(uri)).isTrue();
}
@@ -134,7 +135,7 @@ class UriValidatorTest {
"http://example.com?test=tes1&#",
"http://example.com#onap"})
void shouldTrueForUriWithQueryAndFragmentInPath(String uri) {
- assertThat(UriValidator.isValidUri(uri)).isTrue();
+ assertThat(isValid(uri)).isTrue();
}
@ParameterizedTest
@@ -143,7 +144,7 @@ class UriValidatorTest {
"http://example.com?##",
"http://www.example.com/file%GF.html"})
void shouldFalseForUriWithWrongQueryOrWrongFragmentInPath(String uri) {
- assertThat(UriValidator.isValidUri(uri)).isFalse();
+ assertThat(isValid(uri)).isFalse();
}
@ParameterizedTest
@@ -157,6 +158,7 @@ class UriValidatorTest {
"telnet://192.0.2.16:80/",
"urn:oasis:names:specification:docbook:dtd:xml:4.1.2"})
void shouldTrueForRFC3986Examples(String uri) {
- assertThat(UriValidator.isValidUri(uri)).isTrue();
+ assertThat(isValid(uri)).isTrue();
}
+
}