[PATCH]delete useless code

This commit is contained in:
liuwentan
2024-04-03 16:47:30 +08:00
parent cbabcbd6b0
commit 7e25f48fdd
21 changed files with 3147 additions and 5266 deletions

View File

@@ -13,7 +13,7 @@ include_directories(${PROJECT_SOURCE_DIR}/scanner/bool_matcher)
add_executable(rcu_hash_gtest rcu_hash_gtest.cpp)
target_link_libraries(rcu_hash_gtest maat_frame_static gtest_static)
add_executable(maat_input_mode_gtest maat_input_mode_gtest.cpp)
add_executable(maat_input_mode_gtest maat_input_mode_gtest.cpp test_utils.cpp)
target_link_libraries(maat_input_mode_gtest maat_frame_static gtest_static)
add_executable(maat_framework_gtest maat_framework_gtest.cpp test_utils.cpp)

View File

@@ -1,7 +1,4 @@
include_directories(${PROJECT_SOURCE_DIR}/src/inc_internal)
add_executable(benchmark_hs_gtest benchmark_hs_gtest.cpp)
target_link_libraries(benchmark_hs_gtest maat_frame_static gtest_static)
add_executable(benchmark_rs_gtest benchmark_rs_gtest.cpp)
target_link_libraries(benchmark_rs_gtest maat_frame_static gtest_static)
add_executable(benchmark_gtest benchmark_gtest.cpp ../test_utils.cpp)
target_link_libraries(benchmark_gtest maat_frame_static gtest_static)

File diff suppressed because it is too large Load Diff

View File

@@ -41,7 +41,7 @@ enum expr_case_sensitive case_sensitive_str_to_enum(const char *str)
return case_sensitive;
}
int is_hexbin_str_to_int(const char *str)
static int is_hexbin_str_to_int(const char *str)
{
int ret = 0;
@@ -94,7 +94,8 @@ enum expr_pattern_type pattern_type_str_to_enum(const char *str)
return pattern_type;
}
int parse_config_file(const char *filename, struct expr_rule exprs[], size_t *n_expr)
static int
parse_config_file(const char *filename, struct expr_rule exprs[], size_t *n_expr)
{
unsigned char *json_buff = NULL;
size_t json_buff_size = 0;
@@ -147,17 +148,20 @@ int parse_config_file(const char *filename, struct expr_rule exprs[], size_t *n_
cJSON *item = cJSON_GetObjectItem(pat_item, "pattern_type");
if (item != NULL && item->type == cJSON_String) {
exprs[i].patterns[j].type = pattern_type_str_to_enum(item->valuestring);
exprs[i].patterns[j].type =
pattern_type_str_to_enum(item->valuestring);
}
item = cJSON_GetObjectItem(pat_item, "match_method");
if (item != NULL && item->type == cJSON_String) {
exprs[i].patterns[j].match_mode = match_method_to_match_mode(item->valuestring);
exprs[i].patterns[j].match_mode =
match_method_to_match_mode(item->valuestring);
}
item = cJSON_GetObjectItem(pat_item, "case_sensitive");
if (item != NULL && item->type == cJSON_String) {
exprs[i].patterns[j].case_sensitive = case_sensitive_str_to_enum(item->valuestring);
exprs[i].patterns[j].case_sensitive =
case_sensitive_str_to_enum(item->valuestring);
}
int is_hexbin = 0;
@@ -191,9 +195,11 @@ int parse_config_file(const char *filename, struct expr_rule exprs[], size_t *n_
if (item != NULL && item->type == cJSON_String) {
int key_left_offset = -1;
int key_right_offset = -1;
sscanf(item->valuestring, "%d~%d", &key_left_offset, &key_right_offset);
sscanf(item->valuestring, "%d~%d", &key_left_offset,
&key_right_offset);
if (key_left_offset < -1 || key_right_offset < -1) {
printf("Error: offset should not less than -1, left_offset:%d, right_offset:%d\n",
printf("Error: offset should not less than -1, "
"left_offset:%d, right_offset:%d\n",
key_left_offset, key_right_offset);
}
exprs[i].patterns[j].start_offset = key_left_offset;
@@ -219,7 +225,7 @@ next:
return ret;
}
void expr_array_free(struct expr_rule rules[], size_t n_rule)
static void expr_array_free(struct expr_rule rules[], size_t n_rule)
{
for (size_t i = 0; i < n_rule; i++) {
for (size_t j = 0; j < rules[i].n_patterns; j++) {
@@ -236,7 +242,8 @@ TEST(hs_expr_matcher_init, invalid_input_parameter)
struct expr_rule rules[64];
size_t n_rule = 0;
struct expr_matcher *matcher = expr_matcher_new(NULL, 0, EXPR_ENGINE_TYPE_HS, 1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(NULL, 0, EXPR_ENGINE_TYPE_HS, 1, g_logger);
EXPECT_TRUE(matcher == NULL);
matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS, 1, g_logger);
@@ -261,7 +268,8 @@ TEST(rs_expr_matcher_init, invalid_input_parameter)
struct expr_rule rules[64];
size_t n_rule = 0;
struct expr_matcher *matcher = expr_matcher_new(NULL, 0, EXPR_ENGINE_TYPE_RS, 1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(NULL, 0, EXPR_ENGINE_TYPE_RS, 1, g_logger);
EXPECT_TRUE(matcher == NULL);
matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS, 1, g_logger);
@@ -289,7 +297,8 @@ TEST(hs_expr_matcher_match, literal_sub_has_normal_offset)
int ret = parse_config_file("./expr_matcher.conf", rules, &n_rule);
EXPECT_EQ(ret, 0);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS, 1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
@@ -345,7 +354,8 @@ TEST(rs_expr_matcher_match, literal_sub_has_normal_offset)
int ret = parse_config_file("./expr_matcher.conf", rules, &n_rule);
EXPECT_EQ(ret, 0);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS, 1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
@@ -401,8 +411,8 @@ TEST(hs_expr_matcher_match, literal_sub_has_left_unlimit_offset)
int ret = parse_config_file("./expr_matcher.conf", rules, &n_rule);
EXPECT_EQ(ret, 0);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS,
1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
@@ -459,8 +469,8 @@ TEST(rs_expr_matcher_match, literal_sub_has_left_unlimit_offset)
int ret = parse_config_file("./expr_matcher.conf", rules, &n_rule);
EXPECT_EQ(ret, 0);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS,
1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
@@ -517,8 +527,8 @@ TEST(hs_expr_matcher_match, literal_sub_has_right_unlimit_offset)
int ret = parse_config_file("./expr_matcher.conf", rules, &n_rule);
EXPECT_EQ(ret, 0);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS,
1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
@@ -584,8 +594,8 @@ TEST(rs_expr_matcher_match, literal_sub_has_right_unlimit_offset)
int ret = parse_config_file("./expr_matcher.conf", rules, &n_rule);
EXPECT_EQ(ret, 0);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS,
1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
@@ -651,8 +661,8 @@ TEST(hs_expr_matcher_match, literal_sub_with_no_offset)
int ret = parse_config_file("./expr_matcher.conf", rules, &n_rule);
EXPECT_EQ(ret, 0);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS,
1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
@@ -709,8 +719,8 @@ TEST(rs_expr_matcher_match, literal_sub_with_no_offset)
int ret = parse_config_file("./expr_matcher.conf", rules, &n_rule);
EXPECT_EQ(ret, 0);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS,
1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
@@ -767,8 +777,8 @@ TEST(hs_expr_matcher_match, literal_exactly)
int ret = parse_config_file("./expr_matcher.conf", rules, &n_rule);
EXPECT_EQ(ret, 0);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS,
1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
@@ -815,8 +825,8 @@ TEST(rs_expr_matcher_match, literal_exactly)
int ret = parse_config_file("./expr_matcher.conf", rules, &n_rule);
EXPECT_EQ(ret, 0);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS,
1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
@@ -864,8 +874,8 @@ TEST(hs_expr_matcher_match, literal_prefix)
int ret = parse_config_file("./expr_matcher.conf", rules, &n_rule);
EXPECT_EQ(ret, 0);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS,
1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
@@ -924,8 +934,8 @@ TEST(rs_expr_matcher_match, literal_prefix)
int ret = parse_config_file("./expr_matcher.conf", rules, &n_rule);
EXPECT_EQ(ret, 0);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS,
1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
@@ -984,8 +994,8 @@ TEST(hs_expr_matcher_match, literal_suffix)
int ret = parse_config_file("./expr_matcher.conf", rules, &n_rule);
EXPECT_EQ(ret, 0);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS,
1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
@@ -1044,8 +1054,8 @@ TEST(rs_expr_matcher_match, literal_suffix)
int ret = parse_config_file("./expr_matcher.conf", rules, &n_rule);
EXPECT_EQ(ret, 0);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS,
1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
@@ -1104,8 +1114,8 @@ TEST(hs_expr_matcher_match, literal_sub_with_hex)
int ret = parse_config_file("./expr_matcher.conf", rules, &n_rule);
EXPECT_EQ(ret, 0);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS,
1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
@@ -1143,8 +1153,8 @@ TEST(rs_expr_matcher_match, literal_sub_with_hex)
int ret = parse_config_file("./expr_matcher.conf", rules, &n_rule);
EXPECT_EQ(ret, 0);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS,
1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
@@ -1182,8 +1192,8 @@ TEST(hs_expr_matcher_match, literal_with_chinese)
int ret = parse_config_file("./expr_matcher.conf", rules, &n_rule);
EXPECT_EQ(ret, 0);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS,
1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
@@ -1211,8 +1221,8 @@ TEST(rs_expr_matcher_match, literal_with_chinese)
int ret = parse_config_file("./expr_matcher.conf", rules, &n_rule);
EXPECT_EQ(ret, 0);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS,
1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
@@ -1240,8 +1250,8 @@ TEST(hs_expr_matcher_match, same_pattern_different_offset)
int ret = parse_config_file("./expr_matcher.conf", rules, &n_rule);
EXPECT_EQ(ret, 0);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS,
1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
@@ -1269,8 +1279,8 @@ TEST(rs_expr_matcher_match, same_pattern_different_offset)
int ret = parse_config_file("./expr_matcher.conf", rules, &n_rule);
EXPECT_EQ(ret, 0);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS,
1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
@@ -1298,8 +1308,8 @@ TEST(hs_expr_matcher_match, long_scan_data)
int ret = parse_config_file("./expr_matcher.conf", rules, &n_rule);
EXPECT_EQ(ret, 0);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS,
1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
@@ -1329,8 +1339,8 @@ TEST(rs_expr_matcher_match, long_scan_data)
int ret = parse_config_file("./expr_matcher.conf", rules, &n_rule);
EXPECT_EQ(ret, 0);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS,
1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
@@ -1377,20 +1387,22 @@ TEST(hs_expr_matcher_stream, basic)
int ret = parse_config_file("./expr_matcher.conf", rules, &n_rule);
EXPECT_EQ(ret, 0);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS,
1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
const char* scan_data1 = "A directed path in a directed graph is a finite";
const char *scan_data2 = " or infinite sequence of edges which joins a sequence of distinct vertices";
const char *scan_data2 = " or infinite sequence of edges which joins a "
"sequence of distinct vertices";
struct expr_scan_result result[64] = {0};
size_t n_hit_result = 0;
size_t n_hit_pattern = 0;
int thread_id = 0;
struct expr_matcher_stream *stream = expr_matcher_stream_open(matcher, thread_id);
struct expr_matcher_stream *stream =
expr_matcher_stream_open(matcher, thread_id);
EXPECT_TRUE(stream != NULL);
ret = expr_matcher_stream_match(stream, scan_data1, strlen(scan_data1), result,
@@ -1419,13 +1431,14 @@ TEST(rs_expr_matcher_stream, basic)
int ret = parse_config_file("./expr_matcher.conf", rules, &n_rule);
EXPECT_EQ(ret, 0);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS,
1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
const char* scan_data1 = "A directed path in a directed graph is a finite";
const char *scan_data2 = " or infinite sequence of edges which joins a sequence of distinct vertices";
const char *scan_data2 = " or infinite sequence of edges which joins a "
"sequence of distinct vertices";
struct expr_scan_result result[64] = {0};
size_t n_hit_result = 0;
@@ -1464,11 +1477,13 @@ TEST(hs_expr_matcher, regex_basic)
ret = expr_matcher_verify_regex_expression("[0-9]rain", g_logger);
EXPECT_EQ(ret, 1);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS, 1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
const char *scan_data1 = "http://www.cyberessays.com/search_results.php?action=search&query=username,abckkk,1234567";
const char *scan_data1 = "http://www.cyberessays.com/search_results.php?"
"action=search&query=username,abckkk,1234567";
//const char *scan_data2 = "8rain";
struct expr_scan_result result[64] = {0};
size_t n_result = 0;
@@ -1495,12 +1510,13 @@ TEST(rs_expr_matcher, regex_basic)
ret = expr_matcher_verify_regex_expression("[0-9]rain", g_logger);
EXPECT_EQ(ret, 1);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS,
1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
const char *scan_data1 = "http://www.cyberessays.com/search_results.php?action=search&query=username,abckkk,1234567";
const char *scan_data1 = "http://www.cyberessays.com/search_results.php?"
"action=search&query=username,abckkk,1234567";
struct expr_scan_result result[64] = {0};
size_t n_result = 0;
@@ -1525,7 +1541,8 @@ TEST(hs_expr_matcher, regex_unicode)
int ret = parse_config_file("./expr_matcher.conf", rules, &n_rule);
EXPECT_EQ(ret, 0);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS, 1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
@@ -1552,7 +1569,8 @@ TEST(rs_expr_matcher, regex_unicode)
int ret = parse_config_file("./expr_matcher.conf", rules, &n_rule);
EXPECT_EQ(ret, 0);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS, 1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_RS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);
@@ -1579,8 +1597,8 @@ TEST(hs_expr_matcher, hit_pattern_num)
int ret = parse_config_file("./expr_matcher.conf", rules, &n_rule);
EXPECT_EQ(ret, 0);
struct expr_matcher *matcher = expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS,
1, g_logger);
struct expr_matcher *matcher =
expr_matcher_new(rules, n_rule, EXPR_ENGINE_TYPE_HS, 1, g_logger);
EXPECT_TRUE(matcher != NULL);
expr_array_free(rules, n_rule);

View File

@@ -33,7 +33,8 @@ struct group2group_rule {
UT_icd ut_ex_group_id_icd = {sizeof(long long), NULL, NULL, NULL};
static int parse_config_file(const char *filename, struct group2group_rule *rules)
static int
parse_config_file(const char *filename, struct group2group_rule *rules)
{
unsigned char *json_buff = NULL;
size_t json_buff_size = 0;

View File

@@ -1,4 +1,4 @@
include_directories(${PROJECT_SOURCE_DIR}/src/inc_internal)
add_executable(ipport_plugin_gtest ipport_plugin_gtest.cpp)
add_executable(ipport_plugin_gtest ipport_plugin_gtest.cpp ../test_utils.cpp)
target_link_libraries(ipport_plugin_gtest maat_frame_static gtest_static)

View File

@@ -4,6 +4,7 @@
#include "maat_rule.h"
#include "maat_utils.h"
#include "log/log.h"
#include "../test_utils.h"
#include "maat_config_monitor.h"
#include "maat_redis_monitor.h"
@@ -33,133 +34,6 @@ protected:
struct log_handle *IPPortPluginTable::logger;
int count_line_num_cb(const char *table_name, const char *line, void *u_para)
{
(*((unsigned int *)u_para))++;
return 0;
}
int line_idx = 0;
long long absolute_expire_time=0;
int make_serial_rule(const char *table_name, const char *line, void *u_para)
{
struct serial_rule *s_rule=(struct serial_rule *)u_para;
redisContext *ctx = s_rule->ref_ctx;
char *buff = ALLOC(char, strlen(line) + 1);
memcpy(buff, line, strlen(line) + 1);
while(buff[strlen(line) - 1] == '\n' || buff[strlen(line) - 1] == '\t') {
buff[strlen(line) - 1] = '\0';
}
const char *redis_rule_key = "TEST_RULE_KEY";
redisReply *reply =
maat_wrap_redis_command(ctx, NULL, "INCRBY %s %d", redis_rule_key, 1);
if (reply->type == REDIS_REPLY_NIL) {
printf("incrby redis_rule_key:%s failed.", redis_rule_key);
return -1;
} else {
s_rule->rule_id = maat_read_redis_integer(reply);
freeReplyObject(reply);
reply = NULL;
}
maat_set_serial_rule(s_rule + line_idx, MAAT_OP_ADD, s_rule->rule_id,
table_name, buff, absolute_expire_time);
(s_rule + line_idx)->ref_ctx = ctx;
line_idx++;
FREE(buff);
return 0;
}
int write_config_to_redis(char *redis_ip, int redis_port, int redis_db,
struct log_handle *logger)
{
redisContext *c =
maat_connect_redis(redis_ip, redis_port, redis_db, logger);
if (NULL == c) {
return -1;
}
redisReply *reply = maat_wrap_redis_command(c, logger, "flushdb");
if (NULL == reply) {
return -1;
} else {
freeReplyObject(reply);
reply = NULL;
}
reply = maat_wrap_redis_command(c, logger, "SET MAAT_VERSION 1");
if (NULL == reply) {
return -1;
} else {
freeReplyObject(reply);
reply = NULL;
}
reply = maat_wrap_redis_command(c, logger, "SET MAAT_PRE_VER 1");
if (NULL == reply) {
return -1;
} else {
freeReplyObject(reply);
reply = NULL;
}
reply = maat_wrap_redis_command(c, logger, "SET SEQUENCE_REGION 1");
if (NULL == reply) {
return -1;
} else {
freeReplyObject(reply);
reply = NULL;
}
reply = maat_wrap_redis_command(c, logger, "SET SEQUENCE_GROUP 1");
if (NULL == reply) {
return -1;
} else {
freeReplyObject(reply);
reply = NULL;
}
size_t total_line_cnt = 0;
char iris_idx_path[PATH_MAX] = {0};
snprintf(iris_idx_path, sizeof(iris_idx_path), "test_data/index");
config_monitor_traverse(0, iris_idx_path, NULL, count_line_num_cb, NULL,
&total_line_cnt, NULL, logger);
struct serial_rule *s_rule = ALLOC(struct serial_rule, total_line_cnt);
s_rule->ref_ctx = c;
long long server_time = maat_redis_server_time_s(c);
if (server_time < 0) {
return -1;
}
absolute_expire_time = server_time + 300;
config_monitor_traverse(0, iris_idx_path, NULL, make_serial_rule, NULL,
s_rule, NULL, logger);
s_rule->ref_ctx = NULL;
line_idx = 0;
absolute_expire_time = 0;
int success_cnt = 0;
do {
success_cnt = maat_cmd_write_rule(c, s_rule, total_line_cnt,
server_time, logger);
} while (success_cnt < 0);
EXPECT_EQ(success_cnt, (int)total_line_cnt);
for (size_t i = 0; i < total_line_cnt; i++) {
maat_clear_rule_cache(s_rule + i);
}
FREE(s_rule);
redisFree(c);
return 0;
}
struct ipport_plugin_ud {
long long rule_id;
char *buffer;
@@ -365,9 +239,13 @@ TEST_F(IPPortPluginTable, WITHOUT_SAME_IP) {
char redis_ip[32] = "127.0.0.1";
int redis_port = 6379;
int redis_db = 0;
char iris_idx_path[PATH_MAX] = {0};
struct log_handle *logger = IPPortPluginTable::logger;
int ret = write_config_to_redis(redis_ip, redis_port, redis_db, logger);
snprintf(iris_idx_path, sizeof(iris_idx_path), "test_data/index");
int ret = write_iris_to_redis(iris_idx_path, redis_ip, redis_port,
redis_db, logger);
assert(ret == 0);
struct maat_options *opts = maat_options_new();
@@ -434,9 +312,13 @@ TEST_F(IPPortPluginTable, WITH_256SAME_IP) {
char redis_ip[32] = "127.0.0.1";
int redis_port = 6379;
int redis_db = 0;
char iris_idx_path[PATH_MAX] = {0};
struct log_handle *logger = IPPortPluginTable::logger;
int ret = write_config_to_redis(redis_ip, redis_port, redis_db, logger);
snprintf(iris_idx_path, sizeof(iris_idx_path), "test_data/index");
int ret = write_iris_to_redis(iris_idx_path, redis_ip, redis_port,
redis_db, logger);
assert(ret == 0);
struct maat_options *opts = maat_options_new();

View File

@@ -20,8 +20,9 @@ struct user_info {
int id;
};
void ex_data_new_cb(const char *table_name, int table_id, const char *key, const char *table_line,
void **ad, long argl, void *argp)
static void
ex_data_new_cb(const char *table_name, 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);
@@ -33,7 +34,7 @@ void ex_data_new_cb(const char *table_name, int table_id, const char *key, const
(*counter)++;
}
void ex_data_free_cb(int table_id, void **ad, long argl, void *argp)
static void ex_data_free_cb(int table_id, void **ad, long argl, void *argp)
{
struct user_info *u = (struct user_info *)(*ad);
@@ -42,7 +43,8 @@ void ex_data_free_cb(int table_id, void **ad, long argl, void *argp)
*ad = NULL;
}
void ex_data_dup_cb(int table_id, void **to, void **from, long argl, void *argp)
static void
ex_data_dup_cb(int table_id, void **to, void **from, long argl, void *argp)
{
struct user_info *u = (struct user_info *)(*from);
*to = u;
@@ -70,7 +72,8 @@ TEST(EXDataRuntime, Update) {
const char *row1 = "101\t192.168.0.1\tmahuateng";
const char *key1 = "192.168.0.1";
size_t key1_len = strlen(key1);
void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, table_name, row1, key1, key1_len);
void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, table_name, row1,
key1, key1_len);
EXPECT_EQ(ex_data_counter, 1);
struct ex_container *ex_container = ex_container_new(ex_data, NULL);
@@ -130,27 +133,7 @@ int main(int argc, char ** argv)
::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", g_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(g_json_filename, (unsigned char**)&json_buff, &json_buff_sz);
EXPECT_NE(ret, -1);
ret = json2iris(json_buff, g_json_filename, NULL, tmp_iris_path, sizeof(tmp_iris_path),
NULL, NULL, g_logger);
FREE(json_buff);
EXPECT_NE(ret, -1);
}
struct maat_options *opts = maat_options_new();
char json_path[PATH_MAX] = {0};
snprintf(json_path, sizeof(json_path), "./%s", g_json_filename);

View File

@@ -58,19 +58,6 @@ int del_command(struct maat *maat_inst, int compile_id)
compile_id, "null", 1, 0);
}
static void random_keyword_generate(char *keyword_buf, size_t sz)
{
#define MIN_KEYWORD_LEN 4
size_t i = 0;
size_t len = random() % (sz - 1 - MIN_KEYWORD_LEN) + MIN_KEYWORD_LEN;
for (i = 0; i < len; i++) {
keyword_buf[i] = 'a' + random() % ('z' - 'a');
}
keyword_buf[i] = '\0';
}
const char *watched_json = "./json_update/maat.json";
const char *old_json = "./json_update/old.json";
const char *new_json = "./json_update/new.json";

View File

@@ -30,19 +30,6 @@ struct thread_param {
struct log_handle *logger;
};
void random_keyword_generate(char *keyword_buf, size_t sz)
{
#define MIN_KEYWORD_LEN 4
size_t i = 0;
size_t len = random() % (sz - 1 - MIN_KEYWORD_LEN) + MIN_KEYWORD_LEN;
for (i = 0; i < len; i++) {
keyword_buf[i] = 'a' + random() % ('z' - 'a');
}
keyword_buf[i] = '\0';
}
static void
test_add_expr_command(struct maat *maat_inst, const char *table_name,
const char *keywords)
@@ -1644,78 +1631,6 @@ TEST_F(MaatPerfFileScan, IPPlugin) {
}
}
class MaatTSGFqdnScan : public testing::Test
{
protected:
static void SetUpTestCase() {
char redis_ip[64] = "127.0.0.1";
int redis_port = 6379;
int redis_db = 0;
const char *tsg_table_info = "./tsg_table_info.conf";
logger = log_handle_create("./maat_framework_perf_gtest.log", 0);
int ret = write_iris_to_redis("./tsgrule", redis_ip, redis_port,
redis_db, logger);
if (ret < 0) {
log_fatal(logger, MODULE_FRAMEWORK_PERF_GTEST,
"[%s:%d] write config to redis failed.",
__FUNCTION__, __LINE__);
}
struct maat_options *opts = maat_options_new();
maat_options_set_stat_file(opts, "./stat.log");
maat_options_set_perf_on(opts);
maat_options_set_redis(opts, redis_ip, redis_port, redis_db);
maat_options_set_logger(opts, "./maat_framework_perf_gtest.log",
LOG_LEVEL_INFO);
_shared_maat_inst = maat_new(opts, tsg_table_info);
maat_options_free(opts);
if (NULL == _shared_maat_inst) {
log_fatal(logger, MODULE_FRAMEWORK_PERF_GTEST,
"[%s:%d] create maat instance in MaatTSGFqdnScan failed.",
__FUNCTION__, __LINE__);
}
}
static void TearDownTestCase() {
maat_free(_shared_maat_inst);
log_handle_destroy(logger);
}
static struct log_handle *logger;
static struct maat *_shared_maat_inst;
};
struct maat *MaatTSGFqdnScan::_shared_maat_inst;
struct log_handle *MaatTSGFqdnScan::logger;
#if 0
TEST_F(MaatTSGFqdnScan, mem_used) {
char keyword_buf[128];
const char *table_name1 = "TSG_OBJ_FQDN";
const char *table_name2= "TSG_IP_ADDR";
struct maat *maat_inst = MaatTSGFqdnScan::_shared_maat_inst;
struct log_handle *logger = MaatTSGFqdnScan::logger;
for (size_t i = 0; i < 100; i++) {
random_keyword_generate(keyword_buf, sizeof(keyword_buf));
int ret = expr_table_set_line(maat_inst, table_name1, MAAT_OP_ADD,
600000000+i, 600000000+i, keyword_buf,
"null", 1, 0, 0, 0);
EXPECT_EQ(ret, 1);
ret = ip_table_set_line(maat_inst, table_name2, MAAT_OP_ADD,
600000000+i, 600000000+i, IPv4, "100.64.1.1",
"100.64.1.2", 0);
EXPECT_EQ(ret, 1);
log_info(logger, MODULE_FRAMEWORK_PERF_GTEST, "MaatTSGFqdnScan update one line");
sleep(150);
}
}
#endif
class MaatPerfIPPortPluginScan : public testing::Test
{
protected:

View File

@@ -2,6 +2,7 @@
#include "maat_utils.h"
#include "maat_rule.h"
#include "json2iris.h"
#include "test_utils.h"
#include "maat_table.h"
#include "maat_config_monitor.h"
#include "maat_redis_monitor.h"
@@ -15,29 +16,26 @@ const char *g_json_filename = "maat_json.json";
struct log_handle *g_logger = NULL;
TEST(json_mode, maat_scan_string) {
char tmp_iris_path[PATH_MAX] = {0};
char iris_idx_path[PATH_MAX] = {0};
char json_iris_path[PATH_MAX] = {0};
snprintf(json_iris_path, sizeof(json_iris_path),
"./%s_iris_tmp", g_json_filename);
char json_path[PATH_MAX] = {0};
long long results[5] = {0};
size_t n_hit_result = 0;
int thread_id = 0;
char scan_data[128] = "string1, string2, string3, string4, "
"string5, string6, string7, string8";
const char *table_name = "KEYWORDS_TABLE";
if (access(json_iris_path, F_OK) < 0) {
char *json_buff = NULL;
size_t json_buff_sz = 0;
snprintf(json_iris_path, sizeof(json_iris_path), "./%s_iris_tmp",
g_json_filename);
snprintf(iris_idx_path, sizeof(iris_idx_path), "%s/index", json_iris_path);
snprintf(json_path, sizeof(json_path), "./%s", g_json_filename);
int ret = load_file_to_memory(g_json_filename,
(unsigned char**)&json_buff,
&json_buff_sz);
EXPECT_NE(ret, -1);
ret = json2iris(json_buff, g_json_filename, NULL, tmp_iris_path,
sizeof(tmp_iris_path), NULL, NULL, g_logger);
FREE(json_buff);
EXPECT_NE(ret, -1);
}
int ret = write_json_to_iris(g_json_filename, iris_idx_path,
sizeof(iris_idx_path), g_logger);
assert(ret == 0);
struct maat_options *opts = maat_options_new();
char json_path[PATH_MAX] = {0};
snprintf(json_path, sizeof(json_path), "./%s", g_json_filename);
maat_options_set_json_file(opts, json_path);
maat_options_set_stat_on(opts);
maat_options_set_perf_on(opts);
@@ -45,188 +43,76 @@ TEST(json_mode, maat_scan_string) {
struct maat *maat_inst = maat_new(opts, g_table_info_path);
EXPECT_TRUE(maat_inst != NULL);
const char *table_name = "KEYWORDS_TABLE";
int table_id = maat_get_table_id(maat_inst, table_name);
char scan_data[128] =
"string1, string2, string3, string4, string5, string6, string7, string8";
long long results[5] = {0};
size_t n_hit_result = 0;
int thread_id = 0;
struct maat_state *state = maat_state_new(maat_inst, thread_id);
int ret = maat_scan_string(maat_inst, table_id, scan_data, strlen(scan_data),
results, sizeof(results), &n_hit_result, state);
ret = maat_scan_string(maat_inst, table_id, scan_data, strlen(scan_data),
results, sizeof(results), &n_hit_result, state);
EXPECT_EQ(ret, MAAT_SCAN_HIT);
EXPECT_EQ(n_hit_result, 1);
EXPECT_EQ(results[0], 182);
maat_options_free(opts);
maat_state_free(state);
state = NULL;
maat_free(maat_inst);
}
TEST(iris_mode, maat_scan_string) {
char tmp_iris_path[512] = {0};
char json_iris_path[512] = {0};
snprintf(json_iris_path, sizeof(json_iris_path),
"./%s_iris_tmp", g_json_filename);
char iris_idx_path[PATH_MAX] = {0};
long long results[5] = {0};
size_t n_hit_result = 0;
int thread_id = 0;
const char *table_name = "KEYWORDS_TABLE";
char scan_data[128] = "string1, string2, string3, string4, "
"string5, string6, string7, string8";
if (access(json_iris_path, F_OK) < 0) {
char *json_buff = NULL;
size_t json_buff_sz = 0;
snprintf(json_iris_path, sizeof(json_iris_path), "./%s_iris_tmp",
g_json_filename);
snprintf(iris_idx_path, sizeof(iris_idx_path), "%s/index", json_iris_path);
int ret = load_file_to_memory(g_json_filename,
(unsigned char**)&json_buff,
&json_buff_sz);
EXPECT_NE(ret, -1);
ret = json2iris(json_buff, g_json_filename, NULL, tmp_iris_path,
sizeof(tmp_iris_path), NULL, NULL, g_logger);
FREE(json_buff);
EXPECT_NE(ret, -1);
}
char tmp_iris_full_idx_path[PATH_MAX] = {0};
char tmp_iris_inc_idx_path[PATH_MAX] = {0};
snprintf(tmp_iris_full_idx_path, sizeof(tmp_iris_full_idx_path),
"%s/index", json_iris_path);
snprintf(tmp_iris_inc_idx_path, sizeof(tmp_iris_inc_idx_path),
"%s/index", json_iris_path);
int ret = write_json_to_iris(g_json_filename, iris_idx_path,
sizeof(iris_idx_path), g_logger);
assert(ret == 0);
struct maat_options *opts = maat_options_new();
maat_options_set_stat_on(opts);
maat_options_set_perf_on(opts);
maat_options_set_iris(opts, tmp_iris_full_idx_path, tmp_iris_inc_idx_path);
maat_options_set_iris(opts, iris_idx_path, iris_idx_path);
maat_options_set_logger(opts, "./maat_input_mode_gtest.log", LOG_LEVEL_INFO);
struct maat *maat_inst = maat_new(opts, g_table_info_path);
EXPECT_TRUE(maat_inst != NULL);
const char *table_name = "KEYWORDS_TABLE";
int table_id = maat_get_table_id(maat_inst, table_name);
char scan_data[128] =
"string1, string2, string3, string4, string5, string6, string7, string8";
long long results[5] = {0};
size_t n_hit_result = 0;
int thread_id = 0;
struct maat_state *state = maat_state_new(maat_inst, thread_id);
int ret = maat_scan_string(maat_inst, table_id, scan_data, strlen(scan_data),
results, sizeof(results), &n_hit_result, state);
ret = maat_scan_string(maat_inst, table_id, scan_data, strlen(scan_data),
results, sizeof(results), &n_hit_result, state);
EXPECT_EQ(ret, MAAT_SCAN_HIT);
EXPECT_EQ(n_hit_result, 1);
EXPECT_EQ(results[0], 182);
maat_options_free(opts);
maat_state_free(state);
state = NULL;
maat_free(maat_inst);
}
int count_line_num_cb(const char *table_name, const char *line, void *u_para)
{
(*((unsigned int *)u_para))++;
return 0;
}
int line_idx = 0;
long long absolute_expire_time=0;
int make_serial_rule(const char *table_name, const char *line, void *u_para)
{
struct serial_rule *s_rule=(struct serial_rule *)u_para;
redisContext *ctx = s_rule->ref_ctx;
char *buff = ALLOC(char, strlen(line) + 1);
memcpy(buff, line, strlen(line) + 1);
while(buff[strlen(line) - 1] == '\n' || buff[strlen(line) - 1] == '\t') {
buff[strlen(line) - 1] = '\0';
}
const char *redis_rule_key = "TEST_RULE_KEY";
redisReply *reply =
maat_wrap_redis_command(ctx, NULL, "INCRBY %s %d", redis_rule_key, 1);
if (reply->type == REDIS_REPLY_NIL) {
printf("incrby redis_rule_key:%s failed.", redis_rule_key);
return -1;
} else {
s_rule->rule_id = maat_read_redis_integer(reply);
freeReplyObject(reply);
reply = NULL;
}
maat_set_serial_rule(s_rule + line_idx, MAAT_OP_ADD, s_rule->rule_id,
table_name, buff, absolute_expire_time);
(s_rule + line_idx)->ref_ctx = ctx;
line_idx++;
FREE(buff);
return 0;
}
TEST(redis_mode, maat_scan_string) {
char json_iris_path[512] = {0};
char redis_ip[64] = "127.0.0.1";
char redis_ip[32] = "127.0.0.1";
int redis_port = 6379;
int redis_db = 0;
const char *table_name = "KEYWORDS_TABLE";
long long results[5] = {0};
size_t n_hit_result = 0;
int thread_id = 0;
char scan_data[128] = "string1, string2, string3, string4, "
"string5, string6, string7, string8";
snprintf(json_iris_path, sizeof(json_iris_path),
"./%s_iris_tmp", g_json_filename);
redisContext *c =
maat_connect_redis(redis_ip, redis_port, redis_db, g_logger);
EXPECT_TRUE(c != NULL);
redisReply *reply = maat_wrap_redis_command(c, NULL, "flushdb");
EXPECT_TRUE(reply != NULL);
freeReplyObject(reply);
reply = NULL;
if (access(json_iris_path, F_OK) < 0) {
char tmp_iris_path[128] = {0};
char *json_buff = NULL;
size_t json_buff_sz = 0;
int ret = load_file_to_memory(g_json_filename,
(unsigned char **)&json_buff,
&json_buff_sz);
EXPECT_NE(ret, -1);
ret = json2iris(json_buff, g_json_filename, c, tmp_iris_path,
sizeof(tmp_iris_path), NULL, NULL, g_logger);
FREE(json_buff);
EXPECT_NE(ret, -1);
}
size_t total_line_cnt = 0;
char tmp_iris_full_idx_path[PATH_MAX] = {0};
snprintf(tmp_iris_full_idx_path, sizeof(tmp_iris_full_idx_path),
"%s/index", json_iris_path);
config_monitor_traverse(0, tmp_iris_full_idx_path, NULL, count_line_num_cb,
NULL, &total_line_cnt, NULL, g_logger);
struct serial_rule *s_rule = ALLOC(struct serial_rule, total_line_cnt);
s_rule->ref_ctx = c;
long long server_time = maat_redis_server_time_s(c);
EXPECT_NE(server_time, -1);
absolute_expire_time = server_time + 300;
config_monitor_traverse(0, tmp_iris_full_idx_path, NULL, make_serial_rule,
NULL, s_rule, NULL, g_logger);
s_rule->ref_ctx = NULL;
int success_cnt = 0;
do {
success_cnt = maat_cmd_write_rule(c, s_rule, total_line_cnt,
server_time, g_logger);
} while (success_cnt < 0);
EXPECT_EQ(success_cnt, (int)total_line_cnt);
for (size_t i = 0; i < total_line_cnt; i++) {
maat_clear_rule_cache(s_rule + i);
}
FREE(s_rule);
redisFree(c);
int ret = write_json_to_redis(g_json_filename, redis_ip, redis_port,
redis_db, g_logger);
assert(ret == 0);
struct maat_options *opts = maat_options_new();
maat_options_set_stat_on(opts);
@@ -236,23 +122,19 @@ TEST(redis_mode, maat_scan_string) {
LOG_LEVEL_INFO);
struct maat *maat_inst = maat_new(opts, g_table_info_path);
const char *table_name = "KEYWORDS_TABLE";
int table_id = maat_get_table_id(maat_inst, table_name);
char scan_data[128] =
"string1, string2, string3, string4, string5, string6, string7, string8";
long long results[5] = {0};
size_t n_hit_result = 0;
int thread_id = 0;
assert(maat_inst != NULL);
struct maat_state *state = maat_state_new(maat_inst, thread_id);
int ret = maat_scan_string(maat_inst, table_id, scan_data, strlen(scan_data),
results, sizeof(results), &n_hit_result, state);
int table_id = maat_get_table_id(maat_inst, table_name);
ret = maat_scan_string(maat_inst, table_id, scan_data, strlen(scan_data),
results, sizeof(results), &n_hit_result, state);
EXPECT_EQ(ret, MAAT_SCAN_HIT);
EXPECT_EQ(n_hit_result, 1);
EXPECT_EQ(results[0], 182);
maat_options_free(opts);
maat_state_free(state);
state = NULL;
maat_free(maat_inst);
}

View File

@@ -9,28 +9,34 @@
#include <assert.h>
#include <unistd.h>
int count_line_num_cb(const char *table_name, const char *line, void *u_para)
int line_idx = 0;
long long absolute_expire_time = 0;
static int
count_line_num_cb(const char *table_name, const char *line, void *u_para)
{
(*((unsigned int *)u_para))++;
return 0;
}
int line_idx = 0;
long long absolute_expire_time = 0;
int make_serial_rule(const char *table_name, const char *line, void *u_para)
static int
make_serial_rule(const char *table_name, const char *line, void *u_para)
{
struct serial_rule *s_rule=(struct serial_rule *)u_para;
redisContext *ctx = s_rule->ref_ctx;
char *buff = ALLOC(char, strlen(line) + 1);
memcpy(buff, line, strlen(line) + 1);
while (buff[strlen(line) - 1] == '\n' || buff[strlen(line) - 1] == '\t') {
while (buff[strlen(line) - 1] == '\n' ||
buff[strlen(line) - 1] == '\t') {
buff[strlen(line) - 1] = '\0';
}
const char *redis_rule_key = "TEST_RULE_KEY";
redisReply *reply = maat_wrap_redis_command(ctx, NULL, "INCRBY %s %d",
redis_rule_key, 1);
redisReply *reply =
maat_wrap_redis_command(ctx, NULL, "INCRBY %s %d", redis_rule_key, 1);
if (reply->type == REDIS_REPLY_NIL) {
printf("incrby redis_rule_key:%s failed.", redis_rule_key);
return -1;
@@ -49,29 +55,32 @@ int make_serial_rule(const char *table_name, const char *line, void *u_para)
return 0;
}
int write_json_to_iris(const char* json_fn, char *iris_path, size_t path_sz, struct log_handle *logger)
int write_json_to_iris(const char* json_fn, char *iris_path, size_t path_sz,
struct log_handle *logger)
{
char *json_buff = NULL;
size_t json_buff_sz = 0;
int ret = load_file_to_memory(json_fn, (unsigned char **)&json_buff,
&json_buff_sz);
&json_buff_sz);
if (ret < 0) {
return -1;
}
ret = json2iris(json_buff, json_fn, NULL, iris_path,
path_sz, NULL, NULL, logger);
ret = json2iris(json_buff, json_fn, NULL, iris_path, path_sz, NULL, NULL, logger);
FREE(json_buff);
if (ret < 0) {
return -1;
}
return 0;
}
int write_iris_to_redis(const char* iris_path, char *redis_ip, int redis_port, int redis_db,
struct log_handle *logger)
int write_iris_to_redis(const char *iris_path, char *redis_ip, int redis_port,
int redis_db, struct log_handle *logger)
{
redisContext *c = maat_connect_redis(redis_ip, redis_port, redis_db, logger);
if (NULL == c) {
@@ -119,12 +128,21 @@ int write_iris_to_redis(const char* iris_path, char *redis_ip, int redis_port, i
redisFree(c);
return 0;
}
int write_json_to_redis(const char* json_filename, char *redis_ip, int redis_port, int redis_db,
struct log_handle *logger)
int write_json_to_redis(const char *json_filename, char *redis_ip, int redis_port,
int redis_db, struct log_handle *logger)
{
char iris_path[512] = {0};
write_json_to_iris(json_filename, iris_path, sizeof(iris_path), logger);
write_iris_to_redis(iris_path, redis_ip, redis_port, redis_db, logger);
int ret = write_json_to_iris(json_filename, iris_path, sizeof(iris_path), logger);
if (ret < 0) {
return -1;
}
ret = write_iris_to_redis(iris_path, redis_ip, redis_port, redis_db, logger);
if (ret < 0) {
return -1;
}
return 0;
}
@@ -147,7 +165,8 @@ int compile_table_set_line(struct maat *maat_inst, const char *table_name,
return maat_cmd_set_line(maat_inst, &line_rule);
}
#define TO_GROUP2X_KEY(group_id, parent_id, clause_index) (((unsigned long)group_id<<32|parent_id) + clause_index)
#define TO_GROUP2X_KEY(group_id, parent_id, clause_index) \
(((unsigned long)group_id<<32|parent_id) + clause_index)
int group2compile_table_set_line(struct maat *maat_inst, const char *table_name,
enum maat_operation op, long long group_id,
@@ -197,9 +216,10 @@ int expr_table_set_line(struct maat *maat_inst, const char *table_name,
return 0;
}
enum table_type table_type = table_manager_get_table_type(maat_inst->tbl_mgr,
table_id);
assert(table_type == TABLE_TYPE_EXPR || table_type == TABLE_TYPE_EXPR_PLUS);
enum table_type table_type =
table_manager_get_table_type(maat_inst->tbl_mgr, table_id);
assert(table_type == TABLE_TYPE_EXPR ||
table_type == TABLE_TYPE_EXPR_PLUS);
if (table_type == TABLE_TYPE_EXPR_PLUS) {
sprintf(table_line, "%lld\t%lld\t%s\t%s\t%d\t%d\t%d\t%d",
@@ -231,9 +251,10 @@ int interval_table_set_line(struct maat *maat_inst, const char *table_name,
return 0;
}
enum table_type table_type = table_manager_get_table_type(maat_inst->tbl_mgr,
table_id);
assert(table_type == TABLE_TYPE_INTERVAL || table_type == TABLE_TYPE_INTERVAL_PLUS);
enum table_type table_type =
table_manager_get_table_type(maat_inst->tbl_mgr, table_id);
assert(table_type == TABLE_TYPE_INTERVAL ||
table_type == TABLE_TYPE_INTERVAL_PLUS);
if (table_type == TABLE_TYPE_INTERVAL_PLUS) {
sprintf(table_line, "%lld\t%lld\t%s\t%u\t%u\t%d",
@@ -254,8 +275,8 @@ int interval_table_set_line(struct maat *maat_inst, const char *table_name,
int ip_table_set_line(struct maat *maat_inst, const char *table_name,
enum maat_operation op, long long item_id,
long long group_id, enum IP_TYPE type, const char *ip1,
const char *ip2, int expire_after)
long long group_id, enum IP_TYPE type,
const char *ip1, const char *ip2, int expire_after)
{
char table_line[1024] = {0};
int table_id = maat_get_table_id(maat_inst, table_name);
@@ -300,4 +321,16 @@ int flag_table_set_line(struct maat *maat_inst, const char *table_name,
line_rule.expire_after = expire_after;
return maat_cmd_set_line(maat_inst, &line_rule);
}
}
void random_keyword_generate(char *keyword_buf, size_t sz)
{
#define MIN_KEYWORD_LEN 4
size_t i = 0, len = 0;
len = random() % (sz - 1 - MIN_KEYWORD_LEN) + MIN_KEYWORD_LEN;
for (i = 0; i < len; i++) {
keyword_buf[i] = 'a' + random() % ('z' - 'a');
}
keyword_buf[i] = '\0';
}

View File

@@ -1,37 +1,53 @@
#pragma once
#include "maat_utils.h"
#include "maat_command.h"
#include "ip_matcher.h"
int write_json_to_redis(const char* json_filename, char *redis_ip, int redis_port, int redis_db,
struct log_handle *logger);
int write_iris_to_redis(const char* iris_path, char *redis_ip, int redis_port, int redis_db,
struct log_handle *logger);
#include "ip_matcher/ip_matcher.h"
int write_json_to_redis(const char *json_filename, char *redis_ip, int redis_port,
int redis_db, struct log_handle *logger);
int write_iris_to_redis(const char *iris_path, char *redis_ip, int redis_port,
int redis_db, struct log_handle *logger);
int write_json_to_iris(const char* json_fn, char *iris_path, size_t path_sz,
struct log_handle *logger);
int compile_table_set_line(struct maat *maat_inst, const char *table_name,
enum maat_operation op, long long compile_id,
const char *user_region, int clause_num,
int expire_after);
int group2compile_table_set_line(struct maat *maat_inst, const char *table_name,
enum maat_operation op, long long group_id,
long long compile_id, int not_flag,
const char *vtable_name, int clause_index,
int expire_after);
int group2group_table_set_line(struct maat *maat_inst, const char *table_name,
enum maat_operation op, long long group_id,
long long sub_group_id, int expire_after);
int expr_table_set_line(struct maat *maat_inst, const char *table_name,
enum maat_operation op, long long item_id,
long long group_id, const char *keywords,
const char *district, int expr_type,
int match_method, int is_hexbin, int expire_after);
int interval_table_set_line(struct maat *maat_inst, const char *table_name,
enum maat_operation op, long long item_id, long long group_id,
unsigned int low_boundary, unsigned int up_boundary,
const char *district, int expire_after);
enum maat_operation op, long long item_id,
long long group_id, unsigned int low_boundary,
unsigned int up_boundary, const char *district,
int expire_after);
int ip_table_set_line(struct maat *maat_inst, const char *table_name,
enum maat_operation op, long long item_id,
long long group_id, enum IP_TYPE type, const char *ip1,
const char *ip2, int expire_after);
int flag_table_set_line(struct maat *maat_inst, const char *table_name,
enum maat_operation op, long long item_id,
long long group_id, long long flag,
long long flag_mask, int expire_after);
enum maat_operation op, long long item_id,
long long group_id, long long flag,
long long flag_mask, int expire_after);
void random_keyword_generate(char *keyword_buf, size_t sz);