Handle draft-hoffman-idn-reg-*.txt tables better.
[libidn.git] / tests / tst_tld.c
blob0de67dd13c3a98e4197fef854266d5e428fff5d8
1 /* tst_tld.c --- Self tests for tld_*().
2 * Copyright (C) 2004 Simon Josefsson.
4 * This file is part of GNU Libidn.
6 * GNU Libidn is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GNU Libidn is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Libidn; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #if HAVE_CONFIG_H
23 # include "config.h"
24 #endif
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <string.h>
31 #include <stringprep.h>
32 #include <tld.h>
34 static int debug = 0;
35 static int error_count = 0;
36 static int break_on_error = 0;
38 static void
39 fail (const char *format, ...)
41 va_list arg_ptr;
43 va_start (arg_ptr, format);
44 vfprintf (stderr, format, arg_ptr);
45 va_end (arg_ptr);
46 error_count++;
47 if (break_on_error)
48 exit (1);
51 static void
52 ucs4print (const uint32_t * str, size_t len)
54 size_t i;
56 printf ("\t;; ");
57 for (i = 0; i < len; i++)
59 printf ("U+%04ux ", str[i]);
60 if ((i + 1) % 4 == 0)
61 printf (" ");
62 if ((i + 1) % 8 == 0 && i + 1 < len)
63 printf ("\n\t;; ");
65 puts ("");
68 struct tld
70 char *name;
71 char *tld;
72 size_t inlen;
73 uint32_t in[100];
74 int rc;
75 int errpos;
78 const struct tld tld[] = {
80 "Simple valid French domain",
81 "fr",
83 {0x00E0, 0x00E2, 0x00E6},
84 TLD_SUCCESS},
86 "Simple invalid French domain",
87 "fr",
89 {0x00E0, 0x00E2, 0x00E6, 0x4711, 0x0042},
90 TLD_INVALID,
94 int
95 main (int argc, char *argv[])
97 size_t i;
98 const Tld_table *tldtable;
99 int errpos;
100 int rc;
103 if (strcmp (argv[argc - 1], "-v") == 0 ||
104 strcmp (argv[argc - 1], "--verbose") == 0)
105 debug = 1;
106 else if (strcmp (argv[argc - 1], "-b") == 0 ||
107 strcmp (argv[argc - 1], "--break-on-error") == 0)
108 break_on_error = 1;
109 else if (strcmp (argv[argc - 1], "-h") == 0 ||
110 strcmp (argv[argc - 1], "-?") == 0 ||
111 strcmp (argv[argc - 1], "--help") == 0)
113 printf ("Usage: %s [-vbh?] [--verbose] [--break-on-error] [--help]\n",
114 argv[0]);
115 return 1;
117 while (argc-- > 1);
119 for (i = 0; i < sizeof (tld) / sizeof (tld[0]); i++)
121 if (debug)
122 printf ("TLD entry %d: %s\n", i, tld[i].name);
124 if (debug)
126 printf ("in:\n");
127 ucs4print (tld[i].in, tld[i].inlen);
130 tldtable = tld_default_table (tld[i].tld, NULL);
131 if (tldtable == NULL)
133 fail ("TLD entry %d tld_get_table (%s)\n", i, tld[i].tld);
134 if (debug)
135 printf ("FATAL\n");
136 continue;
139 rc = tld_check_4t (tld[i].in, tld[i].inlen, &errpos, tldtable);
140 if (rc != tld[i].rc)
142 fail ("TLD entry %d failed: %d\n", i, rc);
143 if (debug)
144 printf ("FATAL\n");
145 continue;
148 if (debug)
149 printf ("returned %d expected %d\n", rc, tld[i].rc);
151 if (rc != tld[i].rc)
153 fail ("TLD entry %d failed\n", i);
154 if (debug)
155 printf ("ERROR\n");
157 else if (rc == TLD_INVALID)
159 if (debug)
160 printf ("returned errpos %d expected errpos %d\n",
161 errpos, tld[i].errpos);
163 if (tld[i].errpos != errpos)
165 fail ("TLD entry %d failed because errpos %d != %d\n", i,
166 tld[i].errpos, errpos);
167 if (debug)
168 printf ("ERROR\n");
171 else if (debug)
172 printf ("OK\n");
175 if (debug)
176 printf ("TLD self tests done with %d errors\n", error_count);
178 return error_count ? 1 : 0;