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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
use sal_net::SshConnectionBuilder;
use std::path::PathBuf;
use std::time::Duration;
#[tokio::test]
async fn test_ssh_connection_builder_new() {
// Test that builder creates a functional connection with defaults
let connection = SshConnectionBuilder::new().build();
// Test that the connection can actually attempt operations
// Use an invalid host to verify the connection object works but fails as expected
let result = connection.execute("echo test").await;
// Should fail because no host is configured, but the connection object should work
match result {
Ok((exit_code, _)) => assert!(exit_code != 0), // Should fail due to missing host
Err(_) => {} // Error is expected when no host is configured
}
}
#[tokio::test]
async fn test_ssh_connection_builder_host_functionality() {
// Test that setting a host actually affects connection behavior
let connection = SshConnectionBuilder::new()
.host("nonexistent-host-12345.invalid")
.user("testuser")
.timeout(Duration::from_millis(100))
.build();
// This should fail because the host doesn't exist
let result = connection.execute("echo test").await;
match result {
Ok((exit_code, _)) => assert!(exit_code != 0), // Should fail
Err(_) => {} // Error is expected for invalid hosts
}
}
#[tokio::test]
async fn test_ssh_connection_builder_port_functionality() {
// Test that setting a custom port affects connection behavior
let connection = SshConnectionBuilder::new()
.host("127.0.0.1")
.port(12345) // Non-standard SSH port that should be closed
.user("testuser")
.timeout(Duration::from_millis(100))
.build();
// This should fail because port 12345 is not running SSH
let result = connection.ping().await;
match result {
Ok(success) => assert!(!success), // Should fail to connect
Err(_) => {} // Error is expected for closed ports
}
}
#[tokio::test]
async fn test_ssh_connection_builder_user_functionality() {
// Test that setting a user affects connection behavior
let connection = SshConnectionBuilder::new()
.host("127.0.0.1")
.user("nonexistent-user-12345")
.timeout(Duration::from_millis(100))
.build();
// This should fail because the user doesn't exist
let result = connection.execute("whoami").await;
match result {
Ok((exit_code, _)) => assert!(exit_code != 0), // Should fail
Err(_) => {} // Error is expected for invalid users
}
}
#[tokio::test]
async fn test_ssh_connection_builder_identity_file() {
// Test that setting an identity file affects connection behavior
let path = PathBuf::from("/nonexistent/path/to/key");
let connection = SshConnectionBuilder::new()
.host("127.0.0.1")
.user("testuser")
.identity_file(path)
.timeout(Duration::from_millis(100))
.build();
// Test that connection with identity file attempts operations but fails as expected
let result = connection.ping().await;
// Should fail due to invalid key file or authentication, but connection should work
match result {
Ok(success) => assert!(!success), // Should fail due to invalid key or auth
Err(_) => {} // Error is expected for invalid key file
}
}
#[tokio::test]
async fn test_ssh_connection_builder_timeout_functionality() {
// Test that timeout setting actually affects connection behavior
let short_timeout = Duration::from_secs(1); // More reasonable timeout
let connection = SshConnectionBuilder::new()
.host("10.255.255.1") // Non-routable IP to trigger timeout
.timeout(short_timeout)
.build();
let start = std::time::Instant::now();
let result = connection.ping().await;
let elapsed = start.elapsed();
// Should timeout reasonably quickly (within 10 seconds)
assert!(elapsed < Duration::from_secs(10));
match result {
Ok(success) => assert!(!success), // Should timeout/fail
Err(_) => {} // Error is expected for timeouts
}
}
#[tokio::test]
async fn test_ssh_connection_builder_chaining() {
// Test that method chaining works and produces a functional connection
let connection = SshConnectionBuilder::new()
.host("invalid-host-12345.test")
.port(2222)
.user("testuser")
.timeout(Duration::from_millis(100))
.build();
// Test that the chained configuration actually works
let result = connection.ping().await;
match result {
Ok(success) => assert!(!success), // Should fail to connect to invalid host
Err(_) => {} // Error is expected for invalid hosts
}
}
#[tokio::test]
async fn test_ssh_execute_invalid_host() {
let connection = SshConnectionBuilder::new()
.host("this-host-definitely-does-not-exist-12345")
.user("testuser")
.timeout(Duration::from_secs(1))
.build();
let result = connection.execute("echo 'test'").await;
// Should fail because host doesn't exist
// Note: This test depends on SSH client being available
match result {
Ok((exit_code, _output)) => {
// SSH might return various exit codes for connection failures
assert!(exit_code != 0); // Should not succeed
}
Err(_) => {
// Error is also acceptable (SSH client might not be available)
// This is expected behavior for invalid hosts
}
}
}
#[tokio::test]
async fn test_ssh_execute_localhost_no_auth() {
let connection = SshConnectionBuilder::new()
.host("localhost")
.user("nonexistentuser12345")
.timeout(Duration::from_secs(1))
.build();
let result = connection.execute("echo 'test'").await;
// Should fail due to authentication/user issues
match result {
Ok((exit_code, _output)) => {
// SSH should fail with non-zero exit code
assert!(exit_code != 0);
}
Err(_) => {
// Error is also acceptable (SSH client might not be available)
// This is expected behavior for authentication failures
}
}
}
#[tokio::test]
async fn test_ssh_ping_invalid_host() {
let connection = SshConnectionBuilder::new()
.host("this-host-definitely-does-not-exist-12345")
.user("testuser")
.timeout(Duration::from_secs(1))
.build();
let result = connection.ping().await;
match result {
Ok(success) => {
assert!(!success); // Should not succeed
}
Err(_) => {
// Error is also acceptable for invalid hosts
// This is expected behavior
}
}
}
#[tokio::test]
async fn test_ssh_ping_localhost_no_auth() {
let connection = SshConnectionBuilder::new()
.host("localhost")
.user("nonexistentuser12345")
.timeout(Duration::from_secs(1))
.build();
let result = connection.ping().await;
match result {
Ok(success) => {
// Should fail due to authentication issues
assert!(!success);
}
Err(_) => {
// Error is also acceptable for authentication failures
// This is expected behavior
}
}
}
#[tokio::test]
async fn test_ssh_connection_builder_default_values() {
// Test that builder creates connection with reasonable defaults
let connection = SshConnectionBuilder::new().build();
// Test that default connection can attempt operations but fails gracefully
let result = connection.ping().await;
// Should fail because no host is configured, but should handle it gracefully
match result {
Ok(success) => assert!(!success), // Should fail due to missing host
Err(_) => {} // Error is expected when no host is configured
}
}
#[tokio::test]
async fn test_ssh_connection_builder_full_config() {
// Test builder with all options set
let connection = SshConnectionBuilder::new()
.host("nonexistent-host-12345.invalid")
.port(2222)
.user("testuser")
.identity_file(PathBuf::from("/nonexistent/path/to/key"))
.timeout(Duration::from_millis(100))
.build();
// Test that fully configured connection attempts operations but fails as expected
let result = connection.ping().await;
// Should fail because host doesn't exist, but all configuration should be applied
match result {
Ok(success) => assert!(!success), // Should fail due to invalid host
Err(_) => {} // Error is expected for invalid host
}
}
// Integration test that requires actual SSH setup
// This test is disabled by default as it requires SSH server and keys
#[tokio::test]
#[ignore]
async fn test_ssh_execute_real_connection() {
// This test would require:
// 1. SSH server running on localhost
// 2. Valid SSH keys set up
// 3. User account configured
let connection = SshConnectionBuilder::new()
.host("localhost")
.user("testuser") // Replace with actual user
.build();
let result = connection.execute("echo 'Hello from SSH'").await;
match result {
Ok((exit_code, output)) => {
assert_eq!(exit_code, 0);
assert!(output.contains("Hello from SSH"));
}
Err(e) => {
panic!("SSH execution failed: {}", e);
}
}
}