Refactoring source code privilege checker 78/312678/22
authortranthanhtung2001 <tran.tung@samsung.com>
Thu, 13 Jun 2024 02:39:10 +0000 (09:39 +0700)
committerDariusz Michaluk <d.michaluk@samsung.com>
Tue, 16 Jul 2024 12:51:14 +0000 (12:51 +0000)
Change-Id: Iaeac4e860925ad5221284cba4d018214b061d1da
Signed-off-by: tranthanhtung2001 <tran.tung@samsung.com>
test/privilege-checker.c
test/tc-common.c
test/tc-common.h
test/tc-privilege-db-manager.c
test/tc-privilege-info.c

index d24b76d..6ae7fbd 100644 (file)
@@ -187,6 +187,7 @@ int main()
 
        __tcinfo(function, "privilege_db_manager_get_package_list_by_privacy");
        __test_privilege_db_manager_get_package_list_by_privacy();
+
        //#########################################################################
 
        //#########################################################################
index 24d41ab..d2d5eaf 100644 (file)
@@ -321,6 +321,32 @@ void __print_result(char type, int result)
        __color_to_origin();
 }
 
+bool __check_the_return_code(char type, int expected_result, int ret)
+{
+       bool isDiff = false;
+       if (expected_result != ret) {
+               printf_red("TEST RESULT: FAILED\n");
+               fail_cnt++;
+               isDiff = true;
+       } else {
+               printf_green("TEST RESULT: SUCCESS\n");
+               success_cnt++;
+       }
+
+       if (isDiff == true) {
+               if (type != 'n') {
+                       printf("EXPECTED: %s, RETURNED : %s\n",
+                               __get_result_string(type, expected_result),
+                               __get_result_string(type, ret));
+               } else {
+                       printf("EXPECTED: %d, RETURNED : %d\n", expected_result, ret);
+               }
+       }
+       __print_line();
+
+       return isDiff;
+}
+
 #define INI_PATH "/etc/privilege-checker.ini"
 #define INI_KEYWORD_PROFILE "General:Profile"
 tizen_profile_t _get_tizen_profile()
index 973ec4c..89dea88 100644 (file)
@@ -87,7 +87,7 @@ void __print_glist(GList *glist);
 bool __check_result(int result);
 bool __count_result(int result);
 void __print_result(char type, int result);
-
+bool __check_the_return_code(char type, int expected_result, int ret);
 
 void __set_privacy_test_pkg_info();
 void __unset_privacy_test_pkg_info();
index 228d380..63e5dd9 100644 (file)
@@ -4,6 +4,63 @@
 #include <privilege_db_manager.h>
 #include <tc-common.h>
 
+void __check_result_code_and_list(
+       GList *expected_list,
+       GList *achieved_list,
+       int expected_result,
+       int achieved_result,
+       bool is_single_data_at_node,
+       char type)
+{
+       bool isFalse = __check_the_return_code(type, expected_result, achieved_result);
+
+       if (isFalse)
+               return;
+
+       GList *l = expected_list;
+       GList *l1 = achieved_list;
+
+       if (g_list_length(expected_list) != g_list_length(achieved_list)) {
+               printf("EXPECTED LIST LENGTH DONT MATCH ACHIEVED LIST LENGTH\n");
+               fail_cnt++;
+               success_cnt--;
+               return;
+       }
+
+       while (l != NULL && l1 != NULL) {
+
+               printf("--- Check content: \n");
+
+               if (is_single_data_at_node) {
+                       printf("\texpected data: %s\n\tactual data: %s\n", (char*)l->data, (char*)l1->data);
+
+                       if (strcmp((char*)l->data, (char*)l1->data)) {
+                               printf("---> INVALID\n");
+                               fail_cnt++;
+                               success_cnt--;
+                               return;
+                       }
+               } else {
+                       privilege_privacy_info_s *p = l->data;
+                       privilege_privacy_info_s *p1 = l1->data;
+
+                       printf("\texpected privilege name: %s\n\tactual privilege name: %s\n", p->privilege_name, p1->privilege_name);
+                       printf("\texpected privacy name: %s\n\tactual privacy name: %s\n", p->privacy_name, p1->privacy_name);
+
+                       if (strcmp(p->privilege_name, p1->privilege_name) || strcmp(p->privacy_name, p1->privacy_name)) {
+                               printf("---> INVALID\n");
+                               fail_cnt++;
+                               success_cnt--;
+                               return;
+                       }
+               }
+
+               printf("---> VALID\n");
+               l = l->next;
+               l1 = l1->next;
+       }
+}
+
 static void __free_privilege_list(GList * privilege_list)
 {
        GList *l = NULL;
@@ -14,7 +71,6 @@ static void __free_privilege_list(GList * privilege_list)
        }
 }
 
-
 void __test_privilege_db_manager_get_privilege_level_info_list()
 {
        GList *privilege_list = NULL;
@@ -642,9 +698,6 @@ void __test_privilege_db_manager_get_same_privacy_grouped_privileges()
 
 void __test_privilege_db_manager_set_package_privacy_privilege_info()
 {
-       __print_line();
-
-       int ret = PRIVILEGE_DB_MANAGER_ERR_NONE;
        GList* privilege_list = NULL;
        privilege_privacy_info_s p;
 
@@ -656,53 +709,15 @@ void __test_privilege_db_manager_set_package_privacy_privilege_info()
        char *pkgid = "attach-panel-camera";
        char *api_version = "5.5";
 
-       printf("uid = %d\npkgid = %s\napi_version = %s\nprivilege_privacy {privilege_name = %s privacy_name = %s}\n",
-       uid, pkgid, api_version, p.privilege_name, p.privacy_name);
-
-       ret = privilege_db_manager_set_package_privacy_privilege_info(uid, pkgid, api_version, privilege_list);
-
-       printf("- EXPECTED RESULT : %d\n", PRIVILEGE_DB_MANAGER_ERR_NONE);
-       printf("- ACHIEVED RESULT : %d\n", ret);
-
-       if(ret == PRIVILEGE_DB_MANAGER_ERR_NONE){
-               printf("match\n");
-               success_cnt++;
-               printf_green("success\n");
-       }else{
-               printf("don't match\n");
-               fail_cnt++;
-               printf_red("fail\n");
-       }
-
-       __print_line();
-
-       privilege_list = NULL;
-
-       p.privilege_name = "http://tizen.org/privilege/externalstorage";
-       p.privacy_name = "http://tizen.org/privacy/storage";
-       privilege_list = g_list_append(privilege_list, &p);
-
-       uid = 5001;
-       pkgid = "cert-svc-ui";
-       api_version = "3.0";
+       __tcinfo(goal, POS_TC_PREFIX "set package privacy privilege info");
 
        printf("uid = %d\npkgid = %s\napi_version = %s\nprivilege_privacy {privilege_name = %s privacy_name = %s}\n",
-       uid, pkgid, api_version, p.privilege_name, p.privacy_name);
+               uid, pkgid, api_version, p.privilege_name, p.privacy_name);
 
-       ret = privilege_db_manager_set_package_privacy_privilege_info(uid, pkgid, api_version, privilege_list);
+       int ret = privilege_db_manager_set_package_privacy_privilege_info(uid, pkgid, api_version, privilege_list);
 
-       printf("- EXPECTED RESULT : %d\n", PRIVILEGE_DB_MANAGER_ERR_NONE);
-       printf("- ACHIEVED RESULT : %d\n", ret);
+       __check_the_return_code('d', PRIVILEGE_DB_MANAGER_ERR_NONE, ret);
 
-       if(ret == PRIVILEGE_DB_MANAGER_ERR_NONE){
-               printf("match\n");
-               success_cnt++;
-               printf_green("success\n");
-       }else{
-               printf("don't match");
-               fail_cnt++;
-               printf_red("fail\n");
-       }
        privilege_list = NULL;
 }
 
@@ -710,85 +725,65 @@ void __test_privilege_db_manager_unset_package_privilege_info()
 {
        __print_line();
 
-       int ret = PRIVILEGE_DB_MANAGER_ERR_NONE;
-
        uid_t uid = 5001;
        char *pkgid = "attach-panel-camera";
 
+       __tcinfo(goal, POS_TC_PREFIX "unset package privacy privilege info");
        printf("uid = %d\npkgid = %s\n", uid, pkgid);
 
-       ret = privilege_db_manager_unset_package_privilege_info(uid, pkgid);
-
-       printf("- EXPECTED RESULT : %d\n", PRIVILEGE_DB_MANAGER_ERR_NONE);
-       printf("- ACHIEVED RESULT : %d\n", ret);
-
-       if(ret == PRIVILEGE_DB_MANAGER_ERR_NONE){
-               printf("match\n");
-               success_cnt++;
-               printf_green("success\n");
-       }else{
-               printf("don't match");
-               fail_cnt++;
-               printf_red("fail\n");
-       }
-
-       __print_line();
-
-       uid = 5001;
-       pkgid = "jadjasffjf";
-
-       printf("uid = %d\npkgid = %s\n", uid, pkgid);
-
-       ret = privilege_db_manager_unset_package_privilege_info(uid, pkgid);
-
-       printf("- EXPECTED RESULT : %d\n", PRIVILEGE_DB_MANAGER_ERR_NONE);
-       printf("- ACHIEVED RESULT : %d\n", ret);
+       int ret = privilege_db_manager_unset_package_privilege_info(uid, pkgid);
 
-       if(ret == PRIVILEGE_DB_MANAGER_ERR_NONE){
-               printf("match\n");
-               success_cnt++;
-               printf_green("success\n");
-       }else{
-               printf("don't match");
-               fail_cnt++;
-               printf_red("fail\n");
-       }
+       __check_the_return_code('d', PRIVILEGE_DB_MANAGER_ERR_NONE, ret);
 }
 
 void __test_privilege_db_manager_is_privacy_requestable()
 {
        __print_line();
 
-       int expected_ret = PRIVILEGE_DB_MANAGER_ERR_NONE;
        bool expected_is_privacy_requestable = false;
        bool is_privacy_requestable;
+       privilege_privacy_info_s p;
 
        uid_t uid = 5001;
        char *pkgid = "cert-svc-ui";
+       char *api_version = "3.0";
        char *privilege = "http://tizen.org/privilege/externalstorage";
        privilege_manager_package_type_e prvmgr_package_type_e = PRVMGR_PACKAGE_TYPE_CORE;
 
-       printf("uid = %d\npkgid = %s\nprivilege = %s\nprvmgr_package_type_e %d\n",
-       uid, pkgid, privilege, prvmgr_package_type_e);
+       p.privilege_name = privilege;
+       p.privacy_name = "http://tizen.org/privacy/storage";
+       privilege_list = g_list_append(privilege_list, &p);
 
-       printf("- EXPECTED RESULT:\nexpected_ret = %d\nexpected_is_privacy_requestable = %d\n",
-       expected_ret, expected_is_privacy_requestable);
+       privilege_db_manager_set_package_privacy_privilege_info(uid, pkgid, api_version, privilege_list);
 
-       int ret = privilege_db_manager_is_privacy_requestable(uid, pkgid, privilege,
-       prvmgr_package_type_e, &is_privacy_requestable);
+       __tcinfo(goal, NEG_TC_PREFIX "privacy requestable is false");
 
-       printf("- ACHIEVED RESULT:\nret = %d\nis_privacy_requestable = %d\n",
-       ret, is_privacy_requestable);
+       printf("uid = %d\npkgid = %s\nprivilege = %s\nprvmgr package type e = %d\n",
+               uid, pkgid, privilege, prvmgr_package_type_e);
 
-       if(ret == expected_ret && is_privacy_requestable == expected_is_privacy_requestable){
-               printf("match\n");
-               success_cnt++;
-               printf_green("success\n");
-       }else{
-               printf("don't match");
-               fail_cnt++;
-               printf_red("fail\n");
+       int ret = privilege_db_manager_is_privacy_requestable(
+               uid,
+               pkgid,
+               privilege,
+               prvmgr_package_type_e,
+               &is_privacy_requestable);
+
+       bool isDiff = __check_the_return_code('d', PRIVILEGE_DB_MANAGER_ERR_NONE, ret);
+
+       if (!isDiff) {
+               printf("EXPECTED REQUESTABLE: %d, RETURNED REQUESTABLE : %d\n",
+                       expected_is_privacy_requestable, is_privacy_requestable);
+
+               if (expected_is_privacy_requestable == is_privacy_requestable) {
+                       printf_green("RESULT MATCH\n");
+               } else {
+                       printf_red("RESULT DONT MATCH\n");
+                       fail_cnt++;
+                       success_cnt--;
+               }
        }
+
+       privilege_list = NULL;
 }
 
 void __test_privilege_db_manager_get_all_privacy_package_list()
@@ -799,52 +794,20 @@ void __test_privilege_db_manager_get_all_privacy_package_list()
        GList *expected_package_list = NULL;
 
        uid_t uid = 5001;
-       int expected_ret = PRIVILEGE_DB_MANAGER_ERR_NONE;
-       char *pkgname = NULL;
+       char *pkg0 = "cert-svc-ui";
+       char *pkg1 = "org.test.nativeapp_3";
 
-       pkgname = "cert-svc-ui";
-       expected_package_list = g_list_append(expected_package_list, pkgname);
+       expected_package_list = g_list_append(expected_package_list, pkg0);
+       expected_package_list = g_list_append(expected_package_list, pkg1);
 
-       printf("uid = %d\n", uid);
+       __tcinfo(goal, POS_TC_PREFIX "get all privacy package list");
 
-       printf("- EXPECTED RESULT:\nexpected_ret = %d\n", expected_ret);
-       printf("package_list :\n");
-       __print_glist(expected_package_list);
+       printf("uid = %d\n", uid);
 
        int ret = privilege_db_manager_get_all_privacy_package_list(uid, &package_list);
 
-       printf("- ACHIEVED RESULT:\nret = %d\n", ret);
-       printf("package_list :\n");
-       __print_glist(package_list);
-
-       bool success = true;
-       if(ret != expected_ret){
-               success = false;
-       }
-
-       GList *l = package_list;
-       GList *l1 = expected_package_list;
-
-       while (l != NULL && l1 != NULL && success)
-       {
-               if(strcmp((char*)l->data, (char*)l1->data)){
-                       printf("don't match\n");
-                       success = false;
-                       break;
-               }
-               l = l->next;
-               l1 = l1->next;
-       }
+       __check_result_code_and_list(expected_package_list, package_list, PRIVILEGE_DB_MANAGER_ERR_NONE, ret, true, 'd');
 
-       if(success){
-               printf("match\n");
-               success_cnt++;
-               printf_green("success\n");
-       }else{
-               printf("don't match\n");
-               fail_cnt++;
-               printf_red("fail\n");
-       }
        g_list_free_full(package_list, free);
 }
 
@@ -856,57 +819,18 @@ void __test_privilege_db_manager_get_privacy_list_by_pkgid()
        GList *expected_privacy_list = NULL;
 
        uid_t uid = 5001;
-       char *expected_ret = __get_result_string('d', PRIVILEGE_DB_MANAGER_ERR_NONE);
-
        char *pkgid = "cert-svc-ui";
        char *privacy_name = "http://tizen.org/privacy/storage";
        expected_privacy_list = g_list_append(expected_privacy_list, privacy_name);
 
-       printf("uid = %d\npkgid = %s\n", uid, pkgid);
+       __tcinfo(goal, POS_TC_PREFIX "get all privacy list by pkgid");
 
-       printf("- EXPECTED RESULT:\nexpected_ret = %s\n", expected_ret);
-       printf("privacy_list :\n");
-       __print_glist(expected_privacy_list);
+       printf("uid = %d\npkgid = %s\n", uid, pkgid);
 
        int ret = privilege_db_manager_get_privacy_list_by_pkgid(uid, pkgid, &privacy_list);
 
-       char *str_ret = __get_result_string('d', ret);
-       printf("- ACHIEVED RESULT:\nret = %s\n", str_ret);
-       printf("privacy_list :\n");
-       __print_glist(privacy_list);
-
-       bool success = true;
-       if(strcmp(expected_ret, str_ret)){
-               success = false;
-       }
-
-       if(g_list_length(expected_privacy_list) != g_list_length(privacy_list) && success){
-               success = false;
-       }
-
-       GList *l = privacy_list;
-       GList *l1 = expected_privacy_list;
-
-       while (l != NULL && l1 != NULL && success)
-       {
-               if(strcmp((char*)l->data, (char*)l1->data)){
-                       printf("don't match\n");
-                       success = false;
-                       break;
-               }
-               l = l->next;
-               l1 = l1->next;
-       }
+       __check_result_code_and_list(expected_privacy_list, privacy_list, PRIVILEGE_DB_MANAGER_ERR_NONE, ret, true , 'd');
 
-       if(success){
-               printf("match\n");
-               success_cnt++;
-               printf_green("success\n");
-       }else{
-               printf("don't match\n");
-               fail_cnt++;
-               printf_red("fail\n");
-       }
        privilege_db_manager_list_free(privacy_list);
 }
 
@@ -922,49 +846,13 @@ void __test_privilege_db_manager_get_package_list_by_privacy()
        char *pkgid = "cert-svc-ui";
        expected_package_list = g_list_append(expected_package_list, pkgid);
 
-       char *expected_ret = __get_result_string('d', PRIVILEGE_DB_MANAGER_ERR_NONE);
+       __tcinfo(goal, POS_TC_PREFIX "get package list by privacy");
 
        printf("uid = %d\nprivacy = %s\n", uid, privacy);
 
-       printf("- EXPECTED RESULT:\nexpected_ret = %s\n", expected_ret);
-       printf("package_list :\n");
-       __print_glist(expected_package_list);
-
        int ret = privilege_db_manager_get_package_list_by_privacy(uid, privacy, &package_list);
 
-       char *str_ret = __get_result_string('d', ret);
-       printf("- ACHIEVED RESULT:\nret = %s\n", str_ret);
-       printf("package_list :\n");
-       __print_glist(package_list);
-
-       bool success = true;
-       if(strcmp(expected_ret, str_ret)){
-               success = false;
-       }
-
-       if(g_list_length(expected_package_list) != g_list_length(package_list) && success){
-               success = false;
-       }
-
-       GList *l = expected_package_list;
-       GList *l1 = package_list;
-       while (l != NULL && l1 != NULL && success)
-       {
-               if(strcmp((char*)l->data, (char*)l1->data)){
-                       success = false;
-                       break;
-               }
-               l = l->next;
-               l1 = l1->next;
-       }
+       __check_result_code_and_list(expected_package_list, package_list, PRIVILEGE_DB_MANAGER_ERR_NONE, ret, true, 'd');
 
-       if(success){
-               printf("match\n");
-               success_cnt++;
-               printf_green("success\n");
-       }else{
-               fail_cnt++;
-               printf_red("fail\n");
-       }
        privilege_db_manager_list_free(package_list);
 }
index 9aaf1be..b0ba8cb 100644 (file)
@@ -1010,21 +1010,6 @@ int __check_privilege_info_foreach_list_callback(const char *name, void* user_da
        return PRVMGR_ERR_NO_DATA;
 }
 
-void __check_the_return_code(int expected_result, int ret)
-{
-       if (expected_result != ret) {
-               printf_red("TEST RESULT: FAILED\n");
-               fail_cnt++;
-       } else {
-               printf_green("TEST RESULT: SUCCESS\n");
-               success_cnt++;
-       }
-       printf("EXPECTED: %s, RETURNED : %s\n",
-       __get_result_string('m', expected_result),
-       __get_result_string('m', ret));
-       __print_line();
-}
-
 void __test_privilege_info_foreach_privilege_group_list_by_pkgid()
 {
        GList* group_list_user_data = NULL;
@@ -1048,7 +1033,7 @@ void __test_privilege_info_foreach_privilege_group_list_by_pkgid()
                __check_privilege_info_foreach_list_callback,
                group_list_user_data);
 
-       __check_the_return_code(expected_result, ret);
+       __check_the_return_code('m', expected_result, ret);
        gfree(group_list_user_data);
 }
 
@@ -1061,6 +1046,8 @@ void __test_privilege_info_foreach_privilege_group_list_by_pkgid_and_uid()
        char *privilege_group_1 = "http://tizen.org/privilege/group/group0";
        char *privilege_group_2 = "http://tizen.org/privilege/group/group4";
 
+       __tcinfo(goal, POS_TC_PREFIX "foreach privilege group list by pkgid and uid");
+
        printf("pkgid : %s\n", pkgid);
        printf("privilege group : %s\n", privilege_group_1);
        printf("privilege group : %s\n", privilege_group_2);
@@ -1076,7 +1063,7 @@ void __test_privilege_info_foreach_privilege_group_list_by_pkgid_and_uid()
                __check_privilege_info_foreach_list_callback,
                group_list_user_data);
 
-       __check_the_return_code(expected_result, ret);
+       __check_the_return_code('m', expected_result, ret);
        gfree(group_list_user_data);
 }
 
@@ -1089,6 +1076,8 @@ void __test_privilege_info_foreach_privilege_list_by_pkgid_and_privilege_group()
        char *privilege_group = "http://tizen.org/privilege/group/group0";
        char *privilege = "http://tizen.org/privilege/camera";
 
+       __tcinfo(goal, POS_TC_PREFIX "foreach privilege list by pkgid and privilege group");
+
        printf("pkgid : %s\n", pkgid);
        printf("privilege_group : %s\n", privilege_group);
        printf("privilege : %s\n", privilege);
@@ -1103,11 +1092,13 @@ void __test_privilege_info_foreach_privilege_list_by_pkgid_and_privilege_group()
                __check_privilege_info_foreach_list_callback,
                privilege_list);
 
-       __check_the_return_code(expected_result, ret);
+       __check_the_return_code('m', expected_result, ret);
        gfree(privilege_list);
 
        privilege = "http://tizen.org/privilege/audiorecorder";
 
+       __tcinfo(goal, POS_TC_PREFIX "foreach privilege list by pkgid and privilege group");
+
        printf("pkgid : %s\n", pkgid);
        printf("privilege_group : %s\n", privilege_group);
        printf("privilege : %s\n", privilege);
@@ -1122,7 +1113,7 @@ void __test_privilege_info_foreach_privilege_list_by_pkgid_and_privilege_group()
                __check_privilege_info_foreach_list_callback,
                privilege_list);
 
-       __check_the_return_code(expected_result, ret);
+       __check_the_return_code('m', expected_result, ret);
        gfree(privilege_list);
 }
 
@@ -1135,6 +1126,8 @@ void __test_privilege_info_foreach_privilege_list_by_pkgid_and_privilege_group_a
        char *privilege_group = "http://tizen.org/privilege/group/group0";
        char *privilege = "http://tizen.org/privilege/camera";
 
+       __tcinfo(goal, POS_TC_PREFIX "foreach privilege list by pkgid and privilege group and uid");
+
        printf("pkgid : %s\n", pkgid);
        printf("privilege_group : %s\n", privilege_group);
        printf("privilege : %s\n", privilege);
@@ -1150,11 +1143,13 @@ void __test_privilege_info_foreach_privilege_list_by_pkgid_and_privilege_group_a
                __check_privilege_info_foreach_list_callback,
                privilege_list);
 
-       __check_the_return_code(expected_result, ret);
+       __check_the_return_code('m', expected_result, ret);
        gfree(privilege_list);
 
        privilege = "http://tizen.org/privilege/d2d.datasharing";
 
+       __tcinfo(goal, POS_TC_PREFIX "foreach privilege list by pkgid and privilege group and uid");
+
        printf("pkgid : %s\n", pkgid);
        printf("privilege_group : %s\n", privilege_group);
        printf("privilege : %s\n", privilege);
@@ -1170,6 +1165,6 @@ void __test_privilege_info_foreach_privilege_list_by_pkgid_and_privilege_group_a
                __check_privilege_info_foreach_list_callback,
                privilege_list);
 
-       __check_the_return_code(expected_result, ret);
+       __check_the_return_code('m', expected_result, ret);
        gfree(privilege_list);
 }