tests: added proc-stat API test
authorPrajwal A N <an.prajwal@samsung.com>
Sun, 13 Dec 2015 23:51:00 +0000 (08:51 +0900)
committerPrajwal A N <an.prajwal@samsung.com>
Tue, 15 Dec 2015 00:28:55 +0000 (09:28 +0900)
* added tests for the proc_stat_get_pid_entry API
* other API tests will be added in future

Change-Id: Ic4bc35bfd8ae06d8c0a92af44f91d687beabda1f
Signed-off-by: Prajwal A N <an.prajwal@samsung.com>
packaging/resourced.spec
src/tests/CMakeLists.txt
src/tests/proc-stat/resourced_proc_stat_test.c [new file with mode: 0644]

index c3463d868f795ba221f1f17fc64267d01876a978..757ad7eda20e6e51d4255c5b541f5abcd27d577e 100644 (file)
@@ -271,6 +271,8 @@ fi
 %defattr(-,root,root,-)
 %{_bindir}/resourced_memory_test
 %defattr(-,root,root,-)
+%{_bindir}/resourced_proc_stat_test
+%defattr(-,root,root,-)
 %{_bindir}/resourced_dummy_process
 %defattr(-,root,root,-)
 %{_bindir}/resourced_hogger_memory
index 84ab56bbc7ceeea224a543edf3cdb76e65af08f0..c483de90d4f86b316495ae7e6003e0812ac63a60 100644 (file)
@@ -16,6 +16,7 @@ SET(SRC_MEMORY_TEST ${TESTS_SOURCE_DIR}/memory/resourced_memory_test.c
 
 SET(SRC_DUMMY_PROCESS ${TESTS_SOURCE_DIR}/util/resourced_dummy_process.c)
 SET(SRC_MEMORY_HOG ${TESTS_SOURCE_DIR}/util/resourced_hogger_memory.c)
+SET(SRC_PROC_STAT_TEST ${TESTS_SOURCE_DIR}/proc-stat/resourced_proc_stat_test.c)
 
 #Linking flags
 INCLUDE(FindPkgConfig)
@@ -30,6 +31,10 @@ ADD_EXECUTABLE (resourced_memory_test ${SRC_MEMORY_TEST})
 TARGET_LINK_LIBRARIES(resourced_memory_test ${daemon_pkgs_LDFLAGS})
 INSTALL(TARGETS resourced_memory_test DESTINATION bin)
 
+ADD_EXECUTABLE(resourced_proc_stat_test ${SRC_PROC_STAT_TEST})
+TARGET_LINK_LIBRARIES(resourced_proc_stat_test ${daemon_pkgs_LDFLAGS} ${PROC-STAT})
+INSTALL(TARGETS resourced_proc_stat_test DESTINATION bin)
+
 ADD_EXECUTABLE(resourced_dummy_process ${SRC_DUMMY_PROCESS})
 INSTALL(TARGETS resourced_dummy_process DESTINATION bin)
 
diff --git a/src/tests/proc-stat/resourced_proc_stat_test.c b/src/tests/proc-stat/resourced_proc_stat_test.c
new file mode 100644 (file)
index 0000000..87320da
--- /dev/null
@@ -0,0 +1,99 @@
+/*
+   Copyright (c) 2000 - 2014 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.
+
+   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
+*/
+
+/* @author: Prajwal A N
+ * @file: proc-stat.c
+ * @desc: Tests for proc-stat APIs in resourced
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <proc_stat.h>
+
+#include "resourced_tests.h"
+
+#define PSTAT_GET_PID_ENTRY_MAX_TESTS 5
+
+enum {
+       PSTAT_TESTS_GET_PID_ENTRY,
+       PSTAT_TESTS_MAX,
+};
+
+struct pstat_test_t {
+       int test_num;
+       char name[STRING_MAX];
+       int (*test_func)(void);
+};
+
+int pstat_get_pid_entry(void)
+{
+       char buf[4*STRING_MAX];
+       int i, ret, final_ret;
+       int pid;
+       char *tests[] = {
+               "cmdline",
+               "exe",
+               "stat",
+               "status",
+               "oomscore"
+       };
+       int test_inp[] = {
+               PROC_CGROUP_GET_CMDLINE,
+               PROC_CGROUP_GET_EXE,
+               PROC_CGROUP_GET_STAT,
+               PROC_CGROUP_GET_STATUS,
+               PROC_CGROUP_GET_OOMSCORE,
+       };
+
+       pid = getpid();
+       final_ret = ERROR_NONE;
+       for (i = 0; i < PSTAT_GET_PID_ENTRY_MAX_TESTS; ++i) {
+               ret = proc_stat_get_pid_entry(test_inp[i], pid, buf, sizeof(buf)-1);
+               buf[sizeof(buf)-1] = 0;
+               if (ret != RESOURCED_ERROR_NONE) {
+                       _E("Test %s: failed with %d error", tests[i], ret);
+                       final_ret = ERROR_FAIL;
+               } else
+                       _D("Test %s: Passed. buf: %s", tests[i], buf);
+       }
+       return final_ret;
+}
+
+static struct pstat_test_t pstat_tests[] = {
+       { PSTAT_TESTS_GET_PID_ENTRY, "pstat_get_pid_entry", pstat_get_pid_entry },
+       { PSTAT_TESTS_MAX, "", NULL },
+};
+
+int main(int argc, char *argv[])
+{
+       int i, ret;
+       char buf[STRING_MAX];
+
+       printf("Testing proc-stat module. Current pid: %d\n", getpid());
+       printf("Start journalctl and enter input:");
+       ret = scanf("%s\n", buf);
+
+       for (i = 0; i < PSTAT_TESTS_MAX; ++i) {
+               _D("=======================================");
+               _D("Current Test: %s", pstat_tests[i].name);
+               ret = (*pstat_tests[i].test_func)();
+               if (ret)
+                       _E("Test %s failed!", pstat_tests[i].name);
+               else
+                       _D("Test %s passed!", pstat_tests[i].name);
+       }
+       return 0;
+}