summaryrefslogtreecommitdiff
path: root/test/test_utils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_utils.cpp')
-rw-r--r--test/test_utils.cpp303
1 files changed, 303 insertions, 0 deletions
diff --git a/test/test_utils.cpp b/test/test_utils.cpp
new file mode 100644
index 0000000..0f5234a
--- /dev/null
+++ b/test/test_utils.cpp
@@ -0,0 +1,303 @@
+#include "test_utils.h"
+#include "maat_redis_monitor.h"
+#include "maat_utils.h"
+#include "maat_table.h"
+#include "maat_rule.h"
+#include "maat_config_monitor.h"
+#include "json2iris.h"
+
+#include <assert.h>
+#include <unistd.h>
+
+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_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);
+ if (ret < 0) {
+ return -1;
+ }
+
+ 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)
+{
+ 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;
+ }
+
+ size_t total_line_cnt = 0;
+ config_monitor_traverse(0, iris_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_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);
+
+ assert(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;
+}
+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);
+
+ return 0;
+}
+
+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)
+{
+ char table_line[1024 * 16] = {0};
+ sprintf(table_line, "%lld\t0\t0\t0\t0\t0\t%s\t%d\t%d\t0.0",
+ compile_id, user_region, clause_num, op);
+
+ struct maat_cmd_line line_rule;
+ line_rule.rule_id = compile_id;
+ line_rule.table_line = table_line;
+ line_rule.table_name = table_name;
+ line_rule.expire_after = expire_after;
+
+ 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)
+
+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)
+{
+ char table_line[128] = {0};
+ sprintf(table_line, "%lld\t%lld\t%d\t%s\t%d\t%d",
+ group_id, compile_id, not_flag, vtable_name, clause_index, op);
+
+ struct maat_cmd_line line_rule;
+ line_rule.rule_id = TO_GROUP2X_KEY(group_id, compile_id, clause_index);
+ line_rule.table_line = table_line;
+ line_rule.table_name = table_name;
+ line_rule.expire_after = expire_after;
+
+ return maat_cmd_set_line(maat_inst, &line_rule);
+}
+
+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)
+{
+ char table_line[128] = {0};
+ sprintf(table_line, "%lld\t%lld\t%s\t%d", group_id, sub_group_id,
+ "null", op);
+
+ struct maat_cmd_line line_rule;
+ line_rule.rule_id = TO_GROUP2X_KEY(group_id, sub_group_id, 0);
+ line_rule.table_line = table_line;
+ line_rule.table_name = table_name;
+ line_rule.expire_after = expire_after;
+
+ return maat_cmd_set_line(maat_inst, &line_rule);
+}
+
+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)
+{
+ char table_line[1024] = {0};
+ int table_id = maat_get_table_id(maat_inst, table_name);
+ if (table_id < 0) {
+ 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);
+
+ 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",
+ item_id, group_id, district, keywords, expr_type,
+ match_method, is_hexbin, op);
+ } else {
+ sprintf(table_line, "%lld\t%lld\t%s\t%d\t%d\t%d\t%d",
+ item_id, group_id, keywords, expr_type,
+ match_method, is_hexbin, op);
+ }
+
+ struct maat_cmd_line line_rule;
+ line_rule.rule_id = item_id;
+ line_rule.table_line = table_line;
+ line_rule.table_name = table_name;
+ line_rule.expire_after = expire_after;
+
+ return maat_cmd_set_line(maat_inst, &line_rule);
+}
+
+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)
+{
+ char table_line[1024] = {0};
+ int table_id = maat_get_table_id(maat_inst, table_name);
+ if (table_id < 0) {
+ 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);
+
+ if (table_type == TABLE_TYPE_INTERVAL_PLUS) {
+ sprintf(table_line, "%lld\t%lld\t%s\t%u\t%u\t%d",
+ item_id, group_id, district, low_boundary, up_boundary, op);
+ } else {
+ sprintf(table_line, "%lld\t%lld\t%u\t%u\t%d",
+ item_id, group_id, low_boundary, up_boundary, op);
+ }
+
+ struct maat_cmd_line line_rule;
+ line_rule.rule_id = item_id;
+ line_rule.table_line = table_line;
+ line_rule.table_name = table_name;
+ line_rule.expire_after = expire_after;
+
+ return maat_cmd_set_line(maat_inst, &line_rule);
+}
+
+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)
+{
+ char table_line[1024] = {0};
+ int table_id = maat_get_table_id(maat_inst, table_name);
+ if (table_id < 0) {
+ return 0;
+ }
+
+ int ip_type = IPV4;
+ if (type == IPv6) {
+ ip_type = IPV6;
+ }
+
+ sprintf(table_line, "%lld\t%lld\t%d\trange\t%s\t%s\t0-65535\t%d",
+ item_id, group_id, ip_type, ip1, ip2, op);
+ struct maat_cmd_line line_rule;
+
+ line_rule.rule_id = item_id;
+ line_rule.table_line = table_line;
+ line_rule.table_name = table_name;
+ line_rule.expire_after = expire_after;
+
+ return maat_cmd_set_line(maat_inst, &line_rule);
+}
+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)
+{
+ char table_line[1024] = {0};
+ int table_id = maat_get_table_id(maat_inst, table_name);
+ if (table_id < 0) {
+ return 0;
+ }
+
+ sprintf(table_line, "%lld\t%lld\t%lld\t%lld\t%d",
+ item_id, group_id, flag, flag_mask, op);
+ struct maat_cmd_line line_rule;
+
+ line_rule.rule_id = item_id;
+ line_rule.table_line = table_line;
+ line_rule.table_name = table_name;
+ line_rule.expire_after = expire_after;
+
+ return maat_cmd_set_line(maat_inst, &line_rule);
+} \ No newline at end of file