feat(glimpse_detector test): enable test case

This commit is contained in:
yangwei
2024-08-21 12:16:19 +08:00
parent baefff5a16
commit 28936ec2ad
101 changed files with 1659 additions and 1783 deletions

View File

@@ -327,128 +327,27 @@ error_out:
* GTEST MAIN *
**********************************************/
char *expect_json_path=NULL;
#define RESULT_JSON_DIR_NAME "test_result_json"
#define TEST_ENV_DIR_NAME "test_env"
TEST(gtest_glimpse_detector, run) {
const char *g_test_dir=NULL;
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);
#include <limits.h>
char g_cwd[PATH_MAX];
#include <stdarg.h>
static inline void system_cmd(const char *cmd, ...)
{
char buf[4096] = {0};
va_list args;
va_start(args, cmd);
vsnprintf(buf, sizeof(buf), cmd, args);
va_end(args);
system(buf);
}
class gtest_glimpse_detector : public ::testing::Test {
protected:
char result_json_path[4096] = {0};
char pcap_path[4096] = {0};
// Constructor should be default; static members should be set externally
gtest_glimpse_detector() = default;
void SetUp() override {
chdir(g_cwd);
std::cout << "SetUpTestCase called for work_dir: " << g_test_dir<< std::endl;
system_cmd("mkdir -p ./conf/");
system_cmd("mkdir -p ./log/ ");
system_cmd("mkdir -p ./plugin");
system_cmd("mkdir -p ./tsgconf/");
system_cmd("cp %s/%s/tsg_l7_protocol.conf ./tsgconf/tsg_l7_protocol.conf", g_test_dir, TEST_ENV_DIR_NAME);
system_cmd("cp %s/%s/stellar.toml ./conf/stellar.toml", g_test_dir, TEST_ENV_DIR_NAME);
system_cmd("cp %s/%s/log.toml ./conf/log.toml", g_test_dir, TEST_ENV_DIR_NAME);
system_cmd("cp %s/%s/spec.toml ./plugin/spec.toml", g_test_dir, TEST_ENV_DIR_NAME);
// Retrieve current test info
const ::testing::TestInfo* test_info = ::testing::UnitTest::GetInstance()->current_test_info();
std::cout << "Setting up test environment for test case: "
<< test_info->test_case_name()
<< " in work_dir: " << g_test_dir << std::endl;
// Initialize paths based on test case name and arguments
memset(&g_test_para, 0, sizeof(struct app_test_para));
snprintf(result_json_path, sizeof(result_json_path),
"%s/%s/%s.json", g_test_dir, RESULT_JSON_DIR_NAME,
test_info->name());
snprintf(pcap_path, sizeof(pcap_path),
"%s/%s", g_test_dir, test_info->name());
std::cout << "result_json_path: " << result_json_path << std::endl;
std::cout << "pcap_path: " << pcap_path << std::endl;
system_cmd("tomlq -t -i '.packet_io.dumpfile_dir = \"%s\" ' ./conf/stellar.toml", pcap_path);
system_cmd("pwd");
system_cmd("cat ./conf/stellar.toml");
}
};
TEST_F(gtest_glimpse_detector, app_pcap) {
app_test_para_start(&g_test_para, result_json_path);
stellar_run(0, NULL);
EXPECT_EQ(app_test_para_finish(&g_test_para), 1);
}
TEST_F(gtest_glimpse_detector, dns_pcap) {
app_test_para_start(&g_test_para, result_json_path);
stellar_run(0, NULL);
EXPECT_EQ(app_test_para_finish(&g_test_para), 1);
}
TEST_F(gtest_glimpse_detector, mixed_pcap) {
app_test_para_start(&g_test_para, result_json_path);
stellar_run(0, NULL);
EXPECT_EQ(app_test_para_finish(&g_test_para), 1);
}
TEST_F(gtest_glimpse_detector, openvpn_pcap) {
app_test_para_start(&g_test_para, result_json_path);
stellar_run(0, NULL);
EXPECT_EQ(app_test_para_finish(&g_test_para), 1);
}
TEST_F(gtest_glimpse_detector, ppp_pcap) {
app_test_para_start(&g_test_para, result_json_path);
stellar_run(0, NULL);
EXPECT_EQ(app_test_para_finish(&g_test_para), 1);
}
TEST_F(gtest_glimpse_detector, socks_pcap) {
app_test_para_start(&g_test_para, result_json_path);
stellar_run(0, NULL);
EXPECT_EQ(app_test_para_finish(&g_test_para), 1);
}
int main(int argc, char ** argv)
{
if(argc != 2)
{
printf("Invalid Argument!!!\n Usage: ./[gtest_main] [/path/to/glimpse_detector_test_dir] \n");
printf("Invalid Argument!!!\n Usage: ./[gtest_main] [/path/to/expect_json]\n");
return -1;
}
getcwd(g_cwd, sizeof(g_cwd));
::testing::InitGoogleTest(&argc, argv);
g_test_dir=argv[1];
expect_json_path=argv[1];
return RUN_ALL_TESTS();
}