test: cmocka-memory-cgroup: Add unit test for is_valid_cgroup_type() 86/320486/5
authorUnsung <unsung.lee@samsung.com>
Mon, 14 Oct 2024 11:09:16 +0000 (20:09 +0900)
committerUnsung Lee <unsung.lee@samsung.com>
Fri, 22 Nov 2024 09:53:06 +0000 (18:53 +0900)
Add a unit test for is_valid_cgroup_type() to check correctness of the function.
is_valid_cgroup_type() is currently static function,
so it is impossible to call this function in the cmocka unit test file.
Instead, perform unit test of this function by utilizing another function
that calls 'is_valid_cgroup_type()'.

Change-Id: Idbfc5310d3c4054626abdfef13c976a46c592d0a
Signed-off-by: Unsung Lee <unsung.lee@samsung.com>
tests/CMakeLists.txt
tests/cmocka-memory-cgroup.c [new file with mode: 0644]

index cf1eadb88888e7923756cee289237314718f4bc4..5417e3c6fb267c3e7bf24cdb5de7e9e4baed3166 100644 (file)
@@ -49,6 +49,7 @@ SET(UNIT_TESTS_CFLAGS "${UNIT_TESTS_CFLAGS} -I${MEMORY_LIMITER_SOURCE_DIR}")
 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)
@@ -75,6 +76,7 @@ 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)
 
 function(ADD_SKIP_TEST name wraps sources)
        ADD_EXECUTABLE(${name} ${sources})
diff --git a/tests/cmocka-memory-cgroup.c b/tests/cmocka-memory-cgroup.c
new file mode 100644 (file)
index 0000000..dec7cae
--- /dev/null
@@ -0,0 +1,78 @@
+/**
+ * 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);
+}