summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzhengchao <[email protected]>2022-08-23 18:32:03 +0800
committerzhengchao <[email protected]>2022-08-23 18:32:03 +0800
commit10118ffed370af7310a537392e680ededa093ac9 (patch)
treeca2467cf1dfd1de17031d1f3caddac123840b0b7
parentddd36c29697ad123e10a0e5c26d0c16bee605c06 (diff)
Add unit test cases for Boolean Expression Plugin (bool_plug) table.
-rw-r--r--inc/Maat_rule.h6
-rw-r--r--src/entry/Maat_api.cpp4
-rw-r--r--src/entry/Maat_command.cpp41
-rw-r--r--src/entry/Maat_rule.cpp84
-rw-r--r--src/entry/Maat_table_runtime.cpp50
-rw-r--r--src/entry/Maat_table_schema.cpp2
-rw-r--r--src/inc_internal/Maat_table_runtime.h2
-rw-r--r--test/maat_json.json11
-rw-r--r--test/table_info.conf3
-rw-r--r--test/test_maatframe.cpp174
10 files changed, 273 insertions, 104 deletions
diff --git a/inc/Maat_rule.h b/inc/Maat_rule.h
index a0d3c1e..0a1c096 100644
--- a/inc/Maat_rule.h
+++ b/inc/Maat_rule.h
@@ -331,6 +331,12 @@ int Maat_fqdn_plugin_EX_register(Maat_feather_t feather, int table_id,
//Return order: Longger suffix first, then fqdn with bigger index first.
int Maat_fqdn_plugin_get_EX_data(Maat_feather_t feather, int table_id, const char* fqdn, MAAT_PLUGIN_EX_DATA* ex_data_array, size_t n_ex_data);
+int Maat_bool_plugin_EX_register(Maat_feather_t feather, int table_id,
+ Maat_plugin_EX_new_func_t *new_func,
+ Maat_plugin_EX_free_func_t *free_func,
+ Maat_plugin_EX_dup_func_t *dup_func,
+ long argl, void *argp);
+int Maat_bool_plugin_get_EX_data(Maat_feather_t feather, int table_id, unsigned long long item_ids[], size_t n_item, MAAT_PLUGIN_EX_DATA* ex_data_array, size_t n_ex_data);
enum MAAT_RULE_OPT
{
diff --git a/src/entry/Maat_api.cpp b/src/entry/Maat_api.cpp
index d113ba6..970283a 100644
--- a/src/entry/Maat_api.cpp
+++ b/src/entry/Maat_api.cpp
@@ -1365,7 +1365,7 @@ int Maat_fqdn_plugin_get_EX_data(Maat_feather_t feather, int table_id, const cha
return n_get;
}
-int Maat_bool_plugin_get_EX_data(Maat_feather_t feather, int table_id, unsigned long long item_id[], size_t n_item, MAAT_PLUGIN_EX_DATA* ex_data_array, size_t n_ex_data)
+int Maat_bool_plugin_get_EX_data(Maat_feather_t feather, int table_id, unsigned long long item_ids[], size_t n_item, MAAT_PLUGIN_EX_DATA* ex_data_array, size_t n_ex_data)
{
struct _Maat_feather_t *_feather=(_Maat_feather_t*)feather;
struct Maat_table_schema *table_schema=NULL;
@@ -1388,7 +1388,7 @@ int Maat_bool_plugin_get_EX_data(Maat_feather_t feather, int table_id, unsigned
{
return -1;
}
- n_get=Maat_table_runtime_bool_plugin_get_N_ex_data(table_rt, table_schema, item_id, n_item, ex_data_array, n_ex_data);
+ n_get=Maat_table_runtime_bool_plugin_get_N_ex_data(table_rt, table_schema, item_ids, n_item, ex_data_array, n_ex_data);
if(_feather->perf_on==1)
{
diff --git a/src/entry/Maat_command.cpp b/src/entry/Maat_command.cpp
index 724fc35..ef52b3c 100644
--- a/src/entry/Maat_command.cpp
+++ b/src/entry/Maat_command.cpp
@@ -183,7 +183,7 @@ enum MAAT_TABLE_TYPE type_region2table(const struct Maat_region_t* p)
}
return ret;
}
-int get_valid_flag_offset(const char* line, enum MAAT_TABLE_TYPE type,int valid_column_seq)
+int get_valid_flag_offset(const char* line, enum MAAT_TABLE_TYPE type, int valid_column_seq)
{
size_t offset=0, len=0;
unsigned int column_seq=0, ret=0;
@@ -204,6 +204,7 @@ int get_valid_flag_offset(const char* line, enum MAAT_TABLE_TYPE type,int valid_
case TABLE_TYPE_PLUGIN:
case TABLE_TYPE_IP_PLUGIN:
case TABLE_TYPE_FQDN_PLUGIN:
+ case TABLE_TYPE_BOOL_PLUGIN:
if(valid_column_seq<0)
{
return -1;
@@ -2019,10 +2020,10 @@ int Maat_cmd_set_lines(Maat_feather_t feather,const struct Maat_cmd_line** line_
valid_flag_column=Maat_table_schema_get_valid_flag_column(p_table);
if(valid_flag_column<0)
{
- MESA_handle_runtime_log(_feather->logger,RLOG_LV_FATAL,maat_command
- ,"Command set line id %d failed: table %s is not a plugin or ip_plugin table."
- , line_rule[i]->rule_id
- , line_rule[i]->table_name);
+ MESA_handle_runtime_log(_feather->logger, RLOG_LV_FATAL, maat_command,
+ "Command set line id %d failed: table %s is not a plugin or ip_plugin table.",
+ line_rule[i]->rule_id,
+ line_rule[i]->table_name);
ret=-1;
goto error_out;
@@ -2030,15 +2031,15 @@ int Maat_cmd_set_lines(Maat_feather_t feather,const struct Maat_cmd_line** line_
if(op==MAAT_OP_ADD)
{
- ret=get_valid_flag_offset(line_rule[i]->table_line
- , p_table->table_type
- , valid_flag_column);
+ ret=get_valid_flag_offset(line_rule[i]->table_line,
+ p_table->table_type,
+ valid_flag_column);
if(ret<0||
(op==MAAT_OP_ADD&&line_rule[i]->table_line[ret]!='1'))
{
- MESA_handle_runtime_log(_feather->logger,RLOG_LV_FATAL,maat_command
- ,"Command set line %s %d failed: illegal valid flag."
- , line_rule[i]->table_name, line_rule[i]->rule_id);
+ MESA_handle_runtime_log(_feather->logger, RLOG_LV_FATAL, maat_command,
+ "Command set line %s %d failed: illegal valid flag.",
+ line_rule[i]->table_name, line_rule[i]->rule_id);
ret=-1;
goto error_out;
}
@@ -2058,18 +2059,18 @@ int Maat_cmd_set_lines(Maat_feather_t feather,const struct Maat_cmd_line** line_
p_foreign=find_Nth_column(line_rule[i]->table_line, plugin_desc->foreign_columns[j], &foreign_key_size);
if(p_foreign==NULL)
{
- MESA_handle_runtime_log(_feather->logger,RLOG_LV_FATAL, maat_command
- , "Command set line %s %d failed: No %dth column."
- , line_rule[i]->table_name, line_rule[i]->rule_id
- , plugin_desc->foreign_columns[j]);
+ MESA_handle_runtime_log(_feather->logger,RLOG_LV_FATAL, maat_command,
+ "Command set line %s %d failed: No %dth column.",
+ line_rule[i]->table_name, line_rule[i]->rule_id,
+ plugin_desc->foreign_columns[j]);
ret=-1;
goto error_out;
}
if(0!=strncmp(p_foreign, foreign_source_prefix, strlen(foreign_source_prefix)))
{
- MESA_handle_runtime_log(_feather->logger,RLOG_LV_FATAL,maat_redis_monitor
- ,"Command set line %s %d failed: Source prefix %s is mandatory."
- , line_rule[i]->table_name, line_rule[i]->rule_id, foreign_source_prefix);
+ MESA_handle_runtime_log(_feather->logger, RLOG_LV_FATAL, maat_redis_monitor,
+ "Command set line %s %d failed: Source prefix %s is mandatory.",
+ line_rule[i]->table_name, line_rule[i]->rule_id, foreign_source_prefix);
ret=-1;
goto error_out;
}
@@ -2135,7 +2136,7 @@ int Maat_cmd_set_file(Maat_feather_t feather,const char* key, const char* value,
reply= (redisReply *)redisCommandArgv(ctx, sizeof(arg_vec) / sizeof(arg_vec[0]), arg_vec, len_vec);
break;
case MAAT_OP_DEL:
- reply=_wrap_redisCommand(ctx,"EXPIRE %s %d", key, MAAT_REDIS_SYNC_TIME);
+ reply=_wrap_redisCommand(ctx, "EXPIRE %s %d", key, MAAT_REDIS_SYNC_TIME);
break;
default:
return -1;
@@ -2164,7 +2165,7 @@ long long Maat_cmd_incrby(Maat_feather_t feather,const char* key, int increment)
{
return -1;
}
- data_reply=_wrap_redisCommand(write_ctx,"INCRBY %s %d", key, increment);
+ data_reply=_wrap_redisCommand(write_ctx, "INCRBY %s %d", key, increment);
if(data_reply->type==REDIS_REPLY_INTEGER)
{
result=data_reply->integer;
diff --git a/src/entry/Maat_rule.cpp b/src/entry/Maat_rule.cpp
index 65f4152..fd568a9 100644
--- a/src/entry/Maat_rule.cpp
+++ b/src/entry/Maat_rule.cpp
@@ -2198,71 +2198,31 @@ error_out:
digest_rule=NULL;
return;
}
-void update_plugin_table(struct Maat_table_schema* table_schema, const char* row, Maat_scanner* scanner, const struct rule_tag* tags, int n_tags, void* logger)
-{
- int ret=1, matched_tag=1;
- struct plugin_table_schema* plugin_desc=&(table_schema->plugin);
- struct Maat_table_runtime* table_rt=Maat_table_runtime_get(scanner->table_rt_mgr, table_schema->table_id);
- char* copy=NULL;
- size_t accept_tag_offset=0, accept_tag_len=0;
- if(plugin_desc->rule_tag_column>0&&n_tags>0)
- {
- ret=Maat_helper_read_column(row, plugin_desc->rule_tag_column, &accept_tag_offset, &accept_tag_len);
- if(ret<0)
- {
- MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_module ,
- "update error, could not locate tag in column %d of plugin table_schema %s:%s",
- plugin_desc->rule_tag_column,
- table_schema->table_name[table_schema->updating_name],
- row);
- table_schema->udpate_err_cnt++;
- return;
- }
- if(accept_tag_len>2)
- {
- copy=ALLOC(char, accept_tag_len+1);
- memcpy(copy, row+accept_tag_offset, accept_tag_len);
- matched_tag=compare_accept_tag(copy, tags, n_tags);
- if(matched_tag<0)
- {
- MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_module ,
- "update error,invalid tag format of plugin table_schema %s:%s"
- ,table_schema->table_name[table_schema->updating_name],row);
- table_schema->udpate_err_cnt++;
- }
- if(matched_tag==0)
- {
- table_schema->unmatch_tag_cnt++;
- }
- free(copy);
- copy=NULL;
- }
- if(!matched_tag)
- {
- return;
- }
- }
- Maat_table_runtime_plugin_new_row(table_rt, table_schema, row, logger);
-}
-void update_xx_plugin_table(struct Maat_table_schema* table_schema, const char* table_row, Maat_scanner* scanner, const struct rule_tag* tags, int n_tags, void* logger)
+void update_generic_plugin_table(struct Maat_table_schema* table_schema, const char* table_row, Maat_scanner* scanner, const struct rule_tag* tags, int n_tags, void* logger)
{
int ret=1, matched_tag=1;
struct Maat_table_runtime* table_rt=Maat_table_runtime_get(scanner->table_rt_mgr, table_schema->table_id);
char* copy=NULL;
size_t accept_tag_offset=0, accept_tag_len=0;
int rule_tag_column=-1;
- if(table_schema->table_type==TABLE_TYPE_IP_PLUGIN)
- {
- rule_tag_column=table_schema->ip_plugin.rule_tag_column;
- }
- else if(table_schema->table_type==TABLE_TYPE_FQDN_PLUGIN)
- {
- rule_tag_column=table_schema->fqdn_plugin.rule_tag_column;
- }
- else
+
+ switch(table_schema->table_type)
{
- assert(0);
- return;
+ case TABLE_TYPE_PLUGIN:
+ rule_tag_column=table_schema->plugin.rule_tag_column;
+ break;
+ case TABLE_TYPE_IP_PLUGIN:
+ rule_tag_column=table_schema->ip_plugin.rule_tag_column;
+ break;
+ case TABLE_TYPE_FQDN_PLUGIN:
+ rule_tag_column=table_schema->fqdn_plugin.rule_tag_column;
+ break;
+ case TABLE_TYPE_BOOL_PLUGIN:
+ rule_tag_column=table_schema->bool_plugin.rule_tag_column;
+ break;
+ default:
+ assert(0);
+ break;
}
if(rule_tag_column>0&&n_tags>0)
{
@@ -2303,6 +2263,9 @@ void update_xx_plugin_table(struct Maat_table_schema* table_schema, const char*
}
switch(table_schema->table_type)
{
+ case TABLE_TYPE_PLUGIN:
+ Maat_table_runtime_plugin_new_row(table_rt, table_schema, table_row, logger);
+ break;
case TABLE_TYPE_IP_PLUGIN:
Maat_table_runtime_ip_plugin_new_row(table_rt, table_schema, table_row, logger);
break;
@@ -2553,11 +2516,10 @@ int maat_update_cb(const char* table_name,const char* line,void *u_para)
update_group2group_rule(p_table, line, scanner, feather->table_mgr, feather->logger);
break;
case TABLE_TYPE_PLUGIN:
- update_plugin_table(p_table, line, scanner, feather->accept_tags, feather->n_tags, feather->logger);
- break;
case TABLE_TYPE_IP_PLUGIN:
case TABLE_TYPE_FQDN_PLUGIN:
- update_xx_plugin_table(p_table, line, scanner, feather->accept_tags, feather->n_tags, feather->logger);
+ case TABLE_TYPE_BOOL_PLUGIN:
+ update_generic_plugin_table(p_table, line, scanner, feather->accept_tags, feather->n_tags, feather->logger);
break;
default:
break;
diff --git a/src/entry/Maat_table_runtime.cpp b/src/entry/Maat_table_runtime.cpp
index 13a44fa..feb7bc2 100644
--- a/src/entry/Maat_table_runtime.cpp
+++ b/src/entry/Maat_table_runtime.cpp
@@ -63,6 +63,33 @@ void ip_rule_free(struct ip_rule* p)
free(p);
return;
}
+static int cmp_ull_p(const void *p1, const void *p2)
+{
+ if(* (unsigned long long*) p1 > * (unsigned long long*) p2)
+ {
+ return 1;
+ }
+ else if(* (unsigned long long*) p1 < * (unsigned long long*) p2)
+ {
+ return -1;
+ }
+ else
+ {
+ return 0;
+ }
+}
+size_t ull_dedup(unsigned long long item_ids[], size_t n_item)
+{
+ qsort(item_ids, n_item, sizeof(unsigned long long), cmp_ull_p);
+
+ size_t J=0;
+ for(size_t i=1; i<n_item; i++)
+ {
+ if(item_ids[i]!=item_ids[J]) item_ids[++J]=item_ids[i];
+ }
+ return J+1;
+}
+
struct Maat_table_runtime_manager
{
struct Maat_table_runtime** table_rt;
@@ -127,16 +154,18 @@ void fqdn_rule_free(struct FQDN_rule *fqdn_rule)
free(fqdn_rule);
return;
}
-struct bool_expr *bool_expr_new(unsigned int id, unsigned long long item_id[], size_t n_item)
+struct bool_expr *bool_expr_new(unsigned int id, unsigned long long item_ids[], size_t n_item)
{
struct bool_expr *expr=ALLOC(struct bool_expr, 1);
expr->expr_id=id;
size_t i=0;
+ n_item=ull_dedup(item_ids, n_item);
for(i=0; i<n_item; i++)
{
- expr->items[i].item_id=item_id[i];
+ expr->items[i].item_id=item_ids[i];
expr->items[i].not_flag=0;
}
+ expr->item_num=n_item;
return expr;
}
void bool_expr_free(struct bool_expr *expr)
@@ -656,7 +685,7 @@ void Maat_table_runtime_bool_plugin_new_row(struct Maat_table_runtime* table_rt,
if(ret<0)
{
MESA_handle_runtime_log(logger, RLOG_LV_FATAL, maat_module,
- "fqdn_plugin EX data process error: cannot find bool_expr column %d of %s",
+ "bool_plugin EX data process error: cannot find bool_expr column %d of %s",
bool_plugin_schema->bool_expr_column, row);
return;
}
@@ -671,10 +700,10 @@ void Maat_table_runtime_bool_plugin_new_row(struct Maat_table_runtime* table_rt,
break;
ret=sscanf(sub_token, "%llu", item_id+n_item);
n_item++;
- if(ret!=1||n_item==MAX_ITEMS_PER_BOOL_EXPR)
+ if(ret!=1||n_item>MAX_ITEMS_PER_BOOL_EXPR)
{
MESA_handle_runtime_log(logger, RLOG_LV_FATAL, maat_module,
- "fqdn_plugin EX data process error: invalid format of bool_expr column %d of %s",
+ "bool_plugin EX data process error: invalid format of bool_expr column %d of %s",
bool_plugin_schema->bool_expr_column, row);
return;
}
@@ -682,7 +711,6 @@ void Maat_table_runtime_bool_plugin_new_row(struct Maat_table_runtime* table_rt,
if(atoi(row+is_valid_offset)==1)//add
{
expr=bool_expr_new((unsigned int)atoi(row+row_id_offset), item_id, n_item);
- if(expr==NULL)
ret=EX_data_rt_row2EX_data(bool_plugin_rt->ex_data_rt, row, row+row_id_offset, row_id_len, expr, logger);
if(ret<0)
{
@@ -743,10 +771,10 @@ int Maat_table_runtime_bool_plugin_commit_update(struct Maat_table_runtime* tabl
return ret;
}
-int Maat_table_runtime_bool_plugin_get_N_ex_data(struct Maat_table_runtime* table_rt, struct Maat_table_schema* table_schema, unsigned long long item_id[], size_t n_item, MAAT_PLUGIN_EX_DATA* ex_data_array, size_t size)
+int Maat_table_runtime_bool_plugin_get_N_ex_data(struct Maat_table_runtime* table_rt, struct Maat_table_schema* table_schema, unsigned long long item_ids[], size_t n_item, MAAT_PLUGIN_EX_DATA* ex_data_array, size_t size)
{
struct bool_expr_match results[size];
- int n_result=0, i=0;
+ int n_result=0;
if(table_rt->table_type!=TABLE_TYPE_BOOL_PLUGIN)
{
return -1;
@@ -755,8 +783,10 @@ int Maat_table_runtime_bool_plugin_get_N_ex_data(struct Maat_table_runtime* tabl
{
return 0;
}
- n_result=bool_matcher_match(table_rt->bool_plugin.matcher, item_id, n_item, results, size);
- for(i=0; i<n_result; i++)
+ n_item=ull_dedup(item_ids, n_item);
+
+ n_result=bool_matcher_match(table_rt->bool_plugin.matcher, item_ids, n_item, results, size);
+ for(int i=0; i<n_result; i++)
{
ex_data_array[i]=EX_data_rt_get_EX_data_by_container(table_rt->bool_plugin.ex_data_rt, (struct EX_data_container *)results[i].user_tag);
}
diff --git a/src/entry/Maat_table_schema.cpp b/src/entry/Maat_table_schema.cpp
index 0b68e29..aa51285 100644
--- a/src/entry/Maat_table_schema.cpp
+++ b/src/entry/Maat_table_schema.cpp
@@ -516,7 +516,7 @@ int read_bool_plugin_table_schema(const char* line, struct Maat_table_schema* p)
cJSON_Delete(json);
free(copy_line);
- if(read_cnt<4)
+ if(read_cnt<3)
{
return -1;
}
diff --git a/src/inc_internal/Maat_table_runtime.h b/src/inc_internal/Maat_table_runtime.h
index 4eca371..4568fed 100644
--- a/src/inc_internal/Maat_table_runtime.h
+++ b/src/inc_internal/Maat_table_runtime.h
@@ -107,6 +107,6 @@ int Maat_table_runtime_fqdn_plugin_get_N_ex_data(struct Maat_table_runtime* tabl
void Maat_table_runtime_bool_plugin_new_row(struct Maat_table_runtime* table_rt, struct Maat_table_schema* table_schema, const char* row, void *logger);
int Maat_table_runtime_bool_plugin_commit_update(struct Maat_table_runtime* table_rt);
-int Maat_table_runtime_bool_plugin_get_N_ex_data(struct Maat_table_runtime* table_rt, struct Maat_table_schema* table_schema, unsigned long long item_id[], size_t n_item, MAAT_PLUGIN_EX_DATA* ex_data_array, size_t size);
+int Maat_table_runtime_bool_plugin_get_N_ex_data(struct Maat_table_runtime* table_rt, struct Maat_table_schema* table_schema, unsigned long long item_ids[], size_t n_item, MAAT_PLUGIN_EX_DATA* ex_data_array, size_t size);
diff --git a/test/maat_json.json b/test/maat_json.json
index 6ae6632..43bc420 100644
--- a/test/maat_json.json
+++ b/test/maat_json.json
@@ -2373,6 +2373,17 @@
"204\t0\tr3---sn-i3belne6.example2.com\tcatid=3\t1",
"205\t0\tr3---sn-i3belne6.example2.com\tcatid=3\t1"
]
+ },
+ {
+ "table_name":"TEST_BOOL_PLUGIN_WITH_EXDATA",
+ "table_content": [
+ "301\t1&2&1000\ttunnel1\t1",
+ "302\t101&102\ttunnel2\t1",
+ "303\t102\ttunnel3\t1",
+ "304\t101\ttunnel4\t1",
+ "305\t0&1&2&3&4&5&6&7\ttunnel5\t1",
+ "306\t101&101\tinvalid\t1"
+ ]
}
]
}
diff --git a/test/table_info.conf b/test/table_info.conf
index d7f5553..59d56dd 100644
--- a/test/table_info.conf
+++ b/test/table_info.conf
@@ -61,4 +61,5 @@
38 APP_ID intval --
39 EMPTY_KEYWORD expr UTF8 UTF8 yes 0
40 EMPTY_INTERGER intval UTF8 UTF8 yes 0
-10 EMPTY_SIMILAR similar -- \ No newline at end of file
+10 EMPTY_SIMILAR similar --
+41 TEST_BOOL_PLUGIN_WITH_EXDATA bool_plugin {"row_id":1,"bool_expr":2,"valid":4} -- \ No newline at end of file
diff --git a/test/test_maatframe.cpp b/test/test_maatframe.cpp
index 35a2c59..6c49e72 100644
--- a/test/test_maatframe.cpp
+++ b/test/test_maatframe.cpp
@@ -279,7 +279,7 @@ void ip_plugin_EX_dup_cb(int table_id, MAAT_PLUGIN_EX_DATA *to, MAAT_PLUGIN_EX_D
*to=u;
}
-TEST(IP_Plugin_Table, EX_DATA)
+TEST(IPPlugin, EX_DATA)
{
int ip_plugin_ex_data_counter=0, i=0;
@@ -324,7 +324,7 @@ TEST(IP_Plugin_Table, EX_DATA)
}
#define FQDN_PLUGIN_EX_DATA
-struct perf_fqdn_plugin_ud
+struct fqdn_plugin_ud
{
int rule_id;
int catid;
@@ -334,7 +334,7 @@ void fqdn_plugin_EX_new_cb(int table_id, const char* key, const char* table_line
{
int *counter=(int *)argp, ret=0;
size_t column_offset=0, column_len=0;
- struct perf_fqdn_plugin_ud* ud=(struct perf_fqdn_plugin_ud*)calloc(sizeof(struct perf_fqdn_plugin_ud), 1);
+ struct fqdn_plugin_ud* ud=(struct fqdn_plugin_ud*)calloc(sizeof(struct fqdn_plugin_ud), 1);
ret=Maat_helper_read_column(table_line, 1, &column_offset, &column_len);
EXPECT_EQ(ret, 0);
ud->rule_id=atoi(table_line+column_offset);
@@ -348,7 +348,7 @@ void fqdn_plugin_EX_new_cb(int table_id, const char* key, const char* table_line
}
void fqdn_plugin_EX_free_cb(int table_id, MAAT_PLUGIN_EX_DATA* ad, long argl, void *argp)
{
- struct perf_fqdn_plugin_ud* u=(struct perf_fqdn_plugin_ud*)(*ad);
+ struct fqdn_plugin_ud* u=(struct fqdn_plugin_ud*)(*ad);
if ((__sync_sub_and_fetch(&u->ref_cnt, 1) == 0))
{
free(u);
@@ -357,11 +357,11 @@ void fqdn_plugin_EX_free_cb(int table_id, MAAT_PLUGIN_EX_DATA* ad, long argl, vo
}
void fqdn_plugin_EX_dup_cb(int table_id, MAAT_PLUGIN_EX_DATA *to, MAAT_PLUGIN_EX_DATA *from, long argl, void *argp)
{
- struct perf_fqdn_plugin_ud* u=(struct perf_fqdn_plugin_ud*)(*from);
+ struct fqdn_plugin_ud* u=(struct fqdn_plugin_ud*)(*from);
__sync_add_and_fetch(&(u->ref_cnt), 1);
*to=u;
}
-TEST(FQDN_Plugin_Table, EX_DATA)
+TEST(FQDNPlugin, EX_DATA)
{
int fqdn_plugin_ex_data_counter=0, i=0;
@@ -377,7 +377,7 @@ TEST(FQDN_Plugin_Table, EX_DATA)
ASSERT_TRUE(ret>=0);
EXPECT_EQ(fqdn_plugin_ex_data_counter, 5);
- struct perf_fqdn_plugin_ud* result[4];
+ struct fqdn_plugin_ud* result[4];
ret=Maat_fqdn_plugin_get_EX_data(g_feather, table_id, "www.example1.com", (void**)result, 4);
ASSERT_EQ(ret, 2);
@@ -402,7 +402,96 @@ TEST(FQDN_Plugin_Table, EX_DATA)
}
}
+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, MAAT_PLUGIN_EX_DATA* ad, long argl, void *argp)
+{
+ int *counter=(int *)argp, ret=0;
+ size_t column_offset=0, column_len=0;
+ struct bool_plugin_ud* ud=(struct bool_plugin_ud*)calloc(sizeof(struct bool_plugin_ud), 1);
+ ret=Maat_helper_read_column(table_line, 1, &column_offset, &column_len);
+ EXPECT_EQ(ret, 0);
+ ud->id=atoi(table_line+column_offset);
+ ret=Maat_helper_read_column(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)++;
+ return;
+}
+void bool_plugin_EX_free_cb(int table_id, MAAT_PLUGIN_EX_DATA* 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, MAAT_PLUGIN_EX_DATA *to, MAAT_PLUGIN_EX_DATA *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;
+}
+TEST(BoolPlugin, EX_DATA)
+{
+ int ex_data_counter=0, i=0;
+ const char* table_name="TEST_BOOL_PLUGIN_WITH_EXDATA";
+ int table_id=0, ret=0;
+ table_id=Maat_table_register(g_feather, table_name);
+ ASSERT_GT(table_id, 0);
+ ret=Maat_bool_plugin_EX_register(g_feather, 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_get_EX_data(g_feather, table_id, items_1, 1, (void**)result, 6);
+ EXPECT_EQ(ret, 0);
+ for(i=0; i<ret; i++)
+ {
+ fqdn_plugin_EX_free_cb(0, (void**)&(result[i]), 0, NULL);
+ }
+
+ unsigned long long items_2[]={1, 2, 1000};
+ ret=Maat_bool_plugin_get_EX_data(g_feather, table_id, items_2, 3, (void**)result, 6);
+ EXPECT_EQ(ret, 1);
+ EXPECT_EQ(result[0]->id, 301);
+ for(i=0; i<ret; i++)
+ {
+ fqdn_plugin_EX_free_cb(0, (void**)&(result[i]), 0, NULL);
+ }
+
+ unsigned long long items_3[]={101, 102, 1000};
+ ret=Maat_bool_plugin_get_EX_data(g_feather, table_id, items_3, 3, (void**)result, 6);
+ EXPECT_EQ(ret, 4);
+ for(i=0; i<ret; i++)
+ {
+ fqdn_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_get_EX_data(g_feather, 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++)
+ {
+ fqdn_plugin_EX_free_cb(0, (void**)&(result[i]), 0, NULL);
+ }
+}
TEST(StringScan, Full)
{
@@ -3813,12 +3902,81 @@ TEST_F(MaatCmdTest, UpdateFQDNPlugin)
ret=Maat_cmd_set_lines(feather, p_line+3, TEST_CMD_LINE_NUM-3, MAAT_OP_DEL);
EXPECT_GT(ret, 0);
- usleep(WAIT_FOR_EFFECTIVE_US*5);
+ usleep(WAIT_FOR_EFFECTIVE_US);
ret=Maat_fqdn_plugin_get_EX_data(feather, table_id, "r3---sn-i3belne6.example2.com", (void**)result, 4);
ASSERT_EQ(ret, 0);
return;
}
+TEST_F(MaatCmdTest, UpdateBoolPlugin)
+{
+#define Bool_Plugin_EX_data
+ Maat_feather_t feather=MaatCmdTest::_shared_feather;
+
+ int ex_data_counter=0, i=0;
+ const char* table_name="TEST_BOOL_PLUGIN_WITH_EXDATA";
+ int table_id=0, ret=0;
+
+ const int TEST_CMD_LINE_NUM=6;
+ const struct Maat_cmd_line *p_line[TEST_CMD_LINE_NUM];
+ struct Maat_cmd_line line_rule[TEST_CMD_LINE_NUM];
+ const char* table_line[TEST_CMD_LINE_NUM]={
+ "301\t1&2&1000\ttunnel1\t1",
+ "302\t101&102\ttunnel2\t1",
+ "303\t102\ttunnel3\t1",
+ "304\t101\ttunnel4\t1",
+ "305\t0&1&2&3&4&5&6&7\ttunnel5\t1",
+ "306\t101&101\tinvalid\t1"
+ };
+ table_id=Maat_table_register(feather, table_name);
+ ASSERT_GT(table_id, 0);
+ memset(&line_rule,0,sizeof(line_rule));
+ for(i=0;i<TEST_CMD_LINE_NUM;i++)
+ {
+ line_rule[i].label_id=0;
+ line_rule[i].rule_id=(int)Maat_cmd_incrby(feather,"TEST_PLUG_SEQ", 1);
+ line_rule[i].table_name=table_name;
+ line_rule[i].table_line=table_line[i];
+ line_rule[i].expire_after=0;
+ p_line[i]=line_rule+i;
+ }
+ ret=Maat_cmd_set_lines(feather, p_line,TEST_CMD_LINE_NUM, MAAT_OP_ADD);
+ EXPECT_GT(ret, 0);
+
+ usleep(WAIT_FOR_EFFECTIVE_US);
+
+
+ ret=Maat_bool_plugin_EX_register(feather, 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_3[]={101, 102, 1000};
+ ret=Maat_bool_plugin_get_EX_data(feather, table_id, items_3, 3, (void**)result, 6);
+ EXPECT_EQ(ret, 4);
+ for(i=0; i<ret; i++)
+ {
+ fqdn_plugin_EX_free_cb(0, (void**)&(result[i]), 0, NULL);
+ }
+
+ ret=Maat_cmd_set_lines(feather, p_line+3, TEST_CMD_LINE_NUM-3, MAAT_OP_DEL);
+ EXPECT_GT(ret, 0);
+
+ usleep(WAIT_FOR_EFFECTIVE_US);
+
+ ret=Maat_bool_plugin_get_EX_data(feather, table_id, items_3, 3, (void**)result, 6);
+ EXPECT_EQ(ret, 2);
+ for(i=0; i<ret; i++)
+ {
+ fqdn_plugin_EX_free_cb(0, (void**)&(result[i]), 0, NULL);
+ }
+
+}
#define TEST_GROUP_IN_MASS_COMPILES
TEST_F(MaatCmdTest, GroupInMassCompiles)
{