This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
tango-maat/test/maat_input_mode_gtest.cpp

158 lines
5.4 KiB
C++
Raw Normal View History

#include "maat.h"
2022-12-05 23:21:18 +08:00
#include "maat_utils.h"
#include "maat_core.h"
2022-12-05 23:21:18 +08:00
#include "json2iris.h"
2024-04-03 16:47:30 +08:00
#include "test_utils.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"
2023-07-06 18:58:15 +08:00
#include "maat_redis_monitor.h"
2022-12-05 23:21:18 +08:00
#include <gtest/gtest.h>
#define MODULE_INPUT_MODE_GTEST module_name_str("maat.input_mode_gtest")
const char *g_table_info_path = "./table_info.json";
const char *g_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) {
2024-04-03 16:47:30 +08:00
char iris_idx_path[PATH_MAX] = {0};
char json_iris_path[PATH_MAX] = {0};
2024-04-03 16:47:30 +08:00
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";
2023-02-09 22:13:15 +08:00
2024-04-03 16:47:30 +08:00
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);
2023-02-09 22:13:15 +08:00
2024-04-03 16:47:30 +08:00
int ret = write_json_to_iris(g_json_filename, iris_idx_path,
sizeof(iris_idx_path), g_logger);
assert(ret == 0);
2022-12-05 23:21:18 +08:00
struct maat_options *opts = maat_options_new();
maat_options_set_json_file(opts, json_path);
2024-03-15 18:17:36 +08:00
maat_options_set_stat_on(opts);
maat_options_set_perf_on(opts);
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_inst = maat_new(opts, g_table_info_path);
2023-06-16 15:59:30 +08:00
EXPECT_TRUE(maat_inst != NULL);
2024-04-03 16:47:30 +08:00
2023-06-16 15:59:30 +08:00
int table_id = maat_get_table_id(maat_inst, table_name);
struct maat_state *state = maat_state_new(maat_inst, thread_id);
2024-04-03 16:47:30 +08:00
ret = maat_scan_string(maat_inst, table_id, scan_data, strlen(scan_data),
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);
2023-06-16 15:59:30 +08:00
maat_free(maat_inst);
2022-12-05 23:21:18 +08:00
}
2022-12-05 23:21:18 +08:00
TEST(iris_mode, maat_scan_string) {
char json_iris_path[512] = {0};
2024-04-03 16:47:30 +08:00
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";
2022-12-05 23:21:18 +08:00
2024-04-03 16:47:30 +08:00
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);
2022-12-05 23:21:18 +08:00
2024-04-03 16:47:30 +08:00
int ret = write_json_to_iris(g_json_filename, iris_idx_path,
sizeof(iris_idx_path), g_logger);
assert(ret == 0);
2022-12-05 23:21:18 +08:00
struct maat_options *opts = maat_options_new();
2024-03-15 18:17:36 +08:00
maat_options_set_stat_on(opts);
maat_options_set_perf_on(opts);
2024-04-03 16:47:30 +08:00
maat_options_set_iris(opts, iris_idx_path, iris_idx_path);
2023-03-28 15:41:24 +08:00
maat_options_set_logger(opts, "./maat_input_mode_gtest.log", LOG_LEVEL_INFO);
2023-11-24 15:36:27 +08:00
struct maat *maat_inst = maat_new(opts, g_table_info_path);
2023-06-16 15:59:30 +08:00
EXPECT_TRUE(maat_inst != NULL);
2022-12-05 23:21:18 +08:00
2023-06-16 15:59:30 +08:00
int table_id = maat_get_table_id(maat_inst, table_name);
struct maat_state *state = maat_state_new(maat_inst, thread_id);
2024-04-03 16:47:30 +08:00
ret = maat_scan_string(maat_inst, table_id, scan_data, strlen(scan_data),
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);
2023-06-16 15:59:30 +08:00
maat_free(maat_inst);
2022-12-05 23:21:18 +08:00
}
2023-02-09 22:13:15 +08:00
2022-12-05 23:21:18 +08:00
TEST(redis_mode, maat_scan_string) {
2024-04-03 16:47:30 +08:00
char redis_ip[32] = "127.0.0.1";
2022-12-05 23:21:18 +08:00
int redis_port = 6379;
int redis_db = 0;
2024-04-03 16:47:30 +08:00
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";
2022-12-05 23:21:18 +08:00
2024-04-03 16:47:30 +08:00
int ret = write_json_to_redis(g_json_filename, redis_ip, redis_port,
redis_db, g_logger);
assert(ret == 0);
2022-12-05 23:21:18 +08:00
struct maat_options *opts = maat_options_new();
2024-03-15 18:17:36 +08:00
maat_options_set_stat_on(opts);
maat_options_set_perf_on(opts);
2023-01-06 18:54:59 +08:00
maat_options_set_redis(opts, redis_ip, redis_port, redis_db);
maat_options_set_logger(opts, "./maat_input_mode_gtest.log",
LOG_LEVEL_INFO);
2022-12-05 23:21:18 +08:00
struct maat *maat_inst = maat_new(opts, g_table_info_path);
2024-04-03 16:47:30 +08:00
assert(maat_inst != NULL);
2023-06-16 15:59:30 +08:00
struct maat_state *state = maat_state_new(maat_inst, thread_id);
2024-04-03 16:47:30 +08:00
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);
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);
2023-06-16 15:59:30 +08:00
maat_free(maat_inst);
2022-12-05 23:21:18 +08:00
}
2022-12-05 23:21:18 +08:00
int main(int argc, char ** argv)
{
int ret=0;
::testing::InitGoogleTest(&argc, argv);
2023-11-24 15:36:27 +08:00
g_logger = log_handle_create("./maat_input_mode_gtest.log", 0);
2023-02-09 22:13:15 +08:00
char json_iris_path[NAME_MAX] = {0};
snprintf(json_iris_path, sizeof(json_iris_path),
"./%s_iris_tmp", g_json_filename);
2023-02-09 22:13:15 +08:00
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-08-09 19:22:09 +08:00
}