2023-02-15 11:53:46 +08:00
|
|
|
#include "maat.h"
|
2022-12-05 23:21:18 +08:00
|
|
|
#include "maat_utils.h"
|
|
|
|
|
#include "maat_rule.h"
|
|
|
|
|
#include "json2iris.h"
|
2023-01-31 20:39:53 +08:00
|
|
|
#include "maat_table.h"
|
2022-12-05 23:21:18 +08:00
|
|
|
#include "maat_config_monitor.h"
|
|
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
2023-03-23 19:16:23 +08:00
|
|
|
#define MODULE_INPUT_MODE_GTEST module_name_str("maat.input_mode_gtest")
|
|
|
|
|
|
2022-12-05 23:21:18 +08:00
|
|
|
const char *table_info_path = "./table_info.conf";
|
|
|
|
|
const char *json_filename = "maat_json.json";
|
2023-02-09 22:13:15 +08:00
|
|
|
struct log_handle *g_logger = NULL;
|
2022-12-05 23:21:18 +08:00
|
|
|
|
|
|
|
|
TEST(json_mode, maat_scan_string) {
|
2023-02-21 11:27:18 +08:00
|
|
|
char tmp_iris_path[PATH_MAX] = {0};
|
|
|
|
|
char json_iris_path[PATH_MAX] = {0};
|
2022-12-05 23:21:18 +08:00
|
|
|
snprintf(json_iris_path, sizeof(json_iris_path), "./%s_iris_tmp", json_filename);
|
|
|
|
|
|
2023-02-09 22:13:15 +08:00
|
|
|
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);
|
2023-03-15 11:36:54 +08:00
|
|
|
FREE(json_buff);
|
2023-02-09 22:13:15 +08:00
|
|
|
EXPECT_NE(ret, -1);
|
2022-12-05 23:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct maat_options *opts = maat_options_new();
|
2023-02-21 11:27:18 +08:00
|
|
|
char json_path[PATH_MAX] = {0};
|
2022-12-05 23:21:18 +08:00
|
|
|
snprintf(json_path, sizeof(json_path), "./%s", json_filename);
|
|
|
|
|
maat_options_set_json_file(opts, json_path);
|
2023-03-28 15:41:24 +08:00
|
|
|
maat_options_set_logger(opts, "./maat_input_mode_gtest.log", LOG_LEVEL_INFO);
|
2022-12-05 23:21:18 +08:00
|
|
|
|
|
|
|
|
struct maat *maat_instance = maat_new(opts, table_info_path);
|
2023-02-16 16:45:06 +08:00
|
|
|
EXPECT_TRUE(maat_instance != NULL);
|
2022-12-05 23:21:18 +08:00
|
|
|
|
2023-02-20 11:43:43 +08:00
|
|
|
const char *table_name = "KEYWORDS_TABLE";
|
2023-03-01 17:44:07 +08:00
|
|
|
int table_id = maat_get_table_id(maat_instance, table_name);
|
2023-02-09 22:13:15 +08:00
|
|
|
char scan_data[128] = "string1, string2, string3, string4, string5, string6, string7, string8";
|
2023-02-22 15:22:41 +08:00
|
|
|
long long results[5] = {0};
|
2023-01-30 21:59:35 +08:00
|
|
|
size_t n_hit_result = 0;
|
2023-03-23 11:57:17 +08:00
|
|
|
int thread_id = 0;
|
|
|
|
|
struct maat_state *state = maat_state_new(maat_instance, thread_id);
|
2023-03-28 14:07:44 +08:00
|
|
|
int ret = maat_scan_string(maat_instance, table_id, scan_data, strlen(scan_data),
|
2023-03-23 11:57:17 +08:00
|
|
|
results, sizeof(results), &n_hit_result, state);
|
2023-02-03 17:28:14 +08:00
|
|
|
EXPECT_EQ(ret, MAAT_SCAN_HIT);
|
2023-01-30 21:59:35 +08:00
|
|
|
EXPECT_EQ(n_hit_result, 1);
|
2023-02-09 22:13:15 +08:00
|
|
|
EXPECT_EQ(results[0], 182);
|
2022-12-05 23:21:18 +08:00
|
|
|
|
2023-03-15 11:36:54 +08:00
|
|
|
long long clause_id = maat_runtime_get_sequence(maat_instance->maat_rt, "test_seq");
|
|
|
|
|
EXPECT_EQ(clause_id, 0);
|
|
|
|
|
clause_id = maat_runtime_get_sequence(maat_instance->maat_rt, "test_seq");
|
|
|
|
|
EXPECT_EQ(clause_id, 1);
|
|
|
|
|
clause_id = maat_runtime_get_sequence(maat_instance->maat_rt, "test_seq");
|
|
|
|
|
EXPECT_EQ(clause_id, 2);
|
|
|
|
|
|
2023-01-30 21:59:35 +08:00
|
|
|
maat_options_free(opts);
|
2023-03-23 11:57:17 +08:00
|
|
|
maat_state_free(state);
|
|
|
|
|
state = NULL;
|
2022-12-05 23:21:18 +08:00
|
|
|
maat_free(maat_instance);
|
|
|
|
|
}
|
2023-03-16 15:16:42 +08:00
|
|
|
|
2022-12-05 23:21:18 +08:00
|
|
|
TEST(iris_mode, maat_scan_string) {
|
2023-02-21 11:27:18 +08:00
|
|
|
char tmp_iris_path[512] = {0};
|
|
|
|
|
char json_iris_path[512] = {0};
|
2022-12-05 23:21:18 +08:00
|
|
|
snprintf(json_iris_path, sizeof(json_iris_path), "./%s_iris_tmp", json_filename);
|
|
|
|
|
|
2023-02-09 22:13:15 +08:00
|
|
|
if (access(json_iris_path, F_OK) < 0) {
|
2022-12-05 23:21:18 +08:00
|
|
|
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);
|
|
|
|
|
|
2023-02-09 22:13:15 +08:00
|
|
|
ret = json2iris(json_buff, json_filename, NULL, tmp_iris_path, sizeof(tmp_iris_path),
|
|
|
|
|
NULL, NULL, g_logger);
|
2023-03-15 11:36:54 +08:00
|
|
|
FREE(json_buff);
|
2022-12-05 23:21:18 +08:00
|
|
|
EXPECT_NE(ret, -1);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-21 11:27:18 +08:00
|
|
|
char tmp_iris_full_idx_path[PATH_MAX] = {0};
|
|
|
|
|
char tmp_iris_inc_idx_path[PATH_MAX] = {0};
|
2022-12-05 23:21:18 +08:00
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
struct maat_options *opts = maat_options_new();
|
2023-01-06 18:54:59 +08:00
|
|
|
maat_options_set_iris(opts, tmp_iris_full_idx_path, tmp_iris_inc_idx_path);
|
2023-03-28 15:41:24 +08:00
|
|
|
maat_options_set_logger(opts, "./maat_input_mode_gtest.log", LOG_LEVEL_INFO);
|
2022-12-05 23:21:18 +08:00
|
|
|
|
|
|
|
|
struct maat *maat_instance = maat_new(opts, table_info_path);
|
2023-02-16 16:45:06 +08:00
|
|
|
EXPECT_TRUE(maat_instance != NULL);
|
2022-12-05 23:21:18 +08:00
|
|
|
|
2023-02-20 11:43:43 +08:00
|
|
|
const char *table_name = "KEYWORDS_TABLE";
|
2023-03-01 17:44:07 +08:00
|
|
|
int table_id = maat_get_table_id(maat_instance, table_name);
|
2023-02-09 22:13:15 +08:00
|
|
|
char scan_data[128] = "string1, string2, string3, string4, string5, string6, string7, string8";
|
2023-02-22 15:22:41 +08:00
|
|
|
long long results[5] = {0};
|
2023-01-30 21:59:35 +08:00
|
|
|
size_t n_hit_result = 0;
|
2023-03-23 11:57:17 +08:00
|
|
|
int thread_id = 0;
|
|
|
|
|
struct maat_state *state = maat_state_new(maat_instance, thread_id);
|
2023-03-28 14:07:44 +08:00
|
|
|
int ret = maat_scan_string(maat_instance, table_id, scan_data, strlen(scan_data),
|
2023-03-23 11:57:17 +08:00
|
|
|
results, sizeof(results), &n_hit_result, state);
|
2023-02-03 17:28:14 +08:00
|
|
|
EXPECT_EQ(ret, MAAT_SCAN_HIT);
|
2023-01-30 21:59:35 +08:00
|
|
|
EXPECT_EQ(n_hit_result, 1);
|
2023-02-09 22:13:15 +08:00
|
|
|
EXPECT_EQ(results[0], 182);
|
2022-12-05 23:21:18 +08:00
|
|
|
|
2023-01-30 21:59:35 +08:00
|
|
|
maat_options_free(opts);
|
2023-03-23 11:57:17 +08:00
|
|
|
maat_state_free(state);
|
|
|
|
|
state = NULL;
|
2022-12-05 23:21:18 +08:00
|
|
|
maat_free(maat_instance);
|
|
|
|
|
}
|
2023-02-09 22:13:15 +08:00
|
|
|
|
2022-12-05 23:21:18 +08:00
|
|
|
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;
|
2023-03-23 19:16:23 +08:00
|
|
|
redisContext *ctx = s_rule->ref_ctx;
|
2022-12-05 23:21:18 +08:00
|
|
|
char *buff = ALLOC(char, strlen(line) + 1);
|
|
|
|
|
|
2023-03-23 19:16:23 +08:00
|
|
|
memcpy(buff, line, strlen(line) + 1);
|
|
|
|
|
while(buff[strlen(line) - 1] == '\n' || buff[strlen(line) - 1] == '\t') {
|
|
|
|
|
buff[strlen(line) - 1] = '\0';
|
2022-12-05 23:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-23 19:16:23 +08:00
|
|
|
const char *redis_rule_key = "TEST_RULE_KEY";
|
|
|
|
|
redisReply *reply = maat_cmd_wrap_redis_command(ctx, "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_cmd_read_redis_integer(reply);
|
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
reply = NULL;
|
2022-12-05 23:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-23 19:16:23 +08:00
|
|
|
maat_cmd_set_serial_rule(s_rule + line_idx, MAAT_OP_ADD, s_rule->rule_id, table_name,
|
2023-02-21 11:27:18 +08:00
|
|
|
buff, absolute_expire_time);
|
2023-03-23 19:16:23 +08:00
|
|
|
(s_rule + line_idx)->ref_ctx = ctx;
|
2022-12-05 23:21:18 +08:00
|
|
|
line_idx++;
|
2023-03-23 19:16:23 +08:00
|
|
|
|
|
|
|
|
FREE(buff);
|
2022-12-05 23:21:18 +08:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(redis_mode, maat_scan_string) {
|
2023-02-21 11:27:18 +08:00
|
|
|
char json_iris_path[512] = {0};
|
2022-12-05 23:21:18 +08:00
|
|
|
char redis_ip[64] = "127.0.0.1";
|
|
|
|
|
int redis_port = 6379;
|
|
|
|
|
int redis_db = 0;
|
|
|
|
|
|
|
|
|
|
snprintf(json_iris_path, sizeof(json_iris_path), "./%s_iris_tmp", json_filename);
|
|
|
|
|
|
2023-02-09 22:13:15 +08:00
|
|
|
redisContext *c = maat_cmd_connect_redis(redis_ip, redis_port, redis_db, g_logger);
|
2023-02-16 16:45:06 +08:00
|
|
|
EXPECT_TRUE(c != NULL);
|
2022-12-05 23:21:18 +08:00
|
|
|
|
2022-12-06 00:35:36 +08:00
|
|
|
redisReply *reply = maat_cmd_wrap_redis_command(c, "flushdb");
|
2023-02-16 16:45:06 +08:00
|
|
|
EXPECT_TRUE(reply != NULL);
|
2023-04-05 21:09:19 +08:00
|
|
|
freeReplyObject(reply);
|
|
|
|
|
reply = NULL;
|
2022-12-06 00:35:36 +08:00
|
|
|
|
2023-02-09 22:13:15 +08:00
|
|
|
if (access(json_iris_path, F_OK) < 0) {
|
2022-12-05 23:21:18 +08:00
|
|
|
char tmp_iris_path[128] = {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);
|
|
|
|
|
|
2023-02-09 22:13:15 +08:00
|
|
|
ret = json2iris(json_buff, json_filename, c, tmp_iris_path, sizeof(tmp_iris_path),
|
|
|
|
|
NULL, NULL, g_logger);
|
2023-03-15 11:36:54 +08:00
|
|
|
FREE(json_buff);
|
2022-12-05 23:21:18 +08:00
|
|
|
EXPECT_NE(ret, -1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t total_line_cnt = 0;
|
2023-02-21 11:27:18 +08:00
|
|
|
char tmp_iris_full_idx_path[PATH_MAX] = {0};
|
2022-12-05 23:21:18 +08:00
|
|
|
snprintf(tmp_iris_full_idx_path, sizeof(tmp_iris_full_idx_path), "%s/index", json_iris_path);
|
2023-02-09 22:13:15 +08:00
|
|
|
config_monitor_traverse(0, tmp_iris_full_idx_path, NULL, count_line_num_cb, NULL,
|
2023-05-04 17:10:19 +08:00
|
|
|
&total_line_cnt, NULL, g_logger);
|
2022-12-05 23:21:18 +08:00
|
|
|
|
|
|
|
|
struct serial_rule *s_rule = ALLOC(struct serial_rule, total_line_cnt);
|
2023-03-23 19:16:23 +08:00
|
|
|
s_rule->ref_ctx = c;
|
2022-12-05 23:21:18 +08:00
|
|
|
long long server_time = maat_cmd_redis_server_time_s(c);
|
|
|
|
|
EXPECT_NE(server_time, -1);
|
|
|
|
|
|
|
|
|
|
absolute_expire_time = server_time + 300;
|
2023-02-09 22:13:15 +08:00
|
|
|
config_monitor_traverse(0, tmp_iris_full_idx_path, NULL, make_serial_rule, NULL,
|
2023-05-04 17:10:19 +08:00
|
|
|
s_rule, NULL, g_logger);
|
2023-03-23 19:16:23 +08:00
|
|
|
s_rule->ref_ctx = NULL;
|
2022-12-05 23:21:18 +08:00
|
|
|
int success_cnt = 0;
|
|
|
|
|
do {
|
2023-02-09 22:13:15 +08:00
|
|
|
success_cnt = maat_cmd_write_rule(c, s_rule, total_line_cnt, server_time, g_logger);
|
2022-12-05 23:21:18 +08:00
|
|
|
} while (success_cnt < 0);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(success_cnt, (int)total_line_cnt);
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < total_line_cnt; i++) {
|
|
|
|
|
maat_cmd_clear_rule_cache(s_rule + i);
|
|
|
|
|
}
|
|
|
|
|
FREE(s_rule);
|
|
|
|
|
redisFree(c);
|
|
|
|
|
|
|
|
|
|
struct maat_options *opts = maat_options_new();
|
2023-01-06 18:54:59 +08:00
|
|
|
maat_options_set_redis(opts, redis_ip, redis_port, redis_db);
|
2023-03-28 15:41:24 +08:00
|
|
|
maat_options_set_logger(opts, "./maat_input_mode_gtest.log", LOG_LEVEL_INFO);
|
2022-12-05 23:21:18 +08:00
|
|
|
|
|
|
|
|
struct maat *maat_instance = maat_new(opts, table_info_path);
|
2023-02-20 11:43:43 +08:00
|
|
|
const char *table_name = "KEYWORDS_TABLE";
|
2023-03-01 17:44:07 +08:00
|
|
|
int table_id = maat_get_table_id(maat_instance, table_name);
|
2023-02-09 22:13:15 +08:00
|
|
|
char scan_data[128] = "string1, string2, string3, string4, string5, string6, string7, string8";
|
2023-02-22 15:22:41 +08:00
|
|
|
long long results[5] = {0};
|
2023-01-30 21:59:35 +08:00
|
|
|
size_t n_hit_result = 0;
|
2023-03-23 11:57:17 +08:00
|
|
|
int thread_id = 0;
|
|
|
|
|
struct maat_state *state = maat_state_new(maat_instance, thread_id);
|
2023-03-28 14:07:44 +08:00
|
|
|
int ret = maat_scan_string(maat_instance, table_id, scan_data, strlen(scan_data),
|
2023-03-23 11:57:17 +08:00
|
|
|
results, sizeof(results), &n_hit_result, state);
|
2023-02-03 17:28:14 +08:00
|
|
|
EXPECT_EQ(ret, MAAT_SCAN_HIT);
|
2023-01-30 21:59:35 +08:00
|
|
|
EXPECT_EQ(n_hit_result, 1);
|
2023-02-09 22:13:15 +08:00
|
|
|
EXPECT_EQ(results[0], 182);
|
2022-12-05 23:21:18 +08:00
|
|
|
|
2023-01-30 21:59:35 +08:00
|
|
|
maat_options_free(opts);
|
2023-03-23 11:57:17 +08:00
|
|
|
maat_state_free(state);
|
|
|
|
|
state = NULL;
|
2022-12-05 23:21:18 +08:00
|
|
|
maat_free(maat_instance);
|
|
|
|
|
}
|
2023-03-16 15:16:42 +08:00
|
|
|
|
2022-12-05 23:21:18 +08:00
|
|
|
int main(int argc, char ** argv)
|
|
|
|
|
{
|
|
|
|
|
int ret=0;
|
|
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
2023-02-09 22:13:15 +08:00
|
|
|
g_logger = log_handle_create("./input_mode_gtest.log", 0);
|
|
|
|
|
|
2023-02-21 11:27:18 +08:00
|
|
|
char json_iris_path[NAME_MAX] = {0};
|
2023-02-09 22:13:15 +08:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-05 23:21:18 +08:00
|
|
|
ret=RUN_ALL_TESTS();
|
2023-02-09 22:13:15 +08:00
|
|
|
|
|
|
|
|
log_handle_destroy(g_logger);
|
2022-12-05 23:21:18 +08:00
|
|
|
return ret;
|
2023-02-03 17:28:14 +08:00
|
|
|
}
|