about summary refs log tree commit diff
path: root/resolv/tst-resolv-short-response.c
blob: 9b06b0c1762f860cdc7ece81df06bbd3574e5faf (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
/* Test for spurious timeouts with short 12-byte responses (bug 31890).
   Copyright (C) 2024 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 <resolv.h>
#include <support/check.h>
#include <support/resolv_test.h>
#include <support/check_nss.h>

/* The rcode in the initial response.  */
static volatile int rcode;

static void
response (const struct resolv_response_context *ctx,
          struct resolv_response_builder *b,
          const char *qname, uint16_t qclass, uint16_t qtype)
{
  switch (ctx->server_index)
    {
    case 0:
      /* First server times out.  */
      {
        struct resolv_response_flags flags = {.rcode = rcode};
        resolv_response_init (b, flags);
      }
      break;
    case 1:
      /* Second server sends reply.  */
      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, qtype, 0);
      switch (qtype)
        {
        case T_A:
          {
            char ipv4[4] = {192, 0, 2, 17};
            resolv_response_add_data (b, &ipv4, sizeof (ipv4));
          }
          break;
        case T_AAAA:
          {
            char ipv6[16]
              = {0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
            resolv_response_add_data (b, &ipv6, sizeof (ipv6));
          }
          break;
        default:
          FAIL_EXIT1 ("unexpected TYPE%d query", qtype);
        }
      resolv_response_close_record (b);
      break;
    default:
      FAIL_EXIT1 ("unexpected query to server %d", ctx->server_index);
    }
}

static void
check_one (void)
{

  /* The buggy 1-second query timeout results in 30 seconds of delay,
     which triggers a test timeout failure.  */
  for (int i = 0;  i < 10; ++i)
    {
      check_hostent ("www.example", gethostbyname ("www.example"),
                     "name: www.example\n"
                     "address: 192.0.2.17\n");
      check_hostent ("www.example", gethostbyname2 ("www.example", AF_INET6),
                     "name: www.example\n"
                     "address: 2001:db8::1\n");
      static const struct addrinfo hints =
        {
          .ai_family = AF_UNSPEC,
          .ai_socktype = SOCK_STREAM,
        };
      struct addrinfo *ai;
      int ret = getaddrinfo ("www.example", "80", &hints, &ai);
      check_addrinfo ("www.example", ai, ret,
                      "address: STREAM/TCP 192.0.2.17 80\n"
                      "address: STREAM/TCP 2001:db8::1 80\n");
      if (ret == 0)
        freeaddrinfo (ai);
    }
}

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

  _res.options |= RES_SNGLKUP;

  rcode = 2; /* SERVFAIL.  */
  check_one ();

  rcode = 4; /* NOTIMP.  */
  check_one ();

  rcode = 5; /* REFUSED.  */
  check_one ();

  resolv_test_end (aux);

  return 0;
}

#include <support/test-driver.c>