8597553
commit cb3c27e87b914bde5ec00a02363536c76e08b850
8597553
Author: Florian Weimer <fweimer@redhat.com>
8597553
Date:   Wed Jul 5 17:39:33 2017 +0200
8597553
8597553
    support: Add resolver testing mode which does not patch _res
8597553
8597553
diff --git a/resolv/Makefile b/resolv/Makefile
8597553
index e80583c72b96efb4..83f99791e3a097cc 100644
8597553
--- a/resolv/Makefile
8597553
+++ b/resolv/Makefile
8597553
@@ -65,7 +65,9 @@ tests += \
8597553
   tst-resolv-res_init-thread \
8597553
 
8597553
 # Needs resolv_context.
8597553
-tests += tst-resolv-res_ninit
8597553
+tests += \
8597553
+  tst-resolv-res_ninit \
8597553
+  tst-resolv-threads \
8597553
 
8597553
 endif
8597553
 
8597553
@@ -168,6 +170,8 @@ $(objpfx)tst-resolv-res_init-thread: $(libdl) $(objpfx)libresolv.so \
8597553
 $(objpfx)tst-resolv-qtypes: $(objpfx)libresolv.so $(shared-thread-library)
8597553
 $(objpfx)tst-resolv-rotate: $(objpfx)libresolv.so $(shared-thread-library)
8597553
 $(objpfx)tst-resolv-search: $(objpfx)libresolv.so $(shared-thread-library)
8597553
+$(objpfx)tst-resolv-threads: \
8597553
+  $(libdl) $(objpfx)libresolv.so $(shared-thread-library)
8597553
 $(objpfx)tst-resolv-canonname: \
8597553
   $(libdl) $(objpfx)libresolv.so $(shared-thread-library)
8597553
 
8597553
diff --git a/resolv/tst-resolv-threads.c b/resolv/tst-resolv-threads.c
8597553
new file mode 100644
8597553
index 0000000000000000..7be417b056f720d8
8597553
--- /dev/null
8597553
+++ b/resolv/tst-resolv-threads.c
8597553
@@ -0,0 +1,484 @@
8597553
+/* Test basic nss_dns functionality with multiple threads.
8597553
+   Copyright (C) 2016-2017 Free Software Foundation, Inc.
8597553
+   This file is part of the GNU C Library.
8597553
+
8597553
+   The GNU C Library is free software; you can redistribute it and/or
8597553
+   modify it under the terms of the GNU Lesser General Public
8597553
+   License as published by the Free Software Foundation; either
8597553
+   version 2.1 of the License, or (at your option) any later version.
8597553
+
8597553
+   The GNU C Library is distributed in the hope that it will be useful,
8597553
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8597553
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8597553
+   Lesser General Public License for more details.
8597553
+
8597553
+   You should have received a copy of the GNU Lesser General Public
8597553
+   License along with the GNU C Library; if not, see
8597553
+   <http://www.gnu.org/licenses/>.  */
8597553
+
8597553
+/* Unlike tst-resolv-basic, this test does not overwrite the _res
8597553
+   structure and relies on namespaces to achieve the redirection to
8597553
+   the test servers with a custom /etc/resolv.conf file.  */
8597553
+
8597553
+#include <dlfcn.h>
8597553
+#include <errno.h>
8597553
+#include <gnu/lib-names.h>
8597553
+#include <netdb.h>
8597553
+#include <resolv/resolv-internal.h>
8597553
+#include <resolv/resolv_context.h>
8597553
+#include <stdio.h>
8597553
+#include <stdlib.h>
8597553
+#include <string.h>
8597553
+#include <support/check.h>
8597553
+#include <support/namespace.h>
8597553
+#include <support/resolv_test.h>
8597553
+#include <support/support.h>
8597553
+#include <support/temp_file.h>
8597553
+#include <support/test-driver.h>
8597553
+#include <support/xthread.h>
8597553
+#include <support/xunistd.h>
8597553
+
8597553
+/* Each client thread sends this many queries.  */
8597553
+enum { queries_per_thread = 500 };
8597553
+
8597553
+/* Return a small positive number identifying this thread.  */
8597553
+static int
8597553
+get_thread_number (void)
8597553
+{
8597553
+  static int __thread local;
8597553
+  if (local != 0)
8597553
+    return local;
8597553
+  static int global = 1;
8597553
+  local = __atomic_fetch_add (&global, 1, __ATOMIC_RELAXED);
8597553
+  return local;
8597553
+}
8597553
+
8597553
+static void
8597553
+response (const struct resolv_response_context *ctx,
8597553
+          struct resolv_response_builder *b,
8597553
+          const char *qname, uint16_t qclass, uint16_t qtype)
8597553
+{
8597553
+  TEST_VERIFY_EXIT (qname != NULL);
8597553
+
8597553
+  int counter = 0;
8597553
+  int thread = 0;
8597553
+  int dummy = 0;
8597553
+  TEST_VERIFY (sscanf (qname, "counter%d.thread%d.example.com%n",
8597553
+                       &counter, &thread, &dummy) == 2);
8597553
+  TEST_VERIFY (dummy > 0);
8597553
+
8597553
+  struct resolv_response_flags flags = { 0 };
8597553
+  resolv_response_init (b, flags);
8597553
+  resolv_response_add_question (b, qname, qclass, qtype);
8597553
+
8597553
+  resolv_response_section (b, ns_s_an);
8597553
+  resolv_response_open_record (b, qname, qclass, qtype, 0);
8597553
+  switch (qtype)
8597553
+    {
8597553
+    case T_A:
8597553
+      {
8597553
+        char ipv4[4] = {10, 0, counter, thread};
8597553
+        resolv_response_add_data (b, &ipv4, sizeof (ipv4));
8597553
+      }
8597553
+      break;
8597553
+    case T_AAAA:
8597553
+      {
8597553
+        char ipv6[16]
8597553
+          = {0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0,
8597553
+             counter, 0, thread, 0, 0};
8597553
+        resolv_response_add_data (b, &ipv6, sizeof (ipv6));
8597553
+      }
8597553
+      break;
8597553
+    default:
8597553
+      support_record_failure ();
8597553
+      printf ("error: unexpected QTYPE: %s/%u/%u\n",
8597553
+              qname, qclass, qtype);
8597553
+    }
8597553
+  resolv_response_close_record (b);
8597553
+}
8597553
+
8597553
+/* Check that the resolver configuration for this thread has an
8597553
+   extended resolver configuration.  */
8597553
+static void
8597553
+check_have_conf (void)
8597553
+{
8597553
+  struct resolv_context *ctx = __resolv_context_get ();
8597553
+  TEST_VERIFY_EXIT (ctx != NULL);
8597553
+  TEST_VERIFY (ctx->conf != NULL);
8597553
+  __resolv_context_put (ctx);
8597553
+}
8597553
+
8597553
+/* Verify that E matches the expected response for FAMILY and
8597553
+   COUNTER.  */
8597553
+static void
8597553
+check_hostent (const char *caller, const char *function, const char *qname,
8597553
+               int ret, struct hostent *e, int family, int counter)
8597553
+{
8597553
+  if (ret != 0)
8597553
+    {
8597553
+      errno = ret;
8597553
+      support_record_failure ();
8597553
+      printf ("error: %s: %s for %s failed: %m\n", caller, function, qname);
8597553
+      return;
8597553
+    }
8597553
+
8597553
+  TEST_VERIFY_EXIT (e != NULL);
8597553
+  TEST_VERIFY (strcmp (qname, e->h_name) == 0);
8597553
+  TEST_VERIFY (e->h_addrtype == family);
8597553
+  TEST_VERIFY_EXIT (e->h_addr_list[0] != NULL);
8597553
+  TEST_VERIFY (e->h_addr_list[1] == NULL);
8597553
+  switch (family)
8597553
+    {
8597553
+    case AF_INET:
8597553
+      {
8597553
+        char addr[4] = {10, 0, counter, get_thread_number ()};
8597553
+        TEST_VERIFY (e->h_length == sizeof (addr));
8597553
+        TEST_VERIFY (memcmp (e->h_addr_list[0], addr, sizeof (addr)) == 0);
8597553
+      }
8597553
+      break;
8597553
+    case AF_INET6:
8597553
+      {
8597553
+      char addr[16]
8597553
+        = {0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0,
8597553
+           0, counter, 0, get_thread_number (), 0, 0};
8597553
+      TEST_VERIFY (e->h_length == sizeof (addr));
8597553
+      TEST_VERIFY (memcmp (e->h_addr_list[0], addr, sizeof (addr)) == 0);
8597553
+      }
8597553
+      break;
8597553
+    default:
8597553
+      FAIL_EXIT1 ("%s: invalid address family %d", caller, family);
8597553
+    }
8597553
+  check_have_conf ();
8597553
+}
8597553
+
8597553
+/* Check a getaddrinfo result.  */
8597553
+static void
8597553
+check_addrinfo (const char *caller, const char *qname,
8597553
+                int ret, struct addrinfo *ai, int family, int counter)
8597553
+{
8597553
+  if (ret != 0)
8597553
+    {
8597553
+      support_record_failure ();
8597553
+      printf ("error: %s: getaddrinfo for %s failed: %s\n",
8597553
+              caller, qname, gai_strerror (ret));
8597553
+      return;
8597553
+    }
8597553
+
8597553
+  TEST_VERIFY_EXIT (ai != NULL);
8597553
+
8597553
+  /* Check that available data matches the requirements.  */
8597553
+  bool have_ipv4 = false;
8597553
+  bool have_ipv6 = false;
8597553
+  for (struct addrinfo *p = ai; p != NULL; p = p->ai_next)
8597553
+    {
8597553
+      TEST_VERIFY (p->ai_socktype == SOCK_STREAM);
8597553
+      TEST_VERIFY (p->ai_protocol == IPPROTO_TCP);
8597553
+      TEST_VERIFY_EXIT (p->ai_addr != NULL);
8597553
+      TEST_VERIFY (p->ai_addr->sa_family == p->ai_family);
8597553
+
8597553
+      switch (p->ai_family)
8597553
+        {
8597553
+        case AF_INET:
8597553
+          {
8597553
+            TEST_VERIFY (!have_ipv4);
8597553
+            have_ipv4 = true;
8597553
+            struct sockaddr_in *sa = (struct sockaddr_in *) p->ai_addr;
8597553
+            TEST_VERIFY (p->ai_addrlen == sizeof (*sa));
8597553
+            char addr[4] = {10, 0, counter, get_thread_number ()};
8597553
+            TEST_VERIFY (memcmp (&sa->sin_addr, addr, sizeof (addr)) == 0);
8597553
+            TEST_VERIFY (ntohs (sa->sin_port) == 80);
8597553
+          }
8597553
+          break;
8597553
+        case AF_INET6:
8597553
+          {
8597553
+            TEST_VERIFY (!have_ipv6);
8597553
+            have_ipv6 = true;
8597553
+            struct sockaddr_in6 *sa = (struct sockaddr_in6 *) p->ai_addr;
8597553
+            TEST_VERIFY (p->ai_addrlen == sizeof (*sa));
8597553
+            char addr[16]
8597553
+              = {0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0,
8597553
+                 0, counter, 0, get_thread_number (), 0, 0};
8597553
+            TEST_VERIFY (memcmp (&sa->sin6_addr, addr, sizeof (addr)) == 0);
8597553
+            TEST_VERIFY (ntohs (sa->sin6_port) == 80);
8597553
+          }
8597553
+          break;
8597553
+        default:
8597553
+          FAIL_EXIT1 ("%s: invalid address family %d", caller, family);
8597553
+        }
8597553
+    }
8597553
+
8597553
+  switch (family)
8597553
+    {
8597553
+      case AF_INET:
8597553
+        TEST_VERIFY (have_ipv4);
8597553
+        TEST_VERIFY (!have_ipv6);
8597553
+        break;
8597553
+      case AF_INET6:
8597553
+        TEST_VERIFY (!have_ipv4);
8597553
+        TEST_VERIFY (have_ipv6);
8597553
+        break;
8597553
+      case AF_UNSPEC:
8597553
+        TEST_VERIFY (have_ipv4);
8597553
+        TEST_VERIFY (have_ipv6);
8597553
+        break;
8597553
+    default:
8597553
+      FAIL_EXIT1 ("%s: invalid address family %d", caller, family);
8597553
+    }
8597553
+
8597553
+  check_have_conf ();
8597553
+}
8597553
+
8597553
+/* This barrier ensures that all test threads begin their work
8597553
+   simultaneously.  */
8597553
+static pthread_barrier_t barrier;
8597553
+
8597553
+/* Test gethostbyname2_r (if do_2 is false) or gethostbyname2_r with
8597553
+   AF_INET (if do_2 is true).  */
8597553
+static void *
8597553
+byname (bool do_2)
8597553
+{
8597553
+  int this_thread = get_thread_number ();
8597553
+  xpthread_barrier_wait (&barrier);
8597553
+  for (int i = 0; i < queries_per_thread; ++i)
8597553
+    {
8597553
+      char qname[100];
8597553
+      snprintf (qname, sizeof (qname), "counter%d.thread%d.example.com",
8597553
+                i, this_thread);
8597553
+      struct hostent storage;
8597553
+      char buf[1000];
8597553
+      struct hostent *e = NULL;
8597553
+      int herrno;
8597553
+      int ret;
8597553
+      if (do_2)
8597553
+        ret = gethostbyname_r (qname, &storage, buf, sizeof (buf),
8597553
+                               &e, &herrno);
8597553
+      else
8597553
+        ret = gethostbyname2_r (qname, AF_INET, &storage, buf, sizeof (buf),
8597553
+                                &e, &herrno);
8597553
+      check_hostent (__func__, do_2 ? "gethostbyname2_r" : "gethostbyname_r",
8597553
+                     qname, ret, e, AF_INET, i);
8597553
+    }
8597553
+  check_have_conf ();
8597553
+  return NULL;
8597553
+}
8597553
+
8597553
+/* Test gethostbyname_r.  */
8597553
+static void *
8597553
+thread_byname (void *closure)
8597553
+{
8597553
+  return byname (false);
8597553
+}
8597553
+
8597553
+/* Test gethostbyname2_r with AF_INET.  */
8597553
+static void *
8597553
+thread_byname2 (void *closure)
8597553
+{
8597553
+  return byname (true);
8597553
+}
8597553
+
8597553
+/* Call gethostbyname_r with RES_USE_INET6 (if do_2 is false), or
8597553
+   gethostbyname_r with AF_INET6 (if do_2 is true).  */
8597553
+static void *
8597553
+byname_inet6 (bool do_2)
8597553
+{
8597553
+  int this_thread = get_thread_number ();
8597553
+  xpthread_barrier_wait (&barrier);
8597553
+  if (!do_2)
8597553
+    {
8597553
+      res_init ();
8597553
+      _res.options |= DEPRECATED_RES_USE_INET6;
8597553
+      TEST_VERIFY (strcmp (_res.defdname, "example.com") == 0);
8597553
+    }
8597553
+  for (int i = 0; i < queries_per_thread; ++i)
8597553
+    {
8597553
+      char qname[100];
8597553
+      snprintf (qname, sizeof (qname), "counter%d.thread%d.example.com",
8597553
+                i, this_thread);
8597553
+      struct hostent storage;
8597553
+      char buf[1000];
8597553
+      struct hostent *e = NULL;
8597553
+      int herrno;
8597553
+      int ret;
8597553
+      if (do_2)
8597553
+        ret = gethostbyname2_r (qname, AF_INET6, &storage, buf, sizeof (buf),
8597553
+                                &e, &herrno);
8597553
+      else
8597553
+        ret = gethostbyname_r (qname, &storage, buf, sizeof (buf),
8597553
+                               &e, &herrno);
8597553
+      check_hostent (__func__,
8597553
+                     do_2 ? "gethostbyname2_r" : "gethostbyname_r",
8597553
+                     qname, ret, e, AF_INET6, i);
8597553
+    }
8597553
+  return NULL;
8597553
+}
8597553
+
8597553
+/* Test gethostbyname_r with AF_INET6.  */
8597553
+static void *
8597553
+thread_byname_inet6 (void *closure)
8597553
+{
8597553
+  return byname_inet6 (false);
8597553
+}
8597553
+
8597553
+/* Test gethostbyname2_r with AF_INET6.  */
8597553
+static void *
8597553
+thread_byname2_af_inet6 (void *closure)
8597553
+{
8597553
+  return byname_inet6 (true);
8597553
+}
8597553
+
8597553
+/* Run getaddrinfo tests for FAMILY.  */
8597553
+static void *
8597553
+gai (int family, bool do_inet6)
8597553
+{
8597553
+  int this_thread = get_thread_number ();
8597553
+  xpthread_barrier_wait (&barrier);
8597553
+  if (do_inet6)
8597553
+    {
8597553
+      res_init ();
8597553
+      _res.options |= DEPRECATED_RES_USE_INET6;
8597553
+      check_have_conf ();
8597553
+    }
8597553
+  for (int i = 0; i < queries_per_thread; ++i)
8597553
+    {
8597553
+      char qname[100];
8597553
+      snprintf (qname, sizeof (qname), "counter%d.thread%d.example.com",
8597553
+                i, this_thread);
8597553
+      struct addrinfo hints =
8597553
+        {
8597553
+          .ai_family = family,
8597553
+          .ai_socktype = SOCK_STREAM,
8597553
+          .ai_protocol = IPPROTO_TCP,
8597553
+        };
8597553
+      struct addrinfo *ai;
8597553
+      int ret = getaddrinfo (qname, "80", &hints, &ai;;
8597553
+      check_addrinfo (__func__, qname, ret, ai, family, i);
8597553
+      if (ret == 0)
8597553
+        freeaddrinfo (ai);
8597553
+    }
8597553
+  return NULL;
8597553
+}
8597553
+
8597553
+/* Test getaddrinfo with AF_INET.  */
8597553
+static void *
8597553
+thread_gai_inet (void *closure)
8597553
+{
8597553
+  return gai (AF_INET, false);
8597553
+}
8597553
+
8597553
+/* Test getaddrinfo with AF_INET6.  */
8597553
+static void *
8597553
+thread_gai_inet6 (void *closure)
8597553
+{
8597553
+  return gai (AF_INET6, false);
8597553
+}
8597553
+
8597553
+/* Test getaddrinfo with AF_UNSPEC.  */
8597553
+static void *
8597553
+thread_gai_unspec (void *closure)
8597553
+{
8597553
+  return gai (AF_UNSPEC, false);
8597553
+}
8597553
+
8597553
+/* Test getaddrinfo with AF_INET.  */
8597553
+static void *
8597553
+thread_gai_inet_inet6 (void *closure)
8597553
+{
8597553
+  return gai (AF_INET, true);
8597553
+}
8597553
+
8597553
+/* Test getaddrinfo with AF_INET6.  */
8597553
+static void *
8597553
+thread_gai_inet6_inet6 (void *closure)
8597553
+{
8597553
+  return gai (AF_INET6, true);
8597553
+}
8597553
+
8597553
+/* Test getaddrinfo with AF_UNSPEC.  */
8597553
+static void *
8597553
+thread_gai_unspec_inet6 (void *closure)
8597553
+{
8597553
+  return gai (AF_UNSPEC, true);
8597553
+}
8597553
+
8597553
+/* Description of the chroot environment used to run the tests.  */
8597553
+static struct support_chroot *chroot_env;
8597553
+
8597553
+/* Set up the chroot environment.  */
8597553
+static void
8597553
+prepare (int argc, char **argv)
8597553
+{
8597553
+  chroot_env = support_chroot_create
8597553
+    ((struct support_chroot_configuration)
8597553
+     {
8597553
+       .resolv_conf =
8597553
+         "search example.com\n"
8597553
+         "nameserver 127.0.0.1\n"
8597553
+         "nameserver 127.0.0.2\n"
8597553
+         "nameserver 127.0.0.3\n",
8597553
+     });
8597553
+}
8597553
+
8597553
+static int
8597553
+do_test (void)
8597553
+{
8597553
+  support_become_root ();
8597553
+  if (!support_enter_network_namespace ())
8597553
+    return EXIT_UNSUPPORTED;
8597553
+  if (!support_can_chroot ())
8597553
+    return EXIT_UNSUPPORTED;
8597553
+
8597553
+  /* Load the shared object outside of the chroot.  */
8597553
+  TEST_VERIFY (dlopen (LIBNSS_DNS_SO, RTLD_LAZY) != NULL);
8597553
+
8597553
+  xchroot (chroot_env->path_chroot);
8597553
+  TEST_VERIFY_EXIT (chdir ("/") == 0);
8597553
+
8597553
+  struct sockaddr_in server_address =
8597553
+    {
8597553
+      .sin_family = AF_INET,
8597553
+      .sin_addr = { .s_addr = htonl (INADDR_LOOPBACK) },
8597553
+      .sin_port = htons (53)
8597553
+    };
8597553
+  const struct sockaddr *server_addresses[1] =
8597553
+    { (const struct sockaddr *) &server_address };
8597553
+
8597553
+  struct resolv_test *aux = resolv_test_start
8597553
+    ((struct resolv_redirect_config)
8597553
+     {
8597553
+       .response_callback = response,
8597553
+       .nscount = 1,
8597553
+       .disable_redirect = true,
8597553
+       .server_address_overrides = server_addresses,
8597553
+     });
8597553
+
8597553
+  enum { thread_count = 10 };
8597553
+  xpthread_barrier_init (&barrier, NULL, thread_count + 1);
8597553
+  pthread_t threads[thread_count];
8597553
+  typedef void *(*thread_func) (void *);
8597553
+  thread_func thread_funcs[thread_count] =
8597553
+    {
8597553
+      thread_byname,
8597553
+      thread_byname2,
8597553
+      thread_byname_inet6,
8597553
+      thread_byname2_af_inet6,
8597553
+      thread_gai_inet,
8597553
+      thread_gai_inet6,
8597553
+      thread_gai_unspec,
8597553
+      thread_gai_inet_inet6,
8597553
+      thread_gai_inet6_inet6,
8597553
+      thread_gai_unspec_inet6,
8597553
+    };
8597553
+  for (int i = 0; i < thread_count; ++i)
8597553
+    threads[i] = xpthread_create (NULL, thread_funcs[i], NULL);
8597553
+  xpthread_barrier_wait (&barrier); /* Start the test threads.  */
8597553
+  for (int i = 0; i < thread_count; ++i)
8597553
+    xpthread_join (threads[i]);
8597553
+
8597553
+  resolv_test_end (aux);
8597553
+  support_chroot_free (chroot_env);
8597553
+
8597553
+  return 0;
8597553
+}
8597553
+
8597553
+#define PREPARE prepare
8597553
+#include <support/test-driver.c>