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/>. */
21 #include <support/check.h>
22 #include <support/check_nss.h>
23 #include <support/resolv_test.h>
24 #include <support/support.h>
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
);
36 run_query_addrinfo (const char *query
, const char *address
)
38 char *quoted_query
= support_quote_string (query
);
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
);
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
);
65 context
= xasprintf ("getaddrinfo \"%s\" AF_INET6", quoted_query
);
66 expected
= xasprintf ("flags: AI_V4MAPPED\n"
67 "address: STREAM/TCP ::ffff:%s 80\n",
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
);
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
);
91 context
= xasprintf ("gethostbyname_r \"%s\"", quoted_query
);
92 struct hostent storage
;
94 struct hostent
*e
= NULL
;
95 TEST_COMPARE (gethostbyname_r (query
, &storage
, buf
, sizeof (buf
),
97 check_hostent (context
, e
, expected
);
100 context
= xasprintf ("gethostbyname2 (\"%s\", AF_INET)", quoted_query
);
101 check_hostent (context
, gethostbyname2 (query
, AF_INET
), expected
);
104 context
= xasprintf ("gethostbyname2_r \"%s\" AF_INET", quoted_query
);
106 TEST_COMPARE (gethostbyname2_r (query
, AF_INET
, &storage
, buf
, sizeof (buf
),
108 check_hostent (context
, e
, expected
);
114 /* The gethostbyname tests are always valid for getaddrinfo, but not
116 run_query_addrinfo (query
, address
);
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
);
139 #include <support/test-driver.c>