forked from ProvableHQ/snarkOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: reject unsolicited
PeerResponse
- Loading branch information
1 parent
dd2890e
commit e0825cd
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright (C) 2019-2023 Aleo Systems Inc. | ||
// This file is part of the snarkOS library. | ||
|
||
// 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. | ||
|
||
#[allow(dead_code)] | ||
mod common; | ||
use common::{node::*, test_peer::TestPeer}; | ||
|
||
use snarkos_node_router::{ | ||
messages::{Message, PeerResponse}, | ||
Outbound, | ||
}; | ||
use snarkos_node_tcp::P2P; | ||
|
||
use deadline::deadline; | ||
use pea2pea::{protocols::Writing, Pea2Pea}; | ||
use std::time::Duration; | ||
|
||
#[tokio::test] | ||
async fn reject_unsolicited_peer_response() { | ||
// Spin up a full node. | ||
let node = validator().await; | ||
|
||
// Spin up a test peer (synthetic node). | ||
let peer = TestPeer::validator().await; | ||
let peer_addr = peer.node().listening_addr().unwrap(); | ||
|
||
// Connect the node to the test peer. | ||
node.router().connect(peer_addr).unwrap().await.unwrap(); | ||
|
||
// Check the peer counts. | ||
let node_clone = node.clone(); | ||
deadline!(Duration::from_secs(5), move || node_clone.router().number_of_connected_peers() == 1); | ||
let node_clone = node.clone(); | ||
deadline!(Duration::from_secs(5), move || node_clone.tcp().num_connected() == 1); | ||
let peer_clone = peer.clone(); | ||
deadline!(Duration::from_secs(5), move || peer_clone.node().num_connected() == 1); | ||
|
||
// Check the candidate peers. | ||
assert_eq!(node.router().number_of_candidate_peers(), 0); | ||
|
||
let peers = vec!["1.1.1.1:1111".parse().unwrap(), "2.2.2.2:2222".parse().unwrap()]; | ||
|
||
// Send a `PeerResponse` to the node. | ||
assert!( | ||
peer.unicast( | ||
*peer.node().connected_addrs().first().unwrap(), | ||
Message::PeerResponse(PeerResponse { peers: peers.clone() }) | ||
) | ||
.is_ok() | ||
); | ||
|
||
let node_clone = node.clone(); | ||
deadline!(Duration::from_secs(5), move || node_clone.router().number_of_connected_peers() == 0); | ||
|
||
// Make sure the sent addresses weren't inserted in the candidate peers. | ||
for peer in peers { | ||
assert!(!node.router().candidate_peers().contains(&peer)); | ||
} | ||
} |