Adding a few tests based on longstanding POSIX specification (i.e. not
added in 2024).

* AF_UNSPEC should be 0.
* All supported AF_* constants must have distinct values.
* SHUT_RD, SHUT_RDWR, SHUT_WR must have distinct values.

Gnulib doesn't guarantee this but it would be good to know if a platform
doesn't satisfy these rules.

Collin

>From 4f7969ccb3036e9bef5bfc2fbfef3263d5255376 Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.fu...@gmail.com>
Date: Mon, 22 Jul 2024 20:54:17 -0700
Subject: [PATCH] sys_socket tests: Improve tests for macro definitions.

* modules/sys_socket-tests (Depends-on): Add assert-h.
* tests/test-sys_socket.c (a): Remove variable.
(main): Test that each supported address family is defined to a distinct
value. Test that each constant passed as the second argument of
'shutdown' is defined to a distinct value.
---
 ChangeLog                |  9 +++++++++
 modules/sys_socket-tests |  1 +
 tests/test-sys_socket.c  | 35 +++++++++++++++++++++++++++++++----
 3 files changed, 41 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 335a29b0c0..cf20d60b76 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2024-07-22  Collin Funk  <collin.fu...@gmail.com>
+
+	sys_socket tests: Improve tests for macro definitions.
+	* modules/sys_socket-tests (Depends-on): Add assert-h.
+	* tests/test-sys_socket.c (a): Remove variable.
+	(main): Test that each supported address family is defined to a distinct
+	value. Test that each constant passed as the second argument of
+	'shutdown' is defined to a distinct value.
+
 2024-07-22  Bruno Haible  <br...@clisp.org>
 
 	gnulib-tool: Omit the logs of skipped tests from test-suite.log.
diff --git a/modules/sys_socket-tests b/modules/sys_socket-tests
index da67baf99a..b0bb4356ca 100644
--- a/modules/sys_socket-tests
+++ b/modules/sys_socket-tests
@@ -2,6 +2,7 @@ Files:
 tests/test-sys_socket.c
 
 Depends-on:
+assert-h
 sys_socket-c++-tests
 
 configure.ac:
diff --git a/tests/test-sys_socket.c b/tests/test-sys_socket.c
index 4e0f9ad46e..6fbfb98a70 100644
--- a/tests/test-sys_socket.c
+++ b/tests/test-sys_socket.c
@@ -20,10 +20,8 @@
 
 #include <sys/socket.h>
 
-#if HAVE_SHUTDOWN
-/* Check some integer constant expressions.  */
-int a[] = { SHUT_RD, SHUT_WR, SHUT_RDWR };
-#endif
+/* POSIX mandates that AF_UNSPEC shall be 0.  */
+static_assert (AF_UNSPEC == 0);
 
 /* Check that the 'socklen_t' type is defined.  */
 socklen_t t1;
@@ -59,6 +57,35 @@ main (void)
       break;
     }
 
+  /* Check that each supported address family has a distinct value.  */
+  switch (0)
+    {
+    case AF_UNSPEC:
+#if HAVE_IPV4
+    case AF_INET:
+#endif
+#if HAVE_IPV6
+    case AF_INET6:
+#endif
+#if HAVE_UNIXSOCKET
+    case AF_UNIX:
+#endif
+    default:
+      break;
+    }
+
+  /* Check that the shutdown type macros are defined to distinct values.  */
+#if HAVE_SHUTDOWN
+  switch (0)
+    {
+    case SHUT_RD:
+    case SHUT_WR:
+    case SHUT_RDWR:
+    default:
+      break;
+    }
+#endif
+
   x.ss_family = 42;
   i = 42;
   msg.msg_iov = &io;
-- 
2.45.2

Reply via email to