1 /* Test name resolution behavior with trailing characters.
2    Copyright (C) 2019-2021 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
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.
9 
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.
14 
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/>.  */
18 
19 #include <array_length.h>
20 #include <netdb.h>
21 #include <support/check.h>
22 #include <support/check_nss.h>
23 #include <support/resolv_test.h>
24 #include <support/support.h>
25 
26 static void
response(const struct resolv_response_context * ctx,struct resolv_response_builder * b,const char * qname,uint16_t qclass,uint16_t qtype)27 response (const struct resolv_response_context *ctx,
28           struct resolv_response_builder *b,
29           const char *qname, uint16_t qclass, uint16_t qtype)
30 {
31   /* The tests are not supposed send any DNS queries.  */
32   FAIL_EXIT1 ("unexpected DNS query for %s/%d/%d", qname, qclass, qtype);
33 }
34 
35 static int
do_test(void)36 do_test (void)
37 {
38   struct resolv_test *aux = resolv_test_start
39     ((struct resolv_redirect_config)
40      {
41        .response_callback = response,
42      });
43 
44   static const char *const queries[] =
45     {
46      "192.0.2.1 ",
47      "192.0.2.2\t",
48      "192.0.2.3\n",
49      "192.0.2.4 X",
50      "192.0.2.5\tY",
51      "192.0.2.6\nZ",
52      "192.0.2. ",
53      "192.0.2.\t",
54      "192.0.2.\n",
55      "192.0.2. X",
56      "192.0.2.\tY",
57      "192.0.2.\nZ",
58      "2001:db8::1 ",
59      "2001:db8::2\t",
60      "2001:db8::3\n",
61      "2001:db8::4 X",
62      "2001:db8::5\tY",
63      "2001:db8::6\nZ",
64     };
65   for (size_t query_idx = 0; query_idx < array_length (queries); ++query_idx)
66     {
67       const char *query = queries[query_idx];
68       struct hostent storage;
69       char buf[4096];
70       struct hostent *e;
71 
72       h_errno = 0;
73       TEST_VERIFY (gethostbyname (query) == NULL);
74       TEST_COMPARE (h_errno, HOST_NOT_FOUND);
75 
76       h_errno = 0;
77       e = NULL;
78       TEST_COMPARE (gethostbyname_r (query, &storage, buf, sizeof (buf),
79                                      &e, &h_errno), 0);
80       TEST_VERIFY (e == NULL);
81       TEST_COMPARE (h_errno, HOST_NOT_FOUND);
82 
83       h_errno = 0;
84       TEST_VERIFY (gethostbyname2 (query, AF_INET) == NULL);
85       TEST_COMPARE (h_errno, HOST_NOT_FOUND);
86 
87       h_errno = 0;
88       e = NULL;
89       TEST_COMPARE (gethostbyname2_r (query, AF_INET,
90                                       &storage, buf, sizeof (buf),
91                                      &e, &h_errno), 0);
92       TEST_VERIFY (e == NULL);
93       TEST_COMPARE (h_errno, HOST_NOT_FOUND);
94 
95       h_errno = 0;
96       TEST_VERIFY (gethostbyname2 (query, AF_INET6) == NULL);
97       TEST_COMPARE (h_errno, HOST_NOT_FOUND);
98 
99       h_errno = 0;
100       e = NULL;
101       TEST_COMPARE (gethostbyname2_r (query, AF_INET6,
102                                       &storage, buf, sizeof (buf),
103                                      &e, &h_errno), 0);
104       TEST_VERIFY (e == NULL);
105       TEST_COMPARE (h_errno, HOST_NOT_FOUND);
106 
107       static const int gai_flags[] =
108         {
109          0,
110          AI_ADDRCONFIG,
111          AI_NUMERICHOST,
112          AI_IDN,
113          AI_IDN | AI_NUMERICHOST,
114          AI_V4MAPPED,
115          AI_V4MAPPED | AI_NUMERICHOST,
116         };
117       for (size_t gai_flags_idx; gai_flags_idx < array_length (gai_flags);
118              ++gai_flags_idx)
119         {
120           struct addrinfo hints = { .ai_flags = gai_flags[gai_flags_idx], };
121           struct addrinfo *ai;
122           hints.ai_family = AF_INET;
123           TEST_COMPARE (getaddrinfo (query, "80", &hints, &ai), EAI_NONAME);
124           hints.ai_family = AF_INET6;
125           TEST_COMPARE (getaddrinfo (query, "80", &hints, &ai), EAI_NONAME);
126           hints.ai_family = AF_UNSPEC;
127           TEST_COMPARE (getaddrinfo (query, "80", &hints, &ai), EAI_NONAME);
128         }
129     };
130 
131   resolv_test_end (aux);
132 
133   return 0;
134 }
135 
136 #include <support/test-driver.c>
137