#include #include #include #include #include #include #ifdef __cplusplus extern "C" { #endif #include "monitor/monitor_private.h" #include "sds/sds.h" #include "stellar/stellar.h" #ifdef __cplusplus } #endif #define TEST_PKT_DUMP_FILE_NAME "__gtest_stm_pkt_dump.pcap" static pthread_t tid; static int cmd_result_array_size; static sds *cmd_result_array; static sds *stellar_cli_exec_cmd(const char *command_str, int *result_size) { int ret; sds *cmd_res_array = NULL; char result[4096] = {}; FILE *fp = popen(command_str, "r"); if (NULL == fp) { return NULL; } ret = fread(result, 1, sizeof(result), fp); if (ret > 0) { cmd_res_array = sdssplitlen(result, strlen(result), "\r\n", 1, result_size); } pclose(fp); return cmd_res_array; } static void *async_cmd_run_thread(void *arg) { cmd_result_array = stellar_cli_exec_cmd((char *)arg, &cmd_result_array_size); return NULL; } static void async_cmd_run(char *cmd) { pthread_create(&tid, NULL, async_cmd_run_thread, cmd); usleep(100000); } static void async_cmd_wait(void) { pthread_join(tid, NULL); } // class MonitorServerMock : public testing::Test // { // public: // static struct stellar *st; // static pthread_t tid; // static int thread_count; // static int cmd_result_line_num; // static int cmd_result_array_size; // static sds *cmd_result_array; // protected: // static void SetUpTestCase() // { // printf("Gtest Stm Server: Setup Test Case Env...\n"); // st = stellar_new("./conf/stellar.toml"); // assert(st != NULL); // } // static void TearDownTestCase() // { // printf("Gtest Stm Server: Tear Down Test Case Env...\n"); // stellar_free(st); // } // }; // pthread_t MonitorServerMock::tid; // int MonitorServerMock::thread_count; // int MonitorServerMock::cmd_result_line_num; // struct stellar *MonitorServerMock::st; // int MonitorServerMock::cmd_result_array_size; // sds *MonitorServerMock::cmd_result_array; static int get_local_pcap_packet_number(const char *filename) { char cmd_buf[256] = {}; snprintf(cmd_buf, sizeof(cmd_buf), "tcpdump -r %s -n -nn -t -q | wc -l", filename); int cmd_res_size; sds *cmd_res = stellar_cli_exec_cmd(cmd_buf, &cmd_res_size); int pkt_num = 0; for (int i = 0; i < cmd_res_size; i++) { if (strstr(cmd_res[i], "reading from file") != NULL) { continue; } if (strstr(cmd_res[i], "tcpdump") != NULL) { continue; } if (isdigit(cmd_res[i][0])) { pkt_num = atoi(cmd_res[i]); } } sdsfreesplitres(cmd_res, cmd_res_size); return pkt_num; } TEST(MONITOR_PKT_DUMP_TUNNEL, arg_bpf) { char cmd_buf[256] = {}; remove(TEST_PKT_DUMP_FILE_NAME); struct stellar *st = stellar_new("./conf/stellar.toml"); ASSERT_TRUE(st != NULL); const int expect_cap_pkt_num = 5; snprintf(cmd_buf, sizeof(cmd_buf), "./stellar-dump -n -nn -g -c %d udp port 53 or udp port 443 or udp port 8246 or udp port 6620 -s0 -U -w %s", expect_cap_pkt_num, TEST_PKT_DUMP_FILE_NAME); async_cmd_run(cmd_buf); stellar_run(st); async_cmd_wait(); sdsfreesplitres(cmd_result_array, cmd_result_array_size); int actual_cap_num = get_local_pcap_packet_number(TEST_PKT_DUMP_FILE_NAME); EXPECT_EQ(expect_cap_pkt_num, actual_cap_num); stellar_free(st); } int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv); int ret = RUN_ALL_TESTS(); return ret; }