增加tfe_stream类型转字符串的工具函数及对应单元测试用例
This commit is contained in:
@@ -1,3 +1,11 @@
|
||||
add_library(common src/tfe_stat.cpp src/tfe_utils.cpp src/tfe_future.cpp src/tfe_http.cpp src/tfe_plugin.cpp src/tfe_rpc.cpp)
|
||||
target_include_directories(common PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)
|
||||
target_link_libraries(common MESA_handle_logger libevent-static libevent-static-openssl libevent-static-pthreads)
|
||||
|
||||
### UNITTEST CASE
|
||||
add_executable(test-addr test/test_addr.cpp)
|
||||
target_include_directories(test-addr PRIVATE include)
|
||||
target_link_libraries(test-addr gtest)
|
||||
|
||||
include(GoogleTest)
|
||||
gtest_discover_tests(test-addr)
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <features.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <netinet/in.h> //defines struct in_addr
|
||||
#include <arpa/inet.h>
|
||||
|
||||
/* network-order */
|
||||
struct tfe_stream_addr_tuple4_v4
|
||||
@@ -138,3 +141,35 @@ struct tfe_stream_addr
|
||||
unsigned char paddr[0];
|
||||
};
|
||||
};
|
||||
|
||||
static inline char * tfe_stream_addr_to_str(const struct tfe_stream_addr * addr)
|
||||
{
|
||||
char * __str_ret = NULL;
|
||||
if (addr->addrtype == TFE_ADDR_STREAM_TUPLE4_V4)
|
||||
{
|
||||
const struct tfe_stream_addr_tuple4_v4 * tuple4_v4 = addr->tuple4_v4;
|
||||
char __src_addr[INET_ADDRSTRLEN];
|
||||
char __dst_addr[INET_ADDRSTRLEN];
|
||||
uint16_t __src_port = ntohs((uint16_t) tuple4_v4->source);
|
||||
uint16_t __dst_port = ntohs((uint16_t) tuple4_v4->dest);
|
||||
|
||||
inet_ntop(AF_INET, &tuple4_v4->saddr, __src_addr, sizeof(__src_addr));
|
||||
inet_ntop(AF_INET, &tuple4_v4->daddr, __dst_addr, sizeof(__dst_addr));
|
||||
asprintf(&__str_ret, "%s %s %u %u", __src_addr, __dst_addr, __src_port, __dst_port);
|
||||
}
|
||||
|
||||
if(addr->addrtype == TFE_ADDR_STREAM_TUPLE4_V6)
|
||||
{
|
||||
const struct tfe_stream_addr_tuple4_v6 * tuple4_v6 = addr->tuple4_v6;
|
||||
char __src_addr[INET6_ADDRSTRLEN];
|
||||
char __dst_addr[INET6_ADDRSTRLEN];
|
||||
uint16_t __src_port = ntohs((uint16_t) tuple4_v6->source);
|
||||
uint16_t __dst_port = ntohs((uint16_t) tuple4_v6->dest);
|
||||
|
||||
inet_ntop(AF_INET6, &tuple4_v6->saddr, __src_addr, sizeof(__src_addr));
|
||||
inet_ntop(AF_INET6, &tuple4_v6->daddr, __dst_addr, sizeof(__dst_addr));
|
||||
asprintf(&__str_ret, "%s %s %u %u", __src_addr, __dst_addr, __src_port, __dst_port);
|
||||
}
|
||||
|
||||
return __str_ret;
|
||||
}
|
||||
|
||||
33
common/test/test_addr.cpp
Normal file
33
common/test/test_addr.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <tfe_types.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
TEST(TfeStreamAddrToStr, StreamTuple4)
|
||||
{
|
||||
auto * __stream_addr = (struct tfe_stream_addr *) malloc(sizeof(struct tfe_stream_addr) +
|
||||
sizeof(struct tfe_stream_addr_tuple4_v4));
|
||||
|
||||
struct tfe_stream_addr_ipv4 * st_addr_v4 = __stream_addr->ipv4;
|
||||
__stream_addr->addrtype = TFE_ADDR_STREAM_TUPLE4_V4;
|
||||
__stream_addr->addrlen = sizeof(struct tfe_stream_addr_tuple4_v4);
|
||||
|
||||
/* 192.168.100.50 192.168.100.100 10080 80 */
|
||||
st_addr_v4->saddr.s_addr = 0x3264a8c0;
|
||||
st_addr_v4->source = 24615;
|
||||
st_addr_v4->daddr.s_addr = 0x6464a8c0;
|
||||
st_addr_v4->dest = 20480;
|
||||
|
||||
const char * __sample = "192.168.100.50 192.168.100.100 10080 80";
|
||||
char * __result = tfe_stream_addr_to_str(__stream_addr);
|
||||
|
||||
EXPECT_STREQ(__sample, __result);
|
||||
free(__result);
|
||||
free(__stream_addr);
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
Reference in New Issue
Block a user