Blame resolv/tst-bug18665.c

Packit 6c4009
/* Test for __libc_res_nsend buffer mismanagent (bug 18665), UDP case.
Packit 6c4009
   Copyright (C) 2016-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <netdb.h>
Packit 6c4009
#include <resolv.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <support/check.h>
Packit 6c4009
#include <support/resolv_test.h>
Packit 6c4009
#include <support/xthread.h>
Packit 6c4009
Packit 6c4009
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
Packit 6c4009
Packit 6c4009
static int initial_address_count;
Packit 6c4009
static int response_count;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
response (const struct resolv_response_context *ctx,
Packit 6c4009
          struct resolv_response_builder *b,
Packit 6c4009
          const char *qname, uint16_t qclass, uint16_t qtype)
Packit 6c4009
{
Packit 6c4009
  TEST_VERIFY_EXIT (qname != NULL);
Packit 6c4009
  struct resolv_response_flags flags = {};
Packit 6c4009
  resolv_response_init (b, flags);
Packit 6c4009
  resolv_response_add_question (b, qname, qclass, qtype);
Packit 6c4009
Packit 6c4009
  resolv_response_section (b, ns_s_an);
Packit 6c4009
Packit 6c4009
  /* Add many A/AAAA records to the second response.  */
Packit 6c4009
  int address_count;
Packit 6c4009
  xpthread_mutex_lock (&lock);
Packit 6c4009
  if (response_count == 0)
Packit 6c4009
    address_count = initial_address_count;
Packit 6c4009
  else
Packit 6c4009
    address_count = 2000;
Packit 6c4009
  ++response_count;
Packit 6c4009
  xpthread_mutex_unlock (&lock);
Packit 6c4009
Packit 6c4009
  for (int i = 0; i < address_count; ++i)
Packit 6c4009
    {
Packit 6c4009
      resolv_response_open_record (b, qname, qclass, qtype, 0);
Packit 6c4009
      switch (qtype)
Packit 6c4009
        {
Packit 6c4009
        case T_A:
Packit 6c4009
          {
Packit 6c4009
            char ipv4[4] = {10, i >> 8, i, 0};
Packit 6c4009
            ipv4[3] = 2 * ctx->tcp + 4 * ctx->server_index;
Packit 6c4009
            resolv_response_add_data (b, &ipv4, sizeof (ipv4));
Packit 6c4009
          }
Packit 6c4009
          break;
Packit 6c4009
        case T_AAAA:
Packit 6c4009
          {
Packit 6c4009
            char ipv6[16]
Packit 6c4009
              = {0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Packit 6c4009
                 i >> 8, i, 0};
Packit 6c4009
            ipv6[15] = 2 * ctx->tcp + 4 * ctx->server_index;
Packit 6c4009
            resolv_response_add_data (b, &ipv6, sizeof (ipv6));
Packit 6c4009
          }
Packit 6c4009
          break;
Packit 6c4009
        default:
Packit 6c4009
          support_record_failure ();
Packit 6c4009
          printf ("error: unexpected QTYPE: %s/%u/%u\n",
Packit 6c4009
                  qname, qclass, qtype);
Packit 6c4009
        }
Packit 6c4009
      resolv_response_close_record (b);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
test_different_sizes (void)
Packit 6c4009
{
Packit 6c4009
  struct addrinfo hints = { .ai_family = AF_UNSPEC, };
Packit 6c4009
  struct addrinfo *ai;
Packit 6c4009
  int ret;
Packit 6c4009
Packit 6c4009
  /* This magic number produces a response size close to 2048
Packit 6c4009
     bytes.  */
Packit 6c4009
  initial_address_count = 126;
Packit 6c4009
  response_count = 0;
Packit 6c4009
Packit 6c4009
  ret = getaddrinfo ("www.example", "80", &hints, &ai;;
Packit 6c4009
  if (ret == 0)
Packit 6c4009
    freeaddrinfo (ai);
Packit 6c4009
Packit 6c4009
  response_count = 0;
Packit 6c4009
  ret = getaddrinfo ("www123.example", "80", &hints, &ai;;
Packit 6c4009
  if (ret == 0)
Packit 6c4009
    freeaddrinfo (ai);
Packit 6c4009
Packit 6c4009
  response_count = 0;
Packit 6c4009
  ret = getaddrinfo ("www1234.example", "80", &hints, &ai;;
Packit 6c4009
  if (ret == 0)
Packit 6c4009
    freeaddrinfo (ai);
Packit 6c4009
Packit 6c4009
  response_count = 0;
Packit 6c4009
  ret = getaddrinfo ("www12345.example", "80", &hints, &ai;;
Packit 6c4009
  if (ret == 0)
Packit 6c4009
    freeaddrinfo (ai);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  struct resolv_test *obj = resolv_test_start
Packit 6c4009
    ((struct resolv_redirect_config)
Packit 6c4009
     {
Packit 6c4009
       .response_callback = response
Packit 6c4009
     });
Packit 6c4009
Packit 6c4009
  test_different_sizes ();
Packit 6c4009
Packit 6c4009
  _res.options |= RES_SNGLKUP;
Packit 6c4009
  test_different_sizes ();
Packit 6c4009
Packit 6c4009
  _res.options |= RES_SNGLKUPREOP;
Packit 6c4009
  test_different_sizes ();
Packit 6c4009
Packit 6c4009
  resolv_test_end (obj);
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>