1

Add some basic packet translation tests

This commit is contained in:
Evan Pratten 2023-07-18 19:03:25 -04:00
parent 10d7bf520f
commit c6db02c80a
2 changed files with 140 additions and 0 deletions

View File

@ -44,3 +44,80 @@ pub fn translate_tcp6_to_tcp4(
input.payload, input.payload,
)?) )?)
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_translate_tcp4_to_tcp6() {
let input = TcpPacket::new(
"192.0.2.1:1234".parse().unwrap(),
"192.0.2.2:5678".parse().unwrap(),
123456,
654321,
0,
4096,
0,
Vec::new(),
RawBytes("Hello, world!".as_bytes().to_vec()),
)
.unwrap();
let result = translate_tcp4_to_tcp6(
input,
"2001:db8::1".parse().unwrap(),
"2001:db8::2".parse().unwrap(),
)
.unwrap();
assert_eq!(result.source(), "[2001:db8::1]:1234".parse().unwrap());
assert_eq!(result.destination(), "[2001:db8::2]:5678".parse().unwrap());
assert_eq!(result.sequence, 123456);
assert_eq!(result.ack_number, 654321);
assert_eq!(result.flags, 0);
assert_eq!(result.window_size, 4096);
assert_eq!(result.urgent_pointer, 0);
assert_eq!(result.options.len(), 0);
assert_eq!(
result.payload,
RawBytes("Hello, world!".as_bytes().to_vec())
);
}
#[test]
fn test_translate_tcp6_to_tcp4() {
let input = TcpPacket::new(
"[2001:db8::1]:1234".parse().unwrap(),
"[2001:db8::2]:5678".parse().unwrap(),
123456,
654321,
0,
4096,
0,
Vec::new(),
RawBytes("Hello, world!".as_bytes().to_vec()),
)
.unwrap();
let result = translate_tcp6_to_tcp4(
input,
"192.0.2.1".parse().unwrap(),
"192.0.2.2".parse().unwrap(),
)
.unwrap();
assert_eq!(result.source(), "192.0.2.1:1234".parse().unwrap());
assert_eq!(result.destination(), "192.0.2.2:5678".parse().unwrap());
assert_eq!(result.sequence, 123456);
assert_eq!(result.ack_number, 654321);
assert_eq!(result.flags, 0);
assert_eq!(result.window_size, 4096);
assert_eq!(result.urgent_pointer, 0);
assert_eq!(result.options.len(), 0);
assert_eq!(
result.payload,
RawBytes("Hello, world!".as_bytes().to_vec())
);
}
}

View File

@ -32,3 +32,66 @@ pub fn translate_udp6_to_udp4(
input.payload, input.payload,
)?) )?)
} }
#[cfg(test)]
mod tests {
use super::*;
use crate::packet::protocols::udp::UdpPacket;
#[test]
fn test_translate_udp4_to_udp6() {
// Create an IPv4 UDP packet
let ipv4_packet = UdpPacket::new(
"192.0.2.1:1234".parse().unwrap(),
"192.0.2.2:5678".parse().unwrap(),
RawBytes("Hello, world!".as_bytes().to_vec()),
)
.unwrap();
// Translate the packet to IPv6
let ipv6_packet = translate_udp4_to_udp6(
ipv4_packet,
"2001:db8::1".parse().unwrap(),
"2001:db8::2".parse().unwrap(),
)
.unwrap();
// Ensure the translation is correct
assert_eq!(ipv6_packet.source(), "[2001:db8::1]:1234".parse().unwrap());
assert_eq!(
ipv6_packet.destination(),
"[2001:db8::2]:5678".parse().unwrap()
);
assert_eq!(
ipv6_packet.payload,
RawBytes("Hello, world!".as_bytes().to_vec())
);
}
#[test]
fn test_translate_udp6_to_udp4() {
// Create an IPv6 UDP packet
let ipv6_packet = UdpPacket::new(
"[2001:db8::1]:1234".parse().unwrap(),
"[2001:db8::2]:5678".parse().unwrap(),
RawBytes("Hello, world!".as_bytes().to_vec()),
)
.unwrap();
// Translate the packet to IPv4
let ipv4_packet = translate_udp6_to_udp4(
ipv6_packet,
"192.0.2.1".parse().unwrap(),
"192.0.2.2".parse().unwrap(),
)
.unwrap();
// Ensure the translation is correct
assert_eq!(ipv4_packet.source(), "192.0.2.1:1234".parse().unwrap());
assert_eq!(ipv4_packet.destination(), "192.0.2.2:5678".parse().unwrap());
assert_eq!(
ipv4_packet.payload,
RawBytes("Hello, world!".as_bytes().to_vec())
);
}
}