fix hit repeated compile_id bug & unify compile+plugin table register API

This commit is contained in:
liuwentan
2023-03-01 13:12:22 +08:00
parent 1566a30002
commit 2c6cca6f56
8 changed files with 236 additions and 142 deletions

View File

@@ -95,15 +95,6 @@ int maat_table_callback_register(struct maat *instance, int table_id,
maat_finish_callback_t *finish, maat_finish_callback_t *finish,
void *u_para); void *u_para);
/* maat compile table API */
int maat_compile_table_ex_schema_register(struct maat *instance, int table_id,
maat_ex_new_func_t *new_func,
maat_ex_free_func_t *free_func,
maat_ex_dup_func_t *dup_func,
long argl, void *argp);
void *maat_compile_table_get_ex_data(struct maat *instance, int compile_table_id,
long long compile_id);
/* maat plugin table API */ /* maat plugin table API */
int maat_plugin_table_ex_schema_register(struct maat *instance, int table_id, int maat_plugin_table_ex_schema_register(struct maat *instance, int table_id,
maat_ex_new_func_t *new_func, maat_ex_new_func_t *new_func,
@@ -112,7 +103,8 @@ int maat_plugin_table_ex_schema_register(struct maat *instance, int table_id,
long argl, void *argp); long argl, void *argp);
/* returned data is duplicated by dup_func of maat_plugin_table_ex_schema_register, /* returned data is duplicated by dup_func of maat_plugin_table_ex_schema_register,
caller is responsible to free the data. */ caller is responsible to free the data. */
void *maat_plugin_table_get_ex_data(struct maat *instance, int table_id, const char *key); void *maat_plugin_table_get_ex_data(struct maat *instance, int table_id,
const char *key, size_t key_len);
int maat_ip_plugin_table_get_ex_data(struct maat *instance, int table_id, int maat_ip_plugin_table_get_ex_data(struct maat *instance, int table_id,
const struct ip_addr *ip, void **ex_data_array, const struct ip_addr *ip, void **ex_data_array,

View File

@@ -42,8 +42,6 @@ int compile_table_set_ex_data_schema(struct compile_schema *compile_schema, int
maat_ex_dup_func_t *dup_func, maat_ex_dup_func_t *dup_func,
long argl, void *argp, long argl, void *argp,
struct log_handle *logger); struct log_handle *logger);
void *compile_table_get_ex_data(struct compile_schema *compile_schema, long long compile_id);
void compile_table_ex_data_iterate(struct compile_schema *compile_schema);
/* compile runtime API */ /* compile runtime API */
void *compile_runtime_new(void *compile_schema, int max_thread_num, void *compile_runtime_new(void *compile_schema, int max_thread_num,
@@ -63,6 +61,13 @@ size_t compile_runtime_get_hit_paths(struct compile_runtime *compile_rt,
struct maat_compile_state *compile_state, struct maat_compile_state *compile_state,
struct maat_hit_path *hit_paths, struct maat_hit_path *hit_paths,
size_t hit_path_index, size_t n_hit_path); size_t hit_path_index, size_t n_hit_path);
void *compile_runtime_get_ex_data(struct compile_runtime *compile_rt,
struct compile_schema *compile_schema,
long long compile_id);
void compile_runtime_ex_data_iterate(struct compile_runtime *compile_rt,
struct compile_schema *compile_schema);
/* group2compile runtime API */ /* group2compile runtime API */
void *group2compile_runtime_new(void *g2c_schema, int max_thread_num, void *group2compile_runtime_new(void *g2c_schema, int max_thread_num,
struct maat_garbage_bin *garbage_bin, struct maat_garbage_bin *garbage_bin,

View File

@@ -64,7 +64,8 @@ size_t plugin_runtime_cached_row_count(void *plugin_runtime);
const char *plugin_runtime_cached_row_get(void *plugin_runtime, size_t index); const char *plugin_runtime_cached_row_get(void *plugin_runtime, size_t index);
void *plugin_runtime_get_ex_data(void *plugin_runtime, void *plugin_schema, const char *key); void *plugin_runtime_get_ex_data(void *plugin_runtime, void *plugin_schema,
const char *key, size_t key_len);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@@ -458,66 +458,38 @@ int maat_table_callback_register(struct maat *maat_instance, int table_id,
return 0; return 0;
} }
int maat_compile_table_ex_schema_register(struct maat *maat_instance, int table_id, int compile_table_ex_schema_register(struct maat *maat_instance, int table_id,
maat_ex_new_func_t *new_func, maat_ex_new_func_t *new_func,
maat_ex_free_func_t *free_func, maat_ex_free_func_t *free_func,
maat_ex_dup_func_t *dup_func, maat_ex_dup_func_t *dup_func,
long argl, void *argp) long argl, void *argp)
{ {
if (NULL == maat_instance || table_id < 0 || table_id >= MAX_TABLE_NUM) { void *schema = table_manager_get_schema(maat_instance->tbl_mgr, table_id);
return -1; assert(schema != NULL);
}
enum table_type table_type = table_manager_get_table_type(maat_instance->tbl_mgr, table_id);
if (table_type != TABLE_TYPE_COMPILE) {
log_error(maat_instance->logger, MODULE_MAAT_API,
"table(tabld_id:%d) is not compile table", table_id);
return -1;
}
void *compile_schema = table_manager_get_schema(maat_instance->tbl_mgr, table_id); int ret = compile_table_set_ex_data_schema((struct compile_schema *)schema,
assert(compile_schema != NULL); table_id, new_func, free_func, dup_func,
argl, argp, maat_instance->logger);
pthread_mutex_lock(&(maat_instance->background_update_mutex));
int ret = compile_table_set_ex_data_schema((struct compile_schema *)compile_schema, table_id,
new_func, free_func, dup_func,
argl, argp, maat_instance->logger);
if (ret < 0) { if (ret < 0) {
pthread_mutex_unlock(&(maat_instance->background_update_mutex));
return -1; return -1;
} }
if (maat_instance->maat_rt != NULL) { if (maat_instance->maat_rt != NULL) {
compile_table_ex_data_iterate((struct compile_schema *)compile_schema); void *runtime = table_manager_get_runtime(maat_instance->tbl_mgr, table_id);
assert(runtime != NULL);
compile_runtime_ex_data_iterate((struct compile_runtime *)runtime,
(struct compile_schema *)schema);
} }
pthread_mutex_unlock(&(maat_instance->background_update_mutex));
return 0; return 0;
} }
void *maat_compile_table_get_ex_data(struct maat *maat_instance, int compile_table_id, int generic_plugin_table_set_ex_schema(struct table_manager *tbl_mgr, int table_id,
long long compile_id) maat_ex_new_func_t *new_func,
maat_ex_free_func_t *free_func,
maat_ex_dup_func_t *dup_func,
long argl, void *argp, struct log_handle *logger)
{ {
struct compile_schema *schema = (struct compile_schema *)table_manager_get_schema(maat_instance->tbl_mgr,
compile_table_id);
enum table_type table_type = table_manager_get_table_type(maat_instance->tbl_mgr, compile_table_id);
assert(table_type == TABLE_TYPE_COMPILE);
return compile_table_get_ex_data(schema, compile_id);
}
int generic_plugin_table_ex_schema_register(struct table_manager *tbl_mgr, int table_id,
maat_ex_new_func_t *new_func,
maat_ex_free_func_t *free_func,
maat_ex_dup_func_t *dup_func,
long argl, void *argp, struct log_handle *logger)
{
if (NULL == tbl_mgr || NULL == new_func || NULL == free_func || NULL == dup_func) {
assert(0);
log_error(logger, MODULE_MAAT_API,
"table(table_id:%d) %s failed: invalid parameter", __FUNCTION__);
return -1;
}
void *schema = table_manager_get_schema(tbl_mgr, table_id); void *schema = table_manager_get_schema(tbl_mgr, table_id);
if (NULL == schema) { if (NULL == schema) {
log_error(logger, MODULE_MAAT_API, log_error(logger, MODULE_MAAT_API,
@@ -574,7 +546,10 @@ int generic_plugin_table_ex_schema_register(struct table_manager *tbl_mgr, int t
dup_func, argl, argp, logger); dup_func, argl, argp, logger);
break; break;
default: default:
break; log_error(logger, MODULE_MAAT_API,
"Error: %s, table(table_id:%d) is not plugin table, can't set ex schema",
__FUNCTION__, table_id);
return -1;
} }
return 0; return 0;
@@ -653,22 +628,16 @@ void generic_plugin_runtime_commit_ex_schema(void *runtime, void *schema, int ta
} }
} }
int maat_plugin_table_ex_schema_register(struct maat *maat_instance, int table_id, int generic_plugin_table_ex_schema_register(struct maat *maat_instance, int table_id,
maat_ex_new_func_t *new_func, maat_ex_new_func_t *new_func,
maat_ex_free_func_t *free_func, maat_ex_free_func_t *free_func,
maat_ex_dup_func_t *dup_func, maat_ex_dup_func_t *dup_func,
long argl, void *argp) long argl, void *argp)
{ {
if (NULL == maat_instance || table_id < 0 || table_id >= MAX_TABLE_NUM) { int ret = generic_plugin_table_set_ex_schema(maat_instance->tbl_mgr, table_id,
return -1; new_func, free_func, dup_func,
} argl, argp, maat_instance->logger);
pthread_mutex_lock(&(maat_instance->background_update_mutex));
int ret = generic_plugin_table_ex_schema_register(maat_instance->tbl_mgr, table_id,
new_func, free_func, dup_func,
argl, argp, maat_instance->logger);
if (ret < 0) { if (ret < 0) {
pthread_mutex_unlock(&(maat_instance->background_update_mutex));
return -1; return -1;
} }
@@ -683,34 +652,80 @@ int maat_plugin_table_ex_schema_register(struct maat *maat_instance, int table_i
valid_column = table_manager_get_valid_column(maat_instance->tbl_mgr, table_id); valid_column = table_manager_get_valid_column(maat_instance->tbl_mgr, table_id);
generic_plugin_runtime_commit_ex_schema(runtime, schema, table_id, table_type, valid_column); generic_plugin_runtime_commit_ex_schema(runtime, schema, table_id, table_type, valid_column);
} }
pthread_mutex_unlock(&(maat_instance->background_update_mutex));
return 0; return 0;
} }
void *maat_plugin_table_get_ex_data(struct maat *maat_instance, int table_id, const char *key) int maat_plugin_table_ex_schema_register(struct maat *maat_instance, int table_id,
maat_ex_new_func_t *new_func,
maat_ex_free_func_t *free_func,
maat_ex_dup_func_t *dup_func,
long argl, void *argp)
{
if (NULL == maat_instance || table_id < 0 || table_id >= MAX_TABLE_NUM) {
return -1;
}
pthread_mutex_lock(&(maat_instance->background_update_mutex));
int ret = -1;
enum table_type table_type = table_manager_get_table_type(maat_instance->tbl_mgr, table_id);
if (TABLE_TYPE_COMPILE == table_type) {
ret = compile_table_ex_schema_register(maat_instance, table_id,
new_func, free_func, dup_func,
argl, argp);
} else {
ret = generic_plugin_table_ex_schema_register(maat_instance, table_id,
new_func, free_func, dup_func,
argl, argp);
}
pthread_mutex_unlock(&(maat_instance->background_update_mutex));
return ret;
}
void *maat_plugin_table_get_ex_data(struct maat *maat_instance, int table_id,
const char *key, size_t key_len)
{ {
if (NULL == maat_instance || table_id < 0 || table_id >= MAX_TABLE_NUM || NULL == key) { if (NULL == maat_instance || table_id < 0 || table_id >= MAX_TABLE_NUM
|| NULL == key || 0 == key_len) {
log_error(maat_instance->logger, MODULE_MAAT_API,
"input parameter is invalid.");
return NULL; return NULL;
} }
struct maat_runtime *maat_rt = maat_instance->maat_rt; struct maat_runtime *maat_rt = maat_instance->maat_rt;
if (NULL == maat_rt) { if (NULL == maat_rt) {
log_error(maat_instance->logger, MODULE_MAAT_API, "maat runtime is NULL");
return NULL; return NULL;
} }
void *plugin_rt = table_manager_get_runtime(maat_rt->ref_tbl_mgr, table_id); void *runtime = table_manager_get_runtime(maat_rt->ref_tbl_mgr, table_id);
if (NULL == plugin_rt) { if (NULL == runtime) {
log_error(maat_instance->logger, MODULE_MAAT_API,
"table(table_id:%d) runtime is NULL", table_id);
return NULL; return NULL;
} }
void *plugin_schema = table_manager_get_schema(maat_rt->ref_tbl_mgr, table_id); void *schema = table_manager_get_schema(maat_rt->ref_tbl_mgr, table_id);
if (NULL == plugin_schema) { if (NULL == schema) {
log_error(maat_instance->logger, MODULE_MAAT_API,
"table(table_id:%d) schema is NULL", table_id);
return NULL; return NULL;
} }
return plugin_runtime_get_ex_data(plugin_rt, plugin_schema, key); void *ret = NULL;
enum table_type table_type = table_manager_get_table_type(maat_instance->tbl_mgr, table_id);
if (TABLE_TYPE_COMPILE == table_type) {
ret = compile_runtime_get_ex_data(runtime, schema, *(long long *)key);
} else if (TABLE_TYPE_PLUGIN == table_type) {
ret = plugin_runtime_get_ex_data(runtime, schema, key, key_len);
} else {
return NULL;
}
return ret;
} }
int maat_ip_plugin_table_get_ex_data(struct maat *maat_instance, int table_id, int maat_ip_plugin_table_get_ex_data(struct maat *maat_instance, int table_id,

View File

@@ -163,9 +163,7 @@ int compile_table_set_ex_data_schema(struct compile_schema *compile_schema,
return -1; return -1;
} }
//compile_schema->ex_schema[idx].table_id = table_id;
compile_schema->ex_schema = ALLOC(struct ex_data_schema, 1); compile_schema->ex_schema = ALLOC(struct ex_data_schema, 1);
compile_schema->ex_schema->argl = argl; compile_schema->ex_schema->argl = argl;
compile_schema->ex_schema->argp = argp; compile_schema->ex_schema->argp = argp;
compile_schema->ex_schema->new_func = new_func; compile_schema->ex_schema->new_func = new_func;
@@ -237,46 +235,6 @@ void compile_runtime_user_data_iterate(struct compile_runtime *compile_rt,
pthread_rwlock_unlock(&compile_rt->rwlock); pthread_rwlock_unlock(&compile_rt->rwlock);
} }
void compile_table_ex_data_iterate(struct compile_schema *compile_schema)
{
if (NULL == compile_schema) {
return;
}
if (NULL == compile_schema->ex_schema) {
return;
}
struct ex_data_schema *ex_schema = compile_schema->ex_schema;
struct compile_runtime *compile_rt = NULL;
compile_rt = (struct compile_runtime *)table_manager_get_runtime(compile_schema->ref_tbl_mgr,
compile_schema->table_id);
compile_runtime_user_data_iterate(compile_rt, rule_ex_data_new_cb, ex_schema, compile_schema->table_id);
}
void *compile_table_get_ex_data(struct compile_schema *compile_schema, long long compile_id)
{
if (NULL == compile_schema) {
return NULL;
}
struct compile_rule *compile_rule = NULL;
struct compile_runtime *compile_rt = NULL;
compile_rt = (struct compile_runtime *)table_manager_get_runtime(compile_schema->ref_tbl_mgr,
compile_schema->table_id);
compile_rule = (struct compile_rule *)compile_runtime_get_user_data(compile_rt, compile_id, 0);
if (NULL == compile_rule) {
return NULL;
}
void *ex_data = NULL;
struct ex_data_schema *ex_schema = compile_schema->ex_schema;
ex_schema->dup_func(compile_schema->table_id, &ex_data, compile_rule->ex_data, ex_schema->argl, ex_schema->argp);
return ex_data;
}
UT_icd ut_literal_id_icd = {sizeof(struct maat_literal_id), NULL, NULL, NULL}; UT_icd ut_literal_id_icd = {sizeof(struct maat_literal_id), NULL, NULL, NULL};
UT_icd ut_clause_id_icd = {sizeof(long long), NULL, NULL, NULL}; UT_icd ut_clause_id_icd = {sizeof(long long), NULL, NULL, NULL};
UT_icd ut_hit_path_icd = {sizeof(struct maat_internal_hit_path), NULL, NULL, NULL}; UT_icd ut_hit_path_icd = {sizeof(struct maat_internal_hit_path), NULL, NULL, NULL};
@@ -997,7 +955,7 @@ maat_compile_bool_matcher_new(struct maat_compile *compile_hash,
#if 0 #if 0
struct maat_literal_id *p = NULL; struct maat_literal_id *p = NULL;
for(p = (struct maat_literal_id *)utarray_front(compile->clause_states[i].literal_ids); p!=NULL; p=(struct maat_literal_id *)utarray_next(compile->clause_states[i].literal_ids,p)) { for(p = (struct maat_literal_id *)utarray_front(compile->clause_states[i].literal_ids); p!=NULL; p=(struct maat_literal_id *)utarray_next(compile->clause_states[i].literal_ids,p)) {
printf("compile_id:%llu, clause_id:%llu, literal{%llu: %d}\n", printf("<before bool_matcher_new> compile_id:%llu, clause_id:%llu, literal{%llu: %d}\n",
compile->compile_id, compile->clause_states[i].clause_id, p->group_id, p->vtable_id); compile->compile_id, compile->clause_states[i].clause_id, p->group_id, p->vtable_id);
} }
#endif #endif
@@ -1063,6 +1021,43 @@ void maat_compile_bool_matcher_free(struct bool_matcher *bm)
bool_matcher_free(bm); bool_matcher_free(bm);
} }
static int maat_compile_has_clause(struct maat_compile *compile,
unsigned long long clause_id)
{
struct maat_clause_state *clause_state = NULL;
for (size_t i = 0; i < MAX_ITEMS_PER_BOOL_EXPR; i++) {
clause_state = compile->clause_states + i;
if (!clause_state->in_use) {
continue;
}
if (clause_state->clause_id == clause_id) {
return 1;
}
}
return 0;
}
static size_t compile_state_if_new_hit_compile(struct maat_compile_state *compile_state,
struct maat_compile *compile)
{
size_t r_in_c_cnt = 0;
int ret = 0;
unsigned long long new_hit_clause_id = 0;
for (size_t i = 0; i < utarray_len(compile_state->this_scan_hit_clauses); i++) {
new_hit_clause_id = *(unsigned long long*)utarray_eltptr(compile_state->this_scan_hit_clauses, i);
ret = maat_compile_has_clause(compile, new_hit_clause_id);
if (ret) {
r_in_c_cnt++;
}
}
return r_in_c_cnt;
}
size_t maat_compile_bool_matcher_match(struct bool_matcher *bm, int is_last_scan, size_t maat_compile_bool_matcher_match(struct bool_matcher *bm, int is_last_scan,
struct maat_compile_state *compile_state, struct maat_compile_state *compile_state,
void **user_data_array, size_t ud_array_size) void **user_data_array, size_t ud_array_size)
@@ -1092,14 +1087,20 @@ size_t maat_compile_bool_matcher_match(struct bool_matcher *bm, int is_last_scan
continue; continue;
} }
size_t n_new_hit_compile = compile_state_if_new_hit_compile(compile_state, compile);
size_t n_this_scan_hit_item = compile_state->this_scan_hit_item_cnt;
if ((compile->not_clause_cnt > 0) && (LAST_SCAN_UNSET == is_last_scan)) { if ((compile->not_clause_cnt > 0) && (LAST_SCAN_UNSET == is_last_scan)) {
compile_state->not_clause_hitted_flag = 1; compile_state->not_clause_hitted_flag = 1;
} }
//TODO: not_clause //TODO: not_clause
if (compile->user_data) { if (compile->user_data) {
user_data_array[ud_result_cnt] = compile->user_data; if (n_new_hit_compile > 0 || 0 == n_this_scan_hit_item) {
ud_result_cnt++; /* compile hit because of new item or
hit a compile that refer a NOT-logic group in previous scan */
user_data_array[ud_result_cnt] = compile->user_data;
ud_result_cnt++;
}
} }
} }
@@ -1461,6 +1462,41 @@ void destroy_compile_rule(struct compile_rule *compile_rule)
FREE(compile_rule); FREE(compile_rule);
} }
void compile_runtime_ex_data_iterate(struct compile_runtime *compile_rt,
struct compile_schema *compile_schema)
{
if (NULL == compile_rt || NULL == compile_schema ||
NULL == compile_schema->ex_schema) {
return;
}
compile_runtime_user_data_iterate(compile_rt, rule_ex_data_new_cb,
compile_schema->ex_schema,
compile_schema->table_id);
}
void *compile_runtime_get_ex_data(struct compile_runtime *compile_rt,
struct compile_schema *compile_schema,
long long compile_id)
{
if (NULL == compile_rt || NULL == compile_schema || compile_id < 0) {
return NULL;
}
struct compile_rule *compile_rule = NULL;
compile_rule = (struct compile_rule *)compile_runtime_get_user_data(compile_rt, compile_id, 0);
if (NULL == compile_rule) {
return NULL;
}
void *ex_data = NULL;
struct ex_data_schema *ex_schema = compile_schema->ex_schema;
ex_schema->dup_func(compile_schema->table_id, &ex_data, compile_rule->ex_data,
ex_schema->argl, ex_schema->argp);
return ex_data;
}
int compile_runtime_update(void *compile_runtime, void *compile_schema, int compile_runtime_update(void *compile_runtime, void *compile_schema,
const char *line, int valid_column) const char *line, int valid_column)
{ {

View File

@@ -438,7 +438,8 @@ const char *plugin_runtime_cached_row_get(void *plugin_runtime, size_t index)
return ex_data_runtime_cached_row_get(plugin_rt->ex_data_rt, index); return ex_data_runtime_cached_row_get(plugin_rt->ex_data_rt, index);
} }
void *plugin_runtime_get_ex_data(void *plugin_runtime, void *plugin_schema, const char *key) void *plugin_runtime_get_ex_data(void *plugin_runtime, void *plugin_schema,
const char *key, size_t key_len)
{ {
if (NULL == plugin_runtime || NULL == plugin_schema) { if (NULL == plugin_runtime || NULL == plugin_schema) {
return NULL; return NULL;
@@ -450,5 +451,5 @@ void *plugin_runtime_get_ex_data(void *plugin_runtime, void *plugin_schema, cons
return NULL; return NULL;
} }
return ex_data_runtime_get_ex_data_by_key(plugin_rt->ex_data_rt, key, strlen(key)); return ex_data_runtime_get_ex_data_by_key(plugin_rt->ex_data_rt, key, key_len);
} }

View File

@@ -54,7 +54,6 @@ struct table_manager {
int default_compile_table_id; int default_compile_table_id;
int g2g_table_id; int g2g_table_id;
struct maat_kv_store *tablename2id_map; struct maat_kv_store *tablename2id_map;
struct maat_kv_store *district_map; struct maat_kv_store *district_map;
struct maat_kv_store *tmp_district_map; struct maat_kv_store *tmp_district_map;
uint32_t district_num; uint32_t district_num;
@@ -597,7 +596,6 @@ void table_manager_destroy(struct table_manager *tbl_mgr)
FREE(tbl_mgr->accept_tags); FREE(tbl_mgr->accept_tags);
maat_kv_store_free(tbl_mgr->tablename2id_map); maat_kv_store_free(tbl_mgr->tablename2id_map);
FREE(tbl_mgr); FREE(tbl_mgr);
} }

View File

@@ -125,10 +125,50 @@ TEST_F(MaatFlagScan, hitMultiCompile) {
EXPECT_EQ(results[0], 194); EXPECT_EQ(results[0], 194);
EXPECT_EQ(results[1], 192); EXPECT_EQ(results[1], 192);
// memset(results, 0, sizeof(results)); memset(results, 0, sizeof(results));
// ret = maat_scan_flag(g_maat_instance, flag_table_id, 0, flag_scan_data, results, ret = maat_scan_flag(g_maat_instance, flag_table_id, 0, flag_scan_data, results,
// ARRAY_SIZE, &n_hit_result, &state); ARRAY_SIZE, &n_hit_result, &state);
// EXPECT_EQ(ret, MAAT_SCAN_OK); EXPECT_EQ(ret, MAAT_SCAN_HALF_HIT);
struct maat_hit_path hit_path[HIT_PATH_SIZE] = {0};
int n_read = 0;
n_read = maat_state_get_hit_paths(g_maat_instance, &state, hit_path, HIT_PATH_SIZE);
EXPECT_NE(n_read, 0);
maat_state_free(&state);
}
TEST_F(MaatFlagScan, hitRepeatedCompile) {
const char *flag_table_name = "FLAG_CONFIG";
int flag_table_id = maat_table_get_id(g_maat_instance, flag_table_name);
long long results[ARRAY_SIZE] = {0};
size_t n_hit_result = 0;
struct maat_state *state = NULL;
//compile_id:192 flag: 0000 0001 mask: 0000 0011
//scan_data: 0000 1001 or 0000 1101 should hit
long long flag_scan_data1 = 9;
int ret = maat_scan_flag(g_maat_instance, flag_table_id, 0, flag_scan_data1, results,
ARRAY_SIZE, &n_hit_result, &state);
EXPECT_EQ(ret, MAAT_SCAN_HIT);
EXPECT_EQ(n_hit_result, 1);
EXPECT_EQ(results[0], 192);
//compile_id:192 flag: 0000 0001 mask: 0000 0011
//compile_id:194 flag: 0001 0101 mask: 0001 1111
//scan_data: 0001 0101 should hit compile192 and compile194
long long flag_scan_data2 = 21;
memset(results, 0, sizeof(results));
ret = maat_scan_flag(g_maat_instance, flag_table_id, 0, flag_scan_data2, results,
ARRAY_SIZE, &n_hit_result, &state);
EXPECT_EQ(ret, MAAT_SCAN_HIT);
EXPECT_EQ(n_hit_result, 1);
EXPECT_EQ(results[0], 194);
memset(results, 0, sizeof(results));
ret = maat_scan_flag(g_maat_instance, flag_table_id, 0, flag_scan_data2, results,
ARRAY_SIZE, &n_hit_result, &state);
EXPECT_EQ(ret, MAAT_SCAN_HALF_HIT);
struct maat_hit_path hit_path[HIT_PATH_SIZE] = {0}; struct maat_hit_path hit_path[HIT_PATH_SIZE] = {0};
int n_read = 0; int n_read = 0;
@@ -1197,7 +1237,7 @@ TEST_F(CompileTable, CompileEXData) {
int compile_table_id = maat_table_get_id(g_maat_instance, compile_table_name); int compile_table_id = maat_table_get_id(g_maat_instance, compile_table_name);
int ex_data_counter = 0; int ex_data_counter = 0;
int ret = maat_compile_table_ex_schema_register(g_maat_instance, compile_table_id, int ret = maat_plugin_table_ex_schema_register(g_maat_instance, compile_table_id,
compile_ex_param_new, compile_ex_param_new,
compile_ex_param_free, compile_ex_param_free,
compile_ex_param_dup, compile_ex_param_dup,
@@ -1210,7 +1250,8 @@ TEST_F(CompileTable, CompileEXData) {
EXPECT_EQ(n_hit_result, 1); EXPECT_EQ(n_hit_result, 1);
EXPECT_EQ(results[0], 141); EXPECT_EQ(results[0], 141);
void *ex_data = maat_compile_table_get_ex_data(g_maat_instance, compile_table_id, results[0]); void *ex_data = maat_plugin_table_get_ex_data(g_maat_instance, compile_table_id,
(char *)&results[0], sizeof(long long));
ASSERT_TRUE(ex_data!=NULL); ASSERT_TRUE(ex_data!=NULL);
struct rule_ex_param *param = (struct rule_ex_param *)ex_data; struct rule_ex_param *param = (struct rule_ex_param *)ex_data;
EXPECT_EQ(param->id, 7799); EXPECT_EQ(param->id, 7799);
@@ -1358,7 +1399,7 @@ TEST_F(Policy, CompileEXData) {
int ex_data_counter = 0; int ex_data_counter = 0;
int compile_table_id = maat_table_get_id(g_maat_instance, "COMPILE"); int compile_table_id = maat_table_get_id(g_maat_instance, "COMPILE");
int ret = maat_compile_table_ex_schema_register(g_maat_instance, compile_table_id, int ret = maat_plugin_table_ex_schema_register(g_maat_instance, compile_table_id,
compile_ex_param_new, compile_ex_param_new,
compile_ex_param_free, compile_ex_param_free,
compile_ex_param_dup, compile_ex_param_dup,
@@ -1371,7 +1412,8 @@ TEST_F(Policy, CompileEXData) {
EXPECT_EQ(ret, MAAT_SCAN_HIT); EXPECT_EQ(ret, MAAT_SCAN_HIT);
EXPECT_EQ(results[0], 141); EXPECT_EQ(results[0], 141);
void *ex_data = maat_compile_table_get_ex_data(g_maat_instance, compile_table_id, results[0]); void *ex_data = maat_plugin_table_get_ex_data(g_maat_instance, compile_table_id,
(char *)&results[0], sizeof(long long));
ASSERT_TRUE(ex_data != NULL); ASSERT_TRUE(ex_data != NULL);
struct rule_ex_param *param = (struct rule_ex_param *)ex_data; struct rule_ex_param *param = (struct rule_ex_param *)ex_data;
@@ -1523,7 +1565,9 @@ TEST_F(MaatCmdTest, PluginEXData) {
EXPECT_EQ(ex_data_counter, TEST_CMD_LINE_NUM); EXPECT_EQ(ex_data_counter, TEST_CMD_LINE_NUM);
struct user_info *uinfo = NULL; struct user_info *uinfo = NULL;
uinfo = (struct user_info *)maat_plugin_table_get_ex_data(g_maat_instance, table_id, "192.168.0.4"); const char *key1 = "192.168.0.4";
uinfo = (struct user_info *)maat_plugin_table_get_ex_data(g_maat_instance, table_id,
key1, strlen(key1));
ASSERT_TRUE(uinfo != NULL); ASSERT_TRUE(uinfo != NULL);
EXPECT_EQ(0, strcmp(uinfo->name, "liuqiangdong")); EXPECT_EQ(0, strcmp(uinfo->name, "liuqiangdong"));
EXPECT_EQ(uinfo->id, 2); EXPECT_EQ(uinfo->id, 2);
@@ -1533,7 +1577,9 @@ TEST_F(MaatCmdTest, PluginEXData) {
EXPECT_GT(ret, 0); EXPECT_GT(ret, 0);
sleep(1); sleep(1);
uinfo = (struct user_info *)maat_plugin_table_get_ex_data(g_maat_instance, table_id, "192.168.0.2"); const char *key2 = "192.168.0.2";
uinfo = (struct user_info *)maat_plugin_table_get_ex_data(g_maat_instance, table_id,
key2, strlen(key2));
ASSERT_TRUE(uinfo == NULL); ASSERT_TRUE(uinfo == NULL);
} }
#endif #endif