|
@@ -241,7 +241,97 @@ impl FirewallMessage {
|
|
|
mod tests {
|
|
|
use super::*;
|
|
|
|
|
|
- //TODO
|
|
|
+ fn check_ser_de(msg: &FirewallMessage) {
|
|
|
+ // Serialize a message and then deserialize the byte stream
|
|
|
+ // and check if the resulting message is the same.
|
|
|
+ let bytes = msg.msg_serialize().unwrap();
|
|
|
+ let msg_de = FirewallMessage::try_msg_deserialize(&bytes).unwrap();
|
|
|
+ assert_eq!(*msg, msg_de);
|
|
|
+ }
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn test_msg_open_v6() {
|
|
|
+ let msg = FirewallMessage::new_open("::1".parse().unwrap(), 0x9876);
|
|
|
+ assert_eq!(msg.operation(), FirewallOperation::OpenV6);
|
|
|
+ assert_eq!(msg.port(), Some(0x9876));
|
|
|
+ assert_eq!(msg.addr(), Some("::1".parse().unwrap()));
|
|
|
+ check_ser_de(&msg);
|
|
|
+
|
|
|
+ let msg = FirewallMessage::new_open(
|
|
|
+ "0102:0304:0506:0708:090A:0B0C:0D0E:0F10".parse().unwrap(),
|
|
|
+ 0x9876,
|
|
|
+ );
|
|
|
+ let bytes = msg.msg_serialize().unwrap();
|
|
|
+ assert_eq!(
|
|
|
+ bytes,
|
|
|
+ [
|
|
|
+ 0x00, 0x00, // operation
|
|
|
+ 0x98, 0x76, // port
|
|
|
+ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, // addr
|
|
|
+ 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, // addr
|
|
|
+ ]
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn test_msg_open_v4() {
|
|
|
+ let msg = FirewallMessage::new_open("1.2.3.4".parse().unwrap(), 0x9876);
|
|
|
+ assert_eq!(msg.operation(), FirewallOperation::OpenV4);
|
|
|
+ assert_eq!(msg.port(), Some(0x9876));
|
|
|
+ assert_eq!(msg.addr(), Some("1.2.3.4".parse().unwrap()));
|
|
|
+ check_ser_de(&msg);
|
|
|
+
|
|
|
+ let bytes = msg.msg_serialize().unwrap();
|
|
|
+ assert_eq!(
|
|
|
+ bytes,
|
|
|
+ [
|
|
|
+ 0x00, 0x01, // operation
|
|
|
+ 0x98, 0x76, // port
|
|
|
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // addr
|
|
|
+ 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, // addr
|
|
|
+ ]
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn test_msg_ack() {
|
|
|
+ let msg = FirewallMessage::new_ack();
|
|
|
+ assert_eq!(msg.operation(), FirewallOperation::Ack);
|
|
|
+ assert_eq!(msg.port(), None);
|
|
|
+ assert_eq!(msg.addr(), None);
|
|
|
+ check_ser_de(&msg);
|
|
|
+
|
|
|
+ let bytes = msg.msg_serialize().unwrap();
|
|
|
+ assert_eq!(
|
|
|
+ bytes,
|
|
|
+ [
|
|
|
+ 0x00, 0x02, // operation
|
|
|
+ 0x00, 0x00, // port
|
|
|
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // addr
|
|
|
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // addr
|
|
|
+ ]
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn test_msg_nack() {
|
|
|
+ let msg = FirewallMessage::new_nack();
|
|
|
+ assert_eq!(msg.operation(), FirewallOperation::Nack);
|
|
|
+ assert_eq!(msg.port(), None);
|
|
|
+ assert_eq!(msg.addr(), None);
|
|
|
+ check_ser_de(&msg);
|
|
|
+
|
|
|
+ let bytes = msg.msg_serialize().unwrap();
|
|
|
+ assert_eq!(
|
|
|
+ bytes,
|
|
|
+ [
|
|
|
+ 0x00, 0x03, // operation
|
|
|
+ 0x00, 0x00, // port
|
|
|
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // addr
|
|
|
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // addr
|
|
|
+ ]
|
|
|
+ );
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// vim: ts=4 sw=4 expandtab
|