File: test-register.c

package info (click to toggle)
libidn2 2.3.8-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 8,760 kB
  • sloc: ansic: 6,049; sh: 1,480; makefile: 498; xml: 50; perl: 15
file content (243 lines) | stat: -rw-r--r-- 7,938 bytes parent folder | download | duplicates (2)
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
/* test-register.c --- Self tests for IDNA processing
   Copyright (C) 2011-2025 Simon Josefsson

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <stdint.h>

#include <idn2.h>

struct idna
{
  const char *alabel;
  const char *ulabel;
  const char *out;
  int rc;
  int flags;
};

static const struct idna idna[] = {
  {"xn--rksmrgs-5wao1o", "räksmörgås", "xn--rksmrgs-5wao1o", IDN2_OK},
  {NULL, "sharpß", "xn--sharp-pqa", IDN2_OK},
  {"xn--sharp-pqa", "sharpß", "xn--sharp-pqa", IDN2_OK},
  {"foo", NULL, NULL, IDN2_INVALID_ALABEL},
  {NULL, "foo", "foo", IDN2_OK},
  {NULL, "räksmörgås", "xn--rksmrgs-5wao1o", IDN2_OK},
  /* U+00B7 MIDDLE DOT */
  {NULL, "·", "", IDN2_CONTEXTO},
  {NULL, "a·", "", IDN2_CONTEXTO},
  {NULL, "·a", "", IDN2_CONTEXTO},
  {NULL, "a·a", "", IDN2_CONTEXTO},
  {NULL, "l·l", "xn--ll-0ea", IDN2_OK},
  {NULL, "al·la", "xn--alla-6ha", IDN2_OK},
  /* U+0375 GREEK LOWER NUMERAL SIGN (KERAIA) */
  {NULL, "͵", "", IDN2_CONTEXTO},
  {NULL, "͵a", "", IDN2_CONTEXTO},
  {NULL, "͵a͵ϳ", "", IDN2_CONTEXTO},
  {NULL, "͵ϳ͵a", "", IDN2_CONTEXTO},
  {NULL, "͵ϳ", "xn--wva6w", IDN2_OK},
  {NULL, "͵ϳ͵ϳ", "xn--wvaa19ab", IDN2_OK},
  /* U+05F3 HEBREW PUNCTUATION GERESH */
  {NULL, "׳", "", IDN2_CONTEXTO},
  {NULL, "a׳", "", IDN2_CONTEXTO},
  {NULL, "a׳א׳", "", IDN2_CONTEXTO},
  {NULL, "א׳a׳", "", IDN2_CONTEXTO},
  {NULL, "א׳", "xn--4db4e", IDN2_OK},
  {NULL, "בא׳ב", "xn--4dbbb9k", IDN2_OK},
  /* U+05F4 HEBREW PUNCTUATION GERSHAYIM */
  {NULL, "״", "", IDN2_CONTEXTO},
  {NULL, "a״", "", IDN2_CONTEXTO},
  {NULL, "a״א", "", IDN2_CONTEXTO},
  {NULL, "א״", "xn--4db6e", IDN2_OK},
  {NULL, "בא״ב", "xn--4dbbb3l", IDN2_OK},
  /* U+0660..U+0669 ARABIC-INDIC DIGITS and
     U+06F0..U+06F9 EXTENDED ARABIC-INDIC DIGITS */
  {NULL, "٠", "", IDN2_BIDI},
  {NULL, "ء٠", "xn--ggb0k", IDN2_OK},
  {NULL, "ء۰", "xn--ggb82b", IDN2_OK},
  {NULL, "ء٠ءء", "xn--ggbaa4w", IDN2_OK},
  {NULL, "ء٠۰", "", IDN2_CONTEXTO},
  {NULL, "ء٠ءء۰", "", IDN2_CONTEXTO},
  {NULL, "ء۰ءء٠", "", IDN2_CONTEXTO},
  {NULL, "٠ء۰ءء٠", "", IDN2_CONTEXTO},
  /* U+30FB KATAKANA MIDDLE DOT */
  {NULL, "・", "", IDN2_CONTEXTO},
  {NULL, "foo・", "", IDN2_CONTEXTO},
  {NULL, "foo・bar", "", IDN2_CONTEXTO},
  {NULL, "foo・barぁbaz",	/* U+3041 HIRAGANA LETTER SMALL A */
   "xn--foobarbaz-b23h61e", IDN2_OK},
  {NULL, "foo・barァbaz",	/* U+30A1 KATAKANA LETTER SMALL A */
   "xn--foobarbaz-qu4h06a", IDN2_OK},
  {NULL, "foo・bar〇baz",	/* U+3007 IDEOGRAPHIC NUMBER ZERO */
   "xn--foobarbaz-ql3hk3g", IDN2_OK},
  {NULL, "foo・bar㐀baz",	/* U+3400 CJK UNIFIED IDEOGRAPH-3400 */
   "xn--foobarbaz-dl5hq7z", IDN2_OK},
  {NULL, "foo・bar㐀baz",	/* U+3400 CJK UNIFIED IDEOGRAPH-3400 */
   "xn--foobarbaz-dl5hq7z", IDN2_OK},
  {				/* A-Label with 63 chars */
   "xn--dominiomuylargoconmuchas-olcaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
   "dominiomuylargoconmuchas\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1",
   "xn--dominiomuylargoconmuchas-olcaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
   IDN2_OK},
  {				/* A-Label with 64 chars */
   "xn--dominiomuylargoconmuchas-olcaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
   "dominiomuylargoconmuchas\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1\xc3\xb1",
   "xn--dominiomuylargoconmuchas-olcaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
   IDN2_TOO_BIG_LABEL},
};


int
main (void)
{
  int error_count = 0;
  uint8_t *out;
  unsigned i;
  int rc;

  puts ("-----------------------------------------------------------"
	"-------------------------------------");
  puts ("                                          IDNA2008 Register\n");
  puts ("  #  Result                    Output                    A-label"
	" input             U-label input");
  puts ("-----------------------------------------------------------"
	"-------------------------------------");
  for (i = 0; i < sizeof (idna) / sizeof (idna[0]); i++)
    {
      out = (uint8_t *) 0x1234;
      rc = idn2_register_u8 ((uint8_t *) idna[i].ulabel,
			     (uint8_t *) idna[i].alabel, &out, idna[i].flags);
      printf ("%3u  %-25s %-25s %-25s %s\n", i, idn2_strerror_name (rc),
	      rc == IDN2_OK ? idna[i].out : "",
	      idna[i].alabel ? idna[i].alabel : "(null)",
	      idna[i].ulabel ? idna[i].ulabel : "(null)");
      if (rc != idna[i].rc)
	{
	  printf ("expected rc %d got rc %d\n", idna[i].rc, rc);
	  error_count++;
	}
      else if (rc == IDN2_OK && strcmp ((char *) out, idna[i].out) != 0)
	{
	  printf ("expected: %s\ngot: %s\n", idna[i].out, out);
	  error_count++;
	}

      if (rc == IDN2_OK)
	{
	  uint8_t *tmp;

	  if (out == (void *) 0x1234)
	    {
	      printf ("out has not been set\n");
	      error_count++;
	    }

	  rc = idn2_lookup_u8 ((uint8_t *) idna[i].ulabel, &tmp,
			       idna[i].flags);
	  if (rc != IDN2_OK)
	    {
	      printf ("lookup failed?! tv %u\n", i);
	      error_count++;
	    }
	  if (strcmp ((char *) out, (char *) tmp) != 0)
	    {
	      printf
		("lookup and register different? lookup %s register %s\n",
		 tmp, out);
	      error_count++;
	    }
	  idn2_free (tmp);
	  idn2_free (out);
	}
      else
	{
	  if (out != (void *) 0x1234)
	    {
	      printf ("out has been tainted on error\n");
	      error_count++;
	    }
	}
    }
  puts ("-----------------------------------------------------------"
	"-------------------------------------");

  /* special calls to cover edge cases */
  if ((rc = idn2_register_u8 (NULL, NULL, NULL, 0)) != IDN2_OK)
    {
      printf ("special #1 failed with %d\n", rc);
      error_count++;
    }

  out = (uint8_t *) 0x123;
  if ((rc = idn2_register_u8 (NULL, NULL, &out, 0)) != IDN2_OK)
    {
      printf ("special #2 failed with %d\n", rc);
      error_count++;
    }
  if (out)
    {
      printf ("special #2 failed with out!=NULL\n");
      error_count++;
    }

  if ((rc = idn2_register_u8 (NULL, (uint8_t *) "xn+-xxx",
			      &out, 0)) != IDN2_INVALID_ALABEL)
    {
      printf ("special #3 failed with %d\n", rc);
      error_count++;
    }
  if (out)
    {
      printf ("special #3 failed with out!=NULL\n");

      error_count++;
    }
  if ((rc = idn2_register_u8 (NULL, (uint8_t *) "xn--\xff",
			      &out, 0)) != IDN2_INVALID_ALABEL)
    {
      printf ("special #4 failed with %d\n", rc);
      error_count++;
    }
  if (out)
    {
      printf ("special #4 failed with out!=NULL\n");

      error_count++;
    }
  if ((rc = idn2_register_ul (NULL, NULL, NULL, 0)) != IDN2_OK)
    {
      printf ("special #5 failed with %d\n", rc);

      error_count++;
    }
  if ((rc = idn2_register_u8 ((uint8_t *) "faß",
			      NULL, NULL, IDN2_NFC_INPUT)) != IDN2_OK)
    {
      printf ("special #7 failed with %d\n", rc);
      error_count++;
    }

  printf ("\nerror_count: %d\n", error_count);

  return !!error_count;
}