diff --git a/src/entry/Maat_api.cpp b/src/entry/Maat_api.cpp index 43e217c..7927922 100644 --- a/src/entry/Maat_api.cpp +++ b/src/entry/Maat_api.cpp @@ -105,6 +105,13 @@ void fill_maat_rule(struct Maat_rule_t *rule, const struct _head_Maat_rule_t* ru memcpy(rule->service_defined, srv_def, MIN(srv_def_len,MAX_SERVICE_DEFINE_LEN)); return; } +static int compare_compile_id(const void *a, const void *b) +{ + struct Maat_rule_t *ra=(struct Maat_rule_t *)a; + struct Maat_rule_t *rb=(struct Maat_rule_t *)b; + + return (rb->config_id-ra->config_id); +} int region_compile(_Maat_feather_t*feather,struct _INNER_scan_status_t *_mid,int is_last_region,void* region_hit,int region_type_size,int group_offset,int region_hit_num,struct Maat_rule_t* result,_compile_result_t *rs_result, int size,int thread_num) { @@ -187,6 +194,8 @@ int region_compile(_Maat_feather_t*feather,struct _INNER_scan_status_t *_mid,int } if(result_cnt>0) { + qsort(result, result_cnt, sizeof(struct Maat_rule_t), + compare_compile_id); alignment_int64_array_add(feather->hit_cnt,thread_num,1); } return result_cnt; diff --git a/test/test_maatframe.cpp b/test/test_maatframe.cpp index 1dfe92d..6d516eb 100644 --- a/test/test_maatframe.cpp +++ b/test/test_maatframe.cpp @@ -1038,7 +1038,6 @@ int test_add_expr_command(Maat_feather_t feather,const char* region_table,int co int group_num=1,ret=0; memset(&rule,0,sizeof(rule)); rule.config_id=config_id; - strcpy(rule.service_defined,"maat_command"); //MUST acqire by function, because Maat_cmd_t has some hidden members. cmd=Maat_create_cmd(&rule, group_num); @@ -1301,6 +1300,46 @@ TEST_F(MaatCmdTest, RuleIDRecycle) EXPECT_EQ(result.config_id, rule_id); return; } +TEST_F(MaatCmdTest, ReturnRuleIDWithDescendingOrder) +{ + const char* table_name="HTTP_URL"; + const char* scan_data="This string will hit mulptiple rules."; + const char* keywords="string\\bwill\\bhit"; + Maat_feather_t feather=MaatCmdTest::_shared_feather; + + + int table_id=0; + table_id=Maat_table_register(feather,table_name); + ASSERT_GT(table_id, 0); + + int i=0, repeat_times=4; + struct Maat_rule_t result[8]; + int expect_ruleid[8]; + scan_status_t mid=NULL; + + int rule_id=(int)Maat_cmd_incrby(feather, "TEST_SEQ", repeat_times); + int label_id=5211, ret=0; + for(i=0; i=0); + usleep(WAIT_FOR_EFFECTIVE_US);//waiting for commands go into effect + ret=Maat_full_scan_string(feather, table_id,CHARSET_GBK, scan_data, strlen(scan_data), + result, NULL, 8, + &mid, 0); + Maat_clean_status(&mid); + EXPECT_EQ(ret, repeat_times); + for(i=0;i