summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2022-06-06 11:55:04 +0200
committerBruno Haible <bruno@clisp.org>2022-06-06 12:13:47 +0200
commitedbf01f92b772439260c6a245669a045038d53e3 (patch)
tree66e16a30772db6db2a30aa5d9fa922f671c4fa6f
parent4d9ec6cb01a8425bd59a1fdf71caa1d48b10cfcc (diff)
downloadgnulib-edbf01f92b772439260c6a245669a045038d53e3.tar.gz
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".
-rw-r--r--ChangeLog11
-rw-r--r--modules/getlogin-tests1
-rw-r--r--modules/getlogin_r-tests1
-rw-r--r--tests/test-getlogin.h32
4 files changed, 42 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index 46c90605d8..dbf555ff1f 100644
--- 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.
diff --git a/modules/getlogin-tests b/modules/getlogin-tests
index 0ccd2ebb71..5056a9ca47 100644
--- a/modules/getlogin-tests
+++ b/modules/getlogin-tests
@@ -5,6 +5,7 @@ tests/signature.h
tests/macros.h
Depends-on:
+stdbool
configure.ac:
diff --git a/modules/getlogin_r-tests b/modules/getlogin_r-tests
index 190bc9148d..7918ebd183 100644
--- a/modules/getlogin_r-tests
+++ b/modules/getlogin_r-tests
@@ -5,6 +5,7 @@ tests/signature.h
tests/macros.h
Depends-on:
+stdbool
configure.ac:
diff --git a/tests/test-getlogin.h b/tests/test-getlogin.h
index a11b80e4a3..3489e3e03d 100644
--- a/tests/test-getlogin.h
+++ b/tests/test-getlogin.h
@@ -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);