From cf1221cac89cc3310017d6c72ee0638ddec24cea Mon Sep 17 00:00:00 2001 From: Chengkai Yan Date: Mon, 29 Oct 2018 12:57:47 +0100 Subject: Add test for FtpsClient, Improve code coverage. Fix a security vulnerability risk Change-Id: I71560dfd9977a7e96ac4441e468602b616c7e7a6 Issue-ID: DCAEGEN2-889 Signed-off-by: Chengkai Yan --- .../collectors/datafile/ftp/FileCollectResult.java | 2 +- .../collectors/datafile/ftp/ErrorDataTest.java | 57 ++++++++++++++++++++++ .../collectors/datafile/ftp/FtpsClientTest.java | 22 +++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/ErrorDataTest.java (limited to 'datafile-dmaap-client/src') diff --git a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FileCollectResult.java b/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FileCollectResult.java index c5962172..9b6eacb0 100644 --- a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FileCollectResult.java +++ b/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FileCollectResult.java @@ -44,6 +44,6 @@ public class FileCollectResult { @Override public String toString() { - return "Download successful: " + result + " Error data: " + getErrorData(); + return "FileCollectResult: " + result + " Error data: " + getErrorData(); } } diff --git a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/ErrorDataTest.java b/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/ErrorDataTest.java new file mode 100644 index 00000000..e40d7d78 --- /dev/null +++ b/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/ErrorDataTest.java @@ -0,0 +1,57 @@ +/* + * ============LICENSE_START====================================================================== + * Copyright (C) 2018 NOKIA Intellectual Property, 2018 Nordix Foundation. 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.dcaegen2.collectors.datafile.ftp; + +import static org.mockito.Mockito.mock; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class ErrorDataTest { + private List errorMessages = new ArrayList<>(); + private List errorCauses = new ArrayList<>(); + private ErrorData errorData = new ErrorData(); + + @BeforeEach + protected void setUp() { + int testSize = 3; + for (int i = 0; i < testSize; i++) { + errorMessages.add("test"); + errorCauses.add(mock(Throwable.class)); + } + for (int i = 0; i < testSize; i++) { + errorData.addError(errorMessages.get(i), errorCauses.get(i)); + } + } + + public String getMessageAsString() { + StringBuilder message = new StringBuilder(); + for (int i = 0; i < errorMessages.size(); i++) { + message.append(errorMessages.get(i)).append(" Cause: ").append(errorCauses.get(i)).append("\n"); + } + return message.toString(); + } + + @Test + public void testToString_returnExpectedString() { + Assertions.assertEquals(getMessageAsString(), errorData.toString()); + } +} diff --git a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClientTest.java b/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClientTest.java index e5693d50..66d3b00c 100644 --- a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClientTest.java +++ b/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClientTest.java @@ -233,4 +233,26 @@ public class FtpsClientTest { assertFalse(result.downloadSuccessful()); verify(localFileMock, times(1)).delete(); } + + @Test + public void collectFileFailingFileRetrieve_shouldFail() throws Exception { + when(keyManagerUtilsMock.getClientKeyManager()).thenReturn(keyManagerMock); + when(fileResourceMock.getInputStream()).thenReturn(inputStreamMock); + when(keyStoreWrapperMock.getKeyStore()).thenReturn(keyStoreMock); + when(trustManagerFactoryMock.getTrustManagers()).thenReturn(new TrustManager[] {trustManagerMock}); + when(ftpsClientMock.login(USERNAME, PASSWORD)).thenReturn(true); + when(ftpsClientMock.getReplyCode()).thenReturn(HttpStatus.OK.value()); + File fileMock = mock(File.class); + when(localFileMock.getFile()).thenReturn(fileMock); + OutputStream osMock = mock(OutputStream.class); + when(outputStreamMock.getOutputStream(fileMock)).thenReturn(osMock); + when(ftpsClientMock.retrieveFile(REMOTE_FILE_PATH, osMock)).thenReturn(false); + + ImmutableFileServerData fileServerData = ImmutableFileServerData.builder().serverAddress(XNF_ADDRESS) + .userId(USERNAME).password(PASSWORD).port(PORT).build(); + + FileCollectResult result = clientUnderTest.collectFile(fileServerData, REMOTE_FILE_PATH, LOCAL_FILE_PATH); + + assertFalse(result.downloadSuccessful()); + } } \ No newline at end of file -- cgit 1.2.3-korg