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 1 commit
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 @@ -43,7 +43,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
73 changes: 72 additions & 1 deletion massa-bootstrap/src/tests/binders.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::messages::{BootstrapClientMessage, BootstrapServerMessage};
use crate::settings::{BootstrapClientConfig, BootstrapSrvBindCfg};
use crate::BootstrapConfig;
use crate::{
bindings::{BootstrapClientBinder, BootstrapServerBinder},
tests::tools::get_bootstrap_config,
BootstrapPeers,
};
use crate::{BootstrapConfig, BootstrapError};
use massa_models::config::{
BOOTSTRAP_RANDOMNESS_SIZE_BYTES, CONSENSUS_BOOTSTRAP_PART_SIZE, ENDORSEMENT_COUNT,
MAX_ADVERTISE_LENGTH, MAX_ASYNC_MESSAGE_DATA, MAX_ASYNC_POOL_LENGTH,
Expand All @@ -23,6 +23,7 @@ use massa_protocol_exports::{PeerId, TransportType};
use massa_signature::{KeyPair, PublicKey};
use massa_time::MassaTime;
use std::collections::HashMap;
use std::io::Write;
use std::net::TcpStream;
use std::str::FromStr;

Expand Down Expand Up @@ -484,3 +485,73 @@ fn test_binders_try_double_send_client_works() {
server_thread.join().unwrap();
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 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 || {
let version: Version = Version::from_str("TEST.1.10").unwrap();
Ben-PH marked this conversation as resolved.
Show resolved Hide resolved

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!("Bad message receive: Expected a peers list message"),
Ben-PH marked this conversation as resolved.
Show resolved Hide resolved
}
}
})
.unwrap();

let client_thread = std::thread::Builder::new()
.name("test_binders::server_thread".to_string())
.spawn({
move || {
let version: Version = Version::from_str("TEST.1.10").unwrap();
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();
}