forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontract.rs
More file actions
160 lines (116 loc) · 5.65 KB
/
contract.rs
File metadata and controls
160 lines (116 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// UDP tracker documentation:
//
// BEP 15. UDP Tracker Protocol for BitTorrent
// https://www.bittorrent.org/beps/bep_0015.html
use core::panic;
use aquatic_udp_protocol::{ConnectRequest, ConnectionId, Response, TransactionId};
use torrust_tracker::shared::bit_torrent::udp::client::{new_udp_client_connected, UdpTrackerClient};
use torrust_tracker::shared::bit_torrent::udp::MAX_PACKET_SIZE;
use torrust_tracker_test_helpers::configuration;
use crate::servers::udp::asserts::is_error_response;
use crate::servers::udp::test_environment::running_test_environment;
fn empty_udp_request() -> [u8; MAX_PACKET_SIZE] {
[0; MAX_PACKET_SIZE]
}
fn empty_buffer() -> [u8; MAX_PACKET_SIZE] {
[0; MAX_PACKET_SIZE]
}
async fn send_connection_request(transaction_id: TransactionId, client: &UdpTrackerClient) -> ConnectionId {
let connect_request = ConnectRequest { transaction_id };
client.send(connect_request.into()).await;
let response = client.receive().await;
match response {
Response::Connect(connect_response) => connect_response.connection_id,
_ => panic!("error connecting to udp server {:?}", response),
}
}
#[tokio::test]
async fn should_return_a_bad_request_response_when_the_client_sends_an_empty_request() {
let test_env = running_test_environment(configuration::ephemeral()).await;
let client = new_udp_client_connected(&test_env.bind_address().to_string()).await;
client.send(&empty_udp_request()).await;
let mut buffer = empty_buffer();
client.receive(&mut buffer).await;
let response = Response::from_bytes(&buffer, true).unwrap();
assert!(is_error_response(&response, "bad request"));
}
mod receiving_a_connection_request {
use aquatic_udp_protocol::{ConnectRequest, TransactionId};
use torrust_tracker::shared::bit_torrent::udp::client::new_udp_tracker_client_connected;
use torrust_tracker_test_helpers::configuration;
use crate::servers::udp::asserts::is_connect_response;
use crate::servers::udp::test_environment::running_test_environment;
#[tokio::test]
async fn should_return_a_connect_response() {
let test_env = running_test_environment(configuration::ephemeral()).await;
let client = new_udp_tracker_client_connected(&test_env.bind_address().to_string()).await;
let connect_request = ConnectRequest {
transaction_id: TransactionId(123),
};
client.send(connect_request.into()).await;
let response = client.receive().await;
assert!(is_connect_response(&response, TransactionId(123)));
}
}
mod receiving_an_announce_request {
use std::net::Ipv4Addr;
use aquatic_udp_protocol::{
AnnounceEvent, AnnounceRequest, ConnectionId, InfoHash, NumberOfBytes, NumberOfPeers, PeerId, PeerKey, Port,
TransactionId,
};
use torrust_tracker::shared::bit_torrent::udp::client::new_udp_tracker_client_connected;
use torrust_tracker_test_helpers::configuration;
use crate::servers::udp::asserts::is_ipv4_announce_response;
use crate::servers::udp::contract::send_connection_request;
use crate::servers::udp::test_environment::running_test_environment;
#[tokio::test]
async fn should_return_an_announce_response() {
let test_env = running_test_environment(configuration::ephemeral()).await;
let client = new_udp_tracker_client_connected(&test_env.bind_address().to_string()).await;
let connection_id = send_connection_request(TransactionId(123), &client).await;
// Send announce request
let announce_request = AnnounceRequest {
connection_id: ConnectionId(connection_id.0),
transaction_id: TransactionId(123i32),
info_hash: InfoHash([0u8; 20]),
peer_id: PeerId([255u8; 20]),
bytes_downloaded: NumberOfBytes(0i64),
bytes_uploaded: NumberOfBytes(0i64),
bytes_left: NumberOfBytes(0i64),
event: AnnounceEvent::Started,
ip_address: Some(Ipv4Addr::new(0, 0, 0, 0)),
key: PeerKey(0u32),
peers_wanted: NumberOfPeers(1i32),
port: Port(client.udp_client.socket.local_addr().unwrap().port()),
};
client.send(announce_request.into()).await;
let response = client.receive().await;
assert!(is_ipv4_announce_response(&response));
}
}
mod receiving_an_scrape_request {
use aquatic_udp_protocol::{ConnectionId, InfoHash, ScrapeRequest, TransactionId};
use torrust_tracker::shared::bit_torrent::udp::client::new_udp_tracker_client_connected;
use torrust_tracker_test_helpers::configuration;
use crate::servers::udp::asserts::is_scrape_response;
use crate::servers::udp::contract::send_connection_request;
use crate::servers::udp::test_environment::running_test_environment;
#[tokio::test]
async fn should_return_a_scrape_response() {
let test_env = running_test_environment(configuration::ephemeral()).await;
let client = new_udp_tracker_client_connected(&test_env.bind_address().to_string()).await;
let connection_id = send_connection_request(TransactionId(123), &client).await;
// Send scrape request
// Full scrapes are not allowed you need to pass an array of info hashes otherwise
// it will return "bad request" error with empty vector
let info_hashes = vec![InfoHash([0u8; 20])];
let scrape_request = ScrapeRequest {
connection_id: ConnectionId(connection_id.0),
transaction_id: TransactionId(123i32),
info_hashes,
};
client.send(scrape_request.into()).await;
let response = client.receive().await;
assert!(is_scrape_response(&response));
}
}