use super::control::{RawFtpControl, parse_epsv_response, parse_pasv_response, urlencoding_decode};
use super::types::FtpDownloadCommand;
use crate::request::request_group::{DownloadOptions, GroupId};
#[test]
fn test_parse_uri_simple() {
let uri = "ftp://example.com/file.txt";
let result = FtpDownloadCommand::parse_uri(uri).unwrap();
assert_eq!(result.0, "example.com");
assert_eq!(result.1, 21);
assert_eq!(result.2, "anonymous");
assert_eq!(result.3, "aria2@");
assert_eq!(result.4, "/file.txt");
}
#[test]
fn test_retry_policy_comes_from_download_options() {
let options = DownloadOptions {
max_retries: 7,
retry_wait: 3,
..DownloadOptions::default()
};
let command = FtpDownloadCommand::new(
GroupId::new(101),
"ftp://example.com/file.txt",
&options,
None,
None,
)
.unwrap();
assert_eq!(command.retry_policy.max_tries(), 7);
assert_eq!(command.retry_policy.base_wait_ms, 3000);
}
#[test]
fn test_parse_uri_with_port() {
let uri = "ftp://example.com:2121/file.txt";
let result = FtpDownloadCommand::parse_uri(uri).unwrap();
assert_eq!(result.0, "example.com");
assert_eq!(result.1, 2121);
}
#[test]
fn test_parse_uri_with_auth() {
let uri = "ftp://user:pass@example.com/file.txt";
let result = FtpDownloadCommand::parse_uri(uri).unwrap();
assert_eq!(result.2, "user");
assert_eq!(result.3, "pass");
}
#[test]
fn test_parse_uri_with_encoded_chars() {
let uri = "ftp://example.com/my%20file.txt";
let result = FtpDownloadCommand::parse_uri(uri).unwrap();
assert_eq!(result.4, "/my file.txt");
}
#[test]
fn test_parse_uri_invalid_protocol() {
let uri = "http://example.com/file.txt";
let result = FtpDownloadCommand::parse_uri(uri);
assert!(result.is_err());
}
#[test]
fn test_extract_filename_from_path() {
assert_eq!(
FtpDownloadCommand::extract_filename("/path/to/file.txt"),
Some("file.txt".to_string())
);
assert_eq!(
FtpDownloadCommand::extract_filename("/file.txt"),
Some("file.txt".to_string())
);
assert_eq!(FtpDownloadCommand::extract_filename("/"), None);
assert_eq!(FtpDownloadCommand::extract_filename(""), None);
}
#[test]
fn test_urlencoding_decode() {
assert_eq!(urlencoding_decode("hello%20world"), "hello world");
assert_eq!(urlencoding_decode("%2F"), "/");
assert_eq!(urlencoding_decode("normal"), "normal");
assert_eq!(urlencoding_decode("%41"), "A");
}
#[test]
fn test_urlencoding_decode_utf8() {
assert_eq!(urlencoding_decode("%E6%96%87%E4%BB%B6"), "文件");
}
#[test]
fn test_parse_pasv_response_standard() {
let resp = "227 Entering Passive Mode (192,168,1,100,200,10)";
let result = parse_pasv_response(resp).unwrap();
assert_eq!(result.0, "192.168.1.100");
assert_eq!(result.1, 200 * 256 + 10); }
#[test]
fn test_parse_pasv_response_minimal() {
let resp = "(10,0,0,1,0,21)";
let result = parse_pasv_response(resp).unwrap();
assert_eq!(result.0, "10.0.0.1");
assert_eq!(result.1, 21);
}
#[test]
fn test_parse_pasv_response_invalid() {
assert!(parse_pasv_response("no parentheses").is_none());
assert!(parse_pasv_response("(1,2,3)").is_none()); }
#[test]
fn test_parse_epsv_response_standard() {
let resp = "229 Entering Extended Passive Mode (|||50001|)";
let result = parse_epsv_response(resp).unwrap();
assert_eq!(result, 50001);
}
#[test]
fn test_parse_epsv_response_minimal() {
let resp = "|||60000|";
let result = parse_epsv_response(resp).unwrap();
assert_eq!(result, 60000);
}
#[test]
fn test_classify_ftp_error_transient() {
let transient_codes = [421u16, 425, 426, 450, 451, 452];
for code in transient_codes {
assert!(
(400..=499).contains(&code),
"Code {} should be in transient range",
code
);
}
}
#[test]
fn test_classify_ftp_error_permanent() {
let permanent_codes = [500u16, 501, 502, 503, 504, 530, 550, 553];
for code in permanent_codes {
assert!(
(500..=599).contains(&code),
"Code {} should be in permanent range",
code
);
}
}
#[test]
fn test_resume_offset_calculation() {
let path = std::path::PathBuf::from("/tmp/test_file");
if path.exists() {
let metadata = std::fs::metadata(&path).unwrap();
let _offset = metadata.len();
} else {
assert_eq!(0u64, 0);
}
}
#[tokio::test]
async fn test_raw_ftp_control_connect_invalid_address() {
let result = RawFtpControl::connect_at(
"invalid.host.name.invalid",
21,
"127.0.0.1:0".parse().unwrap(),
)
.await;
assert!(result.is_err());
}