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.
32 static int error_count
= 0;
33 static int break_on_error
= 0;
36 fail (const char *format
, ...)
40 va_start (arg_ptr
, format
);
41 vfprintf (stderr
, format
, arg_ptr
);
49 escapeprint (char *str
, int len
)
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
]);
60 printf ("\\x%02x", str
[i
]);
61 printf ("' (length %d bytes)\n", len
);
65 hexprint (char *str
, int len
)
70 for (i
= 0; i
< len
; i
++)
72 printf ("%02x ", str
[i
]);
75 if ((i
+ 1) % 16 == 0 && i
+ 1 < len
)
81 binprint (char *str
, int len
)
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);
98 if ((i
+ 1) % 6 == 0 && i
+ 1 < len
)
111 "\xC2\xB5", "\xCE\xBC"}
118 main (int argc
, char *argv
[])
124 if (strcmp (argv
[argc
- 1], "-v") == 0 ||
125 strcmp (argv
[argc
- 1], "--verbose") == 0)
127 else if (strcmp (argv
[argc
- 1], "-b") == 0 ||
128 strcmp (argv
[argc
- 1], "--break-on-error") == 0)
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",
140 for (i
= 0; i
< sizeof (nfkc
) / sizeof (nfkc
[0]); i
++)
143 printf ("NFKC entry %d\n", i
);
145 out
= stringprep_utf8_nfkc_normalize (nfkc
[i
].in
, strlen (nfkc
[i
].in
));
148 fail ("gsasl_utf8_nfkc_normalize() entry %d failed fatally\n", i
);
155 escapeprint (nfkc
[i
].in
, strlen (nfkc
[i
].in
));
156 hexprint (nfkc
[i
].in
, strlen (nfkc
[i
].in
));
158 binprint (nfkc
[i
].in
, strlen (nfkc
[i
].in
));
162 escapeprint (out
, strlen (out
));
163 hexprint (out
, strlen (out
));
165 binprint (out
, strlen (out
));
168 printf ("expected out:\n");
169 escapeprint (nfkc
[i
].out
, strlen (nfkc
[i
].out
));
170 hexprint (nfkc
[i
].out
, strlen (nfkc
[i
].out
));
172 binprint (nfkc
[i
].out
, strlen (nfkc
[i
].out
));
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
);
188 printf ("Libgsasl unicode self tests done with %d errors\n", error_count
);
190 return error_count
? 1 : 0;