增强 SSH/SFTP 稳定性并完善安全校验与前端交互
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package com.sshmanager.service;
|
||||
|
||||
import com.sshmanager.entity.Connection;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class SftpServiceTest {
|
||||
|
||||
private SftpService sftpService;
|
||||
private ExecutorService executorService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
executorService = Executors.newFixedThreadPool(2);
|
||||
sftpService = new SftpService();
|
||||
sftpService.setExecutorService(executorService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPasswordAuthenticationRequiredWithValidConnection() {
|
||||
Exception exception = assertThrows(Exception.class, () -> {
|
||||
Connection conn = new Connection();
|
||||
conn.setHost("127.0.0.1");
|
||||
conn.setPort(22);
|
||||
conn.setUsername("test");
|
||||
conn.setAuthType(Connection.AuthType.PASSWORD);
|
||||
sftpService.connect(conn, "", null, null);
|
||||
});
|
||||
assertTrue(exception.getMessage().contains("Password is required") ||
|
||||
exception instanceof IllegalArgumentException);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPasswordAuthenticationRequiredWithNullConn() {
|
||||
Exception exception = assertThrows(Exception.class, () -> {
|
||||
sftpService.connect(null, "", null, null);
|
||||
});
|
||||
assertTrue(exception instanceof NullPointerException || exception instanceof IllegalArgumentException);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExecutorServiceShutdown() throws Exception {
|
||||
executorService.shutdown();
|
||||
assertTrue(executorService.isTerminated() || executorService.isShutdown());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user