mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-14 02:21:21 +00:00
Add some regression tests in libp2p-core (#237)
This commit is contained in:
@ -21,4 +21,5 @@ tokio-io = "0.1"
|
|||||||
libp2p-ping = { path = "../ping" }
|
libp2p-ping = { path = "../ping" }
|
||||||
libp2p-tcp-transport = { path = "../tcp-transport" }
|
libp2p-tcp-transport = { path = "../tcp-transport" }
|
||||||
libp2p-mplex = { path = "../mplex" }
|
libp2p-mplex = { path = "../mplex" }
|
||||||
|
rand = "0.5"
|
||||||
tokio-core = "0.1"
|
tokio-core = "0.1"
|
||||||
|
@ -218,6 +218,9 @@ extern crate quick_error;
|
|||||||
extern crate smallvec;
|
extern crate smallvec;
|
||||||
extern crate tokio_io;
|
extern crate tokio_io;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
extern crate rand;
|
||||||
|
|
||||||
/// Multi-address re-export.
|
/// Multi-address re-export.
|
||||||
pub extern crate multiaddr;
|
pub extern crate multiaddr;
|
||||||
|
|
||||||
|
@ -189,3 +189,43 @@ impl FromStr for PeerId {
|
|||||||
PeerId::from_bytes(bytes).map_err(|_| ParseError::MultiHash)
|
PeerId::from_bytes(bytes).map_err(|_| ParseError::MultiHash)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use rand::random;
|
||||||
|
use {PeerId, PublicKeyBytes, PublicKeyBytesSlice};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn pubkey_as_slice_to_owned() {
|
||||||
|
let key = PublicKeyBytes((0 .. 2048).map(|_| -> u8 { random() }).collect());
|
||||||
|
assert_eq!(key.clone().as_slice().to_owned(), key);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn peer_id_is_public_key() {
|
||||||
|
let key = (0 .. 2048).map(|_| -> u8 { random() }).collect::<Vec<u8>>();
|
||||||
|
let peer_id = PeerId::from_public_key(PublicKeyBytesSlice(&key));
|
||||||
|
assert_eq!(peer_id.is_public_key(PublicKeyBytesSlice(&key)), Some(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn pubkey_to_peer_id() {
|
||||||
|
let key = PublicKeyBytes((0 .. 2048).map(|_| -> u8 { random() }).collect());
|
||||||
|
let peer_id = key.to_peer_id();
|
||||||
|
assert_eq!(peer_id.is_public_key(key.as_slice()), Some(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn peer_id_into_bytes_then_from_bytes() {
|
||||||
|
let peer_id = PublicKeyBytes((0 .. 2048).map(|_| -> u8 { random() }).collect()).to_peer_id();
|
||||||
|
let second = PeerId::from_bytes(peer_id.clone().into_bytes()).unwrap();
|
||||||
|
assert_eq!(peer_id, second);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn peer_id_to_base58_then_back() {
|
||||||
|
let peer_id = PublicKeyBytes((0 .. 2048).map(|_| -> u8 { random() }).collect()).to_peer_id();
|
||||||
|
let second: PeerId = peer_id.to_base58().parse().unwrap();
|
||||||
|
assert_eq!(peer_id, second);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user