From aa16ab0dc18df9f9e49554eb0ce8eefbf0a5cf22 Mon Sep 17 00:00:00 2001 From: Craig Small Date: Tue, 19 Apr 2016 21:33:02 +1000 Subject: [PATCH] tests: update template and add pids Generalised the library API tests and created a common test-runner. Instead of copy and pasting the same code in each time. First cut of pids API test, for _new so far. --- Makefile.am | 3 +++ include/tests.h | 25 ++++++++++++++++--- proc/.gitignore | 1 + proc/test_namespace.c | 45 +++++++++++++-------------------- proc/test_pids.c | 58 +++++++++++++++++++++++++++++++++++++++++++ proc/test_sysinfo.c | 33 ++++++++---------------- proc/test_uptime.c | 36 +++++++++++---------------- proc/test_version.c | 21 ++++------------ 8 files changed, 130 insertions(+), 92 deletions(-) create mode 100644 proc/test_pids.c diff --git a/Makefile.am b/Makefile.am index 42a67e5f..77e34f34 100644 --- a/Makefile.am +++ b/Makefile.am @@ -269,6 +269,8 @@ proc_test_sysinfo_SOURCES = proc/test_sysinfo.c proc_test_sysinfo_LDADD = proc/libprocps.la proc_test_namespace_SOURCES = proc/test_namespace.c proc_test_namespace_LDADD = proc/libprocps.la +proc_test_pids_SOURCES = proc/test_pids.c +proc_test_pids_LDADD = proc/libprocps.la proc_test_uptime_SOURCES = include/tests.h proc/test_uptime.c proc_test_uptime_LDADD = proc/libprocps.la proc_test_version_SOURCES = include/tests.h proc/test_version.c @@ -283,6 +285,7 @@ endif BUILT_SOURCES = $(top_srcdir)/.version TESTS = proc/test_sysinfo \ + proc/test_pids \ proc/test_namespace \ proc/test_uptime \ proc/test_version \ diff --git a/include/tests.h b/include/tests.h index c65e0ff6..1774f52d 100644 --- a/include/tests.h +++ b/include/tests.h @@ -2,9 +2,26 @@ #ifndef PROCPS_NG_TESTS_H #define PROCPS_NG_TESTS_H -struct test_func { - int (*func)(void *data); - char *name; -}; +typedef int (*TestFunction)(void *data); +char *testname; + + +static inline int run_tests(TestFunction *list, void *data) +{ + int i; + TestFunction current; + + for (i=0; list[i] != NULL; i++) { + testname = NULL; + current = list[i]; + if (!current(data)) { + fprintf(stderr, "FAIL: %s\n", testname); + return EXIT_FAILURE; + } else { + fprintf(stderr, "PASS: %s\n", testname); + } + } + return EXIT_SUCCESS; +} #endif diff --git a/proc/.gitignore b/proc/.gitignore index 7f596aa5..d3b428d9 100644 --- a/proc/.gitignore +++ b/proc/.gitignore @@ -1,4 +1,5 @@ test_namespace +test_pids test_sysinfo test_uptime test_version diff --git a/proc/test_namespace.c b/proc/test_namespace.c index ec9c5862..d39f03d8 100644 --- a/proc/test_namespace.c +++ b/proc/test_namespace.c @@ -20,68 +20,57 @@ #include #include -#include - -struct test_func { - int (*func)(void *data); - char *name; -}; +#include +#include "tests.h" int check_name_minus(void *data) { + testname = "procps_ns_get_name() negative id"; return (procps_ns_get_name(-1) == NULL); } int check_name_over(void *data) { + testname = "procps_ns_get_name() id over limit"; return (procps_ns_get_name(999) == NULL); } int check_name_ipc(void *data) { + testname = "procps_ns_get_name() ipc"; return (strcmp(procps_ns_get_name(PROCPS_NS_IPC),"ipc")==0); } int check_id_null(void *data) { + testname = "procps_ns_get_id(NULL)"; return (procps_ns_get_id(NULL) < 0); } int check_id_unfound(void *data) { + testname = "procps_ns_get_id(unknown)"; return (procps_ns_get_id("foobar") < 0); } int check_id_mnt(void *data) { + testname = "procps_ns_get_id(mnt)"; return (procps_ns_get_id("mnt") == PROCPS_NS_MNT); } -struct test_func tests[] = { - { check_name_minus, "procps_ns_get_name() negative id"}, - { check_name_over, "procps_ns_get_name() id over limit"}, - { check_name_ipc, "procps_ns_get_name() ipc"}, - { check_id_null, "procps_ns_get_id(NULL)"}, - { check_id_unfound, "procps_ns_get_id(unknown)"}, - { check_id_mnt, "procps_ns_get_id(mnt)"}, - { NULL, NULL} +TestFunction test_funcs[] = { + check_name_minus, + check_name_over, + check_name_ipc, + check_id_null, + check_id_unfound, + check_id_mnt, + NULL }; int main(int argc, char *argv[]) { - int i; - struct test_func *current; - - for(i=0; tests[i].func != NULL; i++) { - current = &tests[i]; - if (!current->func(NULL)) { - fprintf(stderr, "FAIL: %s\n", current->name); - return EXIT_FAILURE; - } else { - fprintf(stderr, "PASS: %s\n", current->name); - } - } - return EXIT_SUCCESS; + return run_tests(test_funcs, NULL); } - diff --git a/proc/test_pids.c b/proc/test_pids.c new file mode 100644 index 00000000..ad37eab1 --- /dev/null +++ b/proc/test_pids.c @@ -0,0 +1,58 @@ +/* + * libprocps - Library to read proc filesystem + * Tests for pids library calls + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include +#include +#include + +#include +#include "tests.h" + +int check_pids_new_nullinfo(void *data) +{ + testname = "procps_pids_new() info=NULL returns -EINVAL"; + return (procps_pids_new(NULL, 0, NULL) == -EINVAL); +} + +int check_pids_new_enum(void *data) +{ + testname = "procps_pids_new() items=enum returns -EINVAL"; + struct procps_pidsinfo *info; + return (procps_pids_new(&info, 3, PROCPS_PIDS_noop) == -EINVAL); +} + +int check_pids_new_toomany(void *data) +{ + struct procps_pidsinfo *info; + enum pids_item items[] = { PROCPS_PIDS_ID_PID, PROCPS_PIDS_ID_PID }; + testname = "procps_pids_new() too many items returns -EINVAL"; + return (procps_pids_new(&info, 1, items) == -EINVAL); +} + +TestFunction test_funcs[] = { + check_pids_new_nullinfo, + check_pids_new_enum, + check_pids_new_toomany, + NULL }; + +int main(int argc, char *argv[]) +{ + return run_tests(test_funcs, NULL); +} + + diff --git a/proc/test_sysinfo.c b/proc/test_sysinfo.c index 9597d50d..80f627d3 100644 --- a/proc/test_sysinfo.c +++ b/proc/test_sysinfo.c @@ -20,15 +20,12 @@ #include #include - -struct test_func { - int (*func)(void *data); - char *name; -}; +#include "tests.h" int check_hertz(void *data) { long hz; + testname = "procps_hertz_get()"; hz = procps_hertz_get(); return (hz > 0); @@ -37,6 +34,7 @@ int check_hertz(void *data) int check_loadavg(void *data) { double a,b,c; + testname = "procps_loadavg()"; if (procps_loadavg(&a, &b, &c) == 0) return 1; @@ -45,33 +43,22 @@ int check_loadavg(void *data) int check_loadavg_null(void *data) { + testname = "procps_loadavg() with NULLs"; if (procps_loadavg(NULL, NULL, NULL) == 0) return 1; return 0; } -struct test_func tests[] = { - { check_hertz, "procps_hertz_get()"}, - { check_loadavg, "procps_loadavg()"}, - { check_loadavg, "procps_loadavg() with NULLs"}, - { NULL, NULL} +TestFunction test_funcs[] = { + check_hertz, + check_loadavg, + check_loadavg_null, + NULL, }; int main(int argc, char *argv[]) { - int i; - struct test_func *current; - - for(i=0; tests[i].func != NULL; i++) { - current = &tests[i]; - if (!current->func(NULL)) { - fprintf(stderr, "FAIL: %s\n", current->name); - return EXIT_FAILURE; - } else { - fprintf(stderr, "PASS: %s\n", current->name); - } - } - return EXIT_SUCCESS; + return run_tests(test_funcs, NULL); } diff --git a/proc/test_uptime.c b/proc/test_uptime.c index 92fc5fdb..5058294b 100644 --- a/proc/test_uptime.c +++ b/proc/test_uptime.c @@ -26,6 +26,7 @@ int check_uptime(void *data) { + testname = "procps_uptime()"; double up=0, idle=0; int rc; rc = procps_uptime(&up, &idle); @@ -36,6 +37,7 @@ int check_uptime_nullup(void *data) { double idle=0; int rc; + testname = "procps_uptime() (up=NULL)"; rc = procps_uptime(NULL, &idle); return (rc > 0 && idle > 0); } @@ -44,6 +46,7 @@ int check_uptime_nullidle(void *data) { double up=0; int rc; + testname = "procps_uptime() (idle=NULL)"; rc = procps_uptime(&up, NULL); return (rc > 0 && up > 0); } @@ -51,6 +54,7 @@ int check_uptime_nullidle(void *data) int check_uptime_nullall(void *data) { int rc; + testname = "procps_uptime() (up,idle=NULL)"; rc = procps_uptime(NULL, NULL); return (rc > 0); } @@ -58,6 +62,7 @@ int check_uptime_nullall(void *data) int check_uptime_sprint(void *data) { char *str; + testname = "procps_uptime_sprint()"; str = procps_uptime_sprint(); @@ -67,37 +72,26 @@ int check_uptime_sprint(void *data) int check_uptime_sprint_short(void *data) { char *str; + testname = "procps_uptime_sprint_short()"; str = procps_uptime_sprint_short(); return (str != NULL && str[0] != '\0'); } -struct test_func tests[] = { - { check_uptime, "procps_uptime()"}, - { check_uptime_nullup, "procps_uptime() (up=NULL)"}, - { check_uptime_nullidle, "procps_uptime() (idle=NULL)"}, - { check_uptime_nullall, "procps_uptime() (up,idle=NULL)"}, - { check_uptime_sprint, "procps_uptime_sprint()"}, - { check_uptime_sprint_short, "procps_uptime_sprint_short()"}, - { NULL, NULL} +TestFunction test_funcs[] = { + check_uptime, + check_uptime_nullup, + check_uptime_nullidle, + check_uptime_nullall, + check_uptime_sprint, + check_uptime_sprint_short, + NULL, }; int main(int argc, char *argv[]) { - int i; - struct test_func *current; - - for(i=0; tests[i].func != NULL; i++) { - current = &tests[i]; - if (!current->func(NULL)) { - fprintf(stderr, "FAIL: %s\n", current->name); - return EXIT_FAILURE; - } else { - fprintf(stderr, "PASS: %s\n", current->name); - } - } - return EXIT_SUCCESS; + return run_tests(test_funcs, NULL); } diff --git a/proc/test_version.c b/proc/test_version.c index c55dae54..ba01925c 100644 --- a/proc/test_version.c +++ b/proc/test_version.c @@ -26,29 +26,18 @@ int check_linux_version(void *data) { + testname = "procps_linux_version()"; return (procps_linux_version() > 0); } -struct test_func tests[] = { - { check_linux_version, "procps_linux_version()"}, - { NULL, NULL} +TestFunction test_funcs[] = { + check_linux_version, + NULL }; int main(int argc, char *argv[]) { - int i; - struct test_func *current; - - for(i=0; tests[i].func != NULL; i++) { - current = &tests[i]; - if (!current->func(NULL)) { - fprintf(stderr, "FAIL: %s\n", current->name); - return EXIT_FAILURE; - } else { - fprintf(stderr, "PASS: %s\n", current->name); - } - } - return EXIT_SUCCESS; + return run_tests(test_funcs, NULL); }