From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from olivedrab.birch.relay.mailchannels.net (olivedrab.birch.relay.mailchannels.net [23.83.209.135]) by sourceware.org (Postfix) with ESMTPS id EF419385BAF0 for ; Mon, 29 Aug 2022 19:57:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org EF419385BAF0 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gotplt.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gotplt.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id A2A84921749; Mon, 29 Aug 2022 19:57:15 +0000 (UTC) Received: from pdx1-sub0-mail-a305 (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id E44CC921707; Mon, 29 Aug 2022 19:57:14 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1661803034; a=rsa-sha256; cv=none; b=mELiZ4udyFz68Evm39h88iG2arHBPMKrJ8BYjx3Eh0G9+UfLRUAmJBOX002xT16ptVNR06 /Id6wf9bhQ3RHzUI2nQFD97eXr3YrtKXLTZlLM35AgNzWcdi6wOBdmoeZynTrm5whQiQfb 8k3/eFPjQWLShQrMjLuupsJS+w8JOXjrGlnX8wztGl593yDSMbwYwEczRIwYen61tKANQ2 whi1pQVXhmJMTq/o4VAoj4TYdRWdI3CDZlAYjRRwvXFd8xUXhtGP1/0abxbFvmMDEDjmJD ZUHShe79C/9PJ3pkZ4584lAm48NMerWQagF3UELvttigEo/+lcBXVZw2XmJVmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1661803034; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=sBlTBoFvMozGNLh2Q7HS+4IxgQ8pxYeHzSZkME1fE0s=; b=shxUaaaklGdfBH24iY1Sy2UnWhiFEN5QTw6/hczCWYnXR1FALnraWosF4zTDhBWaUZ1l8R jpAbg5VPQuaMCTUhqZJmXh58Uh3WaCUjT/rOra1m6bG7XnKS/4RYcpub5P0QdC8g6yvHdN I5nIIfFk/aX7zpjR6+C66pJrHmc12WWgbs8zQIJkzRPSeEiGmKbGo77Cj7YHqx36e9eOjA aso71fuFECX/JYfDVFHZx6EFdNzvQeglTQkIRbZriGk9kPbiT8uz+2Y4BbGUAv4ri6nkmJ MpdMkaPM36xNJrtMlK+4y1lMjOhvBuxoYEKvW4cAiezswQExq2Ae5wUWyImsdQ== ARC-Authentication-Results: i=1; rspamd-75b4464bd-xzkt6; auth=pass smtp.auth=dreamhost smtp.mailfrom=siddhesh@gotplt.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Arithmetic-Callous: 142d16080d84f9e9_1661803035185_881329670 X-MC-Loop-Signature: 1661803035185:1918293618 X-MC-Ingress-Time: 1661803035185 Received: from pdx1-sub0-mail-a305 (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.109.219.78 (trex/6.7.1); Mon, 29 Aug 2022 19:57:15 +0000 Received: from [192.168.0.182] (bras-vprn-toroon4834w-lp130-16-184-147-84-238.dsl.bell.ca [184.147.84.238]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a305 (Postfix) with ESMTPSA id 4MGh6d3MlVzbC; Mon, 29 Aug 2022 12:57:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gotplt.org; s=dreamhost; t=1661803033; bh=sBlTBoFvMozGNLh2Q7HS+4IxgQ8pxYeHzSZkME1fE0s=; h=Date:Subject:To:From:Content-Type:Content-Transfer-Encoding; b=AlFy28B9zPjsf2eGMxLVksS3vBsNxnh49CYqE9i+BHvY4d2CHmSRzWmHNOHPZwCNF h5wbM//gmfpAQ+EqA9JP4Y3zTzCtqsNsqRanL9KvpEc+16hxDtQFUhNzJb9YDbiKVa arUgyhOXM+Ky6sgSIyt/Y85poOV/NkmFdbPPivEQm9UoV/pVcCBHzYL/wthOz0d1j4 e0LCJEIi/Bo/jDP9rsnb67H7ImjZTJyMWVew4Vwa8yLMImyghWtcYGes85lge8cfEc 7MPLtBbEbHbJqYl4iZgwO55MBsBXkHPBijl6fhhf/GOg84V/0oseGMlx2dc4SHrSfI KS68q5peaszxg== Message-ID: <181c7e42-50a6-02f3-8eb2-6b109940f74c@gotplt.org> Date: Mon, 29 Aug 2022 15:57:12 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.12.0 Subject: Re: [PATCH v2 02/13] resolv: Add tst-resolv-aliases Content-Language: en-US To: Florian Weimer , libc-alpha@sourceware.org References: <54b9a79f91655996cfe4f21b4e9206d7138247f3.1661509943.git.fweimer@redhat.com> From: Siddhesh Poyarekar In-Reply-To: <54b9a79f91655996cfe4f21b4e9206d7138247f3.1661509943.git.fweimer@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3038.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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 > > 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 > + . */ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#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