aboutsummaryrefslogtreecommitdiff
path: root/resolv/tst-ns_name_pton.c
blob: 6640ed3021961491fce9b967d7ac2f8d9a14a418 (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
/* Tests for ns_name_pton.
   Copyright (C) 2017-2018 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 <arpa/nameser.h>
#include <array_length.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <support/check.h>
#include <support/support.h>
#include <support/test-driver.h>

/* Bits which indicate which functions are supposed to report
   success.  */
enum
  {
    hnok = 1,
    dnok = 2,
    mailok = 4,
    ownok = 8,
    allnomailok = hnok | dnok | ownok,
    allok = hnok | dnok | mailok | ownok
  };

/* A string of 60 characters.  */
#define STRING60 "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"

/* A string of 63 characters (maximum label length).  */
#define STRING63 STRING60 "zzz"

/* A string of 60 bytes (non-ASCII).  */
#define STRING60OCT \
  "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" \
  "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" \
  "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" \
  "\377\377\377\377\377\377\377\377\377"

/* A string of 63 bytes (non-ASCII).  */
#define STRING63OCT STRING60OCT "\377\377\377"

/* A string of 60 bytes (non-ASCII, quoted decimal).  */
#define STRING60DEC \
  "\\255\\255\\255\\255\\255\\255\\255\\255\\255\\255" \
  "\\255\\255\\255\\255\\255\\255\\255\\255\\255\\255" \
  "\\255\\255\\255\\255\\255\\255\\255\\255\\255\\255" \
  "\\255\\255\\255\\255\\255\\255\\255\\255\\255\\255" \
  "\\255\\255\\255\\255\\255\\255\\255\\255\\255\\255" \
  "\\255\\255\\255\\255\\255\\255\\255\\255\\255\\255"

/* A string of 63 bytes (non-ASCII, quoted decimal).  */
#define STRING63DEC STRING60DEC "\\255\\255\\255"

/* Combines a test name with the expected results.  */
struct test_case
{
  const char *dn;
  const char *back; /* Expected test result converted using ns_name_ntop.  */
  bool fully_qualified; /* True if the domain name has a trailing dot.  */
};

static const struct test_case tests[] =
  {
    { "", ".", false },
    { ".", ".", true },
    { "..", NULL, },
    { "www", "www", false },
    { "www.", "www", true },
    { "www\\.", "www\\.", false },
    { ".www", NULL, },
    { ".www\\.", NULL, },
    { "example.com", "example.com", false },
    { "example.com.", "example.com", true },
    { ".example.com", NULL, },
    { ".example.com.", NULL, },
    { "example\\.com", "example\\.com", false },
    { "example\\.com.", "example\\.com", true },
    { "example..", NULL, },
    { "example..com", NULL, },
    { "example..com", NULL, },
    { "\\0", NULL, },
    { "\\00", NULL, },
    { "\\000", "\\000", false },
    { "\\1", NULL, },
    { "\\01", NULL, },
    { "\\001", "\\001", false },
    { "\\1x", NULL, },
    { "\\01x", NULL, },
    { "\\001x", "\\001x", false },
    { "\\256", NULL, },
    { "\\0641", "\\@1", false },
    { "\\0011", "\\0011", false },
    { STRING63, STRING63, false },
    { STRING63 ".", STRING63, true },
    { STRING63 "z", NULL, },
    { STRING63 "\\.", NULL, },
    { STRING60 "zz\\.", STRING60 "zz\\.", false },
    { STRING60 "zz\\..", STRING60 "zz\\.", true },
    { STRING63 "." STRING63 "." STRING63 "." STRING60 "z",
      STRING63 "." STRING63 "." STRING63 "." STRING60 "z", false },
    { STRING63 "." STRING63 "." STRING63 "." STRING60 "z.",
      STRING63 "." STRING63 "." STRING63 "." STRING60 "z", true },
    { STRING63 "." STRING63 "." STRING63 "." STRING60 "zz", NULL, },
    { STRING63 "." STRING63 "." STRING63 "." STRING60 "zzz", NULL, },
    { STRING63OCT "." STRING63OCT "." STRING63OCT "." STRING60OCT "\377",
      STRING63DEC "." STRING63DEC "." STRING63DEC "." STRING60DEC "\\255",
      false },
    { STRING63OCT "." STRING63OCT "." STRING63OCT "." STRING60OCT "\377.",
      STRING63DEC "." STRING63DEC "." STRING63DEC "." STRING60DEC "\\255",
      true },
    { STRING63OCT "." STRING63OCT "." STRING63OCT "." STRING60OCT
      "\377\377", NULL, },
    { STRING63OCT "." STRING63OCT "." STRING63OCT "." STRING60OCT
      "\377\377\377", NULL, },
    { "\\", NULL, },
    { "\\\\", "\\\\", false },
    { "\\\\.", "\\\\", true },
    { "\\\\\\", NULL, },
    { "a\\", NULL, },
    { "a.\\", NULL, },
    { "a.b\\", NULL, },
  };

static int
do_test (void)
{
  unsigned char *wire = xmalloc (NS_MAXCDNAME);
  char *text = xmalloc (NS_MAXDNAME);
  for (const struct test_case *test = tests; test < array_end (tests); ++test)
    {
      if (test_verbose)
        printf ("info: testing domain name [[[%s]]]\n", test->dn);
      int ret = ns_name_pton (test->dn, wire, NS_MAXCDNAME);
      if (ret == -1)
        {
          if (test->back != NULL)
            {
              support_record_failure ();
              printf ("error: unexpected decoding failure for [[%s]]\n",
                      test->dn);
            }
          /* Otherwise, we have an expected decoding failure.  */
          continue;
        }

      if (ret < -1 || ret > 1)
        {
          support_record_failure ();
          printf ("error: invalid return value %d for [[%s]]\n",
                  ret, test->dn);
          continue;
        }

      int ret2 = ns_name_ntop (wire, text, NS_MAXDNAME);

      if (ret2 < 0)
        {
          support_record_failure ();
          printf ("error: failure to convert back [[%s]]\n", test->dn);
        }

      if (test->back == NULL)
        {
          support_record_failure ();
          printf ("error: unexpected success converting [[%s]]\n", test->dn);
          if (ret2 >= 1)
            printf ("error:   result converts back to [[%s]]\n", test->dn);
          continue;
        }

      if (strcmp (text, test->back) != 0)
        {
          support_record_failure ();
          printf ("error: back-conversion of [[%s]] did not match\n",
                  test->dn);
          printf ("error:   expected: [[%s]]\n", test->back);
          printf ("error:     actual: [[%s]]\n", text);
        }

      if (ret != test->fully_qualified)
        {
          support_record_failure ();
          printf ("error: invalid fully-qualified status for [[%s]]\n",
                  test->dn);
          printf ("error:   expected: %d\n", (int) test->fully_qualified);
          printf ("error:     actual: %d\n", ret);
        }
    }

  free (text);
  free (wire);
  return 0;
}

#include <support/test-driver.c>