scripts: Fix fortify checks if compiler does not support _FORTIFY_SOURCE=3
[glibc.git] / resolv / tst-resolv-nondecimal.c
blobd2d12e8facc225688c164ef36e4c9e854c5ed2d2
1 /* Test name resolution behavior for octal, hexadecimal IPv4 addresses.
2 Copyright (C) 2019-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <netdb.h>
20 #include <stdlib.h>
21 #include <support/check.h>
22 #include <support/check_nss.h>
23 #include <support/resolv_test.h>
24 #include <support/support.h>
26 static void
27 response (const struct resolv_response_context *ctx,
28 struct resolv_response_builder *b,
29 const char *qname, uint16_t qclass, uint16_t qtype)
31 /* The tests are not supposed send any DNS queries. */
32 FAIL_EXIT1 ("unexpected DNS query for %s/%d/%d", qname, qclass, qtype);
35 static void
36 run_query_addrinfo (const char *query, const char *address)
38 char *quoted_query = support_quote_string (query);
40 struct addrinfo *ai;
41 struct addrinfo hints =
43 .ai_socktype = SOCK_STREAM,
44 .ai_protocol = IPPROTO_TCP,
47 char *context = xasprintf ("getaddrinfo \"%s\" AF_INET", quoted_query);
48 char *expected = xasprintf ("address: STREAM/TCP %s 80\n", address);
49 hints.ai_family = AF_INET;
50 int ret = getaddrinfo (query, "80", &hints, &ai);
51 check_addrinfo (context, ai, ret, expected);
52 if (ret == 0)
53 freeaddrinfo (ai);
54 free (context);
56 context = xasprintf ("getaddrinfo \"%s\" AF_UNSPEC", quoted_query);
57 hints.ai_family = AF_UNSPEC;
58 ret = getaddrinfo (query, "80", &hints, &ai);
59 check_addrinfo (context, ai, ret, expected);
60 if (ret == 0)
61 freeaddrinfo (ai);
62 free (expected);
63 free (context);
65 context = xasprintf ("getaddrinfo \"%s\" AF_INET6", quoted_query);
66 expected = xasprintf ("flags: AI_V4MAPPED\n"
67 "address: STREAM/TCP ::ffff:%s 80\n",
68 address);
69 hints.ai_family = AF_INET6;
70 hints.ai_flags = AI_V4MAPPED;
71 ret = getaddrinfo (query, "80", &hints, &ai);
72 check_addrinfo (context, ai, ret, expected);
73 if (ret == 0)
74 freeaddrinfo (ai);
75 free (expected);
76 free (context);
78 free (quoted_query);
81 static void
82 run_query (const char *query, const char *address)
84 char *quoted_query = support_quote_string (query);
85 char *context = xasprintf ("gethostbyname (\"%s\")", quoted_query);
86 char *expected = xasprintf ("name: %s\n"
87 "address: %s\n", query, address);
88 check_hostent (context, gethostbyname (query), expected);
89 free (context);
91 context = xasprintf ("gethostbyname_r \"%s\"", quoted_query);
92 struct hostent storage;
93 char buf[4096];
94 struct hostent *e = NULL;
95 TEST_COMPARE (gethostbyname_r (query, &storage, buf, sizeof (buf),
96 &e, &h_errno), 0);
97 check_hostent (context, e, expected);
98 free (context);
100 context = xasprintf ("gethostbyname2 (\"%s\", AF_INET)", quoted_query);
101 check_hostent (context, gethostbyname2 (query, AF_INET), expected);
102 free (context);
104 context = xasprintf ("gethostbyname2_r \"%s\" AF_INET", quoted_query);
105 e = NULL;
106 TEST_COMPARE (gethostbyname2_r (query, AF_INET, &storage, buf, sizeof (buf),
107 &e, &h_errno), 0);
108 check_hostent (context, e, expected);
109 free (context);
110 free (expected);
112 free (quoted_query);
114 /* The gethostbyname tests are always valid for getaddrinfo, but not
115 vice versa. */
116 run_query_addrinfo (query, address);
119 static int
120 do_test (void)
122 struct resolv_test *aux = resolv_test_start
123 ((struct resolv_redirect_config)
125 .response_callback = response,
128 run_query ("192.000.002.010", "192.0.2.8");
130 /* Hexadecimal numbers are not accepted by gethostbyname. */
131 run_query_addrinfo ("0xc0000210", "192.0.2.16");
132 run_query_addrinfo ("192.0x234", "192.0.2.52");
134 resolv_test_end (aux);
136 return 0;
139 #include <support/test-driver.c>