changeset 16149:4e3101cbdf81

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.
author Bruno Haible <bruno@clisp.org>
date Sat, 03 Dec 2011 14:50:45 +0100
parents 3225b19f3d2d
children ceb95637edc6
files ChangeLog modules/sethostname-tests tests/test-sethostname.c
diffstat 3 files changed, 37 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- 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.
--- a/modules/sethostname-tests
+++ b/modules/sethostname-tests
@@ -1,12 +1,13 @@
 Files:
+tests/test-sethostname.c
 tests/signature.h
-tests/test-sethostname.c
 tests/macros.h
 
 Depends-on:
 sys_types
 
 configure.ac:
+AC_CHECK_FUNCS_ONCE([geteuid])
 
 Makefile.am:
 TESTS += test-sethostname
--- a/tests/test-sethostname.c
+++ b/tests/test-sethostname.c
@@ -55,13 +55,16 @@
      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 @@
   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 @@
 
   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;
 }