error: depend on stdio
[gnulib.git] / tests / test-sethostname2.c
index 51f92ae..a96f36a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011 Free Software Foundation, Inc.
+ * Copyright (C) 2011-2013 Free Software Foundation, Inc.
  * Written by Ben Walton.
  *
  * This program is free software: you can redistribute it and/or modify
 #include <errno.h>
 #include <stdio.h>
 
+#include "root-uid.h"
+
 #include "macros.h"
 
 #define TESTHOSTNAME "gnulib-hostname"
 
-/* mingw and MSVC 9 lack geteuid, so setup a dummy value.  */
-#if !HAVE_GETEUID
-# define geteuid() 0
+/* mingw and MSVC 9 lack geteuid, so setup a dummy value.
+   On Cygwin, geteuid() may return non-zero even for user accounts with
+   administrator privileges, so use a dummy value as well.  */
+#if !HAVE_GETEUID || defined __CYGWIN__
+# define geteuid() ROOT_UID
 #endif
 
 int
@@ -48,7 +52,7 @@ main (int argc, char *argv[] _GL_UNUSED)
      consider things like CAP_SYS_ADMIN (linux) or PRIV_SYS_ADMIN
      (solaris), etc.  systems without a working geteuid (mingw, MSVC
      9) will always skip this test. */
-  if (geteuid () != 0)
+  if (geteuid () != ROOT_UID)
     {
       fprintf (stderr, "Skipping test: insufficient permissions.\n");
       return 77;
@@ -72,6 +76,11 @@ main (int argc, char *argv[] _GL_UNUSED)
                    "Skipping test: sethostname is not really implemented.\n");
           return 77;
         }
+      else if (rcs == -1 && errno == EPERM)
+        {
+          fprintf (stderr, "Skipping test: insufficient permissions.\n");
+          return 77;
+        }
       else
         {
           fprintf (stderr, "error setting valid hostname.\n");
@@ -82,6 +91,10 @@ main (int argc, char *argv[] _GL_UNUSED)
     {
       ASSERT (gethostname (newname, sizeof (newname)) == 0);
 
+      /* On Windows, a hostname change becomes effective only after
+         a reboot.  */
+#if !((defined _WIN32 || defined __WIN32__) || defined __CYGWIN__)
+
       /* if we don't get back what we put in, there is no need to
          restore the original name as we will assume it was not
          properly changed. */
@@ -90,6 +103,7 @@ main (int argc, char *argv[] _GL_UNUSED)
           fprintf (stderr, "set/get comparison failed.\n");
           return 1;
         }
+#endif
     }
 
   /* glibc does allow setting a zero length name, so the lower bound