summary refs log tree commit diff
path: root/resolv/tst-resolv-rotate.c
blob: d91d937c3d8d7114b1a9ad89046fa2cb54ad7456 (plain) (blame)
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
/* Check that RES_ROTATE works with few nameserver entries (bug 13028).
   Copyright (C) 2017-2019 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
   <http://www.gnu.org/licenses/>.  */

#include <netdb.h>
#include <resolv.h>
#include <stdlib.h>
#include <string.h>
#include <support/check.h>
#include <support/check_nss.h>
#include <support/resolv_test.h>
#include <support/test-driver.h>

static volatile int drop_server = -1;
static volatile unsigned int query_counts[resolv_max_test_servers];

static const char address_ipv4[4] = {192, 0, 2, 1};
static const char address_ipv6[16]
  = {0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};

static void
response (const struct resolv_response_context *ctx,
          struct resolv_response_builder *b,
          const char *qname, uint16_t qclass, uint16_t qtype)
{
  if (ctx->server_index == drop_server)
    {
      resolv_response_drop (b);
      resolv_response_close (b);
      return;
    }

  bool force_tcp = strncmp (qname, "2.", 2) == 0;
  struct resolv_response_flags flags = {.tc = force_tcp && !ctx->tcp};
  resolv_response_init (b, flags);
  resolv_response_add_question (b, qname, qclass, qtype);
  if (flags.tc)
    return;

  TEST_VERIFY_EXIT (ctx->server_index < resolv_max_test_servers);
  ++query_counts[ctx->server_index];

  resolv_response_section (b, ns_s_an);
  resolv_response_open_record (b, qname, qclass, qtype, 0);
  switch (qtype)
    {
    case T_A:
      {
        char addr[sizeof (address_ipv4)];
        memcpy (addr, address_ipv4, sizeof (address_ipv4));
        addr[3] = 1 + ctx->tcp;
        resolv_response_add_data (b, addr, sizeof (addr));
      }
      break;
    case T_AAAA:
      {
        char addr[sizeof (address_ipv6)];
        memcpy (addr, address_ipv6, sizeof (address_ipv6));
        addr[15] = 1 + ctx->tcp;
        resolv_response_add_data (b, addr, sizeof (addr));
      }
      break;
    case T_PTR:
      if (force_tcp)
        resolv_response_add_name (b, "2.host.example");
      else
        resolv_response_add_name (b, "host.example");
      break;
    default:
      FAIL_EXIT1 ("unexpected QTYPE: %s/%u/%u", qname, qclass, qtype);
    }
  resolv_response_close_record (b);
}

static void
check_forward_1 (const char *name, int family)
{
  unsigned char lsb;
  if (strncmp (name, "2.", 2) == 0)
    lsb = 2;
  else
    lsb = 1;

  char expected_hostent_v4[200];
  snprintf (expected_hostent_v4, sizeof (expected_hostent_v4),
            "name: %s\naddress: 192.0.2.%d\n", name, lsb);
  char expected_hostent_v6[200];
  snprintf (expected_hostent_v6, sizeof (expected_hostent_v6),
            "name: %s\naddress: 2001:db8::%d\n", name, lsb);
  char expected_ai[200];

  unsigned char address[16];
  size_t address_length;

  char *expected_hostent;
  switch (family)
    {
    case AF_INET:
      expected_hostent = expected_hostent_v4;
      snprintf (expected_ai, sizeof (expected_ai),
                "address: STREAM/TCP 192.0.2.%d 80\n", lsb);
      TEST_VERIFY_EXIT (sizeof (address_ipv4) == sizeof (struct in_addr));
      memcpy (address, address_ipv4, sizeof (address_ipv4));
      address_length = sizeof (address_ipv4);
      break;
    case AF_INET6:
      expected_hostent = expected_hostent_v6;
      snprintf (expected_ai, sizeof (expected_ai),
                "address: STREAM/TCP 2001:db8::%d 80\n", lsb);
      TEST_VERIFY_EXIT (sizeof (address_ipv6) == sizeof (struct in6_addr));
      memcpy (address, address_ipv6, sizeof (address_ipv6));
      address_length = sizeof (address_ipv6);
      break;
    case AF_UNSPEC:
      expected_hostent = NULL;
      snprintf (expected_ai, sizeof (expected_ai),
                "address: STREAM/TCP 192.0.2.%d 80\n"
                "address: STREAM/TCP 2001:db8::%d 80\n",
                lsb, lsb);
      address_length = 0;
      break;
    default:
      FAIL_EXIT1 ("unknown address family %d", family);
    }


  if (family == AF_INET)
    {
      struct hostent *e = gethostbyname (name);
      check_hostent (name, e, expected_hostent_v4);
    }

  if (family != AF_UNSPEC)
    {
      struct hostent *e = gethostbyname2 (name, family);
      check_hostent (name, e, expected_hostent);
    }

  if (address_length > 0)
    {
      address[address_length - 1] = lsb;
      struct hostent *e = gethostbyaddr (address, address_length, family);
      check_hostent (name, e, expected_hostent);
    }

  struct addrinfo hints =
    {
      .ai_family = family,
      .ai_socktype = SOCK_STREAM,
      .ai_protocol = IPPROTO_TCP,
    };
  struct addrinfo *ai;
  int ret = getaddrinfo (name, "80", &hints, &ai);
  check_addrinfo (name, ai, ret, expected_ai);
  if (ret == 0)
    {
      for (struct addrinfo *p = ai; p != NULL; p = p->ai_next)
        {
          char host[200];
          ret = getnameinfo (p->ai_addr, p->ai_addrlen,
                             host, sizeof (host),
                             NULL, 0, /* service */
                             0);
          if (ret != 0)
            {
              support_record_failure ();
              printf ("error: getnameinfo: %d\n", ret);
            }
          else
            {
              if (lsb == 1)
                TEST_VERIFY (strcmp (host, "host.example") == 0);
              else
                TEST_VERIFY (strcmp (host, "2.host.example") == 0);
            }
        }
      freeaddrinfo (ai);
    }
}

static void
check_forward (int family)
{
  check_forward_1 ("host.example", family);
  check_forward_1 ("2.host.example", family);
}

static int
do_test (void)
{
  for (int force_tcp = 0; force_tcp < 2; ++force_tcp)
    for (int nscount = 1; nscount <= 3; ++nscount)
      for (int disable_server = -1; disable_server < nscount; ++disable_server)
        for (drop_server = -1; drop_server < nscount; ++drop_server)
          {
            /* A disabled server will never receive queries and
               therefore cannot drop them.  */
            if (drop_server >= 0 && drop_server == disable_server)
              continue;
            /* No servers remaining to query, all queries are expected
               to fail.  */
            int broken_servers = (disable_server >= 0) + (drop_server >= 0);
            if (nscount <= broken_servers)
              continue;

            if (test_verbose > 0)
              printf ("info: tcp=%d nscount=%d disable=%d drop=%d\n",
                      force_tcp, nscount, disable_server, drop_server);
            struct resolv_redirect_config config =
              {
                .response_callback = response,
                .nscount = nscount
              };
            if (disable_server >= 0)
              {
                config.servers[disable_server].disable_udp = true;
                config.servers[disable_server].disable_tcp = true;
              }

            struct resolv_test *aux = resolv_test_start (config);
            _res.options |= RES_ROTATE;

            /* Run a few queries to make sure that all of them
               succeed.  We always perform more than nscount queries,
               so we cover all active servers due to RES_ROTATE.  */
            for (size_t i = 0; i < resolv_max_test_servers; ++i)
              query_counts[i] = 0;
            check_forward (AF_INET);
            check_forward (AF_INET6);
            check_forward (AF_UNSPEC);

            for (int i = 0; i < nscount; ++i)
              {
                if (i != disable_server && i != drop_server
                    && query_counts[i] == 0)
                  {
                    support_record_failure ();
                    printf ("error: nscount=%d, but no query to server %d\n",
                            nscount, i);
                  }
              }

            resolv_test_end (aux);
          }
  return 0;
}

#define TIMEOUT 300
#include <support/test-driver.c>