summaryrefslogtreecommitdiff
path: root/test/verify_policy_test.cpp
diff options
context:
space:
mode:
authorfengweihao <[email protected]>2024-08-02 10:55:58 +0800
committerfengweihao <[email protected]>2024-08-02 10:55:58 +0800
commit41caf21f43df13785d2e1bee814a3005abc9b43e (patch)
treeccf37a5032c4bd6fe990bde93107c76556589e03 /test/verify_policy_test.cpp
parent60cd4283dbe1ca374cfb672fbc0c3e48fde9fa30 (diff)
修复Verify Policy中fqdn entry的命中路径错误,增加Verify Policy测试用例代码框架v4.0.22-20240802
Diffstat (limited to 'test/verify_policy_test.cpp')
-rw-r--r--test/verify_policy_test.cpp308
1 files changed, 308 insertions, 0 deletions
diff --git a/test/verify_policy_test.cpp b/test/verify_policy_test.cpp
new file mode 100644
index 0000000..c76a1ef
--- /dev/null
+++ b/test/verify_policy_test.cpp
@@ -0,0 +1,308 @@
+/*************************************************************************
+ > File Name:
+ > Author:
+ > Mail:
+ > Created Time: 2020��05��28�� ������ 19ʱ21��37��
+ ************************************************************************/
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <cjson/cJSON.h>
+#include <sys/stat.h>
+#include <gtest/gtest.h>
+
+#include <MESA/MESA_prof_load.h>
+
+#include "verify_policy.h"
+#include "utils.h"
+
+cJSON *verify_policy_result;
+cJSON *verify_policy_request;
+
+struct verify_policy * g_verify_proxy = NULL;
+extern cJSON *get_library_search_query(const char *data, ssize_t data_len);
+extern cJSON *get_verify_policy_query(const char *data, ssize_t data_len, int thread_id);
+
+int load_json_file_system_cmd(const char *load_json_file, const char *run_json_file)
+{
+ char command[1024] = {0};
+ snprintf(command, sizeof(command), "cp ./resource/%s ./resource/%s", load_json_file, run_json_file);
+ system(command);
+ sleep(2);
+ return 0;
+}
+
+static char *select_hit_policy_result_item(int gtest_id)
+{
+ if(verify_policy_result == NULL || verify_policy_result->type!=cJSON_Array)
+ {
+ return NULL;
+ }
+
+ int foreach=0;
+ char *hit_policy_result = NULL;
+ cJSON *subitem = NULL;
+
+ for (subitem = verify_policy_result->child; subitem != NULL; subitem = subitem->next)
+ {
+ if(foreach == gtest_id)
+ {
+ hit_policy_result = cJSON_PrintUnformatted(subitem);
+ break;
+ }
+ foreach++;
+ }
+ return hit_policy_result;
+}
+
+static char *select_hit_policy_request_item(int gtest_id)
+{
+ if(verify_policy_request == NULL || verify_policy_request->type!=cJSON_Array)
+ {
+ return NULL;
+ }
+
+ int foreach=0;
+ char *hit_policy_request = NULL;
+ cJSON *subitem = NULL;
+
+ for (subitem = verify_policy_request->child; subitem != NULL; subitem = subitem->next)
+ {
+ if(foreach == gtest_id)
+ {
+ hit_policy_request = cJSON_PrintUnformatted(subitem);
+ break;
+ }
+ foreach++;
+ }
+ return hit_policy_request;
+}
+
+TEST(LibrarySearch, HitFqdnEntry)
+{
+ const char *cm_http_request = "{\"ip\":null,\"fqdn\":\"www.126.com\",\"vsys_id\":1}";
+ const char *expect_result="{\"code\":200,\"msg\":\"Success\",\"data\":{\"hit_library\":[{\"entry_id\":1,\"tag_ids\":\"1\"}]},\"success\":true}";
+
+ cJSON *result_json = get_library_search_query(cm_http_request, strlen(cm_http_request));
+ ASSERT_TRUE(result_json != NULL);
+
+ char *hit_policy_list = cJSON_PrintUnformatted(result_json);
+ ASSERT_TRUE(hit_policy_list != NULL);
+
+ int equal = strncasecmp(hit_policy_list, expect_result, strlen(expect_result));
+ EXPECT_EQ(equal, 0);
+
+ cJSON_Delete(result_json);
+ FREE(&hit_policy_list);
+
+ cm_http_request = "{\"ip\":null,\"fqdn\":\"www.baidu.com\",\"vsys_id\":1}";
+ expect_result="{\"code\":200,\"msg\":\"Success\",\"data\":{\"hit_library\":[{\"entry_id\":2,\"tag_ids\":\"2,3\"}]},\"success\":true}";
+
+ result_json = get_library_search_query(cm_http_request, strlen(cm_http_request));
+ ASSERT_TRUE(result_json != NULL);
+
+ hit_policy_list = cJSON_PrintUnformatted(result_json);
+ ASSERT_TRUE(hit_policy_list != NULL);
+
+ equal = strncasecmp(hit_policy_list, expect_result, strlen(expect_result));
+ EXPECT_EQ(equal, 0);
+
+ cJSON_Delete(result_json);
+ FREE(&hit_policy_list);
+
+ cm_http_request = "{\"ip\":null,\"fqdn\":\"www.qq.com\",\"vsys_id\":1}";
+ expect_result="{\"code\":200,\"msg\":\"Success\",\"data\":{\"hit_library\":[{\"entry_id\":4,\"tag_ids\":\"4,5,6\"}]},\"success\":true}";
+
+ result_json = get_library_search_query(cm_http_request, strlen(cm_http_request));
+ ASSERT_TRUE(result_json != NULL);
+
+ hit_policy_list = cJSON_PrintUnformatted(result_json);
+ ASSERT_TRUE(hit_policy_list != NULL);
+
+ equal = strncasecmp(hit_policy_list, expect_result, strlen(expect_result));
+ EXPECT_EQ(equal, 0);
+
+ cJSON_Delete(result_json);
+ FREE(&hit_policy_list);
+}
+
+TEST(LibrarySearch, HitIpEntry)
+{
+ const char *cm_http_request = "{\"ip\":\"192.168.1.1\",\"fqdn\":null,\"vsys_id\":1}";
+ const char *expect_result="{\"code\":200,\"msg\":\"Success\",\"data\":{\"hit_library\":[{\"entry_id\":1,\"tag_ids\":\"11\"}]},\"success\":true}";
+
+ cJSON *result_json = get_library_search_query(cm_http_request, strlen(cm_http_request));
+ ASSERT_TRUE(result_json != NULL);
+
+ char *hit_policy_list = cJSON_PrintUnformatted(result_json);
+ ASSERT_TRUE(hit_policy_list != NULL);
+
+ int equal = strncasecmp(hit_policy_list, expect_result, strlen(expect_result));
+ EXPECT_EQ(equal, 0);
+
+ cJSON_Delete(result_json);
+ FREE(&hit_policy_list);
+
+ cm_http_request ="{\"ip\":\"192.168.1.2\",\"fqdn\":null,\"vsys_id\":1}";
+ expect_result="{\"code\":200,\"msg\":\"Success\",\"data\":{\"hit_library\":[{\"entry_id\":2,\"tag_ids\":\"12,13\"}]},\"success\":true}";
+
+ result_json = get_library_search_query(cm_http_request, strlen(cm_http_request));
+ ASSERT_TRUE(result_json != NULL);
+
+ hit_policy_list = cJSON_PrintUnformatted(result_json);
+ ASSERT_TRUE(hit_policy_list != NULL);
+
+ equal = strncasecmp(hit_policy_list, expect_result, strlen(expect_result));
+ EXPECT_EQ(equal, 0);
+
+ cJSON_Delete(result_json);
+ FREE(&hit_policy_list);
+
+ cm_http_request = "{\"ip\":\"192.168.1.3\",\"fqdn\":null,\"vsys_id\":1}";
+ expect_result="{\"code\":200,\"msg\":\"Success\",\"data\":{\"hit_library\":[{\"entry_id\":4,\"tag_ids\":\"14,15,16\"}]},\"success\":true}";
+
+ result_json = get_library_search_query(cm_http_request, strlen(cm_http_request));
+ ASSERT_TRUE(result_json != NULL);
+
+ hit_policy_list = cJSON_PrintUnformatted(result_json);
+ ASSERT_TRUE(hit_policy_list != NULL);
+
+ equal = strncasecmp(hit_policy_list, expect_result, strlen(expect_result));
+ EXPECT_EQ(equal, 0);
+
+ cJSON_Delete(result_json);
+ FREE(&hit_policy_list);
+}
+
+TEST(VerifyPolicy, HitIpPolicy)
+{
+ char *hit_policy_request = select_hit_policy_request_item(0);
+ ASSERT_TRUE(hit_policy_request != NULL);
+ char *hit_policy_result = select_hit_policy_result_item(0);
+ ASSERT_TRUE(hit_policy_result != NULL);
+
+ cJSON *result_json = get_verify_policy_query(hit_policy_request, strlen(hit_policy_request), 1);
+ ASSERT_TRUE(result_json != NULL);
+
+ char *hit_policy_query = cJSON_PrintUnformatted(result_json);
+ ASSERT_TRUE(hit_policy_query != NULL);
+
+ int equal = strncasecmp(hit_policy_query, hit_policy_result, strlen(hit_policy_result));
+ EXPECT_EQ(equal, 0);
+
+ cJSON_Delete(result_json);
+ FREE(&hit_policy_query);
+ FREE(&hit_policy_request);
+ FREE(&hit_policy_result);
+}
+
+TEST(VerifyPolicy, HitLibraryPolicy)
+{
+ char *hit_policy_request = select_hit_policy_request_item(1);
+ ASSERT_TRUE(hit_policy_request != NULL);
+ char *hit_policy_result = select_hit_policy_result_item(1);
+ ASSERT_TRUE(hit_policy_result != NULL);
+
+ cJSON *result_json = get_verify_policy_query(hit_policy_request, strlen(hit_policy_request), 1);
+ ASSERT_TRUE(result_json != NULL);
+
+ char *hit_policy_query = cJSON_PrintUnformatted(result_json);
+ ASSERT_TRUE(hit_policy_query != NULL);
+ //printf("hit_policy_query =%s\n", hit_policy_query);
+
+ int equal = strncasecmp(hit_policy_query, hit_policy_result, strlen(hit_policy_result));
+ EXPECT_EQ(equal, 0);
+
+ cJSON_Delete(result_json);
+ FREE(&hit_policy_query);
+ FREE(&hit_policy_request);
+ FREE(&hit_policy_result);
+}
+
+static char *read_json_file(const char *filename, size_t *input_sz)
+{
+ FILE* fp=NULL;
+ struct stat file_info;
+ stat(filename, &file_info);
+ *input_sz=file_info.st_size;
+
+ fp=fopen(filename,"r");
+ if(fp==NULL)
+ {
+ return NULL;
+ }
+ char* input=(char*)malloc(*input_sz);
+ fread(input,1,*input_sz,fp);
+ fclose(fp);
+ return input;
+}
+
+cJSON *load_verify_policy_result_by_file(const char *filename, int hit_policy_result)
+{
+ size_t input_sz = 0;
+ char *input = read_json_file(filename, &input_sz);
+ if(input == NULL)
+ {
+ return NULL;
+ }
+
+ cJSON* data = cJSON_Parse(input);
+ if(data == NULL)
+ {
+ FREE(&input);
+ return NULL;
+ }
+
+ if(hit_policy_result)
+ {
+ verify_policy_result = cJSON_GetObjectItem(data,"Verify_Policy_Result");
+ }
+ else
+ {
+ verify_policy_request = cJSON_GetObjectItem(data,"Verify_Policy_Request");
+ }
+
+ FREE(&input);
+ return data;
+}
+
+int main(int argc, char ** argv)
+{
+ int ret = 0;
+ int log_level=0;
+ const char * main_profile = "./conf/verify_policy.conf";
+
+ g_verify_proxy = ALLOC(struct verify_policy, 1);
+ assert(g_verify_proxy);
+ strcpy(g_verify_proxy->name, "verify_policy");
+
+ const char *log_path="./logs/verify_policy.log";
+ MESA_load_profile_int_def(main_profile, "SYSTEM", "log_level", &log_level, LOG_FATAL);
+ g_verify_proxy->logger = log_handle_create(log_path, log_level);
+ CHECK_OR_EXIT(g_verify_proxy->logger != NULL, "Failed at init log module. Exit.");
+
+ ret = maat_table_init(g_verify_proxy, main_profile);
+ CHECK_OR_EXIT(ret == 0, "Failed at init maat module, Exit.");
+
+ const char *filename1 = "./resource/HitPolicyResult.json";
+ cJSON *data1=load_verify_policy_result_by_file(filename1, 1);
+ const char *filename2 = "./resource/HitPolicyRequest.json";
+ cJSON *data2=load_verify_policy_result_by_file(filename2, 0);
+
+ testing::InitGoogleTest(&argc, argv);
+ ret=RUN_ALL_TESTS();
+
+ if(data1 != NULL)
+ {
+ cJSON_Delete(data1);
+ }
+ if(data2 != NULL)
+ {
+ cJSON_Delete(data2);
+ }
+
+ return ret;
+}
+