changeset 17663:59df01cdab26

getlogin_r-tests: avoid false failure under sudo/ssh etc. * tests/test-getlogin_r.c (main): Sync up with test-getlogin.c changes from commit 97249cf29 to not depend on environment variables.
author Pádraig Brady <P@draigBrady.com>
date Mon, 19 May 2014 13:04:23 +0100
parents 9454e10c1c1e
children f22ef1f10c3f
files ChangeLog tests/test-getlogin_r.c
diffstat 2 files changed, 30 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Sun May 18 02:48:03 2014 +0100
+++ b/ChangeLog	Mon May 19 13:04:23 2014 +0100
@@ -1,3 +1,9 @@
+2014-05-19  Pádraig Brady  <P@draigBrady.com>
+
+	getlogin_r-tests: avoid false failure under sudo/ssh etc.
+	* tests/test-getlogin_r.c (main): Sync up with test-getlogin.c
+	changes from commit 97249cf29 to not depend on environment variables.
+
 2014-05-18  Pádraig Brady  <P@draigBrady.com>
 
 	getlogin-tests: avoid false failure under cron
--- a/tests/test-getlogin_r.c	Sun May 18 02:48:03 2014 +0100
+++ b/tests/test-getlogin_r.c	Mon May 19 13:04:23 2014 +0100
@@ -63,11 +63,30 @@
 #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)
+      {
+         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__