SET(UNIT_TESTS_CFLAGS "${UNIT_TESTS_CFLAGS} -I${PROCESS_SOURCE_DIR}")
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} -Wno-psabi")
function(ADD_TESTS name flags wraps sources)
#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)
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-memory-cgroup.c
+ * @desc CMOCKA test code for the memory-cgroup.c/.h
+ */
+
+#include <assert.h>
+#include <stdio.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include "memory-cgroup.h"
+
+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_ulonglong(const char *path, unsigned long long *number)
+{
+ return 0;
+}
+
+static void test_memory_cgroup_is_valid_cgroup_type(void **state)
+{
+ enum cgroup_type valid_cgroup_type[] = {MEMCG_ROOT, MEMCG_BACKGROUND_MRU,
+ MEMCG_BACKGROUND_LRU};
+ enum cgroup_type invalid_cgroup_type[] = {MEMCG_TOP, MEMCG_END};
+ int end;
+
+ /**
+ * Since is_valid_cgroup_type() is static function, perform unit test of this function
+ * by utilizing another function that calls 'is_valid_cgroup_type()'.
+ * memcg_write_optimizer_params returns RESOURCED_ERROR_INVALID_PARAMETER
+ * only when cgroup_type is invalid.
+ */
+ end = sizeof(valid_cgroup_type)/sizeof(*valid_cgroup_type);
+ for (int index = 0; index < end; index++)
+ assert_true(memcg_write_optimizer_params(valid_cgroup_type[index]) !=
+ RESOURCED_ERROR_INVALID_PARAMETER);
+
+ end = sizeof(invalid_cgroup_type)/sizeof(*invalid_cgroup_type);
+ for (int index = 0; index < end; index++)
+ assert_true(memcg_write_optimizer_params(invalid_cgroup_type[index]) ==
+ RESOURCED_ERROR_INVALID_PARAMETER);
+}
+
+int main(int argc, char* argv[])
+{
+ const struct CMUnitTest memory_cgroup_tests[] = {
+ cmocka_unit_test(test_memory_cgroup_is_valid_cgroup_type),
+ };
+
+ return cmocka_run_group_tests(memory_cgroup_tests, NULL, NULL);
+}