Skip to content

Commit

Permalink
Merge #245: Add prefix 0x to peer ID hex string
Browse files Browse the repository at this point in the history
d51aae0 feat(tracker): [#164] add prefix 0x to peer ID hex string (Jose Celano)

Pull request description:

  A peer id like this:

  ```rust
  peer::Id(*b"-qB00000000000000000")
  ```

  has now this hex string representation:

  ```s
  0x2d71423030303030303030303030303030303030
  ```

  with the `0x` prefix as suggested by @da2ce7 [here](#164 (comment)).

Top commit has no ACKs.

Tree-SHA512: 28d5522e14bf6e2159d7c0e3377a4b5d2242b45124d01403b50d32bf01e8c4e48eb8f86f1e1bbccea062ae2d48f7f2e3a9aa1c79dd9ae7593996c73e9dac3afa
  • Loading branch information
josecelano committed Mar 16, 2023
2 parents 7206407 + d51aae0 commit 0bb295c
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions src/tracker/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,13 @@ impl Id {
pub fn to_hex_string(&self) -> Option<String> {
let buff_size = self.0.len() * 2;
let mut tmp: Vec<u8> = vec![0; buff_size];

binascii::bin2hex(&self.0, &mut tmp).unwrap();
std::str::from_utf8(&tmp).ok().map(std::string::ToString::to_string)

match std::str::from_utf8(&tmp) {
Ok(hex) => Some(format!("0x{hex}")),
Err(_) => None,
}
}

#[must_use]
Expand Down Expand Up @@ -360,23 +365,23 @@ mod test {
#[test]
fn should_be_converted_to_hex_string() {
let id = peer::Id(*b"-qB00000000000000000");
assert_eq!(id.to_hex_string().unwrap(), "2d71423030303030303030303030303030303030");
assert_eq!(id.to_hex_string().unwrap(), "0x2d71423030303030303030303030303030303030");

let id = peer::Id([
0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150,
]);
assert_eq!(id.to_hex_string().unwrap(), "009f9296009f9296009f9296009f9296009f9296");
assert_eq!(id.to_hex_string().unwrap(), "0x009f9296009f9296009f9296009f9296009f9296");
}

#[test]
fn should_be_converted_into_string_type_using_the_hex_string_format() {
let id = peer::Id(*b"-qB00000000000000000");
assert_eq!(id.to_string(), "2d71423030303030303030303030303030303030");
assert_eq!(id.to_string(), "0x2d71423030303030303030303030303030303030");

let id = peer::Id([
0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150,
]);
assert_eq!(id.to_string(), "009f9296009f9296009f9296009f9296009f9296");
assert_eq!(id.to_string(), "0x009f9296009f9296009f9296009f9296009f9296");
}

#[test]
Expand All @@ -390,6 +395,7 @@ mod test {
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes};
use serde_json::Value;

use crate::protocol::clock::{Current, Time};
use crate::tracker::peer::{self, Peer};
Expand All @@ -406,12 +412,26 @@ mod test {
event: AnnounceEvent::Started,
};

let json_serialized_value = serde_json::to_string(&torrent_peer).unwrap();
let raw_json = serde_json::to_string(&torrent_peer).unwrap();

let expected_raw_json = r#"
{
"peer_id": {
"id": "0x2d71423030303030303030303030303030303030",
"client": "qBittorrent"
},
"peer_addr":"126.0.0.1:8080",
"updated":0,
"uploaded":0,
"downloaded":0,
"left":0,
"event":"Started"
}
"#;

assert_eq!(
json_serialized_value,
// todo: compare using pretty json format to improve readability
r#"{"peer_id":{"id":"2d71423030303030303030303030303030303030","client":"qBittorrent"},"peer_addr":"126.0.0.1:8080","updated":0,"uploaded":0,"downloaded":0,"left":0,"event":"Started"}"#
serde_json::from_str::<Value>(&raw_json).unwrap(),
serde_json::from_str::<Value>(expected_raw_json).unwrap()
);
}
}
Expand Down

0 comments on commit 0bb295c

Please sign in to comment.