glibc/resolv/tst-resolv-canonname.c
Paul Eggert 5a82c74822 Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:

sed -ri '
  s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
  s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
  $(find $(git ls-files) -prune -type f \
      ! -name '*.po' \
      ! -name 'ChangeLog*' \
      ! -path COPYING ! -path COPYING.LIB \
      ! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
      ! -path manual/texinfo.tex ! -path scripts/config.guess \
      ! -path scripts/config.sub ! -path scripts/install-sh \
      ! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
      ! -path INSTALL ! -path  locale/programs/charmap-kw.h \
      ! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
      ! '(' -name configure \
            -execdir test -f configure.ac -o -f configure.in ';' ')' \
      ! '(' -name preconfigure \
            -execdir test -f preconfigure.ac ';' ')' \
      -print)

and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:

  chmod a+x sysdeps/unix/sysv/linux/riscv/configure
  # Omit irrelevant whitespace and comment-only changes,
  # perhaps from a slightly-different Autoconf version.
  git checkout -f \
    sysdeps/csky/configure \
    sysdeps/hppa/configure \
    sysdeps/riscv/configure \
    sysdeps/unix/sysv/linux/csky/configure
  # Omit changes that caused a pre-commit check to fail like this:
  # remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
  git checkout -f \
    sysdeps/powerpc/powerpc64/ppc-mcount.S \
    sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
  # Omit change that caused a pre-commit check to fail like this:
  # remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
  git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 02:43:31 -07:00

314 lines
10 KiB
C

/* Test _nss_dns_getcanonname_r corner cases.
Copyright (C) 2017-2019 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <dlfcn.h>
#include <errno.h>
#include <gnu/lib-names.h>
#include <netdb.h>
#include <nss.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <support/check.h>
#include <support/resolv_test.h>
#include <support/support.h>
/* _nss_dns_getcanonname_r is not called during regular operation
because nss_dns directly provides a canonical name, so we have to
test it directly. The function pointer is initialized by do_test
below. */
static enum nss_status
(*getcanonname) (const char *name, char *buffer, size_t buflen,
char **result, int *errnop, int *h_errnop);
static void
response (const struct resolv_response_context *ctx,
struct resolv_response_builder *b,
const char *qname, uint16_t qclass, uint16_t qtype)
{
int code;
{
char *tail;
if (sscanf (qname, "code%d.%ms", &code, &tail) != 2
|| strcmp (tail, "example") != 0)
FAIL_EXIT1 ("error: invalid QNAME: %s\n", qname);
free (tail);
}
switch (code)
{
case 1:
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, "www.example", qclass, qtype, 0);
resolv_response_add_data (b, "\xC0\x00\x02\x01", 4);
resolv_response_close_record (b);
break;
case 2:
resolv_response_init (b, (struct resolv_response_flags) {});
resolv_response_add_question (b, qname, qclass, qtype);
resolv_response_section (b, ns_s_an);
if (qtype == T_AAAA)
{
resolv_response_open_record (b, "www.example", qclass, qtype, 0);
resolv_response_add_data (b, "\xC0\x00\x02\x01", 4);
resolv_response_close_record (b);
for (int i = 0; i < 30000; ++i)
resolv_response_add_data (b, "", 1);
}
break;
case 3:
resolv_response_init (b, (struct resolv_response_flags) {});
resolv_response_add_question (b, qname, qclass, qtype);
resolv_response_section (b, ns_s_an);
if (qtype == T_AAAA)
{
resolv_response_open_record (b, "www.example", qclass, qtype, 0);
resolv_response_add_data (b, "\xC0\x00\x02\x01", 4);
resolv_response_close_record (b);
}
else
{
for (int i = 0; i < 30000; ++i)
resolv_response_add_data (b, "", 1);
}
break;
case 4:
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, T_CNAME, 0);
resolv_response_add_name (b, "www.example");
resolv_response_close_record (b);
resolv_response_open_record (b, "www.example", qclass, qtype, 0);
resolv_response_add_data (b, "\xC0\x00\x02\x01", 4);
resolv_response_close_record (b);
break;
case 5:
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, T_CNAME, 0);
resolv_response_add_name (b, "www.example");
resolv_response_close_record (b);
resolv_response_open_record (b, qname, qclass, T_CNAME, 0);
resolv_response_add_name (b, "www1.example");
resolv_response_close_record (b);
resolv_response_open_record (b, "www1.example", qclass, qtype, 0);
resolv_response_add_data (b, "\xC0\x00\x02\x01", 4);
resolv_response_close_record (b);
break;
case 6:
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, T_CNAME, 0);
resolv_response_add_name (b, "www.example");
resolv_response_close_record (b);
resolv_response_open_record (b, qname, qclass, 46 /* RRSIG */, 0);
resolv_response_add_name (b, ".");
resolv_response_close_record (b);
resolv_response_open_record (b, "www.example", qclass, qtype, 0);
resolv_response_add_data (b, "\xC0\x00\x02\x01", 4);
resolv_response_close_record (b);
break;
case 102:
if (!ctx->tcp)
{
resolv_response_init (b, (struct resolv_response_flags) {.tc = true});
resolv_response_add_question (b, qname, qclass, qtype);
}
else
{
resolv_response_init
(b, (struct resolv_response_flags) {.ancount = 1});
resolv_response_add_question (b, qname, qclass, qtype);
resolv_response_section (b, ns_s_an);
resolv_response_open_record (b, qname, qclass, T_CNAME, 0);
size_t to_fill = 65535 - resolv_response_length (b)
- 2 /* length, "n" */ - 2 /* compression reference */
- 2 /* RR type */;
for (size_t i = 0; i < to_fill; ++i)
resolv_response_add_data (b, "", 1);
resolv_response_close_record (b);
resolv_response_add_name (b, "n.example");
uint16_t rrtype = htons (T_CNAME);
resolv_response_add_data (b, &rrtype, sizeof (rrtype));
}
break;
case 103:
/* NODATA repsonse. */
resolv_response_init (b, (struct resolv_response_flags) {});
resolv_response_add_question (b, qname, qclass, qtype);
break;
case 104:
resolv_response_init (b, (struct resolv_response_flags) {.ancount = 1});
resolv_response_add_question (b, qname, qclass, qtype);
/* No RR metadata. */
resolv_response_add_name (b, "www.example");
break;
case 105:
if (qtype == T_A)
{
resolv_response_init (b, (struct resolv_response_flags) {});
resolv_response_add_question (b, qname, qclass, qtype);
/* No data, trigger AAAA query. */
}
else
{
resolv_response_init
(b, (struct resolv_response_flags) {.ancount = 1});
resolv_response_add_question (b, qname, qclass, qtype);
/* No RR metadata. */
resolv_response_add_name
(b, "long-name-exceed-previously-initialized-buffer.example");
}
break;
case 106:
resolv_response_init (b, (struct resolv_response_flags) {.ancount = 1});
resolv_response_add_question (b, qname, qclass, qtype);
/* No RR metadata. */
resolv_response_add_name (b, "www.example");
resolv_response_add_data (b, "\xff\xff", 2);
break;
case 107:
if (qtype == T_A)
{
resolv_response_init (b, (struct resolv_response_flags) {});
resolv_response_add_question (b, qname, qclass, qtype);
/* No data, trigger AAAA query. */
}
else
{
resolv_response_init
(b, (struct resolv_response_flags) {.ancount = 1});
resolv_response_add_question (b, qname, qclass, qtype);
/* No RR metadata. */
resolv_response_add_name (b, "www.example");
resolv_response_add_data (b, "\xff\xff", 2);
}
break;
default:
FAIL_EXIT1 ("error: invalid QNAME: %s (code %d)\n", qname, code);
}
}
static void
check (int code, const char *expected)
{
char qname[200];
snprintf (qname, sizeof (qname), "code%d.example", code);
char *result;
enum nss_status status;
{
enum { buffer_size = 4096 };
char *buffer = xmalloc (buffer_size);
char *temp_result;
int temp_errno;
int temp_herrno;
status = getcanonname
(qname, buffer, buffer_size, &temp_result, &temp_errno, &temp_herrno);
if (status == NSS_STATUS_SUCCESS)
result = xstrdup (temp_result);
else
{
errno = temp_errno;
h_errno = temp_herrno;
}
free (buffer);
}
if (status == NSS_STATUS_SUCCESS)
{
if (expected != NULL)
{
if (strcmp (result, expected) != 0)
{
support_record_failure ();
printf ("error: getcanonname (%s) failed\n", qname);
printf ("error: expected: %s\n", expected);
printf ("error: actual: %s\n", result);
free (result);
return;
}
}
else
{
support_record_failure ();
printf ("error: getcanonname (%s) unexpected success\n", qname);
printf ("error: actual: %s\n", result);
free (result);
return;
}
free (result);
}
else
{
if (expected != NULL)
{
support_record_failure ();
printf ("error: getcanonname (%s) failed\n", qname);
printf ("error: expected: %s\n", expected);
return;
}
}
}
static int
do_test (void)
{
void *nss_dns_handle = dlopen (LIBNSS_DNS_SO, RTLD_LAZY);
if (nss_dns_handle == NULL)
FAIL_EXIT1 ("could not dlopen %s: %s", LIBNSS_DNS_SO, dlerror ());
{
const char *func = "_nss_dns_getcanonname_r";
void *ptr = dlsym (nss_dns_handle, func);
if (ptr == NULL)
FAIL_EXIT1 ("could not look up %s: %s", func, dlerror ());
getcanonname = ptr;
}
struct resolv_test *aux = resolv_test_start
((struct resolv_redirect_config)
{
.response_callback = response,
});
check (1, "www.example");
check (2, "www.example");
check (3, "www.example");
check (4, "www.example");
check (5, "www1.example");
/* This should really result in "www.example", but the fake RRSIG
record causes the current implementation to stop parsing. */
check (6, NULL);
for (int i = 102; i <= 107; ++i)
check (i, NULL);
resolv_test_end (aux);
TEST_VERIFY (dlclose (nss_dns_handle) == 0);
return 0;
}
#include <support/test-driver.c>