summaryrefslogtreecommitdiff
path: root/resolv/tst-resolv-canonname.c
blob: f0a60eeb05772f6bd885afb42a546dbf671f3ca4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* Test _nss_dns_getcanonname_r corner cases.
   Copyright (C) 2017-2020 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 <dlfcn.h>
#include <errno.h>
#include <gnu/lib-names.h>
#include <netdb.h>
#include <netinet/in.h>
#include <nss.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <support/check.h>
#include <support/resolv_test.h>
#include <support/support.h>

/* _nss_dns_getcanonname_r is not called during regular operation
   because nss_dns directly provides a canonical name, so we have to
   test it directly.  The function pointer is initialized by do_test
   below.  */
static enum nss_status
(*getcanonname) (const char *name, char *buffer, size_t buflen,
                 char **result, int *errnop, int *h_errnop);

static void
response (const struct resolv_response_context *ctx,
          struct resolv_response_builder *b,
          const char *qname, uint16_t qclass, uint16_t qtype)
{
  int code;
  {
    char *tail;
    if (sscanf (qname, "code%d.%ms", &code, &tail) != 2
        || strcmp (tail, "example") != 0)
      FAIL_EXIT1 ("error: invalid QNAME: %s\n", qname);
    free (tail);
  }

  switch (code)
    {
    case 1:
      resolv_response_init (b, (struct resolv_response_flags) {});
      resolv_response_add_question (b, qname, qclass, qtype);
      resolv_response_section (b, ns_s_an);
      resolv_response_open_record (b, "www.example", qclass, qtype, 0);
      resolv_response_add_data (b, "\xC0\x00\x02\x01", 4);
      resolv_response_close_record (b);
      break;
    case 2:
      resolv_response_init (b, (struct resolv_response_flags) {});
      resolv_response_add_question (b, qname, qclass, qtype);
      resolv_response_section (b, ns_s_an);
      if (qtype == T_AAAA)
        {
          resolv_response_open_record (b, "www.example", qclass, qtype, 0);
          resolv_response_add_data (b, "\xC0\x00\x02\x01", 4);
          resolv_response_close_record (b);
          for (int i = 0; i < 30000; ++i)
            resolv_response_add_data (b, "", 1);
        }
      break;
    case 3:
      resolv_response_init (b, (struct resolv_response_flags) {});
      resolv_response_add_question (b, qname, qclass, qtype);
      resolv_response_section (b, ns_s_an);
      if (qtype == T_AAAA)
        {
          resolv_response_open_record (b, "www.example", qclass, qtype, 0);
          resolv_response_add_data (b, "\xC0\x00\x02\x01", 4);
          resolv_response_close_record (b);
        }
      else
        {
          for (int i = 0; i < 30000; ++i)
            resolv_response_add_data (b, "", 1);
        }
      break;
    case 4:
      resolv_response_init (b, (struct resolv_response_flags) {});
      resolv_response_add_question (b, qname, qclass, qtype);
      resolv_response_section (b, ns_s_an);
      resolv_response_open_record (b, qname, qclass, T_CNAME, 0);
      resolv_response_add_name (b, "www.example");
      resolv_response_close_record (b);
      resolv_response_open_record (b, "www.example", qclass, qtype, 0);
      resolv_response_add_data (b, "\xC0\x00\x02\x01", 4);
      resolv_response_close_record (b);
      break;
    case 5:
      resolv_response_init (b, (struct resolv_response_flags) {});
      resolv_response_add_question (b, qname, qclass, qtype);
      resolv_response_section (b, ns_s_an);
      resolv_response_open_record (b, qname, qclass, T_CNAME, 0);
      resolv_response_add_name (b, "www.example");
      resolv_response_close_record (b);
      resolv_response_open_record (b, qname, qclass, T_CNAME, 0);
      resolv_response_add_name (b, "www1.example");
      resolv_response_close_record (b);
      resolv_response_open_record (b, "www1.example", qclass, qtype, 0);
      resolv_response_add_data (b, "\xC0\x00\x02\x01", 4);
      resolv_response_close_record (b);
      break;
    case 6:
      resolv_response_init (b, (struct resolv_response_flags) {});
      resolv_response_add_question (b, qname, qclass, qtype);
      resolv_response_section (b, ns_s_an);
      resolv_response_open_record (b, qname, qclass, T_CNAME, 0);
      resolv_response_add_name (b, "www.example");
      resolv_response_close_record (b);
      resolv_response_open_record (b, qname, qclass, 46 /* RRSIG */, 0);
      resolv_response_add_name (b, ".");
      resolv_response_close_record (b);
      resolv_response_open_record (b, "www.example", qclass, qtype, 0);
      resolv_response_add_data (b, "\xC0\x00\x02\x01", 4);
      resolv_response_close_record (b);
      break;
    case 102:
      if (!ctx->tcp)
        {
          resolv_response_init (b, (struct resolv_response_flags) {.tc = true});
          resolv_response_add_question (b, qname, qclass, qtype);
        }
      else
        {
          resolv_response_init
            (b, (struct resolv_response_flags) {.ancount = 1});
          resolv_response_add_question (b, qname, qclass, qtype);
          resolv_response_section (b, ns_s_an);
          resolv_response_open_record (b, qname, qclass, T_CNAME, 0);
          size_t to_fill = 65535 - resolv_response_length (b)
            - 2 /* length, "n" */ - 2 /* compression reference */
            - 2 /* RR type */;
          for (size_t i = 0; i < to_fill; ++i)
            resolv_response_add_data (b, "", 1);
          resolv_response_close_record (b);
          resolv_response_add_name (b, "n.example");
          uint16_t rrtype = htons (T_CNAME);
          resolv_response_add_data (b, &rrtype, sizeof (rrtype));
        }
      break;
    case 103:
      /* NODATA repsonse.  */
      resolv_response_init (b, (struct resolv_response_flags) {});
      resolv_response_add_question (b, qname, qclass, qtype);
      break;
    case 104:
      resolv_response_init (b, (struct resolv_response_flags) {.ancount = 1});
      resolv_response_add_question (b, qname, qclass, qtype);
      /* No RR metadata.  */
      resolv_response_add_name (b, "www.example");
      break;
    case 105:
      if (qtype == T_A)
        {
          resolv_response_init (b, (struct resolv_response_flags) {});
          resolv_response_add_question (b, qname, qclass, qtype);
          /* No data, trigger AAAA query.  */
        }
      else
        {
          resolv_response_init
            (b, (struct resolv_response_flags) {.ancount = 1});
          resolv_response_add_question (b, qname, qclass, qtype);
          /* No RR metadata.  */
          resolv_response_add_name
            (b, "long-name-exceed-previously-initialized-buffer.example");
        }
      break;
    case 106:
      resolv_response_init (b, (struct resolv_response_flags) {.ancount = 1});
      resolv_response_add_question (b, qname, qclass, qtype);
      /* No RR metadata.  */
      resolv_response_add_name (b, "www.example");
      resolv_response_add_data (b, "\xff\xff", 2);
      break;
    case 107:
      if (qtype == T_A)
        {
          resolv_response_init (b, (struct resolv_response_flags) {});
          resolv_response_add_question (b, qname, qclass, qtype);
          /* No data, trigger AAAA query.  */
        }
      else
        {
          resolv_response_init
            (b, (struct resolv_response_flags) {.ancount = 1});
          resolv_response_add_question (b, qname, qclass, qtype);
          /* No RR metadata.  */
          resolv_response_add_name (b, "www.example");
          resolv_response_add_data (b, "\xff\xff", 2);
        }
      break;
    default:
      FAIL_EXIT1 ("error: invalid QNAME: %s (code %d)\n", qname, code);
    }
}

static void
check (int code, const char *expected)
{
  char qname[200];
  snprintf (qname, sizeof (qname), "code%d.example", code);
  char *result;
  enum nss_status status;
  {
    enum { buffer_size = 4096 };
    char *buffer = xmalloc (buffer_size);
    char *temp_result;
    int temp_errno;
    int temp_herrno;
    status = getcanonname
      (qname, buffer, buffer_size, &temp_result, &temp_errno, &temp_herrno);
    if (status == NSS_STATUS_SUCCESS)
      result = xstrdup (temp_result);
    else
      {
        errno = temp_errno;
        h_errno = temp_herrno;
      }
    free (buffer);
  }

  if (status == NSS_STATUS_SUCCESS)
    {
      if (expected != NULL)
        {
          if (strcmp (result, expected) != 0)
            {
              support_record_failure ();
              printf ("error: getcanonname (%s) failed\n", qname);
              printf ("error:  expected: %s\n", expected);
              printf ("error:  actual:   %s\n", result);
              free (result);
              return;
            }
        }
      else
        {
          support_record_failure ();
          printf ("error: getcanonname (%s) unexpected success\n", qname);
          printf ("error:  actual:   %s\n", result);
          free (result);
          return;
        }
      free (result);
    }
  else
    {
      if (expected != NULL)
        {
          support_record_failure ();
          printf ("error: getcanonname (%s) failed\n", qname);
          printf ("error:  expected: %s\n", expected);
          return;
        }
    }
}


static int
do_test (void)
{
  void *nss_dns_handle = dlopen (LIBNSS_DNS_SO, RTLD_LAZY);
  if (nss_dns_handle == NULL)
    FAIL_EXIT1 ("could not dlopen %s: %s", LIBNSS_DNS_SO, dlerror ());
  {
    const char *func = "_nss_dns_getcanonname_r";
    void *ptr = dlsym (nss_dns_handle, func);
    if (ptr == NULL)
      FAIL_EXIT1 ("could not look up %s: %s", func, dlerror ());
    getcanonname = ptr;
  }

  struct resolv_test *aux = resolv_test_start
    ((struct resolv_redirect_config)
     {
       .response_callback = response,
     });

  check (1, "www.example");
  check (2, "www.example");
  check (3, "www.example");
  check (4, "www.example");
  check (5, "www1.example");

  /* This should really result in "www.example", but the fake RRSIG
     record causes the current implementation to stop parsing.  */
  check (6, NULL);

  for (int i = 102; i <= 107; ++i)
  check (i, NULL);

  resolv_test_end (aux);

  TEST_VERIFY (dlclose (nss_dns_handle) == 0);
  return 0;
}

#include <support/test-driver.c>