public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Siddhesh Poyarekar <siddhesh@gotplt.org>
To: Florian Weimer <fweimer@redhat.com>, libc-alpha@sourceware.org
Subject: Re: [PATCH v2 02/13] resolv: Add tst-resolv-aliases
Date: Mon, 29 Aug 2022 15:57:12 -0400	[thread overview]
Message-ID: <181c7e42-50a6-02f3-8eb2-6b109940f74c@gotplt.org> (raw)
In-Reply-To: <54b9a79f91655996cfe4f21b4e9206d7138247f3.1661509943.git.fweimer@redhat.com>



On 2022-08-26 06:33, Florian Weimer via Libc-alpha wrote:
> ---
>   resolv/Makefile             |   2 +
>   resolv/tst-resolv-aliases.c | 254 ++++++++++++++++++++++++++++++++++++
>   2 files changed, 256 insertions(+)
>   create mode 100644 resolv/tst-resolv-aliases.c

LGTM.

Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

> 
> diff --git a/resolv/Makefile b/resolv/Makefile
> index 98b10d97a0..0038bb7028 100644
> --- a/resolv/Makefile
> +++ b/resolv/Makefile
> @@ -89,6 +89,7 @@ tests += \
>     tst-ns_name_pton \
>     tst-res_hconf_reorder \
>     tst-res_hnok \
> +  tst-resolv-aliases \
>     tst-resolv-basic \
>     tst-resolv-binary \
>     tst-resolv-byaddr \
> @@ -259,6 +260,7 @@ $(objpfx)tst-resolv-ai_idn.out: $(gen-locales)
>   $(objpfx)tst-resolv-ai_idn-latin1.out: $(gen-locales)
>   $(objpfx)tst-resolv-ai_idn-nolibidn2.out: \
>     $(gen-locales) $(objpfx)tst-no-libidn2.so
> +$(objpfx)tst-resolv-aliases: $(objpfx)libresolv.so $(shared-thread-library)
>   $(objpfx)tst-resolv-basic: $(objpfx)libresolv.so $(shared-thread-library)
>   $(objpfx)tst-resolv-binary: $(objpfx)libresolv.so $(shared-thread-library)
>   $(objpfx)tst-resolv-byaddr: $(objpfx)libresolv.so $(shared-thread-library)
> diff --git a/resolv/tst-resolv-aliases.c b/resolv/tst-resolv-aliases.c
> new file mode 100644
> index 0000000000..b212823aa0
> --- /dev/null
> +++ b/resolv/tst-resolv-aliases.c
> @@ -0,0 +1,254 @@
> +/* Test alias handling (mainly for gethostbyname).
> +   Copyright (C) 2022 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 <array_length.h>
> +#include <arpa/inet.h>
> +#include <netdb.h>
> +#include <stdbool.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <support/check.h>
> +#include <support/check_nss.h>
> +#include <support/resolv_test.h>
> +#include <support/support.h>
> +
> +#include "tst-resolv-maybe_insert_sig.h"
> +
> +/* QNAME format:
> +
> +   aADDRESSES-cCNAMES.example.net
> +
> +   CNAMES is the length of the CNAME chain, ADDRESSES is the number of
> +   addresses in the response.  The special value 255 means that there
> +   are no addresses, and the RCODE is NXDOMAIN.  */
> +static void
> +response (const struct resolv_response_context *ctx,
> +          struct resolv_response_builder *b,
> +          const char *qname, uint16_t qclass, uint16_t qtype)
> +{
> +  TEST_COMPARE (qclass, C_IN);
> +  if (qtype != T_A)
> +    TEST_COMPARE (qtype, T_AAAA);
> +
> +  unsigned int addresses, cnames;
> +  char *tail;
> +  if (sscanf (qname, "a%u-c%u%ms", &addresses, &cnames, &tail) == 3)
> +    {
> +      if (strcmp (tail, ".example.com") == 0
> +          || strcmp (tail, ".example.net.example.net") == 0
> +          || strcmp (tail, ".example.net.example.com") == 0)
> +        /* These only happen after NXDOMAIN.  */
> +        TEST_VERIFY (addresses == 255);
> +      else if (strcmp (tail, ".example.net") != 0)
> +        FAIL_EXIT1 ("invalid QNAME: %s", qname);
> +    }
> +  free (tail);
> +
> +  int rcode;
> +  if (addresses == 255)
> +    {
> +      /* Special case: Use no addresses with NXDOMAIN response.  */
> +      rcode = ns_r_nxdomain;
> +      addresses = 0;
> +    }
> +  else
> +    rcode = 0;
> +
> +  struct resolv_response_flags flags = { .rcode = rcode };
> +  resolv_response_init (b, flags);
> +  resolv_response_add_question (b, qname, qclass, qtype);
> +  resolv_response_section (b, ns_s_an);
> +  maybe_insert_sig (b, qname);
> +
> +  /* Provide the requested number of CNAME records.  */
> +  char *previous_name = (char *) qname;
> +  for (int unique = 0; unique < cnames; ++unique)
> +    {
> +      resolv_response_open_record (b, previous_name, qclass, T_CNAME, 60);
> +      char *new_name = xasprintf ("%d.alias.example", unique);
> +      resolv_response_add_name (b, new_name);
> +      resolv_response_close_record (b);
> +
> +      maybe_insert_sig (b, qname);
> +
> +      if (previous_name != qname)
> +        free (previous_name);
> +      previous_name = new_name;
> +    }
> +
> +  for (int unique = 0; unique < addresses; ++unique)
> +    {
> +      resolv_response_open_record (b, previous_name, qclass, qtype, 60);
> +
> +      if (qtype == T_A)
> +        {
> +          char ipv4[4] = {192, 0, 2, 1 + unique};
> +          resolv_response_add_data (b, &ipv4, sizeof (ipv4));
> +        }
> +      else if (qtype == T_AAAA)
> +        {
> +          char ipv6[16] =
> +            {
> +              0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
> +              1 + unique
> +            };
> +          resolv_response_add_data (b, &ipv6, sizeof (ipv6));
> +        }
> +      resolv_response_close_record (b);
> +    }
> +
> +  if (previous_name != qname)
> +    free (previous_name);
> +}
> +
> +static char *
> +make_qname (bool do_search, int cnames, int addresses)
> +{
> +  return xasprintf ("a%d-c%d%s",
> +                    addresses, cnames, do_search ? "" : ".example.net");
> +}
> +
> +static void
> +check_cnames_failure (int af, bool do_search, int cnames, int addresses)
> +{
> +  char *qname = make_qname (do_search, cnames, addresses);
> +
> +  struct hostent *e;
> +  if (af == AF_UNSPEC)
> +    e = gethostbyname (qname);
> +  else
> +    e = gethostbyname2 (qname, af);
> +
> +  if (addresses == 0)
> +    check_hostent (qname, e, "error: NO_RECOVERY\n");
> +  else
> +    check_hostent (qname, e, "error: HOST_NOT_FOUND\n");
> +
> +  free (qname);
> +}
> +
> +static void
> +check (int af, bool do_search, int cnames, int addresses)
> +{
> +  char *qname = make_qname (do_search, cnames, addresses);
> +  char *fqdn = make_qname (false, cnames, addresses);
> +
> +  struct hostent *e;
> +  if (af == AF_UNSPEC)
> +    e = gethostbyname (qname);
> +  else
> +    e = gethostbyname2 (qname, af);
> +  if (e == NULL)
> +    FAIL_EXIT1 ("unexpected failure for %d, %d, %d", af, cnames, addresses);
> +
> +  if (af == AF_UNSPEC || af == AF_INET)
> +    {
> +      TEST_COMPARE (e->h_addrtype, AF_INET);
> +      TEST_COMPARE (e->h_length, 4);
> +    }
> +  else
> +    {
> +      TEST_COMPARE (e->h_addrtype, AF_INET6);
> +      TEST_COMPARE (e->h_length, 16);
> +    }
> +
> +  for (int i = 0; i < addresses; ++i)
> +    {
> +      char ipv4[4] = {192, 0, 2, 1 + i};
> +      char ipv6[16] =
> +        { 0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 + i };
> +      char *expected = e->h_addrtype == AF_INET ? ipv4 : ipv6;
> +      TEST_COMPARE_BLOB (e->h_addr_list[i], e->h_length,
> +                         expected, e->h_length);
> +    }
> +  TEST_VERIFY (e->h_addr_list[addresses] == NULL);
> +
> +
> +  if (cnames == 0)
> +    {
> +      /* QNAME is fully qualified.  */
> +      TEST_COMPARE_STRING (e->h_name, fqdn);
> +      TEST_VERIFY (e->h_aliases[0] == NULL);
> +    }
> +  else
> +   {
> +     /* Fully-qualified QNAME is demoted to an aliases.  */
> +     TEST_COMPARE_STRING (e->h_aliases[0], fqdn);
> +
> +     for (int i = 1; i <= cnames; ++i)
> +       {
> +         char *expected = xasprintf ("%d.alias.example", i - 1);
> +         if (i == cnames)
> +           TEST_COMPARE_STRING (e->h_name, expected);
> +         else
> +           TEST_COMPARE_STRING (e->h_aliases[i], expected);
> +         free (expected);
> +       }
> +     TEST_VERIFY (e->h_aliases[cnames] == NULL);
> +   }
> +
> +  free (fqdn);
> +  free (qname);
> +}
> +
> +static int
> +do_test (void)
> +{
> +  struct resolv_test *obj = resolv_test_start
> +    ((struct resolv_redirect_config)
> +     {
> +       .response_callback = response,
> +       .search = { "example.net", "example.com" },
> +     });
> +
> +  static const int families[] = { AF_UNSPEC, AF_INET, AF_INET6 };
> +
> +  for (int do_insert_sig = 0; do_insert_sig < 2; ++do_insert_sig)
> +    {
> +      insert_sig = do_insert_sig;
> +
> +      /* If do_search is true, a bare host name (for example, a1-c1)
> +         is used.  This exercises search path processing and FQDN
> +         qualification.  */
> +      for (int do_search = 0; do_search < 2; ++do_search)
> +        for (const int *paf = families; paf != array_end (families); ++paf)
> +          {
> +            for (int cnames = 0; cnames <= 100; ++cnames)
> +              {
> +                check_cnames_failure (*paf, do_search, cnames, 0);
> +                /* Now with NXDOMAIN responses.  */
> +                check_cnames_failure (*paf, do_search, cnames, 255);
> +              }
> +
> +            for (int cnames = 0; cnames <= 10; ++cnames)
> +              for (int addresses = 1; addresses <= 10; ++addresses)
> +                check (*paf, do_search, cnames, addresses);
> +
> +            /* The current implementation is limited to 47 aliases.
> +               Addresses do not have such a limit.  */
> +            check (*paf, do_search, 47, 60);
> +          }
> +    }
> +
> +  resolv_test_end (obj);
> +
> +  return 0;
> +}
> +
> +#include <support/test-driver.c>

  reply	other threads:[~2022-08-29 19:57 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-26 10:33 [PATCH v2 00/13] nss_dns: Fix handling of non-host CNAMEs (bug 12154) Florian Weimer
2022-08-26 10:33 ` [PATCH v2 01/13] resolv: Add tst-resolv-byaddr for testing reverse lookup Florian Weimer
2022-08-29 19:56   ` Siddhesh Poyarekar
2022-08-26 10:33 ` [PATCH v2 02/13] resolv: Add tst-resolv-aliases Florian Weimer
2022-08-29 19:57   ` Siddhesh Poyarekar [this message]
2022-08-26 10:33 ` [PATCH v2 03/13] resolv: Add internal __res_binary_hnok function Florian Weimer
2022-08-26 10:33 ` [PATCH v2 04/13] resolv: Add the __ns_samebinaryname function Florian Weimer
2022-08-26 10:33 ` [PATCH v2 05/13] resolv: Add internal __ns_name_length_uncompressed function Florian Weimer
2022-08-26 10:33 ` [PATCH v2 06/13] resolv: Add DNS packet parsing helpers geared towards wire format Florian Weimer
2022-08-26 10:33 ` [PATCH v2 07/13] nss_dns: Split getanswer_ptr from getanswer_r Florian Weimer
2022-08-26 10:33 ` [PATCH v2 08/13] nss_dns: Rewrite _nss_dns_gethostbyaddr2_r and getanswer_ptr Florian Weimer
2022-08-26 10:33 ` [PATCH v2 09/13] nss_dns: Remove remnants of IPv6 address mapping Florian Weimer
2022-08-26 10:34 ` [PATCH v2 10/13] nss_dns: Rewrite getanswer_r to match getanswer_ptr (bug 12154, bug 29305) Florian Weimer
2022-08-26 10:34 ` [PATCH v2 11/13] nss_dns: In gaih_getanswer_slice, skip strange aliases (bug 12154) Florian Weimer
2022-08-26 10:34 ` [PATCH v2 12/13] resolv: Add new tst-resolv-invalid-cname Florian Weimer
2022-08-30 10:59   ` Szabolcs Nagy
2022-08-30 11:13     ` Florian Weimer
2022-08-26 10:34 ` [PATCH v2 13/13] nss_dns: Rewrite _nss_dns_gethostbyname4_r using current interfaces Florian Weimer
2022-08-29 20:01   ` Siddhesh Poyarekar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=181c7e42-50a6-02f3-8eb2-6b109940f74c@gotplt.org \
    --to=siddhesh@gotplt.org \
    --cc=fweimer@redhat.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).