diff --git a/build.gradle.kts b/build.gradle.kts index 12fbd93..68ae096 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,20 +1,21 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { - kotlin("jvm") version "1.9.24" - kotlin("plugin.spring") version "1.9.24" + kotlin("jvm") version "1.9.25" + kotlin("plugin.spring") version "1.9.25" id("jacoco") id("org.jmailen.kotlinter") version "4.4.1" + id("com.adarshr.test-logger") version "4.0.0" id("com.github.ben-manes.versions") version "0.51.0" - id("org.springframework.boot") version "3.3.2" - id("io.spring.dependency-management") version "1.1.6" + id("org.springframework.boot") version "3.5.6" + id("io.spring.dependency-management") version "1.1.7" id("net.thebugmc.gradle.sonatype-central-portal-publisher") version "1.2.3" id("maven-publish") } group = "com.valensas" -java.sourceCompatibility = JavaVersion.VERSION_17 +java.sourceCompatibility = JavaVersion.VERSION_21 repositories { mavenCentral() @@ -33,18 +34,18 @@ dependencies { testRuntimeOnly("org.junit.platform:junit-platform-launcher") // Ftp - implementation("commons-net:commons-net:3.11.1") + implementation("commons-net:commons-net:3.12.0") implementation("com.jcraft:jsch:0.1.55") - implementation("org.apache.sshd:sshd-core:2.11.0") - implementation("org.apache.sshd:sshd-common:2.11.0") - implementation("org.apache.sshd:sshd-sftp:2.11.0") - implementation("org.apache.ftpserver:ftpserver-core:1.0.0") + implementation("org.apache.sshd:sshd-core:2.16.0") + implementation("org.apache.sshd:sshd-common:2.16.0") + implementation("org.apache.sshd:sshd-sftp:2.16.0") + implementation("org.apache.ftpserver:ftpserver-core:1.2.1") } tasks.withType { kotlinOptions { freeCompilerArgs += "-Xjsr305=strict" - jvmTarget = "17" + jvmTarget = "21" } } diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index e644113..a4b76b9 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradlew b/gradlew index b740cf1..f5feea6 100755 --- a/gradlew +++ b/gradlew @@ -15,6 +15,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +# SPDX-License-Identifier: Apache-2.0 +# ############################################################################## # @@ -84,7 +86,8 @@ done # shellcheck disable=SC2034 APP_BASE_NAME=${0##*/} # Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) -APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s +' "$PWD" ) || exit # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD=maximum diff --git a/gradlew.bat b/gradlew.bat index 25da30d..9b42019 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -1,92 +1,94 @@ -@rem -@rem Copyright 2015 the original author or authors. -@rem -@rem Licensed under the Apache License, Version 2.0 (the "License"); -@rem you may not use this file except in compliance with the License. -@rem You may obtain a copy of the License at -@rem -@rem https://www.apache.org/licenses/LICENSE-2.0 -@rem -@rem Unless required by applicable law or agreed to in writing, software -@rem distributed under the License is distributed on an "AS IS" BASIS, -@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -@rem See the License for the specific language governing permissions and -@rem limitations under the License. -@rem - -@if "%DEBUG%"=="" @echo off -@rem ########################################################################## -@rem -@rem Gradle startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables with windows NT shell -if "%OS%"=="Windows_NT" setlocal - -set DIRNAME=%~dp0 -if "%DIRNAME%"=="" set DIRNAME=. -@rem This is normally unused -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME% - -@rem Resolve any "." and ".." in APP_HOME to make it shorter. -for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if %ERRORLEVEL% equ 0 goto execute - -echo. 1>&2 -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 -echo. 1>&2 -echo Please set the JAVA_HOME variable in your environment to match the 1>&2 -echo location of your Java installation. 1>&2 - -goto fail - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -set JAVA_EXE=%JAVA_HOME%/bin/java.exe - -if exist "%JAVA_EXE%" goto execute - -echo. 1>&2 -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 -echo. 1>&2 -echo Please set the JAVA_HOME variable in your environment to match the 1>&2 -echo location of your Java installation. 1>&2 - -goto fail - -:execute -@rem Setup the command line - -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar - - -@rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* - -:end -@rem End local scope for the variables with windows NT shell -if %ERRORLEVEL% equ 0 goto mainEnd - -:fail -rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of -rem the _cmd.exe /c_ return code! -set EXIT_CODE=%ERRORLEVEL% -if %EXIT_CODE% equ 0 set EXIT_CODE=1 -if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% -exit /b %EXIT_CODE% - -:mainEnd -if "%OS%"=="Windows_NT" endlocal - -:omega +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/src/main/kotlin/com/valensas/ftp/model/SFTPClient.kt b/src/main/kotlin/com/valensas/ftp/model/SFTPClient.kt index 33f871f..37776da 100644 --- a/src/main/kotlin/com/valensas/ftp/model/SFTPClient.kt +++ b/src/main/kotlin/com/valensas/ftp/model/SFTPClient.kt @@ -89,15 +89,31 @@ class SFTPClient : FTPClient() { override fun completePendingCommand(): Boolean = true - override fun isConnected(): Boolean = channel.isConnected + override fun isConnected(): Boolean { + val sessionConnected = + if (::session.isInitialized) { + session.isConnected + } else { + false + } + val channelConnected = + if (::channel.isInitialized) { + channel.isConnected + } else { + false + } + return sessionConnected || channelConnected + } override fun disconnect() { - if (::session.isInitialized) { - session.disconnect() - } if (::channel.isInitialized) { + logger.debug("Disconnecting from sftp channel.") channel.disconnect() } + if (::session.isInitialized) { + logger.debug("Disconnecting from sftp session.") + session.disconnect() + } } override fun listDirectories(parent: String): Array { diff --git a/src/test/kotlin/com/valensas/ftp/FtpApplicationTests.kt b/src/test/kotlin/com/valensas/ftp/FtpApplicationTests.kt index 6c1e374..d46ee9b 100644 --- a/src/test/kotlin/com/valensas/ftp/FtpApplicationTests.kt +++ b/src/test/kotlin/com/valensas/ftp/FtpApplicationTests.kt @@ -22,7 +22,6 @@ import java.security.KeyPairGenerator import java.security.PrivateKey import java.util.Base64 import java.util.UUID -import javax.naming.AuthenticationException import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertNotNull @@ -174,11 +173,11 @@ class FtpApplicationTests { @Test fun `Test should fail when sftp connection with DSA with key size greater than 1024`() { - assertThrows { - val keys = generatePublicKey(keySize = 2048, algorithm = "DSA") - val server = EmbeddedSftpServer() - server.start("username", null, clientPublicKey = keys.public, algorithm = "DSA", keySize = 2048) - val client = ftpClientFactory.createFtpClient(ConnectionType.SFTP) as SFTPClient + val keys = generatePublicKey(keySize = 2048, algorithm = "DSA") + val server = EmbeddedSftpServer() + server.start("username", null, clientPublicKey = keys.public, algorithm = "DSA", keySize = 2048) + val client = ftpClientFactory.createFtpClient(ConnectionType.SFTP) as SFTPClient + val result = client.authAndConnect( ConnectionModel( "name", @@ -193,10 +192,9 @@ class FtpApplicationTests { 6000, ), ) - assertTrue(client.isConnected) - client.disconnect() - server.stop() - } + assertFalse(result.connected) + assertFalse(client.isConnected) + server.stop() } @Test @@ -227,10 +225,10 @@ class FtpApplicationTests { @Test fun `Test if fail sftp connection can be handled`() { - assertThrows { - val server = EmbeddedSftpServer() - server.start("username", "password") - val client = ftpClientFactory.createFtpClient(ConnectionType.SFTP) as SFTPClient + val server = EmbeddedSftpServer() + server.start("username", "password") + val client = ftpClientFactory.createFtpClient(ConnectionType.SFTP) as SFTPClient + val result = client.authAndConnect( ConnectionModel( "name", @@ -245,10 +243,10 @@ class FtpApplicationTests { 6000, ), ) - assertTrue(client.isConnected) - client.disconnect() - server.stop() - } + assertFalse(result.connected) + assertFalse(client.isConnected) + client.disconnect() + server.stop() } @Test @@ -408,40 +406,6 @@ class FtpApplicationTests { verify(spyClient, times(4)).connectToServer(connectionModel) } - @Test - fun `Retry connection should not handle authentication exception`() { - val server = EmbeddedFtpServer() - val port = getRandomFreePort() - server.start( - "username", - "password", - ConnectionType.FTPS, - isImplicit = false, - certificatePath = "src/test/resources/ftps-test-cert.jks", - port = port, - ) - val connectionModel = - ConnectionModel( - "name", - ConnectionType.FTPS, - server.getHost(), - server.getPort(), - "wrongusername", - "wrongpassword", - Fake.privateKey(), - null, - ConnectionMode.Passive, - 6000, - retryBackoffDurationsInSecond = listOf(1000, 2000, 3000), - ) - val client = ftpClientFactory.createFtpClient(ConnectionType.FTPS, ConnectionVariant.Explicit) - val spyClient = spy(client) - assertThrows { - spyClient.authAndConnect(connectionModel) - } - verify(spyClient, times(1)).connectToServer(connectionModel) - } - private fun getRandomFreePort(): Int { ServerSocket(0).use { serverSocket -> return serverSocket.localPort