use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use crate::wire;
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum DownloadStatus {
#[default]
Waiting,
Active,
Paused,
Error(String),
Complete,
Removed,
}
impl Serialize for DownloadStatus {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(match self {
Self::Waiting => "waiting",
Self::Active => "active",
Self::Paused => "paused",
Self::Error(_) => "error",
Self::Complete => "complete",
Self::Removed => "removed",
})
}
}
impl<'de> Deserialize<'de> for DownloadStatus {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
match String::deserialize(deserializer)?.as_str() {
"waiting" => Ok(Self::Waiting),
"active" => Ok(Self::Active),
"paused" => Ok(Self::Paused),
"error" => Ok(Self::Error(String::new())),
"complete" => Ok(Self::Complete),
"removed" => Ok(Self::Removed),
value => Err(serde::de::Error::unknown_variant(
value,
&[
"waiting", "active", "paused", "error", "complete", "removed",
],
)),
}
}
}
impl DownloadStatus {
pub fn as_str(&self) -> &'static str {
match self {
Self::Waiting => "waiting",
Self::Active => "active",
Self::Paused => "paused",
Self::Error(_) => "error",
Self::Complete => "complete",
Self::Removed => "removed",
}
}
pub fn is_active(&self) -> bool {
matches!(self, Self::Active | Self::Waiting)
}
pub fn is_stopped(&self) -> bool {
!self.is_active()
}
}
pub type GlobalOptions = Arc<RwLock<HashMap<String, serde_json::Value>>>;
pub type TaskOptions = Arc<RwLock<HashMap<String, HashMap<String, serde_json::Value>>>>;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BittorrentInfo {
pub announce_list: Vec<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub comment: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub creation_date: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub mode: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub info: Option<BittorrentMetaInfo>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BittorrentMetaInfo {
pub name: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StatusInfo {
pub gid: String,
#[serde(
default,
serialize_with = "wire::serialize_option_display_as_string",
deserialize_with = "wire::deserialize_option_string_or_number",
skip_serializing_if = "Option::is_none"
)]
pub total_length: Option<u64>,
#[serde(
default,
serialize_with = "wire::serialize_option_display_as_string",
deserialize_with = "wire::deserialize_option_string_or_number",
skip_serializing_if = "Option::is_none"
)]
pub completed_length: Option<u64>,
#[serde(
default,
serialize_with = "wire::serialize_option_display_as_string",
deserialize_with = "wire::deserialize_option_string_or_number",
skip_serializing_if = "Option::is_none"
)]
pub upload_length: Option<u64>,
#[serde(
default,
serialize_with = "wire::serialize_option_display_as_string",
deserialize_with = "wire::deserialize_option_string_or_number",
skip_serializing_if = "Option::is_none"
)]
pub download_speed: Option<u64>,
#[serde(
default,
serialize_with = "wire::serialize_option_display_as_string",
deserialize_with = "wire::deserialize_option_string_or_number",
skip_serializing_if = "Option::is_none"
)]
pub upload_speed: Option<u64>,
#[serde(
default,
serialize_with = "wire::serialize_option_display_as_string",
deserialize_with = "wire::deserialize_option_string_or_number",
skip_serializing_if = "Option::is_none"
)]
pub connections: Option<u16>,
#[serde(
default,
serialize_with = "wire::serialize_option_display_as_string",
deserialize_with = "wire::deserialize_option_string_or_number",
skip_serializing_if = "Option::is_none"
)]
pub error_code: Option<i32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub error_message: Option<String>,
pub status: DownloadStatus,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub dir: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub files: Option<Vec<FileInfo>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub bittorrent: Option<BittorrentInfo>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub following: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub seeder: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub bitfield: Option<String>,
#[serde(
default,
serialize_with = "wire::serialize_option_display_as_string",
deserialize_with = "wire::deserialize_option_string_or_number",
skip_serializing_if = "Option::is_none"
)]
pub piece_length: Option<u64>,
#[serde(
default,
serialize_with = "wire::serialize_option_display_as_string",
deserialize_with = "wire::deserialize_option_string_or_number",
skip_serializing_if = "Option::is_none"
)]
pub num_pieces: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub followed_by: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub belongs_to: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub info_hash: Option<String>,
#[serde(
default,
serialize_with = "wire::serialize_option_display_as_string",
deserialize_with = "wire::deserialize_option_string_or_number",
skip_serializing_if = "Option::is_none"
)]
pub num_seeders: Option<u32>,
#[serde(
default,
serialize_with = "wire::serialize_option_display_as_string",
deserialize_with = "wire::deserialize_option_string_or_number",
skip_serializing_if = "Option::is_none"
)]
pub verified_length: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub verify_integrity_pending: Option<String>,
}
impl Default for StatusInfo {
fn default() -> Self {
Self {
gid: String::new(),
total_length: None,
completed_length: None,
upload_length: None,
download_speed: None,
upload_speed: None,
connections: None,
error_code: None,
error_message: None,
status: DownloadStatus::Active,
dir: None,
files: None,
bittorrent: None,
following: None,
seeder: None,
bitfield: None,
piece_length: None,
num_pieces: None,
followed_by: None,
belongs_to: None,
info_hash: None,
num_seeders: None,
verified_length: None,
verify_integrity_pending: None,
}
}
}
impl StatusInfo {
pub fn new(gid: impl Into<String>) -> Self {
Self {
gid: gid.into(),
..Default::default()
}
}
pub fn with_total_length(mut self, v: u64) -> Self {
self.total_length = Some(v);
self
}
pub fn with_completed_length(mut self, v: u64) -> Self {
self.completed_length = Some(v);
self
}
pub fn with_download_speed(mut self, v: u64) -> Self {
self.download_speed = Some(v);
self
}
pub fn with_status(mut self, s: DownloadStatus) -> Self {
self.status = s;
self
}
pub fn with_dir(mut self, d: impl Into<String>) -> Self {
self.dir = Some(d.into());
self
}
pub fn with_files(mut self, f: Vec<FileInfo>) -> Self {
self.files = Some(f);
self
}
pub fn with_bittorrent(mut self, v: BittorrentInfo) -> Self {
self.bittorrent = Some(v);
self
}
pub fn with_following(mut self, v: impl Into<String>) -> Self {
self.following = Some(v.into());
self
}
pub fn with_error_code(mut self, c: i32) -> Self {
self.error_code = Some(c);
self
}
pub fn with_error_message(mut self, m: impl Into<String>) -> Self {
self.error_message = Some(m.into());
self
}
pub fn with_connections(mut self, c: u16) -> Self {
self.connections = Some(c);
self
}
pub fn with_upload_length(mut self, v: u64) -> Self {
self.upload_length = Some(v);
self
}
pub fn with_upload_speed(mut self, v: u64) -> Self {
self.upload_speed = Some(v);
self
}
pub fn with_seeder(mut self, v: impl Into<String>) -> Self {
self.seeder = Some(v.into());
self
}
pub fn with_bitfield(mut self, v: impl Into<String>) -> Self {
self.bitfield = Some(v.into());
self
}
pub fn with_piece_length(mut self, v: u64) -> Self {
self.piece_length = Some(v);
self
}
pub fn with_num_pieces(mut self, v: u32) -> Self {
self.num_pieces = Some(v);
self
}
pub fn with_followed_by(mut self, v: Vec<String>) -> Self {
self.followed_by = Some(v);
self
}
pub fn with_belongs_to(mut self, v: impl Into<String>) -> Self {
self.belongs_to = Some(v.into());
self
}
pub fn with_info_hash(mut self, v: impl Into<String>) -> Self {
self.info_hash = Some(v.into());
self
}
pub fn with_num_seeders(mut self, v: u32) -> Self {
self.num_seeders = Some(v);
self
}
pub fn with_verified_length(mut self, v: u64) -> Self {
self.verified_length = Some(v);
self
}
pub fn with_verify_integrity_pending(mut self, v: impl Into<String>) -> Self {
self.verify_integrity_pending = Some(v.into());
self
}
pub fn progress_percent(&self) -> f64 {
match (self.total_length, self.completed_length) {
(Some(total), Some(done)) if total > 0 => (done as f64 / total as f64) * 100.0,
_ => 0.0,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FileInfo {
#[serde(
serialize_with = "wire::serialize_display_as_string",
deserialize_with = "wire::deserialize_string_or_number"
)]
pub index: usize,
pub path: String,
#[serde(
serialize_with = "wire::serialize_display_as_string",
deserialize_with = "wire::deserialize_string_or_number"
)]
pub length: u64,
#[serde(
serialize_with = "wire::serialize_display_as_string",
deserialize_with = "wire::deserialize_string_or_number"
)]
pub completed_length: u64,
#[serde(
serialize_with = "wire::serialize_bool_as_string",
deserialize_with = "wire::deserialize_bool_from_string_or_bool"
)]
pub selected: bool,
pub uris: Vec<UriEntry>,
}
impl Default for FileInfo {
fn default() -> Self {
Self {
index: 1,
path: String::new(),
length: 0,
completed_length: 0,
selected: true,
uris: vec![],
}
}
}
impl FileInfo {
pub fn new(path: impl Into<String>, length: u64) -> Self {
Self {
path: path.into(),
length,
..Default::default()
}
}
pub fn with_uris(mut self, uris: Vec<UriEntry>) -> Self {
self.uris = uris;
self
}
pub fn with_completed(mut self, v: u64) -> Self {
self.completed_length = v;
self
}
pub fn with_index(mut self, v: usize) -> Self {
self.index = v;
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UriEntry {
pub uri: String,
pub status: UriStatus,
}
impl UriEntry {
pub fn new(uri: impl Into<String>) -> Self {
Self {
uri: uri.into(),
status: UriStatus::Waiting,
}
}
pub fn used(mut self) -> Self {
self.status = UriStatus::Used;
self
}
pub fn waiting(mut self) -> Self {
self.status = UriStatus::Waiting;
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum UriStatus {
Used,
Spent,
#[default]
Waiting,
}
impl Serialize for UriStatus {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(match self {
Self::Used | Self::Spent => "used",
Self::Waiting => "waiting",
})
}
}
impl<'de> Deserialize<'de> for UriStatus {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let value = String::deserialize(deserializer)?;
match value.as_str() {
"used" => Ok(Self::Used),
"waiting" => Ok(Self::Waiting),
"spent" => Ok(Self::Spent),
_ => Err(serde::de::Error::unknown_variant(
&value,
&["used", "waiting"],
)),
}
}
}
pub type UriInfo = UriEntry;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ServerInfoIndex {
#[serde(
serialize_with = "wire::serialize_display_as_string",
deserialize_with = "wire::deserialize_string_or_number"
)]
pub index: usize,
pub servers: Vec<ServerInfo>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ServerInfo {
pub uri: String,
pub current_uri: String,
#[serde(
serialize_with = "wire::serialize_display_as_string",
deserialize_with = "wire::deserialize_string_or_number"
)]
pub download_speed: u64,
}
impl ServerInfo {
pub fn new(uri: impl Into<String>) -> Self {
let uri_str = uri.into();
Self {
current_uri: uri_str.clone(),
uri: uri_str,
download_speed: 0,
}
}
pub fn with_current_uri(mut self, uri: impl Into<String>) -> Self {
self.current_uri = uri.into();
self
}
pub fn with_download_speed(mut self, speed: u64) -> Self {
self.download_speed = speed;
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PeerInfo {
pub peer_id: String,
pub ip: String,
#[serde(
serialize_with = "wire::serialize_display_as_string",
deserialize_with = "wire::deserialize_string_or_number"
)]
pub port: u16,
#[serde(skip_serializing_if = "Option::is_none")]
pub bitfield: Option<String>,
#[serde(
serialize_with = "wire::serialize_bool_as_string",
deserialize_with = "wire::deserialize_bool_from_string_or_bool"
)]
pub am_choking: bool,
#[serde(
serialize_with = "wire::serialize_bool_as_string",
deserialize_with = "wire::deserialize_bool_from_string_or_bool"
)]
pub peer_choking: bool,
#[serde(
serialize_with = "wire::serialize_display_as_string",
deserialize_with = "wire::deserialize_string_or_number"
)]
pub download_speed: u64,
#[serde(
serialize_with = "wire::serialize_display_as_string",
deserialize_with = "wire::deserialize_string_or_number"
)]
pub upload_speed: u64,
#[serde(skip_serializing_if = "Option::is_none")]
pub seeder: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct GlobalStat {
#[serde(
serialize_with = "wire::serialize_display_as_string",
deserialize_with = "wire::deserialize_string_or_number"
)]
pub download_speed: u64,
#[serde(
serialize_with = "wire::serialize_display_as_string",
deserialize_with = "wire::deserialize_string_or_number"
)]
pub upload_speed: u64,
#[serde(
serialize_with = "wire::serialize_display_as_string",
deserialize_with = "wire::deserialize_string_or_number"
)]
pub num_active: usize,
#[serde(
serialize_with = "wire::serialize_display_as_string",
deserialize_with = "wire::deserialize_string_or_number"
)]
pub num_waiting: usize,
#[serde(
serialize_with = "wire::serialize_display_as_string",
deserialize_with = "wire::deserialize_string_or_number"
)]
pub num_stopped: usize,
#[serde(
serialize_with = "wire::serialize_display_as_string",
deserialize_with = "wire::deserialize_string_or_number"
)]
pub num_stopped_total: usize,
}
impl GlobalStat {
pub fn to_json_value(&self) -> serde_json::Value {
serde_json::to_value(self).expect("GlobalStat contains only serializable fields")
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VersionInfo {
pub version: String,
#[serde(rename = "enabledFeatures")]
pub enabled_features: Vec<String>,
}
impl VersionInfo {
pub fn from_env() -> Self {
Self::from_version(env!("CARGO_PKG_VERSION"))
}
pub fn from_version(version: impl Into<String>) -> Self {
let mut features = vec!["Async DNS"];
#[cfg(feature = "bittorrent")]
features.push("BitTorrent");
features.extend(["Firefox3 Cookie", "GZip", "HTTPS", "Message Digest"]);
#[cfg(feature = "metalink")]
features.push("Metalink");
features.push("XML-RPC");
#[cfg(feature = "sftp")]
features.push("SFTP");
Self {
version: version.into(),
enabled_features: features.into_iter().map(|s| s.to_string()).collect(),
}
}
pub fn to_json_value(&self) -> serde_json::Value {
serde_json::json!({
"enabledFeatures": self.enabled_features,
"version": self.version
})
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SessionInfo {
pub session_id: String,
pub session_start_time: u64,
}
impl SessionInfo {
pub fn new() -> Self {
use std::time::{SystemTime, UNIX_EPOCH};
let start_time = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
Self {
session_id: generate_session_id(),
session_start_time: start_time,
}
}
pub fn to_json_value(&self) -> serde_json::Value {
serde_json::json!({
"sessionId": self.session_id
})
}
}
pub fn generate_session_id() -> String {
use rand::RngCore;
const SESSION_ID_BYTES: usize = 20;
const HEX: &[u8; 16] = b"0123456789abcdef";
let mut bytes = [0u8; SESSION_ID_BYTES];
rand::thread_rng().fill_bytes(&mut bytes);
let mut session_id = String::with_capacity(SESSION_ID_BYTES * 2);
for byte in bytes {
session_id.push(HEX[(byte >> 4) as usize] as char);
session_id.push(HEX[(byte & 0x0f) as usize] as char);
}
session_id
}
impl Default for SessionInfo {
fn default() -> Self {
Self::new()
}
}
fn generate_gid() -> String {
use std::time::{SystemTime, UNIX_EPOCH};
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_nanos();
let mut hasher = std::collections::hash_map::DefaultHasher::new();
use std::hash::{Hash, Hasher};
nanos.hash(&mut hasher);
rand::random::<u64>().hash(&mut hasher);
format!("{:01$x}", hasher.finish(), crate::constants::GID_HEX_DIGITS)
}
pub fn create_gid() -> String {
generate_gid()
}
#[cfg(test)]
mod tests {
use super::*;
fn ben_int(v: i64) -> Vec<u8> {
format!("i{}e", v).into_bytes()
}
fn ben_str(s: &str) -> Vec<u8> {
format!("{}:{}", s.len(), s).into_bytes()
}
fn ben_bytes(data: &[u8]) -> Vec<u8> {
let mut out = format!("{}:", data.len()).into_bytes();
out.extend_from_slice(data);
out
}
fn ben_dict(entries: &[(Vec<u8>, Vec<u8>)]) -> Vec<u8> {
let mut out = b"d".to_vec();
for (k, v) in entries {
out.extend_from_slice(k);
out.extend_from_slice(v);
}
out.push(b'e');
out
}
fn ben_list(items: &[Vec<u8>]) -> Vec<u8> {
let mut out = b"l".to_vec();
for item in items {
out.extend_from_slice(item);
}
out.push(b'e');
out
}
fn build_mock_torrent_with_full_metadata() -> Vec<u8> {
let pieces: Vec<u8> = (0..40).map(|i| i as u8).collect();
let info_dict = ben_dict(&[
(ben_str("name"), ben_str("test-file.iso")),
(ben_str("length"), ben_int(1048576)), (ben_str("piece length"), ben_int(262144)), (ben_str("pieces"), ben_bytes(&pieces)),
]);
let tier1 = ben_list(&[ben_str("udp://tracker.aria2.org:80")]);
let tier2 = ben_list(&[
ben_str("http://tracker.example.com:80/announce"),
ben_str("https://tracker.example.org:443/announce"),
]);
let announce_list = ben_list(&[tier1, tier2]);
ben_dict(&[
(
ben_str("announce"),
ben_str("http://tracker.example.com:80/announce"),
),
(ben_str("announce-list"), announce_list),
(
ben_str("comment"),
ben_str("Aria2 Rust mock torrent for testing"),
),
(ben_str("creation date"), ben_int(1700000000)),
(
ben_str("created by"),
ben_str(concat!("aria2-rust-test/", env!("CARGO_PKG_VERSION"))),
),
(ben_str("info"), info_dict),
])
}
fn build_mock_multi_file_torrent() -> Vec<u8> {
let pieces: Vec<u8> = (0..60).map(|i| i as u8).collect();
let file1_dict = ben_dict(&[
(ben_str("length"), ben_int(500)),
(
ben_str("path"),
ben_list(&[ben_str("dir1"), ben_str("file1.txt")]),
),
]);
let file2_dict = ben_dict(&[
(ben_str("length"), ben_int(524)),
(
ben_str("path"),
ben_list(&[ben_str("dir2"), ben_str("file2.dat")]),
),
]);
let info_dict = ben_dict(&[
(ben_str("name"), ben_str("multi-dir-torrent")),
(ben_str("files"), ben_list(&[file1_dict, file2_dict])),
(ben_str("piece length"), ben_int(512)),
(ben_str("pieces"), ben_bytes(&pieces)),
]);
let announce_list = ben_list(&[ben_list(&[ben_str("udp://tracker.multi.com:80")])]);
ben_dict(&[
(
ben_str("announce"),
ben_str("http://tracker.multi.com:80/announce"),
),
(ben_str("announce-list"), announce_list),
(
ben_str("comment"),
ben_str("Multi-file torrent for testing"),
),
(ben_str("creation date"), ben_int(1800000000)),
(ben_str("created by"), ben_str("aria2-rust-test")),
(ben_str("info"), info_dict),
])
}
fn extract_bencode_str(data: &[u8], key: &str) -> Option<String> {
let key_bytes = key.as_bytes();
let search = format!("{}:{}", key_bytes.len(), key);
let needle = search.as_bytes();
let pos = data.windows(needle.len()).position(|w| w == needle)?;
let value_start = pos + needle.len();
let colon_pos = data[value_start..].iter().position(|&b| b == b':')?;
let len_str = std::str::from_utf8(&data[value_start..value_start + colon_pos]).ok()?;
let len: usize = len_str.parse().ok()?;
let val_start = value_start + colon_pos + 1;
if val_start + len > data.len() {
return None;
}
Some(String::from_utf8_lossy(&data[val_start..val_start + len]).to_string())
}
fn extract_bencode_int(data: &[u8], key: &str) -> Option<i64> {
let key_bytes = key.as_bytes();
let search = format!("{}:{}", key_bytes.len(), key);
let needle = search.as_bytes();
let pos = data.windows(needle.len()).position(|w| w == needle)?;
let value_start = pos + needle.len();
if data[value_start] != b'i' {
return None;
}
let end = data[value_start..].iter().position(|&b| b == b'e')?;
let int_str = std::str::from_utf8(&data[value_start + 1..value_start + end]).ok()?;
int_str.parse().ok()
}
fn parse_announce_list_from_bytes(data: &[u8]) -> Vec<Vec<String>> {
let search = b"13:announce-list";
let pos = data.windows(search.len()).position(|w| w == search);
let start = match pos {
Some(p) => p + search.len(),
None => return Vec::new(),
};
if start >= data.len() || data[start] != b'l' {
return Vec::new();
}
let mut result = Vec::new();
let mut i = start + 1; let data_len = data.len();
while i < data_len && data[i] != b'e' {
if data[i] != b'l' {
break; }
i += 1; let mut tier = Vec::new();
while i < data_len && data[i] != b'e' {
let colon_pos = match data[i..].iter().position(|&b| b == b':') {
Some(p) => p,
None => return result,
};
let len_str = match std::str::from_utf8(&data[i..i + colon_pos]) {
Ok(s) => s,
Err(_) => return result,
};
let len: usize = match len_str.parse() {
Ok(n) => n,
Err(_) => return result,
};
let val_start = i + colon_pos + 1;
if val_start + len > data_len {
return result;
}
let url = String::from_utf8_lossy(&data[val_start..val_start + len]).to_string();
tier.push(url);
i = val_start + len;
}
if data[i] == b'e' {
i += 1; }
if !tier.is_empty() {
result.push(tier);
}
}
result
}
fn bittorrent_info_from_torrent_bytes(data: &[u8]) -> BittorrentInfo {
let announce_list = parse_announce_list_from_bytes(data);
let comment = extract_bencode_str(data, "comment");
let creation_date = extract_bencode_int(data, "creation date");
let name = extract_bencode_str(data, "name").unwrap_or_default();
let mode = if data.windows(7).any(|w| w == b"5:files") {
Some("multi".to_string())
} else {
Some("single".to_string())
};
BittorrentInfo {
announce_list,
comment,
creation_date,
mode,
info: Some(BittorrentMetaInfo { name }),
}
}
#[test]
fn test_status_info_default() {
let info = StatusInfo::default();
assert!(info.gid.is_empty());
assert_eq!(info.progress_percent(), 0.0);
}
#[test]
fn test_status_info_builder() {
let info = StatusInfo::new("abc123")
.with_total_length(1000)
.with_completed_length(500)
.with_download_speed(1024)
.with_status(DownloadStatus::Active);
assert_eq!(info.gid, "abc123");
assert!((info.progress_percent() - 50.0).abs() < 0.01);
}
#[test]
fn test_download_status_variants() {
assert!(DownloadStatus::Active.is_active());
assert!(DownloadStatus::Complete.is_stopped());
assert!(DownloadStatus::Removed.is_stopped());
assert_eq!(DownloadStatus::Error("test".to_string()).as_str(), "error");
}
#[test]
fn test_file_info_default() {
let fi = FileInfo::default();
assert!(fi.selected);
assert_eq!(fi.uris.len(), 0);
}
#[test]
fn test_file_info_builder() {
let fi = FileInfo::new("/tmp/file.iso", 1048576)
.with_uris(vec![UriEntry::new("http://example.com/file.iso")]);
assert_eq!(fi.length, 1048576);
assert_eq!(fi.uris.len(), 1);
}
#[test]
fn test_uri_entry() {
let uri = UriEntry::new("http://example.com/file.iso").used();
assert_eq!(uri.status, UriStatus::Used);
let w = UriEntry::new("http://x.com/f").waiting();
assert_eq!(w.status, UriStatus::Waiting);
}
#[test]
fn test_global_stat_default() {
let stat = GlobalStat::default();
assert_eq!(stat.download_speed, 0);
let val = stat.to_json_value();
assert!(val.get("downloadSpeed").is_some());
}
#[test]
fn test_generate_gid() {
let gid1 = create_gid();
let gid2 = create_gid();
assert_eq!(gid1.len(), 16);
assert_ne!(gid1, gid2);
}
#[test]
fn test_bittorrent_info_serialization() {
let bt = BittorrentInfo {
announce_list: vec![
vec!["udp://tracker1:80".to_string()],
vec!["http://tracker2:80".to_string()],
],
comment: Some("Test torrent".to_string()),
creation_date: Some(1700000000),
mode: Some("single".to_string()),
info: Some(BittorrentMetaInfo {
name: "test-file.iso".to_string(),
}),
};
let json = serde_json::to_value(&bt).unwrap();
assert_eq!(json["announceList"][0][0], "udp://tracker1:80");
assert_eq!(json["comment"], "Test torrent");
assert_eq!(json["creationDate"], 1700000000);
assert_eq!(json["mode"], "single");
assert_eq!(json["info"]["name"], "test-file.iso");
let info = StatusInfo::new("gid-bt-001").with_bittorrent(bt);
let serialized = serde_json::to_value(&info).unwrap();
assert!(
serialized.get("bittorrent").is_some(),
"bittorrent field should appear in serialized StatusInfo"
);
let bt_json = serialized.get("bittorrent").unwrap();
assert_eq!(bt_json["announceList"][0][0], "udp://tracker1:80");
assert_eq!(bt_json["info"]["name"], "test-file.iso");
let default_info = StatusInfo::default();
let default_serialized = serde_json::to_value(&default_info).unwrap();
assert!(
default_serialized.get("bittorrent").is_none(),
"Default StatusInfo should not have bittorrent field"
);
}
#[test]
fn test_peer_info_with_bitfield_seeder() {
let peer = PeerInfo {
peer_id: "peer-abc123".to_string(),
ip: "192.168.1.100".to_string(),
port: 6881,
bitfield: Some("ff00ff00".to_string()),
am_choking: false,
peer_choking: true,
download_speed: 1048576,
upload_speed: 512000,
seeder: Some("true".to_string()),
};
let json = serde_json::to_value(&peer).unwrap();
assert_eq!(json["peerId"], "peer-abc123");
assert_eq!(json["ip"], "192.168.1.100");
assert_eq!(json["port"], "6881");
assert_eq!(json["bitfield"], "ff00ff00");
assert_eq!(json["amChoking"], "false");
assert_eq!(json["peerChoking"], "true");
assert_eq!(json["downloadSpeed"], "1048576");
assert_eq!(json["uploadSpeed"], "512000");
assert_eq!(json["seeder"], "true");
let roundtrip: PeerInfo = serde_json::from_value(json).unwrap();
assert_eq!(roundtrip.bitfield, Some("ff00ff00".to_string()));
assert_eq!(roundtrip.seeder, Some("true".to_string()));
}
#[test]
fn test_status_info_following_field() {
let info = StatusInfo::new("gid-test".to_string()).with_following("gid-following-001");
let serialized = serde_json::to_value(&info).unwrap();
assert_eq!(
serialized.get("following").unwrap().as_str().unwrap(),
"gid-following-001",
"following should be a single GID string"
);
let default_info = StatusInfo::default();
let default_serialized = serde_json::to_value(&default_info).unwrap();
assert!(
default_serialized.get("following").is_none(),
"Default StatusInfo should not have following field"
);
}
#[test]
fn test_bittorrent_info_from_mock_single_file_torrent() {
let torrent_bytes = build_mock_torrent_with_full_metadata();
let bt_info = bittorrent_info_from_torrent_bytes(&torrent_bytes);
assert_eq!(
bt_info.announce_list.len(),
2,
"announce-list should have 2 tiers"
);
assert_eq!(
bt_info.announce_list[0],
vec!["udp://tracker.aria2.org:80"],
"tier 1 should have 1 tracker"
);
assert_eq!(
bt_info.announce_list[1],
vec![
"http://tracker.example.com:80/announce",
"https://tracker.example.org:443/announce",
],
"tier 2 should have 2 trackers"
);
assert_eq!(
bt_info.comment.as_deref(),
Some("Aria2 Rust mock torrent for testing"),
"comment should match"
);
assert_eq!(
bt_info.creation_date,
Some(1700000000),
"creationDate should be 1700000000"
);
assert_eq!(
bt_info.mode.as_deref(),
Some("single"),
"mode should be 'single' for single-file torrent"
);
assert_eq!(
bt_info.info.as_ref().unwrap().name,
"test-file.iso",
"info.name should match"
);
let json = serde_json::to_value(&bt_info).unwrap();
let announce_list = json["announceList"].as_array().unwrap();
assert_eq!(announce_list.len(), 2);
assert_eq!(announce_list[0][0], "udp://tracker.aria2.org:80");
assert_eq!(
announce_list[1][0],
"http://tracker.example.com:80/announce"
);
assert_eq!(
announce_list[1][1],
"https://tracker.example.org:443/announce"
);
assert_eq!(json["comment"], "Aria2 Rust mock torrent for testing");
assert_eq!(json["creationDate"], 1700000000);
assert!(
json["creationDate"].is_number(),
"creationDate should be a JSON number in original aria2"
);
assert_eq!(json["mode"], "single");
assert_eq!(json["info"]["name"], "test-file.iso");
}
#[test]
fn test_bittorrent_info_from_mock_multi_file_torrent() {
let torrent_bytes = build_mock_multi_file_torrent();
let bt_info = bittorrent_info_from_torrent_bytes(&torrent_bytes);
assert_eq!(
bt_info.announce_list,
vec![vec!["udp://tracker.multi.com:80"]],
"announce-list should have 1 tier with 1 tracker"
);
assert_eq!(
bt_info.comment.as_deref(),
Some("Multi-file torrent for testing"),
"comment should match"
);
assert_eq!(
bt_info.creation_date,
Some(1800000000),
"creationDate should be 1800000000"
);
assert_eq!(
bt_info.mode.as_deref(),
Some("multi"),
"mode should be 'multi' for multi-file torrent"
);
assert_eq!(
bt_info.info.as_ref().unwrap().name,
"multi-dir-torrent",
"info.name should be the top-level dir name"
);
let json = serde_json::to_value(&bt_info).unwrap();
assert_eq!(json["mode"], "multi");
assert_eq!(json["info"]["name"], "multi-dir-torrent");
assert_eq!(json["creationDate"], 1800000000);
}
#[test]
fn test_bittorrent_info_minimal_fields() {
let bt = BittorrentInfo {
announce_list: vec![],
comment: None,
creation_date: None,
mode: Some("single".to_string()),
info: Some(BittorrentMetaInfo {
name: "unknown.torrent".to_string(),
}),
};
let json = serde_json::to_value(&bt).unwrap();
assert!(json.get("comment").is_none(), "comment should be omitted");
assert!(
json.get("creationDate").is_none(),
"creationDate should be omitted"
);
assert_eq!(json["mode"], "single");
assert_eq!(json["info"]["name"], "unknown.torrent");
assert_eq!(
json["announceList"].as_array().unwrap().len(),
0,
"announce-list should be empty array"
);
}
#[test]
fn test_tell_status_with_bittorrent_from_mock_torrent() {
let torrent_bytes = build_mock_torrent_with_full_metadata();
let bt_info = bittorrent_info_from_torrent_bytes(&torrent_bytes);
let status = StatusInfo::new("bt-mock-gid-001")
.with_total_length(1048576)
.with_completed_length(0)
.with_download_speed(0)
.with_upload_speed(0)
.with_status(DownloadStatus::Active)
.with_dir("/downloads/aria2")
.with_bittorrent(bt_info)
.with_following("child-gid-001");
let json = serde_json::to_value(&status).unwrap();
assert_eq!(json["gid"], "bt-mock-gid-001");
assert_eq!(json["status"], "active");
assert_eq!(json["dir"], "/downloads/aria2");
assert_eq!(json["following"], "child-gid-001");
let bt_json = json.get("bittorrent").unwrap();
assert_eq!(bt_json["announceList"][0][0], "udp://tracker.aria2.org:80");
assert_eq!(bt_json["comment"], "Aria2 Rust mock torrent for testing");
assert_eq!(bt_json["creationDate"], 1700000000);
assert_eq!(bt_json["mode"], "single");
assert_eq!(bt_json["info"]["name"], "test-file.iso");
let json_str = serde_json::to_string_pretty(&json).unwrap();
assert!(
json_str.contains("\"bittorrent\""),
"JSON should contain bittorrent key"
);
assert!(
json_str.contains("\"announceList\""),
"JSON should contain announceList inside bittorrent"
);
assert!(
json_str.contains("\"Aria2 Rust mock torrent for testing\""),
"JSON should contain the comment text"
);
}
#[test]
fn test_mock_torrent_builder_roundtrip() {
let torrent_bytes = build_mock_torrent_with_full_metadata();
assert_eq!(
torrent_bytes.first(),
Some(&b'd'),
"Bencoded torrent should start with 'd'"
);
assert_eq!(
torrent_bytes.last(),
Some(&b'e'),
"Bencoded torrent should end with 'e'"
);
let as_text = String::from_utf8_lossy(&torrent_bytes);
assert!(
as_text.contains("8:announce"),
"Should contain announce key"
);
assert!(
as_text.contains("13:announce-list"),
"Should contain announce-list key"
);
assert!(as_text.contains("7:comment"), "Should contain comment key");
assert!(
as_text.contains("13:creation date"),
"Should contain creation date key"
);
assert!(as_text.contains("4:info"), "Should contain info key");
assert!(as_text.contains("4:name"), "Should contain name key");
assert_eq!(
extract_bencode_str(&torrent_bytes, "comment"),
Some("Aria2 Rust mock torrent for testing".to_string())
);
assert_eq!(
extract_bencode_int(&torrent_bytes, "creation date"),
Some(1700000000)
);
assert_eq!(
extract_bencode_str(&torrent_bytes, "name"),
Some("test-file.iso".to_string())
);
let announce_list = parse_announce_list_from_bytes(&torrent_bytes);
assert_eq!(announce_list.len(), 2);
assert_eq!(announce_list[0], vec!["udp://tracker.aria2.org:80"]);
assert_eq!(
announce_list[1],
vec![
"http://tracker.example.com:80/announce",
"https://tracker.example.org:443/announce",
]
);
}
#[test]
fn test_empty_announce_list() {
let pieces: Vec<u8> = (0..20).map(|i| i as u8).collect();
let info_dict = ben_dict(&[
(ben_str("name"), ben_str("no-tracker.torrent")),
(ben_str("length"), ben_int(1024)),
(ben_str("piece length"), ben_int(512)),
(ben_str("pieces"), ben_bytes(&pieces)),
]);
let torrent = ben_dict(&[
(ben_str("announce"), ben_str("http://example.com/announce")),
(ben_str("info"), info_dict),
]);
let announce_list = parse_announce_list_from_bytes(&torrent);
assert!(
announce_list.is_empty(),
"announce-list should be empty when not present in torrent"
);
let bt_info = bittorrent_info_from_torrent_bytes(&torrent);
assert!(
bt_info.announce_list.is_empty(),
"BittorrentInfo.announce_list should be empty"
);
assert!(bt_info.comment.is_none(), "comment should be None");
assert!(
bt_info.creation_date.is_none(),
"creationDate should be None"
);
assert_eq!(
bt_info.mode.as_deref(),
Some("single"),
"mode should be 'single'"
);
}
#[test]
fn test_status_info_numeric_fields_are_strings() {
let info = StatusInfo::new("wire-format-test")
.with_status(DownloadStatus::Active)
.with_total_length(104857600)
.with_completed_length(52428800)
.with_upload_length(0)
.with_download_speed(1024000)
.with_upload_speed(0)
.with_connections(5);
let json = serde_json::to_value(&info).unwrap();
assert_eq!(
json["totalLength"].as_str(),
Some("104857600"),
"totalLength must be a string: got {:?}",
json["totalLength"]
);
assert_eq!(
json["completedLength"].as_str(),
Some("52428800"),
"completedLength must be a string: got {:?}",
json["completedLength"]
);
assert_eq!(
json["uploadLength"].as_str(),
Some("0"),
"uploadLength must be a string: got {:?}",
json["uploadLength"]
);
assert_eq!(
json["downloadSpeed"].as_str(),
Some("1024000"),
"downloadSpeed must be a string: got {:?}",
json["downloadSpeed"]
);
assert_eq!(
json["uploadSpeed"].as_str(),
Some("0"),
"uploadSpeed must be a string: got {:?}",
json["uploadSpeed"]
);
assert_eq!(
json["connections"].as_str(),
Some("5"),
"connections must be a string: got {:?}",
json["connections"]
);
}
#[test]
fn test_global_stat_numeric_fields_are_strings() {
let stat = GlobalStat {
download_speed: 1024000,
upload_speed: 51200,
num_active: 2,
num_waiting: 3,
num_stopped: 1,
num_stopped_total: 1,
};
let json = stat.to_json_value();
assert_eq!(json["downloadSpeed"].as_str(), Some("1024000"));
assert_eq!(json["uploadSpeed"].as_str(), Some("51200"));
assert_eq!(json["numActive"].as_str(), Some("2"));
assert_eq!(json["numWaiting"].as_str(), Some("3"));
assert_eq!(json["numStopped"].as_str(), Some("1"));
assert_eq!(json["numStoppedTotal"].as_str(), Some("1"));
}
#[test]
fn test_file_info_numeric_fields_are_strings() {
let fi = FileInfo::new("/downloads/file.iso", 104857600)
.with_completed(52428800)
.with_index(1);
let json = serde_json::to_value(&fi).unwrap();
assert_eq!(
json["index"].as_str(),
Some("1"),
"index must be a string: got {:?}",
json["index"]
);
assert_eq!(
json["length"].as_str(),
Some("104857600"),
"length must be a string: got {:?}",
json["length"]
);
assert_eq!(
json["completedLength"].as_str(),
Some("52428800"),
"completedLength must be a string: got {:?}",
json["completedLength"]
);
}
#[test]
fn test_status_info_deserialization_roundtrip() {
let info = StatusInfo::new("test")
.with_total_length(104857600)
.with_completed_length(0);
let json = serde_json::to_value(&info).unwrap();
assert_eq!(json["totalLength"].as_str(), Some("104857600"));
let roundtrip: StatusInfo = serde_json::from_value(json.clone()).unwrap();
assert_eq!(roundtrip.total_length, Some(104857600));
assert_eq!(roundtrip.completed_length, Some(0));
}
#[test]
fn test_wire_codecs_accept_aria2_literals_and_native_values() {
let wire = serde_json::json!({
"gid": "wire-test",
"status": "active",
"totalLength": "104857600",
"completedLength": 52428800,
"connections": "4",
"pieceLength": 262144,
"numPieces": "400",
"numSeeders": 3,
"files": [{
"index": "1",
"path": "/downloads/file.bin",
"length": 104857600,
"completedLength": "52428800",
"selected": "true",
"uris": [{"uri": "http://example.test/file.bin", "status": "used"}]
}]
});
let info: StatusInfo = serde_json::from_value(wire).unwrap();
assert_eq!(info.total_length, Some(104857600));
assert_eq!(info.completed_length, Some(52428800));
assert_eq!(info.connections, Some(4));
assert_eq!(info.num_pieces, Some(400));
assert_eq!(info.num_seeders, Some(3));
assert_eq!(info.files.as_ref().unwrap()[0].index, 1);
assert_eq!(
info.files.as_ref().unwrap()[0].uris[0].status,
UriStatus::Used
);
let encoded = serde_json::to_value(info).unwrap();
assert_eq!(encoded["completedLength"], "52428800");
assert_eq!(encoded["connections"], "4");
assert_eq!(encoded["numPieces"], "400");
assert_eq!(encoded["numSeeders"], "3");
assert_eq!(encoded["files"][0]["index"], "1");
assert_eq!(encoded["files"][0]["selected"], "true");
}
#[test]
fn test_uri_status_hides_internal_spent_state_on_wire() {
assert_eq!(serde_json::to_value(UriStatus::Spent).unwrap(), "used");
assert_eq!(
serde_json::from_value::<UriStatus>(serde_json::json!("spent")).unwrap(),
UriStatus::Spent
);
assert_eq!(
serde_json::from_value::<UriStatus>(serde_json::json!("used")).unwrap(),
UriStatus::Used
);
}
#[test]
fn test_global_stat_roundtrip_uses_wire_strings() {
let stat: GlobalStat = serde_json::from_value(serde_json::json!({
"downloadSpeed": "100",
"uploadSpeed": 20,
"numActive": "1",
"numWaiting": 2,
"numStopped": "3",
"numStoppedTotal": 4
}))
.unwrap();
assert_eq!(stat.download_speed, 100);
assert_eq!(stat.upload_speed, 20);
assert_eq!(stat.num_stopped_total, 4);
let encoded = serde_json::to_value(stat).unwrap();
assert_eq!(encoded["downloadSpeed"], "100");
assert_eq!(encoded["numStoppedTotal"], "4");
}
}