#include "Maat_rule.h" #include "stream_fuzzy_hash.h" #include "Maat_command.h" #include #include #include #include #define WAIT_FOR_EFFECTIVE_SECOND 4 void ipv4_addr_set_copy(struct ipaddr *ipv4_addr, struct stream_tuple4_v4* v4_addr, const char* src_ip, unsigned short sport, const char* dest_ip, unsigned short dport) { ipv4_addr->addrtype=ADDR_TYPE_IPV4; inet_pton(AF_INET, src_ip, &(v4_addr->saddr)); v4_addr->source=htons(sport); inet_pton(AF_INET, dest_ip, &(v4_addr->daddr)); v4_addr->dest=htons(dport); ipv4_addr->v4=v4_addr; return; } void random_keyword_generate(char* keyword_buf, size_t sz) { #define MIN_KEYWORD_LEN 4 size_t i=0, len=0; len=random()%(sz-1-MIN_KEYWORD_LEN)+MIN_KEYWORD_LEN; for(i=0; irule_id=atoi(table_line+column_offset); ret=Maat_helper_read_column(table_line, 5, &column_offset, &column_len); EXPECT_EQ(ret, 0); ud->buffer=(char*)calloc(sizeof(char), column_len+1); strncpy(ud->buffer, table_line+column_offset, column_len); ud->ref_cnt=1; *ad=ud; (*counter)++; return; } void ip_plugin_EX_free_cb(int table_id, MAAT_PLUGIN_EX_DATA* ad, long argl, void *argp) { struct ip_plugin_ud* u=(struct ip_plugin_ud*)(*ad); u->ref_cnt--; if(u->ref_cnt>0) return; free(u->buffer); free(u); *ad=NULL; } void ip_plugin_EX_dup_cb(int table_id, MAAT_PLUGIN_EX_DATA *to, MAAT_PLUGIN_EX_DATA *from, long argl, void *argp) { struct ip_plugin_ud* u=(struct ip_plugin_ud*)(*from); u->ref_cnt++; *to=u; } TEST_F(MaatCMDPerfTest, UpdateIPPlugin) { #define IP_Plugin_EX_data Maat_feather_t feather=MaatCMDPerfTest::_shared_feather; int ret=0, i=0; int table_id=0, ip_plugin_ex_data_counter=0; const char* table_name="TEST_IP_PLUGIN_WITH_EXDATA"; const int TEST_CMD_LINE_NUM=4; 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]={ "101\t4\t192.168.30.99\t192.168.30.101\tSomething-like-json\t1", "102\t4\t192.168.30.90\t192.168.30.128\tBigger-range-should-in-the-back\t1", "103\t6\t2001:db8:1234::\t2001:db8:1235::\tBigger-range-should-in-the-back\t1", "104\t6\t2001:db8:1234::1\t2001:db8:1234::5210\tSomething-like-json\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=0); EXPECT_EQ(ip_plugin_ex_data_counter, 4); struct ip_address ipv4, ipv6; struct ip_plugin_ud* result[4]; ipv4.ip_type=4; inet_pton(AF_INET, "192.168.30.100", &(ipv4.ipv4)); memset(&result, 0, sizeof(result)); ret=Maat_ip_plugin_get_EX_data(feather, table_id, &ipv4, (void**)result, 4); ASSERT_EQ(ret, 2); EXPECT_EQ(result[0]->rule_id, 101); EXPECT_EQ(result[1]->rule_id, 102); for(i=0; irule_id, 104); EXPECT_EQ(result[1]->rule_id, 103); for(i=0; irule_id=atoi(table_line+column_offset); ret=Maat_helper_read_column(table_line, 4, &column_offset, &column_len); EXPECT_EQ(ret, 0); sscanf(table_line+column_offset, "catid=%d",&ud->catid); ud->ref_cnt=1; *ad=ud; (*counter)++; return; } void fqdn_plugin_EX_free_cb(int table_id, MAAT_PLUGIN_EX_DATA* ad, long argl, void *argp) { struct fqdn_plugin_ud* u=(struct fqdn_plugin_ud*)(*ad); u->ref_cnt--; if(u->ref_cnt>0) return; free(u); *ad=NULL; } void fqdn_plugin_EX_dup_cb(int table_id, MAAT_PLUGIN_EX_DATA *to, MAAT_PLUGIN_EX_DATA *from, long argl, void *argp) { struct fqdn_plugin_ud* u=(struct fqdn_plugin_ud*)(*from); u->ref_cnt++; *to=u; } TEST_F(MaatCMDPerfTest, UpdateFQDNPlugin) { #define FQDN_Plugin_EX_data Maat_feather_t feather=MaatCMDPerfTest::_shared_feather; int ret=0, i=0; int table_id=0, fqdn_plugin_ex_data_counter=0; const char* table_name="TEST_FQDN_PLUGIN_WITH_EXDATA"; const int TEST_CMD_LINE_NUM=5; 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]={ "201\t0\twww.example1.com\tcatid=1\t1", "202\t1\t.example1.com\tcatid=1\t1", "203\t0\tnews.example1.com\tcatid=2\t1", "204\t0\tr3---sn-i3belne6.example2.com\tcatid=3\t1", "205\t0\tr3---sn-i3belne6.example2.com\tcatid=3\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=0); EXPECT_EQ(fqdn_plugin_ex_data_counter, 5); struct fqdn_plugin_ud* result[4]; ret=Maat_fqdn_plugin_get_EX_data(feather, table_id, "r3---sn-i3belne6.example2.com", (void**)result, 4); ASSERT_EQ(ret, 2); for(i=0; i