glibc/resolv/tst-resolv-qtypes.c

186 lines
5.9 KiB
C
Raw Normal View History

/* Exercise low-level query functions with different QTYPEs.
Copyright (C) 2016-2020 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
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 13:40:42 +08:00
<https://www.gnu.org/licenses/>. */
#include <resolv.h>
#include <string.h>
#include <support/check.h>
#include <support/check_nss.h>
#include <support/resolv_test.h>
#include <support/support.h>
#include <support/test-driver.h>
#include <support/xmemstream.h>
/* If ture, the response function will send the actual response packet
over TCP instead of UDP. */
static volatile bool force_tcp;
/* Send back a fake resource record matching the QTYPE. */
static void
response (const struct resolv_response_context *ctx,
struct resolv_response_builder *b,
const char *qname, uint16_t qclass, uint16_t qtype)
{
if (force_tcp && ctx->tcp)
{
resolv_response_init (b, (struct resolv_response_flags) { .tc = 1 });
resolv_response_add_question (b, qname, qclass, qtype);
return;
}
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);
resolv_response_add_data (b, &qtype, sizeof (qtype));
resolv_response_close_record (b);
}
static const char domain[] = "www.example.com";
static int
wrap_res_query (int type, unsigned char *answer, int answer_length)
{
return res_query (domain, C_IN, type, answer, answer_length);
}
static int
wrap_res_search (int type, unsigned char *answer, int answer_length)
{
return res_query (domain, C_IN, type, answer, answer_length);
}
static int
wrap_res_querydomain (int type, unsigned char *answer, int answer_length)
{
return res_querydomain ("www", "example.com", C_IN, type,
answer, answer_length);
}
static int
wrap_res_send (int type, unsigned char *answer, int answer_length)
{
unsigned char buf[512];
int ret = res_mkquery (QUERY, domain, C_IN, type,
(const unsigned char *) "", 0, NULL,
buf, sizeof (buf));
if (type < 0 || type >= 65536)
{
/* res_mkquery fails for out-of-range record types. */
TEST_VERIFY_EXIT (ret == -1);
return -1;
}
TEST_VERIFY_EXIT (ret > 12); /* DNS header length. */
return res_send (buf, ret, answer, answer_length);
}
static int
wrap_res_nquery (int type, unsigned char *answer, int answer_length)
{
return res_nquery (&_res, domain, C_IN, type, answer, answer_length);
}
static int
wrap_res_nsearch (int type, unsigned char *answer, int answer_length)
{
return res_nquery (&_res, domain, C_IN, type, answer, answer_length);
}
static int
wrap_res_nquerydomain (int type, unsigned char *answer, int answer_length)
{
return res_nquerydomain (&_res, "www", "example.com", C_IN, type,
answer, answer_length);
}
static int
wrap_res_nsend (int type, unsigned char *answer, int answer_length)
{
unsigned char buf[512];
int ret = res_nmkquery (&_res, QUERY, domain, C_IN, type,
(const unsigned char *) "", 0, NULL,
buf, sizeof (buf));
if (type < 0 || type >= 65536)
{
/* res_mkquery fails for out-of-range record types. */
TEST_VERIFY_EXIT (ret == -1);
return -1;
}
TEST_VERIFY_EXIT (ret > 12); /* DNS header length. */
return res_nsend (&_res, buf, ret, answer, answer_length);
}
static void
test_function (const char *fname,
int (*func) (int type,
unsigned char *answer, int answer_length))
{
unsigned char buf[512];
for (int tcp = 0; tcp < 2; ++tcp)
{
force_tcp = tcp;
for (unsigned int type = 1; type <= 65535; ++type)
{
if (test_verbose)
printf ("info: sending QTYPE %d with %s (tcp=%d)\n",
type, fname, tcp);
int ret = func (type, buf, sizeof (buf));
if (ret != 47)
FAIL_EXIT1 ("%s tcp=%d qtype=%d return value %d",
fname,tcp, type, ret);
/* One question, one answer record. */
TEST_VERIFY (memcmp (buf + 4, "\0\1\0\1\0\0\0\0", 8) == 0);
/* Question section. */
static const char qname[] = "\3www\7example\3com";
size_t qname_length = sizeof (qname);
TEST_VERIFY (memcmp (buf + 12, qname, qname_length) == 0);
/* RDATA part of answer. */
uint16_t type16 = type;
TEST_VERIFY (memcmp (buf + ret - 2, &type16, sizeof (type16)) == 0);
}
}
TEST_VERIFY (func (-1, buf, sizeof (buf) == -1));
TEST_VERIFY (func (65536, buf, sizeof (buf) == -1));
}
static int
do_test (void)
{
struct resolv_redirect_config config =
{
.response_callback = response,
};
struct resolv_test *obj = resolv_test_start (config);
test_function ("res_query", &wrap_res_query);
test_function ("res_search", &wrap_res_search);
test_function ("res_querydomain", &wrap_res_querydomain);
test_function ("res_send", &wrap_res_send);
test_function ("res_nquery", &wrap_res_nquery);
test_function ("res_nsearch", &wrap_res_nsearch);
test_function ("res_nquerydomain", &wrap_res_nquerydomain);
test_function ("res_nsend", &wrap_res_nsend);
resolv_test_end (obj);
return 0;
}
#define TIMEOUT 300
#include <support/test-driver.c>