unit tests: extend proc_dbus_aul_group tests 60/220360/9
authorMaciej Slodczyk <m.slodczyk2@partner.samsung.com>
Tue, 17 Dec 2019 17:02:08 +0000 (18:02 +0100)
committerMaciej Slodczyk <m.slodczyk2@partner.samsung.com>
Tue, 21 Jan 2020 16:08:10 +0000 (17:08 +0100)
Change-Id: I6735f09b0e8f7b3a312434c709cb4ee82b86e59f
Signed-off-by: Maciej Slodczyk <m.slodczyk2@partner.samsung.com>
tests/CMakeLists.txt
tests/cmocka_proc_dbus_aul_group.c [new file with mode: 0644]

index 17c3f74..e76335a 100644 (file)
@@ -26,6 +26,7 @@ ADD_CUSTOM_TARGET(do-test ALL make test
 # build unit test
 ADD_EXECUTABLE(cmocka-core cmocka-core.c)
 ADD_EXECUTABLE(cmocka-proc-dbus-typecheck cmocka-proc-dbus-typecheck.c)
+ADD_EXECUTABLE(cmocka_proc_dbus_aul_group cmocka_proc_dbus_aul_group.c)
 
 PKG_CHECK_MODULES(CMOCKA REQUIRED cmocka)
 
@@ -48,6 +49,11 @@ SET_TARGET_PROPERTIES(cmocka-proc-dbus-typecheck PROPERTIES COMPILE_FLAGS
 TARGET_LINK_LIBRARIES(cmocka-proc-dbus-typecheck 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=proc_set_group,--wrap=g_dbus_method_invocation_get_message,--wrap=g_dbus_message_new_method_reply,--wrap=d_bus_reply_message,--wrap=g_object_unref,--wrap=g_dbus_method_invocation_return_value,--wrap=find_app_info_by_appid,--wrap=proc_get_mem_usage,--wrap=proc_get_cpu_time,--wrap=resourced_proc_status_change,--wrap=resourced_notify,--wrap=proc_set_runtime_exclude_list,--wrap=find_app_info,--wrap=proc_add_app_info,--wrap=proc_create_app_info,--wrap=proc_app_list_add_app_info,--wrap=proc_check_suspend_state,--wrap=g_dbus_method_invocation_return_error,--wrap=proc_dbus_exclude_signal_handler -O0")
 
+SET_TARGET_PROPERTIES(cmocka_proc_dbus_aul_group 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_proc_dbus_aul_group 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=proc_set_group,--wrap=proc_get_oom_score_adj,--wrap=resourced_notify -O0")
+
 # add unit test to test target
 ADD_TEST(core cmocka-core)
 ADD_DEPENDENCIES(do-test cmocka-core)
@@ -86,6 +92,8 @@ ADD_MEMORY_TESTS(lowmem-system-test ""
 ADD_MEMORY_TESTS(lowmem-dbus-test "${GLIB2_LDFLAGS}" ""
                lowmem-dbus-test.cpp lowmem-dbus-mock.cpp lowmem-dbus-env.cpp lowmem-env.cpp lowmem-env-mock.cpp
                           ../src/memory/lowmem-dbus.c)
+ADD_TEST(proc_dbus_aul_group cmocka_proc_dbus_aul_group)
+ADD_DEPENDENCIES(do-test cmocka_proc_dbus_aul_group)
 
 ADD_TEST(proc-dbus-typecheck cmocka-proc-dbus-typecheck)
 ADD_DEPENDENCIES(do-test cmocka-proc-dbus-typecheck)
diff --git a/tests/cmocka_proc_dbus_aul_group.c b/tests/cmocka_proc_dbus_aul_group.c
new file mode 100644 (file)
index 0000000..8ae8b47
--- /dev/null
@@ -0,0 +1,117 @@
+#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 "file-helper.h"
+#include "notifier.h"
+#include "proc-monitor.h"
+#include "proc-main.h"
+
+void __real_proc_set_group(pid_t ownerpid, pid_t childpid, char *pkgname);
+void __real_resourced_notify(enum notifier_type status, void *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;
+}
+
+int __wrap_proc_get_oom_score_adj(int pid, int *oom_score_adj)
+{
+       *oom_score_adj = 1000;
+       return RESOURCED_ERROR_NONE;
+}
+
+void __wrap_proc_set_group(pid_t ownerpid, pid_t childpid, char *pkgname)
+{
+       check_expected(ownerpid);
+       check_expected(childpid);
+       check_expected_ptr(pkgname);
+       __real_proc_set_group(ownerpid, childpid, pkgname);
+}
+
+void __wrap_resourced_notify(enum notifier_type status, void *data)
+{
+       check_expected(status);
+       __real_resourced_notify(status, data);
+}
+
+int APP_GROUP_notifier_cb(void *data)
+{
+       check_expected(data);
+       return 0;
+}
+
+int APP_WAKEUP_notifier_cb(void *data)
+{
+       check_expected(data);
+       return 0;
+}
+
+static void test_proc_dbus_aul_group(void **state)
+{
+       /* TODO: prerequisite: test proc_app_info list manipulation functions */
+       struct proc_app_info *one = proc_add_app_info("proc1", "proc1", 1, 0, 0, PROC_TYPE_SERVICE, PROC_STATE_BACKGROUND);
+       struct proc_app_info *two = proc_add_app_info("proc2", "proc2", 2, 0, 0, PROC_TYPE_SERVICE, PROC_STATE_BACKGROUND);
+       struct proc_app_info *three = proc_add_app_info("proc3", "proc3", 3, 0, 0, PROC_TYPE_GUI, PROC_STATE_FOREGROUND);
+
+       struct proc_status ps = {0};
+       ps.pid = 1;
+       ps.pai = one;
+
+       register_notifier(RESOURCED_NOTIFIER_APP_GROUP, APP_GROUP_notifier_cb);
+       register_notifier(RESOURCED_NOTIFIER_APP_WAKEUP, APP_WAKEUP_notifier_cb);
+
+       GVariant *params = g_variant_new("(iis)", 0, 0, NULL);
+       proc_dbus_aul_group(params);
+       g_variant_unref(params);
+
+       params = g_variant_new("(iis)", 123, 123, "appid");
+       proc_dbus_aul_group(params);
+       g_variant_unref(params);
+
+       params = g_variant_new("(iis)", 1, 2, "appid");
+       expect_value(__wrap_proc_set_group, ownerpid, 1);
+       expect_value(__wrap_proc_set_group, childpid, 2);
+       expect_string(__wrap_proc_set_group, pkgname, "appid");
+       expect_string(APP_GROUP_notifier_cb, data, "2 proc2 proc1");
+
+       expect_value(__wrap_resourced_notify, status, RESOURCED_NOTIFIER_APP_GROUP);
+
+       proc_dbus_aul_group(params);
+       g_variant_unref(params);
+       params = g_variant_new("(iis)", 1, 3, "appid");
+       expect_value(__wrap_proc_set_group, ownerpid, 1);
+       expect_value(__wrap_proc_set_group, childpid, 3);
+       expect_string(__wrap_proc_set_group, pkgname, "appid");
+       expect_string(APP_GROUP_notifier_cb, data, "3 proc3 proc1");
+       expect_memory(APP_WAKEUP_notifier_cb, data, &ps, sizeof ps);
+       expect_value(__wrap_resourced_notify, status, RESOURCED_NOTIFIER_APP_GROUP);
+       expect_value(__wrap_resourced_notify, status, RESOURCED_NOTIFIER_APP_WAKEUP);
+
+       proc_dbus_aul_group(params);
+       g_variant_unref(params);
+}
+
+int main(int argc, char* argv[])
+{
+       const struct CMUnitTest tests[] = {
+               cmocka_unit_test(test_proc_dbus_aul_group),
+       };
+       return cmocka_run_group_tests(tests, NULL, NULL);
+}