--- /dev/null
+#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;
+}