TSG-13347 Steering Service开发流表
This commit is contained in:
23
common/test/CMakeLists.txt
Normal file
23
common/test/CMakeLists.txt
Normal file
@@ -0,0 +1,23 @@
|
||||
###############################################################################
|
||||
# gtest_stream_addr
|
||||
###############################################################################
|
||||
|
||||
add_executable(gtest_stream_addr gtest_stream_addr.cpp)
|
||||
target_include_directories(gtest_stream_addr PUBLIC ${CMAKE_SOURCE_DIR}/common/include)
|
||||
target_link_libraries(gtest_stream_addr common gtest)
|
||||
|
||||
###############################################################################
|
||||
# gtest_stream_table
|
||||
###############################################################################
|
||||
|
||||
add_executable(gtest_stream_table gtest_stream_table.cpp)
|
||||
target_include_directories(gtest_stream_table PUBLIC ${CMAKE_SOURCE_DIR}/common/include)
|
||||
target_link_libraries(gtest_stream_table common gtest)
|
||||
|
||||
###############################################################################
|
||||
# gtest_discover_tests
|
||||
###############################################################################
|
||||
|
||||
include(GoogleTest)
|
||||
gtest_discover_tests(gtest_stream_addr)
|
||||
gtest_discover_tests(gtest_stream_table)
|
||||
40
common/test/gtest_stream_addr.cpp
Normal file
40
common/test/gtest_stream_addr.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include "stream_addr.h"
|
||||
|
||||
TEST(STREAM_ADDR, IPV4)
|
||||
{
|
||||
struct stream_addr addr;
|
||||
addr.addr_type = STREAM_ADDR_TYPE_V4;
|
||||
addr.addr_v4.src_port = htons(12345);
|
||||
addr.addr_v4.dst_port = htons(23456);
|
||||
inet_pton(AF_INET, "1.2.3.4", &addr.addr_v4.src_addr);
|
||||
inet_pton(AF_INET, "4.3.2.1", &addr.addr_v4.dst_addr);
|
||||
|
||||
char *ret_str = stream_addr_to_str(&addr);
|
||||
EXPECT_TRUE(ret_str != nullptr);
|
||||
EXPECT_STREQ(ret_str, "1.2.3.4 12345 4.3.2.1 23456");
|
||||
free(ret_str);
|
||||
}
|
||||
|
||||
TEST(STREAM_ADDR, IPV6)
|
||||
{
|
||||
struct stream_addr addr;
|
||||
addr.addr_type = STREAM_ADDR_TYPE_V6;
|
||||
addr.addr_v6.src_port = htons(12345);
|
||||
addr.addr_v6.dst_port = htons(23456);
|
||||
inet_pton(AF_INET6, "1:2::3", &addr.addr_v6.src_addr);
|
||||
inet_pton(AF_INET6, "a:b::c", &addr.addr_v6.dst_addr);
|
||||
|
||||
char *ret_str = stream_addr_to_str(&addr);
|
||||
EXPECT_TRUE(ret_str != nullptr);
|
||||
EXPECT_STREQ(ret_str, "1:2::3 12345 a:b::c 23456");
|
||||
free(ret_str);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
84
common/test/gtest_stream_table.cpp
Normal file
84
common/test/gtest_stream_table.cpp
Normal file
@@ -0,0 +1,84 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include "stream_table.h"
|
||||
|
||||
#define INIT_ADDR_V4(name, src_addr_str, src_port_num, dst_addr_str, dst_port_num) \
|
||||
struct stream_addr name; \
|
||||
memset(&name, 0, sizeof(name)); \
|
||||
(name).addr_type = STREAM_ADDR_TYPE_V4; \
|
||||
(name).addr_v4.src_port = htons((src_port_num)); \
|
||||
(name).addr_v4.dst_port = htons((dst_port_num)); \
|
||||
inet_pton(AF_INET, (src_addr_str), &(name).addr_v4.src_addr); \
|
||||
inet_pton(AF_INET, (dst_addr_str), &(name).addr_v4.dst_addr);
|
||||
|
||||
#define INIT_ADDR_V6(name, src_addr_str, src_port_num, dst_addr_str, dst_port_num) \
|
||||
struct stream_addr name; \
|
||||
memset(&name, 0, sizeof(name)); \
|
||||
(name).addr_type = STREAM_ADDR_TYPE_V6; \
|
||||
(name).addr_v6.src_port = htons((src_port_num)); \
|
||||
(name).addr_v6.dst_port = htons((dst_port_num)); \
|
||||
inet_pton(AF_INET6, (src_addr_str), &(name).addr_v6.src_addr); \
|
||||
inet_pton(AF_INET6, (dst_addr_str), &(name).addr_v6.dst_addr);
|
||||
|
||||
TEST(STREAM_TABLE, TEST)
|
||||
{
|
||||
INIT_ADDR_V4(addr1, "1.2.3.4", 1234, "4.3.2.1", 4321);
|
||||
INIT_ADDR_V6(addr2, "2:3:4::5", 2345, "5:4:3::2", 5342);
|
||||
struct stream_addr addr3;
|
||||
char *val_hello = strdup("HELLO");
|
||||
char *val_world = strdup("WORLD");
|
||||
|
||||
struct stream_node *node = NULL;
|
||||
// TEST Create
|
||||
struct stream_table *table = stream_table_create();
|
||||
EXPECT_TRUE(table != nullptr);
|
||||
|
||||
// TEST Insert
|
||||
EXPECT_TRUE(stream_table_insert(table, 1, &addr1, val_hello, free) == 0);
|
||||
EXPECT_TRUE(stream_table_insert(table, 1, &addr1, val_hello, free) == -1);
|
||||
|
||||
EXPECT_TRUE(stream_table_insert(table, 2, &addr2, val_world, free) == 0);
|
||||
EXPECT_TRUE(stream_table_insert(table, 2, &addr2, val_world, free) == -1);
|
||||
|
||||
// TEST Search
|
||||
node = stream_table_search_by_streamid(table, 1);
|
||||
EXPECT_TRUE(node != nullptr);
|
||||
EXPECT_STREQ((const char *)node->val_data, "HELLO");
|
||||
node = stream_table_search_by_streamid(table, 2);
|
||||
EXPECT_TRUE(node != nullptr);
|
||||
EXPECT_STREQ((const char *)node->val_data, "WORLD");
|
||||
node = stream_table_search_by_streamid(table, 3);
|
||||
EXPECT_TRUE(node == nullptr);
|
||||
|
||||
node = stream_table_search_by_streamaddr(table, &addr1);
|
||||
EXPECT_TRUE(node != nullptr);
|
||||
EXPECT_STREQ((const char *)node->val_data, "HELLO");
|
||||
node = stream_table_search_by_streamaddr(table, &addr2);
|
||||
EXPECT_TRUE(node != nullptr);
|
||||
EXPECT_STREQ((const char *)node->val_data, "WORLD");
|
||||
node = stream_table_search_by_streamaddr(table, &addr3);
|
||||
EXPECT_TRUE(node == nullptr);
|
||||
|
||||
// TEST Delete
|
||||
stream_table_delete_by_streamid(table, 1);
|
||||
node = stream_table_search_by_streamid(table, 1);
|
||||
EXPECT_TRUE(node == nullptr);
|
||||
node = stream_table_search_by_streamaddr(table, &addr1);
|
||||
EXPECT_TRUE(node == nullptr);
|
||||
|
||||
stream_table_delete_by_streamaddr(table, &addr2);
|
||||
node = stream_table_search_by_streamid(table, 2);
|
||||
EXPECT_TRUE(node == nullptr);
|
||||
node = stream_table_search_by_streamaddr(table, &addr1);
|
||||
EXPECT_TRUE(node == nullptr);
|
||||
|
||||
// TEST Destory
|
||||
stream_table_destory(table);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
Reference in New Issue
Block a user