[glibc] nss: Use test resolver for tst-getaddrinfo4

Adhemerval Zanella via Glibc-cvs <[email protected]>
Newsgroups gmane.comp.lib.glibc.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=666c2acb7d1cdc348a6de495d9877e9718a6c8d4

commit 666c2acb7d1cdc348a6de495d9877e9718a6c8d4
Author: Adhemerval Zanella <[email protected]>
Date:   Thu Jul 9 14:48:14 2026 -0300

    nss: Use test resolver for tst-getaddrinfo4
    
    Rewrite the BZ#15339 test to use the resolv_test framework instead of
    querying the network, so it can run as a regular test.
    
    Reviewed-by: Florian Weimer <[email protected]>

Diff:
---
 nss/Makefile           |  5 +++-
 nss/tst-getaddrinfo4.c | 73 +++++++++++++++++++++++++++++++++-----------------
 2 files changed, 53 insertions(+), 25 deletions(-)

diff --git a/nss/Makefile b/nss/Makefile
index b1f25f0d71..5a4d5794b3 100644
--- a/nss/Makefile
+++ b/nss/Makefile
@@ -322,6 +322,7 @@ tests := \
   tst-getaddrinfo \
   tst-getaddrinfo2 \
   tst-getaddrinfo3 \
+  tst-getaddrinfo4 \
   tst-gethnm \
   tst-getpw \
   tst-gshadow \
@@ -343,7 +344,6 @@ tests := \
 
 xtests := \
   bug-erange \
-  tst-getaddrinfo4 \
   tst-getaddrinfo5 \
   # xtests
 
@@ -513,6 +513,9 @@ endif
 $(objpfx)tst-nss-files-alias-leak.out: $(objpfx)libnss_files.so
 $(objpfx)tst-nss-files-alias-truncated.out: $(objpfx)libnss_files.so
 
+$(objpfx)tst-getaddrinfo4: \
+  $(common-objpfx)resolv/libresolv.so $(shared-thread-library)
+
 $(objpfx)tst-default-domain: $(objpfx)nisdomain.os
 
 tst-nss-gai-hv2-canonname-ENV = \
diff --git a/nss/tst-getaddrinfo4.c b/nss/tst-getaddrinfo4.c
index c04e12c4b5..67d1acd6f5 100644
--- a/nss/tst-getaddrinfo4.c
+++ b/nss/tst-getaddrinfo4.c
@@ -16,52 +16,77 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <string.h>
 #include <stdio.h>
-#include <errno.h>
 #include <netdb.h>
+#include <string.h>
+#include <support/check.h>
+#include <support/resolv_test.h>
 
-static int
+static void
+response (const struct resolv_response_context *ctx,
+          struct resolv_response_builder *b,
+          const char *qname, uint16_t qclass, uint16_t qtype)
+{
+  resolv_response_init (b, (struct resolv_response_flags) { });
+  resolv_response_add_question (b, qname, qclass, qtype);
+  resolv_response_section (b, ns_s_an);
+  resolv_response_open_record (b, qname, qclass, qtype, 0);
+  switch (qtype)
+    {
+    case T_A:
+      resolv_response_add_data (b, "\xc0\x00\x02\x01", 4);
+      break;
+    case T_AAAA:
+      resolv_response_add_data
+        (b, "\x20\x01\x0d\xb8\0\0\0\0\0\0\0\0\0\0\0\x01", 16);
+      break;
+    }
+  resolv_response_close_record (b);
+}
+
+static void
 try (const char *service, int family, int flags)
 {
-  struct addrinfo hints, *h, *ai;
-  int res;
+  struct addrinfo hints, *ai;
 
   memset (&hints, 0, sizeof hints);
   hints.ai_family = family;
   hints.ai_flags = flags;
 
-  errno = 0;
-  h = (family || flags) ? &hints : NULL;
-  res = getaddrinfo ("example.net", service, h, &ai);
+  int res = getaddrinfo ("example.net", service,
+                         (family || flags) ? &hints : NULL, &ai);
   switch (res)
     {
     case 0:
+      freeaddrinfo (ai);
+      /* Fall through.  */
     case EAI_AGAIN:
     case EAI_NONAME:
       printf ("SUCCESS getaddrinfo(service=%s, family=%d, flags=%d): %s: %m\n",
               service ?: "NULL", family, flags, gai_strerror (res));
-      return 0;
+      return;
     }
-  printf ("FAIL getaddrinfo(service=%s, family=%d, flags=%d): %s: %m\n",
-          service ?: "NULL", family, flags, gai_strerror (res));
-  return 1;
+  FAIL ("getaddrinfo(service=%s, family=%d, flags=%d): %s",
+        service ?: "NULL", family, flags, gai_strerror (res));
 }
 
 static int
 do_test (void)
 {
-  int err = 0;
-  err |= try (NULL, 0, 0);
-  err |= try (NULL, AF_UNSPEC, AI_ADDRCONFIG);
-  err |= try (NULL, AF_INET, 0);
-  err |= try (NULL, AF_INET6, 0);
-  err |= try ("http", 0, 0);
-  err |= try ("http", AF_UNSPEC, AI_ADDRCONFIG);
-  err |= try ("http", AF_INET, 0);
-  err |= try ("http", AF_INET6, 0);
-  return err;
+  struct resolv_test *obj = resolv_test_start
+    ((struct resolv_redirect_config) { .response_callback = response });
+
+  try (NULL, 0, 0);
+  try (NULL, AF_UNSPEC, AI_ADDRCONFIG);
+  try (NULL, AF_INET, 0);
+  try (NULL, AF_INET6, 0);
+  try ("http", 0, 0);
+  try ("http", AF_UNSPEC, AI_ADDRCONFIG);
+  try ("http", AF_INET, 0);
+  try ("http", AF_INET6, 0);
+
+  resolv_test_end (obj);
+  return 0;
 }
 
-#define TEST_FUNCTION do_test ()
-#include "../test-skeleton.c"
+#include <support/test-driver.c>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.