1 /* Test of character set conversion with error handling.
2 Copyright (C) 2007 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
24 #include "striconveh.h"
34 #define SIZEOF(array) (sizeof (array) / sizeof (array[0]))
35 #define ASSERT(expr) if (!(expr)) abort ();
37 /* Magic number for detecting bounds violations. */
38 #define MAGIC 0x1983EFF1
41 new_offsets (size_t n)
43 size_t *offsets = (size_t *) malloc ((n + 1) * sizeof (size_t));
51 static enum iconv_ilseq_handler handlers[] =
52 { iconveh_error, iconveh_question_mark, iconveh_escape_sequence };
58 /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
59 ISO-8859-2, and UTF-8. */
60 iconv_t cd_88591_to_88592 = iconv_open ("ISO-8859-2", "ISO-8859-1");
61 iconv_t cd_88592_to_88591 = iconv_open ("ISO-8859-1", "ISO-8859-2");
62 iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
63 iconv_t cd_utf8_to_88591 = iconv_open ("ISO-8859-1", "UTF-8");
64 iconv_t cd_88592_to_utf8 = iconv_open ("UTF-8", "ISO-8859-2");
65 iconv_t cd_utf8_to_88592 = iconv_open ("ISO-8859-2", "UTF-8");
67 ASSERT (cd_88591_to_utf8 != (iconv_t)(-1));
68 ASSERT (cd_utf8_to_88591 != (iconv_t)(-1));
69 ASSERT (cd_88592_to_utf8 != (iconv_t)(-1));
70 ASSERT (cd_utf8_to_88592 != (iconv_t)(-1));
72 /* ------------------------ Test mem_cd_iconveh() ------------------------ */
74 /* Test conversion from ISO-8859-2 to ISO-8859-1 with no errors. */
75 for (h = 0; h < SIZEOF (handlers); h++)
77 enum iconv_ilseq_handler handler = handlers[h];
78 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
79 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
80 for (o = 0; o < 2; o++)
82 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
85 int retval = mem_cd_iconveh (input, strlen (input),
87 cd_88592_to_utf8, cd_utf8_to_88591,
92 ASSERT (length == strlen (expected));
93 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
96 for (i = 0; i < 37; i++)
97 ASSERT (offsets[i] == i);
98 ASSERT (offsets[37] == MAGIC);
105 /* Test conversion from ISO-8859-2 to ISO-8859-1 with EILSEQ. */
106 for (h = 0; h < SIZEOF (handlers); h++)
108 enum iconv_ilseq_handler handler = handlers[h];
109 static const char input[] = "Rafa\263 Maszkowski"; /* Rafał Maszkowski */
110 for (o = 0; o < 2; o++)
112 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
115 int retval = mem_cd_iconveh (input, strlen (input),
117 cd_88592_to_utf8, cd_utf8_to_88591,
124 ASSERT (retval == -1 && errno == EILSEQ);
125 ASSERT (result == NULL);
129 case iconveh_question_mark:
131 static const char expected[] = "Rafa? Maszkowski";
132 ASSERT (retval == 0);
133 ASSERT (length == strlen (expected));
134 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
137 for (i = 0; i < 16; i++)
138 ASSERT (offsets[i] == i);
139 ASSERT (offsets[16] == MAGIC);
145 case iconveh_escape_sequence:
147 static const char expected[] = "Rafa\\u0142 Maszkowski";
148 ASSERT (retval == 0);
149 ASSERT (length == strlen (expected));
150 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
153 for (i = 0; i < 16; i++)
154 ASSERT (offsets[i] == (i < 5 ? i :
156 ASSERT (offsets[16] == MAGIC);
166 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
167 for (h = 0; h < SIZEOF (handlers); h++)
169 enum iconv_ilseq_handler handler = handlers[h];
170 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
171 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
172 for (o = 0; o < 2; o++)
174 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
177 int retval = mem_cd_iconveh (input, strlen (input),
179 cd_88591_to_utf8, (iconv_t)(-1),
183 ASSERT (retval == 0);
184 ASSERT (length == strlen (expected));
185 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
188 for (i = 0; i < 37; i++)
189 ASSERT (offsets[i] == (i < 1 ? i :
193 ASSERT (offsets[37] == MAGIC);
200 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
201 for (h = 0; h < SIZEOF (handlers); h++)
203 enum iconv_ilseq_handler handler = handlers[h];
204 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
205 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
206 for (o = 0; o < 2; o++)
208 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
211 int retval = mem_cd_iconveh (input, strlen (input),
213 (iconv_t)(-1), cd_utf8_to_88591,
217 ASSERT (retval == 0);
218 ASSERT (length == strlen (expected));
219 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
222 for (i = 0; i < 41; i++)
223 ASSERT (offsets[i] == (i < 1 ? i :
224 i == 1 ? (size_t)(-1) :
226 i == 13 ? (size_t)(-1) :
228 i == 20 ? (size_t)(-1) :
231 ASSERT (offsets[41] == MAGIC);
238 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
239 for (h = 0; h < SIZEOF (handlers); h++)
241 enum iconv_ilseq_handler handler = handlers[h];
242 static const char input[] = "Rafa\305\202 Maszkowski"; /* Rafał Maszkowski */
243 for (o = 0; o < 2; o++)
245 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
248 int retval = mem_cd_iconveh (input, strlen (input),
250 (iconv_t)(-1), cd_utf8_to_88591,
257 ASSERT (retval == -1 && errno == EILSEQ);
258 ASSERT (result == NULL);
262 case iconveh_question_mark:
264 static const char expected[] = "Rafa? Maszkowski";
265 ASSERT (retval == 0);
266 ASSERT (length == strlen (expected));
267 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
270 for (i = 0; i < 17; i++)
271 ASSERT (offsets[i] == (i < 5 ? i :
272 i == 5 ? (size_t)(-1) :
274 ASSERT (offsets[17] == MAGIC);
280 case iconveh_escape_sequence:
282 static const char expected[] = "Rafa\\u0142 Maszkowski";
283 ASSERT (retval == 0);
284 ASSERT (length == strlen (expected));
285 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
288 for (i = 0; i < 17; i++)
289 ASSERT (offsets[i] == (i < 5 ? i :
290 i == 5 ? (size_t)(-1) :
292 ASSERT (offsets[17] == MAGIC);
302 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
303 for (h = 0; h < SIZEOF (handlers); h++)
305 enum iconv_ilseq_handler handler = handlers[h];
306 static const char input[] = "\342";
307 for (o = 0; o < 2; o++)
309 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
312 int retval = mem_cd_iconveh (input, strlen (input),
314 (iconv_t)(-1), cd_utf8_to_88591,
318 ASSERT (retval == 0);
319 ASSERT (length == 0);
322 ASSERT (offsets[0] == 0);
323 ASSERT (offsets[1] == MAGIC);
331 /* ------------------------ Test str_cd_iconveh() ------------------------ */
333 /* Test conversion from ISO-8859-2 to ISO-8859-1 with no errors. */
334 for (h = 0; h < SIZEOF (handlers); h++)
336 enum iconv_ilseq_handler handler = handlers[h];
337 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
338 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
339 char *result = str_cd_iconveh (input,
341 cd_88592_to_utf8, cd_utf8_to_88591,
343 ASSERT (result != NULL);
344 ASSERT (strcmp (result, expected) == 0);
348 /* Test conversion from ISO-8859-2 to ISO-8859-1 with EILSEQ. */
349 for (h = 0; h < SIZEOF (handlers); h++)
351 enum iconv_ilseq_handler handler = handlers[h];
352 static const char input[] = "Rafa\263 Maszkowski"; /* Rafał Maszkowski */
353 char *result = str_cd_iconveh (input,
355 cd_88592_to_utf8, cd_utf8_to_88591,
360 ASSERT (result == NULL && errno == EILSEQ);
362 case iconveh_question_mark:
364 static const char expected[] = "Rafa? Maszkowski";
365 ASSERT (result != NULL);
366 ASSERT (strcmp (result, expected) == 0);
370 case iconveh_escape_sequence:
372 static const char expected[] = "Rafa\\u0142 Maszkowski";
373 ASSERT (result != NULL);
374 ASSERT (strcmp (result, expected) == 0);
381 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
382 for (h = 0; h < SIZEOF (handlers); h++)
384 enum iconv_ilseq_handler handler = handlers[h];
385 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
386 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
387 char *result = str_cd_iconveh (input,
389 cd_88591_to_utf8, (iconv_t)(-1),
391 ASSERT (result != NULL);
392 ASSERT (strcmp (result, expected) == 0);
396 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
397 for (h = 0; h < SIZEOF (handlers); h++)
399 enum iconv_ilseq_handler handler = handlers[h];
400 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
401 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
402 char *result = str_cd_iconveh (input,
404 (iconv_t)(-1), cd_utf8_to_88591,
406 ASSERT (result != NULL);
407 ASSERT (strcmp (result, expected) == 0);
411 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
412 for (h = 0; h < SIZEOF (handlers); h++)
414 enum iconv_ilseq_handler handler = handlers[h];
415 static const char input[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
416 char *result = str_cd_iconveh (input,
418 (iconv_t)(-1), cd_utf8_to_88591,
423 ASSERT (result == NULL && errno == EILSEQ);
425 case iconveh_question_mark:
427 static const char expected[] = "Costs: 27 ?";
428 ASSERT (result != NULL);
429 ASSERT (strcmp (result, expected) == 0);
433 case iconveh_escape_sequence:
435 static const char expected[] = "Costs: 27 \\u20AC";
436 ASSERT (result != NULL);
437 ASSERT (strcmp (result, expected) == 0);
444 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
445 for (h = 0; h < SIZEOF (handlers); h++)
447 enum iconv_ilseq_handler handler = handlers[h];
448 static const char input[] = "\342";
449 char *result = str_cd_iconveh (input,
451 (iconv_t)(-1), cd_utf8_to_88591,
453 ASSERT (result != NULL);
454 ASSERT (strcmp (result, "") == 0);
458 if (cd_88591_to_88592 != (iconv_t)(-1))
459 iconv_close (cd_88591_to_88592);
460 if (cd_88592_to_88591 != (iconv_t)(-1))
461 iconv_close (cd_88592_to_88591);
462 iconv_close (cd_88591_to_utf8);
463 iconv_close (cd_utf8_to_88591);
464 iconv_close (cd_88592_to_utf8);
465 iconv_close (cd_utf8_to_88592);
467 /* ------------------------- Test mem_iconveh() ------------------------- */
469 /* Test conversion from ISO-8859-2 to ISO-8859-1 with no errors. */
470 for (h = 0; h < SIZEOF (handlers); h++)
472 enum iconv_ilseq_handler handler = handlers[h];
473 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
474 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
475 for (o = 0; o < 2; o++)
477 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
480 int retval = mem_iconveh (input, strlen (input),
481 "ISO-8859-2", "ISO-8859-1",
485 ASSERT (retval == 0);
486 ASSERT (length == strlen (expected));
487 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
490 for (i = 0; i < 37; i++)
491 ASSERT (offsets[i] == i);
492 ASSERT (offsets[37] == MAGIC);
499 /* Test conversion from ISO-8859-2 to ISO-8859-1 with EILSEQ. */
500 for (h = 0; h < SIZEOF (handlers); h++)
502 enum iconv_ilseq_handler handler = handlers[h];
503 static const char input[] = "Rafa\263 Maszkowski"; /* Rafał Maszkowski */
504 for (o = 0; o < 2; o++)
506 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
509 int retval = mem_iconveh (input, strlen (input),
510 "ISO-8859-2", "ISO-8859-1",
517 ASSERT (retval == -1 && errno == EILSEQ);
518 ASSERT (result == NULL);
522 case iconveh_question_mark:
524 static const char expected[] = "Rafa? Maszkowski";
525 ASSERT (retval == 0);
526 ASSERT (length == strlen (expected));
527 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
530 for (i = 0; i < 16; i++)
531 ASSERT (offsets[i] == i);
532 ASSERT (offsets[16] == MAGIC);
538 case iconveh_escape_sequence:
540 static const char expected[] = "Rafa\\u0142 Maszkowski";
541 ASSERT (retval == 0);
542 ASSERT (length == strlen (expected));
543 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
546 for (i = 0; i < 16; i++)
547 ASSERT (offsets[i] == (i < 5 ? i :
549 ASSERT (offsets[16] == MAGIC);
559 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
560 for (h = 0; h < SIZEOF (handlers); h++)
562 enum iconv_ilseq_handler handler = handlers[h];
563 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
564 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
565 for (o = 0; o < 2; o++)
567 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
570 int retval = mem_iconveh (input, strlen (input),
571 "ISO-8859-1", "UTF-8",
575 ASSERT (retval == 0);
576 ASSERT (length == strlen (expected));
577 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
580 for (i = 0; i < 37; i++)
581 ASSERT (offsets[i] == (i < 1 ? i :
585 ASSERT (offsets[37] == MAGIC);
592 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
593 for (h = 0; h < SIZEOF (handlers); h++)
595 enum iconv_ilseq_handler handler = handlers[h];
596 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
597 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
598 for (o = 0; o < 2; o++)
600 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
603 int retval = mem_iconveh (input, strlen (input),
604 "UTF-8", "ISO-8859-1",
608 ASSERT (retval == 0);
609 ASSERT (length == strlen (expected));
610 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
613 for (i = 0; i < 41; i++)
614 ASSERT (offsets[i] == (i < 1 ? i :
615 i == 1 ? (size_t)(-1) :
617 i == 13 ? (size_t)(-1) :
619 i == 20 ? (size_t)(-1) :
622 ASSERT (offsets[41] == MAGIC);
629 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
630 for (h = 0; h < SIZEOF (handlers); h++)
632 enum iconv_ilseq_handler handler = handlers[h];
633 static const char input[] = "Rafa\305\202 Maszkowski"; /* Rafał Maszkowski */
634 for (o = 0; o < 2; o++)
636 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
639 int retval = mem_iconveh (input, strlen (input),
640 "UTF-8", "ISO-8859-1",
647 ASSERT (retval == -1 && errno == EILSEQ);
648 ASSERT (result == NULL);
652 case iconveh_question_mark:
654 static const char expected[] = "Rafa? Maszkowski";
655 ASSERT (retval == 0);
656 ASSERT (length == strlen (expected));
657 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
660 for (i = 0; i < 17; i++)
661 ASSERT (offsets[i] == (i < 5 ? i :
662 i == 5 ? (size_t)(-1) :
664 ASSERT (offsets[17] == MAGIC);
670 case iconveh_escape_sequence:
672 static const char expected[] = "Rafa\\u0142 Maszkowski";
673 ASSERT (retval == 0);
674 ASSERT (length == strlen (expected));
675 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
678 for (i = 0; i < 17; i++)
679 ASSERT (offsets[i] == (i < 5 ? i :
680 i == 5 ? (size_t)(-1) :
682 ASSERT (offsets[17] == MAGIC);
692 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
693 for (h = 0; h < SIZEOF (handlers); h++)
695 enum iconv_ilseq_handler handler = handlers[h];
696 static const char input[] = "\342";
697 for (o = 0; o < 2; o++)
699 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
702 int retval = mem_iconveh (input, strlen (input),
703 "UTF-8", "ISO-8859-1",
707 ASSERT (retval == 0);
708 ASSERT (length == 0);
711 ASSERT (offsets[0] == 0);
712 ASSERT (offsets[1] == MAGIC);
720 /* ------------------------- Test str_iconveh() ------------------------- */
722 /* Test conversion from ISO-8859-2 to ISO-8859-1 with no errors. */
723 for (h = 0; h < SIZEOF (handlers); h++)
725 enum iconv_ilseq_handler handler = handlers[h];
726 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
727 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
728 char *result = str_iconveh (input, "ISO-8859-2", "ISO-8859-1", handler);
729 ASSERT (result != NULL);
730 ASSERT (strcmp (result, expected) == 0);
734 /* Test conversion from ISO-8859-2 to ISO-8859-1 with EILSEQ. */
735 for (h = 0; h < SIZEOF (handlers); h++)
737 enum iconv_ilseq_handler handler = handlers[h];
738 static const char input[] = "Rafa\263 Maszkowski"; /* Rafał Maszkowski */
739 char *result = str_iconveh (input, "ISO-8859-2", "ISO-8859-1", handler);
743 ASSERT (result == NULL && errno == EILSEQ);
745 case iconveh_question_mark:
747 static const char expected[] = "Rafa? Maszkowski";
748 ASSERT (result != NULL);
749 ASSERT (strcmp (result, expected) == 0);
753 case iconveh_escape_sequence:
755 static const char expected[] = "Rafa\\u0142 Maszkowski";
756 ASSERT (result != NULL);
757 ASSERT (strcmp (result, expected) == 0);
764 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
765 for (h = 0; h < SIZEOF (handlers); h++)
767 enum iconv_ilseq_handler handler = handlers[h];
768 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
769 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
770 char *result = str_iconveh (input, "ISO-8859-1", "UTF-8", handler);
771 ASSERT (result != NULL);
772 ASSERT (strcmp (result, expected) == 0);
776 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
777 for (h = 0; h < SIZEOF (handlers); h++)
779 enum iconv_ilseq_handler handler = handlers[h];
780 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
781 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
782 char *result = str_iconveh (input, "UTF-8", "ISO-8859-1", handler);
783 ASSERT (result != NULL);
784 ASSERT (strcmp (result, expected) == 0);
788 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
789 for (h = 0; h < SIZEOF (handlers); h++)
791 enum iconv_ilseq_handler handler = handlers[h];
792 static const char input[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
793 char *result = str_iconveh (input, "UTF-8", "ISO-8859-1", handler);
797 ASSERT (result == NULL && errno == EILSEQ);
799 case iconveh_question_mark:
801 static const char expected[] = "Costs: 27 ?";
802 ASSERT (result != NULL);
803 ASSERT (strcmp (result, expected) == 0);
807 case iconveh_escape_sequence:
809 static const char expected[] = "Costs: 27 \\u20AC";
810 ASSERT (result != NULL);
811 ASSERT (strcmp (result, expected) == 0);
818 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
819 for (h = 0; h < SIZEOF (handlers); h++)
821 enum iconv_ilseq_handler handler = handlers[h];
822 static const char input[] = "\342";
823 char *result = str_iconveh (input, "UTF-8", "ISO-8859-1", handler);
824 ASSERT (result != NULL);
825 ASSERT (strcmp (result, "") == 0);