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 actual = Message::unmarshal(&mut bytes); 50 if let Err(err) = actual { 51 assert_eq!(expected, err); 52 } else { 53 panic!("expected err, but got ok"); 54 } 55 } 56 57 #[test] 58 fn test_message_marshal_size() { 59 let msg = Message::DataChannelAck(DataChannelAck {}); 60 61 let actual = msg.marshal_size(); 62 let expected = 1; 63 64 assert_eq!(actual, expected); 65 } 66 67 #[test] 68 fn test_message_marshal() { 69 let marshal_size = 12 + 5 + 8; 70 let mut buf = BytesMut::with_capacity(marshal_size); 71 buf.resize(marshal_size, 0u8); 72 73 let msg = Message::DataChannelOpen(DataChannelOpen { 74 channel_type: ChannelType::Reliable, 75 priority: 3893, 76 reliability_parameter: 16715573, 77 label: b"label".to_vec(), 78 protocol: b"protocol".to_vec(), 79 }); 80 81 let actual = msg.marshal_to(&mut buf).unwrap(); 82 let expected = marshal_size; 83 assert_eq!(actual, expected); 84 85 let bytes = buf.freeze(); 86 87 let actual = &bytes[..]; 88 let expected = &[ 89 0x03, // message type 90 0x00, // channel type 91 0x0f, 0x35, // priority 92 0x00, 0xff, 0x0f, 0x35, // reliability parameter 93 0x00, 0x05, // label length 94 0x00, 0x08, // protocol length 95 0x6c, 0x61, 0x62, 0x65, 0x6c, // label 96 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, // protocol 97 ]; 98 99 assert_eq!(actual, expected); 100 } 101