aria2-core 0.3.11

aria2-rust core download engine: multi-protocol segmented downloads, rate limiting, configuration, session persistence, and BitTorrent seeding
Documentation
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
//! FTP-over-HTTP-proxy CONNECT tunnel establishment
//!
//! Implements the HTTP CONNECT method for tunneling FTP through an
//! HTTP proxy. The flow is:
//!
//! 1. Connect to the HTTP proxy server
//! 2. Send `CONNECT host:port HTTP/1.1` request
//! 3. Optionally handle 407 Proxy Authentication Required
//! 4. On 200 Connection Established, the socket becomes a tunnel
//! 5. Normal FTP negotiation proceeds over the tunneled connection
//!
//! This is equivalent to the C++ `FtpTunnelRequestCommand` +
//! `FtpTunnelResponseCommand` + `AbstractProxyRequestCommand` +
//! `AbstractProxyResponseCommand` chain, collapsed into a single
//! async function using Rust's async/await model.

use std::time::Duration;

use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
use tokio::time::timeout;
use tracing::{debug, info, warn};

use crate::error::{Aria2Error, RecoverableError, Result};
use crate::network::OutboundNetworkPolicy;

/// Configuration for establishing an FTP-over-HTTP-proxy tunnel.
#[derive(Debug, Clone)]
pub struct FtpProxyTunnelConfig {
    /// Proxy server hostname
    pub proxy_host: String,
    /// Proxy server port
    pub proxy_port: u16,
    /// Target FTP server hostname
    pub target_host: String,
    /// Target FTP server port
    pub target_port: u16,
    /// Proxy authentication username (empty if no auth)
    pub proxy_username: String,
    /// Proxy authentication password (empty if no auth)
    pub proxy_password: String,
    /// Connection timeout for connecting to the proxy
    pub connect_timeout: Duration,
    /// Read timeout for proxy response
    pub read_timeout: Duration,
    /// User-Agent header to send
    pub user_agent: String,
}

impl Default for FtpProxyTunnelConfig {
    fn default() -> Self {
        Self {
            proxy_host: String::new(),
            proxy_port: 8080,
            target_host: String::new(),
            target_port: 21,
            proxy_username: String::new(),
            proxy_password: String::new(),
            connect_timeout: Duration::from_secs(30),
            read_timeout: Duration::from_secs(60),
            user_agent: crate::constants::USER_AGENT.to_string(),
        }
    }
}

/// Result of a successful proxy tunnel establishment.
pub struct FtpProxyTunnelResult {
    /// The tunneled TCP stream (now behaves as a direct connection to the FTP server)
    pub stream: TcpStream,
}

/// FTP proxy tunnel handler.
///
/// Establishes an HTTP CONNECT tunnel through a proxy server for
/// FTP protocol. After the tunnel is established, the returned
/// `TcpStream` can be used for normal FTP negotiation.
///
/// # C++ Equivalent
///
/// This replaces the C++ command chain:
/// - `FtpTunnelRequestCommand` (sends CONNECT request)
/// - `AbstractProxyRequestCommand` (handles proxy auth)
/// - `FtpTunnelResponseCommand` (reads 200 response)
/// - `AbstractProxyResponseCommand` (validates response)
pub struct FtpProxyTunnel;

impl FtpProxyTunnel {
    /// Establish an FTP-over-HTTP-proxy tunnel using the CONNECT method.
    ///
    /// # Arguments
    ///
    /// * `config` - Configuration for the proxy tunnel
    ///
    /// # Returns
    ///
    /// A `TcpStream` that is tunneled through the proxy to the target
    /// FTP server. This stream can be used directly for FTP negotiation.
    ///
    /// # Errors
    ///
    /// - If the proxy connection fails
    /// - If the proxy rejects the CONNECT request (non-200 response)
    /// - If proxy authentication fails after retry
    /// - If timeout occurs
    pub async fn establish(config: &FtpProxyTunnelConfig) -> Result<TcpStream> {
        Self::establish_with_policy(config, &OutboundNetworkPolicy::direct()).await
    }

    /// Establish a tunnel through the shared outbound policy.
    pub async fn establish_with_policy(
        config: &FtpProxyTunnelConfig,
        policy: &OutboundNetworkPolicy,
    ) -> Result<TcpStream> {
        // Step 1: Connect to the proxy server
        debug!(
            "Connecting to HTTP proxy {}:{} for FTP tunnel to {}:{}",
            config.proxy_host, config.proxy_port, config.target_host, config.target_port
        );

        let connect = async {
            if policy.is_direct() {
                TcpStream::connect((config.proxy_host.as_str(), config.proxy_port))
                    .await
                    .map_err(|error| Aria2Error::Network(error.to_string()))
            } else {
                policy
                    .connect_host(&config.proxy_host, config.proxy_port)
                    .await
                    .map_err(|error| Aria2Error::Network(error.to_string()))
            }
        };
        let mut stream = timeout(config.connect_timeout, connect)
            .await
            .map_err(|_| Aria2Error::Recoverable(RecoverableError::Timeout))?
            .map_err(|e: Aria2Error| {
                Aria2Error::Recoverable(RecoverableError::TemporaryNetworkFailure {
                    message: format!(
                        "Failed to connect to proxy {}:{}: {}",
                        config.proxy_host, config.proxy_port, e
                    ),
                })
            })?;

        stream.set_nodelay(true).map_err(|e| {
            Aria2Error::Recoverable(RecoverableError::TemporaryNetworkFailure {
                message: format!("set_nodelay failed: {}", e),
            })
        })?;

        // Step 2: Send CONNECT request (without auth first)
        let connect_request = Self::build_connect_request(config, None);
        Self::send_request(&mut stream, &connect_request, config.write_timeout()).await?;

        // Step 3: Read proxy response
        let response = Self::read_proxy_response(&mut stream, config.read_timeout).await?;

        match response.status_code {
            200 => {
                // Tunnel established successfully
                info!(
                    "FTP proxy tunnel established: {}:{} -> {}:{}",
                    config.proxy_host, config.proxy_port, config.target_host, config.target_port
                );
                Ok(stream)
            }
            407 => {
                // Proxy authentication required
                Self::handle_proxy_auth(stream, config, &response).await
            }
            status => {
                // Proxy rejected the CONNECT request
                Err(Aria2Error::Recoverable(
                    RecoverableError::TemporaryNetworkFailure {
                        message: format!(
                            "Proxy {}:{} rejected CONNECT to {}:{}: {} {}",
                            config.proxy_host,
                            config.proxy_port,
                            config.target_host,
                            config.target_port,
                            status,
                            response.reason_phrase
                        ),
                    },
                ))
            }
        }
    }

    /// Handle 407 Proxy Authentication Required response.
    ///
    /// Parses the Proxy-Authenticate header and retries the CONNECT
    /// request with appropriate credentials.
    async fn handle_proxy_auth(
        mut stream: TcpStream,
        config: &FtpProxyTunnelConfig,
        response: &ProxyResponse,
    ) -> Result<TcpStream> {
        if config.proxy_username.is_empty() {
            return Err(Aria2Error::Recoverable(
                RecoverableError::TemporaryNetworkFailure {
                    message: format!(
                        "Proxy {}:{} requires authentication but no credentials provided",
                        config.proxy_host, config.proxy_port
                    ),
                },
            ));
        }

        // Consume any remaining response body
        Self::consume_response_body(&mut stream, config.read_timeout).await?;

        // Check what auth scheme the proxy supports
        let proxy_authenticate = response
            .headers
            .iter()
            .find(|(k, _)| k.eq_ignore_ascii_case("Proxy-Authenticate"))
            .map(|(_, v)| v.as_str())
            .unwrap_or("");

        let auth_header = if proxy_authenticate.starts_with("Basic") {
            // Basic authentication
            Self::build_basic_auth_header(&config.proxy_username, &config.proxy_password)
        } else if proxy_authenticate.starts_with("Digest") {
            // Digest authentication
            Self::build_digest_auth_header(
                &config.proxy_username,
                &config.proxy_password,
                proxy_authenticate,
                &format!("{}:{}", config.target_host, config.target_port),
            )
        } else {
            // Unknown or unsupported auth scheme
            warn!(
                "Unsupported proxy auth scheme: {}",
                proxy_authenticate
                    .split_whitespace()
                    .next()
                    .unwrap_or("unknown")
            );
            return Err(Aria2Error::Recoverable(
                RecoverableError::TemporaryNetworkFailure {
                    message: format!(
                        "Proxy requires unsupported authentication scheme: {}",
                        proxy_authenticate
                    ),
                },
            ));
        };

        // Retry CONNECT with auth
        let connect_request = Self::build_connect_request(config, Some(&auth_header));
        Self::send_request(&mut stream, &connect_request, config.write_timeout()).await?;

        let retry_response = Self::read_proxy_response(&mut stream, config.read_timeout).await?;

        match retry_response.status_code {
            200 => {
                info!(
                    "FTP proxy tunnel established with authentication: {}:{} -> {}:{}",
                    config.proxy_host, config.proxy_port, config.target_host, config.target_port
                );
                Ok(stream)
            }
            407 => Err(Aria2Error::Recoverable(
                RecoverableError::TemporaryNetworkFailure {
                    message: format!(
                        "Proxy authentication failed for {}:{}",
                        config.proxy_host, config.proxy_port
                    ),
                },
            )),
            status => Err(Aria2Error::Recoverable(
                RecoverableError::TemporaryNetworkFailure {
                    message: format!(
                        "Proxy rejected CONNECT after auth: {} {}",
                        status, retry_response.reason_phrase
                    ),
                },
            )),
        }
    }

    /// Build the HTTP CONNECT request string.
    fn build_connect_request(config: &FtpProxyTunnelConfig, auth_header: Option<&str>) -> String {
        let mut request = format!(
            "CONNECT {}:{} HTTP/1.1\r\n\
             Host: {}:{}\r\n\
             User-Agent: {}\r\n",
            config.target_host,
            config.target_port,
            config.target_host,
            config.target_port,
            config.user_agent
        );

        if let Some(auth) = auth_header {
            request.push_str(&format!("Proxy-Authorization: {}\r\n", auth));
        }

        request.push_str("\r\n");
        request
    }

    /// Send the CONNECT request to the proxy.
    async fn send_request(
        stream: &mut TcpStream,
        request: &str,
        write_timeout: Duration,
    ) -> Result<()> {
        debug!("Sending CONNECT request to proxy:\n{}", request.trim_end());
        timeout(write_timeout, stream.write_all(request.as_bytes()))
            .await
            .map_err(|_| Aria2Error::Recoverable(RecoverableError::Timeout))?
            .map_err(|e| {
                Aria2Error::Recoverable(RecoverableError::TemporaryNetworkFailure {
                    message: format!("Failed to send CONNECT request: {}", e),
                })
            })?;
        stream.flush().await.map_err(|e| {
            Aria2Error::Recoverable(RecoverableError::TemporaryNetworkFailure {
                message: format!("Failed to flush CONNECT request: {}", e),
            })
        })?;
        Ok(())
    }

    /// Read the proxy's HTTP response to the CONNECT request.
    async fn read_proxy_response(
        stream: &mut TcpStream,
        read_timeout: Duration,
    ) -> Result<ProxyResponse> {
        let mut buf_reader = tokio::io::BufReader::new(stream);
        let response = timeout(read_timeout, Self::parse_response(&mut buf_reader))
            .await
            .map_err(|_| Aria2Error::Recoverable(RecoverableError::Timeout))??;

        // We need to get the stream back from buf_reader
        // Since we borrowed it, the buffered data is already consumed
        Ok(response)
    }

    /// Parse an HTTP response from the proxy.
    async fn parse_response<R: AsyncBufReadExt + Unpin>(reader: &mut R) -> Result<ProxyResponse> {
        // Parse status line
        let mut status_line = String::new();
        reader.read_line(&mut status_line).await.map_err(|e| {
            Aria2Error::Recoverable(RecoverableError::TemporaryNetworkFailure {
                message: format!("Failed to read proxy response: {}", e),
            })
        })?;

        let status_line = status_line.trim_end();
        debug!("Proxy response status: {}", status_line);

        // Parse: HTTP/x.x status_code reason_phrase
        let parts: Vec<&str> = status_line.splitn(3, ' ').collect();
        if parts.len() < 2 {
            return Err(Aria2Error::Parse(format!(
                "Malformed proxy response: {}",
                status_line
            )));
        }

        let status_code: u16 = parts[1]
            .parse()
            .map_err(|_| Aria2Error::Parse(format!("Invalid proxy status code: {}", parts[1])))?;

        let reason_phrase = parts.get(2).unwrap_or(&"").to_string();

        // Parse headers
        let mut headers = Vec::new();
        loop {
            let mut header_line = String::new();
            reader.read_line(&mut header_line).await.map_err(|e| {
                Aria2Error::Recoverable(RecoverableError::TemporaryNetworkFailure {
                    message: format!("Failed to read proxy headers: {}", e),
                })
            })?;

            let trimmed = header_line.trim_end();
            if trimmed.is_empty() {
                break; // End of headers
            }

            if let Some(colon_pos) = trimmed.find(':') {
                let name = trimmed[..colon_pos].trim().to_string();
                let value = trimmed[colon_pos + 1..].trim().to_string();
                headers.push((name, value));
            }
        }

        Ok(ProxyResponse {
            status_code,
            reason_phrase,
            headers,
        })
    }

    /// Consume any response body data (for 407 responses).
    async fn consume_response_body(stream: &mut TcpStream, _read_timeout: Duration) -> Result<()> {
        // Read any remaining data with a short timeout
        let mut buf = [0u8; 4096];
        match timeout(Duration::from_secs(2), stream.read(&mut buf)).await {
            Ok(Ok(n)) => {
                debug!("Consumed {} bytes of proxy response body", n);
                Ok(())
            }
            Ok(Err(e)) => {
                debug!("Error consuming proxy response body: {}", e);
                Ok(())
            }
            Err(_) => {
                debug!("Timeout consuming proxy response body (acceptable)");
                Ok(())
            }
        }
    }

    /// Build a Basic proxy-auth header value.
    fn build_basic_auth_header(username: &str, password: &str) -> String {
        use base64::Engine;
        let credentials = format!("{}:{}", username, password);
        let encoded = base64::engine::general_purpose::STANDARD.encode(credentials.as_bytes());
        format!("Basic {}", encoded)
    }

    /// Build a Digest proxy-auth header value.
    ///
    /// Parses the Proxy-Authenticate challenge and generates
    /// a Proxy-Authorization response.
    fn build_digest_auth_header(
        username: &str,
        password: &str,
        challenge: &str,
        uri: &str,
    ) -> String {
        // Parse digest challenge parameters
        let realm = extract_digest_param(challenge, "realm").unwrap_or("unknown");
        let nonce = extract_digest_param(challenge, "nonce").unwrap_or("");
        let opaque = extract_digest_param(challenge, "opaque");
        let qop = extract_digest_param(challenge, "qop");
        let algorithm = extract_digest_param(challenge, "algorithm")
            .unwrap_or("MD5")
            .to_uppercase();

        if nonce.is_empty() {
            return Self::build_basic_auth_header(username, password);
        }

        // Compute digest response
        let ha1 = if algorithm == "MD5-SESS" {
            let ha1_base = md5_hex(&format!("{}:{}:{}", username, realm, password));
            md5_hex(&format!("{}:{}:{}", ha1_base, nonce, "00000001"))
        } else {
            md5_hex(&format!("{}:{}:{}", username, realm, password))
        };

        let (ha2, qop_value) = if qop == Some("auth") || qop == Some("auth-int") {
            let ha2 = md5_hex(&format!("CONNECT:{}", uri));
            (ha2, "auth")
        } else {
            let ha2 = md5_hex(&format!("CONNECT:{}", uri));
            (ha2, "")
        };

        let response = if qop_value.is_empty() {
            md5_hex(&format!("{}:{}:{}", ha1, nonce, ha2))
        } else {
            let cnonce = "aria2rustcnonce";
            let nc = "00000001";
            md5_hex(&format!(
                "{}:{}:{}:{}:{}:{}",
                ha1, nonce, nc, cnonce, qop_value, ha2
            ))
        };

        let mut header = format!(
            "Digest username=\"{}\", realm=\"{}\", nonce=\"{}\", uri=\"{}\", response=\"{}\"",
            username, realm, nonce, uri, response
        );

        if let Some(opaque) = opaque {
            header.push_str(&format!(", opaque=\"{}\"", opaque));
        }

        if !qop_value.is_empty() {
            header.push_str(&format!(
                ", qop={}, nc=00000001, cnonce=\"{}\"",
                qop_value, "aria2rustcnonce"
            ));
        }

        header.push_str(&format!(", algorithm={}", algorithm));

        header
    }
}

impl FtpProxyTunnelConfig {
    /// Calculate write timeout from connect_timeout.
    fn write_timeout(&self) -> Duration {
        self.connect_timeout
    }
}

/// Parsed HTTP response from the proxy.
#[derive(Debug)]
struct ProxyResponse {
    /// HTTP status code (e.g., 200, 407)
    status_code: u16,
    /// Reason phrase (e.g., "Connection Established")
    reason_phrase: String,
    /// Response headers as (name, value) pairs
    headers: Vec<(String, String)>,
}

/// Extract a parameter value from a Digest challenge string.
///
/// Parses strings like: `Digest realm="example.com", nonce="abc123"`
fn extract_digest_param<'a>(challenge: &'a str, param: &str) -> Option<&'a str> {
    let search = format!("{}=", param);
    // Split on commas, skipping the "Digest" scheme prefix
    let params_section = if let Some(pos) = challenge.find(' ') {
        &challenge[pos + 1..]
    } else {
        challenge
    };

    for part in params_section.split(',') {
        let part = part.trim();
        if let Some(rest) = part.strip_prefix(&search) {
            // Handle quoted values
            if let Some(stripped) = rest.strip_prefix('"') {
                if let Some(end) = stripped.find('"') {
                    return Some(&stripped[..end]);
                }
            } else {
                // Unquoted value - take until comma or end
                let end = rest.find(',').unwrap_or(rest.len());
                return Some(&rest[..end]);
            }
        }
    }
    None
}

/// Compute MD5 hex digest.
fn md5_hex(input: &str) -> String {
    use md5::{Digest, Md5};
    let mut hasher = Md5::new();
    hasher.update(input.as_bytes());
    let result = hasher.finalize();
    format!("{:x}", result)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn proxy_tunnel_binds_configured_source_address() {
        use tokio::io::{AsyncReadExt, AsyncWriteExt};
        use tokio::net::TcpListener;

        let listener = TcpListener::bind((std::net::Ipv4Addr::LOCALHOST, 0))
            .await
            .expect("proxy tunnel fixture should bind");
        let address = listener.local_addr().expect("proxy tunnel fixture address");
        let server = tokio::spawn(async move {
            let (mut stream, peer) = listener.accept().await.expect("proxy should accept");
            let mut request = [0u8; 4096];
            let bytes = stream
                .read(&mut request)
                .await
                .expect("read CONNECT request");
            assert!(
                String::from_utf8_lossy(&request[..bytes])
                    .starts_with("CONNECT ftp.example.com:21 HTTP/1.1\r\n")
            );
            stream
                .write_all(b"HTTP/1.1 200 Connection Established\r\n\r\n")
                .await
                .expect("write CONNECT response");
            peer
        });

        let config = FtpProxyTunnelConfig {
            proxy_host: "localhost".to_string(),
            proxy_port: address.port(),
            target_host: "ftp.example.com".to_string(),
            target_port: 21,
            ..Default::default()
        };
        let stream = FtpProxyTunnel::establish_with_policy(
            &config,
            &OutboundNetworkPolicy::single(std::net::IpAddr::V4(std::net::Ipv4Addr::new(
                127, 0, 0, 2,
            ))),
        )
        .await
        .expect("proxy tunnel should use the configured source");

        assert_eq!(
            server
                .await
                .expect("proxy tunnel fixture should finish")
                .ip(),
            stream.local_addr().expect("read tunnel local address").ip()
        );
        assert_eq!(
            stream.local_addr().expect("read tunnel local address").ip(),
            std::net::IpAddr::V4(std::net::Ipv4Addr::new(127, 0, 0, 2))
        );
    }

    #[test]
    fn test_build_connect_request_without_auth() {
        let config = FtpProxyTunnelConfig {
            proxy_host: "proxy.example.com".to_string(),
            proxy_port: 8080,
            target_host: "ftp.example.com".to_string(),
            target_port: 21,
            user_agent: crate::constants::USER_AGENT.to_string(),
            ..Default::default()
        };

        let request = FtpProxyTunnel::build_connect_request(&config, None);
        assert!(request.starts_with("CONNECT ftp.example.com:21 HTTP/1.1\r\n"));
        assert!(request.contains("Host: ftp.example.com:21\r\n"));
        assert!(request.contains(&format!("User-Agent: {}\r\n", crate::constants::USER_AGENT)));
        assert!(!request.contains("Proxy-Authorization"));
        assert!(request.ends_with("\r\n\r\n"));
    }

    #[test]
    fn test_build_connect_request_with_auth() {
        let config = FtpProxyTunnelConfig {
            target_host: "ftp.example.com".to_string(),
            target_port: 21,
            ..Default::default()
        };

        let request = FtpProxyTunnel::build_connect_request(&config, Some("Basic dXNlcjpwYXNz"));
        assert!(request.contains("Proxy-Authorization: Basic dXNlcjpwYXNz\r\n"));
    }

    #[test]
    fn test_build_basic_auth_header() {
        let header = FtpProxyTunnel::build_basic_auth_header("user", "pass");
        assert!(header.starts_with("Basic "));
        // Base64 of "user:pass" is "dXNlcjpwYXNz"
        assert_eq!(header, "Basic dXNlcjpwYXNz");
    }

    #[test]
    fn test_extract_digest_param() {
        let challenge = r#"Digest realm="testrealm", nonce="abc123", qop="auth""#;
        assert_eq!(extract_digest_param(challenge, "realm"), Some("testrealm"));
        assert_eq!(extract_digest_param(challenge, "nonce"), Some("abc123"));
        assert_eq!(extract_digest_param(challenge, "qop"), Some("auth"));
        assert_eq!(extract_digest_param(challenge, "opaque"), None);
    }

    #[test]
    fn test_extract_digest_param_unquoted() {
        let challenge = "Digest algorithm=MD5, realm=\"test\"";
        assert_eq!(extract_digest_param(challenge, "algorithm"), Some("MD5"));
        assert_eq!(extract_digest_param(challenge, "realm"), Some("test"));
    }

    #[test]
    fn test_md5_hex() {
        // Known MD5 value
        assert_eq!(md5_hex(""), "d41d8cd98f00b204e9800998ecf8427e");
        assert_eq!(md5_hex("hello"), "5d41402abc4b2a76b9719d911017c592");
    }

    #[test]
    fn test_proxy_tunnel_config_default() {
        let config = FtpProxyTunnelConfig::default();
        assert_eq!(config.proxy_port, 8080);
        assert_eq!(config.target_port, 21);
        assert!(config.proxy_username.is_empty());
        assert!(config.proxy_password.is_empty());
    }

    #[test]
    fn test_build_digest_auth_header() {
        let challenge =
            r#"Digest realm="test@example.com", nonce="abc123", qop="auth", algorithm=MD5"#;
        let header = FtpProxyTunnel::build_digest_auth_header(
            "user",
            "pass",
            challenge,
            "ftp.example.com:21",
        );
        assert!(header.starts_with("Digest"));
        assert!(header.contains("username=\"user\""));
        assert!(header.contains("realm=\"test@example.com\""));
        assert!(header.contains("nonce=\"abc123\""));
        assert!(header.contains("response=\""));
    }
}