use std::net::SocketAddr;
use std::time::{Duration, Instant};
use super::UdpError;
use crate::bittorrent::tracker::udp_tracker_protocol::UdpAction;
use super::*;
mod scrape;
async fn new_direct_client(bind_port: u16) -> Result<UdpTrackerClient, String> {
let socket = tokio::net::UdpSocket::bind((std::net::Ipv4Addr::UNSPECIFIED, bind_port))
.await
.map_err(|error| error.to_string())?;
Ok(UdpTrackerClient::new(socket))
}
#[tokio::test]
async fn test_client_creation() {
let client = new_direct_client(0).await;
assert!(
client.is_ok(),
"UDP client creation should succeed with port 0"
);
let c = client.unwrap();
assert!(c.pending.is_empty());
assert!(c.inflight.is_empty());
assert!(c.waiting_for_conn.is_empty());
}
#[tokio::test]
async fn test_add_announce_request() {
let mut client = new_direct_client(0).await.unwrap();
let addr: SocketAddr = "127.0.0.1:6969".parse().unwrap();
let ih = [0xABu8; 20];
let pid = [0xCDu8; 20];
client.add_announce(&addr, &ih, &pid, 0, 1000, 0, UdpEvent::Started, 50, 6881);
assert_eq!(client.pending.len(), 1);
client.add_announce(&addr, &ih, &pid, 500, 500, 0, UdpEvent::None, -1, 6881);
assert_eq!(client.pending.len(), 2);
}
#[tokio::test]
async fn udp_connection_id_is_reacquired_after_one_minute() {
let tracker = tokio::net::UdpSocket::bind("127.0.0.1:0")
.await
.expect("bind loopback UDP tracker");
let tracker_addr = tracker.local_addr().unwrap();
let mut client = new_direct_client(0).await.unwrap();
client.conn_cache.insert(
tracker_addr,
ConnectionState {
id: 0x1234_5678_9abc_def0,
updated_at: Instant::now() - Duration::from_secs(61),
},
);
client.add_announce(
&tracker_addr,
&[0x12; 20],
&[0x34; 20],
0,
1,
0,
UdpEvent::Started,
50,
6881,
);
assert!(client.process_one().await);
let mut packet = [0u8; 1500];
let (length, _) = tokio::time::timeout(Duration::from_secs(1), tracker.recv_from(&mut packet))
.await
.expect("client should send a UDP request")
.expect("receive UDP request");
assert_eq!(length, 16, "expired IDs require a CONNECT packet");
assert_eq!(
i32::from_be_bytes(packet[8..12].try_into().unwrap()),
UdpAction::Connect as i32,
"client must reacquire its expired connection ID before ANNOUNCE"
);
}
#[tokio::test]
async fn test_process_one_needs_connection() {
let mut client = new_direct_client(0).await.unwrap();
let addr: SocketAddr = "127.0.0.1:6969".parse().unwrap();
let ih = [0x12u8; 20];
let pid = [0x34u8; 20];
client.add_announce(&addr, &ih, &pid, 0, 1000, 0, UdpEvent::Started, 50, 6881);
let processed = client.process_one().await;
assert!(processed, "Should have processed the connect step");
assert!(
!client.inflight.is_empty(),
"Should have an in-flight CONNECT"
);
}
#[tokio::test]
async fn cancel_pending_requests_discards_transactions_but_keeps_connection_cache() {
let tracker = tokio::net::UdpSocket::bind("127.0.0.1:0")
.await
.expect("bind local UDP tracker");
let tracker_addr = tracker.local_addr().unwrap();
let cached_addr = "127.0.0.1:1".parse().unwrap();
let mut client = new_direct_client(0).await.unwrap();
client.conn_cache.insert(
cached_addr,
ConnectionState {
id: 0x1234,
updated_at: Instant::now(),
},
);
client.add_announce(
&tracker_addr,
&[0x12; 20],
&[0x34; 20],
0,
1,
0,
UdpEvent::Started,
50,
6881,
);
client.add_announce(
&tracker_addr,
&[0x56; 20],
&[0x78; 20],
0,
1,
0,
UdpEvent::Started,
50,
6881,
);
assert!(client.process_one().await);
assert!(!client.process_one().await);
let mut packet = [0u8; 64];
let (length, _) = tokio::time::timeout(Duration::from_secs(1), tracker.recv_from(&mut packet))
.await
.expect("client should send its CONNECT request")
.expect("receive CONNECT request");
assert_eq!(length, 16);
assert_eq!(client.inflight.len(), 1);
assert_eq!(client.waiting_for_conn.len(), 2);
assert_eq!(client.txn_map.len(), 1);
client.cancel_pending_requests();
assert!(client.pending.is_empty());
assert!(client.inflight.is_empty());
assert!(client.waiting_for_conn.is_empty());
assert!(client.txn_map.is_empty());
assert!(client.conn_cache.contains_key(&cached_addr));
assert!(!client.process_one().await);
}
#[tokio::test]
async fn test_no_pending_returns_false_when_empty() {
let mut client = new_direct_client(0).await.unwrap();
assert!(client.pending.is_empty());
assert!(client.inflight.is_empty());
assert!(client.waiting_for_conn.is_empty());
let processed = client.process_one().await;
assert!(!processed, "process_one should return false when empty");
}
#[tokio::test]
async fn test_handle_connect_response() {
let mut client = new_direct_client(0).await.unwrap();
let addr: SocketAddr = "127.0.0.1:6969".parse().unwrap();
let ih = [0x11u8; 20];
let pid = [0x22u8; 20];
client.add_announce(&addr, &ih, &pid, 0, 1000, 0, UdpEvent::Started, 50, 6881);
client.process_one().await;
let mut resp_data = vec![0u8; 16];
resp_data[0..4].copy_from_slice(&0i32.to_be_bytes());
let txn_id = client.inflight.front().map(|r| r.txn_id).unwrap_or(0);
resp_data[4..8].copy_from_slice(&txn_id.to_be_bytes());
resp_data[8..16].copy_from_slice(&0x123456789ABCDEF0u64.to_be_bytes());
client.handle_response(&resp_data, &addr).await;
assert!(
client.conn_cache.contains_key(&addr),
"Should cache connection after CONNECT response"
);
}
#[tokio::test]
async fn removing_one_response_reindexes_later_inflight_transactions() {
let tracker_a = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();
let tracker_b = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();
let addr_a = tracker_a.local_addr().unwrap();
let addr_b = tracker_b.local_addr().unwrap();
let mut client = new_direct_client(0).await.unwrap();
client.send_connect(addr_a).await;
client.send_connect(addr_b).await;
let txn_a = client.inflight[0].txn_id;
let txn_b = client.inflight[1].txn_id;
assert_eq!(client.txn_map.get(&txn_a), Some(&0));
assert_eq!(client.txn_map.get(&txn_b), Some(&1));
let mut response = [0; 16];
response[..4].copy_from_slice(&(UdpAction::Connect as i32).to_be_bytes());
response[4..8].copy_from_slice(&txn_a.to_be_bytes());
response[8..].copy_from_slice(&0x1234_5678_9ABC_DEF0u64.to_be_bytes());
client.handle_response(&response, &addr_a).await;
assert_eq!(client.txn_map.get(&txn_b), Some(&0));
response[4..8].copy_from_slice(&txn_b.to_be_bytes());
client.handle_response(&response, &addr_b).await;
assert!(client.inflight.is_empty());
assert!(client.conn_cache.contains_key(&addr_a));
assert!(client.conn_cache.contains_key(&addr_b));
}
#[tokio::test]
async fn timing_out_one_request_reindexes_later_inflight_transactions() {
let tracker_a = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();
let tracker_b = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();
let addr_a = tracker_a.local_addr().unwrap();
let addr_b = tracker_b.local_addr().unwrap();
let mut client = new_direct_client(0).await.unwrap();
client.send_connect(addr_a).await;
client.send_connect(addr_b).await;
let txn_a = client.inflight[0].txn_id;
let txn_b = client.inflight[1].txn_id;
client.inflight[0].dispatched_at = Some(Instant::now() - Duration::from_secs(6));
client
.handle_timeouts_with_timeout(Duration::from_secs(60))
.await;
assert!(!client.txn_map.contains_key(&txn_a));
assert_eq!(client.txn_map.get(&txn_b), Some(&0));
}
#[tokio::test]
async fn connect_response_from_unexpected_udp_source_is_ignored() {
let tracker = tokio::net::UdpSocket::bind("127.0.0.1:0")
.await
.expect("bind expected tracker endpoint");
let tracker_addr = tracker.local_addr().unwrap();
let impostor = tokio::net::UdpSocket::bind("127.0.0.1:0")
.await
.expect("bind unexpected response source");
let impostor_addr = impostor.local_addr().unwrap();
let mut client = new_direct_client(0).await.unwrap();
client.send_connect(tracker_addr).await;
let mut request = [0u8; 64];
let (request_len, request_source) = tracker.recv_from(&mut request).await.unwrap();
assert_eq!(request_len, 16);
assert_eq!(request_source.ip(), std::net::Ipv4Addr::LOCALHOST);
let txn_id = u32::from_be_bytes(request[12..16].try_into().unwrap());
let mut response = Vec::with_capacity(16);
response.extend_from_slice(&0i32.to_be_bytes());
response.extend_from_slice(&txn_id.to_be_bytes());
response.extend_from_slice(&0x1020_3040_5060_7080u64.to_be_bytes());
impostor.send_to(&response, request_source).await.unwrap();
assert!(
client
.receive_next_with_timeout(Duration::from_secs(1))
.await
);
assert_eq!(
client.inflight.len(),
1,
"foreign source must not consume the request"
);
assert!(client.txn_map.contains_key(&txn_id));
assert!(!client.conn_cache.contains_key(&impostor_addr));
tracker.send_to(&response, request_source).await.unwrap();
assert!(
client
.receive_next_with_timeout(Duration::from_secs(1))
.await
);
assert!(client.inflight.is_empty());
assert_eq!(
client.conn_cache.get(&tracker_addr).unwrap().id,
0x1020_3040_5060_7080
);
assert!(!client.conn_cache.contains_key(&impostor_addr));
}
#[tokio::test]
async fn malformed_or_mismatched_connect_response_does_not_consume_transaction() {
let tracker = tokio::net::UdpSocket::bind("127.0.0.1:0")
.await
.expect("bind local UDP tracker");
let tracker_addr = tracker.local_addr().unwrap();
let mut client = new_direct_client(0).await.unwrap();
client.send_connect(tracker_addr).await;
let mut request = [0u8; 64];
let (request_len, request_source) = tracker.recv_from(&mut request).await.unwrap();
assert_eq!(request_len, 16);
let txn_id = u32::from_be_bytes(request[12..16].try_into().unwrap());
let mut malformed_connect = Vec::with_capacity(17);
malformed_connect.extend_from_slice(&0i32.to_be_bytes());
malformed_connect.extend_from_slice(&txn_id.to_be_bytes());
malformed_connect.extend_from_slice(&0x1020_3040_5060_7080u64.to_be_bytes());
malformed_connect.push(0);
tracker
.send_to(&malformed_connect, request_source)
.await
.unwrap();
assert!(
client
.receive_next_with_timeout(Duration::from_secs(1))
.await
);
assert_eq!(
client.inflight.len(),
1,
"CONNECT response must be exactly 16 bytes"
);
assert!(client.txn_map.contains_key(&txn_id));
let mut mismatched_action = vec![0u8; 20];
mismatched_action[0..4].copy_from_slice(&1i32.to_be_bytes());
mismatched_action[4..8].copy_from_slice(&txn_id.to_be_bytes());
tracker
.send_to(&mismatched_action, request_source)
.await
.unwrap();
assert!(
client
.receive_next_with_timeout(Duration::from_secs(1))
.await
);
assert_eq!(
client.inflight.len(),
1,
"ANNOUNCE cannot complete a CONNECT transaction"
);
assert!(client.txn_map.contains_key(&txn_id));
let valid_connect = &malformed_connect[..16];
tracker
.send_to(valid_connect, request_source)
.await
.unwrap();
assert!(
client
.receive_next_with_timeout(Duration::from_secs(1))
.await
);
assert!(client.inflight.is_empty());
assert!(client.conn_cache.contains_key(&tracker_addr));
}
#[tokio::test]
async fn test_handle_announce_response_with_peers() {
let mut client = new_direct_client(0).await.unwrap();
let addr: SocketAddr = "127.0.0.1:6969".parse().unwrap();
let ih = [0x33u8; 20];
let pid = [0x44u8; 20];
let txn_id = client.next_txn();
client.txn_map.insert(txn_id, 0);
let mut dummy_req =
UdpTrackerRequest::new(addr, ih, pid, 0, 1000, 0, UdpEvent::Started, 50, 6881);
dummy_req.txn_id = txn_id;
dummy_req.dispatched_at = Some(Instant::now());
client.inflight.push_back(dummy_req);
let mut resp_data = vec![0u8; 26];
resp_data[0..4].copy_from_slice(&1i32.to_be_bytes());
resp_data[4..8].copy_from_slice(&txn_id.to_be_bytes());
resp_data[8..12].copy_from_slice(&900u32.to_be_bytes());
resp_data[12..16].copy_from_slice(&5u32.to_be_bytes());
resp_data[16..20].copy_from_slice(&3u32.to_be_bytes());
resp_data.extend_from_slice(&[10, 0, 0, 1, 0x1A, 0x04, 192, 168, 1, 100, 0x1F, 0x90]);
client.handle_response(&resp_data, &addr).await;
let completed: Vec<_> = client
.pending
.iter()
.filter_map(|request| request.reply.as_ref())
.collect();
assert!(
!completed.is_empty(),
"Should have at least one completed announce"
);
assert!(
completed[0].peers.len() >= 2,
"Should have at least 2 peers"
);
assert_eq!(completed[0].interval, 900);
assert_eq!(completed[0].leechers, 5);
assert_eq!(completed[0].seeders, 3);
}
#[tokio::test]
async fn test_handle_error_response() {
let mut client = new_direct_client(0).await.unwrap();
let addr: SocketAddr = "127.0.0.1:6969".parse().unwrap();
let ih = [0x55u8; 20];
let pid = [0x66u8; 20];
client.add_announce(&addr, &ih, &pid, 0, 1000, 0, UdpEvent::Started, 50, 6881);
client.process_one().await;
let txn_id = client.inflight.front().map(|r| r.txn_id).unwrap_or(0);
let mut err_data = vec![0u8; 23];
err_data[0..4].copy_from_slice(&3i32.to_be_bytes());
err_data[4..8].copy_from_slice(&txn_id.to_be_bytes());
err_data[8..23].copy_from_slice(b"tracker offline");
client.handle_response(&err_data, &addr).await;
assert!(
!client.conn_cache.contains_key(&addr),
"Error should not create cache entry"
);
assert_eq!(
client.pending.iter().find_map(|request| request.error),
Some(UdpError::TrackerError)
);
}
#[tokio::test]
async fn test_timeout_cleaning() {
let mut client = new_direct_client(0).await.unwrap();
let addr: SocketAddr = "127.0.0.1:6969".parse().unwrap();
let ih = [0x77u8; 20];
let pid = [0x88u8; 20];
client.add_announce(&addr, &ih, &pid, 0, 1000, 0, UdpEvent::Started, 50, 6881);
client.process_one().await;
assert_eq!(client.inflight.len(), 1);
tokio::time::sleep(Duration::from_millis(100)).await;
client
.handle_timeouts_with_timeout(Duration::from_secs(15))
.await;
assert_eq!(client.inflight.len(), 1, "Not yet timed out");
for req in &mut client.inflight {
req.dispatched_at = Some(Instant::now() - Duration::from_secs(16));
}
client
.handle_timeouts_with_timeout(Duration::from_secs(15))
.await;
assert!(
client.inflight.is_empty()
|| !client.pending.is_empty()
|| !client.waiting_for_conn.is_empty(),
"Timed-out request should be moved"
);
for _ in 0..MAX_RETRIES.saturating_sub(1) {
let mut req = client
.pending
.pop_front()
.expect("timeout retry should be queued");
req.dispatched_at = Some(Instant::now() - Duration::from_secs(16));
client.inflight.push_back(req);
client
.handle_timeouts_with_timeout(Duration::from_secs(15))
.await;
}
assert_eq!(
client.pending.iter().find_map(|request| request.error),
Some(UdpError::Timeout),
"exhausted UDP retries should retain a timeout classification"
);
}
#[tokio::test]
async fn udp_retry_uses_five_then_ten_second_deadlines_and_two_attempts() {
let tracker = tokio::net::UdpSocket::bind("127.0.0.1:0")
.await
.expect("bind loopback UDP tracker");
let addr = tracker.local_addr().unwrap();
let mut client = new_direct_client(0).await.unwrap();
client.conn_cache.insert(
addr,
ConnectionState {
id: 0x1234_5678_9abc_def0,
updated_at: Instant::now(),
},
);
client.add_announce(
&addr,
&[0x77; 20],
&[0x88; 20],
0,
1000,
0,
UdpEvent::Started,
50,
6881,
);
assert!(client.process_one().await);
assert_eq!(client.inflight.len(), 1);
client.inflight[0].dispatched_at = Some(Instant::now() - Duration::from_secs(6));
client
.handle_timeouts_with_timeout(Duration::from_secs(60))
.await;
assert!(
client.inflight.is_empty(),
"first attempt expires at five seconds"
);
assert_eq!(client.pending.front().unwrap().fail_count, 1);
assert!(
client.process_one().await,
"the request should be retransmitted"
);
client.inflight[0].dispatched_at = Some(Instant::now() - Duration::from_secs(11));
client
.handle_timeouts_with_timeout(Duration::from_secs(60))
.await;
assert!(client.inflight.is_empty());
assert_eq!(
client.pending.front().and_then(|request| request.error),
Some(UdpError::Timeout),
"the retransmission expires after ten seconds without a third send"
);
}
#[tokio::test(start_paused = true)]
async fn udp_receive_wait_is_cut_short_by_the_retry_deadline() {
let tracker = tokio::net::UdpSocket::bind("127.0.0.1:0")
.await
.expect("bind silent loopback UDP tracker");
let mut client = new_direct_client(0).await.unwrap();
assert!(client.send_connect(tracker.local_addr().unwrap()).await);
let dispatched_at = client.inflight[0]
.dispatched_at
.expect("connect request should have a dispatch time");
let expected_wait = Duration::from_secs(5)
.saturating_sub(std::time::Instant::now().saturating_duration_since(dispatched_at));
let started = tokio::time::Instant::now();
assert!(
!client
.receive_next_with_timeout(Duration::from_secs(60))
.await
);
let elapsed = tokio::time::Instant::now() - started;
let tolerance = Duration::from_millis(10);
assert!(
elapsed >= expected_wait.saturating_sub(tolerance)
&& elapsed <= expected_wait.saturating_add(tolerance),
"a 60-second tracker timeout must not postpone the first UDP retry: expected about {expected_wait:?}, waited {elapsed:?}"
);
}
#[tokio::test]
async fn test_txn_id_generation() {
let mut client = new_direct_client(0).await.unwrap();
let mut txn_ids = Vec::new();
for index in 0..5 {
let txn_id = client.next_txn();
txn_ids.push(txn_id);
client.txn_map.insert(txn_id, index);
}
let unique: std::collections::HashSet<_> = txn_ids.iter().cloned().collect();
assert_eq!(
unique.len(),
txn_ids.len(),
"Transaction IDs should all be unique"
);
}