2024-08-20 19:01:06 +08:00
|
|
|
/*
|
|
|
|
|
* author:yangwei
|
|
|
|
|
* create time:2021-8-21
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
2024-08-22 11:58:07 +08:00
|
|
|
#include <gtest/gtest.h>
|
2024-08-20 19:01:06 +08:00
|
|
|
|
|
|
|
|
#include "stellar/stellar.h"
|
|
|
|
|
#include "stellar/session.h"
|
|
|
|
|
#include "stellar/stellar_mq.h"
|
|
|
|
|
|
2024-08-22 11:58:07 +08:00
|
|
|
#include "cjson/cJSON.h"
|
2024-08-20 19:01:06 +08:00
|
|
|
|
|
|
|
|
|
2024-08-22 11:58:07 +08:00
|
|
|
struct gtest_json_result
|
2024-08-20 19:01:06 +08:00
|
|
|
{
|
2024-08-22 11:58:07 +08:00
|
|
|
cJSON *test_json_root;
|
|
|
|
|
cJSON *expect_json_root;
|
|
|
|
|
int result_count;
|
2024-08-20 19:01:06 +08:00
|
|
|
};
|
|
|
|
|
|
2024-08-22 11:58:07 +08:00
|
|
|
static struct gtest_json_result *gtest_result_new(const char *expect_json_path)
|
2024-08-20 19:01:06 +08:00
|
|
|
{
|
2024-08-22 11:58:07 +08:00
|
|
|
struct gtest_json_result *para = (struct gtest_json_result *)calloc(1, sizeof(struct gtest_json_result));
|
|
|
|
|
para->test_json_root = cJSON_CreateArray();
|
2024-08-20 19:01:06 +08:00
|
|
|
|
2024-08-22 11:58:07 +08:00
|
|
|
FILE *file = fopen(expect_json_path, "rb");
|
|
|
|
|
if(file)
|
2024-08-20 19:01:06 +08:00
|
|
|
{
|
2024-08-22 11:58:07 +08:00
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
|
long filesize = ftell(file);
|
|
|
|
|
rewind(file);
|
|
|
|
|
char *buffer = (char *)calloc(filesize + 1, 1);
|
|
|
|
|
fread(buffer, 1, filesize, file);
|
2024-08-20 19:01:06 +08:00
|
|
|
|
2024-08-22 11:58:07 +08:00
|
|
|
para->expect_json_root=cJSON_Parse(buffer);
|
2024-08-20 19:01:06 +08:00
|
|
|
|
2024-08-22 11:58:07 +08:00
|
|
|
free(buffer);
|
|
|
|
|
fclose(file);
|
2024-08-20 19:01:06 +08:00
|
|
|
}
|
|
|
|
|
para->result_count=1;//count start from 1
|
2024-08-22 11:58:07 +08:00
|
|
|
return para;
|
2024-08-20 19:01:06 +08:00
|
|
|
}
|
|
|
|
|
|
2024-08-22 11:58:07 +08:00
|
|
|
static int gtest_result_compare(struct gtest_json_result *para)
|
2024-08-20 19:01:06 +08:00
|
|
|
{
|
2024-08-22 11:58:07 +08:00
|
|
|
if(cJSON_GetArraySize(para->test_json_root)!=cJSON_GetArraySize(para->expect_json_root))
|
2024-08-20 19:01:06 +08:00
|
|
|
{
|
2024-08-22 11:58:07 +08:00
|
|
|
char *load_json_str = cJSON_Print(para->expect_json_root);
|
2024-08-20 19:01:06 +08:00
|
|
|
printf("LOAD Raw:\n%s\n", load_json_str);
|
|
|
|
|
free(load_json_str);
|
2024-08-22 11:58:07 +08:00
|
|
|
char *result_json_str = cJSON_Print(para->test_json_root);
|
2024-08-20 19:01:06 +08:00
|
|
|
printf("TEST Raw:\n%s\n", result_json_str);
|
|
|
|
|
free(result_json_str);
|
2024-08-22 11:58:07 +08:00
|
|
|
return -1;
|
2024-08-20 19:01:06 +08:00
|
|
|
}
|
2024-08-22 11:58:07 +08:00
|
|
|
int compare_ret = cJSON_Compare(para->expect_json_root, para->test_json_root, 0);
|
|
|
|
|
if (compare_ret != 1)
|
2024-08-20 19:01:06 +08:00
|
|
|
{
|
2024-08-22 11:58:07 +08:00
|
|
|
char *load_json_str = cJSON_Print(para->expect_json_root);
|
2024-08-20 19:01:06 +08:00
|
|
|
printf("LOAD Raw:\n%s\n", load_json_str);
|
|
|
|
|
free(load_json_str);
|
2024-08-22 11:58:07 +08:00
|
|
|
char *result_json_str = cJSON_Print(para->test_json_root);
|
2024-08-20 19:01:06 +08:00
|
|
|
printf("TEST Raw:\n%s\n", result_json_str);
|
|
|
|
|
free(result_json_str);
|
|
|
|
|
|
2024-08-22 11:58:07 +08:00
|
|
|
cJSON *t_load = para->expect_json_root->child, *t_test = para->test_json_root->child;
|
2024-08-20 19:01:06 +08:00
|
|
|
while (t_load != NULL)
|
|
|
|
|
{
|
|
|
|
|
// print first diff item, then return;
|
|
|
|
|
if(1 != cJSON_Compare(t_load, t_test, 0))
|
|
|
|
|
{
|
|
|
|
|
load_json_str = cJSON_Print(t_load);
|
|
|
|
|
printf("LOAD Diff:\n%s\n", load_json_str);
|
|
|
|
|
free(load_json_str);
|
|
|
|
|
result_json_str = cJSON_Print(t_test);
|
|
|
|
|
printf("TEST Diff:\n%s\n", result_json_str);
|
|
|
|
|
free(result_json_str);
|
2024-08-22 11:58:07 +08:00
|
|
|
return -1;
|
2024-08-20 19:01:06 +08:00
|
|
|
}
|
|
|
|
|
t_load = t_load->next;
|
|
|
|
|
t_test = t_test->next;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-22 11:58:07 +08:00
|
|
|
return compare_ret;
|
|
|
|
|
}
|
2024-08-20 19:01:06 +08:00
|
|
|
|
2024-08-22 11:58:07 +08:00
|
|
|
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;
|
2024-08-20 19:01:06 +08:00
|
|
|
}
|
|
|
|
|
|
2024-08-22 11:58:07 +08:00
|
|
|
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;
|
|
|
|
|
}
|
2024-08-20 19:01:06 +08:00
|
|
|
|
|
|
|
|
/**********************************************
|
|
|
|
|
* GTEST MAIN *
|
|
|
|
|
**********************************************/
|
|
|
|
|
|
2024-08-22 11:58:07 +08:00
|
|
|
int main(int argc, char ** argv)
|
|
|
|
|
{
|
|
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(argc, 2);
|
2024-08-20 19:01:06 +08:00
|
|
|
|
2024-08-22 11:58:07 +08:00
|
|
|
if(argc != 2)
|
|
|
|
|
{
|
|
|
|
|
printf("Invalid Argument!!!\n Usage: ./[gtest_main] [/path/to/expect_json]\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2024-08-20 19:01:06 +08:00
|
|
|
|
2024-08-22 11:58:07 +08:00
|
|
|
char *expect_json_path=argv[1];
|
|
|
|
|
struct gtest_json_result *g_test_para = gtest_result_new(expect_json_path);
|
2024-08-21 12:16:19 +08:00
|
|
|
|
|
|
|
|
struct stellar *st=stellar_new("./conf/stellar.toml", "./plugin/spec.toml", "./conf/log.toml");
|
2024-08-22 11:58:07 +08:00
|
|
|
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);
|
|
|
|
|
|
2024-08-21 12:16:19 +08:00
|
|
|
stellar_run(st);
|
|
|
|
|
stellar_free(st);
|
2024-08-20 19:01:06 +08:00
|
|
|
|
2024-08-22 11:58:07 +08:00
|
|
|
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);
|
2024-08-20 19:01:06 +08:00
|
|
|
|
2024-08-22 11:58:07 +08:00
|
|
|
gtest_result_free(g_test_para);
|
|
|
|
|
|
|
|
|
|
return ::testing::Test::HasFailure() ? 1 : 0;
|
2024-08-20 19:01:06 +08:00
|
|
|
}
|