From: Unsung Lee Date: Thu, 14 Nov 2024 02:58:40 +0000 (+0900) Subject: test: cmocka-heart: Add unit tests for the heart module X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fheads%2Ftizen_dev;p=platform%2Fcore%2Fsystem%2Fresourced.git test: cmocka-heart: Add unit tests for the heart module Add unit tests for the heart module to check correctness of the function (heart_module_add(), heart_module_find(), and heart_module_remove()) in heart.c. For this goal, several modifications of CMakeLists.txt have been added. First, make a new static library called resourced_heart_test for linking. Second, add src/common/conf heart path to UNIT_TESTS_CFLAGS to include dbus-handler.h. Change-Id: Ib5dbd62d00d3c7645db7e6f51dc83c4b601bff6d Signed-off-by: Unsung Lee --- diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 907deb70..596b9d32 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -224,6 +224,13 @@ SET_TARGET_PROPERTIES(resourced_shared_test PROPERTIES COMPILE_FLAGS ${CMAKE_C_FLAGS_RELEASE}) TARGET_LINK_LIBRARIES(resourced_shared_test ${RESOURCED_REQUIRE_PKGS_LDFLAGS}) +ADD_LIBRARY(resourced_heart_test STATIC + ${RESOURCE_MONITOR_SOURCE_DIR}/heart.c) + +SET_TARGET_PROPERTIES(resourced_heart_test + PROPERTIES COMPILE_FLAGS ${CMAKE_C_FLAGS_RELEASE}) +TARGET_LINK_LIBRARIES(resourced_heart_test ${RESOURCED_REQUIRE_PKGS_LDFLAGS}) + SET(EXTRA_CFLAGS ${SAVE_EXTRA_CFLAGS}) SET(EXTRA_CFLAGS_RELEASE ${SAVE_EXTRA_CFLAGS_RELEASE}) SET(CMAKE_C_FLAGS ${SAVE_CMAKE_C_FLAGS}) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index bbb72b4d..e060a129 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -53,14 +53,15 @@ SET(UNIT_TESTS_CFLAGS "${UNIT_TESTS_CFLAGS} -I${RESOURCED_SOURCE_DIR}") 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}) @@ -79,12 +80,13 @@ endfunction() #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}) diff --git a/tests/cmocka-heart.c b/tests/cmocka-heart.c new file mode 100644 index 00000000..1470f366 --- /dev/null +++ b/tests/cmocka-heart.c @@ -0,0 +1,186 @@ +/** + * 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 +#include +#include +#include +#include +#include +#include + +#include + +#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; +}