aria2-core 0.3.13

aria2-rust core download engine: multi-protocol segmented downloads, rate limiting, configuration, session persistence, and BitTorrent seeding
Documentation
use super::{DownloadOptions, FollowMode, HttpAdaptiveOptions};
use std::collections::HashMap;

#[test]
fn follow_mode_preserves_all_wire_values() {
    assert_eq!(FollowMode::parse("true"), Some(FollowMode::Follow));
    assert_eq!(FollowMode::parse("false"), Some(FollowMode::Disabled));
    assert_eq!(FollowMode::parse("mem"), Some(FollowMode::Memory));
    assert_eq!(FollowMode::parse("invalid"), None);
    assert_eq!(FollowMode::Memory.as_str(), "mem");
}

#[test]
fn option_map_keeps_memory_follow_mode() {
    let mut values = HashMap::new();
    values.insert("follow-torrent".to_string(), "mem".to_string());
    values.insert("follow-metalink".to_string(), "false".to_string());

    let options = DownloadOptions::from_option_strings(&values);
    assert_eq!(options.follow_torrent, Some(FollowMode::Memory));
    assert_eq!(options.follow_metalink, Some(FollowMode::Disabled));
    assert!(options.uses_memory_download());
}

#[test]
fn split_option_keeps_large_positive_values() {
    let values = HashMap::from([("split".to_string(), "1000000".to_string())]);
    assert_eq!(
        DownloadOptions::from_option_strings(&values).split,
        Some(1_000_000)
    );
}

#[test]
fn http_user_agent_matches_explicit_header_precedence() {
    let options = DownloadOptions {
        header: vec![
            "x-extra: value".to_string(),
            "uSeR-aGeNt: header/1.0".to_string(),
        ],
        user_agent: Some("option/1.0".to_string()),
        ..DownloadOptions::default()
    };
    assert_eq!(options.http_user_agent(), Some("header/1.0"));

    let options = DownloadOptions {
        user_agent: Some("option/1.0".to_string()),
        ..DownloadOptions::default()
    };
    assert_eq!(options.http_user_agent(), Some("option/1.0"));
    assert_eq!(DownloadOptions::default().http_user_agent(), None);
}

#[test]
fn adaptive_http_options_parse_and_normalize_external_values() {
    let values = HashMap::from([
        ("http-adaptive-enabled".to_string(), "false".to_string()),
        (
            "http-adaptive-reduction-percent".to_string(),
            "30".to_string(),
        ),
        (
            "http-adaptive-recovery-successes-min".to_string(),
            "40".to_string(),
        ),
        (
            "http-adaptive-recovery-successes-max".to_string(),
            "12".to_string(),
        ),
        (
            "http-adaptive-start-interval-ms".to_string(),
            "250".to_string(),
        ),
        ("http-adaptive-backoff-base".to_string(), "15".to_string()),
        ("http-adaptive-backoff-max".to_string(), "10".to_string()),
    ]);

    let options = DownloadOptions::from_option_strings(&values).http_adaptive;
    assert!(!options.enabled);
    assert_eq!(options.reduction_percent, 30);
    assert_eq!(options.recovery_successes_min, 40);
    assert_eq!(options.recovery_successes_max, 40);
    assert_eq!(options.start_interval_ms, 250);
    assert_eq!(options.backoff_base_secs, 15);
    assert_eq!(options.backoff_max_secs, 15);
}

#[test]
fn adaptive_http_concurrency_defaults_to_disabled() {
    assert!(!HttpAdaptiveOptions::default().enabled);
    assert!(
        !DownloadOptions::from_option_strings(&HashMap::new())
            .http_adaptive
            .enabled
    );
}

#[test]
fn memory_follow_mode_is_limited_to_metadata_sources() {
    let options = DownloadOptions {
        follow_torrent: Some(FollowMode::Memory),
        follow_metalink: Some(FollowMode::Memory),
        ..DownloadOptions::default()
    };

    assert!(!options.uses_memory_download_for_uri("https://example.test/file.bin"));
    assert!(
        options.uses_memory_download_for_uri(
            "https://example.test/source.torrent?download=1#metadata"
        )
    );
    assert!(options.uses_memory_download_for_uri("https://example.test/index.meta4"));
    assert!(options.uses_memory_download_for_uri("/tmp/index.metalink3"));
}

#[test]
fn memory_follow_mode_matches_metadata_content_types() {
    let options = DownloadOptions {
        follow_torrent: Some(FollowMode::Memory),
        follow_metalink: Some(FollowMode::Memory),
        ..DownloadOptions::default()
    };

    assert!(
        options.uses_memory_download_for_content_type("application/x-bittorrent; charset=binary")
    );
    assert!(options.uses_memory_download_for_content_type("application/metalink4+xml"));
    assert!(!options.uses_memory_download_for_content_type("application/octet-stream"));
}

#[test]
fn proxy_credentials_prefer_protocol_specific_values() {
    let options = DownloadOptions {
        http_proxy: Some("http://url-user:url-pass@http-proxy.example:8080".to_string()),
        https_proxy: Some("http://url-user:url-pass@https-proxy.example:8080".to_string()),
        all_proxy: Some("http://url-user:url-pass@all-proxy.example:8080".to_string()),
        http_proxy_user: Some("http-user".to_string()),
        http_proxy_passwd: Some("http-pass".to_string()),
        https_proxy_user: Some("https-user".to_string()),
        all_proxy_user: Some("all-user".to_string()),
        all_proxy_passwd: Some("all-pass".to_string()),
        ..DownloadOptions::default()
    };

    assert_eq!(
        options.proxy_credentials_for_scheme("http"),
        (Some("http-user".to_string()), Some("http-pass".to_string()))
    );
    assert_eq!(
        options.proxy_credentials_for_scheme("https"),
        (Some("https-user".to_string()), Some("url-pass".to_string()))
    );
    assert_eq!(
        options.proxy_credentials_for_scheme("all"),
        (Some("all-user".to_string()), Some("all-pass".to_string()))
    );
}

#[test]
fn protocol_credentials_do_not_cross_to_all_proxy_or_vice_versa() {
    let options = DownloadOptions {
        all_proxy: Some("http://all-url-user:all-url-pass@all-proxy.example:8080".into()),
        all_proxy_user: Some("all-user".into()),
        all_proxy_passwd: Some("all-pass".into()),
        http_proxy_user: Some("http-user".into()),
        http_proxy_passwd: Some("http-pass".into()),
        ..DownloadOptions::default()
    };

    assert_eq!(
        options.proxy_credentials_for_scheme("http"),
        (Some("all-user".into()), Some("all-pass".into()))
    );
    assert_eq!(
        options.proxy_credentials_for_scheme("all"),
        (Some("all-user".into()), Some("all-pass".into()))
    );
}

#[test]
fn proxy_credentials_fall_back_to_embedded_proxy_url_values() {
    let options = DownloadOptions {
        http_proxy: Some("http://url-user:url-pass@proxy.example:8080".to_string()),
        ..DownloadOptions::default()
    };

    assert_eq!(
        options.proxy_credentials_for_scheme("http"),
        (Some("url-user".to_string()), Some("url-pass".to_string()))
    );
}

#[test]
fn explicit_proxy_credentials_override_embedded_proxy_url_values() {
    let options = DownloadOptions {
        http_proxy: Some("http://url-user:url-pass@proxy.example:8080".to_string()),
        http_proxy_user: Some("option-user".to_string()),
        http_proxy_passwd: Some("option-pass".to_string()),
        ..DownloadOptions::default()
    };

    assert_eq!(
        options.proxy_credentials_for_scheme("http"),
        (
            Some("option-user".to_string()),
            Some("option-pass".to_string())
        )
    );
}

#[test]
fn embedded_proxy_credentials_follow_non_empty_proxy_fallback() {
    let options = DownloadOptions {
        http_proxy: Some(String::new()),
        all_proxy: Some("http://all-user:all-pass@proxy.example:8080".to_string()),
        ..DownloadOptions::default()
    };

    assert_eq!(
        options.proxy_credentials_for_scheme("http"),
        (Some("all-user".to_string()), Some("all-pass".to_string()))
    );
}

#[cfg(feature = "bittorrent")]
#[test]
fn rpc_option_map_uses_aria2_wire_strings() {
    let mut values = HashMap::new();
    values.insert("max-download-limit".to_string(), serde_json::json!("100K"));
    values.insert("max-retries".to_string(), serde_json::json!("7"));
    values.insert("follow-torrent".to_string(), serde_json::json!("mem"));
    values.insert(
        "index-out".to_string(),
        serde_json::json!(["1=first.iso", "2=second.iso"]),
    );
    values.insert(
        "header".to_string(),
        serde_json::json!(["X-One: 1", "X-Two: 2"]),
    );

    let options = DownloadOptions::try_from_rpc_options(&values).unwrap();

    assert_eq!(options.max_download_limit, Some(100 * 1024));
    assert_eq!(options.max_retries, 7);
    assert_eq!(options.follow_torrent, Some(FollowMode::Memory));
    assert_eq!(
        options.index_out.as_deref(),
        Some("1=first.iso\n2=second.iso")
    );
    assert_eq!(options.header, vec!["X-One: 1", "X-Two: 2"]);
}

#[test]
fn min_http_range_size_is_a_separate_rpc_option() {
    let values = HashMap::from([("min-http-range-size".to_string(), serde_json::json!("128K"))]);

    let options = DownloadOptions::try_from_rpc_options(&values).unwrap();
    assert_eq!(options.min_http_range_size, Some(128 * 1024));
    assert_eq!(
        options.min_split_size,
        Some(crate::constants::DEFAULT_MIN_SPLIT_SIZE)
    );
}

#[test]
fn min_http_range_size_rpc_validation_enforces_configured_bounds() {
    for invalid in ["512", "2G"] {
        let values = HashMap::from([(
            "min-http-range-size".to_string(),
            serde_json::json!(invalid),
        )]);
        assert!(
            DownloadOptions::try_from_rpc_options(&values).is_err(),
            "expected {invalid} to be rejected"
        );
    }
}

#[test]
fn rpc_option_map_rejects_invalid_registered_values() {
    let mut values = HashMap::new();
    values.insert(
        "metalink-preferred-protocol".to_string(),
        serde_json::json!("gopher"),
    );

    let error = DownloadOptions::try_from_rpc_options(&values)
        .expect_err("invalid enum values must not fall back to defaults");
    assert!(error.contains("metalink-preferred-protocol"));
}

#[test]
fn continue_option_defaults_to_true_and_accepts_explicit_false() {
    assert!(DownloadOptions::from_option_strings(&HashMap::new()).continue_download);

    let mut values = HashMap::new();
    values.insert("continue".to_string(), "false".to_string());
    assert!(!DownloadOptions::from_option_strings(&values).continue_download);
}

#[cfg(feature = "bittorrent")]
#[test]
fn hash_check_controls_survive_option_conversion() {
    let values = HashMap::from([
        (
            "bt-enable-hook-after-hash-check".to_string(),
            "false".to_string(),
        ),
        ("bt-hash-check-seed".to_string(), "false".to_string()),
        ("bt-seed-unverified".to_string(), "true".to_string()),
        ("bt-remove-unselected-file".to_string(), "true".to_string()),
    ]);

    let options = DownloadOptions::from_option_strings(&values);

    assert!(!options.bt_enable_hook_after_hash_check);
    assert!(!options.bt_hash_check_seed);
    assert!(options.bt_seed_unverified);
    assert!(options.bt_remove_unselected_file);
}

#[cfg(feature = "bittorrent")]
#[test]
fn bt_seed_unverified_defaults_to_false() {
    assert!(!DownloadOptions::default().bt_seed_unverified);
    assert!(!DownloadOptions::from_option_strings(&HashMap::new()).bt_seed_unverified);
}