From 32286ccd7aa8776bbcff2f72ed24a2dd14a5c0d6 Mon Sep 17 00:00:00 2001 From: Antioch Peverell Date: Thu, 12 Sep 2019 10:37:29 +0100 Subject: [PATCH] add some test coverage around peers map (peer_addr hashing impl) (#3039) --- p2p/tests/peer_addr.rs | 55 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 p2p/tests/peer_addr.rs diff --git a/p2p/tests/peer_addr.rs b/p2p/tests/peer_addr.rs new file mode 100644 index 000000000..7ac3a9abd --- /dev/null +++ b/p2p/tests/peer_addr.rs @@ -0,0 +1,55 @@ +// Copyright 2019 The Grin Developers +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::collections::HashMap; +use std::net::{IpAddr, Ipv4Addr, SocketAddr}; + +use grin_p2p as p2p; + +use crate::p2p::types::PeerAddr; + +// Test the behavior of a hashmap of peers keyed by peer_addr. +#[test] +fn test_peer_addr_hashing() { + let mut peers: HashMap = HashMap::new(); + + let socket_addr1 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1)), 8080); + let peer_addr1 = PeerAddr(socket_addr1); + peers.insert(peer_addr1, "peer1".into()); + + assert!(peers.contains_key(&peer_addr1)); + assert_eq!(peers.len(), 1); + + let socket_addr2 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1)), 8081); + let peer_addr2 = PeerAddr(socket_addr2); + + // Expected behavior here is to ignore the port when hashing peer_addr. + // This means the two peer_addr instances above are seen as the same addr. + assert!(peers.contains_key(&peer_addr1)); + assert!(peers.contains_key(&peer_addr2)); + + peers.insert(peer_addr2, "peer2".into()); + + // Inserting the second instance is a no-op as they are treated as the same addr. + assert!(peers.contains_key(&peer_addr1)); + assert!(peers.contains_key(&peer_addr2)); + assert_eq!(peers.len(), 1); + + // Check they are treated as the same even though their underlying ports are different. + assert_eq!(peer_addr1, peer_addr2); + assert_eq!(peer_addr1.0, socket_addr1); + assert_eq!(peer_addr2.0, socket_addr2); + assert_eq!(peer_addr1.0.port(), 8080); + assert_eq!(peer_addr2.0.port(), 8081); +}