This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
stellar-stellar/test/lpi_plus/gtest_lpip_main.cpp
2024-11-27 10:29:23 +08:00

165 lines
4.2 KiB
C++

/*
* 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>
#include <gtest/gtest.h>
#include "stellar/stellar.h"
#include "cJSON.h"
#include "gtest_lpip.h"
struct gtest_json_result
{
cJSON *test_json_root;
cJSON *expect_json_root;
int result_count;
};
static struct gtest_json_result *gtest_result_new(const char *expect_json_path)
{
struct gtest_json_result *para = (struct gtest_json_result *)calloc(1, sizeof(struct gtest_json_result));
if(expect_json_path==NULL)
{
para->expect_json_root=cJSON_CreateArray();
return para;
}
FILE *file = fopen(expect_json_path, "rb");
if(file)
{
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;
}
int compare_ret = cJSON_Compare(para->expect_json_root, para->test_json_root, 0);
if (compare_ret != 1)
{
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);
cJSON *t_load = para->expect_json_root->child, *t_test = para->test_json_root->child;
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);
return -1;
}
t_load = t_load->next;
t_test = t_test->next;
}
}
return compare_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;
}
/**********************************************
* GTEST MAIN *
**********************************************/
#include "stellar/packet.h"
extern "C" void gtest_lpip_on_packet(struct packet *pkt, struct module *mod);
int main(int argc, char ** argv)
{
::testing::InitGoogleTest(&argc, argv);
//EXPECT_EQ(argc, 2);
printf("Usage: ./[gtest_main] [/path/to/expect_json]\n");
char *expect_json_path=argv[1];
struct gtest_json_result *g_test_para = gtest_result_new(expect_json_path);
struct stellar *st=stellar_new("./conf/stellar.toml");
struct module_manager *mod_mgr=stellar_get_module_manager(st);
struct module *pkt_mgr_mod=module_manager_get_module(mod_mgr, PACKET_MANAGER_MODULE_NAME);
struct packet_manager *pkt_mgr=module_to_packet_manager(pkt_mgr_mod);
struct module *gtest_lpip_mod=gtest_lpip_module_init(mod_mgr);
packet_manager_register_node(pkt_mgr, "LPI_PLUS", PACKET_STAGE_FORWARD, PKT_TAG_KEY_IPPROTO, PKT_TAG_VAL_IPPROTO_TCP | PKT_TAG_VAL_IPPROTO_UDP, gtest_lpip_on_packet, gtest_lpip_mod);
stellar_test_result_setup();
EXPECT_TRUE(st!=NULL);
stellar_run(st);
stellar_free(st);
gtest_lpip_module_exit(mod_mgr, gtest_lpip_mod);
char *test_result_json=stellar_test_result_json_export();
g_test_para->test_json_root=cJSON_Parse(test_result_json);
free(test_result_json);
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);
stellar_test_result_cleanup();
return ::testing::Test::HasFailure() ? 1 : 0;
}