🧪 test(refactor glimpse_detector test): split test plugin and test main
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
add_executable(gtest_glimpse_detector gtest_glimpse_detector_main.cpp)
|
add_executable(gtest_glimpse_detector gtest_glimpse_detector_main.cpp gtest_glimpse_detector_plugin.cpp)
|
||||||
|
|
||||||
target_include_directories(gtest_glimpse_detector PRIVATE ${CMAKE_SOURCE_DIR}/deps/)
|
target_include_directories(gtest_glimpse_detector PRIVATE ${CMAKE_SOURCE_DIR}/deps/)
|
||||||
target_include_directories(gtest_glimpse_detector PRIVATE ${CMAKE_SOURCE_DIR}/decoders/glimpse_detector)
|
target_include_directories(gtest_glimpse_detector PRIVATE ${CMAKE_SOURCE_DIR}/decoders/glimpse_detector)
|
||||||
|
|||||||
@@ -11,292 +11,68 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
#include "app_l7_protocol.h"
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
#include "cjson/cJSON.h"
|
|
||||||
//#include "MESA_prof_load.h"
|
|
||||||
|
|
||||||
#include "stellar/stellar.h"
|
#include "stellar/stellar.h"
|
||||||
#include "stellar/session.h"
|
#include "stellar/session.h"
|
||||||
#include "stellar/stellar_exdata.h"
|
|
||||||
#include "stellar/stellar_mq.h"
|
#include "stellar/stellar_mq.h"
|
||||||
|
|
||||||
|
#include "cjson/cJSON.h"
|
||||||
|
|
||||||
struct app_test_para
|
|
||||||
|
struct gtest_json_result
|
||||||
{
|
{
|
||||||
cJSON *test_result_root;
|
cJSON *test_json_root;
|
||||||
cJSON *load_result_root;
|
cJSON *expect_json_root;
|
||||||
int result_count;
|
int result_count;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct app_test_para g_test_para={};
|
static struct gtest_json_result *gtest_result_new(const char *expect_json_path)
|
||||||
|
|
||||||
static int commit_test_result_json(struct app_test_para *para, cJSON *node, const char *name);
|
|
||||||
|
|
||||||
struct app_test_plugin_env
|
|
||||||
{
|
{
|
||||||
int test_exdata_idx;
|
struct gtest_json_result *para = (struct gtest_json_result *)calloc(1, sizeof(struct gtest_json_result));
|
||||||
int l7_exdata_idx;
|
para->test_json_root = cJSON_CreateArray();
|
||||||
int test_app_plugin_id;
|
|
||||||
struct stellar *st;
|
|
||||||
};
|
|
||||||
|
|
||||||
#define MAX_PROTO_ID_NUM 10000
|
FILE *file = fopen(expect_json_path, "rb");
|
||||||
static char *g_proto_id2name[MAX_PROTO_ID_NUM];
|
if(file)
|
||||||
|
|
||||||
static int load_l7_protocol_mapper(const char *filename)
|
|
||||||
{
|
|
||||||
memset(g_proto_id2name, 0, sizeof(g_proto_id2name));
|
|
||||||
int ret=0, proto_id=0;;
|
|
||||||
FILE *fp=NULL;
|
|
||||||
char line[1024]={0};
|
|
||||||
char type_name[32]={0};
|
|
||||||
char proto_name[32]={0};
|
|
||||||
|
|
||||||
fp=fopen(filename, "r");
|
|
||||||
if(fp==NULL)
|
|
||||||
{
|
{
|
||||||
printf("Open %s failed ...", filename);
|
fseek(file, 0, SEEK_END);
|
||||||
|
long filesize = ftell(file);
|
||||||
|
rewind(file);
|
||||||
|
char *buffer = (char *)calloc(filesize + 1, 1);
|
||||||
|
fread(buffer, 1, filesize, file);
|
||||||
|
|
||||||
|
para->expect_json_root=cJSON_Parse(buffer);
|
||||||
|
|
||||||
|
free(buffer);
|
||||||
|
fclose(file);
|
||||||
|
}
|
||||||
|
para->result_count=1;//count start from 1
|
||||||
|
return para;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int gtest_result_compare(struct gtest_json_result *para)
|
||||||
|
{
|
||||||
|
if(cJSON_GetArraySize(para->test_json_root)!=cJSON_GetArraySize(para->expect_json_root))
|
||||||
|
{
|
||||||
|
char *load_json_str = cJSON_Print(para->expect_json_root);
|
||||||
|
printf("LOAD Raw:\n%s\n", load_json_str);
|
||||||
|
free(load_json_str);
|
||||||
|
char *result_json_str = cJSON_Print(para->test_json_root);
|
||||||
|
printf("TEST Raw:\n%s\n", result_json_str);
|
||||||
|
free(result_json_str);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
int compare_ret = cJSON_Compare(para->expect_json_root, para->test_json_root, 0);
|
||||||
memset(line, 0, sizeof(line));
|
if (compare_ret != 1)
|
||||||
|
|
||||||
while((fgets(line, sizeof(line), fp))!=NULL)
|
|
||||||
{
|
{
|
||||||
if(line[0]=='#' || line[0]=='\n' || line[0]=='\r' ||line[0]=='\0')
|
char *load_json_str = cJSON_Print(para->expect_json_root);
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret=sscanf(line, "%31s %31s %d", type_name, proto_name, &proto_id);
|
|
||||||
assert(ret==3 && proto_id < MAX_PROTO_ID_NUM);
|
|
||||||
g_proto_id2name[proto_id] = (char*)calloc(strlen(proto_name)+1, 1);
|
|
||||||
strcpy(g_proto_id2name[proto_id], proto_name);
|
|
||||||
memset(line, 0, sizeof(line));
|
|
||||||
}
|
|
||||||
|
|
||||||
fclose(fp);
|
|
||||||
fp=NULL;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void commit_test_result(struct app_test_plugin_env *env, cJSON *ctx, struct session *sess)
|
|
||||||
{
|
|
||||||
assert(env->l7_exdata_idx >= 0 && ctx != NULL);
|
|
||||||
struct l7_protocol_label *label = (struct l7_protocol_label *)session_exdata_get(sess, env->l7_exdata_idx);;
|
|
||||||
if(label != NULL)
|
|
||||||
{
|
|
||||||
int proto_ids[8];
|
|
||||||
const char* proto_names[8];
|
|
||||||
for(int i = 0; i < label->protocol_id_num; i++)
|
|
||||||
{
|
|
||||||
proto_ids[i] = (int)(label->protocol_id[i]);
|
|
||||||
proto_names[i] = g_proto_id2name[proto_ids[i]];
|
|
||||||
|
|
||||||
}
|
|
||||||
cJSON *label_ids = cJSON_CreateIntArray(proto_ids, label->protocol_id_num);
|
|
||||||
cJSON_AddItemToObject(ctx, "l7_label_id", label_ids);
|
|
||||||
cJSON *label_names = cJSON_CreateStringArray(proto_names, label->protocol_id_num);
|
|
||||||
cJSON_AddItemToObject(ctx, "l7_label_name", label_names);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
cJSON_AddStringToObject(ctx, "l7_label_id", "UNKNOWN");
|
|
||||||
}
|
|
||||||
unsigned char dir_flag;
|
|
||||||
int is_symmetric=session_is_symmetric(sess, &dir_flag);
|
|
||||||
if(is_symmetric)
|
|
||||||
{
|
|
||||||
cJSON_AddStringToObject(ctx, "STREAM_DIR", "DOUBLE");
|
|
||||||
}
|
|
||||||
else if(dir_flag == SESSION_SEEN_C2S_FLOW)
|
|
||||||
{
|
|
||||||
cJSON_AddStringToObject(ctx, "STREAM_DIR", "C2S");
|
|
||||||
}
|
|
||||||
else if(dir_flag == SESSION_SEEN_S2C_FLOW)
|
|
||||||
{
|
|
||||||
cJSON_AddStringToObject(ctx, "STREAM_DIR", "S2C");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
assert(0);
|
|
||||||
}
|
|
||||||
if (ctx)
|
|
||||||
{
|
|
||||||
char result_name[128] = "";
|
|
||||||
sprintf(result_name, "APP_PROTO_IDENTIFY_RESULT_%d", g_test_para.result_count);
|
|
||||||
commit_test_result_json(&g_test_para, ctx, result_name);
|
|
||||||
g_test_para.result_count += 1;
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void *APP_TEST_CTX_NEW(struct session *sess, void *plugin_env)
|
|
||||||
{
|
|
||||||
cJSON *ctx =cJSON_CreateObject();
|
|
||||||
cJSON_AddStringToObject(ctx, "Tuple4", session_get0_readable_addr(sess));
|
|
||||||
enum session_type type= session_get_type(sess);
|
|
||||||
if (type == SESSION_TYPE_TCP)
|
|
||||||
{
|
|
||||||
cJSON_AddStringToObject(ctx, "STREAM_TYPE", "TCP");
|
|
||||||
}
|
|
||||||
if (type == SESSION_TYPE_UDP)
|
|
||||||
{
|
|
||||||
cJSON_AddStringToObject(ctx, "STREAM_TYPE", "UDP");
|
|
||||||
}
|
|
||||||
return ctx;
|
|
||||||
}
|
|
||||||
|
|
||||||
void APP_TEST_CTX_FREE(struct session *sess, void *session_ctx, void *plugin_env)
|
|
||||||
{
|
|
||||||
cJSON *ctx = (cJSON *)session_ctx;
|
|
||||||
commit_test_result((struct app_test_plugin_env*)plugin_env, ctx, sess);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void APP_TEST_ON_SESSION_MSG(struct session *sess, int topic_id, const void *msg, void *per_session_ctx, void *plugin_env)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C" void *APP_TEST_PLUG_INIT(struct stellar *st)
|
|
||||||
{
|
|
||||||
struct app_test_plugin_env *env = (struct app_test_plugin_env *)calloc(1, sizeof(struct app_test_plugin_env));
|
|
||||||
env->st=st;
|
|
||||||
//const char *l7_label_name=(const char*)"L7_PROTOCOL_LABEL";
|
|
||||||
//const char *l7_bridge_name=(const char*)"APP_BRIDGE";
|
|
||||||
const char *l7_proto_name=(const char*)"./tsgconf/tsg_l7_protocol.conf";
|
|
||||||
//MESA_load_profile_string_def("./tsgconf/main.conf", "SYSTEM", "L7_LABEL_NAME", l7_label_name, sizeof(l7_label_name), "L7_PROTOCOL_LABEL");
|
|
||||||
//MESA_load_profile_string_def("./tsgconf/main.conf", "SYSTEM", "APP_BRIDGE_NAME", l7_bridge_name, sizeof(l7_bridge_name), "APP_BRIDGE");
|
|
||||||
//MESA_load_profile_string_def("./tsgconf/main.conf", "SYSTEM", "L7_PROTOCOL_FILE", l7_proto_name, sizeof(l7_proto_name), "./tsgconf/tsg_l7_protocol.conf");
|
|
||||||
env->l7_exdata_idx= stellar_exdata_new_index(st, "L7_PROTOCOL", stellar_exdata_free_default, NULL);
|
|
||||||
env->test_exdata_idx= stellar_exdata_new_index(st, "APP_PROTO_TEST", stellar_exdata_free_default, NULL);
|
|
||||||
if(env->l7_exdata_idx<0 || env->test_exdata_idx<0)
|
|
||||||
{
|
|
||||||
printf("APP_PROTO_IDENTIFY_TEST_PLUG_INIT:stellar_session_get_ex_new_index faild!!!\n");
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
int ret = load_l7_protocol_mapper(l7_proto_name);
|
|
||||||
if(ret<0)
|
|
||||||
{
|
|
||||||
printf("APP_PROTO_IDENTIFY_TEST_PLUG_INIT:l7_protocol_mapper failed !!!\n");
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
env->test_app_plugin_id=stellar_session_plugin_register(st, APP_TEST_CTX_NEW, APP_TEST_CTX_FREE, env);
|
|
||||||
if(env->test_app_plugin_id < 0)
|
|
||||||
{
|
|
||||||
printf("APP_PROTO_IDENTIFY_TEST_PLUG_INIT:stellar_plugin_register failed !!!\n");
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
int tcp_topic_id=stellar_mq_get_topic_id(st, TOPIC_TCP);
|
|
||||||
int udp_topic_id=stellar_mq_get_topic_id(st, TOPIC_UDP);
|
|
||||||
if(tcp_topic_id < 0 || udp_topic_id < 0)
|
|
||||||
{
|
|
||||||
perror("get tcp or udp topic id failed\n");
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
stellar_session_mq_subscribe(st, tcp_topic_id, APP_TEST_ON_SESSION_MSG, env->test_app_plugin_id);
|
|
||||||
stellar_session_mq_subscribe(st, udp_topic_id, APP_TEST_ON_SESSION_MSG, env->test_app_plugin_id);
|
|
||||||
printf("APP_PROTO_IDENTIFY_TEST_PLUG_INIT OK!\n");
|
|
||||||
|
|
||||||
return env;
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C" void APP_TEST_PLUG_DESTROY(void *ctx)
|
|
||||||
{
|
|
||||||
struct app_test_plugin_env *env = (struct app_test_plugin_env *)ctx;
|
|
||||||
free(env);
|
|
||||||
printf("APP_PROTO_IDENTIFY_TEST_PLUG_DESTROY OK!\n");
|
|
||||||
return ;
|
|
||||||
}
|
|
||||||
|
|
||||||
#include <gtest/gtest.h>
|
|
||||||
#include "stellar/stellar.h"
|
|
||||||
|
|
||||||
static int commit_test_result_json(struct app_test_para *para, cJSON *node, const char *name)
|
|
||||||
{
|
|
||||||
assert(node != NULL || name != NULL || para != NULL);
|
|
||||||
if(para->test_result_root)
|
|
||||||
{
|
|
||||||
//cJSON_AddItemToObject(g_test_result_root, name, node);
|
|
||||||
cJSON_AddStringToObject(node, "name", name);
|
|
||||||
cJSON_AddItemToArray(para->test_result_root, node);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static cJSON *load_result_from_jsonfile(const char *json_path)
|
|
||||||
{
|
|
||||||
|
|
||||||
if(json_path == NULL)return NULL;
|
|
||||||
|
|
||||||
long file_len = 0;
|
|
||||||
char *file_content = NULL;
|
|
||||||
FILE *fp = NULL;
|
|
||||||
|
|
||||||
fp = fopen(json_path, "r+");
|
|
||||||
if (NULL == fp)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
fseek(fp, 0, SEEK_END);
|
|
||||||
file_len = ftell(fp);
|
|
||||||
fseek(fp, 0, SEEK_SET);
|
|
||||||
if (file_len == 0) {
|
|
||||||
fclose(fp);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
file_content = (char *) malloc(file_len + 1);
|
|
||||||
fread(file_content, file_len, 1, fp);
|
|
||||||
file_content[file_len] = '\0';
|
|
||||||
cJSON *load = cJSON_Parse(file_content);
|
|
||||||
free(file_content);
|
|
||||||
fclose(fp);
|
|
||||||
return load;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static int app_test_para_start(struct app_test_para *para, const char *load_json_path)
|
|
||||||
{
|
|
||||||
para->test_result_root = cJSON_CreateArray();
|
|
||||||
para->load_result_root = load_result_from_jsonfile(load_json_path);
|
|
||||||
para->result_count=1;//count start from 1
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int app_test_para_finish(struct app_test_para *para)
|
|
||||||
{
|
|
||||||
if(para==NULL)return -1;
|
|
||||||
if(para->test_result_root==NULL || para->load_result_root==NULL)return -1;
|
|
||||||
int ret=-1;
|
|
||||||
if(cJSON_GetArraySize(para->test_result_root)!=cJSON_GetArraySize(para->load_result_root))
|
|
||||||
{
|
|
||||||
char *load_json_str = cJSON_Print(para->load_result_root);
|
|
||||||
printf("LOAD Raw:\n%s\n", load_json_str);
|
printf("LOAD Raw:\n%s\n", load_json_str);
|
||||||
free(load_json_str);
|
free(load_json_str);
|
||||||
char *result_json_str = cJSON_Print(para->test_result_root);
|
char *result_json_str = cJSON_Print(para->test_json_root);
|
||||||
printf("TEST Raw:\n%s\n", result_json_str);
|
|
||||||
free(result_json_str);
|
|
||||||
ret=-1;
|
|
||||||
goto error_out;
|
|
||||||
}
|
|
||||||
ret = cJSON_Compare(para->load_result_root, para->test_result_root, 0);
|
|
||||||
if (ret != 1)
|
|
||||||
{
|
|
||||||
char *load_json_str = cJSON_Print(para->load_result_root);
|
|
||||||
printf("LOAD Raw:\n%s\n", load_json_str);
|
|
||||||
free(load_json_str);
|
|
||||||
char *result_json_str = cJSON_Print(para->test_result_root);
|
|
||||||
printf("TEST Raw:\n%s\n", result_json_str);
|
printf("TEST Raw:\n%s\n", result_json_str);
|
||||||
free(result_json_str);
|
free(result_json_str);
|
||||||
|
|
||||||
cJSON *t_load = para->load_result_root->child, *t_test = para->test_result_root->child;
|
cJSON *t_load = para->expect_json_root->child, *t_test = para->test_json_root->child;
|
||||||
while (t_load != NULL)
|
while (t_load != NULL)
|
||||||
{
|
{
|
||||||
// print first diff item, then return;
|
// print first diff item, then return;
|
||||||
@@ -308,46 +84,81 @@ static int app_test_para_finish(struct app_test_para *para)
|
|||||||
result_json_str = cJSON_Print(t_test);
|
result_json_str = cJSON_Print(t_test);
|
||||||
printf("TEST Diff:\n%s\n", result_json_str);
|
printf("TEST Diff:\n%s\n", result_json_str);
|
||||||
free(result_json_str);
|
free(result_json_str);
|
||||||
goto error_out;
|
return -1;
|
||||||
}
|
}
|
||||||
t_load = t_load->next;
|
t_load = t_load->next;
|
||||||
t_test = t_test->next;
|
t_test = t_test->next;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return compare_ret;
|
||||||
error_out:
|
|
||||||
if(para->test_result_root)cJSON_Delete(para->test_result_root);
|
|
||||||
if(para->load_result_root)cJSON_Delete(para->load_result_root);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void gtest_result_free(struct gtest_json_result *para)
|
||||||
|
{
|
||||||
|
if(para)
|
||||||
|
{
|
||||||
|
if(para->test_json_root)cJSON_Delete(para->test_json_root);
|
||||||
|
if(para->expect_json_root)cJSON_Delete(para->expect_json_root);
|
||||||
|
free(para);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void gtest_on_session_msg_expect_json(struct session *sess, int topic_id, const void *msg, void *per_session_ctx,
|
||||||
|
void *plugin_env)
|
||||||
|
{
|
||||||
|
struct gtest_json_result *g_test_para = (struct gtest_json_result *)plugin_env;
|
||||||
|
|
||||||
|
cJSON *per_session_json = cJSON_Parse((const char *)msg);
|
||||||
|
|
||||||
|
if (g_test_para->test_json_root)
|
||||||
|
{
|
||||||
|
char result_name[128] = "";
|
||||||
|
sprintf(result_name, "APP_PROTO_IDENTIFY_RESULT_%d", g_test_para->result_count);
|
||||||
|
cJSON_AddStringToObject(per_session_json, "name", result_name);
|
||||||
|
cJSON_AddItemToArray(g_test_para->test_json_root, per_session_json);
|
||||||
|
}
|
||||||
|
g_test_para->result_count += 1;
|
||||||
|
}
|
||||||
|
|
||||||
/**********************************************
|
/**********************************************
|
||||||
* GTEST MAIN *
|
* GTEST MAIN *
|
||||||
**********************************************/
|
**********************************************/
|
||||||
|
|
||||||
char *expect_json_path=NULL;
|
|
||||||
|
|
||||||
TEST(gtest_glimpse_detector, run) {
|
|
||||||
|
|
||||||
app_test_para_start(&g_test_para, expect_json_path);
|
|
||||||
|
|
||||||
struct stellar *st=stellar_new("./conf/stellar.toml", "./plugin/spec.toml", "./conf/log.toml");
|
|
||||||
stellar_run(st);
|
|
||||||
stellar_free(st);
|
|
||||||
|
|
||||||
EXPECT_EQ(app_test_para_finish(&g_test_para), 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char ** argv)
|
int main(int argc, char ** argv)
|
||||||
{
|
{
|
||||||
|
::testing::InitGoogleTest(&argc, argv);
|
||||||
|
|
||||||
|
EXPECT_EQ(argc, 2);
|
||||||
|
|
||||||
if(argc != 2)
|
if(argc != 2)
|
||||||
{
|
{
|
||||||
printf("Invalid Argument!!!\n Usage: ./[gtest_main] [/path/to/expect_json]\n");
|
printf("Invalid Argument!!!\n Usage: ./[gtest_main] [/path/to/expect_json]\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
::testing::InitGoogleTest(&argc, argv);
|
|
||||||
expect_json_path=argv[1];
|
char *expect_json_path=argv[1];
|
||||||
return RUN_ALL_TESTS();
|
struct gtest_json_result *g_test_para = gtest_result_new(expect_json_path);
|
||||||
|
|
||||||
|
struct stellar *st=stellar_new("./conf/stellar.toml", "./plugin/spec.toml", "./conf/log.toml");
|
||||||
|
EXPECT_TRUE(st!=NULL);
|
||||||
|
|
||||||
|
int plugin_id=stellar_session_plugin_register(st, NULL, NULL, g_test_para);
|
||||||
|
EXPECT_TRUE(plugin_id>=0);
|
||||||
|
|
||||||
|
int expect_json_topic_id = stellar_mq_get_topic_id(st, "EXPECT_JSON");
|
||||||
|
EXPECT_TRUE(expect_json_topic_id>=0);
|
||||||
|
|
||||||
|
stellar_session_mq_subscribe(st, expect_json_topic_id, gtest_on_session_msg_expect_json, plugin_id);
|
||||||
|
|
||||||
|
stellar_run(st);
|
||||||
|
stellar_free(st);
|
||||||
|
|
||||||
|
EXPECT_TRUE(g_test_para->expect_json_root != NULL && g_test_para->test_json_root != NULL);
|
||||||
|
EXPECT_EQ(gtest_result_compare(g_test_para), 1);
|
||||||
|
|
||||||
|
gtest_result_free(g_test_para);
|
||||||
|
|
||||||
|
return ::testing::Test::HasFailure() ? 1 : 0;
|
||||||
}
|
}
|
||||||
191
test/glimpse_detector/gtest_glimpse_detector_plugin.cpp
Normal file
191
test/glimpse_detector/gtest_glimpse_detector_plugin.cpp
Normal file
@@ -0,0 +1,191 @@
|
|||||||
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#include "app_l7_protocol.h"
|
||||||
|
|
||||||
|
#include "stellar/stellar.h"
|
||||||
|
#include "stellar/session.h"
|
||||||
|
#include "stellar/stellar_exdata.h"
|
||||||
|
#include "stellar/stellar_mq.h"
|
||||||
|
|
||||||
|
#include "cjson/cJSON.h"
|
||||||
|
|
||||||
|
|
||||||
|
#define MAX_APP_ID_VALUE 10000
|
||||||
|
struct glimpse_detector_test_plugin_env
|
||||||
|
{
|
||||||
|
int test_exdata_idx;
|
||||||
|
int l7_exdata_idx;
|
||||||
|
int test_app_plugin_id;
|
||||||
|
int expect_json_topic_id;
|
||||||
|
struct stellar *st;
|
||||||
|
char *g_proto_id2name[MAX_APP_ID_VALUE];
|
||||||
|
};
|
||||||
|
|
||||||
|
static int load_l7_protocol_mapper(const char *filename, struct glimpse_detector_test_plugin_env *env)
|
||||||
|
{
|
||||||
|
memset(env->g_proto_id2name, 0, sizeof(env->g_proto_id2name));
|
||||||
|
int ret=0, proto_id=0;;
|
||||||
|
FILE *fp=NULL;
|
||||||
|
char line[1024]={0};
|
||||||
|
char type_name[32]={0};
|
||||||
|
char proto_name[32]={0};
|
||||||
|
|
||||||
|
fp=fopen(filename, "r");
|
||||||
|
if(fp==NULL)
|
||||||
|
{
|
||||||
|
printf("Open %s failed ...", filename);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(line, 0, sizeof(line));
|
||||||
|
|
||||||
|
while((fgets(line, sizeof(line), fp))!=NULL)
|
||||||
|
{
|
||||||
|
if(line[0]=='#' || line[0]=='\n' || line[0]=='\r' ||line[0]=='\0')
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret=sscanf(line, "%31s %31s %d", type_name, proto_name, &proto_id);
|
||||||
|
env->g_proto_id2name[proto_id] = (char*)calloc(strlen(proto_name)+1, 1);
|
||||||
|
strcpy(env->g_proto_id2name[proto_id], proto_name);
|
||||||
|
memset(line, 0, sizeof(line));
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(fp);
|
||||||
|
fp=NULL;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void publish_session_test_result(struct glimpse_detector_test_plugin_env *env, cJSON *ctx, struct session *sess)
|
||||||
|
{
|
||||||
|
assert(env->l7_exdata_idx >= 0 && ctx != NULL);
|
||||||
|
struct l7_protocol_label *label = (struct l7_protocol_label *)session_exdata_get(sess, env->l7_exdata_idx);;
|
||||||
|
if(label != NULL)
|
||||||
|
{
|
||||||
|
int proto_ids[8];
|
||||||
|
const char* proto_names[8];
|
||||||
|
for(int i = 0; i < label->protocol_id_num; i++)
|
||||||
|
{
|
||||||
|
proto_ids[i] = (int)(label->protocol_id[i]);
|
||||||
|
proto_names[i] = env->g_proto_id2name[proto_ids[i]];
|
||||||
|
|
||||||
|
}
|
||||||
|
cJSON *label_ids = cJSON_CreateIntArray(proto_ids, label->protocol_id_num);
|
||||||
|
cJSON_AddItemToObject(ctx, "l7_label_id", label_ids);
|
||||||
|
cJSON *label_names = cJSON_CreateStringArray(proto_names, label->protocol_id_num);
|
||||||
|
cJSON_AddItemToObject(ctx, "l7_label_name", label_names);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cJSON_AddStringToObject(ctx, "l7_label_id", "UNKNOWN");
|
||||||
|
}
|
||||||
|
unsigned char dir_flag;
|
||||||
|
int is_symmetric=session_is_symmetric(sess, &dir_flag);
|
||||||
|
if(is_symmetric)
|
||||||
|
{
|
||||||
|
cJSON_AddStringToObject(ctx, "STREAM_DIR", "DOUBLE");
|
||||||
|
}
|
||||||
|
else if(dir_flag == SESSION_SEEN_C2S_FLOW)
|
||||||
|
{
|
||||||
|
cJSON_AddStringToObject(ctx, "STREAM_DIR", "C2S");
|
||||||
|
}
|
||||||
|
else if(dir_flag == SESSION_SEEN_S2C_FLOW)
|
||||||
|
{
|
||||||
|
cJSON_AddStringToObject(ctx, "STREAM_DIR", "S2C");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
assert(0);
|
||||||
|
}
|
||||||
|
session_mq_publish_message(sess, env->expect_json_topic_id, cJSON_Print(ctx));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void *glimpse_detector_test_ctx_new(struct session *sess, void *plugin_env)
|
||||||
|
{
|
||||||
|
cJSON *ctx =cJSON_CreateObject();
|
||||||
|
cJSON_AddStringToObject(ctx, "Tuple4", session_get0_readable_addr(sess));
|
||||||
|
enum session_type type= session_get_type(sess);
|
||||||
|
if (type == SESSION_TYPE_TCP)
|
||||||
|
{
|
||||||
|
cJSON_AddStringToObject(ctx, "STREAM_TYPE", "TCP");
|
||||||
|
}
|
||||||
|
if (type == SESSION_TYPE_UDP)
|
||||||
|
{
|
||||||
|
cJSON_AddStringToObject(ctx, "STREAM_TYPE", "UDP");
|
||||||
|
}
|
||||||
|
return ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void APP_TEST_ON_SESSION_MSG(struct session *sess, int topic_id, const void *msg, void *per_session_ctx, void *plugin_env)
|
||||||
|
{
|
||||||
|
if(session_get_current_state(sess)==SESSION_STATE_CLOSED)
|
||||||
|
{
|
||||||
|
publish_session_test_result((struct glimpse_detector_test_plugin_env*)plugin_env, (cJSON *)per_session_ctx, sess);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" void *GLIMPSE_DETECTOR_TEST_PLUG_LOAD(struct stellar *st)
|
||||||
|
{
|
||||||
|
struct glimpse_detector_test_plugin_env *env = (struct glimpse_detector_test_plugin_env *)calloc(1, sizeof(struct glimpse_detector_test_plugin_env));
|
||||||
|
env->st=st;
|
||||||
|
const char *l7_proto_name=(const char*)"./tsgconf/tsg_l7_protocol.conf";
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
const char *l7_label_name=(const char*)"L7_PROTOCOL_LABEL";
|
||||||
|
const char *l7_bridge_name=(const char*)"APP_BRIDGE";
|
||||||
|
MESA_load_profile_string_def("./tsgconf/main.conf", "SYSTEM", "L7_LABEL_NAME", l7_label_name, sizeof(l7_label_name), "L7_PROTOCOL_LABEL");
|
||||||
|
MESA_load_profile_string_def("./tsgconf/main.conf", "SYSTEM", "APP_BRIDGE_NAME", l7_bridge_name, sizeof(l7_bridge_name), "APP_BRIDGE");
|
||||||
|
MESA_load_profile_string_def("./tsgconf/main.conf", "SYSTEM", "L7_PROTOCOL_FILE", l7_proto_name, sizeof(l7_proto_name), "./tsgconf/tsg_l7_protocol.conf");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
env->l7_exdata_idx= stellar_exdata_new_index(st, "L7_PROTOCOL", stellar_exdata_free_default, NULL);
|
||||||
|
env->test_exdata_idx= stellar_exdata_new_index(st, "APP_PROTO_TEST", stellar_exdata_free_default, NULL);
|
||||||
|
if(env->l7_exdata_idx<0 || env->test_exdata_idx<0)
|
||||||
|
{
|
||||||
|
perror("GLIMPSE_DETECTOR_TEST_PLUG_INIT:stellar_session_get_ex_new_index faild!!!\n");
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
if(load_l7_protocol_mapper(l7_proto_name, env)<0)
|
||||||
|
{
|
||||||
|
perror("GLIMPSE_DETECTOR_TEST_PLUG_INIT:l7_protocol_mapper failed !!!\n");
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
env->test_app_plugin_id=stellar_session_plugin_register(st, glimpse_detector_test_ctx_new, NULL, env);
|
||||||
|
if(env->test_app_plugin_id < 0)
|
||||||
|
{
|
||||||
|
perror("GLIMPSE_DETECTOR_TEST_PLUG_INIT:stellar_plugin_register failed !!!\n");
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int tcp_topic_id=stellar_mq_get_topic_id(st, TOPIC_TCP);
|
||||||
|
int udp_topic_id=stellar_mq_get_topic_id(st, TOPIC_UDP);
|
||||||
|
if(tcp_topic_id < 0 || udp_topic_id < 0)
|
||||||
|
{
|
||||||
|
perror("GLIMPSE_DETECTOR_TEST get tcp or udp topic id failed\n");
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
stellar_session_mq_subscribe(st, tcp_topic_id, APP_TEST_ON_SESSION_MSG, env->test_app_plugin_id);
|
||||||
|
stellar_session_mq_subscribe(st, udp_topic_id, APP_TEST_ON_SESSION_MSG, env->test_app_plugin_id);
|
||||||
|
env->expect_json_topic_id = stellar_mq_create_topic(st, "EXPECT_JSON", stellar_msg_free_default, NULL);
|
||||||
|
printf("GLIMPSE_DETECTOR_TEST_PLUG_LOAD OK!\n");
|
||||||
|
|
||||||
|
return env;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" void GLIMPSE_DETECTOR_TEST_PLUG_UNLOAD(void *plugin_env)
|
||||||
|
{
|
||||||
|
struct glimpse_detector_test_plugin_env *env = (struct glimpse_detector_test_plugin_env *)plugin_env;
|
||||||
|
free(env);
|
||||||
|
printf("GLIMPSE_DETECTOR_TEST_PLUG_UNLOAD OK!\n");
|
||||||
|
return ;
|
||||||
|
}
|
||||||
@@ -7,5 +7,5 @@ exit = "APP_GLIMPSE_DETECTOR_UNLOAD"
|
|||||||
|
|
||||||
[[plugin]]
|
[[plugin]]
|
||||||
path = ""
|
path = ""
|
||||||
init = "APP_TEST_PLUG_INIT"
|
init = "GLIMPSE_DETECTOR_TEST_PLUG_LOAD"
|
||||||
exit = "APP_TEST_PLUG_DESTROY"
|
exit = "GLIMPSE_DETECTOR_TEST_PLUG_UNLOAD"
|
||||||
|
|||||||
Reference in New Issue
Block a user