Fix printf() format warnings.
[libidn.git] / tst_stringprep.c
blobb91033a73a4b9c6645456aafc335c783a418e674
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 #include "internal.h"
24 static int debug = 0;
25 static int error_count = 0;
26 static int break_on_error = 0;
28 static void
29 fail (const char *format, ...)
31 va_list arg_ptr;
33 va_start (arg_ptr, format);
34 vfprintf (stderr, format, arg_ptr);
35 va_end (arg_ptr);
36 error_count++;
37 if (break_on_error)
38 exit (1);
41 static void
42 escapeprint (char *str, int len)
44 int i;
46 printf ("\t;; `");
47 for (i = 0; i < len; i++)
49 if (((str[i] & 0xFF) >= 'A' && (str[i] & 0xFF) <= 'Z') ||
50 ((str[i] & 0xFF) >= 'a' && (str[i] & 0xFF) <= 'z') ||
51 ((str[i] & 0xFF) >= '0' && (str[i] & 0xFF) <= '9')
52 || (str[i] & 0xFF) == '.')
53 printf ("%c", (str[i] & 0xFF));
54 else
55 printf ("\\x%02x", (str[i] & 0xFF));
57 printf ("' (length %d bytes)\n", len);
60 static void
61 hexprint (char *str, int len)
63 int i;
65 printf ("\t;; ");
66 for (i = 0; i < len; i++)
68 printf ("%02x ", (str[i] & 0xFF));
69 if ((i + 1) % 8 == 0)
70 printf (" ");
71 if ((i + 1) % 16 == 0 && i + 1 < len)
72 printf ("\n\t;; ");
76 static void
77 binprint (char *str, int len)
79 int i;
81 printf ("\t;; ");
82 for (i = 0; i < len; i++)
84 printf ("%d%d%d%d%d%d%d%d ",
85 (str[i] & 0xFF) & 0x80 ? 1 : 0,
86 (str[i] & 0xFF) & 0x40 ? 1 : 0,
87 (str[i] & 0xFF) & 0x20 ? 1 : 0,
88 (str[i] & 0xFF) & 0x10 ? 1 : 0,
89 (str[i] & 0xFF) & 0x08 ? 1 : 0,
90 (str[i] & 0xFF) & 0x04 ? 1 : 0,
91 (str[i] & 0xFF) & 0x02 ? 1 : 0, (str[i] & 0xFF) & 0x01 ? 1 : 0);
92 if ((i + 1) % 3 == 0)
93 printf (" ");
94 if ((i + 1) % 6 == 0 && i + 1 < len)
95 printf ("\n\t;; ");
99 struct stringprep
101 char *in;
102 int flags;
103 char *out;
104 Stringprep_profile *profile;
105 int rc;
107 strprep[] =
109 /* map to nothing U+00AD */
111 "foo\xC2\xAD" "bar", 0, "foobar", stringprep_generic}
113 /* map case_nfkc + normalization: */
115 "\xC2\xB5", 0, "\xCE\xBC", stringprep_generic}
117 /* case_nonfkc: */
119 "\xC2\xB5", STRINGPREP_NO_NFKC, "\xCE\xBC", stringprep_generic}
122 "\xC2\xAA", 0, "\x61", stringprep_generic}
124 /* unassigned code point U+0221: */
126 "\xC8\xA1", 0, "\xC8\xA1", stringprep_generic}
128 /* unassigned code point U+0221: */
130 "\xC8\xA1", STRINGPREP_NO_UNASSIGNED, NULL, stringprep_generic,
131 STRINGPREP_CONTAINS_UNASSIGNED}
133 /* unassigned code point U+0236: */
135 "\xC8\xB6", 0, "\xC8\xB6", stringprep_generic}
137 /* unassigned code point U+0236: */
139 "\xC8\xB6", STRINGPREP_NO_UNASSIGNED, NULL, stringprep_generic,
140 STRINGPREP_CONTAINS_UNASSIGNED},
141 /* prohibited ASCII character U+0020: */
143 "\x20", 0, NULL, stringprep_generic,
144 STRINGPREP_CONTAINS_PROHIBITED},
145 /* prohibited character U+00A0: */
147 "\xC2\xA0", 0, NULL, stringprep_generic,
148 STRINGPREP_CONTAINS_PROHIBITED},
149 /* prohibited non-character U+10FFFE: */
151 "\xF4\x8F\xBF\xBE", 0, NULL, stringprep_generic,
152 STRINGPREP_CONTAINS_PROHIBITED},
153 /* prohibited surrogate character U+D801: */
155 "\xED\xA0\x81", 0, NULL, stringprep_generic,
156 STRINGPREP_CONTAINS_PROHIBITED},
157 /* bidi RandALCat without trailing RandALCat <U+0627><U+0031>: */
159 "\xD8\xA7\x31", 0, NULL, stringprep_generic,
160 STRINGPREP_BIDI_LEADTRAIL_NOT_RAL},
161 /* bidi RandALCat correct <U+0627><U+0031><U+0628>: */
163 "\xD8\xA7\x31\xD8\xA8", 0, "\xD8\xA7\x31\xD8\xA8", stringprep_generic},
164 /* bidi both RandALCat and LCat <U+0627><U+00AA><U+0628>: */
166 "\xD8\xA7\xC2\xAA\xD8\xA8", 0, NULL, stringprep_generic,
167 STRINGPREP_BIDI_BOTH_L_AND_RAL}
171 main (int argc, char *argv[])
173 char *p;
174 int rc, i;
177 if (strcmp (argv[argc - 1], "-v") == 0 ||
178 strcmp (argv[argc - 1], "--verbose") == 0)
179 debug = 1;
180 else if (strcmp (argv[argc - 1], "-b") == 0 ||
181 strcmp (argv[argc - 1], "--break-on-error") == 0)
182 break_on_error = 1;
183 else if (strcmp (argv[argc - 1], "-h") == 0 ||
184 strcmp (argv[argc - 1], "-?") == 0 ||
185 strcmp (argv[argc - 1], "--help") == 0)
187 printf ("Usage: %s [-vbh?] [--verbose] [--break-on-error] [--help]\n",
188 argv[0]);
189 return 1;
191 while (argc-- > 1);
193 p = malloc (BUFSIZ);
194 if (p == NULL)
195 fail ("malloc() returned NULL\n");
197 for (i = 0; i < sizeof (strprep) / sizeof (strprep[0]); i++)
199 if (debug)
200 printf ("STRINGPREP entry %d\n", i);
202 strcpy (p, strprep[i].in);
204 if (debug)
206 printf ("flags: %d\n", strprep[i].flags);
208 printf ("in:\n");
209 escapeprint (strprep[i].in, strlen (strprep[i].in));
210 hexprint (strprep[i].in, strlen (strprep[i].in));
211 puts ("");
212 binprint (strprep[i].in, strlen (strprep[i].in));
213 puts ("");
216 rc = stringprep (p, BUFSIZ, strprep[i].flags, strprep[i].profile);
217 if (rc != strprep[i].rc)
219 fail ("stringprep() entry %d failed: %d\n", i, rc);
220 if (debug)
221 printf ("FATAL\n");
222 continue;
225 if (debug && rc == STRINGPREP_OK)
227 printf ("out:\n");
228 escapeprint (p, strlen (p));
229 hexprint (p, strlen (p));
230 puts ("");
231 binprint (p, strlen (p));
232 puts ("");
234 printf ("expected out:\n");
235 escapeprint (strprep[i].out, strlen (strprep[i].out));
236 hexprint (strprep[i].out, strlen (strprep[i].out));
237 puts ("");
238 binprint (strprep[i].out, strlen (strprep[i].out));
239 puts ("");
241 else if (debug)
242 printf("returned %d expected %d\n", rc, strprep[i].rc);
244 if (rc == STRINGPREP_OK)
246 if (strlen (strprep[i].out) != strlen (p) ||
247 memcmp (strprep[i].out, p, strlen (p)) != 0)
249 fail ("stringprep() entry %d failed\n", i);
250 if (debug)
251 printf ("ERROR\n");
253 else if (debug)
254 printf ("OK\n\n");
256 else if (debug)
257 printf ("OK\n\n");
260 free (p);
262 #if 0
263 memset(p, 0, 10);
264 stringprep_unichar_to_utf8 (0x0628, p);
265 hexprint (p, strlen (p)); puts("");
266 #endif
268 if (debug)
269 printf ("Stringprep self tests done with %d errors\n", error_count);
271 return error_count ? 1 : 0;