]> Savannah Git Hosting - gnulib.git/commitdiff
getlogin, getlogin_r tests: Avoid test failure in specific environments.
authorBruno Haible <bruno@clisp.org>
Mon, 6 Jun 2022 09:55:04 +0000 (11:55 +0200)
committerBruno Haible <bruno@clisp.org>
Mon, 6 Jun 2022 10:13:47 +0000 (12:13 +0200)
Reported by Letu Ren <fantasquex@gmail.com> in
<https://lists.gnu.org/archive/html/bug-gnulib/2022-06/msg00001.html>.

* modules/getlogin-tests (Depends-on): Add stdbool.
* modules/getlogin_r-tests (Depends-on): Likewise.
* tests/test-getlogin.h: Include stdbool.h.
(test_getlogin_result): On Linux, skip the test if /proc/self/loginuid
contains "-1".

ChangeLog
modules/getlogin-tests
modules/getlogin_r-tests
tests/test-getlogin.h

index 46c90605d8d07e95730b4bfe3563b045c25e9323..dbf555ff1fd68d87400c401d657c08c9e66f275f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2022-06-06  Bruno Haible  <bruno@clisp.org>
+
+       getlogin, getlogin_r tests: Avoid test failure in specific environments.
+       Reported by Letu Ren <fantasquex@gmail.com> in
+       <https://lists.gnu.org/archive/html/bug-gnulib/2022-06/msg00001.html>.
+       * modules/getlogin-tests (Depends-on): Add stdbool.
+       * modules/getlogin_r-tests (Depends-on): Likewise.
+       * tests/test-getlogin.h: Include stdbool.h.
+       (test_getlogin_result): On Linux, skip the test if /proc/self/loginuid
+       contains "-1".
+
 2022-06-05  Bruno Haible  <bruno@clisp.org>
 
        doc: Add section to attract more people towards the GNU project.
index 0ccd2ebb714a0bf648919b5db1e3937345eeeaee..5056a9ca4737b02e28561a7aeb06609bb4bc3d92 100644 (file)
@@ -5,6 +5,7 @@ tests/signature.h
 tests/macros.h
 
 Depends-on:
+stdbool
 
 configure.ac:
 
index 190bc9148d1f4d66f8555535b12159e39297d30f..7918ebd183053c2a036aac7cc78383e86544462e 100644 (file)
@@ -5,6 +5,7 @@ tests/signature.h
 tests/macros.h
 
 Depends-on:
+stdbool
 
 configure.ac:
 
index a11b80e4a3b2fd326a4bda77f43209d270d024be..3489e3e03d3a86f787c0a1fb54630bca0aba9770 100644 (file)
@@ -18,6 +18,7 @@
 
 #include <errno.h>
 #include <stdio.h>
+#include <stdbool.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
@@ -42,11 +43,34 @@ test_getlogin_result (const char *buf, int err)
           exit (77);
         }
 
-      /* It fails when stdin is not connected to a tty.  */
       ASSERT (err == ENOTTY
               || err == EINVAL /* seen on Linux/SPARC */
               || err == ENXIO
              );
+
+#if defined __linux__
+      /* On Linux, it is possible to set up a chroot environment in such a way
+         that stdin is connected to a tty and nervertheless /proc/self/loginuid
+         contains "-1".  In this situation, getlogin() and getlogin_r() fail;
+         this is expected.  */
+      bool loginuid_undefined = false;
+      /* Does the special file /proc/self/loginuid contain "-1"?  */
+      FILE *fp = fopen ("/proc/self/loginuid", "r");
+      if (fp != NULL)
+        {
+          char buf[3];
+          loginuid_undefined =
+            (fread (buf, 1, 3, fp) == 2 && buf[0] == '-' && buf[1] == '1');
+          fclose (fp);
+        }
+      if (loginuid_undefined)
+        {
+          fprintf (stderr, "Skipping test: loginuid is undefined.\n");
+          exit (77);
+        }
+#endif
+
+      /* It fails when stdin is not connected to a tty.  */
 #if !defined __hpux /* On HP-UX 11.11 it fails anyway.  */
       ASSERT (! isatty (0));
 #endif
@@ -63,8 +87,10 @@ test_getlogin_result (const char *buf, int err)
 
     if (!isatty (STDIN_FILENO))
       {
-         fprintf (stderr, "Skipping test: stdin is not a tty.\n");
-         exit (77);
+        /* We get here, for example, when running under 'nohup' or as part of a
+           non-interactive ssh command.  */
+        fprintf (stderr, "Skipping test: stdin is not a tty.\n");
+        exit (77);
       }
 
     ASSERT (fstat (STDIN_FILENO, &stat_buf) == 0);