Add proc_app_info childs list unit test 27/223427/3
authorBaumann <a.baumann@samsung.com>
Thu, 9 Jan 2020 08:29:44 +0000 (09:29 +0100)
committerAgnieszka Baumann <a.baumann@samsung.com>
Fri, 7 Feb 2020 13:33:20 +0000 (14:33 +0100)
Change-Id: I61859321333ce5e56a837e7536b290b15fca1706

src/proc-stat/include/proc-main.h
src/proc-stat/proc-main.c
tests/CMakeLists.txt
tests/cmocka_test_child_pid.c [new file with mode: 0644]

index 1747688..a11a721 100644 (file)
@@ -84,7 +84,9 @@ void proc_set_group(pid_t ownerpid, pid_t childpid, char *pkgname);
 int proc_get_state(int type, pid_t pid, char *buf, int len);
 
 #ifdef _UNIT_TEST
+void proc_app_info_add_child_pid(struct proc_app_info *pai, pid_t pid);
 void proc_remove_app_info(struct proc_app_info *pai);
+void proc_app_info_remove_child_pid(struct proc_app_info *pai, pid_t pid);
 #endif
 
 #endif /*__PROC_MAIN_H__ */
index 7d384b9..012f95a 100644 (file)
@@ -358,7 +358,7 @@ on_error:
        return -ENOMEM;
 }
 
-static void proc_app_info_add_child_pid(struct proc_app_info *pai, pid_t pid)
+EXPORT_TEST void proc_app_info_add_child_pid(struct proc_app_info *pai, pid_t pid)
 {
        _cleanup_free_ char *runtime_app_info_path = NULL;
        int ret;
@@ -832,7 +832,7 @@ struct proc_app_info *proc_add_app_info(const char *appid,
        return pai;
 }
 
-static void proc_app_info_remove_child_pid(struct proc_app_info *pai, pid_t pid)
+EXPORT_TEST void proc_app_info_remove_child_pid(struct proc_app_info *pai, pid_t pid)
 {
        _cleanup_free_ char *runtime_app_info_path = NULL;
        int ret;
index 3d5cf97..a302b0a 100644 (file)
@@ -31,6 +31,7 @@ ADD_EXECUTABLE(cmocka_proc_add_app_info cmocka_proc_add_app_info.c)
 ADD_EXECUTABLE(cmocka_dbus_get_memory_lists cmocka_dbus_get_memory_lists.c)
 ADD_EXECUTABLE(cmocka_dbus_get_cpu_lists cmocka_dbus_get_cpu_lists.c)
 ADD_EXECUTABLE(cmocka-file-helper cmocka-file-helper.c)
+ADD_EXECUTABLE(cmocka_test_child_pid cmocka_test_child_pid.c)
 
 PKG_CHECK_MODULES(CMOCKA REQUIRED cmocka)
 
@@ -88,6 +89,11 @@ SET_TARGET_PROPERTIES(cmocka-file-helper PROPERTIES COMPILE_FLAGS
 TARGET_LINK_LIBRARIES(cmocka-file-helper resourced_shared_test cmocka dlog gio-2.0 gobject-2.0 glib-2.0
        "-Wl,--wrap=fopen,--wrap=fputs,--wrap=fscanf,--wrap=fgets,--wrap=fopen64,--wrap=vfscanf,--wrap=fscanf64,--wrap=__isoc99_fscanf,--wrap=asprintf -O0")
 
+SET_TARGET_PROPERTIES(cmocka_test_child_pid PROPERTIES COMPILE_FLAGS
+       "-I${INCLUDE_COMMON_DIR} -I${INCLUDE_PUBLIC_DIR} -I${COMMON_SOURCE_DIR} -I${PROC-STAT_SOURCE_DIR}/include -I${PROC-STAT_SOURCE_DIR} -I${RESOURCED_SOURCE_DIR} -I${MEMORY_SOURCE_DIR} -I${SWAP_SOURCE_DIR} -I${MODULES_SOURCE_DIR} -I${FREEZER_SOURCE_DIR} -I${FREEZER_SOURCE_DIR}/include -I${HEART_SOURCE_DIR} -I${CPU_SOURCE_DIR} -I${VIP_SOURCE_DIR} -I${BLOCK_SOURCE_DIR} -I/usr/include/dlog ${GLIB2_CFLAGS} ${UNIT_TESTS_CFLAGS}")
+TARGET_LINK_LIBRARIES(cmocka_test_child_pid resourced_shared_test cmocka dlog gio-2.0 gobject-2.0 glib-2.0
+       "-Wl,--wrap=fread_int,--wrap=fread_uint,--wrap=fread_ulong,--wrap=g_slist_prepend,--wrap=g_slist_remove -O0 -g -ggdb")
+
 # add unit test to test target
 ADD_TEST(core cmocka-core)
 ADD_TEST(file-helper cmocka-file-helper)
diff --git a/tests/cmocka_test_child_pid.c b/tests/cmocka_test_child_pid.c
new file mode 100644 (file)
index 0000000..2a42ab3
--- /dev/null
@@ -0,0 +1,558 @@
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <setjmp.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <cmocka.h>
+#include <assert.h>
+#include <gio/gio.h>
+
+#include "macro.h"
+#include "file-helper.h"
+#include "notifier.h"
+#include "proc-monitor.h"
+#include "proc-main.h"
+
+GSList *__real_g_slist_prepend(GSList *list, gpointer data);
+GSList *__real_g_slist_remove(GSList *list, gconstpointer data);
+
+int __wrap_fread_uint(const char *path, u_int32_t *number)
+{
+       return 0;
+}
+
+int __wrap_fread_int(const char *path, int32_t *number)
+{
+       return 0;
+}
+
+int __wrap_fread_ulong(const char *path, unsigned long *number)
+{
+       return 0;
+}
+
+GSList *main_list = NULL;
+
+GSList * __wrap_g_slist_prepend(GSList *proc_app_list, gpointer pai)
+{
+       int wrap_prepend = mock_type(int);
+
+       if (wrap_prepend == 1) {
+               main_list =  __real_g_slist_prepend(main_list, pai);
+               return main_list;
+       } else if (wrap_prepend == 2) {
+               return __real_g_slist_prepend(proc_app_list, pai);
+       }
+
+       return NULL;
+}
+
+static void test_proc_app_info_add_child_pid(void **state)
+{
+       /*
+        * pai:
+        *      -> appid: "proc1"
+        *      -> main_pid: 1
+        *      -> childs: 3, 2, 1
+        *  => ppi:
+        *              pkgname: "proc1",
+        *              svc_list
+        */
+
+       will_return(__wrap_g_slist_prepend, 2);
+       will_return(__wrap_g_slist_prepend, 2);
+       will_return(__wrap_g_slist_prepend, 1);
+       pid_t pid = 1;
+       struct proc_app_info *pai = proc_add_app_info("proc1", "proc1", 1, 1, 0, PROC_TYPE_SERVICE, PROC_STATE_BACKGROUND);
+
+       will_return(__wrap_g_slist_prepend, 2);
+       proc_app_info_add_child_pid(pai, pid);
+
+       will_return(__wrap_g_slist_prepend, 2);
+       proc_app_info_add_child_pid(pai, 2);
+
+       will_return(__wrap_g_slist_prepend, 2);
+       proc_app_info_add_child_pid(pai, 3);
+       proc_app_info_add_child_pid(pai, 3); //the same pid - won't be added to childs
+
+       assert(g_slist_length(main_list) == 1);
+
+       /*
+        * pai:
+        *      -> appid: "proc2"
+        *      -> main_pid: 2
+        *      -> childs: 6, 5, 4
+        *  => ppi:
+        *              pkgname: "proc2",
+        *              svc_list
+        *
+        * pai:
+        *      -> appid: "proc1"
+        *      -> main_pid: 1
+        *      -> childs: 3, 2, 1
+        *  => ppi:
+        *              pkgname: "proc1",
+        *              svc_list
+        */
+
+       /* adding different appid, pkgid and pid */
+       will_return(__wrap_g_slist_prepend, 2);
+       will_return(__wrap_g_slist_prepend, 2);
+       will_return(__wrap_g_slist_prepend, 1);
+       pid = 2;
+       pai = proc_add_app_info("proc2", "proc2", 2, 1, 0, PROC_TYPE_SERVICE, PROC_STATE_BACKGROUND);
+
+       will_return(__wrap_g_slist_prepend, 2);
+       proc_app_info_add_child_pid(pai, 4);
+
+       will_return(__wrap_g_slist_prepend, 2);
+       proc_app_info_add_child_pid(pai, 5);
+
+       will_return(__wrap_g_slist_prepend, 2);
+       proc_app_info_add_child_pid(pai, 6);
+       proc_app_info_add_child_pid(pai, 6); //the same pid - won't be added to childs
+
+       assert(g_slist_length(main_list) == 2);
+
+       /*
+        *      pai:
+        *      -> appid: "proc2"
+        *      -> main_pid: 3
+        *      -> childs: 9, 8, 7
+        *  => ppi:
+        *              pkgname: "proc2",
+        *              svc_list
+        *
+        * pai:
+        *      -> appid: "proc2"
+        *      -> main_pid: 2
+        *      -> childs: 6, 5, 4
+        *  => ppi:
+        *              pkgname: "proc2",
+        *              svc_list
+        *
+        * pai:
+        *      -> appid: "proc1"
+        *      -> main_pid: 1
+        *      -> childs: 3, 2, 1
+        *  => ppi:
+        *              pkgname: "proc1",
+        *              svc_list
+        */
+
+       /* adding different pid, the same pkgname */
+       will_return(__wrap_g_slist_prepend, 2);
+       will_return(__wrap_g_slist_prepend, 1);
+       pid = 3;
+       pai = proc_add_app_info("proc2", "proc2", 3, 1, 0, PROC_TYPE_SERVICE, PROC_STATE_BACKGROUND);
+
+       will_return(__wrap_g_slist_prepend, 2);
+       proc_app_info_add_child_pid(pai, 7);
+
+       will_return(__wrap_g_slist_prepend, 2);
+       proc_app_info_add_child_pid(pai, 8);
+
+       will_return(__wrap_g_slist_prepend, 2);
+       proc_app_info_add_child_pid(pai, 9);
+       proc_app_info_add_child_pid(pai, 9); //the same pid - won't be added to childs
+
+       assert(g_slist_length(main_list) == 3);
+
+       /*
+        * pai:
+        * -> appid: "proc3"
+        * -> main_pid: 4
+        * -> childs: 12, 11, 10
+        *  => ppi:
+        *              pkgname: "proc3",
+        *              svc_list
+
+        *      pai:
+        *      -> appid: "proc2"
+        *      -> main_pid: 3
+        *      -> childs: 9, 8, 7
+        *  => ppi:
+        *              pkgname: "proc2",
+        *              svc_list
+        *
+        * pai:
+        *      -> appid: "proc2"
+        *      -> main_pid: 2
+        *      -> childs: 6, 5, 4
+        *  => ppi:
+        *              pkgname: "proc2",
+        *              svc_list
+        *
+        * pai:
+        *      -> appid: "proc1"
+        *      -> main_pid: 1
+        *      -> childs: 3, 2, 1
+        *  => ppi:
+        *              pkgname: "proc1",
+        *              svc_list
+        */
+
+       /* adding diffenent appid, pkgid and pid */
+       will_return(__wrap_g_slist_prepend, 2);
+       will_return(__wrap_g_slist_prepend, 2);
+       will_return(__wrap_g_slist_prepend, 1);
+       pid = 4;
+       pai = proc_add_app_info("proc3", "proc3", 4, 1, 0, PROC_TYPE_SERVICE, PROC_STATE_BACKGROUND);
+
+       will_return(__wrap_g_slist_prepend, 2);
+       proc_app_info_add_child_pid(pai, 10);
+
+       will_return(__wrap_g_slist_prepend, 2);
+       proc_app_info_add_child_pid(pai, 11);
+
+       will_return(__wrap_g_slist_prepend, 2);
+       proc_app_info_add_child_pid(pai, 12);
+       proc_app_info_add_child_pid(pai, 12); //the same pid - won't be added to childs
+
+       assert(g_slist_length(main_list) == 4);
+
+       /*
+        * pai:
+        * -> appid: "proc4"
+        * -> main_pid: 5
+        * -> childs: 15, 14, 13
+        *  => ppi:
+        *              pkgname: "proc4",
+        *              svc_list
+        *
+        * pai:
+        * -> appid: "proc3"
+        * -> main_pid: 4
+        * -> childs: 12, 11, 10
+        *  => ppi:
+        *              pkgname: "proc3",
+        *              svc_list
+        *
+        *      pai:
+        *      -> appid: "proc2"
+        *      -> main_pid: 3
+        *      -> childs: 9, 8, 7
+        *  => ppi:
+        *              pkgname: "proc2",
+        *              svc_list
+        *
+        * pai:
+        *      -> appid: "proc2"
+        *      -> main_pid: 2
+        *      -> childs: 6, 5, 4
+        *  => ppi:
+        *              pkgname: "proc2",
+        *              svc_list
+        *
+        * pai:
+        *      -> appid: "proc1"
+        *      -> main_pid: 1
+        *      -> childs: 3, 2, 1
+        *  => ppi:
+        *              pkgname: "proc1",
+        *              svc_list
+        */
+
+       /* adding different pid */
+       will_return(__wrap_g_slist_prepend, 2);
+       will_return(__wrap_g_slist_prepend, 2);
+       will_return(__wrap_g_slist_prepend, 1);
+       pid = 5;
+       pai = proc_add_app_info("proc4", "proc4", 5, 1, 0, PROC_TYPE_SERVICE, PROC_STATE_BACKGROUND);
+
+       will_return(__wrap_g_slist_prepend, 2);
+       proc_app_info_add_child_pid(pai, 13);
+
+       will_return(__wrap_g_slist_prepend, 2);
+       proc_app_info_add_child_pid(pai, 14);
+
+       will_return(__wrap_g_slist_prepend, 2);
+       proc_app_info_add_child_pid(pai, 15);
+       proc_app_info_add_child_pid(pai, 15); //the same pid - won't be added to childs
+
+       assert(g_slist_length(main_list) == 5);
+
+       /*
+        * pai:
+        * -> appid: "proc4"
+        * -> main_pid: 6
+        * -> childs: 18, 17, 16
+        *  => ppi:
+        *              pkgname: "proc4",
+        *              svc_list
+        *
+        * pai:
+        * -> appid: "proc4"
+        * -> main_pid: 5
+        * -> childs: 15, 14, 13
+        *  => ppi:
+        *              pkgname: "proc4",
+        *              svc_list
+        *
+        * pai:
+        * -> appid: "proc3"
+        * -> main_pid: 4
+        * -> childs: 12, 11, 10
+        *  => ppi:
+        *              pkgname: "proc3",
+        *              svc_list
+        *
+        *      pai:
+        *      -> appid: "proc2"
+        *      -> main_pid: 3
+        *      -> childs: 9, 8, 7
+        *  => ppi:
+        *              pkgname: "proc2",
+        *              svc_list
+        *
+        * pai:
+        *      -> appid: "proc2"
+        *      -> main_pid: 2
+        *      -> childs: 6, 5, 4
+        *  => ppi:
+        *              pkgname: "proc2",
+        *              svc_list
+        *
+        * pai:
+        *      -> appid: "proc1"
+        *      -> main_pid: 1
+        *      -> childs: 3, 2, 1
+        *  => ppi:
+        *              pkgname: "proc1",
+        *              svc_list
+        */
+
+       /* adding different pid */
+       will_return(__wrap_g_slist_prepend, 2);
+       will_return(__wrap_g_slist_prepend, 1);
+       pid = 6;
+       pai = proc_add_app_info("proc4", "proc4", 6, 1, 0, PROC_TYPE_SERVICE, PROC_STATE_BACKGROUND);
+
+       will_return(__wrap_g_slist_prepend, 2);
+       proc_app_info_add_child_pid(pai, 16);
+
+       will_return(__wrap_g_slist_prepend, 2);
+       proc_app_info_add_child_pid(pai, 17);
+
+       will_return(__wrap_g_slist_prepend, 2);
+       proc_app_info_add_child_pid(pai, 18);
+       proc_app_info_add_child_pid(pai, 18); //the same pid - won't be added to childs
+
+       assert(g_slist_length(main_list) == 6);
+
+       /*
+        * pai:
+        * -> appid: "proc4"
+        * -> main_pid: 7
+        * -> childs: 21, 20, 19
+        *  => ppi:
+        *              pkgname: "proc5",
+        *              svc_list
+        *
+        * pai:
+        * -> appid: "proc4"
+        * -> main_pid: 6
+        * -> childs: 18, 17, 16
+        *  => ppi:
+        *              pkgname: "proc4",
+        *              svc_list
+        *
+        * pai:
+        * -> appid: "proc4"
+        * -> main_pid: 5
+        * -> childs: 15, 14, 13
+        *  => ppi:
+        *              pkgname: "proc4",
+        *              svc_list
+        *
+        * pai:
+        * -> appid: "proc3"
+        * -> main_pid: 4
+        * -> childs: 12, 11, 10
+        *  => ppi:
+        *              pkgname: "proc3",
+        *              svc_list
+        *
+        *      pai:
+        *      -> appid: "proc2"
+        *      -> main_pid: 3
+        *      -> childs: 9, 8, 7
+        *  => ppi:
+        *              pkgname: "proc2",
+        *              svc_list
+        *
+        * pai:
+        *      -> appid: "proc2"
+        *      -> main_pid: 2
+        *      -> childs: 6, 5, 4
+        *  => ppi:
+        *              pkgname: "proc2",
+        *              svc_list
+        *
+        * pai:
+        *      -> appid: "proc1"
+        *      -> main_pid: 1
+        *      -> childs: 3, 2, 1
+        *  => ppi:
+        *              pkgname: "proc1",
+        *              svc_list
+        */
+
+       /* adding different appid and pkgid */
+       will_return(__wrap_g_slist_prepend, 2);
+       will_return(__wrap_g_slist_prepend, 2);
+       will_return(__wrap_g_slist_prepend, 1);
+       pid = 7;
+       pai = proc_add_app_info("proc4", "proc5", 7, 1, 0, PROC_TYPE_SERVICE, PROC_STATE_BACKGROUND);
+
+       will_return(__wrap_g_slist_prepend, 2);
+       proc_app_info_add_child_pid(pai, 19);
+
+       will_return(__wrap_g_slist_prepend, 2);
+       proc_app_info_add_child_pid(pai, 20);
+
+       will_return(__wrap_g_slist_prepend, 2);
+       proc_app_info_add_child_pid(pai, 21);
+       proc_app_info_add_child_pid(pai, 21); //the same pid - won't be added to childs
+
+       assert(g_slist_length(main_list) == 7);
+
+       g_slist_free(main_list);
+       main_list = NULL;
+       assert(g_slist_length(main_list) == 0);
+}
+
+GSList * __wrap_g_slist_remove(GSList *proc_app_list, gconstpointer pai)
+{
+       int wrap_remove = mock_type(int);
+
+       if (wrap_remove == 1) {
+               main_list = __real_g_slist_remove(main_list, pai);
+               return main_list;
+       } else if (wrap_remove == 2) {
+               return  __real_g_slist_remove(proc_app_list, pai);
+       }
+
+       return NULL;
+}
+
+static void test_proc_app_remove_everything(void **state) {
+
+       /*
+        * pai:
+        *      -> appid: "proc6"
+        *      -> main_pid: 1
+        *      -> childs: 2, 1
+        *  => ppi:
+        *              pkgname: "proc6",
+        *              svc_list
+        */
+
+       will_return(__wrap_g_slist_prepend, 2);
+       will_return(__wrap_g_slist_prepend, 2);
+       will_return(__wrap_g_slist_prepend, 1);
+
+       pid_t pid = 1;
+       struct proc_app_info *pai = proc_add_app_info("proc6", "proc6", pid, 1, 0, PROC_TYPE_SERVICE, PROC_STATE_BACKGROUND);
+
+       will_return(__wrap_g_slist_prepend, 2);
+       proc_app_info_add_child_pid(pai, 1);
+
+       will_return(__wrap_g_slist_prepend, 2);
+       proc_app_info_add_child_pid(pai, 2);
+
+       /*
+        * pai:
+        *      -> appid: "proc8"
+        *      -> main_pid: 3
+        *      -> childs: 4, 3
+        *  => ppi:
+        *              pkgname: "proc8",
+        *              svc_list
+        *
+        * pai:
+        *      -> appid: "proc6"
+        *      -> main_pid: 1
+        *      -> childs: 2, 1
+        *  => ppi:
+        *              pkgname: "proc6",
+        *              svc_list
+        */
+
+       will_return(__wrap_g_slist_prepend, 2);
+       will_return(__wrap_g_slist_prepend, 2);
+       will_return(__wrap_g_slist_prepend, 1);
+
+       pid = 3;
+       struct proc_app_info *pai2 = proc_add_app_info("proc8", "proc8", pid, 1, 0, PROC_TYPE_SERVICE, PROC_STATE_BACKGROUND);
+
+       will_return(__wrap_g_slist_prepend, 2);
+       proc_app_info_add_child_pid(pai2, 3);
+
+       will_return(__wrap_g_slist_prepend, 2);
+       proc_app_info_add_child_pid(pai2, 4);
+
+       //deleting only childs from the first element of main_list
+       assert(g_slist_length(main_list) == 2);
+
+       assert(strcmp((((struct proc_app_info *)g_slist_nth_data(main_list, 0))->appid), "proc8") == 0);
+       assert(g_slist_length((((struct proc_app_info *)g_slist_nth_data(main_list, 0))->childs)) == 2);
+
+       will_return(__wrap_g_slist_remove, 2);
+       proc_app_info_remove_child_pid(pai2, 4);
+       assert(g_slist_length((((struct proc_app_info *)g_slist_nth_data(main_list, 0))->childs)) == 1);
+
+       will_return(__wrap_g_slist_remove, 2);
+       proc_app_info_remove_child_pid(pai2, 3);
+       assert(g_slist_length((((struct proc_app_info *)g_slist_nth_data(main_list, 0))->childs)) == 0);
+
+       //deleting only childs from the second element of main_list
+       assert(g_slist_length(main_list) == 2);
+
+       assert(strcmp((((struct proc_app_info *)g_slist_nth_data(main_list, 1))->appid), "proc6") == 0);
+       assert(g_slist_length((((struct proc_app_info *)g_slist_nth_data(main_list, 0))->childs)) == 0);
+       assert(g_slist_length((((struct proc_app_info *)g_slist_nth_data(main_list, 1))->childs)) == 2);
+
+       will_return(__wrap_g_slist_remove, 2);
+       proc_app_info_remove_child_pid(pai, 2);
+
+       assert(g_slist_length((((struct proc_app_info *)g_slist_nth_data(main_list, 0))->childs)) == 0);
+       assert(g_slist_length((((struct proc_app_info *)g_slist_nth_data(main_list, 1))->childs)) == 1);
+
+       will_return(__wrap_g_slist_remove, 2);
+       proc_app_info_remove_child_pid(pai, 1);
+
+       assert(g_slist_length((((struct proc_app_info *)g_slist_nth_data(main_list, 0))->childs)) == 0);
+       assert(g_slist_length((((struct proc_app_info *)g_slist_nth_data(main_list, 1))->childs)) == 0);
+
+       //deleting main_list
+       will_return(__wrap_g_slist_remove, 2);
+       will_return(__wrap_g_slist_remove, 2);
+       will_return(__wrap_g_slist_remove, 1);
+       proc_remove_app_info(pai2);
+
+       assert(g_slist_length(main_list) == 1);
+
+       will_return(__wrap_g_slist_remove, 2);
+       will_return(__wrap_g_slist_remove, 2);
+       will_return(__wrap_g_slist_remove, 1);
+       proc_remove_app_info(pai);
+
+       assert(g_slist_length(main_list) == 0);
+}
+
+int main(int argc, char* argv[])
+{
+       const struct CMUnitTest tests[] = {
+               cmocka_unit_test(test_proc_app_remove_everything),
+               cmocka_unit_test(test_proc_app_info_add_child_pid),
+       };
+
+       int test = cmocka_run_group_tests(tests, NULL, NULL);
+
+       will_return(__wrap_g_slist_remove, 2);
+       will_return(__wrap_g_slist_remove, 2);
+
+       return test;
+}