1 use crate::error::Result; 2 3 use super::*; 4 5 use bytes::{Bytes, BytesMut}; 6 7 #[test] 8 fn test_message_unmarshal_open_success() { 9 let mut bytes = Bytes::from_static(&[ 10 0x03, // message type 11 0x00, // channel type 12 0x0f, 0x35, // priority 13 0x00, 0xff, 0x0f, 0x35, // reliability parameter 14 0x00, 0x05, // label length 15 0x00, 0x08, // protocol length 16 0x6c, 0x61, 0x62, 0x65, 0x6c, // label 17 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, // protocol 18 ]); 19 20 let actual = Message::unmarshal(&mut bytes).unwrap(); 21 22 let expected = Message::DataChannelOpen(DataChannelOpen { 23 channel_type: ChannelType::Reliable, 24 priority: 3893, 25 reliability_parameter: 16715573, 26 label: b"label".to_vec(), 27 protocol: b"protocol".to_vec(), 28 }); 29 30 assert_eq!(actual, expected); 31 } 32 33 #[test] 34 fn test_message_unmarshal_ack_success() -> Result<()> { 35 let mut bytes = Bytes::from_static(&[0x02]); 36 37 let actual = Message::unmarshal(&mut bytes)?; 38 let expected = Message::DataChannelAck(DataChannelAck {}); 39 40 assert_eq!(actual, expected); 41 42 Ok(()) 43 } 44 45 #[test] 46 fn test_message_unmarshal_invalid_message_type() { 47 let mut bytes = Bytes::from_static(&[0x01]); 48 let expected = Error::InvalidMessageType(0x01); 49 let result = Message::unmarshal(&mut bytes); 50 let actual = result.expect_err("expected err, but got ok"); 51 assert_eq!(actual, expected); 52 } 53 54 #[test] 55 fn test_message_marshal_size() { 56 let msg = Message::DataChannelAck(DataChannelAck {}); 57 58 let actual = msg.marshal_size(); 59 let expected = 1; 60 61 assert_eq!(actual, expected); 62 } 63 64 #[test] 65 fn test_message_marshal() { 66 let marshal_size = 12 + 5 + 8; 67 let mut buf = BytesMut::with_capacity(marshal_size); 68 buf.resize(marshal_size, 0u8); 69 70 let msg = Message::DataChannelOpen(DataChannelOpen { 71 channel_type: ChannelType::Reliable, 72 priority: 3893, 73 reliability_parameter: 16715573, 74 label: b"label".to_vec(), 75 protocol: b"protocol".to_vec(), 76 }); 77 78 let actual = msg.marshal_to(&mut buf).unwrap(); 79 let expected = marshal_size; 80 assert_eq!(actual, expected); 81 82 let bytes = buf.freeze(); 83 84 let actual = &bytes[..]; 85 let expected = &[ 86 0x03, // message type 87 0x00, // channel type 88 0x0f, 0x35, // priority 89 0x00, 0xff, 0x0f, 0x35, // reliability parameter 90 0x00, 0x05, // label length 91 0x00, 0x08, // protocol length 92 0x6c, 0x61, 0x62, 0x65, 0x6c, // label 93 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, // protocol 94 ]; 95 96 assert_eq!(actual, expected); 97 } 98