From 12a62f5df43f0f055b1f767caad527f8c494b802 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Sun, 19 Jul 2020 14:23:48 +0200 Subject: [PATCH] aquatic_http: add test for announce request parsing --- TODO.md | 2 +- aquatic_http/src/lib/protocol/common.rs | 2 +- aquatic_http/src/lib/protocol/request.rs | 30 +++++++++++++++++++++--- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/TODO.md b/TODO.md index f8f0cab..2610c45 100644 --- a/TODO.md +++ b/TODO.md @@ -10,7 +10,7 @@ ## aquatic_http * 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 tls * current serialized byte strings valid diff --git a/aquatic_http/src/lib/protocol/common.rs b/aquatic_http/src/lib/protocol/common.rs index a4ef3de..a44bc4e 100644 --- a/aquatic_http/src/lib/protocol/common.rs +++ b/aquatic_http/src/lib/protocol/common.rs @@ -25,7 +25,7 @@ pub struct InfoHash( ); -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum AnnounceEvent { Started, Stopped, diff --git a/aquatic_http/src/lib/protocol/request.rs b/aquatic_http/src/lib/protocol/request.rs index a3b1c9f..793fd73 100644 --- a/aquatic_http/src/lib/protocol/request.rs +++ b/aquatic_http/src/lib/protocol/request.rs @@ -5,7 +5,7 @@ use super::common::*; use super::utils::*; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct AnnounceRequest { pub info_hash: InfoHash, pub peer_id: PeerId, @@ -19,13 +19,13 @@ pub struct AnnounceRequest { } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct ScrapeRequest { pub info_hashes: Vec, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum Request { Announce(AnnounceRequest), Scrape(ScrapeRequest), @@ -277,6 +277,10 @@ impl Request { mod tests { 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] fn test_urldecode(){ let f = Request::urldecode_memchr; @@ -289,4 +293,24 @@ mod tests { assert!(f("%").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); + } } \ No newline at end of file