Add.
[gsasl.git] / tests / unicode.c
blob9b4af1338b0cf9c5a81d5823249bb277d8c64e61
1 /* unicode.c --- Self tests for unicode related functions.
2 * Copyright (C) 2002, 2005 Simon Josefsson
4 * This file is part of GNU SASL.
6 * GNU SASL 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 SASL 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 SASL; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <gsasl.h>
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
31 static int debug = 0;
32 static int error_count = 0;
33 static int break_on_error = 0;
35 static void
36 fail (const char *format, ...)
38 va_list arg_ptr;
40 va_start (arg_ptr, format);
41 vfprintf (stderr, format, arg_ptr);
42 va_end (arg_ptr);
43 error_count++;
44 if (break_on_error)
45 exit (1);
48 static void
49 escapeprint (char *str, int len)
51 int i;
53 printf ("\t ;; `");
54 for (i = 0; i < len; i++)
55 if ((str[i] >= 'A' && str[i] <= 'Z') ||
56 (str[i] >= 'a' && str[i] <= 'z') ||
57 (str[i] >= '0' && str[i] <= '9') || str[i] == '.')
58 printf ("%c", str[i]);
59 else
60 printf ("\\x%02x", str[i]);
61 printf ("' (length %d bytes)\n", len);
64 static void
65 hexprint (char *str, int len)
67 int i;
69 printf ("\t ;; ");
70 for (i = 0; i < len; i++)
72 printf ("%02x ", str[i]);
73 if ((i + 1) % 8 == 0)
74 printf (" ");
75 if ((i + 1) % 16 == 0 && i + 1 < len)
76 printf ("\n\t ;; ");
80 static void
81 binprint (char *str, int len)
83 int i;
85 printf ("\t ;; ");
86 for (i = 0; i < len; i++)
88 printf ("%d%d%d%d%d%d%d%d ",
89 str[i] & 0x80 ? 1 : 0,
90 str[i] & 0x40 ? 1 : 0,
91 str[i] & 0x20 ? 1 : 0,
92 str[i] & 0x10 ? 1 : 0,
93 str[i] & 0x08 ? 1 : 0,
94 str[i] & 0x04 ? 1 : 0,
95 str[i] & 0x02 ? 1 : 0, str[i] & 0x01 ? 1 : 0);
96 if ((i + 1) % 3 == 0)
97 printf (" ");
98 if ((i + 1) % 6 == 0 && i + 1 < len)
99 printf ("\n\t ;; ");
103 struct nfkc
105 char *in;
106 char *out;
108 nfkc[] =
111 "\xC2\xB5", "\xCE\xBC"}
114 "\xC2\xAA", "\x61"}
118 main (int argc, char *argv[])
120 char *out;
121 int i;
124 if (strcmp (argv[argc - 1], "-v") == 0 ||
125 strcmp (argv[argc - 1], "--verbose") == 0)
126 debug = 1;
127 else if (strcmp (argv[argc - 1], "-b") == 0 ||
128 strcmp (argv[argc - 1], "--break-on-error") == 0)
129 break_on_error = 1;
130 else if (strcmp (argv[argc - 1], "-h") == 0 ||
131 strcmp (argv[argc - 1], "-?") == 0 ||
132 strcmp (argv[argc - 1], "--help") == 0)
134 printf ("Usage: %s [-vbh?] [--verbose] [--break-on-error] [--help]\n",
135 argv[0]);
136 return 1;
138 while (argc-- > 1);
140 for (i = 0; i < sizeof (nfkc) / sizeof (nfkc[0]); i++)
142 if (debug)
143 printf ("NFKC entry %d\n", i);
145 out = stringprep_utf8_nfkc_normalize (nfkc[i].in, strlen (nfkc[i].in));
146 if (out == NULL)
148 fail ("gsasl_utf8_nfkc_normalize() entry %d failed fatally\n", i);
149 continue;
152 if (debug)
154 printf ("in:\n");
155 escapeprint (nfkc[i].in, strlen (nfkc[i].in));
156 hexprint (nfkc[i].in, strlen (nfkc[i].in));
157 puts ("");
158 binprint (nfkc[i].in, strlen (nfkc[i].in));
159 puts ("");
161 printf ("out:\n");
162 escapeprint (out, strlen (out));
163 hexprint (out, strlen (out));
164 puts ("");
165 binprint (out, strlen (out));
166 puts ("");
168 printf ("expected out:\n");
169 escapeprint (nfkc[i].out, strlen (nfkc[i].out));
170 hexprint (nfkc[i].out, strlen (nfkc[i].out));
171 puts ("");
172 binprint (nfkc[i].out, strlen (nfkc[i].out));
173 puts ("");
176 if (strlen (nfkc[i].out) != strlen (out) ||
177 memcmp (nfkc[i].out, out, strlen (out)) != 0)
179 fail ("gsasl_utf8_nfkc_normalize() entry %d failed\n", i);
180 if (debug)
181 printf ("ERROR\n");
183 else if (debug)
184 printf ("OK\n");
187 if (debug)
188 printf ("Libgsasl unicode self tests done with %d errors\n", error_count);
190 return error_count ? 1 : 0;