bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: bug-gnulib@gnu.org
Subject: access tests: fix test failure when run as root
Date: Mon, 07 Oct 2019 01:44:23 +0200	[thread overview]
Message-ID: <5867345.KNtYe3x8ii@omega> (raw)

One of the gnulib tests, test-access, fails when running as root.
This patch fixes it.


2019-10-06  Bruno Haible  <bruno@clisp.org>

	access tests: Fix test failure when run as root.
	* tests/test-access.c: Include root-uid.h.
	(geteuid): Define fallback.
	(main): Don't expect that writing to a read-only file would fail when
	running as root. Also, remove the created files at the end.
	* modules/access-tests (Depends-on): Add root-uid.
	(configure.ac): Test whether geteuid exists.

diff --git a/modules/access-tests b/modules/access-tests
index 082aeb5..4b35c21 100644
--- a/modules/access-tests
+++ b/modules/access-tests
@@ -5,8 +5,10 @@ tests/macros.h
 
 Depends-on:
 creat
+root-uid
 
 configure.ac:
+AC_CHECK_FUNCS_ONCE([geteuid])
 
 Makefile.am:
 TESTS += test-access
diff --git a/tests/test-access.c b/tests/test-access.c
index 7af7f9a..1488d55 100644
--- a/tests/test-access.c
+++ b/tests/test-access.c
@@ -24,8 +24,14 @@ SIGNATURE_CHECK (access, int, (const char *, int));
 #include <errno.h>
 #include <fcntl.h>
 #include <sys/stat.h>
+#include "root-uid.h"
 #include "macros.h"
 
+/* mingw and MSVC 9 lack geteuid, so setup a dummy value.  */
+#if !HAVE_GETEUID
+# define geteuid() ROOT_UID
+#endif
+
 #define BASE "test-access.t"
 
 int
@@ -62,9 +68,12 @@ main ()
 
     ASSERT (access (BASE "f2", R_OK) == 0);
 
-    errno = 0;
-    ASSERT (access (BASE "f2", W_OK) == -1);
-    ASSERT (errno == EACCES);
+    if (geteuid () != ROOT_UID)
+      {
+        errno = 0;
+        ASSERT (access (BASE "f2", W_OK) == -1);
+        ASSERT (errno == EACCES);
+      }
 
 #if defined _WIN32 && !defined __CYGWIN__
     /* X_OK works like R_OK.  */
@@ -76,5 +85,9 @@ main ()
 #endif
   }
 
+  /* Cleanup.  */
+  unlink (BASE "f1");
+  unlink (BASE "f2");
+
   return 0;
 }



                 reply	other threads:[~2019-10-06 23:44 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5867345.KNtYe3x8ii@omega \
    --to=bruno@clisp.org \
    --cc=bug-gnulib@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).