Tweak last commit.
authorBruno Haible <bruno@clisp.org>
Sat, 3 Dec 2011 13:50:45 +0000 (14:50 +0100)
committerBruno Haible <bruno@clisp.org>
Sat, 3 Dec 2011 13:50:45 +0000 (14:50 +0100)
* modules/sethostname-tests (Files): Sort by decreasing importance.
(configure.ac): Check for geteuid.
* tests/test-sethostname.c (main): Emit error messages to stderr. Skip
the test when there's nothing to test. Drop an unnecessary cast.
Improve an error message. Verify that the final sethostname() call
succeeds.

ChangeLog
modules/sethostname-tests
tests/test-sethostname.c

index 6b6763a..4fd7b0d 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2011-12-03  Bruno Haible  <bruno@clisp.org>
+
+       Tweak last commit.
+       * modules/sethostname-tests (Files): Sort by decreasing importance.
+       (configure.ac): Check for geteuid.
+       * tests/test-sethostname.c (main): Emit error messages to stderr. Skip
+       the test when there's nothing to test. Drop an unnecessary cast.
+       Improve an error message. Verify that the final sethostname() call
+       succeeds.
+
 2011-12-01  Ben Walton  <bwalton@artsci.utoronto.ca>
 
        Add a test suite for the sethostname module.
index 040b380..60785f0 100644 (file)
@@ -1,12 +1,13 @@
 Files:
-tests/signature.h
 tests/test-sethostname.c
+tests/signature.h
 tests/macros.h
 
 Depends-on:
 sys_types
 
 configure.ac:
+AC_CHECK_FUNCS_ONCE([geteuid])
 
 Makefile.am:
 TESTS += test-sethostname
index 249c064..b9f6a9f 100644 (file)
@@ -55,13 +55,16 @@ 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)
-    return 0;
+  if (geteuid () != 0)
+    {
+      fprintf (stderr, "Skipping test: insufficient permissions.\n");
+      return 77;
+    }
 
   /* we want to ensure we can do a get/set/get check to ensure the
      change is accepted. record the current name so it can be restored
      later */
-  ASSERT(gethostname (origname, sizeof (origname)) == 0);
+  ASSERT (gethostname (origname, sizeof (origname)) == 0);
 
   /* try setting a valid hostname.  if it fails -1/ENOSYS, we will
      skip the test for long names as this is an indication we're using
@@ -71,25 +74,29 @@ main (int argc, char *argv[] _GL_UNUSED)
   if (rcs != 0)
     {
       if (rcs == -1 && errno == ENOSYS)
-       return 0;
+        {
+          fprintf (stderr,
+                   "Skipping test: sethostname is not really implemented.\n");
+          return 77;
+        }
       else
-       {
-         printf ("error setting valid hostname.\n");
-         return 1;
-       }
+        {
+          fprintf (stderr, "error setting valid hostname.\n");
+          return 1;
+        }
     }
   else
     {
       ASSERT (gethostname (newname, sizeof (newname)) == 0);
 
       /* 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. */
+         restore the original name as we will assume it was not
+         properly changed. */
       if (strcmp (newname, TESTHOSTNAME) != 0)
-       {
-         printf ("set/get comparison failed.\n");
-         return 1;
-       }
+        {
+          fprintf (stderr, "set/get comparison failed.\n");
+          return 1;
+        }
     }
 
   /* glibc does allow setting a zero length name, so the lower bound
@@ -100,18 +107,18 @@ main (int argc, char *argv[] _GL_UNUSED)
 
   longname[i] = '\0';
 
-  rcs = sethostname ((const char *) longname, (HOST_NAME_MAX + 1));
+  rcs = sethostname (longname, (HOST_NAME_MAX + 1));
 
   if (rcs != -1)
     {
       /* attempt to restore the original name. */
-      sethostname (origname, strlen (origname));
-      printf ("expected failure when setting very long hostname.\n");
+      ASSERT (sethostname (origname, strlen (origname)) == 0);
+      fprintf (stderr, "setting a too long hostname succeeded.\n");
       return 1;
     }
 
   /* restore the original name. */
-  sethostname (origname, strlen (origname));
+  ASSERT (sethostname (origname, strlen (origname)) == 0);
 
   return 0;
 }