diff --git a/pallets/node-authorization/src/mock.rs b/pallets/node-authorization/src/mock.rs index d8800d8a9..af334c2b7 100644 --- a/pallets/node-authorization/src/mock.rs +++ b/pallets/node-authorization/src/mock.rs @@ -23,6 +23,7 @@ use super::*; use crate as pallet_node_authorization; use crate::{Config, NodeId}; +use frame_support::ensure; use frame_support::{ construct_runtime, derive_impl, ord_parameter_types, @@ -131,3 +132,8 @@ pub fn new_test_ext() -> sp_io::TestExternalities { .unwrap(); t.into() } + +pub fn test_peer_id_length(testing: PeerId) -> Result<(), Error> { + ensure!(testing.0.len() <= 128 as usize, Error::::PeerIdTooLong); + Ok(()) +} diff --git a/pallets/node-authorization/src/tests.rs b/pallets/node-authorization/src/tests.rs index 24af33aef..b37ab84fe 100644 --- a/pallets/node-authorization/src/tests.rs +++ b/pallets/node-authorization/src/tests.rs @@ -459,3 +459,15 @@ fn test_generate_peer_id_invalid_utf8() { let invalid_node_id: NodeId = vec![0xFF, 0xFE, 0xFD]; assert_err!(NodeAuthorization::generate_peer_id(&invalid_node_id), Error::::InvalidUtf8); } + +#[test] +fn peer_id_too_long_test() { + new_test_ext().execute_with(|| { + let node_id: &str = &"nodeid".repeat(32); + + let node_identity = test_node(&node_id); + let testing = NodeAuthorization::generate_peer_id(&node_identity).unwrap(); + + assert_eq!(test_peer_id_length(testing), Err(Error::::PeerIdTooLong)); + }) +}