Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test for when bs-client doesn't send complete message #4078

Merged
merged 3 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion massa-bootstrap/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ trait BindingReadExact: io::Read {
}
if count != buf.len() {
Err((
std::io::Error::new(ErrorKind::UnexpectedEof, "failed to fill whole buffer"),
std::io::Error::new(
ErrorKind::UnexpectedEof,
format!("failed to fill whole buffer: {}/{}", count, buf.len()),
),
count,
))
} else {
Expand Down
89 changes: 74 additions & 15 deletions massa-bootstrap/src/tests/binders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ fn test_binders() {
let addr = server.local_addr().unwrap();
let client = std::net::TcpStream::connect(addr).unwrap();
let server = server.accept().unwrap();
let version = || Version::from_str("TEST.1.10").unwrap();

let mut server = BootstrapServerBinder::new(
server.0,
Expand Down Expand Up @@ -120,9 +121,7 @@ fn test_binders() {
peers: BootstrapPeers(vector_peers.clone()),
};

let version: Version = Version::from_str("TEST.1.10").unwrap();

server.handshake_timeout(version, None).unwrap();
server.handshake_timeout(version(), None).unwrap();

server
.send_timeout(test_peers_message.clone(), None)
Expand Down Expand Up @@ -174,9 +173,7 @@ fn test_binders() {
);
let vector_peers = vec![(peer_id1, listeners)];

let version: Version = Version::from_str("TEST.1.10").unwrap();

client.handshake(version).unwrap();
client.handshake(version()).unwrap();
let message = client.next_timeout(None).unwrap();
match message {
BootstrapServerMessage::BootstrapPeers { peers } => {
Expand Down Expand Up @@ -228,6 +225,7 @@ fn test_binders_double_send_server_works() {
let server = std::net::TcpListener::bind("localhost:0").unwrap();
let client = std::net::TcpStream::connect(server.local_addr().unwrap()).unwrap();
let server = server.accept().unwrap();
let version = || Version::from_str("TEST.1.10").unwrap();

let mut server = BootstrapServerBinder::new(
server.0,
Expand Down Expand Up @@ -270,9 +268,7 @@ fn test_binders_double_send_server_works() {
peers: BootstrapPeers(vector_peers.clone()),
};

let version: Version = Version::from_str("TEST.1.10").unwrap();

server.handshake_timeout(version, None).unwrap();
server.handshake_timeout(version(), None).unwrap();
server
.send_timeout(test_peers_message.clone(), None)
.unwrap();
Expand Down Expand Up @@ -314,9 +310,8 @@ fn test_binders_double_send_server_works() {
TransportType::Tcp,
);
let vector_peers = vec![(peer_id1, listeners.clone())];
let version: Version = Version::from_str("TEST.1.10").unwrap();

client.handshake(version).unwrap();
client.handshake(version()).unwrap();
let message = client.next_timeout(None).unwrap();
match message {
BootstrapServerMessage::BootstrapPeers { peers } => {
Expand Down Expand Up @@ -376,6 +371,7 @@ fn test_binders_try_double_send_client_works() {
client,
bootstrap_config.bootstrap_list[0].1.get_public_key(),
);
let version = || Version::from_str("TEST.1.10").unwrap();

let peer_id1 = PeerId::from_public_key(KeyPair::generate(0).unwrap().get_public_key());

Expand All @@ -394,9 +390,8 @@ fn test_binders_try_double_send_client_works() {
let test_peers_message = BootstrapServerMessage::BootstrapPeers {
peers: BootstrapPeers(vector_peers.clone()),
};
let version: Version = Version::from_str("TEST.1.10").unwrap();

server.handshake_timeout(version, None).unwrap();
server.handshake_timeout(version(), None).unwrap();
server
.send_timeout(test_peers_message.clone(), None)
.unwrap();
Expand Down Expand Up @@ -436,9 +431,8 @@ fn test_binders_try_double_send_client_works() {
TransportType::Tcp,
);
let vector_peers = vec![(peer_id1.clone(), listeners.clone())];
let version: Version = Version::from_str("TEST.1.10").unwrap();

client.handshake(version).unwrap();
client.handshake(version()).unwrap();
let message = client.next_timeout(None).unwrap();
match message {
BootstrapServerMessage::BootstrapPeers { peers } => {
Expand Down Expand Up @@ -487,6 +481,71 @@ fn test_binders_try_double_send_client_works() {
client_thread.join().unwrap();
}

#[test]
fn test_partial_msg() {
let (bootstrap_config, server_keypair): &(BootstrapConfig, KeyPair) = &BOOTSTRAP_CONFIG_KEYPAIR;
let server = std::net::TcpListener::bind("localhost:0").unwrap();
let addr = server.local_addr().unwrap();
let client = std::net::TcpStream::connect(addr).unwrap();
let mut client_clone = client.try_clone().unwrap();
let server = server.accept().unwrap();
let version = || Version::from_str("TEST.1.10").unwrap();

let mut server = BootstrapServerBinder::new(
server.0,
server_keypair.clone(),
BootstrapSrvBindCfg {
max_bytes_read_write: f64::INFINITY,
thread_count: THREAD_COUNT,
max_datastore_key_length: MAX_DATASTORE_KEY_LENGTH,
randomness_size_bytes: BOOTSTRAP_RANDOMNESS_SIZE_BYTES,
consensus_bootstrap_part_size: CONSENSUS_BOOTSTRAP_PART_SIZE,
write_error_timeout: MassaTime::from_millis(1000),
},
);
let mut client = BootstrapClientBinder::test_default(
client,
bootstrap_config.bootstrap_list[0].1.get_public_key(),
);
let server_thread = std::thread::Builder::new()
.name("test_binders::server_thread".to_string())
.spawn({
move || {
server.handshake_timeout(version(), None).unwrap();
let message = server.next_timeout(None).unwrap_err();
match message {
BootstrapError::IoError(message) => {
assert_eq!(message.kind(), std::io::ErrorKind::UnexpectedEof);
assert_eq!(message.to_string(), "failed to fill whole buffer: 0/2");
}
_ => panic!("expected an io_error"),
}
}
})
.unwrap();

let client_thread = std::thread::Builder::new()
.name("test_binders::server_thread".to_string())
.spawn({
move || {
client.handshake(version()).unwrap();

// write the signature.
// This test assumes that the the signature is not checked until the message is read in
// its entirety. The signature here would cause the message exchange to fail on that basis
// if this assumption is broken.
client_clone
.write_all(b"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
.unwrap();
// Give a non-zero message length, but never provide any msg-bytes
client_clone.write_all(&[0, 0, 0, 2]).unwrap();
}
})
.unwrap();

server_thread.join().unwrap();
client_thread.join().unwrap();
}
#[test]
fn test_client_drip_feed() {
let (bootstrap_config, server_keypair): &(BootstrapConfig, KeyPair) = &BOOTSTRAP_CONFIG_KEYPAIR;
Expand Down