mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-01 18:25:30 +00:00
udp: integration: add test for invalid connection id, refactor
This commit is contained in:
parent
589d45a05d
commit
48e383b6a9
1 changed files with 59 additions and 23 deletions
|
|
@ -8,17 +8,16 @@ use std::{
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use aquatic_udp::{common::BUFFER_SIZE, config::Config};
|
use aquatic_udp::{common::BUFFER_SIZE, config::Config};
|
||||||
use aquatic_udp_protocol::{
|
use aquatic_udp_protocol::{
|
||||||
common::PeerId, AnnounceEvent, AnnounceRequest, AnnounceResponse, ConnectRequest, ConnectionId,
|
common::PeerId, AnnounceEvent, AnnounceRequest, ConnectRequest, ConnectionId, InfoHash,
|
||||||
InfoHash, NumberOfBytes, NumberOfPeers, PeerKey, Port, Request, Response, ScrapeRequest,
|
NumberOfBytes, NumberOfPeers, PeerKey, Port, Request, Response, ScrapeRequest, ScrapeResponse,
|
||||||
ScrapeResponse, TransactionId,
|
TransactionId,
|
||||||
};
|
};
|
||||||
|
|
||||||
const PEERS_WANTED: usize = 10;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_multiple_connect_announce_scrape() -> anyhow::Result<()> {
|
fn test_multiple_connect_announce_scrape() -> anyhow::Result<()> {
|
||||||
const TRACKER_PORT: u16 = 40_111;
|
const TRACKER_PORT: u16 = 40_111;
|
||||||
const PEER_PORT_START: u16 = 30_000;
|
const PEER_PORT_START: u16 = 30_000;
|
||||||
|
const PEERS_WANTED: usize = 10;
|
||||||
|
|
||||||
let mut config = Config::default();
|
let mut config = Config::default();
|
||||||
|
|
||||||
|
|
@ -48,15 +47,24 @@ fn test_multiple_connect_announce_scrape() -> anyhow::Result<()> {
|
||||||
|
|
||||||
let connection_id = connect(&socket, tracker_addr).with_context(|| "connect")?;
|
let connection_id = connect(&socket, tracker_addr).with_context(|| "connect")?;
|
||||||
|
|
||||||
let announce_response = announce(
|
let announce_response = {
|
||||||
&socket,
|
let response = announce(
|
||||||
tracker_addr,
|
&socket,
|
||||||
connection_id,
|
tracker_addr,
|
||||||
PEER_PORT_START + i as u16,
|
connection_id,
|
||||||
info_hash,
|
PEER_PORT_START + i as u16,
|
||||||
is_seeder,
|
info_hash,
|
||||||
)
|
PEERS_WANTED,
|
||||||
.with_context(|| "announce")?;
|
is_seeder,
|
||||||
|
)
|
||||||
|
.with_context(|| "announce")?;
|
||||||
|
|
||||||
|
if let Response::AnnounceIpv4(response) = response {
|
||||||
|
response
|
||||||
|
} else {
|
||||||
|
return Err(anyhow::anyhow!("not announce response: {:?}", response));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
assert_eq!(announce_response.peers.len(), i.min(PEERS_WANTED));
|
assert_eq!(announce_response.peers.len(), i.min(PEERS_WANTED));
|
||||||
|
|
||||||
|
|
@ -91,6 +99,39 @@ fn test_multiple_connect_announce_scrape() -> anyhow::Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_announce_with_invalid_connection_id() -> anyhow::Result<()> {
|
||||||
|
const TRACKER_PORT: u16 = 40_112;
|
||||||
|
|
||||||
|
let mut config = Config::default();
|
||||||
|
|
||||||
|
config.network.address.set_port(TRACKER_PORT);
|
||||||
|
|
||||||
|
let tracker_addr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, TRACKER_PORT));
|
||||||
|
let peer_addr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 0));
|
||||||
|
|
||||||
|
run_tracker(config);
|
||||||
|
|
||||||
|
let socket = UdpSocket::bind(peer_addr)?;
|
||||||
|
socket.set_read_timeout(Some(Duration::from_secs(1)))?;
|
||||||
|
|
||||||
|
let res_response = announce(
|
||||||
|
&socket,
|
||||||
|
tracker_addr,
|
||||||
|
ConnectionId(0),
|
||||||
|
1,
|
||||||
|
InfoHash([0; 20]),
|
||||||
|
100,
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
|
||||||
|
// No response should be sent by tracker. Ideally, we would like to test
|
||||||
|
// that the error is in fact a would-block one on the socket.
|
||||||
|
assert!(matches!(res_response, Err(_)));
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
// FIXME: should ideally try different ports and use sync primitives to find
|
// FIXME: should ideally try different ports and use sync primitives to find
|
||||||
// out if tracker was successfully started
|
// out if tracker was successfully started
|
||||||
fn run_tracker(config: Config) {
|
fn run_tracker(config: Config) {
|
||||||
|
|
@ -121,8 +162,9 @@ fn announce(
|
||||||
connection_id: ConnectionId,
|
connection_id: ConnectionId,
|
||||||
peer_port: u16,
|
peer_port: u16,
|
||||||
info_hash: InfoHash,
|
info_hash: InfoHash,
|
||||||
|
peers_wanted: usize,
|
||||||
seeder: bool,
|
seeder: bool,
|
||||||
) -> anyhow::Result<AnnounceResponse<Ipv4Addr>> {
|
) -> anyhow::Result<Response> {
|
||||||
let mut peer_id = PeerId([0; 20]);
|
let mut peer_id = PeerId([0; 20]);
|
||||||
|
|
||||||
for chunk in peer_id.0.chunks_exact_mut(2) {
|
for chunk in peer_id.0.chunks_exact_mut(2) {
|
||||||
|
|
@ -140,17 +182,11 @@ fn announce(
|
||||||
event: AnnounceEvent::Started,
|
event: AnnounceEvent::Started,
|
||||||
ip_address: None,
|
ip_address: None,
|
||||||
key: PeerKey(0),
|
key: PeerKey(0),
|
||||||
peers_wanted: NumberOfPeers(PEERS_WANTED as i32),
|
peers_wanted: NumberOfPeers(peers_wanted as i32),
|
||||||
port: Port(peer_port),
|
port: Port(peer_port),
|
||||||
});
|
});
|
||||||
|
|
||||||
let response = request_and_response(&socket, tracker_addr, request)?;
|
Ok(request_and_response(&socket, tracker_addr, request)?)
|
||||||
|
|
||||||
if let Response::AnnounceIpv4(response) = response {
|
|
||||||
Ok(response)
|
|
||||||
} else {
|
|
||||||
return Err(anyhow::anyhow!("not announce response: {:?}", response));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn scrape(
|
fn scrape(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue