Skip to content

Commit

Permalink
Finish generation of faulty BootstrapServerMessageÃ
Browse files Browse the repository at this point in the history
Signed-off-by: Litchi Pi <[email protected]>
  • Loading branch information
litchipi committed Oct 27, 2023
1 parent fb91f78 commit e7bb8ee
Show file tree
Hide file tree
Showing 3 changed files with 376 additions and 7 deletions.
2 changes: 1 addition & 1 deletion massa-bootstrap/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ impl BootstrapClientMessageDeserializer {
) -> Self {
Self {
id_deserializer: U32VarIntDeserializer::new(Included(0), Included(u32::MAX)),
length_error_deserializer: U32VarIntDeserializer::new(Included(0), Included(100000)),
length_error_deserializer: U32VarIntDeserializer::new(Included(0), Included(u32::MAX)),
slot_deserializer: SlotDeserializer::new(
(Included(0), Included(u64::MAX)),
(Included(0), Excluded(thread_count)),
Expand Down
73 changes: 71 additions & 2 deletions massa-bootstrap/src/tests/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
BootstrapServerMessage, BootstrapServerMessageDeserializer, BootstrapServerMessageSerializer,
};
use massa_models::config::*;
use massa_serialization::{Deserializer, Serializer};
use massa_serialization::{DeserializeError, Deserializer, Serializer};

#[test]
fn test_serialize_bootstrap_server_message() {
Expand Down Expand Up @@ -58,7 +58,7 @@ fn test_serialize_bootstrap_server_message() {
);

let deser = BootstrapServerMessageDeserializer::new(config.into());
match deser.deserialize::<massa_serialization::DeserializeError>(&bytes) {
match deser.deserialize::<DeserializeError>(&bytes) {
Ok((rest, msg_res)) => {
assert!(rest.is_empty(), "Data left after deserialization");
assert!(msg_res.equals(&msg), "BootstrapServerMessages doesn't match after serialization / deserialization process")
Expand Down Expand Up @@ -120,3 +120,72 @@ fn test_serialize_bootstrap_client_message() {
},
);
}

#[test]
fn test_serialize_error_cases_clientmsg() {
let mut rng = rand::thread_rng();
let ser = BootstrapClientMessageSerializer::new();
let deser = BootstrapClientMessageDeserializer::new(
THREAD_COUNT,
MAX_DATASTORE_KEY_LENGTH,
MAX_CONSENSUS_BLOCKS_IDS,
);

for n in 0..4 {
let mut bytes = Vec::new();
let msg = BootstrapClientMessage::generate_faulty(&mut rng, n);
assert!(ser.serialize(&msg, &mut bytes).is_ok());
let res = deser.deserialize::<DeserializeError>(&bytes);
assert!(
res.is_err(),
"Expected error, but deserialization succeeded\nData: {msg:?}"
);
println!("Fault {n} caught");
}
}

#[test]
fn test_serialize_error_cases_servermsg() {
let config = BootstrapClientConfig {
rate_limit: std::u64::MAX,
max_listeners_per_peer: MAX_LISTENERS_PER_PEER as u32,
endorsement_count: ENDORSEMENT_COUNT,
max_advertise_length: MAX_ADVERTISE_LENGTH,
max_bootstrap_blocks_length: MAX_BOOTSTRAP_BLOCKS,
max_operations_per_block: MAX_OPERATIONS_PER_BLOCK,
thread_count: THREAD_COUNT,
randomness_size_bytes: BOOTSTRAP_RANDOMNESS_SIZE_BYTES,
max_bootstrap_error_length: MAX_BOOTSTRAP_ERROR_LENGTH,
max_final_state_elements_size: MAX_BOOTSTRAP_FINAL_STATE_PARTS_SIZE,
max_versioning_elements_size: MAX_BOOTSTRAP_VERSIONING_ELEMENTS_SIZE,
max_datastore_entry_count: MAX_DATASTORE_ENTRY_COUNT,
max_datastore_key_length: MAX_DATASTORE_KEY_LENGTH,
max_datastore_value_length: MAX_DATASTORE_VALUE_LENGTH,
max_ledger_changes_count: MAX_LEDGER_CHANGES_COUNT,
max_changes_slot_count: 1000,
max_rolls_length: MAX_ROLLS_COUNT_LENGTH,
max_production_stats_length: MAX_PRODUCTION_STATS_LENGTH,
max_credits_length: MAX_DEFERRED_CREDITS_LENGTH,
max_executed_ops_length: MAX_EXECUTED_OPS_LENGTH,
max_ops_changes_length: MAX_EXECUTED_OPS_CHANGES_LENGTH,
mip_store_stats_block_considered: MIP_STORE_STATS_BLOCK_CONSIDERED,
max_denunciations_per_block_header: MAX_DENUNCIATIONS_PER_BLOCK_HEADER,
max_denunciation_changes_length: MAX_DENUNCIATION_CHANGES_LENGTH,
};

let mut rng = rand::thread_rng();
let ser = BootstrapServerMessageSerializer::new();
let deser = BootstrapServerMessageDeserializer::new((&config).into());

for n in 0..20 {
let mut bytes = Vec::new();
let msg = BootstrapServerMessage::generate_faulty(&mut rng, n);
assert!(ser.serialize(&msg, &mut bytes).is_ok());
let res = deser.deserialize::<DeserializeError>(&bytes);
assert!(
res.is_err(),
"Expected error, but deserialization succeeded"
);
println!("Fault {n} caught");
}
}
Loading

0 comments on commit e7bb8ee

Please sign in to comment.