Add test case: inject IPv4 based TCP payload packet after recv C2S first payload

This commit is contained in:
luwenpeng
2024-05-21 17:39:16 +08:00
parent 3701fc7361
commit e8c61a1752
24 changed files with 580 additions and 410 deletions

View File

@@ -78,58 +78,44 @@ static int replace_file_string(const char *file, const char *old_str, const char
return 0;
}
static void expect_cmp_inject(const char *work_dir,
const char *expect_dir_abs_path, const char *expect_pcap_file,
const char *output_dir_abs_path, const char *output_pcap_file,
const char *diff_skip_pattern, const char *flow_dir)
static void expect_cmp_inject(const char *expect_pcap_file, const char *inject_pcap_file, const char *diff_skip_pattern, int idx)
{
struct stat s;
char expect_pcap_file_abs_path[1024] = {0};
char output_pcap_file_abs_path[1024] = {0};
char expect_json_file_abs_path[1024] = {0};
char output_json_file_abs_path[1024] = {0};
char diff_txt_file_abs_path[1024] = {0};
char expect_pcap_json[1024] = {0};
char inject_pcap_json[1024] = {0};
char diff_json_txt[1024] = {0};
// absulute path
snprintf(expect_pcap_file_abs_path, sizeof(expect_pcap_file_abs_path), "%s/%s", expect_dir_abs_path, expect_pcap_file);
snprintf(output_pcap_file_abs_path, sizeof(output_pcap_file_abs_path), "%s/%s", output_dir_abs_path, output_pcap_file);
snprintf(expect_json_file_abs_path, sizeof(expect_json_file_abs_path), "%s/expect_%s.json", work_dir, flow_dir);
snprintf(output_json_file_abs_path, sizeof(output_json_file_abs_path), "%s/output_%s.json", work_dir, flow_dir);
snprintf(diff_txt_file_abs_path, sizeof(diff_txt_file_abs_path), "%s/diff_%s.txt", work_dir, flow_dir);
snprintf(expect_pcap_json, sizeof(expect_pcap_json), "expect_pcap_%d.json", idx);
snprintf(inject_pcap_json, sizeof(inject_pcap_json), "inject_pcap_%d.json", idx);
snprintf(diff_json_txt, sizeof(diff_json_txt), "json_diff_%d.txt", idx);
// check pcap file size
stat(expect_pcap_file_abs_path, &s);
stat(expect_pcap_file, &s);
EXPECT_TRUE(s.st_size > 0);
stat(output_pcap_file_abs_path, &s);
stat(inject_pcap_file, &s);
EXPECT_TRUE(s.st_size > 0);
// tcpdump
printf("\033[32m tcpdump read [%s] expect pcap (%s) \033[0m\n", flow_dir, expect_pcap_file);
system_cmd("tcpdump -r %s", expect_pcap_file_abs_path);
printf("\033[32m tcpdump read expect pcap (%s) \033[0m\n", expect_pcap_file);
system_cmd("tcpdump -r %s", expect_pcap_file);
printf("\033[32m tcpdump read [%s] output pcap (%s) \033[0m\n", flow_dir, output_pcap_file);
system_cmd("tcpdump -r %s", output_pcap_file_abs_path);
printf("\033[32m tcpdump read inject pcap (%s) \033[0m\n", inject_pcap_file);
system_cmd("tcpdump -r %s", inject_pcap_file);
// tshark
system_cmd("tshark -r %s -T json | jq >> %s", expect_pcap_file_abs_path, expect_json_file_abs_path);
system_cmd("tshark -r %s -T json | jq >> %s", output_pcap_file_abs_path, output_json_file_abs_path);
system_cmd("tshark -r %s -T json | jq >> %s", expect_pcap_file, expect_pcap_json);
system_cmd("tshark -r %s -T json | jq >> %s", inject_pcap_file, inject_pcap_json);
// check json file size
stat(expect_json_file_abs_path, &s);
stat(expect_pcap_json, &s);
EXPECT_TRUE(s.st_size > 0);
stat(output_json_file_abs_path, &s);
stat(inject_pcap_json, &s);
EXPECT_TRUE(s.st_size > 0);
// diff
system_cmd("diff %s %s %s >> %s", diff_skip_pattern, expect_json_file_abs_path, output_json_file_abs_path, diff_txt_file_abs_path);
system_cmd("diff %s %s %s >> %s", diff_skip_pattern, expect_pcap_json, inject_pcap_json, diff_json_txt);
// check diff file size
stat(diff_txt_file_abs_path, &s);
stat(diff_json_txt, &s);
EXPECT_TRUE(s.st_size == 0);
}
extern int inject_packet_main(int argc, char **argv);
extern int packet_injector_main(int argc, char **argv);
static int args_len(const char **args)
{
@@ -147,67 +133,41 @@ void packet_injector_test_frame_run(struct packet_injector_case *test)
printf("\033[32mTest: %s\033[0m\n", test->descriptor);
printf("\033[32m ============================================= \033[0m\n");
char config_file_abs_path[1024] = {0};
char input_dir_abs_path[1024] = {0};
char output_dir_abs_path[1024] = {0};
char expect_dir_abs_path[1024] = {0};
char log_dir_abs_path[1024] = {0};
// absulute path
snprintf(config_file_abs_path, sizeof(config_file_abs_path), "%s/conf/stellar.toml", test->work_dir);
snprintf(input_dir_abs_path, sizeof(input_dir_abs_path), "%s/input/", test->work_dir);
snprintf(output_dir_abs_path, sizeof(output_dir_abs_path), "%s/output/", test->work_dir);
snprintf(expect_dir_abs_path, sizeof(expect_dir_abs_path), "%s/expect/", test->work_dir);
snprintf(log_dir_abs_path, sizeof(log_dir_abs_path), "%s/log/", test->work_dir);
// create directory
char dumpfile_dir[1024] = {0};
snprintf(dumpfile_dir, sizeof(dumpfile_dir), "%s/input/", test->work_dir);
system_cmd("rm -rf %s", test->work_dir);
system_cmd("mkdir -p %s", input_dir_abs_path);
system_cmd("mkdir -p %s", output_dir_abs_path);
system_cmd("mkdir -p %s", expect_dir_abs_path);
system_cmd("mkdir -p %s", log_dir_abs_path);
system_cmd("mkdir -p %s", dumpfile_dir);
system_cmd("mkdir -p %s/log/", test->work_dir);
// copy file to work directory
if (test->c2s_expect_pcap)
for (int i = 0; i < MAX_COMPARISON; i++)
{
system_cmd("cp %s/%s %s", test->input_prefix, test->c2s_expect_pcap, expect_dir_abs_path);
if (test->compares[i].expect_pcap)
{
system_cmd("cp %s/%s %s", test->input_prefix, test->compares[i].expect_pcap, test->work_dir);
}
}
if (test->s2c_expect_pcap)
{
system_cmd("cp %s/%s %s", test->input_prefix, test->s2c_expect_pcap, expect_dir_abs_path);
}
system_cmd("cp %s/%s %s", test->input_prefix, test->input_pcap, input_dir_abs_path);
system_cmd("cp %s/%s %s", test->input_prefix, test->input_pcap, dumpfile_dir);
system_cmd("cp -r conf %s/", test->work_dir);
system_cmd("cp packet_injector %s/", test->work_dir);
// replace config file
char temp[2048] = {0};
snprintf(temp, sizeof(temp), "dumpfile_dir = \"%s\"", input_dir_abs_path);
EXPECT_TRUE(replace_file_string(config_file_abs_path, "mode = marsio", "mode = dumpfile") == 0);
EXPECT_TRUE(replace_file_string(config_file_abs_path, "dumpfile_dir = \"/tmp/dumpfile/\"", temp) == 0);
// run packet injector
char cwd[1024];
char cwd[2048] = {0};
char temp[2048] = {0};
getcwd(cwd, sizeof(cwd));
chdir(test->work_dir);
inject_packet_main(args_len(test->packet_injector_cmd), (char **)test->packet_injector_cmd);
snprintf(temp, sizeof(temp), "dumpfile_dir = \"%s\"", dumpfile_dir);
EXPECT_TRUE(replace_file_string("./conf/stellar.toml", "mode = marsio", "mode = dumpfile") == 0);
EXPECT_TRUE(replace_file_string("./conf/stellar.toml", "dumpfile_dir = \"/tmp/dumpfile/\"", temp) == 0);
packet_injector_main(args_len(test->packet_injector_cmd), (char **)test->packet_injector_cmd);
// compare pcap
if (test->c2s_output_pcap && test->c2s_expect_pcap)
for (int i = 0; i < MAX_COMPARISON; i++)
{
system_cmd("mv %s/%s %s", test->work_dir, test->c2s_output_pcap, output_dir_abs_path);
expect_cmp_inject(test->work_dir,
expect_dir_abs_path, test->c2s_expect_pcap,
output_dir_abs_path, test->c2s_output_pcap,
test->diff_skip_pattern, "C2S");
}
if (test->s2c_output_pcap && test->s2c_expect_pcap)
{
system_cmd("mv %s/%s %s", test->work_dir, test->s2c_output_pcap, output_dir_abs_path);
expect_cmp_inject(test->work_dir,
expect_dir_abs_path, test->s2c_expect_pcap,
output_dir_abs_path, test->s2c_output_pcap,
test->diff_skip_pattern, "S2C");
if (test->compares[i].expect_pcap && test->compares[i].inject_pcap)
{
expect_cmp_inject(test->compares[i].expect_pcap, test->compares[i].inject_pcap, test->diff_skip_pattern, i + 1);
}
}
// clean work directory