@@ -9,8 +9,7 @@ use rand::{
99 distr:: { Distribution , Uniform } ,
1010 rng,
1111} ;
12- use russh:: client;
13- use russh:: ChannelMsg ;
12+ use russh:: { client, ChannelMsg } ;
1413use russh_sftp:: client:: SftpSession ;
1514use tokio:: io:: { AsyncReadExt , AsyncWriteExt } ;
1615
@@ -45,33 +44,33 @@ pub async fn run_echo_test<H: client::Handler>(
4544 debug ! ( "Running echo test with command: {echo_cmd:?}" ) ;
4645 debug ! ( "Number of characters to echo: {char_count:?}" ) ;
4746 debug ! ( "Time limit for echo: {time_limit:?} seconds" ) ;
48-
47+
4948 // Start the channel server
5049 trace ! ( "Preparing channel session" ) ;
5150 let mut channel = session
5251 . channel_open_session ( )
5352 . await
5453 . map_err ( |e| e. to_string ( ) ) ?;
55-
54+
5655 // Request a pseudo-terminal for the interactive shell
5756 channel
5857 . request_pty ( true , "sshping" , 10 , 5 , 0 , 0 , & [ ] )
5958 . await
6059 . map_err ( |e| e. to_string ( ) ) ?;
61-
60+
6261 channel
6362 . request_shell ( false )
6463 . await
6564 . map_err ( |e| e. to_string ( ) ) ?;
66-
65+
6766 // Send the echo command to accept input
6867 trace ! ( "Starting echo command" ) ;
6968 let echo_cmd_bytes = format ! ( "{echo_cmd}\n " ) . into_bytes ( ) ;
7069 channel
7170 . data ( & echo_cmd_bytes[ ..] )
7271 . await
7372 . map_err ( |e| e. to_string ( ) ) ?;
74-
73+
7574 // Read the initial buffer to clear the echo command
7675 tokio:: time:: sleep ( Duration :: from_millis ( 100 ) ) . await ;
7776 while let Some ( msg) = channel. wait ( ) . await {
@@ -93,14 +92,11 @@ pub async fn run_echo_test<H: client::Handler>(
9392
9493 for ( n, idx) in ( 0 ..char_count) . zip ( ( 0 ..write_buffer. len ( ) ) . cycle ( ) ) {
9594 let start = Instant :: now ( ) ;
96-
95+
9796 // Send one character
9897 let byte_slice = & write_buffer[ idx..idx + 1 ] ;
99- channel
100- . data ( byte_slice)
101- . await
102- . map_err ( |e| e. to_string ( ) ) ?;
103-
98+ channel. data ( byte_slice) . await . map_err ( |e| e. to_string ( ) ) ?;
99+
104100 // Wait for echo back
105101 loop {
106102 if let Some ( msg) = channel. wait ( ) . await {
@@ -117,14 +113,14 @@ pub async fn run_echo_test<H: client::Handler>(
117113 }
118114 }
119115 }
120-
116+
121117 let latency = start. elapsed ( ) . as_nanos ( ) ;
122118 latencies. push ( latency) ;
123-
124- if let Some ( timeout) = timeout {
125- if start_time. elapsed ( ) > timeout {
126- break ;
127- }
119+
120+ if let Some ( timeout) = timeout
121+ && start_time. elapsed ( ) > timeout
122+ {
123+ break ;
128124 }
129125 progress_bar. set_position ( ( n as u64 ) + 1 ) ;
130126 }
@@ -189,7 +185,7 @@ async fn run_upload_test<H: client::Handler>(
189185 formatter : & Formatter ,
190186) -> Result < SpeedTestResult , String > {
191187 info ! ( "Running upload speed test" ) ;
192-
188+
193189 // Establish SFTP channel
194190 trace ! ( "Establishing SFTP channel" ) ;
195191 let channel = session
@@ -203,7 +199,7 @@ async fn run_upload_test<H: client::Handler>(
203199 let sftp = SftpSession :: new ( channel. into_stream ( ) )
204200 . await
205201 . map_err ( |e| e. to_string ( ) ) ?;
206-
202+
207203 // Generate random data to upload
208204 trace ! ( "Generating random data" ) ;
209205 let dist = Uniform :: try_from ( 0 ..128_u8 ) . unwrap ( ) ;
@@ -212,14 +208,11 @@ async fn run_upload_test<H: client::Handler>(
212208 . take ( size as usize )
213209 . map ( |v| ( v & 0x3f ) + 32 )
214210 . collect ( ) ;
215-
211+
216212 // Open remote file for writing
217213 let remote_path = remote_file. to_str ( ) . ok_or ( "Invalid remote file path" ) ?;
218- let mut file = sftp
219- . create ( remote_path)
220- . await
221- . map_err ( |e| e. to_string ( ) ) ?;
222-
214+ let mut file = sftp. create ( remote_path) . await . map_err ( |e| e. to_string ( ) ) ?;
215+
223216 // Preparing logging variables
224217 let mut total_bytes_sent = 0 ;
225218 let start_time: Instant = Instant :: now ( ) ;
@@ -234,7 +227,7 @@ async fn run_upload_test<H: client::Handler>(
234227 progress_bar. set_position ( total_bytes_sent as u64 ) ;
235228 }
236229 progress_bar. finish_and_clear ( ) ;
237-
230+
238231 // Close the file
239232 file. shutdown ( ) . await . map_err ( |e| e. to_string ( ) ) ?;
240233
@@ -254,7 +247,7 @@ async fn run_download_test<H: client::Handler>(
254247 formatter : & Formatter ,
255248) -> Result < SpeedTestResult , String > {
256249 info ! ( "Running download speed test" ) ;
257-
250+
258251 // Establish SFTP channel
259252 trace ! ( "Establishing SFTP channel" ) ;
260253 let channel = session
@@ -268,25 +261,22 @@ async fn run_download_test<H: client::Handler>(
268261 let sftp = SftpSession :: new ( channel. into_stream ( ) )
269262 . await
270263 . map_err ( |e| e. to_string ( ) ) ?;
271-
264+
272265 // Get file size
273266 let remote_path = remote_file. to_str ( ) . ok_or ( "Invalid remote file path" ) ?;
274267 let metadata = sftp
275268 . metadata ( remote_path)
276269 . await
277270 . map_err ( |e| e. to_string ( ) ) ?;
278271 let size = metadata. len ( ) ;
279-
272+
280273 if size == 0 {
281274 return Err ( "Remote file is empty" . to_string ( ) ) ;
282275 }
283-
276+
284277 // Open remote file for reading
285- let mut file = sftp
286- . open ( remote_path)
287- . await
288- . map_err ( |e| e. to_string ( ) ) ?;
289-
278+ let mut file = sftp. open ( remote_path) . await . map_err ( |e| e. to_string ( ) ) ?;
279+
290280 // Prepare buffer for downloading
291281 trace ! ( "Preparing buffer for downloading" ) ;
292282 let mut buffer = vec ! [ 0 ; chunk_size as usize ] ;
@@ -299,13 +289,17 @@ async fn run_download_test<H: client::Handler>(
299289 // Starting downloading file
300290 trace ! ( "Receiving file in chunks" ) ;
301291 while size - total_bytes_recv > chunk_size {
302- file. read_exact ( & mut buffer) . await . map_err ( |e| e. to_string ( ) ) ?;
292+ file. read_exact ( & mut buffer)
293+ . await
294+ . map_err ( |e| e. to_string ( ) ) ?;
303295 total_bytes_recv += chunk_size;
304296 progress_bar. set_position ( total_bytes_recv) ;
305297 }
306298 if size - total_bytes_recv > 0 {
307299 let mut remaining = vec ! [ 0 ; ( size - total_bytes_recv) as usize ] ;
308- file. read_exact ( & mut remaining) . await . map_err ( |e| e. to_string ( ) ) ?;
300+ file. read_exact ( & mut remaining)
301+ . await
302+ . map_err ( |e| e. to_string ( ) ) ?;
309303 total_bytes_recv += remaining. len ( ) as u64 ;
310304 progress_bar. set_position ( total_bytes_recv) ;
311305 }
0 commit comments