From 9f91a1b5a6de9e54abae4ce85940508d42f15b30 Mon Sep 17 00:00:00 2001 From: danielhanrahan Date: Tue, 15 Mar 2022 17:34:14 +0000 Subject: Code coverage for policy/gui gui-server Write tests to increase gui-server code coverage to 100% Issue-ID: POLICY-3351 Signed-off-by: danielhanrahan Change-Id: If5fa5d57f912496d1adab93b1e714e3c77a05b61 --- .../policy/gui/server/GuiServerAppMainTest.java | 41 ++++++++++++++++ .../config/ClampRestTemplateConfig6Test.java | 57 ++++++++++++++++++++++ .../server/filters/ClientSslHeaderFilterTest.java | 25 ++++++++++ 3 files changed, 123 insertions(+) create mode 100644 gui-server/src/test/java/org/onap/policy/gui/server/GuiServerAppMainTest.java create mode 100644 gui-server/src/test/java/org/onap/policy/gui/server/config/ClampRestTemplateConfig6Test.java diff --git a/gui-server/src/test/java/org/onap/policy/gui/server/GuiServerAppMainTest.java b/gui-server/src/test/java/org/onap/policy/gui/server/GuiServerAppMainTest.java new file mode 100644 index 0000000..d0f6598 --- /dev/null +++ b/gui-server/src/test/java/org/onap/policy/gui/server/GuiServerAppMainTest.java @@ -0,0 +1,41 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.gui.server; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +import org.junit.jupiter.api.Test; + +/** + * In this test, we check that application can start via main() method. + */ +class GuiServerAppMainTest { + + @Test + void whenMainIsCalled_thenNoExceptions() { + String[] args = { + "--server.port=0", // use random available port + "--clamp.url=https://clamp-backend:8443/", + "--clamp.disable-ssl-validation=true" + }; + assertDoesNotThrow(() -> GuiServerApplication.main(args)); + } +} diff --git a/gui-server/src/test/java/org/onap/policy/gui/server/config/ClampRestTemplateConfig6Test.java b/gui-server/src/test/java/org/onap/policy/gui/server/config/ClampRestTemplateConfig6Test.java new file mode 100644 index 0000000..d1d3072 --- /dev/null +++ b/gui-server/src/test/java/org/onap/policy/gui/server/config/ClampRestTemplateConfig6Test.java @@ -0,0 +1,57 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.gui.server.config; + +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +import org.junit.jupiter.api.Test; +import org.onap.policy.gui.server.test.util.hello.HelloWorldApplication; +import org.springframework.beans.factory.BeanCreationException; +import org.springframework.beans.factory.config.AutowireCapableBeanFactory; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ApplicationContext; +import org.springframework.test.util.ReflectionTestUtils; + +/** + * In this test, server.ssl.trust-store is unset while SSL validation is enabled. + * An BeanCreationException should be thrown on application startup. + */ +@SpringBootTest( + classes = { HelloWorldApplication.class } +) +class ClampRestTemplateConfig6Test { + + @Test + void expectExceptionWithNoTrustStore(ApplicationContext context) { + // Manually autowire the bean so we can test PostConstruct logic. + ClampRestTemplateConfig restTemplateConfig = new ClampRestTemplateConfig(); + AutowireCapableBeanFactory factory = context.getAutowireCapableBeanFactory(); + factory.autowireBean(restTemplateConfig); + + // Enable SSL validation, but provide no trust store. + ReflectionTestUtils.setField(restTemplateConfig, "disableSslValidation", false); + + // Expect exception when creating bean. + assertThatExceptionOfType(BeanCreationException.class) + .isThrownBy(() -> factory.initializeBean(restTemplateConfig, "clampRestTemplate")) + .withMessageContaining("server.ssl.trust-store must be set"); + } +} diff --git a/gui-server/src/test/java/org/onap/policy/gui/server/filters/ClientSslHeaderFilterTest.java b/gui-server/src/test/java/org/onap/policy/gui/server/filters/ClientSslHeaderFilterTest.java index 5fc026d..fb56fbc 100644 --- a/gui-server/src/test/java/org/onap/policy/gui/server/filters/ClientSslHeaderFilterTest.java +++ b/gui-server/src/test/java/org/onap/policy/gui/server/filters/ClientSslHeaderFilterTest.java @@ -27,12 +27,14 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.onap.policy.gui.server.filters.ClientSslHeaderFilter.SSL_CERT_HEADER_NAME; import static org.onap.policy.gui.server.filters.ClientSslHeaderFilter.X509_ATTRIBUTE_NAME; import static org.onap.policy.gui.server.util.X509CertificateEncoder.urlDecodeCert; import java.io.IOException; +import java.security.cert.CertificateEncodingException; import java.security.cert.X509Certificate; import java.util.Collections; import java.util.Enumeration; @@ -117,6 +119,29 @@ class ClientSslHeaderFilterTest { assertEquals(Collections.emptyEnumeration(), outRequest.getHeaders(SSL_CERT_HEADER_NAME)); } + /* + * If there is a CertificateEncodingException, the filter should not set + * the X-SSL-Cert header. + */ + @Test + void testInvalidClientCert_noHeader() throws Exception { + // Create an invalid cert. + X509Certificate invalidCert = mock(X509Certificate.class); + doThrow(CertificateEncodingException.class).when(invalidCert).getEncoded(); + + // Create a request with an invalid client SSL cert. + MockHttpServletRequest inRequest = new MockHttpServletRequest(); + inRequest.setAttribute(X509_ATTRIBUTE_NAME, new X509Certificate[] { invalidCert }); + + // Apply the filter. + HttpServletRequest outRequest = applyRequestFilter(inRequest); + + // The modified request should not contain a cert header. + assertFalse(containsCertHeader(outRequest.getHeaderNames())); + assertNull(outRequest.getHeader(SSL_CERT_HEADER_NAME)); + assertEquals(Collections.emptyEnumeration(), outRequest.getHeaders(SSL_CERT_HEADER_NAME)); + } + /* * This test is needed to prevent a security vulnerability where a * malicious user does not authenticate using client cert, but defines the -- cgit 1.2.3-korg