1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
25 #undef G_DISABLE_ASSERT
35 test_iconv_state (void)
37 const gchar
*in
= "\xf4\xe5\xf8\xe5\xed";
38 const gchar
*expected
= "\xd7\xa4\xd7\x95\xd7\xa8\xd7\x95\xd7\x9d";
41 gsize bytes_written
= 0;
44 out
= g_convert (in
, -1, "UTF-8", "CP1255",
45 &bytes_read
, &bytes_written
, &error
);
47 if (error
&& error
->code
== G_CONVERT_ERROR_NO_CONVERSION
)
48 return; /* silently skip if CP1255 is not supported, see bug 467707 */
50 g_assert_no_error (error
);
51 g_assert_cmpint (bytes_read
, ==, 5);
52 g_assert_cmpint (bytes_written
, ==, 10);
53 g_assert_cmpstr (out
, ==, expected
);
57 /* Some tests involving "vulgar fraction one half" (U+00BD). This is
58 * represented in UTF-8 as \xC2\xBD, in ISO-8859-1 as \xBD, and is not
59 * represented in ISO-8859-15. */
63 const gchar
*in_utf8
= "\xc2\xbd";
66 gsize bytes_written
= 0;
69 out
= g_convert (in_utf8
, -1,
70 "ISO-8859-1", "UTF-8",
71 &bytes_read
, &bytes_written
,
74 g_assert_no_error (error
);
75 g_assert_cmpint (bytes_read
, ==, 2);
76 g_assert_cmpint (bytes_written
, ==, 1);
77 g_assert_cmpstr (out
, ==, "\xbd");
80 out
= g_convert (in_utf8
, -1,
81 "ISO-8859-15", "UTF-8",
82 &bytes_read
, &bytes_written
,
85 g_assert_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
);
86 g_assert_cmpint (bytes_read
, ==, 0);
87 g_assert_cmpint (bytes_written
, ==, 0);
88 g_assert_cmpstr (out
, ==, NULL
);
89 g_clear_error (&error
);
92 out
= g_convert_with_fallback (in_utf8
, -1,
93 "ISO8859-15", "UTF-8",
95 &bytes_read
, &bytes_written
,
98 g_assert_no_error (error
);
99 g_assert_cmpint (bytes_read
, ==, 2);
100 g_assert_cmpint (bytes_written
, ==, 1);
101 g_assert_cmpstr (out
, ==, "a");
106 test_byte_order (void)
108 gchar in_be
[4] = { 0xfe, 0xff, 0x03, 0x93}; /* capital gamma */
109 gchar in_le
[4] = { 0xff, 0xfe, 0x93, 0x03};
110 const gchar
*expected
= "\xce\x93";
112 gsize bytes_read
= 0;
113 gsize bytes_written
= 0;
114 GError
*error
= NULL
;
116 out
= g_convert (in_be
, sizeof (in_be
),
118 &bytes_read
, &bytes_written
,
121 g_assert_no_error (error
);
122 g_assert_cmpint (bytes_read
, ==, 4);
123 g_assert_cmpint (bytes_written
, ==, 2);
124 g_assert_cmpstr (out
, ==, expected
);
127 out
= g_convert (in_le
, sizeof (in_le
),
129 &bytes_read
, &bytes_written
,
132 g_assert_no_error (error
);
133 g_assert_cmpint (bytes_read
, ==, 4);
134 g_assert_cmpint (bytes_written
, ==, 2);
135 g_assert_cmpstr (out
, ==, expected
);
140 check_utf8_to_ucs4 (const char *utf8
,
142 const gunichar
*ucs4
,
146 gunichar
*result
, *result2
, *result3
;
147 glong items_read
, items_read2
;
148 glong items_written
, items_written2
;
149 GError
*error
, *error2
, *error3
;
154 /* check the fast conversion */
155 result
= g_utf8_to_ucs4_fast (utf8
, utf8_len
, &items_written
);
157 g_assert_cmpint (items_written
, ==, ucs4_len
);
159 for (i
= 0; i
<= items_written
; i
++)
160 g_assert (result
[i
] == ucs4
[i
]);
166 result
= g_utf8_to_ucs4 (utf8
, utf8_len
, &items_read
, &items_written
, &error
);
168 if (utf8_len
== strlen (utf8
))
170 /* check that len == -1 yields identical results */
172 result2
= g_utf8_to_ucs4 (utf8
, -1, &items_read2
, &items_written2
, &error2
);
173 g_assert (error
|| items_read2
== items_read
);
174 g_assert (error
|| items_written2
== items_written
);
175 g_assert_cmpint (!!result
, ==, !!result2
);
176 g_assert_cmpint (!!error
, ==, !!error2
);
178 for (i
= 0; i
<= items_written
; i
++)
179 g_assert (result
[i
] == result2
[i
]);
183 g_error_free (error2
);
187 result3
= g_utf8_to_ucs4 (utf8
, utf8_len
, NULL
, NULL
, &error3
);
189 if (error3
&& error3
->code
== G_CONVERT_ERROR_PARTIAL_INPUT
)
191 g_assert_no_error (error
);
192 g_assert_cmpint (items_read
, ==, error_pos
);
193 g_assert_cmpint (items_written
, ==, ucs4_len
);
195 for (i
= 0; i
<= items_written
; i
++)
196 g_assert (result
[i
] == ucs4
[i
]);
197 g_error_free (error3
);
201 g_assert (error
!= NULL
);
202 g_assert (result
== NULL
);
203 g_assert_cmpint (items_read
, ==, error_pos
);
204 g_error_free (error
);
206 g_assert (error3
!= NULL
);
207 g_assert (result3
== NULL
);
208 g_error_free (error3
);
212 g_assert_no_error (error
);
213 g_assert_cmpint (items_read
, ==, utf8_len
);
214 g_assert_cmpint (items_written
, ==, ucs4_len
);
216 for (i
= 0; i
<= items_written
; i
++)
217 g_assert (result
[i
] == ucs4
[i
]);
219 g_assert_no_error (error3
);
221 for (i
= 0; i
<= ucs4_len
; i
++)
222 g_assert (result3
[i
] == ucs4
[i
]);
230 check_ucs4_to_utf8 (const gunichar
*ucs4
,
236 gchar
*result
, *result2
, *result3
;
237 glong items_read
, items_read2
;
238 glong items_written
, items_written2
;
239 GError
*error
, *error2
, *error3
;
242 result
= g_ucs4_to_utf8 (ucs4
, ucs4_len
, &items_read
, &items_written
, &error
);
244 if (ucs4
[ucs4_len
] == 0)
246 /* check that len == -1 yields identical results */
248 result2
= g_ucs4_to_utf8 (ucs4
, -1, &items_read2
, &items_written2
, &error2
);
250 g_assert (error
|| items_read2
== items_read
);
251 g_assert (error
|| items_written2
== items_written
);
252 g_assert_cmpint (!!result
, ==, !!result2
);
253 g_assert_cmpint (!!error
, ==, !!error2
);
255 g_assert_cmpstr (result
, ==, result2
);
259 g_error_free (error2
);
263 result3
= g_ucs4_to_utf8 (ucs4
, ucs4_len
, NULL
, NULL
, &error3
);
267 g_assert (error
!= NULL
);
268 g_assert (result
== NULL
);
269 g_assert_cmpint (items_read
, ==, error_pos
);
270 g_error_free (error
);
272 g_assert (error3
!= NULL
);
273 g_assert (result3
== NULL
);
274 g_error_free (error3
);
278 g_assert_no_error (error
);
279 g_assert_cmpint (items_read
, ==, ucs4_len
);
280 g_assert_cmpint (items_written
, ==, utf8_len
);
282 g_assert_cmpstr (result
, ==, utf8
);
284 g_assert_no_error (error3
);
286 g_assert_cmpstr (result3
, ==, utf8
);
294 check_utf8_to_utf16 (const char *utf8
,
296 const gunichar2
*utf16
,
300 gunichar2
*result
, *result2
, *result3
;
301 glong items_read
, items_read2
;
302 glong items_written
, items_written2
;
303 GError
*error
, *error2
, *error3
;
307 result
= g_utf8_to_utf16 (utf8
, utf8_len
, &items_read
, &items_written
, &error
);
309 if (utf8_len
== strlen (utf8
))
311 /* check that len == -1 yields identical results */
313 result2
= g_utf8_to_utf16 (utf8
, -1, &items_read2
, &items_written2
, &error2
);
314 g_assert (error
|| items_read2
== items_read
);
315 g_assert (error
|| items_written2
== items_written
);
316 g_assert_cmpint (!!result
, ==, !!result2
);
317 g_assert_cmpint (!!error
, ==, !!error2
);
319 for (i
= 0; i
<= items_written
; i
++)
320 g_assert (result
[i
] == result2
[i
]);
324 g_error_free (error2
);
328 result3
= g_utf8_to_utf16 (utf8
, utf8_len
, NULL
, NULL
, &error3
);
330 if (error3
&& error3
->code
== G_CONVERT_ERROR_PARTIAL_INPUT
)
332 g_assert_no_error (error
);
333 g_assert_cmpint (items_read
, ==, error_pos
);
334 g_assert_cmpint (items_written
, ==, utf16_len
);
336 for (i
= 0; i
<= items_written
; i
++)
337 g_assert (result
[i
] == utf16
[i
]);
338 g_error_free (error3
);
342 g_assert (error
!= NULL
);
343 g_assert (result
== NULL
);
344 g_assert_cmpint (items_read
, ==, error_pos
);
345 g_error_free (error
);
347 g_assert (error3
!= NULL
);
348 g_assert (result3
== NULL
);
349 g_error_free (error3
);
353 g_assert_no_error (error
);
354 g_assert_cmpint (items_read
, ==, utf8_len
);
355 g_assert_cmpint (items_written
, ==, utf16_len
);
357 for (i
= 0; i
<= items_written
; i
++)
358 g_assert (result
[i
] == utf16
[i
]);
360 g_assert_no_error (error3
);
362 for (i
= 0; i
<= utf16_len
; i
++)
363 g_assert (result3
[i
] == utf16
[i
]);
371 check_utf16_to_utf8 (const gunichar2
*utf16
,
377 gchar
*result
, *result2
, *result3
;
378 glong items_read
, items_read2
;
379 glong items_written
, items_written2
;
380 GError
*error
, *error2
, *error3
;
383 result
= g_utf16_to_utf8 (utf16
, utf16_len
, &items_read
, &items_written
, &error
);
384 if (utf16
[utf16_len
] == 0)
386 /* check that len == -1 yields identical results */
388 result2
= g_utf16_to_utf8 (utf16
, -1, &items_read2
, &items_written2
, &error2
);
390 g_assert (error
|| items_read2
== items_read
);
391 g_assert (error
|| items_written2
== items_written
);
392 g_assert_cmpint (!!result
, ==, !!result2
);
393 g_assert_cmpint (!!error
, ==, !!error2
);
395 g_assert_cmpstr (result
, ==, result2
);
399 g_error_free (error2
);
403 result3
= g_utf16_to_utf8 (utf16
, utf16_len
, NULL
, NULL
, &error3
);
405 if (error3
&& error3
->code
== G_CONVERT_ERROR_PARTIAL_INPUT
)
407 g_assert_no_error (error
);
408 g_assert_cmpint (items_read
, ==, error_pos
);
409 g_assert_cmpint (items_read
+ 1, ==, utf16_len
);
410 g_assert_cmpint (items_written
, ==, utf8_len
);
412 g_assert_cmpstr (result
, ==, utf8
);
413 g_error_free (error3
);
417 g_assert (error
!= NULL
);
418 g_assert (result
== NULL
);
419 g_assert_cmpint (items_read
, ==, error_pos
);
420 g_error_free (error
);
422 g_assert (error3
!= NULL
);
423 g_assert (result3
== NULL
);
424 g_error_free (error3
);
428 g_assert_no_error (error
);
429 g_assert_cmpint (items_read
, ==, utf16_len
);
430 g_assert_cmpint (items_written
, ==, utf8_len
);
432 g_assert_cmpstr (result
, ==, utf8
);
434 g_assert_no_error (error3
);
436 g_assert_cmpstr (result3
, ==, utf8
);
444 check_ucs4_to_utf16 (const gunichar
*ucs4
,
446 const gunichar2
*utf16
,
450 gunichar2
*result
, *result2
, *result3
;
451 glong items_read
, items_read2
;
452 glong items_written
, items_written2
;
453 GError
*error
, *error2
, *error3
;
457 result
= g_ucs4_to_utf16 (ucs4
, ucs4_len
, &items_read
, &items_written
, &error
);
459 if (ucs4
[ucs4_len
] == 0)
461 /* check that len == -1 yields identical results */
463 result2
= g_ucs4_to_utf16 (ucs4
, -1, &items_read2
, &items_written2
, &error2
);
465 g_assert (error
|| items_read2
== items_read
);
466 g_assert (error
|| items_written2
== items_written
);
467 g_assert_cmpint (!!result
, ==, !!result2
);
468 g_assert_cmpint (!!error
, ==, !!error2
);
470 for (i
= 0; i
<= utf16_len
; i
++)
471 g_assert (result
[i
] == result2
[i
]);
475 g_error_free (error2
);
479 result3
= g_ucs4_to_utf16 (ucs4
, -1, NULL
, NULL
, &error3
);
483 g_assert (error
!= NULL
);
484 g_assert (result
== NULL
);
485 g_assert_cmpint (items_read
, ==, error_pos
);
486 g_error_free (error
);
488 g_assert (error3
!= NULL
);
489 g_assert (result3
== NULL
);
490 g_error_free (error3
);
494 g_assert_no_error (error
);
495 g_assert_cmpint (items_read
, ==, ucs4_len
);
496 g_assert_cmpint (items_written
, ==, utf16_len
);
498 for (i
= 0; i
<= utf16_len
; i
++)
499 g_assert (result
[i
] == utf16
[i
]);
501 g_assert_no_error (error3
);
503 for (i
= 0; i
<= utf16_len
; i
++)
504 g_assert (result3
[i
] == utf16
[i
]);
512 check_utf16_to_ucs4 (const gunichar2
*utf16
,
514 const gunichar
*ucs4
,
518 gunichar
*result
, *result2
, *result3
;
519 glong items_read
, items_read2
;
520 glong items_written
, items_written2
;
521 GError
*error
, *error2
, *error3
;
525 result
= g_utf16_to_ucs4 (utf16
, utf16_len
, &items_read
, &items_written
, &error
);
526 if (utf16
[utf16_len
] == 0)
528 /* check that len == -1 yields identical results */
530 result2
= g_utf16_to_ucs4 (utf16
, -1, &items_read2
, &items_written2
, &error2
);
531 g_assert (error
|| items_read2
== items_read
);
532 g_assert (error
|| items_written2
== items_written
);
533 g_assert_cmpint (!!result
, ==, !!result2
);
534 g_assert_cmpint (!!error
, ==, !!error2
);
536 for (i
= 0; i
<= items_written
; i
++)
537 g_assert (result
[i
] == result2
[i
]);
541 g_error_free (error2
);
545 result3
= g_utf16_to_ucs4 (utf16
, utf16_len
, NULL
, NULL
, &error3
);
547 if (error3
&& error3
->code
== G_CONVERT_ERROR_PARTIAL_INPUT
)
549 g_assert_no_error (error
);
550 g_assert_cmpint (items_read
, ==, error_pos
);
551 g_assert_cmpint (items_read
+ 1, ==, utf16_len
);
552 g_assert_cmpint (items_written
, ==, ucs4_len
);
554 for (i
= 0; i
<= items_written
; i
++)
555 g_assert (result
[i
] == ucs4
[i
]);
556 g_error_free (error3
);
560 g_assert (error
!= NULL
);
561 g_assert (result
== NULL
);
562 g_assert_cmpint (items_read
, ==, error_pos
);
563 g_error_free (error
);
565 g_assert (error3
!= NULL
);
566 g_assert (result3
== NULL
);
567 g_error_free (error3
);
571 g_assert_no_error (error
);
572 g_assert_cmpint (items_read
, ==, utf16_len
);
573 g_assert_cmpint (items_written
, ==, ucs4_len
);
575 for (i
= 0; i
<= ucs4_len
; i
++)
576 g_assert (result
[i
] == ucs4
[i
]);
578 g_assert_no_error (error3
);
580 for (i
= 0; i
<= ucs4_len
; i
++)
581 g_assert (result3
[i
] == ucs4
[i
]);
589 test_unicode_conversions (void)
593 gunichar2 utf16
[100];
596 ucs4
[0] = 0x61; ucs4
[1] = 0x62; ucs4
[2] = 0x63; ucs4
[3] = 0;
597 utf16
[0] = 0x61; utf16
[1] = 0x62; utf16
[2] = 0x63; utf16
[3] = 0;
599 check_utf8_to_ucs4 (utf8
, 3, ucs4
, 3, 0);
600 check_ucs4_to_utf8 (ucs4
, 3, utf8
, 3, 0);
601 check_utf8_to_utf16 (utf8
, 3, utf16
, 3, 0);
602 check_utf16_to_utf8 (utf16
, 3, utf8
, 3, 0);
603 check_ucs4_to_utf16 (ucs4
, 3, utf16
, 3, 0);
604 check_utf16_to_ucs4 (utf16
, 3, ucs4
, 3, 0);
606 utf8
= "\316\261\316\262\316\263";
607 ucs4
[0] = 0x03b1; ucs4
[1] = 0x03b2; ucs4
[2] = 0x03b3; ucs4
[3] = 0;
608 utf16
[0] = 0x03b1; utf16
[1] = 0x03b2; utf16
[2] = 0x03b3; utf16
[3] = 0;
610 check_utf8_to_ucs4 (utf8
, 6, ucs4
, 3, 0);
611 check_ucs4_to_utf8 (ucs4
, 3, utf8
, 6, 0);
612 check_utf8_to_utf16 (utf8
, 6, utf16
, 3, 0);
613 check_utf16_to_utf8 (utf16
, 3, utf8
, 6, 0);
614 check_ucs4_to_utf16 (ucs4
, 3, utf16
, 3, 0);
615 check_utf16_to_ucs4 (utf16
, 3, ucs4
, 3, 0);
617 /* partial utf8 character */
619 ucs4
[0] = 0x61; ucs4
[1] = 0x62; ucs4
[2] = 0x63; ucs4
[3] = 0;
620 utf16
[0] = 0x61; utf16
[1] = 0x62; utf16
[2] = 0x63; utf16
[3] = 0;
622 check_utf8_to_ucs4 (utf8
, 4, ucs4
, 3, 3);
623 check_utf8_to_utf16 (utf8
, 4, utf16
, 3, 3);
626 utf8
= "abc\316\316";
630 check_utf8_to_ucs4 (utf8
, 5, ucs4
, 0, 3);
631 check_utf8_to_utf16 (utf8
, 5, utf16
, 0, 3);
633 /* partial utf16 character */
635 ucs4
[0] = 0x61; ucs4
[1] = 0x62; ucs4
[2] = 0;
636 utf16
[0] = 0x61; utf16
[1] = 0x62; utf16
[2] = 0xd801; utf16
[3] = 0;
638 check_utf16_to_utf8 (utf16
, 3, utf8
, 2, 2);
639 check_utf16_to_ucs4 (utf16
, 3, ucs4
, 2, 2);
644 utf16
[0] = 0x61; utf16
[1] = 0x62; utf16
[2] = 0xdc01; utf16
[3] = 0;
646 check_utf16_to_utf8 (utf16
, 3, utf8
, 0, 2);
647 check_utf16_to_ucs4 (utf16
, 3, ucs4
, 0, 2);
651 ucs4
[0] = 0x61; ucs4
[1] = 0x62; ucs4
[2] = 0x80000000; ucs4
[3] = 0;
654 check_ucs4_to_utf8 (ucs4
, 3, utf8
, 0, 2);
655 check_ucs4_to_utf16 (ucs4
, 3, utf16
, 0, 2);
659 test_filename_utf8 (void)
661 const gchar
*filename
= "/my/path/to/foo";
667 utf8
= g_filename_to_utf8 (filename
, -1, NULL
, NULL
, &error
);
668 g_assert_no_error (error
);
669 back
= g_filename_from_utf8 (utf8
, -1, NULL
, NULL
, &error
);
670 g_assert_no_error (error
);
671 g_assert_cmpstr (back
, ==, filename
);
678 test_filename_display (void)
680 const gchar
*filename
= "/my/path/to/foo";
683 display
= g_filename_display_basename (filename
);
684 g_assert_cmpstr (display
, ==, "foo");
689 /* g_convert() should accept and produce text buffers with embedded
690 * nul bytes/characters.
693 test_convert_embedded_nul (void)
696 gsize bytes_read
, bytes_written
;
697 GError
*error
= NULL
;
699 res
= g_convert ("ab\0\xf6", 4, "UTF-8", "ISO-8859-1",
700 &bytes_read
, &bytes_written
, &error
);
701 g_assert_no_error (error
);
702 g_assert_cmpuint (bytes_read
, ==, 4);
703 g_assert_cmpmem (res
, bytes_written
, "ab\0\xc3\xb6", 5);
708 test_locale_to_utf8_embedded_nul (void)
710 g_test_trap_subprocess ("/conversion/locale-to-utf8/embedded-nul/subprocess/utf8", 0, 0);
711 g_test_trap_assert_passed ();
712 g_test_trap_subprocess ("/conversion/locale-to-utf8/embedded-nul/subprocess/iconv", 0, 0);
713 g_test_trap_assert_passed ();
716 /* Test that embedded nul characters in UTF-8 input to g_locale_to_utf8()
717 * result in an error.
720 test_locale_to_utf8_embedded_nul_utf8 (void)
724 GError
*error
= NULL
;
726 setlocale (LC_ALL
, "");
727 g_setenv ("CHARSET", "UTF-8", TRUE
);
728 g_assert_true (g_get_charset (NULL
));
730 res
= g_locale_to_utf8 ("ab\0c", 4, &bytes_read
, NULL
, &error
);
733 g_assert_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
);
734 g_assert_cmpuint (bytes_read
, ==, 2);
735 g_error_free (error
);
738 /* Test that embedded nul characters in output of g_locale_to_utf8(),
739 * when converted from non-UTF8 input, result in an error.
742 test_locale_to_utf8_embedded_nul_iconv (void)
745 GError
*error
= NULL
;
747 setlocale (LC_ALL
, "C");
748 g_setenv ("CHARSET", "US-ASCII", TRUE
);
749 g_assert_false (g_get_charset (NULL
));
751 res
= g_locale_to_utf8 ("ab\0c", 4, NULL
, NULL
, &error
);
754 g_assert_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_EMBEDDED_NUL
);
755 g_error_free (error
);
759 test_locale_from_utf8_embedded_nul (void)
761 g_test_trap_subprocess ("/conversion/locale-from-utf8/embedded-nul/subprocess/utf8", 0, 0);
762 g_test_trap_assert_passed ();
763 g_test_trap_subprocess ("/conversion/locale-from-utf8/embedded-nul/subprocess/iconv", 0, 0);
764 g_test_trap_assert_passed ();
767 /* Test that embedded nul characters in input to g_locale_from_utf8(),
768 * when converting (copying) to UTF-8 output, result in an error.
771 test_locale_from_utf8_embedded_nul_utf8 (void)
775 GError
*error
= NULL
;
777 setlocale (LC_ALL
, "");
778 g_setenv ("CHARSET", "UTF-8", TRUE
);
779 g_assert_true (g_get_charset (NULL
));
781 res
= g_locale_from_utf8 ("ab\0c", 4, &bytes_read
, NULL
, &error
);
784 g_assert_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
);
785 g_assert_cmpuint (bytes_read
, ==, 2);
786 g_error_free (error
);
789 /* Test that embedded nul characters in input to g_locale_from_utf8(),
790 * when converting to non-UTF-8 output, result in an error.
793 test_locale_from_utf8_embedded_nul_iconv (void)
797 GError
*error
= NULL
;
799 setlocale (LC_ALL
, "C");
800 g_setenv ("CHARSET", "US-ASCII", TRUE
);
801 g_assert_false (g_get_charset (NULL
));
803 res
= g_locale_from_utf8 ("ab\0c", 4, &bytes_read
, NULL
, &error
);
806 g_assert_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
);
807 g_assert_cmpuint (bytes_read
, ==, 2);
808 g_error_free (error
);
812 test_filename_to_utf8_embedded_nul (void)
814 g_test_trap_subprocess ("/conversion/filename-to-utf8/embedded-nul/subprocess/utf8", 0, 0);
815 g_test_trap_assert_passed ();
816 g_test_trap_subprocess ("/conversion/filename-to-utf8/embedded-nul/subprocess/iconv", 0, 0);
817 g_test_trap_assert_passed ();
820 /* Test that embedded nul characters in UTF-8 input to g_filename_to_utf8()
821 * result in an error.
824 test_filename_to_utf8_embedded_nul_utf8 (void)
828 GError
*error
= NULL
;
830 g_setenv ("G_FILENAME_ENCODING", "UTF-8", TRUE
);
831 g_assert_true (g_get_filename_charsets (NULL
));
833 res
= g_filename_to_utf8 ("ab\0c", 4, &bytes_read
, NULL
, &error
);
836 g_assert_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
);
837 g_assert_cmpuint (bytes_read
, ==, 2);
838 g_error_free (error
);
841 /* Test that embedded nul characters in non-UTF-8 input of g_filename_to_utf8()
842 * result in an error.
845 test_filename_to_utf8_embedded_nul_iconv (void)
849 GError
*error
= NULL
;
851 g_setenv ("G_FILENAME_ENCODING", "US-ASCII", TRUE
);
852 g_assert_false (g_get_filename_charsets (NULL
));
854 res
= g_filename_to_utf8 ("ab\0c", 4, &bytes_read
, NULL
, &error
);
857 g_assert_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
);
858 g_assert_cmpuint (bytes_read
, ==, 2);
859 g_error_free (error
);
863 test_filename_from_utf8_embedded_nul (void)
865 g_test_trap_subprocess ("/conversion/filename-from-utf8/embedded-nul/subprocess/utf8", 0, 0);
866 g_test_trap_assert_passed ();
867 g_test_trap_subprocess ("/conversion/filename-from-utf8/embedded-nul/subprocess/iconv", 0, 0);
868 g_test_trap_assert_passed ();
871 /* Test that embedded nul characters in input to g_filename_from_utf8(),
872 * when converting (copying) to UTF-8 output, result in an error.
875 test_filename_from_utf8_embedded_nul_utf8 (void)
879 GError
*error
= NULL
;
881 g_setenv ("G_FILENAME_ENCODING", "UTF-8", TRUE
);
882 g_assert_true (g_get_filename_charsets (NULL
));
884 res
= g_filename_from_utf8 ("ab\0c", 4, &bytes_read
, NULL
, &error
);
887 g_assert_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
);
888 g_assert_cmpuint (bytes_read
, ==, 2);
889 g_error_free (error
);
892 /* Test that embedded nul characters in input to g_filename_from_utf8(),
893 * when converting to non-UTF-8 output, result in an error.
896 test_filename_from_utf8_embedded_nul_iconv (void)
900 GError
*error
= NULL
;
902 g_setenv ("G_FILENAME_ENCODING", "US-ASCII", TRUE
);
903 g_assert_false (g_get_filename_charsets (NULL
));
905 res
= g_filename_from_utf8 ("ab\0c", 4, &bytes_read
, NULL
, &error
);
908 g_assert_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
);
909 g_assert_cmpuint (bytes_read
, ==, 2);
910 g_error_free (error
);
916 const gchar
*in
= "";
917 gchar
*out G_GNUC_UNUSED
;
918 gsize bytes_read
= 0;
919 gsize bytes_written
= 0;
920 GError
*error
= NULL
;
922 out
= g_convert (in
, -1, "XXX", "UVZ",
923 &bytes_read
, &bytes_written
, &error
);
925 /* error code is unreliable, since we mishandle errno there */
926 g_assert (error
&& error
->domain
== G_CONVERT_ERROR
);
927 g_error_free (error
);
931 main (int argc
, char *argv
[])
933 g_test_init (&argc
, &argv
, NULL
);
935 g_test_add_func ("/conversion/no-conv", test_no_conv
);
936 g_test_add_func ("/conversion/iconv-state", test_iconv_state
);
937 g_test_add_func ("/conversion/illegal-sequence", test_one_half
);
938 g_test_add_func ("/conversion/byte-order", test_byte_order
);
939 g_test_add_func ("/conversion/unicode", test_unicode_conversions
);
940 g_test_add_func ("/conversion/filename-utf8", test_filename_utf8
);
941 g_test_add_func ("/conversion/filename-display", test_filename_display
);
942 g_test_add_func ("/conversion/convert-embedded-nul", test_convert_embedded_nul
);
943 g_test_add_func ("/conversion/locale-to-utf8/embedded-nul", test_locale_to_utf8_embedded_nul
);
944 g_test_add_func ("/conversion/locale-to-utf8/embedded-nul/subprocess/utf8", test_locale_to_utf8_embedded_nul_utf8
);
945 g_test_add_func ("/conversion/locale-to-utf8/embedded-nul/subprocess/iconv", test_locale_to_utf8_embedded_nul_iconv
);
946 g_test_add_func ("/conversion/locale-from-utf8/embedded-nul", test_locale_from_utf8_embedded_nul
);
947 g_test_add_func ("/conversion/locale-from-utf8/embedded-nul/subprocess/utf8", test_locale_from_utf8_embedded_nul_utf8
);
948 g_test_add_func ("/conversion/locale-from-utf8/embedded-nul/subprocess/iconv", test_locale_from_utf8_embedded_nul_iconv
);
949 g_test_add_func ("/conversion/filename-to-utf8/embedded-nul", test_filename_to_utf8_embedded_nul
);
950 g_test_add_func ("/conversion/filename-to-utf8/embedded-nul/subprocess/utf8", test_filename_to_utf8_embedded_nul_utf8
);
951 g_test_add_func ("/conversion/filename-to-utf8/embedded-nul/subprocess/iconv", test_filename_to_utf8_embedded_nul_iconv
);
952 g_test_add_func ("/conversion/filename-from-utf8/embedded-nul", test_filename_from_utf8_embedded_nul
);
953 g_test_add_func ("/conversion/filename-from-utf8/embedded-nul/subprocess/utf8", test_filename_from_utf8_embedded_nul_utf8
);
954 g_test_add_func ("/conversion/filename-from-utf8/embedded-nul/subprocess/iconv", test_filename_from_utf8_embedded_nul_iconv
);
956 return g_test_run ();