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
31 #include <stringprep.h>
35 static int error_count
= 0;
36 static int break_on_error
= 0;
39 fail (const char *format
, ...)
43 va_start (arg_ptr
, format
);
44 vfprintf (stderr
, format
, arg_ptr
);
52 ucs4print (const uint32_t * str
, size_t len
)
57 for (i
= 0; i
< len
; i
++)
59 printf ("U+%04ux ", str
[i
]);
62 if ((i
+ 1) % 8 == 0 && i
+ 1 < len
)
78 const struct tld tld
[] = {
80 "Simple valid French domain",
83 {0x00E0, 0x00E2, 0x00E6},
86 "Simple invalid French domain",
89 {0x00E0, 0x00E2, 0x00E6, 0x4711, 0x0042},
95 main (int argc
, char *argv
[])
98 const Tld_table
*tldtable
;
103 if (strcmp (argv
[argc
- 1], "-v") == 0 ||
104 strcmp (argv
[argc
- 1], "--verbose") == 0)
106 else if (strcmp (argv
[argc
- 1], "-b") == 0 ||
107 strcmp (argv
[argc
- 1], "--break-on-error") == 0)
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",
119 for (i
= 0; i
< sizeof (tld
) / sizeof (tld
[0]); i
++)
122 printf ("TLD entry %d: %s\n", i
, tld
[i
].name
);
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
);
139 rc
= tld_check_4t (tld
[i
].in
, tld
[i
].inlen
, &errpos
, tldtable
);
142 fail ("TLD entry %d failed: %d\n", i
, rc
);
149 printf ("returned %d expected %d\n", rc
, tld
[i
].rc
);
153 fail ("TLD entry %d failed\n", i
);
157 else if (rc
== TLD_INVALID
)
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
);
176 printf ("TLD self tests done with %d errors\n", error_count
);
178 return error_count
? 1 : 0;