aquatic_http: add test for announce request parsing

This commit is contained in:
Joakim Frostegård 2020-07-19 14:23:48 +02:00
parent f078542ffc
commit 12a62f5df4
3 changed files with 29 additions and 5 deletions

View file

@ -10,7 +10,7 @@
## aquatic_http ## aquatic_http
* request parsing: * request parsing:
* tests of main function and the various helper functions * add test of scrape request parsing with multiple info hashes
* test torrent transfer with real clients * test torrent transfer with real clients
* test tls * test tls
* current serialized byte strings valid * current serialized byte strings valid

View file

@ -25,7 +25,7 @@ pub struct InfoHash(
); );
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum AnnounceEvent { pub enum AnnounceEvent {
Started, Started,
Stopped, Stopped,

View file

@ -5,7 +5,7 @@ use super::common::*;
use super::utils::*; use super::utils::*;
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct AnnounceRequest { pub struct AnnounceRequest {
pub info_hash: InfoHash, pub info_hash: InfoHash,
pub peer_id: PeerId, pub peer_id: PeerId,
@ -19,13 +19,13 @@ pub struct AnnounceRequest {
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScrapeRequest { pub struct ScrapeRequest {
pub info_hashes: Vec<InfoHash>, pub info_hashes: Vec<InfoHash>,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum Request { pub enum Request {
Announce(AnnounceRequest), Announce(AnnounceRequest),
Scrape(ScrapeRequest), Scrape(ScrapeRequest),
@ -277,6 +277,10 @@ impl Request {
mod tests { mod tests {
use super::*; use super::*;
static ANNOUNCE_REQUEST_PATH: &str = "/announce?info_hash=%04%0bkV%3f%5cr%14%a6%b7%98%adC%c3%c9.%40%24%00%b9&peer_id=-ABC940-5ert69muw5t8&port=12345&uploaded=0&downloaded=0&left=1&numwant=0&key=4ab4b877&compact=1&supportcrypto=1&event=started";
static REFERENCE_INFO_HASH: [u8; 20] = [0x04, 0x0b, b'k', b'V', 0x3f, 0x5c, b'r', 0x14, 0xa6, 0xb7, 0x98, 0xad, b'C', 0xc3, 0xc9, b'.', 0x40, 0x24, 0x00, 0xb9];
static REFERENCE_PEER_ID: [u8; 20] = [b'-', b'A', b'B', b'C', b'9', b'4', b'0', b'-', b'5', b'e', b'r', b't', b'6', b'9', b'm', b'u', b'w', b'5', b't', b'8'];
#[test] #[test]
fn test_urldecode(){ fn test_urldecode(){
let f = Request::urldecode_memchr; let f = Request::urldecode_memchr;
@ -289,4 +293,24 @@ mod tests {
assert!(f("%").is_err()); assert!(f("%").is_err());
assert!(f("%å7").is_err()); assert!(f("%å7").is_err());
} }
#[test]
fn test_announce_request_from_path(){
let parsed_request = Request::from_http_get_path(
ANNOUNCE_REQUEST_PATH
).unwrap();
let reference_request = Request::Announce(AnnounceRequest {
info_hash: InfoHash(REFERENCE_INFO_HASH),
peer_id: PeerId(REFERENCE_PEER_ID),
port: 12345,
bytes_left: 1,
event: AnnounceEvent::Started,
compact: true,
numwant: Some(0),
key: Some("4ab4b877".to_string())
});
assert_eq!(parsed_request, reference_request);
}
} }