update UDP utils

This commit is contained in:
luwenpeng
2024-02-21 11:14:21 +08:00
parent c0514964f9
commit c37f9869a6
6 changed files with 66 additions and 29 deletions

View File

@@ -5,8 +5,8 @@
add_executable(gtest_packet gtest_packet.cpp)
target_link_libraries(gtest_packet packet gtest)
add_executable(gtest_udp_helpers gtest_udp_helpers.cpp)
target_link_libraries(gtest_udp_helpers packet gtest)
add_executable(gtest_udp_utils gtest_udp_utils.cpp)
target_link_libraries(gtest_udp_utils packet gtest)
add_executable(gtest_tcp_helpers gtest_tcp_helpers.cpp)
target_link_libraries(gtest_tcp_helpers packet gtest)
@@ -22,7 +22,7 @@ target_link_libraries(gtest_packet_helpers packet gtest)
include(GoogleTest)
gtest_discover_tests(gtest_packet)
gtest_discover_tests(gtest_udp_helpers)
gtest_discover_tests(gtest_udp_utils)
gtest_discover_tests(gtest_tcp_helpers)
gtest_discover_tests(gtest_ipv4_helpers)
gtest_discover_tests(gtest_ipv6_helpers)

View File

@@ -1,6 +1,6 @@
#include <gtest/gtest.h>
#include "udp_helpers.h"
#include "udp_utils.h"
/*
* User Datagram Protocol, Src Port: 4001, Dst Port: 8000
@@ -14,15 +14,28 @@
unsigned char data[] = {0x0f, 0xa1, 0x1f, 0x40, 0x00, 0x9b, 0x1e, 0x1e};
TEST(UDP_HELPERS, TEST)
TEST(UDP_UTILS, GET)
{
const struct udphdr *hdr = (struct udphdr *)data;
EXPECT_TRUE(udp_hdr_get_host_order_sport(hdr) == 4001);
EXPECT_TRUE(udp_hdr_get_host_order_dport(hdr) == 8000);
EXPECT_TRUE(udp_hdr_get_len(hdr) == 155);
EXPECT_TRUE(udp_hdr_get_src_port(hdr) == 4001);
EXPECT_TRUE(udp_hdr_get_dst_port(hdr) == 8000);
EXPECT_TRUE(udp_hdr_get_total_len(hdr) == 155);
EXPECT_TRUE(udp_hdr_get_checksum(hdr) == 0x1e1e);
}
TEST(UDP_UTILS, SET)
{
char buff[8] = {0};
struct udphdr *hdr = (struct udphdr *)buff;
udp_hdr_set_src_port(hdr, 4001);
udp_hdr_set_dst_port(hdr, 8000);
udp_hdr_set_total_len(hdr, 155);
udp_hdr_set_checksum(hdr, 0x1e1e);
EXPECT_TRUE(memcmp(buff, data, 8) == 0);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);