broker_encoding.rs (3379B)
1 use radroots_simplex_smp_proto::prelude::*; 2 3 fn broker(error: RadrootsSimplexSmpBrokerError) -> RadrootsSimplexSmpBrokerMessage { 4 RadrootsSimplexSmpBrokerMessage::Err(RadrootsSimplexSmpError::Proxy( 5 RadrootsSimplexSmpProxyError::Broker(error), 6 )) 7 } 8 9 #[test] 10 fn oversized_response_is_a_typed_error() { 11 for detail in ["p".repeat(256), "🍓".repeat(64)] { 12 let message = broker(RadrootsSimplexSmpBrokerError::Response(detail)); 13 for version in [ 14 RADROOTS_SIMPLEX_SMP_INITIAL_TRANSPORT_VERSION, 15 RADROOTS_SIMPLEX_SMP_CURRENT_TRANSPORT_VERSION, 16 ] { 17 assert_eq!( 18 message.encode_for_version(version), 19 Err(RadrootsSimplexSmpProtoError::InvalidShortFieldLength(256)) 20 ); 21 } 22 } 23 } 24 25 #[test] 26 fn oversized_unexpected_is_a_typed_error() { 27 for detail in ["p".repeat(256), "🍓".repeat(64)] { 28 let message = broker(RadrootsSimplexSmpBrokerError::Unexpected(detail)); 29 assert_eq!( 30 message.encode(), 31 Err(RadrootsSimplexSmpProtoError::InvalidShortFieldLength(256)) 32 ); 33 } 34 } 35 36 #[test] 37 fn valid_byte_boundaries_round_trip_through_messages_and_transmissions() { 38 for detail in [ 39 String::new(), 40 "p".repeat(255), 41 format!("{}abc", "🍓".repeat(63)), 42 ] { 43 for error in [ 44 RadrootsSimplexSmpBrokerError::Response(detail.clone()), 45 RadrootsSimplexSmpBrokerError::Unexpected(detail.clone()), 46 ] { 47 let message = broker(error); 48 for version in [ 49 RADROOTS_SIMPLEX_SMP_INITIAL_TRANSPORT_VERSION, 50 RADROOTS_SIMPLEX_SMP_CURRENT_TRANSPORT_VERSION, 51 ] { 52 let encoded = message.encode_for_version(version).unwrap(); 53 assert_eq!( 54 RadrootsSimplexSmpBrokerMessage::decode_for_version(version, &encoded).unwrap(), 55 message 56 ); 57 } 58 let transmission = RadrootsSimplexSmpBrokerTransmission { 59 authorization: Vec::new(), 60 correlation_id: None, 61 entity_id: Vec::new(), 62 message, 63 }; 64 let encoded = transmission.encode().unwrap(); 65 assert_eq!( 66 RadrootsSimplexSmpBrokerTransmission::decode(&encoded).unwrap(), 67 transmission 68 ); 69 } 70 } 71 } 72 73 #[test] 74 fn nested_oversized_errors_propagate_through_transmissions() { 75 for error in [ 76 RadrootsSimplexSmpBrokerError::Response("p".repeat(256)), 77 RadrootsSimplexSmpBrokerError::Unexpected("🍓".repeat(64)), 78 ] { 79 let mut nested = 80 RadrootsSimplexSmpError::Proxy(RadrootsSimplexSmpProxyError::Broker(error)); 81 for _ in 0..3 { 82 nested = RadrootsSimplexSmpError::Proxy(RadrootsSimplexSmpProxyError::Protocol( 83 Box::new(nested), 84 )); 85 } 86 let transmission = RadrootsSimplexSmpBrokerTransmission { 87 authorization: Vec::new(), 88 correlation_id: None, 89 entity_id: Vec::new(), 90 message: RadrootsSimplexSmpBrokerMessage::Err(nested), 91 }; 92 assert_eq!( 93 transmission.encode(), 94 Err(RadrootsSimplexSmpProtoError::InvalidShortFieldLength(256)) 95 ); 96 } 97 }