TSG-14177: 命中Security Policy后根据策略ID发送相应的Metric

This commit is contained in:
刘学利
2023-04-03 09:43:17 +00:00
parent b696e82879
commit 39cdb03b56
9 changed files with 495 additions and 199 deletions

View File

@@ -40,7 +40,10 @@ set(TSG_MASTER_SRC ${PROJECT_SOURCE_DIR}/src/tsg_entry.cpp
)
add_executable(gtest_master ${TSG_MASTER_SRC} gtest_kafka.cpp gtest_common.cpp gtest_master.cpp)
target_link_libraries(gtest_master gtest-static ctemplate-static cjson MESA_prof_load MESA_handle_logger MESA_jump_layer MESA_field_stat2 maat4 MESA_htable)
target_link_libraries(gtest_master gtest-static ctemplate-static cjson MESA_prof_load MESA_handle_logger MESA_jump_layer MESA_field_stat2 maat4 MESA_htable fieldstat3)
add_executable(gtest_sync_session_state ${PROJECT_SOURCE_DIR}/src/tsg_sync_state.cpp gtest_common.cpp gtest_session_state.cpp)
target_link_libraries(gtest_sync_session_state gtest-static cjson ctemplate-static)
add_executable(gtest_fieldstat3 ${PROJECT_SOURCE_DIR}/src/tsg_statistic.cpp gtest_common.cpp gtest_fieldstat3.cpp)
target_link_libraries(gtest_fieldstat3 gtest-static ctemplate-static MESA_field_stat2 fieldstat3 MESA_prof_load MESA_handle_logger)

View File

@@ -27,7 +27,7 @@ const char *printaddr (const struct layer_addr *paddrinfo, int threadindex)
int get_thread_count(void)
{
return 1;
return 8;
}
int MESA_rst_tcp(struct streaminfo * stream, struct rst_tcp_para * paras, int para_len)
@@ -100,6 +100,16 @@ int MESA_set_stream_opt(const struct streaminfo * pstream, enum MESA_stream_opt
int MESA_get_stream_opt(const struct streaminfo * pstream, enum MESA_stream_opt opt, void * opt_val, int * opt_val_len)
{
if (*opt_val_len == 2)
{
*(u_short *)opt_val = 2;
}
if (*opt_val_len == 8)
{
*(long long *)opt_val = 5;
}
return 0;
}

View File

@@ -0,0 +1,169 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <MESA/fieldstat.h>
#include "tsg_entry.h"
#include "gtest_common.h"
#include "tsg_variable.h"
#include <MESA/MESA_prof_load.h>
#include <MESA/MESA_handle_logger.h>
#include <gtest/gtest.h>
const char *tsg_gtest_conffile = "tsgconf/main.conf";
extern struct tsg_statistic g_tsg_statis_para;
pthread_t g_pid[8];
TEST(FIELDSATA3, InterceptIllegalParameter)
{
struct _traffic_info _info;
struct maat_rule p_result;
int ret = tsg_set_intercept_flow(NULL, &_info, 0);
EXPECT_EQ(ret, -1);
ret = tsg_set_intercept_flow(&p_result, NULL, 0);
EXPECT_EQ(ret, -1);
ret = tsg_set_intercept_flow(&p_result, &_info, -1);
EXPECT_EQ(ret, -1);
}
TEST(FIELDSATA3, PolicyIllegalParameter)
{
struct maat_rule p_result;
struct streaminfo a_stream;
int ret = tsg_set_policy_flow(NULL, &p_result, 0);
EXPECT_EQ(ret, -1);
ret = tsg_set_policy_flow(&a_stream, NULL, 0);
EXPECT_EQ(ret, -1);
ret = tsg_set_policy_flow(&a_stream, &p_result, -1);
EXPECT_EQ(ret, -1);
}
TEST(FIELDSATA3, Intercept)
{
struct _traffic_info _info;
struct maat_rule p_result;
_info.con_num = 10;
_info.in_bytes = 1000;
_info.in_packets = 25;
_info.out_bytes = 1001;
_info.out_packets = 24;
p_result.action = TSG_ACTION_INTERCEPT;
p_result.rule_id = 95536;
p_result.do_log = 1;
p_result.service_id = 10;
for (int i = 0; i < 50000; i++)
{
// p_result.rule_id += i;
// _info.in_bytes += i;
int ret = tsg_set_intercept_flow(&p_result, &_info, 0);
EXPECT_EQ(ret, 0);
// usleep(500);
}
}
TEST(FIELDSATA3, PolicyFlow)
{
struct maat_rule p_result;
struct streaminfo a_stream;
p_result.action = TSG_ACTION_MONITOR;
p_result.rule_id = 95500;
for (int i = 0; i < 50000; i++)
{
// p_result.rule_id += i;
// _info.in_bytes += i;
int ret = tsg_set_policy_flow(&a_stream, &p_result, 0);
EXPECT_EQ(ret, 0);
// usleep(500);
}
}
#if 0
void *run_time_funtion(void *arg)
{
uint8_t pid = *(uint8_t *)arg;
struct maat_rule p_result;
struct streaminfo a_stream;
struct _traffic_info _info;
while (1)
{
srand(time(NULL));
p_result.action = TSG_ACTION_MONITOR;
p_result.rule_id = rand() % 95500;
usleep(rand() % 500);
int ret = tsg_set_policy_flow(&a_stream, &p_result, pid);
EXPECT_EQ(ret, 0);
srand(time(NULL));
usleep(rand() % 500);
p_result.action = TSG_ACTION_DENY;
p_result.rule_id = rand() % 95500;
ret = tsg_set_policy_flow(&a_stream, &p_result, pid);
EXPECT_EQ(ret, 0);
srand(time(NULL));
usleep(rand() % 500);
p_result.action = TSG_ACTION_BYPASS;
p_result.rule_id = rand() % 95500;
ret = tsg_set_policy_flow(&a_stream, &p_result, pid);
EXPECT_EQ(ret, 0);
_info.con_num = rand() % 10;
_info.in_bytes = rand() % 1000;
_info.in_packets = rand() % 25;
_info.out_bytes = rand() % 1001;
_info.out_packets = rand() % 24;
p_result.action = TSG_ACTION_INTERCEPT;
p_result.rule_id = rand() % 95536;
ret = tsg_set_intercept_flow(&p_result, &_info, pid);
EXPECT_EQ(ret, 0);
}
return NULL;
}
TEST(FIELDSATA3, MultiThreading)
{
for (uint8_t i = 0; i < 8; i++)
{
uint8_t i_pid = i;
pthread_create(g_pid + i, NULL, run_time_funtion, (void *)&i_pid);
EXPECT_NE(g_pid[i], 0);
}
}
#endif
int main(int argc, char *argv[])
{
void *logger = MESA_create_runtime_log_handle("log/gtest_fieldstat3.log", RLOG_LV_FATAL);
tsg_statistic_init(tsg_gtest_conffile, logger);
testing::InitGoogleTest(&argc, argv);
int ret = RUN_ALL_TESTS();
// sleep(30);
// for (int i = 0; i < 8; i++)
// {
// pthread_cancel(g_pid[i]);
// }
tsg_statistic_destroy();
return ret;
}