From 2ae2d648ab0bd5340ca1342c97d704b2d4ce948c Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Sat, 23 Feb 2019 23:48:32 +0100 Subject: blogc: sysinfo: simplified username impl. fixed tests --- Makefile.am | 9 ++------- configure.ac | 4 +--- src/blogc/funcvars.c | 5 +---- src/blogc/sysinfo.c | 19 +------------------ src/blogc/sysinfo.h | 8 -------- tests/blogc/check_sysinfo.c | 40 +++++++++------------------------------- 6 files changed, 14 insertions(+), 71 deletions(-) diff --git a/Makefile.am b/Makefile.am index f2a8ffa..b136f9f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -534,19 +534,14 @@ tests_blogc_check_sysinfo_CFLAGS = \ tests_blogc_check_sysinfo_LDFLAGS = \ -no-install \ -Wl,--wrap=bc_file_get_contents \ - -Wl,--wrap=gethostname \ + -Wl,--wrap=getenv \ $(NULL) if HAVE_UNISTD_H -if HAVE_SYS_TYPES_H -if HAVE_PWD_H tests_blogc_check_sysinfo_LDFLAGS += \ - -Wl,--wrap=geteuid \ - -Wl,--wrap=getpwuid \ + -Wl,--wrap=gethostname \ $(NULL) endif -endif -endif if HAVE_TIME_H tests_blogc_check_sysinfo_LDFLAGS += \ diff --git a/configure.ac b/configure.ac index a0940c1..d92e0e9 100644 --- a/configure.ac +++ b/configure.ac @@ -220,15 +220,13 @@ AM_CONDITIONAL([USE_BGR_DEPS], [test "x$have_bgr_deps" = "xyes"]) BASH="$ac_cv_path_bash" AC_SUBST(BASH) -AC_CHECK_HEADERS([sys/resource.h sys/stat.h sys/time.h sys/wait.h time.h unistd.h pwd.h]) +AC_CHECK_HEADERS([sys/resource.h sys/stat.h sys/time.h sys/wait.h time.h unistd.h]) AC_CHECK_FUNCS([gethostname]) -AM_CONDITIONAL([HAVE_PWD_H], [test "x$ac_cv_header_pwd_h" = "xyes"]) AM_CONDITIONAL([HAVE_TIME_H], [test "x$ac_cv_header_time_h" = "xyes"]) AM_CONDITIONAL([HAVE_UNISTD_H], [test "x$ac_cv_header_unistd_h" = "xyes"]) AM_CONDITIONAL([HAVE_SYS_RESOURCE_H], [test "x$ac_cv_header_sys_resource_h" = "xyes"]) AM_CONDITIONAL([HAVE_SYS_TIME_H], [test "x$ac_cv_header_sys_time_h" = "xyes"]) -AM_CONDITIONAL([HAVE_SYS_TYPES_H], [test "x$ac_cv_header_sys_types_h" = "xyes"]) LT_LIB_M diff --git a/src/blogc/funcvars.c b/src/blogc/funcvars.c index 42411e2..6f0700b 100644 --- a/src/blogc/funcvars.c +++ b/src/blogc/funcvars.c @@ -30,14 +30,11 @@ static const struct func_map { {"BLOGC_SYSINFO_HOSTNAME", blogc_sysinfo_inject_hostname}, #endif -#ifdef HAVE_SYSINFO_USERNAME - {"BLOGC_SYSINFO_USERNAME", blogc_sysinfo_inject_username}, -#endif - #ifdef HAVE_SYSINFO_DATETIME {"BLOGC_SYSINFO_DATETIME", blogc_sysinfo_inject_datetime}, #endif + {"BLOGC_SYSINFO_USERNAME", blogc_sysinfo_inject_username}, {"BLOGC_SYSINFO_INSIDE_DOCKER", blogc_sysinfo_inject_inside_docker}, {NULL, NULL}, }; diff --git a/src/blogc/sysinfo.c b/src/blogc/sysinfo.c index 4ec1411..cdb860e 100644 --- a/src/blogc/sysinfo.c +++ b/src/blogc/sysinfo.c @@ -14,14 +14,6 @@ #include #endif /* HAVE_UNISTD_H */ -#ifdef HAVE_SYS_TYPES_H -#include -#endif /* HAVE_SYS_TYPES_H */ - -#ifdef HAVE_PWD_H -#include -#endif /* HAVE_PWD_H */ - #ifdef HAVE_TIME_H #include #endif /* HAVE_TIME_H */ @@ -66,16 +58,7 @@ blogc_sysinfo_inject_hostname(bc_trie_t *global) char* blogc_sysinfo_get_username(void) { -#ifndef HAVE_SYSINFO_USERNAME - return NULL; -#else - uid_t u = geteuid(); - struct passwd *p = getpwuid(u); - if (p == NULL) - return NULL; - - return bc_strdup(p->pw_name); -#endif + return bc_strdup(getenv("LOGNAME")); } diff --git a/src/blogc/sysinfo.h b/src/blogc/sysinfo.h index 783b656..b249661 100644 --- a/src/blogc/sysinfo.h +++ b/src/blogc/sysinfo.h @@ -19,14 +19,6 @@ #endif /* HAVE_GETHOSTNAME */ #endif /* HAVE_UNISTD_H */ -#ifdef HAVE_UNISTD_H -#ifdef HAVE_SYS_TYPES_H -#ifdef HAVE_PWD_H -#define HAVE_SYSINFO_USERNAME 1 -#endif /* HAVE_PWD_H */ -#endif /* HAVE_SYS_TYPES_H */ -#endif /* HAVE_UNISTD_H */ - #ifdef HAVE_TIME_H #define HAVE_SYSINFO_DATETIME 1 #endif /* HAVE_TIME_H */ diff --git a/tests/blogc/check_sysinfo.c b/tests/blogc/check_sysinfo.c index d1034fc..01b81b9 100644 --- a/tests/blogc/check_sysinfo.c +++ b/tests/blogc/check_sysinfo.c @@ -17,16 +17,11 @@ #include "../../src/common/utils.h" #include "../../src/blogc/sysinfo.h" -#ifdef HAVE_SYSINFO_HOSTNAME -#include -#include -#include -#endif - #ifdef HAVE_SYSINFO_DATETIME #include #endif +#ifdef HAVE_SYSINFO_HOSTNAME int __wrap_gethostname(char *name, size_t len) { @@ -36,29 +31,14 @@ __wrap_gethostname(char *name, size_t len) strcpy(name, f); return mock_type(int); } +#endif -#ifdef HAVE_SYSINFO_HOSTNAME -uid_t -__wrap_geteuid(void) -{ - return 1234; -} - - -static struct passwd pw; - -struct passwd* -__wrap_getpwuid(uid_t uid) +char* +__wrap_getenv(const char *name) { - assert_int_equal(uid, 1234); - char *n = mock_type(char*); - if (n == NULL) - return NULL; - pw.pw_name = n; - return &pw; + return mock_type(char*); } -#endif #ifdef HAVE_SYSINFO_DATETIME @@ -142,11 +122,11 @@ test_sysinfo_inject_hostname(void **state) static void test_sysinfo_get_username(void **state) { - will_return(__wrap_getpwuid, NULL); + will_return(__wrap_getenv, NULL); char *f = blogc_sysinfo_get_username(); assert_null(f); - will_return(__wrap_getpwuid, "bola"); + will_return(__wrap_getenv, "bola"); f = blogc_sysinfo_get_username(); assert_non_null(f); assert_string_equal(f, "bola"); @@ -159,11 +139,11 @@ test_sysinfo_inject_username(void **state) { bc_trie_t *t = bc_trie_new(free); - will_return(__wrap_getpwuid, NULL); + will_return(__wrap_getenv, NULL); blogc_sysinfo_inject_username(t); assert_int_equal(bc_trie_size(t), 0); - will_return(__wrap_getpwuid, "bola"); + will_return(__wrap_getenv, "bola"); blogc_sysinfo_inject_username(t); assert_int_equal(bc_trie_size(t), 1); assert_string_equal(bc_trie_lookup(t, "BLOGC_SYSINFO_USERNAME"), "bola"); @@ -235,10 +215,8 @@ main(void) unit_test(test_sysinfo_inject_hostname), #endif -#ifdef HAVE_SYSINFO_USERNAME unit_test(test_sysinfo_get_username), unit_test(test_sysinfo_inject_username), -#endif #ifdef HAVE_SYSINFO_DATETIME unit_test(test_sysinfo_get_datetime), -- cgit v1.2.3-18-g5258