Skip to content

Commit

Permalink
Add apply_condition unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex6323 committed Oct 19, 2021
1 parent 536f54d commit ac4fd85
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
19 changes: 16 additions & 3 deletions bee-autopeering/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl Filter {

#[cfg(test)]
mod tests {
use crate::service_map::AUTOPEERING_SERVICE_NAME;
use crate::service_map::{ServiceProtocol, AUTOPEERING_SERVICE_NAME};

use super::*;

Expand All @@ -75,18 +75,31 @@ mod tests {
}

#[test]
fn add_condition() {
fn apply_condition() {
let mut filter = Filter::new();
assert_eq!(0, filter.num_conditions());

let condition = |peer: &Peer| -> bool { peer.services().port(AUTOPEERING_SERVICE_NAME).unwrap() == 8080 };
let condition = |peer: &Peer| -> bool { peer.services().port(AUTOPEERING_SERVICE_NAME).unwrap() == 1337 };
let condition = Box::new(condition);

filter.add_condition(condition);
assert_eq!(1, filter.num_conditions());

let mut peer1 = Peer::new_test_peer(1);
let mut peer2 = Peer::new_test_peer(2);

peer1.add_service(AUTOPEERING_SERVICE_NAME, ServiceProtocol::Udp, 6969);
peer1.add_service(AUTOPEERING_SERVICE_NAME, ServiceProtocol::Udp, 1337);

let candidates = [peer1, peer2];
let filtered = filter.apply(&candidates);
assert_eq!(1, filtered.len());

filter.clear_conditions();
assert_eq!(0, filter.num_conditions());

let filtered = filter.apply(&candidates);
assert_eq!(2, filtered.len());
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions bee-autopeering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ mod proto {
mod request;
mod salt;
mod server;
mod service_map;
mod time;

pub mod identity;
pub mod init;
pub mod peer;
pub mod peerstore;
pub mod service_map;

pub use identity::PeerId;
pub use init::init;
pub use local::Local;
pub use peer::Peer;
pub use service_map::{ServiceMap, ServiceName};
pub use service_map::{ServiceMap, ServiceName, ServiceProtocol};
26 changes: 24 additions & 2 deletions bee-autopeering/src/peer.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
// Copyright 2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use crate::{identity::PeerId, proto, service_map::ServiceMap};
use crate::{
identity::PeerId,
proto,
service_map::{ServiceMap, ServiceProtocol},
};

use bytes::BytesMut;
use crypto::signatures::ed25519::PublicKey;
use prost::{DecodeError, EncodeError, Message};
// use serde::{Deserialize, Serialize};

use std::{convert::TryInto, fmt, net::IpAddr};
use std::{
convert::TryInto,
fmt,
net::{IpAddr, Ipv4Addr},
};

/// Represents a peer.
// #[derive(Serialize, Deserialize)]
Expand Down Expand Up @@ -49,6 +57,20 @@ impl Peer {
&self.services
}

pub fn add_service(&mut self, service_name: impl ToString, protocol: ServiceProtocol, port: u16) {
let ipv = match self.ip_address {
IpAddr::V4(ip4) => "ip4",
IpAddr::V6(ip6) => "ip6",
};

// Unwrap: we control what is being parsed.
let multiaddr = format!("/{}/{}/{}/{}", ipv, self.ip_address, protocol, port)
.parse()
.unwrap();

self.services.insert(service_name.to_string(), multiaddr);
}

/// Creates a discovered peer from its Protobuf representation/encoding.
pub fn from_protobuf(bytes: &[u8]) -> Result<Self, DecodeError> {
Ok(proto::Peer::decode(bytes)?.into())
Expand Down
15 changes: 15 additions & 0 deletions bee-autopeering/src/service_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,21 @@ impl fmt::Display for ServiceMap {
}
}

pub enum ServiceProtocol {
Tcp,
Udp,
}

impl fmt::Display for ServiceProtocol {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let protocol = match self {
ServiceProtocol::Udp => "udp",
ServiceProtocol::Tcp => "tcp",
};
write!(f, "{}", protocol)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit ac4fd85

Please sign in to comment.