2018-03-05 22:33:44 +03:00
|
|
|
// Copyright 2018 The Grin Developers
|
2016-10-30 18:24:19 +03:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2018-12-08 02:59:40 +03:00
|
|
|
use grin_core as core;
|
|
|
|
use grin_p2p as p2p;
|
|
|
|
|
|
|
|
use grin_util as util;
|
2018-12-11 14:07:41 +03:00
|
|
|
use grin_util::{Mutex, StopState};
|
2016-10-30 18:24:19 +03:00
|
|
|
|
2018-02-02 05:03:12 +03:00
|
|
|
use std::net::{SocketAddr, TcpListener, TcpStream};
|
2018-09-19 01:12:57 +03:00
|
|
|
use std::sync::Arc;
|
2018-06-14 15:16:14 +03:00
|
|
|
use std::{thread, time};
|
2016-10-30 18:24:19 +03:00
|
|
|
|
2018-12-08 02:59:40 +03:00
|
|
|
use crate::core::core::hash::Hash;
|
|
|
|
use crate::core::pow::Difficulty;
|
2019-02-18 15:15:32 +03:00
|
|
|
use crate::p2p::types::PeerAddr;
|
2018-12-08 02:59:40 +03:00
|
|
|
use crate::p2p::Peer;
|
2016-11-06 02:31:45 +03:00
|
|
|
|
2018-01-03 04:53:50 +03:00
|
|
|
fn open_port() -> u16 {
|
|
|
|
// use port 0 to allow the OS to assign an open port
|
|
|
|
// TcpListener's Drop impl will unbind the port as soon as
|
|
|
|
// listener goes out of scope
|
|
|
|
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
|
|
|
listener.local_addr().unwrap().port()
|
|
|
|
}
|
|
|
|
|
2017-06-18 02:15:46 +03:00
|
|
|
// Starts a server and connects a client peer to it to check handshake,
|
|
|
|
// followed by a ping/pong exchange to make sure the connection is live.
|
2016-10-30 18:24:19 +03:00
|
|
|
#[test]
|
|
|
|
fn peer_handshake() {
|
2018-02-05 22:52:11 +03:00
|
|
|
util::init_test_logger();
|
|
|
|
|
2018-05-30 23:57:13 +03:00
|
|
|
let p2p_config = p2p::P2PConfig {
|
2019-04-12 21:19:41 +03:00
|
|
|
host: "127.0.0.1".parse().unwrap(),
|
2018-01-31 00:44:13 +03:00
|
|
|
port: open_port(),
|
|
|
|
peers_allow: None,
|
|
|
|
peers_deny: None,
|
2018-03-27 20:17:01 +03:00
|
|
|
..p2p::P2PConfig::default()
|
2018-01-03 04:53:50 +03:00
|
|
|
};
|
2017-12-14 00:30:59 +03:00
|
|
|
let net_adapter = Arc::new(p2p::DummyAdapter {});
|
2018-03-04 03:19:54 +03:00
|
|
|
let server = Arc::new(
|
|
|
|
p2p::Server::new(
|
2019-02-27 12:47:46 +03:00
|
|
|
".grin",
|
2018-03-04 03:19:54 +03:00
|
|
|
p2p::Capabilities::UNKNOWN,
|
2018-05-30 23:57:13 +03:00
|
|
|
p2p_config.clone(),
|
2018-03-04 03:19:54 +03:00
|
|
|
net_adapter.clone(),
|
2018-06-01 22:41:26 +03:00
|
|
|
Hash::from_vec(&vec![]),
|
2018-12-11 14:07:41 +03:00
|
|
|
Arc::new(Mutex::new(StopState::new())),
|
2018-12-08 02:59:40 +03:00
|
|
|
)
|
|
|
|
.unwrap(),
|
2018-03-04 03:19:54 +03:00
|
|
|
);
|
2018-02-02 05:03:12 +03:00
|
|
|
|
|
|
|
let p2p_inner = server.clone();
|
2018-03-04 03:19:54 +03:00
|
|
|
let _ = thread::spawn(move || p2p_inner.listen());
|
2018-02-02 05:03:12 +03:00
|
|
|
|
|
|
|
thread::sleep(time::Duration::from_secs(1));
|
|
|
|
|
2018-05-30 23:57:13 +03:00
|
|
|
let addr = SocketAddr::new(p2p_config.host, p2p_config.port);
|
2019-05-03 17:56:25 +03:00
|
|
|
let socket = TcpStream::connect_timeout(&addr, time::Duration::from_secs(10)).unwrap();
|
2018-02-02 05:03:12 +03:00
|
|
|
|
2019-02-18 15:15:32 +03:00
|
|
|
let my_addr = PeerAddr("127.0.0.1:5000".parse().unwrap());
|
2019-05-03 17:56:25 +03:00
|
|
|
let peer = Peer::connect(
|
|
|
|
socket,
|
2018-02-02 05:03:12 +03:00
|
|
|
p2p::Capabilities::UNKNOWN,
|
2018-10-18 22:18:16 +03:00
|
|
|
Difficulty::min(),
|
2018-02-02 05:03:12 +03:00
|
|
|
my_addr,
|
2018-06-01 22:41:26 +03:00
|
|
|
&p2p::handshake::Handshake::new(Hash::from_vec(&vec![]), p2p_config.clone()),
|
2018-02-02 05:03:12 +03:00
|
|
|
net_adapter,
|
2018-12-08 02:59:40 +03:00
|
|
|
)
|
|
|
|
.unwrap();
|
2016-10-31 22:29:08 +03:00
|
|
|
|
2018-03-27 15:52:59 +03:00
|
|
|
assert!(peer.info.user_agent.ends_with(env!("CARGO_PKG_VERSION")));
|
|
|
|
|
2018-02-02 05:03:12 +03:00
|
|
|
thread::sleep(time::Duration::from_secs(1));
|
2016-10-31 22:29:08 +03:00
|
|
|
|
2018-10-18 22:18:16 +03:00
|
|
|
peer.send_ping(Difficulty::min(), 0).unwrap();
|
2018-02-02 05:03:12 +03:00
|
|
|
thread::sleep(time::Duration::from_secs(1));
|
2018-03-04 03:19:54 +03:00
|
|
|
|
2019-02-18 15:15:32 +03:00
|
|
|
let server_peer = server.peers.get_connected_peer(my_addr).unwrap();
|
2018-10-18 22:18:16 +03:00
|
|
|
assert_eq!(server_peer.info.total_difficulty(), Difficulty::min());
|
2018-02-02 05:03:12 +03:00
|
|
|
assert!(server.peers.peer_count() > 0);
|
2016-10-30 18:24:19 +03:00
|
|
|
}
|