]> Savannah Git Hosting - gnulib.git/commitdiff
getlogin_r-tests: avoid false failure under sudo/ssh etc.
authorPádraig Brady <P@draigBrady.com>
Mon, 19 May 2014 12:04:23 +0000 (13:04 +0100)
committerPádraig Brady <P@draigBrady.com>
Mon, 19 May 2014 12:30:28 +0000 (13:30 +0100)
* tests/test-getlogin_r.c (main): Sync up with test-getlogin.c
changes from commit 97249cf29 to not depend on environment variables.

ChangeLog
tests/test-getlogin_r.c

index bcd414de9252e1efc8dc6cc7c3ccf6b2b24e1873..c91ac60ce7bc1ce601a48f0bfecbfc4795bd61a4 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -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
index ed103c9fc700af310db8e2e3e2e661ddcbb71d68..fb3f9bffb1b590be3169b454d4d2150dded880b5 100644 (file)
@@ -63,11 +63,30 @@ 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)
+      {
+         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__