SET(UNIT_TESTS_CFLAGS "${UNIT_TESTS_CFLAGS} -I${WATCHDOG_SOURCE_DIR}")
SET(UNIT_TESTS_CFLAGS "${UNIT_TESTS_CFLAGS} -I${INCLUDE_CONF_DIR}")
SET(UNIT_TESTS_CFLAGS "${UNIT_TESTS_CFLAGS} -I${INCLUDE_MODULE_DIR}")
+SET(UNIT_TESTS_CFLAGS "${UNIT_TESTS_CFLAGS} -I${INCLUDE_IPC_DIR}")
SET(UNIT_TESTS_CFLAGS "${UNIT_TESTS_CFLAGS} -Wno-psabi")
-function(ADD_TESTS name flags wraps sources)
+function(ADD_TESTS name flags wraps sources extra_resourced_test_library)
ADD_EXECUTABLE(${name} ${sources})
SET_TARGET_PROPERTIES(${name} PROPERTIES COMPILE_FLAGS ${flags})
- TARGET_LINK_LIBRARIES(${name} resourced_shared_test cmocka dlog gio-2.0 gobject-2.0 glib-2.0
+ TARGET_LINK_LIBRARIES(${name} resourced_shared_test ${extra_resourced_test_library} cmocka dlog gio-2.0 gobject-2.0 glib-2.0
${wraps})
ADD_TEST(${name} ${name})
#ADD_TESTS(cmocka-proc-app-list "${UNIT_TESTS_CFLAGS}" "-Wl,--wrap=fread_int,--wrap=fread_uint,--wrap=fread_ulong,--wrap=g_slist_prepend,--wrap=g_slist_remove,--wrap=g_slist_prepend,--wrap=g_slist_remove -O0" cmocka-proc-app-list.c)
#ADD_TESTS(test-common "${UNIT_TESTS_CFLAGS}" "-O0" test-common.c)
#ADD_TESTS(test-safe-kill "${UNIT_TESTS_CFLAGS}" "-Wl,--wrap=kill,--wrap=fopen,--wrap=fopen64 -O0" test-safe-kill.c)
-ADD_TESTS(cmocka-memory-cgroup "${UNIT_TESTS_CFLAGS}" "-Wl,--wrap=fread_int,--wrap=fread_uint,--wrap=fread_ulonglong -O0" cmocka-memory-cgroup.c)
-ADD_TESTS(cmocka-fd-handler "${UNIT_TESTS_CFLAGS}" "-Wl,--wrap=malloc,--wrap=free -O0" cmocka-fd-handler.c)
-ADD_TESTS(cmocka-config-parser "${UNIT_TESTS_CFLAGS}" "-Wl,--wrap=fread_int,--wrap=fread_uint,--wrap=fread_ulonglong -O0" cmocka-config-parser.c)
-ADD_TESTS(cmocka-notifier "${UNIT_TESTS_CFLAGS}" "-Wl,--wrap=malloc,--wrap=free,--wrap=g_slist_append,--wrap=g_slist_remove -O0" cmocka-notifier.c)
-ADD_TESTS(cmocka-freezer-common "${UNIT_TESTS_CFLAGS}" "-Wl,--wrap=find_module -O0" cmocka-freezer-common.c)
-ADD_TESTS(cmocka-proc-main "${UNIT_TESTS_CFLAGS}" "-Wl,--wrap=fread_int,--wrap=fread_uint,--wrap=fread_ulong,--wrap=fread_ulonglong,--wrap=proc_get_oom_score_adj -O0" cmocka-proc-main.c)
+ADD_TESTS(cmocka-memory-cgroup "${UNIT_TESTS_CFLAGS}" "-Wl,--wrap=fread_int,--wrap=fread_uint,--wrap=fread_ulonglong -O0" cmocka-memory-cgroup.c "")
+ADD_TESTS(cmocka-fd-handler "${UNIT_TESTS_CFLAGS}" "-Wl,--wrap=malloc,--wrap=free -O0" cmocka-fd-handler.c "")
+ADD_TESTS(cmocka-config-parser "${UNIT_TESTS_CFLAGS}" "-Wl,--wrap=fread_int,--wrap=fread_uint,--wrap=fread_ulonglong -O0" cmocka-config-parser.c "")
+ADD_TESTS(cmocka-notifier "${UNIT_TESTS_CFLAGS}" "-Wl,--wrap=malloc,--wrap=free,--wrap=g_slist_append,--wrap=g_slist_remove -O0" cmocka-notifier.c "")
+ADD_TESTS(cmocka-freezer-common "${UNIT_TESTS_CFLAGS}" "-Wl,--wrap=find_module -O0" cmocka-freezer-common.c "")
+ADD_TESTS(cmocka-proc-main "${UNIT_TESTS_CFLAGS}" "-Wl,--wrap=fread_int,--wrap=fread_uint,--wrap=fread_ulong,--wrap=fread_ulonglong,--wrap=proc_get_oom_score_adj -O0" cmocka-proc-main.c "")
+ADD_TESTS(cmocka-heart "${UNIT_TESTS_CFLAGS}" "-Wl,--wrap=g_slist_append,--wrap=g_slist_remove,--wrap=heart_dbus_init,--wrap=logging_save_to_storage -O0" cmocka-heart.c resourced_heart_test)
function(ADD_SKIP_TEST name wraps sources)
ADD_EXECUTABLE(${name} ${sources})
--- /dev/null
+/**
+ * resourced
+ *
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file cmocka-heart.c
+ * @desc CMOCKA test code for the heart.c/.h
+ */
+
+#include <stdarg.h>
+#include <setjmp.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <cmocka.h>
+#include <assert.h>
+
+#include <glib.h>
+
+#include "heart.h"
+
+GSList *__real_g_slist_append(GSList *list, gpointer data);
+GSList *__real_g_slist_remove(GSList *list, gconstpointer data);
+
+GSList *g_heart_module;
+
+GSList *__wrap_g_slist_append(GSList *list, gpointer data)
+{
+ assert(data);
+
+ g_heart_module = __real_g_slist_append(g_heart_module, data);
+
+ return g_heart_module;
+}
+
+GSList *__wrap_g_slist_remove(GSList *list, gconstpointer data)
+{
+ assert(g_heart_module);
+ assert(data);
+
+ g_heart_module = __real_g_slist_remove(g_heart_module, data);
+
+ return g_heart_module;
+}
+
+int __wrap_heart_dbus_init(void)
+{
+ return 0;
+}
+
+void __wrap_logging_save_to_storage(int force)
+{
+}
+
+static int heart_test_init(void *data)
+{
+ return 0;
+}
+
+static int heart_test_exit(void *data)
+{
+ return 0;
+}
+
+static const struct heart_module_ops heart_test_ops1 = {
+ .name = "HEART-TEST1",
+ .init = heart_test_init,
+ .exit = heart_test_exit,
+};
+
+static const struct heart_module_ops heart_test_ops2 = {
+ .name = "HEART-TEST2",
+ .init = heart_test_init,
+ .exit = heart_test_exit,
+};
+
+static void test_heart_module_add(void **state)
+{
+ assert(g_slist_length(g_heart_module) == 0);
+
+ heart_module_add(&heart_test_ops1);
+ heart_module_add(&heart_test_ops2);
+
+ assert(g_heart_module);
+ assert(g_slist_length(g_heart_module) == 2);
+}
+
+static int find_module_by_name(char *module_name, struct heart_module_ops **module_ops)
+{
+ assert(g_heart_module);
+
+ GSList *list = g_heart_module;
+ struct heart_module_ops *temp_module_ops;
+
+ if (module_name == NULL)
+ return -EINVAL;
+
+ if (module_ops == NULL)
+ return -EINVAL;
+
+ *module_ops = NULL;
+ while (true) {
+ GSList *next = list->next;
+ temp_module_ops = (struct heart_module_ops *)list->data;
+ if (!strncmp(temp_module_ops->name, module_name, strlen(module_name) + 1)) {
+ *module_ops = temp_module_ops;
+ break;
+ }
+
+ if (next == NULL)
+ break;
+
+ list = next;
+ }
+
+ return 0;
+}
+
+static void test_heart_module_find(void **state)
+{
+ struct heart_module_ops *module_ops = NULL;
+ int result = 0;
+
+ assert(g_slist_length(g_heart_module) == 2);
+
+ assert(find_module_by_name(NULL, &module_ops) == -EINVAL);
+ assert(find_module_by_name("HEART-TEST1", NULL) == -EINVAL);
+
+ /* Success, but no corresponding module_ops */
+ result = find_module_by_name("", &module_ops);
+ assert_int_equal(result, 0);
+ assert_true(module_ops == NULL);
+
+ result = find_module_by_name("HEART", &module_ops);
+ assert_int_equal(result, 0);
+ assert_true(module_ops == NULL);
+
+ /* Success, and find corresponding module_ops */
+ result = find_module_by_name("HEART-TEST1", &module_ops);
+ assert_int_equal(result, 0);
+ assert_true(module_ops == &heart_test_ops1);
+
+ result = find_module_by_name("HEART-TEST2", &module_ops);
+ assert_int_equal(result, 0);
+ assert_true(module_ops == &heart_test_ops2);
+}
+
+static void test_heart_module_remove(void **state)
+{
+ assert(g_slist_length(g_heart_module) == 2);
+
+ heart_module_remove(&heart_test_ops1);
+ heart_module_remove(&heart_test_ops2);
+
+ assert(g_slist_length(g_heart_module) == 0);
+}
+
+int main(int argc, char* argv[])
+{
+ int ret = 0;
+ const struct CMUnitTest notifier_tests[] = {
+ cmocka_unit_test(test_heart_module_add),
+ cmocka_unit_test(test_heart_module_find),
+ cmocka_unit_test(test_heart_module_remove)
+ };
+
+ ret = cmocka_run_group_tests(notifier_tests, NULL, NULL);
+ if (ret != 0)
+ return ret;
+
+ return 0;
+}