add bool_plugin & fqdn_plugin unit-test

This commit is contained in:
liuwentan
2023-02-24 17:29:38 +08:00
parent 6f4b88d690
commit d4e1670987
29 changed files with 654 additions and 159 deletions

View File

@@ -18,6 +18,9 @@ target_link_libraries(maat_framework_gtest maat_frame_static gtest_static)
add_executable(adapter_hs_gtest adapter_hs_gtest.cpp)
target_link_libraries(adapter_hs_gtest maat_frame_static gtest_static)
add_executable(maat_ex_data_gtest maat_ex_data_gtest.cpp)
target_link_libraries(maat_ex_data_gtest maat_frame_static gtest_static)
file(COPY table_info.conf DESTINATION ./)
file(COPY literal_expr.conf DESTINATION ./)
file(COPY regex_expr.conf DESTINATION ./)

137
test/maat_ex_data_gtest.cpp Normal file
View File

@@ -0,0 +1,137 @@
#include "maat.h"
#include "log/log.h"
#include "maat_utils.h"
#include "json2iris.h"
#include "maat_command.h"
#include "maat_ex_data.h"
#include <gtest/gtest.h>
#include <limits.h>
const char *table_info_path = "./table_info.conf";
const char *json_filename = "maat_json.json";
struct log_handle *g_logger = NULL;
struct maat *g_maat_instance = NULL;
struct user_info {
char name[256];
char ip_addr[32];
int id;
int ref_cnt;
};
void ex_data_new_cb(int table_id, const char *key, const char *table_line,
void **ad, long argl, void *argp)
{
int *counter = (int *)argp;
struct user_info *u = ALLOC(struct user_info, 1);
int valid = 0, tag = 0;
int ret = sscanf(table_line, "%d\t%s\t%s%d\t%d", &(u->id), u->ip_addr, u->name, &valid, &tag);
EXPECT_EQ(ret, 5);
u->ref_cnt = 1;
*ad = u;
(*counter)++;
}
void ex_data_free_cb(int table_id, void **ad, long argl, void *argp)
{
struct user_info *u = (struct user_info *)(*ad);
if ((__sync_sub_and_fetch(&u->ref_cnt, 1) == 0)) {
free(u);
*ad = NULL;
}
}
void ex_data_dup_cb(int table_id, void **to, void **from, long argl, void *argp)
{
struct user_info *u = (struct user_info *)(*from);
__sync_add_and_fetch(&(u->ref_cnt), 1);
*to = u;
}
TEST(EXDataRuntime, Update) {
const char *table_name = "TEST_PLUGIN_EXDATA_TABLE";
int table_id = maat_table_get_id(g_maat_instance, table_name);
ASSERT_GT(table_id, 0);
int ex_data_counter = 0;
struct ex_data_runtime *ex_data_rt = ex_data_runtime_new(table_id, ex_data_container_free, g_logger);
struct ex_data_schema *ex_schema = ex_data_schema_new(ex_data_new_cb, ex_data_free_cb, ex_data_dup_cb,
0, &ex_data_counter);
ex_data_runtime_set_schema(ex_data_rt, ex_schema);
const char *row1 = "1\t192.168.0.1\tmahuateng\t1\t0";
const char *key1 = "192.168.0.1";
size_t key1_len = strlen(key1);
void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, row1, key1, key1_len);
EXPECT_EQ(ex_data_counter, 1);
struct ex_data_container *ex_container = ex_data_container_new(ex_data, NULL);
int ret = ex_data_runtime_add_ex_container(ex_data_rt, key1, key1_len, ex_container);
EXPECT_EQ(ret, 0);
const char *row2 = "2\t192.168.0.2\tliyanhong\t1\t0";
const char *key2 = "192.168.0.2";
size_t key2_len = strlen(key2);
ex_data = ex_data_runtime_row2ex_data(ex_data_rt, row2, key2, key2_len);
ex_container = ex_data_container_new(ex_data, NULL);
ret = ex_data_runtime_add_ex_container(ex_data_rt, key2, key2_len, ex_container);
EXPECT_EQ(ret, 0);
ex_data_runtime_commit(ex_data_rt);
void *res_data1 = ex_data_runtime_get_ex_data_by_key(ex_data_rt, "192.168.0.1", 11);
EXPECT_TRUE(res_data1 != NULL);
struct user_info *info = (struct user_info *)res_data1;
EXPECT_EQ(0, strcmp(info->name, "mahuateng"));
EXPECT_EQ(info->id, 1);
void *res_data2 = ex_data_runtime_get_ex_data_by_key(ex_data_rt, "192.168.0.2", 11);
EXPECT_TRUE(res_data2 != NULL);
info = (struct user_info *)res_data2;
EXPECT_EQ(0, strcmp(info->name, "liyanhong"));
EXPECT_EQ(info->id, 2);
}
int main(int argc, char ** argv)
{
int ret=0;
::testing::InitGoogleTest(&argc, argv);
g_logger = log_handle_create("./maat_ex_data_gtest.log", 0);
char json_iris_path[NAME_MAX] = {0};
char tmp_iris_path[PATH_MAX] = {0};
snprintf(json_iris_path, sizeof(json_iris_path), "./%s_iris_tmp", json_filename);
if ((access(json_iris_path, F_OK)) == 0) {
system_cmd_rmdir(json_iris_path);
}
if (access(json_iris_path, F_OK) < 0) {
char *json_buff = NULL;
size_t json_buff_sz = 0;
int ret = load_file_to_memory(json_filename, (unsigned char**)&json_buff, &json_buff_sz);
EXPECT_NE(ret, -1);
ret = json2iris(json_buff, json_filename, NULL, tmp_iris_path, sizeof(tmp_iris_path),
NULL, NULL, g_logger);
EXPECT_NE(ret, -1);
}
struct maat_options *opts = maat_options_new();
char json_path[PATH_MAX] = {0};
snprintf(json_path, sizeof(json_path), "./%s", json_filename);
maat_options_set_json_file(opts, json_path);
g_maat_instance = maat_new(opts, table_info_path);
EXPECT_TRUE(g_maat_instance != NULL);
ret=RUN_ALL_TESTS();
log_handle_destroy(g_logger);
return ret;
}

View File

@@ -260,7 +260,6 @@ TEST_F(MaatStringScan, ExprAndExprPlus) {
EXPECT_EQ(ret, MAAT_SCAN_HIT);
EXPECT_EQ(results[0], 195);
maat_state_free(&state);
}
//TODO:
@@ -866,6 +865,199 @@ TEST_F(IPPluginTable, EX_DATA) {
EXPECT_EQ(ret, 0);
}
class FQDNPluginTable : public testing::Test
{
protected:
static void SetUpTestCase() {
}
static void TearDownTestCase() {
}
};
#define FQDN_PLUGIN_EX_DATA
struct fqdn_plugin_ud
{
int rule_id;
int catid;
int ref_cnt;
};
void fqdn_plugin_ex_new_cb(int table_id, const char *key, const char *table_line, void **ad, long argl, void *argp)
{
int *counter = (int *)argp;
size_t column_offset = 0, column_len = 0;
struct fqdn_plugin_ud *ud = ALLOC(struct fqdn_plugin_ud, 1);
int ret = get_column_pos(table_line, 1, &column_offset, &column_len);
EXPECT_EQ(ret, 0);
ud->rule_id = atoi(table_line + column_offset);
ret = get_column_pos(table_line, 4, &column_offset, &column_len);
EXPECT_EQ(ret, 0);
sscanf(table_line + column_offset, "catid=%d", &ud->catid);
ud->ref_cnt = 1;
*ad = ud;
(*counter)++;
}
void fqdn_plugin_ex_free_cb(int table_id, void **ad, long argl, void *argp)
{
struct fqdn_plugin_ud *u = (struct fqdn_plugin_ud *)(*ad);
if ((__sync_sub_and_fetch(&u->ref_cnt, 1) == 0)) {
free(u);
*ad = NULL;
}
}
void fqdn_plugin_ex_dup_cb(int table_id, void **to, void **from, long argl, void *argp)
{
struct fqdn_plugin_ud *u = (struct fqdn_plugin_ud *)(*from);
__sync_add_and_fetch(&(u->ref_cnt), 1);
*to = u;
}
TEST_F(FQDNPluginTable, EX_DATA) {
const char *table_name = "TEST_FQDN_PLUGIN_WITH_EXDATA";
int table_id = maat_table_get_id(g_maat_instance, table_name);
ASSERT_GT(table_id, 0);
int fqdn_plugin_ex_data_counter = 0;
int ret = maat_plugin_table_ex_schema_register(g_maat_instance, table_id,
fqdn_plugin_ex_new_cb,
fqdn_plugin_ex_free_cb,
fqdn_plugin_ex_dup_cb,
0, &fqdn_plugin_ex_data_counter);
ASSERT_TRUE(ret>=0);
EXPECT_EQ(fqdn_plugin_ex_data_counter, 5);
int i = 0;
struct fqdn_plugin_ud *result[4];
ret = maat_fqdn_plugin_table_get_ex_data(g_maat_instance, table_id, "www.example1.com", (void**)result, 4);
ASSERT_EQ(ret, 2);
EXPECT_EQ(result[0]->rule_id, 201);
EXPECT_EQ(result[1]->rule_id, 202);
for (i = 0; i < ret; i++) {
fqdn_plugin_ex_free_cb(0, (void**)&(result[i]), 0, NULL);
}
ret = maat_fqdn_plugin_table_get_ex_data(g_maat_instance, table_id, "www.example3.com", (void**)result, 4);
EXPECT_EQ(ret, 0);
ret = maat_fqdn_plugin_table_get_ex_data(g_maat_instance, table_id, "r3---sn-i3belne6.example2.com", (void**)result, 4);
ASSERT_EQ(ret, 2);
EXPECT_TRUE(result[0]->rule_id == 205 || result[0]->rule_id == 204);
for (i = 0; i < ret; i++) {
fqdn_plugin_ex_free_cb(0, (void**)&(result[i]), 0, NULL);
}
}
struct bool_plugin_ud {
int id;
char *name;
int ref_cnt;
};
void bool_plugin_ex_new_cb(int table_id, const char *key, const char *table_line, void **ad, long argl, void *argp)
{
int *counter=(int *)argp;
size_t column_offset=0, column_len=0;
struct bool_plugin_ud *ud = ALLOC(struct bool_plugin_ud, 1);
int ret = get_column_pos(table_line, 1, &column_offset, &column_len);
EXPECT_EQ(ret, 0);
ud->id = atoi(table_line + column_offset);
ret = get_column_pos(table_line, 3, &column_offset, &column_len);
EXPECT_EQ(ret, 0);
ud->name = (char *)malloc(column_len+1);
memcpy(ud->name, table_line+column_offset, column_len);
ud->ref_cnt = 1;
*ad = ud;
(*counter)++;
}
void bool_plugin_ex_free_cb(int table_id, void **ad, long argl, void *argp)
{
struct bool_plugin_ud *u = (struct bool_plugin_ud *)(*ad);
if ((__sync_sub_and_fetch(&u->ref_cnt, 1) == 0))
{
free(u->name);
free(u);
*ad = NULL;
}
}
void bool_plugin_ex_dup_cb(int table_id, void **to, void **from, long argl, void *argp)
{
struct bool_plugin_ud *u = (struct bool_plugin_ud *)(*from);
__sync_add_and_fetch(&(u->ref_cnt), 1);
*to = u;
}
class BoolPluginTable : public testing::Test
{
protected:
static void SetUpTestCase() {
}
static void TearDownTestCase() {
}
};
TEST_F(BoolPluginTable, EX_DATA) {
int ex_data_counter = 0, i = 0;
const char *table_name = "TEST_BOOL_PLUGIN_WITH_EXDATA";
int table_id = maat_table_get_id(g_maat_instance, table_name);
ASSERT_GT(table_id, 0);
int ret = maat_plugin_table_ex_schema_register(g_maat_instance, table_id,
bool_plugin_ex_new_cb,
bool_plugin_ex_free_cb,
bool_plugin_ex_dup_cb,
0, &ex_data_counter);
ASSERT_TRUE(ret >= 0);
EXPECT_EQ(ex_data_counter, 6);
struct bool_plugin_ud *result[6];
unsigned long long items_1[] = {999};
ret = maat_bool_plugin_table_get_ex_data(g_maat_instance, table_id, items_1, 1, (void**)result, 6);
EXPECT_EQ(ret, 0);
for (i = 0; i < ret; i++) {
bool_plugin_ex_free_cb(0, (void**)&(result[i]), 0, NULL);
}
unsigned long long items_2[] = {1, 2, 1000};
ret = maat_bool_plugin_table_get_ex_data(g_maat_instance, table_id, items_2, 3, (void**)result, 6);
EXPECT_EQ(ret, 1);
EXPECT_EQ(result[0]->id, 301);
for (i = 0; i < ret; i++) {
bool_plugin_ex_free_cb(0, (void**)&(result[i]), 0, NULL);
}
unsigned long long items_3[]={101, 102, 1000};
ret = maat_bool_plugin_table_get_ex_data(g_maat_instance, table_id, items_3, 3, (void**)result, 6);
EXPECT_EQ(ret, 4);
for (i = 0; i < ret; i++) {
bool_plugin_ex_free_cb(0, (void**)&(result[i]), 0, NULL);
}
unsigned long long items_4[]={7, 0, 1, 2, 3, 4, 5, 6, 7, 7, 7};
ret = maat_bool_plugin_table_get_ex_data(g_maat_instance, table_id, items_4, sizeof(items_4)/sizeof(unsigned long long), (void**)result, 6);
EXPECT_EQ(ret, 1);
EXPECT_EQ(result[0]->id, 305);
for (i = 0; i < ret; i++) {
bool_plugin_ex_free_cb(0, (void**)&(result[i]), 0, NULL);
}
}
class VirtualTable : public testing::Test
{
protected:
@@ -1084,6 +1276,7 @@ void plugin_ex_dup_cb(int table_id, void **to, void **from, long argl, void *arg
__sync_add_and_fetch(&(u->ref_cnt), 1);
*to = u;
}
#if 0
TEST_F(MaatCmdTest, PluginEXData) {
const char *table_name = "TEST_PLUGIN_EXDATA_TABLE";
@@ -1135,8 +1328,8 @@ TEST_F(MaatCmdTest, PluginEXData) {
ret = maat_cmd_set_line(g_maat_instance, &line_rule);
EXPECT_GT(ret, 0);
sleep(1);
sleep(1);
int ex_data_counter = 0;
ret = maat_plugin_table_ex_schema_register(g_maat_instance, table_id,
plugin_ex_new_cb,
@@ -1147,7 +1340,7 @@ TEST_F(MaatCmdTest, PluginEXData) {
EXPECT_EQ(ex_data_counter, TEST_CMD_LINE_NUM);
struct user_info *uinfo = NULL;
uinfo = (struct user_info *)maat_plugin_table_get_ex_data(g_maat_instance, table_id, "192.168.0.2");
uinfo = (struct user_info *)maat_plugin_table_get_ex_data(g_maat_instance, table_id, "192.168.0.4");
ASSERT_TRUE(uinfo != NULL);
EXPECT_EQ(0, strcmp(uinfo->name, "liuqiangdong"));
EXPECT_EQ(uinfo->id, 2);
@@ -1161,6 +1354,7 @@ TEST_F(MaatCmdTest, PluginEXData) {
ASSERT_TRUE(uinfo == NULL);
}
#endif
int count_line_num_cb(const char *table_name, const char *line, void *u_para)
{
(*((unsigned int *)u_para))++;

View File

@@ -90,6 +90,22 @@ TEST(rcu_hash_add_multi_node, single_thread) {
size_t key1_len = strlen(key1);
rcu_hash_add(htable, key1, key1_len, (void *)data1);
struct user_data *data2 = ALLOC(struct user_data, 1);
data2->id = 103;
char name2[64] = "mahuateng";
memcpy(data2->name, name2, strlen(name2));
char key2[64] = "192.168.0.1";
size_t key2_len = strlen(key2);
rcu_hash_add(htable, key2, key2_len, (void *)data2);
struct user_data *data3 = ALLOC(struct user_data, 1);
data3->id = 104;
char name3[64] = "liyanhong";
memcpy(data3->name, name3, strlen(name3));
char key3[64] = "192.168.0.2";
size_t key3_len = strlen(key3);
rcu_hash_add(htable, key3, key3_len, (void *)data3);
/* find in hash before commit */
void *res = rcu_hash_find(htable, key0, key0_len);
EXPECT_TRUE(res == NULL);
@@ -104,27 +120,37 @@ TEST(rcu_hash_add_multi_node, single_thread) {
void **data_array = NULL;
ret = rcu_hash_list_updating_data(htable, &data_array);
EXPECT_EQ(ret, 2);
EXPECT_EQ(ret, 4);
rcu_hash_commit(htable);
/* find in hash after commit */
res = rcu_hash_find(htable, key0, key0_len);
EXPECT_TRUE(res != NULL);
struct user_data *res_data0 = (struct user_data *)res;
EXPECT_EQ(res_data0->id, 101);
EXPECT_STREQ(res_data0->name, "www.baidu.com");
res = rcu_hash_find(htable, key1, key1_len);
EXPECT_TRUE(res != NULL);
struct user_data *res_data1 = (struct user_data *)res;
EXPECT_EQ(res_data1->id, 102);
EXPECT_STREQ(res_data1->name, "127.0.0.1");
res = rcu_hash_find(htable, key2, key2_len);
EXPECT_TRUE(res != NULL);
struct user_data *res_data2 = (struct user_data *)res;
EXPECT_EQ(res_data2->id, 103);
EXPECT_STREQ(res_data2->name, "mahuateng");
res = rcu_hash_find(htable, key3, key3_len);
EXPECT_TRUE(res != NULL);
struct user_data *res_data3 = (struct user_data *)res;
EXPECT_EQ(res_data3->id, 104);
EXPECT_STREQ(res_data3->name, "liyanhong");
ret = rcu_hash_count(htable);
EXPECT_EQ(ret, 2);
EXPECT_EQ(ret, 4);
ret = rcu_hash_garbage_queue_len(htable);
EXPECT_EQ(ret, 0);