-
Notifications
You must be signed in to change notification settings - Fork 0
/
testcasesftp2
151 lines (125 loc) · 4.76 KB
/
testcasesftp2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// pom.xml dependencies
<dependencies>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-sftp</artifactId>
<version>2.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-common</artifactId>
<version>2.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>
// SftpServerConfiguration.java
@TestConfiguration
public class SftpServerConfiguration {
private static final int SFTP_PORT = 2222;
private static final String TEST_USERNAME = "test-user";
@Bean(destroyMethod = "stop")
public SshServer sshServer() throws IOException {
SshServer sshServer = SshServer.setUpDefaultServer();
sshServer.setPort(SFTP_PORT);
sshServer.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
sshServer.setPublickeyAuthenticator(this::authenticatePublicKey);
sshServer.setSubsystemFactories(Collections.singletonList(
new SftpSubsystemFactory.Builder().build()));
// Setup user authentication
sshServer.setPasswordAuthenticator((username, password, session) ->
TEST_USERNAME.equals(username));
// Setup SFTP root directory
Path sftpRoot = Files.createTempDirectory("sftp-test");
sftpRoot.toFile().deleteOnExit();
sshServer.setFileSystemFactory(new VirtualFileSystemFactory(sftpRoot));
// Start the server
sshServer.start();
return sshServer;
}
private boolean authenticatePublicKey(String username, PublicKey key, ServerSession session) {
if (!TEST_USERNAME.equals(username)) {
return false;
}
try {
// Load the authorized public key from resources
Path publicKeyPath = Paths.get(getClass().getResource("/test-keys/id_rsa.pub").toURI());
String authorizedKey = Files.readString(publicKeyPath);
PublicKey authorizedPublicKey = PublicKeyEntry.parsePublicKeyEntry(authorizedKey).resolvePublicKey();
return key.equals(authorizedPublicKey);
} catch (Exception e) {
return false;
}
}
}
// SftpService.java
@Service
@ConfigurationProperties(prefix = "sftp")
public class SftpService {
private String host;
private int port;
private String username;
private String privateKeyPath;
public void downloadFile(String remoteFile, Path localPath) throws IOException {
JSch jsch = new JSch();
Session session = null;
ChannelSftp channelSftp = null;
try {
// Add private key
jsch.addIdentity(privateKeyPath);
session = jsch.getSession(username, host, port);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
channelSftp.get(remoteFile, localPath.toString());
} finally {
if (channelSftp != null) channelSftp.disconnect();
if (session != null) session.disconnect();
}
}
}
// SftpServiceIntegrationTest.java
@SpringBootTest
@Import(SftpServerConfiguration.class)
class SftpServiceIntegrationTest {
@Autowired
private SftpService sftpService;
@Autowired
private SshServer sshServer;
private Path testDirectory;
@BeforeEach
void setUp() throws IOException {
// Create test directory
testDirectory = Files.createTempDirectory("sftp-test-files");
// Create test file in SFTP server directory
Path sftpRoot = sshServer.getFileSystemFactory()
.createFileSystem(null)
.getRoot();
Files.write(sftpRoot.resolve("test-file.txt"),
"Test content".getBytes());
}
@Test
void whenDownloadFile_thenFileExistsLocally() throws IOException {
// Given
Path localFile = testDirectory.resolve("downloaded-file.txt");
// When
sftpService.downloadFile("/test-file.txt", localFile);
// Then
assertTrue(Files.exists(localFile));
assertEquals("Test content", Files.readString(localFile));
}
@AfterEach
void tearDown() throws IOException {
// Cleanup test directories
FileSystemUtils.deleteRecursively(testDirectory);
}
}
// application-test.properties
sftp.host=localhost
sftp.port=2222
sftp.username=test-user
sftp.private-key-path=classpath:test-keys/id_rsa
// Generate test keys using command line:
// ssh-keygen -t rsa -b 2048 -f id_rsa -N ""
// Then place id_rsa and id_rsa.pub in src/test/resources/test-keys/