Export stringprep_utf8_to_unichar.
[libidn.git] / tst_stringprep.c
blob2cd2ee78bc7f12deccd7719c5c9afc438acf9581
1 /* tst_stringprep.c libstringprep self tests for stringprep_stringprep()
2 * Copyright (C) 2002 Simon Josefsson
4 * This file is part of libstringprep.
6 * Libstringprep 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 * Libstringprep 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 libstringprep; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #if HAVE_STRING_H
30 # if !STDC_HEADERS && HAVE_MEMORY_H
31 # include <memory.h>
32 # endif
33 # include <string.h>
34 #endif
35 #if HAVE_STRINGS_H
36 # include <strings.h>
37 #endif
38 #if HAVE_INTTYPES_H
39 # include <inttypes.h>
40 #else
41 # if HAVE_STDINT_H
42 # include <stdint.h>
43 # endif
44 #endif
45 #if HAVE_SYS_TYPES_H
46 #include <sys/types.h>
47 #endif
49 #include <stringprep.h>
50 #include <stringprep_generic.h>
52 static int debug = 0;
53 static int error_count = 0;
54 static int break_on_error = 0;
56 static void
57 fail (const char *format, ...)
59 va_list arg_ptr;
61 va_start (arg_ptr, format);
62 vfprintf (stderr, format, arg_ptr);
63 va_end (arg_ptr);
64 error_count++;
65 if (break_on_error)
66 exit (1);
69 static void
70 escapeprint (char *str, int len)
72 int i;
74 printf ("\t ;; `");
75 for (i = 0; i < len; i++)
77 str[i] = str[i] & 0xFF;
78 if ((str[i] >= 'A' && str[i] <= 'Z') ||
79 (str[i] >= 'a' && str[i] <= 'z') ||
80 (str[i] >= '0' && str[i] <= '9') || str[i] == '.')
81 printf ("%c", str[i]);
82 else
83 printf ("\\x%02x", str[i]);
85 printf ("' (length %d bytes)\n", len);
88 static void
89 hexprint (char *str, int len)
91 int i;
93 printf ("\t ;; ");
94 for (i = 0; i < len; i++)
96 str[i] = str[i] & 0xFF;
97 printf ("%02x ", str[i]);
98 if ((i + 1) % 8 == 0)
99 printf (" ");
100 if ((i + 1) % 16 == 0 && i + 1 < len)
101 printf ("\n\t ;; ");
105 static void
106 binprint (char *str, int len)
108 int i;
110 printf ("\t ;; ");
111 for (i = 0; i < len; i++)
113 str[i] = str[i] & 0xFF;
114 printf ("%d%d%d%d%d%d%d%d ",
115 str[i] & 0x80 ? 1 : 0,
116 str[i] & 0x40 ? 1 : 0,
117 str[i] & 0x20 ? 1 : 0,
118 str[i] & 0x10 ? 1 : 0,
119 str[i] & 0x08 ? 1 : 0,
120 str[i] & 0x04 ? 1 : 0,
121 str[i] & 0x02 ? 1 : 0, str[i] & 0x01 ? 1 : 0);
122 if ((i + 1) % 3 == 0)
123 printf (" ");
124 if ((i + 1) % 6 == 0 && i + 1 < len)
125 printf ("\n\t ;; ");
129 struct stringprep
131 char *in;
132 int flags;
133 char *out;
134 stringprep_profile *profile;
136 strprep[] =
138 /* nothing */
140 "foo\xC2\xAD" "bar", 0, "foobar", stringprep_generic}
142 /* case_nfkc + normalization: */
144 "\xC2\xB5", 0, "\xCE\xBC", stringprep_generic}
146 /* case_nonfkc: */
148 "\xC2\xB5", STRINGPREP_NO_NFKC, "\xCE\xBC", stringprep_generic}
151 "\xC2\xAA", 0, "\x61", stringprep_generic}
155 main (int argc, char *argv[])
157 char *p;
158 int rc, i;
161 if (strcmp (argv[argc - 1], "-v") == 0 ||
162 strcmp (argv[argc - 1], "--verbose") == 0)
163 debug = 1;
164 else if (strcmp (argv[argc - 1], "-b") == 0 ||
165 strcmp (argv[argc - 1], "--break-on-error") == 0)
166 break_on_error = 1;
167 else if (strcmp (argv[argc - 1], "-h") == 0 ||
168 strcmp (argv[argc - 1], "-?") == 0 ||
169 strcmp (argv[argc - 1], "--help") == 0)
171 printf ("Usage: %s [-vbh?] [--verbose] [--break-on-error] [--help]\n",
172 argv[0]);
173 return 1;
175 while (argc-- > 1);
177 p = malloc (BUFSIZ);
178 if (p == NULL)
179 fail ("malloc() returned NULL\n");
181 for (i = 0; i < sizeof (strprep) / sizeof (strprep[0]); i++)
183 if (debug)
184 printf ("STRINGPREP entry %d\n", i);
186 strcpy (p, strprep[i].in);
188 rc = stringprep (p, BUFSIZ, strprep[i].flags, strprep[i].profile);
189 if (rc != STRINGPREP_OK)
191 fail ("stringprep() entry %d failed: %d\n", i, rc);
192 continue;
195 if (debug)
197 printf ("in:\n");
198 escapeprint (strprep[i].in, strlen (strprep[i].in));
199 hexprint (strprep[i].in, strlen (strprep[i].in));
200 puts ("");
201 binprint (strprep[i].in, strlen (strprep[i].in));
202 puts ("");
204 printf ("out:\n");
205 escapeprint (p, strlen (p));
206 hexprint (p, strlen (p));
207 puts ("");
208 binprint (p, strlen (p));
209 puts ("");
211 printf ("expected out:\n");
212 escapeprint (strprep[i].out, strlen (strprep[i].out));
213 hexprint (strprep[i].out, strlen (strprep[i].out));
214 puts ("");
215 binprint (strprep[i].out, strlen (strprep[i].out));
216 puts ("");
219 if (strlen (strprep[i].out) != strlen (p) ||
220 memcmp (strprep[i].out, p, strlen (p)) != 0)
222 fail ("stringprep() entry %d failed\n", i);
223 if (debug)
224 printf ("ERROR\n");
226 else if (debug)
227 printf ("OK\n");
230 free (p);
232 if (debug)
233 printf ("Stringprep self tests done with %d errors\n", error_count);
235 return error_count ? 1 : 0;