[PATCH 2/6] nss: Use test resolver for tst-getaddrinfo5

Adhemerval Zanella <[email protected]>
Newsgroups gmane.comp.lib.glibc.alpha
Message-ID <[email protected]>
Rewrite the BZ#6469 test to use the resolv_test framework instead of
querying the network, so it can run as a regular test.
---
 nss/Makefile           |  4 +-
 nss/tst-getaddrinfo5.c | 83 +++++++++++++++++++-----------------------
 2 files changed, 41 insertions(+), 46 deletions(-)

diff --git a/nss/Makefile b/nss/Makefile
index 5a4d5794b36..bbcb7201699 100644
--- a/nss/Makefile
+++ b/nss/Makefile
@@ -323,6 +323,7 @@ tests := \
   tst-getaddrinfo2 \
   tst-getaddrinfo3 \
   tst-getaddrinfo4 \
+  tst-getaddrinfo5 \
   tst-gethnm \
   tst-getpw \
   tst-gshadow \
@@ -344,7 +345,6 @@ tests := \
 
 xtests := \
   bug-erange \
-  tst-getaddrinfo5 \
   # xtests
 
 tests-container := \
@@ -515,6 +515,8 @@ $(objpfx)tst-nss-files-alias-truncated.out: $(objpfx)libnss_files.so
 
 $(objpfx)tst-getaddrinfo4: \
   $(common-objpfx)resolv/libresolv.so $(shared-thread-library)
+$(objpfx)tst-getaddrinfo5: \
+  $(common-objpfx)resolv/libresolv.so $(shared-thread-library)
 
 $(objpfx)tst-default-domain: $(objpfx)nisdomain.os
 
diff --git a/nss/tst-getaddrinfo5.c b/nss/tst-getaddrinfo5.c
index f00b27372e2..a8297ede3c8 100644
--- a/nss/tst-getaddrinfo5.c
+++ b/nss/tst-getaddrinfo5.c
@@ -16,54 +16,47 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <sys/types.h>
-#include <sys/socket.h>
 #include <netdb.h>
 #include <stdio.h>
-#include <string.h>
+#include <support/check.h>
+#include <support/resolv_test.h>
 
-static int
-test (void)
+static void
+response (const struct resolv_response_context *ctx,
+          struct resolv_response_builder *b,
+          const char *qname, uint16_t qclass, uint16_t qtype)
 {
-  static char host1[] = "localhost..";
-  static char host2[] = "www.gnu.org..";
-  static char *hosts[] = { host1, host2 };
-  int i;
-  int pass = 0;
-
-  for (i = 0; i < sizeof (hosts) / sizeof (*hosts); i++)
-    {
-      char *host = hosts[i];
-      size_t len = strlen (host);
-      struct addrinfo *ai;
-
-      /* If the name doesn't resolve with a single dot at the
-	 end, skip it.  */
-      host[len-1] = 0;
-      if (getaddrinfo (host, NULL, NULL, &ai) != 0)
-	{
-	  printf ("resolving \"%s\" failed, skipping this hostname\n", host);
-	  continue;
-	}
-      printf ("resolving \"%s\" worked, proceeding to test\n", host);
-      freeaddrinfo (ai);
-
-      /* If it resolved with a single dot, check that it doesn't with
-	 a second trailing dot.  */
-      host[len-1] = '.';
-      if (getaddrinfo (host, NULL, NULL, &ai) == 0)
-	{
-	  printf ("resolving \"%s\" worked, test failed\n", host);
-	  return 1;
-	}
-      printf ("resolving \"%s\" failed, test passed\n", host);
-      pass = 1;
-    }
-
-  /* We want at least one successful name resolution for the test to
-     succeed.  */
-  return pass ? 0 : 2;
+  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);
+  if (qtype == T_A)
+    resolv_response_add_data (b, "\xc0\x00\x02\x01", 4);
+  else
+    resolv_response_add_data
+      (b, "\x20\x01\x0d\xb8\0\0\0\0\0\0\0\0\0\0\0\x01", 16);
+  resolv_response_close_record (b);
 }
 
-#define TEST_FUNCTION test ()
-#include "../test-skeleton.c"
+static int
+do_test (void)
+{
+  struct resolv_test *obj = resolv_test_start
+    ((struct resolv_redirect_config) { .response_callback = response });
+
+  struct addrinfo *ai;
+
+  /* A single trailing dot resolves.  */
+  printf ("resolving \"example.net.\", expecting success\n");
+  TEST_COMPARE (getaddrinfo ("example.net.", NULL, NULL, &ai), 0);
+  freeaddrinfo (ai);
+
+  /* A second trailing dot must not.  */
+  printf ("resolving \"example.net..\", expecting failure\n");
+  TEST_COMPARE (getaddrinfo ("example.net..", NULL, NULL, &ai), EAI_NONAME);
+
+  resolv_test_end (obj);
+  return 0;
+}
+
+#include <support/test-driver.c>
-- 
2.43.0
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.