Add dbus_get_memory_lists tests 66/222966/7
authorMaciej Slodczyk <m.slodczyk2@partner.samsung.com>
Mon, 20 Jan 2020 12:16:33 +0000 (13:16 +0100)
committerMaciej Slodczyk <m.slodczyk2@partner.samsung.com>
Mon, 27 Jan 2020 12:02:18 +0000 (13:02 +0100)
Change-Id: Ib0a5678825af80d0048ecb798a890c9b35d83926
Signed-off-by: Maciej Slodczyk <m.slodczyk2@partner.samsung.com>
src/proc-stat/proc-monitor.c
tests/CMakeLists.txt
tests/cmocka_dbus_get_memory_lists.c [new file with mode: 0644]

index 94a4e23..25ac4bf 100644 (file)
@@ -324,7 +324,7 @@ failure:
        D_BUS_REPLY_NULL(invocation);
 }
 
-static void dbus_get_memory_list(GDBusMethodInvocation *invocation, GVariant *params)
+EXPORT_TEST void dbus_get_memory_list(GDBusMethodInvocation *invocation, GVariant *params)
 {
        _cleanup_app_list_close_ GSList *proc_app_list = PAL_INIT_VALUE;
        GSList *giter;
index 9776616..29cf54f 100644 (file)
@@ -28,6 +28,7 @@ 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)
 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)
 
 PKG_CHECK_MODULES(CMOCKA REQUIRED cmocka)
 
@@ -60,6 +61,11 @@ SET_TARGET_PROPERTIES(cmocka_proc_add_app_info PROPERTIES COMPILE_FLAGS
 TARGET_LINK_LIBRARIES(cmocka_proc_add_app_info 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")
 
+SET_TARGET_PROPERTIES(cmocka_dbus_get_memory_lists 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_dbus_get_memory_lists 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_dbus_method_invocation_return_value,--wrap=proc_get_mem_usage,--wrap=g_dbus_method_invocation_get_message,--wrap=g_dbus_message_new_method_reply,--wrap=d_bus_reply_message -O0")
+
 # add unit test to test target
 ADD_TEST(core cmocka-core)
 ADD_DEPENDENCIES(do-test cmocka-core)
@@ -101,6 +107,9 @@ ADD_MEMORY_TESTS(lowmem-dbus-test "${GLIB2_LDFLAGS}" ""
 ADD_TEST(proc_dbus_aul_group cmocka_proc_dbus_aul_group)
 ADD_DEPENDENCIES(do-test cmocka_proc_dbus_aul_group)
 
+ADD_TEST(dbus_get_memory_lists cmocka_dbus_get_memory_lists)
+ADD_DEPENDENCIES(do-test cmocka_dbus_get_memory_lists)
+
 ADD_TEST(proc-dbus-typecheck cmocka-proc-dbus-typecheck)
 ADD_DEPENDENCIES(do-test cmocka-proc-dbus-typecheck)
 
diff --git a/tests/cmocka_dbus_get_memory_lists.c b/tests/cmocka_dbus_get_memory_lists.c
new file mode 100644 (file)
index 0000000..f633ba2
--- /dev/null
@@ -0,0 +1,166 @@
+#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"
+
+#define PROC_USAGE_START 123456
+
+static unsigned int proc_usage_val;
+
+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;
+}
+
+void __wrap_g_dbus_method_invocation_return_value(GDBusMethodInvocation *invocation, GVariant *parameters)
+{
+       GVariantIter *iter;
+       char *appid;
+       unsigned int usage;
+
+       check_expected_ptr(invocation);
+
+       g_variant_get (parameters, "(a(su))", &iter);
+
+       while (g_variant_iter_loop(iter, "(su)", &appid, &usage)) {
+               char *tmp_str = mock_type(char *);
+               unsigned int tmp_uint = mock_type(unsigned int);
+
+               assert_int_equal(strcmp(appid, tmp_str), 0);
+               assert_int_equal(usage, tmp_uint);
+       }
+       g_variant_iter_free(iter);
+}
+
+int __wrap_proc_get_mem_usage(pid_t pid, unsigned int *usage)
+{
+       assert(usage);
+
+       *usage = mock_type(unsigned int);
+
+       return RESOURCED_ERROR_NONE;
+}
+
+void dbus_get_memory_list(GDBusMethodInvocation *invocation, GVariant *params);
+void dbus_get_memory_lists(GDBusMethodInvocation *invocation, GVariant *params);
+
+static void test_dbus_get_memory_list(void **state)
+{
+       proc_usage_val = PROC_USAGE_START;
+       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);
+
+       /* positive test */
+       will_return(__wrap_g_dbus_method_invocation_return_value, "proc3");
+       will_return(__wrap_g_dbus_method_invocation_return_value, proc_usage_val);
+       will_return(__wrap_proc_get_mem_usage, proc_usage_val++);
+       will_return(__wrap_g_dbus_method_invocation_return_value, "proc2");
+       will_return(__wrap_g_dbus_method_invocation_return_value, proc_usage_val);
+       will_return(__wrap_proc_get_mem_usage, proc_usage_val++);
+       will_return(__wrap_g_dbus_method_invocation_return_value, "proc1");
+       will_return(__wrap_g_dbus_method_invocation_return_value, proc_usage_val);
+       will_return(__wrap_proc_get_mem_usage, proc_usage_val++);
+
+       expect_value(__wrap_g_dbus_method_invocation_return_value, invocation, cast_ptr_to_largest_integral_type(0xcaca0));
+
+       dbus_get_memory_list((GDBusMethodInvocation *)0xcaca0, NULL);
+
+       proc_remove_app_info(one);
+       proc_remove_app_info(two);
+       proc_remove_app_info(three);
+}
+
+GDBusMessage *__wrap_g_dbus_method_invocation_get_message(GDBusMethodInvocation *invocation)
+{
+       check_expected_ptr(invocation);
+       return (GDBusMessage *)invocation;
+}
+
+GDBusMessage *__wrap_g_dbus_message_new_method_reply(GDBusMessage *method_call_message)
+{
+       check_expected_ptr(method_call_message);
+       return NULL;
+}
+
+resourced_ret_c __wrap_d_bus_reply_message(GDBusMessage *msg)
+{
+       check_expected_ptr(msg);
+       return RESOURCED_ERROR_NONE;
+}
+
+static void test_dbus_get_memory_lists(void **state)
+{
+       GVariant *params;
+       gpointer obj;
+
+       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);
+
+       obj = g_object_new(G_TYPE_NONE, "");
+
+       /* negative test - no params */
+       expect_value(__wrap_g_dbus_method_invocation_get_message, invocation, cast_ptr_to_largest_integral_type(obj));
+       expect_value(__wrap_g_dbus_message_new_method_reply, method_call_message, cast_ptr_to_largest_integral_type(obj));
+       expect_value(__wrap_d_bus_reply_message, msg, cast_ptr_to_largest_integral_type(NULL));
+
+       dbus_get_memory_lists((GDBusMethodInvocation *)obj, NULL);
+
+       /* negative test - invalid params */
+       params = g_variant_new("(i)", -1);
+
+       expect_value(__wrap_g_dbus_method_invocation_get_message, invocation, cast_ptr_to_largest_integral_type(obj));
+       expect_value(__wrap_g_dbus_message_new_method_reply, method_call_message, cast_ptr_to_largest_integral_type(obj));
+       expect_value(__wrap_d_bus_reply_message, msg, cast_ptr_to_largest_integral_type(NULL));
+
+       dbus_get_memory_lists((GDBusMethodInvocation *)obj, NULL);
+       g_variant_unref(params);
+
+       /* positive test - fetch apps of type PROC_TYPE_GUI, */
+       params = g_variant_new("(i)", PROC_TYPE_GUI);
+
+       will_return(__wrap_g_dbus_method_invocation_return_value, "proc3");
+       will_return(__wrap_g_dbus_method_invocation_return_value, proc_usage_val);
+       will_return(__wrap_proc_get_mem_usage, proc_usage_val++);
+
+       expect_value(__wrap_g_dbus_method_invocation_return_value, invocation, cast_ptr_to_largest_integral_type(0xcaca0));
+
+       dbus_get_memory_lists((GDBusMethodInvocation *)0xcaca0, params);
+       g_variant_unref(params);
+
+       g_object_unref(obj);
+
+       proc_remove_app_info(one);
+       proc_remove_app_info(two);
+       proc_remove_app_info(three);
+}
+
+int main(int argc, char* argv[])
+{
+       const struct CMUnitTest tests[] = {
+               cmocka_unit_test(test_dbus_get_memory_list),
+               cmocka_unit_test(test_dbus_get_memory_lists),
+       };
+       return cmocka_run_group_tests(tests, NULL, NULL);
+}