>From 97249cf2945e8fe18ed138af20d6e46761fd0470 Mon Sep 17 00:00:00 2001 From: Guilherme de Almeida Suckevicz Date: Wed, 14 May 2014 22:06:24 +0100 Subject: [PATCH] getlogin-tests: avoid false failure under sudo/ssh etc. * modules/getlogin-tests (configure.ac): Check for ttyname(). * tests/test-getlogin.c (main): Don't depend on environment variables to correlate with getlogin(), since sudo and ssh etc. can tamper with the LOGNAME and USER env vars. Instead lookup the name from the uid associated with the stdin tty. --- ChangeLog | 9 +++++++++ modules/getlogin-tests | 1 + tests/test-getlogin.c | 36 +++++++++++++++++++++++++++++++----- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index a8b2ee0..19dfba5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2014-05-14 Guilherme de Almeida Suckevicz + + getlogin-tests: avoid false failure under sudo/ssh etc. + * modules/getlogin-tests (configure.ac): Check for ttyname(). + * tests/test-getlogin.c (main): Don't depend on environment variables + to correlate with getlogin(), since sudo and ssh etc. can tamper + with the LOGNAME and USER env vars. Instead lookup the name from + the uid associated with the stdin tty. + 2014-05-11 Paul Eggert mbsstr, quotearg, xstrtol: pacify IRIX 6.5 cc diff --git a/modules/getlogin-tests b/modules/getlogin-tests index 795fba7..6facd60 100644 --- a/modules/getlogin-tests +++ b/modules/getlogin-tests @@ -6,6 +6,7 @@ tests/macros.h Depends-on: configure.ac: +AC_CHECK_FUNCS_ONCE([ttyname]) Makefile.am: TESTS += test-getlogin diff --git a/tests/test-getlogin.c b/tests/test-getlogin.c index 197aed8..32fd893 100644 --- a/tests/test-getlogin.c +++ b/tests/test-getlogin.c @@ -27,6 +27,11 @@ SIGNATURE_CHECK (getlogin, char *, (void)); #include #include #include +#include +#include + +#include +#include #include "macros.h" @@ -62,11 +67,32 @@ main (void) #if !((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) /* Unix platform */ { - const char *name = getenv ("LOGNAME"); - if (name == NULL || name[0] == '\0') - name = getenv ("USER"); - if (name != NULL && name[0] != '\0') - ASSERT (strcmp (buf, name) == 0); +# if HAVE_TTYNAME + const char *tty; + struct stat stat_buf; + struct passwd *pwd; + + tty = ttyname (STDIN_FILENO); + if (tty == NULL) + { + ASSERT (errno == ENOTTY); + + fprintf (stderr, "Skipping test: stdin is not a tty.\n"); + return 77; + } + + ASSERT (stat (tty, &stat_buf) == 0); + + pwd = getpwuid (stat_buf.st_uid); + if (! pwd) + { + fprintf (stderr, "Skipping test: no name found for uid %d\n", + stat_buf.st_uid); + return 77; + } + + ASSERT (strcmp (pwd->pw_name, buf) == 0); +# endif } #endif #if (defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__ -- 1.7.7.6