Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-nt committed May 22, 2024
1 parent 3c78be9 commit 1cd4756
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/console/clients/udp/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,14 @@ use std::net::{SocketAddr, ToSocketAddrs};
use std::str::FromStr;

use anyhow::Context;
use aquatic_udp_protocol::Response::{self, AnnounceIpv4, AnnounceIpv6, Connect, Error, Scrape};
use aquatic_udp_protocol::{Port, TransactionId};
use aquatic_udp_protocol::{Port, Response, TransactionId};
use clap::{Parser, Subcommand};
use log::{debug, LevelFilter};
use torrust_tracker_primitives::info_hash::InfoHash as TorrustInfoHash;
use url::Url;

use crate::console::clients::udp::checker;
use crate::console::clients::udp::responses::{AnnounceResponseDto, ConnectResponseDto, ErrorResponseDto, ScrapeResponseDto};
use crate::console::clients::udp::responses_print_response_fn_example::print_response;

const ASSIGNED_BY_OS: u16 = 0;
const RANDOM_TRANSACTION_ID: i32 = -888_840_697;
Expand Down Expand Up @@ -169,7 +168,7 @@ async fn handle_scrape(tracker_socket_addr: &SocketAddr, info_hashes: &[TorrustI
.await
}

fn print_response(response: Response) -> anyhow::Result<()> {
/* fn print_response(response: Response) -> anyhow::Result<()> {
match response {
Connect(response) => {
let pretty_json = serde_json::to_string_pretty(&ConnectResponseDto::from(response))
Expand Down Expand Up @@ -199,7 +198,7 @@ fn print_response(response: Response) -> anyhow::Result<()> {
};
Ok(())
}
} */

fn parse_socket_addr(tracker_socket_addr_str: &str) -> anyhow::Result<SocketAddr> {
debug!("Tracker socket address: {tracker_socket_addr_str:#?}");
Expand Down
1 change: 1 addition & 0 deletions src/console/clients/udp/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod app;
pub mod checker;
pub mod responses;
pub mod responses_print_response_fn_example;
140 changes: 140 additions & 0 deletions src/console/clients/udp/responses_print_response_fn_example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
//! Aquatic responses are not serializable. These are the serializable wrappers.
use std::net::{Ipv4Addr, Ipv6Addr};

use anyhow::Context;
use aquatic_udp_protocol::Response::{self, AnnounceIpv4, AnnounceIpv6, Connect, Error, Scrape};
use aquatic_udp_protocol::{AnnounceResponse, ConnectResponse, ErrorResponse, Ipv4AddrBytes, Ipv6AddrBytes, ScrapeResponse};
use serde::Serialize;

pub fn print_response(response: Response) -> anyhow::Result<()> {
match response {
Connect(response) => {
let pretty_json = serde_json::to_string_pretty(&ConnectResponseDto::from(response))
.context("connect response JSON serialization")?;
println!("{pretty_json}");
}
AnnounceIpv4(response) => {
let pretty_json = serde_json::to_string_pretty(&AnnounceResponseDto::from(response))
.context("announce IPv4 response JSON serialization")?;
println!("{pretty_json}");
}
AnnounceIpv6(response) => {
let pretty_json = serde_json::to_string_pretty(&AnnounceResponseDto::from(response))
.context("announce IPv6 response JSON serialization")?;
println!("{pretty_json}");
}
Scrape(response) => {

Check warning on line 26 in src/console/clients/udp/responses_print_response_fn_example.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/udp/responses_print_response_fn_example.rs#L9-L26

Added lines #L9 - L26 were not covered by tests
let pretty_json =
serde_json::to_string_pretty(&ScrapeResponseDto::from(response)).context("scrape response JSON serialization")?;
println!("{pretty_json}");
}
Error(response) => {

Check warning on line 31 in src/console/clients/udp/responses_print_response_fn_example.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/udp/responses_print_response_fn_example.rs#L28-L31

Added lines #L28 - L31 were not covered by tests
let pretty_json =
serde_json::to_string_pretty(&ErrorResponseDto::from(response)).context("error response JSON serialization")?;
println!("{pretty_json}");
}

Check warning on line 35 in src/console/clients/udp/responses_print_response_fn_example.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/udp/responses_print_response_fn_example.rs#L33-L35

Added lines #L33 - L35 were not covered by tests
};

Ok(())
}

Check warning on line 39 in src/console/clients/udp/responses_print_response_fn_example.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/udp/responses_print_response_fn_example.rs#L38-L39

Added lines #L38 - L39 were not covered by tests

#[derive(Serialize)]

Check warning on line 41 in src/console/clients/udp/responses_print_response_fn_example.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/udp/responses_print_response_fn_example.rs#L41

Added line #L41 was not covered by tests
pub struct ConnectResponseDto {
transaction_id: i32,
connection_id: i64,
}

impl From<ConnectResponse> for ConnectResponseDto {
fn from(connect: ConnectResponse) -> Self {

Check warning on line 48 in src/console/clients/udp/responses_print_response_fn_example.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/udp/responses_print_response_fn_example.rs#L48

Added line #L48 was not covered by tests
Self {
transaction_id: connect.transaction_id.0.into(),
connection_id: connect.connection_id.0.into(),

Check warning on line 51 in src/console/clients/udp/responses_print_response_fn_example.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/udp/responses_print_response_fn_example.rs#L50-L51

Added lines #L50 - L51 were not covered by tests
}
}

Check warning on line 53 in src/console/clients/udp/responses_print_response_fn_example.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/udp/responses_print_response_fn_example.rs#L53

Added line #L53 was not covered by tests
}

#[derive(Serialize)]

Check warning on line 56 in src/console/clients/udp/responses_print_response_fn_example.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/udp/responses_print_response_fn_example.rs#L56

Added line #L56 was not covered by tests
pub struct AnnounceResponseDto {
transaction_id: i32,
announce_interval: i32,
leechers: i32,
seeders: i32,
peers: Vec<String>,
}

impl From<AnnounceResponse<Ipv4AddrBytes>> for AnnounceResponseDto {
fn from(announce: AnnounceResponse<Ipv4AddrBytes>) -> Self {
Self {
transaction_id: announce.fixed.transaction_id.0.into(),
announce_interval: announce.fixed.announce_interval.0.into(),
leechers: announce.fixed.leechers.0.into(),
seeders: announce.fixed.seeders.0.into(),
peers: announce

Check warning on line 72 in src/console/clients/udp/responses_print_response_fn_example.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/udp/responses_print_response_fn_example.rs#L66-L72

Added lines #L66 - L72 were not covered by tests
.peers
.iter()
.map(|peer| format!("{}:{}", Ipv4Addr::from(peer.ip_address), peer.port.0))

Check warning on line 75 in src/console/clients/udp/responses_print_response_fn_example.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/udp/responses_print_response_fn_example.rs#L75

Added line #L75 was not covered by tests
.collect::<Vec<_>>(),
}
}

Check warning on line 78 in src/console/clients/udp/responses_print_response_fn_example.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/udp/responses_print_response_fn_example.rs#L78

Added line #L78 was not covered by tests
}

impl From<AnnounceResponse<Ipv6AddrBytes>> for AnnounceResponseDto {
fn from(announce: AnnounceResponse<Ipv6AddrBytes>) -> Self {
Self {
transaction_id: announce.fixed.transaction_id.0.into(),
announce_interval: announce.fixed.announce_interval.0.into(),
leechers: announce.fixed.leechers.0.into(),
seeders: announce.fixed.seeders.0.into(),
peers: announce

Check warning on line 88 in src/console/clients/udp/responses_print_response_fn_example.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/udp/responses_print_response_fn_example.rs#L82-L88

Added lines #L82 - L88 were not covered by tests
.peers
.iter()
.map(|peer| format!("{}:{}", Ipv6Addr::from(peer.ip_address), peer.port.0))

Check warning on line 91 in src/console/clients/udp/responses_print_response_fn_example.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/udp/responses_print_response_fn_example.rs#L91

Added line #L91 was not covered by tests
.collect::<Vec<_>>(),
}
}

Check warning on line 94 in src/console/clients/udp/responses_print_response_fn_example.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/udp/responses_print_response_fn_example.rs#L94

Added line #L94 was not covered by tests
}

#[derive(Serialize)]

Check warning on line 97 in src/console/clients/udp/responses_print_response_fn_example.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/udp/responses_print_response_fn_example.rs#L97

Added line #L97 was not covered by tests
pub struct ScrapeResponseDto {
transaction_id: i32,
torrent_stats: Vec<TorrentStats>,
}

impl From<ScrapeResponse> for ScrapeResponseDto {
fn from(scrape: ScrapeResponse) -> Self {
Self {
transaction_id: scrape.transaction_id.0.into(),
torrent_stats: scrape

Check warning on line 107 in src/console/clients/udp/responses_print_response_fn_example.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/udp/responses_print_response_fn_example.rs#L104-L107

Added lines #L104 - L107 were not covered by tests
.torrent_stats
.iter()
.map(|torrent_scrape_statistics| TorrentStats {
seeders: torrent_scrape_statistics.seeders.0.into(),
completed: torrent_scrape_statistics.completed.0.into(),
leechers: torrent_scrape_statistics.leechers.0.into(),
})

Check warning on line 114 in src/console/clients/udp/responses_print_response_fn_example.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/udp/responses_print_response_fn_example.rs#L110-L114

Added lines #L110 - L114 were not covered by tests
.collect::<Vec<_>>(),
}
}

Check warning on line 117 in src/console/clients/udp/responses_print_response_fn_example.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/udp/responses_print_response_fn_example.rs#L117

Added line #L117 was not covered by tests
}

#[derive(Serialize)]

Check warning on line 120 in src/console/clients/udp/responses_print_response_fn_example.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/udp/responses_print_response_fn_example.rs#L120

Added line #L120 was not covered by tests
pub struct ErrorResponseDto {
transaction_id: i32,
message: String,
}

impl From<ErrorResponse> for ErrorResponseDto {
fn from(error: ErrorResponse) -> Self {
Self {
transaction_id: error.transaction_id.0.into(),
message: error.message.to_string(),

Check warning on line 130 in src/console/clients/udp/responses_print_response_fn_example.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/udp/responses_print_response_fn_example.rs#L127-L130

Added lines #L127 - L130 were not covered by tests
}
}

Check warning on line 132 in src/console/clients/udp/responses_print_response_fn_example.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/udp/responses_print_response_fn_example.rs#L132

Added line #L132 was not covered by tests
}

#[derive(Serialize)]

Check warning on line 135 in src/console/clients/udp/responses_print_response_fn_example.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/udp/responses_print_response_fn_example.rs#L135

Added line #L135 was not covered by tests
struct TorrentStats {
seeders: i32,
completed: i32,
leechers: i32,
}

0 comments on commit 1cd4756

Please sign in to comment.