*** empty log message ***
[libidn.git] / tst_stringprep.c
blobe732b42403048bf10ae1fbecc55a2ffe8a093e10
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 str[i] = str[i] & 0xFF;
50 if ((str[i] >= 'A' && str[i] <= 'Z') ||
51 (str[i] >= 'a' && str[i] <= 'z') ||
52 (str[i] >= '0' && str[i] <= '9') || str[i] == '.')
53 printf ("%c", str[i]);
54 else
55 printf ("\\x%02x", str[i]);
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 str[i] = str[i] & 0xFF;
69 printf ("%02x ", str[i]);
70 if ((i + 1) % 8 == 0)
71 printf (" ");
72 if ((i + 1) % 16 == 0 && i + 1 < len)
73 printf ("\n\t ;; ");
77 static void
78 binprint (char *str, int len)
80 int i;
82 printf ("\t ;; ");
83 for (i = 0; i < len; i++)
85 str[i] = str[i] & 0xFF;
86 printf ("%d%d%d%d%d%d%d%d ",
87 str[i] & 0x80 ? 1 : 0,
88 str[i] & 0x40 ? 1 : 0,
89 str[i] & 0x20 ? 1 : 0,
90 str[i] & 0x10 ? 1 : 0,
91 str[i] & 0x08 ? 1 : 0,
92 str[i] & 0x04 ? 1 : 0,
93 str[i] & 0x02 ? 1 : 0, str[i] & 0x01 ? 1 : 0);
94 if ((i + 1) % 3 == 0)
95 printf (" ");
96 if ((i + 1) % 6 == 0 && i + 1 < len)
97 printf ("\n\t ;; ");
101 struct stringprep
103 char *in;
104 int flags;
105 char *out;
106 stringprep_profile *profile;
108 strprep[] =
110 /* nothing */
112 "foo\xC2\xAD" "bar", 0, "foobar", stringprep_generic}
114 /* case_nfkc + normalization: */
116 "\xC2\xB5", 0, "\xCE\xBC", stringprep_generic}
118 /* case_nonfkc: */
120 "\xC2\xB5", STRINGPREP_NO_NFKC, "\xCE\xBC", stringprep_generic}
123 "\xC2\xAA", 0, "\x61", stringprep_generic}
127 main (int argc, char *argv[])
129 char *p;
130 int rc, i;
133 if (strcmp (argv[argc - 1], "-v") == 0 ||
134 strcmp (argv[argc - 1], "--verbose") == 0)
135 debug = 1;
136 else if (strcmp (argv[argc - 1], "-b") == 0 ||
137 strcmp (argv[argc - 1], "--break-on-error") == 0)
138 break_on_error = 1;
139 else if (strcmp (argv[argc - 1], "-h") == 0 ||
140 strcmp (argv[argc - 1], "-?") == 0 ||
141 strcmp (argv[argc - 1], "--help") == 0)
143 printf ("Usage: %s [-vbh?] [--verbose] [--break-on-error] [--help]\n",
144 argv[0]);
145 return 1;
147 while (argc-- > 1);
149 p = malloc (BUFSIZ);
150 if (p == NULL)
151 fail ("malloc() returned NULL\n");
153 for (i = 0; i < sizeof (strprep) / sizeof (strprep[0]); i++)
155 if (debug)
156 printf ("STRINGPREP entry %d\n", i);
158 strcpy (p, strprep[i].in);
160 rc = stringprep (p, BUFSIZ, strprep[i].flags, strprep[i].profile);
161 if (rc != STRINGPREP_OK)
163 fail ("stringprep() entry %d failed: %d\n", i, rc);
164 continue;
167 if (debug)
169 printf ("in:\n");
170 escapeprint (strprep[i].in, strlen (strprep[i].in));
171 hexprint (strprep[i].in, strlen (strprep[i].in));
172 puts ("");
173 binprint (strprep[i].in, strlen (strprep[i].in));
174 puts ("");
176 printf ("out:\n");
177 escapeprint (p, strlen (p));
178 hexprint (p, strlen (p));
179 puts ("");
180 binprint (p, strlen (p));
181 puts ("");
183 printf ("expected out:\n");
184 escapeprint (strprep[i].out, strlen (strprep[i].out));
185 hexprint (strprep[i].out, strlen (strprep[i].out));
186 puts ("");
187 binprint (strprep[i].out, strlen (strprep[i].out));
188 puts ("");
191 if (strlen (strprep[i].out) != strlen (p) ||
192 memcmp (strprep[i].out, p, strlen (p)) != 0)
194 fail ("stringprep() entry %d failed\n", i);
195 if (debug)
196 printf ("ERROR\n");
198 else if (debug)
199 printf ("OK\n");
202 free (p);
204 if (debug)
205 printf ("Stringprep self tests done with %d errors\n", error_count);
207 return error_count ? 1 : 0;