Debian Codegraph @

lib/util/charset/tests/iconv.c

raw
1 /*
2 Unix SMB/CIFS implementation.
3
4 local testing of iconv routines. This tests the system iconv code against
5 the built-in iconv code
6
7 Copyright (C) Andrew Tridgell 2004
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "torture/torture.h"
25 #include "system/iconv.h"
26 #include "system/time.h"
27 #include "libcli/raw/libcliraw.h"
28 #include "param/param.h"
29 #include "torture/util.h"
30 #include "torture/local/proto.h"
31 #include "talloc.h"
32
33 #ifdef HAVE_NATIVE_ICONV
34
35 static bool iconv_untestable(struct torture_context *tctx)
36 {
37 iconv_t cd;
38
39 cd = iconv_open("UTF-16LE", "UCS-4LE");
40 if (cd == (iconv_t)-1)
41 torture_skip(tctx, "unable to test - system iconv library does not support UTF-16LE -> UCS-4LE");
42 iconv_close(cd);
43
44 cd = iconv_open("UTF-16LE", "CP850");
45 if (cd == (iconv_t)-1)
46 torture_skip(tctx, "unable to test - system iconv library does not support UTF-16LE -> CP850\n");
47 iconv_close(cd);
48
49 return false;
50 }
51
52 /*
53 generate a UTF-16LE buffer for a given unicode codepoint
54 */
55 static int gen_codepoint_utf16(unsigned int codepoint,
56 char *buf, size_t *size)
57 {
58 static iconv_t cd;
59 uint8_t in[4];
60 char *ptr_in;
61 size_t size_in, size_out, ret;
62 if (!cd) {
63 cd = iconv_open("UTF-16LE", "UCS-4LE");
64 if (cd == (iconv_t)-1) {
65 cd = NULL;
66 return -1;
67 }
68 }
69
70 in[0] = codepoint & 0xFF;
71 in[1] = (codepoint>>8) & 0xFF;
72 in[2] = (codepoint>>16) & 0xFF;
73 in[3] = (codepoint>>24) & 0xFF;
74
75 ptr_in = (char *)in;
76 size_in = 4;
77 size_out = 8;
78
79 ret = iconv(cd, &ptr_in, &size_in, &buf, &size_out);
80
81 *size = 8 - size_out;
82
83 return ret;
84 }
85
86
87 /*
88 work out the unicode codepoint of the first UTF-8 character in the buffer
89 */
90 static unsigned int get_codepoint(char *buf, size_t size, const char *charset)
91 {
92 iconv_t cd;
93 uint8_t out[4];
94 char *ptr_out;
95 size_t size_out, size_in, ret;
96
97 cd = iconv_open("UCS-4LE", charset);
98
99 size_in = size;
100 ptr_out = (char *)out;
101 size_out = sizeof(out);
102 memset(out, 0, sizeof(out));
103
104 ret = iconv(cd, &buf, &size_in, &ptr_out, &size_out);
105 iconv_close(cd);
106 if (ret == (size_t) -1) {
107 return (unsigned int)-1;
108 }
109
110 return out[0] | (out[1]<<8) | (out[2]<<16) | (out[3]<<24);
111 }
112
113 /*
114 display a buffer with name prefix
115 */
116 static void show_buf(const char *name, uint8_t *buf, size_t size)
117 {
118 int i;
119 printf("%s ", name);
120 for (i=0;i<size;i++) {
121 printf("%02x ", buf[i]);
122 }
123 printf("\n");
124 }
125
126 /*
127 given a UTF-16LE buffer, test the system and built-in iconv code to
128 make sure they do exactly the same thing in converting the buffer to
129 "charset", then convert it back again and ensure we get the same
130 buffer back
131 */
132
133 struct test_buffer_cache {
134 const char *charset;
135 iconv_t cd;
136 smb_iconv_t cd2;
137 smb_iconv_t cd3;
138 };
139
140 static int test_buffer_cache_destructor(struct test_buffer_cache *c)
141 {
142 if (c->cd3 != (smb_iconv_t)-1) {
143 smb_iconv_close(c->cd3);
144 c->cd3 = (smb_iconv_t)-1;
145 }
146 if (c->cd2 != (smb_iconv_t)-1) {
147 smb_iconv_close(c->cd2);
148 c->cd2 = (smb_iconv_t)-1;
149 }
150 if (c->cd != (iconv_t)-1) {
151 iconv_close(c->cd);
152 c->cd = (iconv_t)-1;
153 }
154
155 return 0;
156 }
157
158 static bool test_buffer_create_cache(struct torture_context *test,
159 const char *charset,
160 struct test_buffer_cache **_cache)
161 {
162 struct test_buffer_cache *c = NULL;
163 bool use_builtin;
164
165 use_builtin = lpcfg_parm_bool(test->lp_ctx,
166 NULL,
167 "iconv",
168 "use_builtin_handlers",
169 true);
170
171 c = talloc_zero(test, struct test_buffer_cache);
172 torture_assert(test, c != NULL, "talloc_zero");
173 c->cd = (iconv_t)-1;
174 c->cd2 = (smb_iconv_t)-1;
175 c->cd3 = (smb_iconv_t)-1;
176 talloc_set_destructor(c, test_buffer_cache_destructor);
177
178 c->charset = talloc_strdup(c, charset);
179 torture_assert(test, c->charset != NULL, "talloc_strdup");
180
181 c->cd = iconv_open(charset, "UTF-16LE");
182 if (c->cd == (iconv_t)-1) {
183 torture_fail_goto(test, fail, talloc_asprintf(test,
184 "failed to open %s to UTF-16LE",
185 charset));
186 }
187 c->cd2 = smb_iconv_open_ex(c, charset, "UTF-16LE", use_builtin);
188 if (c->cd2 == (smb_iconv_t)-1) {
189 int saved_errno = errno;
190 torture_fail_goto(test, fail, talloc_asprintf(c,
191 "failed to open %s to UTF-16LE via smb_iconv_open_ex"
192 "use_builtin[%u] %s",
193 charset, use_builtin, strerror(saved_errno)));
194 }
195 c->cd3 = smb_iconv_open_ex(c, "UTF-16LE", charset, use_builtin);
196 if (c->cd3 == (smb_iconv_t)-1) {
197 int saved_errno = errno;
198 torture_fail_goto(test, fail, talloc_asprintf(c,
199 "failed to open UTF-16LE to %s via smb_iconv_open_ex"
200 "use_builtin[%u] %s",
201 charset, use_builtin, strerror(saved_errno)));
202 }
203
204 *_cache = c;
205 return true;
206 fail:
207 TALLOC_FREE(c);
208 return false;
209 }
210
211 static bool test_buffer(struct torture_context *test,
212 struct test_buffer_cache *c,
213 uint8_t *inbuf, size_t size)
214 {
215 uint8_t buf1[1000], buf2[1000], buf3[1000];
216 size_t outsize1, outsize2, outsize3;
217 const char *ptr_in1;
218 char *ptr_in2;
219 char *ptr_out;
220 size_t size_in1, size_in2, size_in3;
221 size_t ret1, ret2, ret3, len1, len2;
222 int errno1, errno2;
223 const char *charset = c->charset;
224 iconv_t cd = c->cd;
225 smb_iconv_t cd2 = c->cd2;
226 smb_iconv_t cd3 = c->cd3;
227
228 /* internal convert to charset - placing result in buf1 */
229 ptr_in1 = (const char *)inbuf;
230 ptr_out = (char *)buf1;
231 size_in1 = size;
232 outsize1 = sizeof(buf1);
233
234 memset(ptr_out, 0, outsize1);
235 errno = 0;
236 ret1 = smb_iconv(cd2, &ptr_in1, &size_in1, &ptr_out, &outsize1);
237 errno1 = errno;
238
239 /* system convert to charset - placing result in buf2 */
240 ptr_in2 = (char *)inbuf;
241 ptr_out = (char *)buf2;
242 size_in2 = size;
243 outsize2 = sizeof(buf2);
244
245 memset(ptr_out, 0, outsize2);
246 errno = 0;
247 ret2 = iconv(cd, &ptr_in2, &size_in2, &ptr_out, &outsize2);
248 errno2 = errno;
249
250 len1 = sizeof(buf1) - outsize1;
251 len2 = sizeof(buf2) - outsize2;
252
253 /* codepoints above 1M are not interesting for now */
254 if (len2 > len1 &&
255 memcmp(buf1, buf2, len1) == 0 &&
256 get_codepoint((char *)(buf2+len1), len2-len1, charset) >= (1<<20)) {
257 return true;
258 }
259 if (len1 > len2 &&
260 memcmp(buf1, buf2, len2) == 0 &&
261 get_codepoint((char *)(buf1+len2), len1-len2, charset) >= (1<<20)) {
262 return true;
263 }
264
265 torture_assert_int_equal(test, ret1, ret2, "ret mismatch");
266
267 if (errno1 != errno2) {
268 show_buf(" rem1:", inbuf+(size-size_in1), size_in1);
269 show_buf(" rem2:", inbuf+(size-size_in2), size_in2);
270 torture_fail(test, talloc_asprintf(test,
271 "errno mismatch with %s internal=%d/%s system=%d/%s",
272 charset,
273 errno1, strerror(errno1),
274 errno2, strerror(errno2)));
275 }
276
277 torture_assert_int_equal(test, outsize1, outsize2, "outsize mismatch");
278
279 torture_assert_int_equal(test, size_in1, size_in2, "size_in mismatch");
280
281 if (len1 != len2 ||
282 memcmp(buf1, buf2, len1) != 0) {
283 torture_comment(test, "size=%d ret1=%d ret2=%d", (int)size, (int)ret1, (int)ret2);
284 show_buf(" IN1:", inbuf, size-size_in1);
285 show_buf(" IN2:", inbuf, size-size_in2);
286 show_buf("OUT1:", buf1, len1);
287 show_buf("OUT2:", buf2, len2);
288 if (len2 > len1 && memcmp(buf1, buf2, len1) == 0) {
289 torture_comment(test, "next codepoint is %u",
290 get_codepoint((char *)(buf2+len1), len2-len1, charset));
291 }
292 if (len1 > len2 && memcmp(buf1, buf2, len2) == 0) {
293 torture_comment(test, "next codepoint is %u",
294 get_codepoint((char *)(buf1+len2),len1-len2, charset));
295 }
296
297 torture_fail(test, "failed");
298 }
299
300 /* convert back to UTF-16, putting result in buf3 */
301 size = size - size_in1;
302 ptr_in1 = (const char *)buf1;
303 ptr_out = (char *)buf3;
304 size_in3 = len1;
305 outsize3 = sizeof(buf3);
306
307 memset(ptr_out, 0, outsize3);
308 ret3 = smb_iconv(cd3, &ptr_in1, &size_in3, &ptr_out, &outsize3);
309
310 /* we only internally support the first 1M codepoints */
311 if (outsize3 != sizeof(buf3) - size &&
312 get_codepoint((char *)(inbuf+sizeof(buf3) - outsize3),
313 size - (sizeof(buf3) - outsize3),
314 "UTF-16LE") >= (1<<20)) {
315 return true;
316 }
317
318 torture_assert_int_equal(test, ret3, 0, talloc_asprintf(test,
319 "pull failed - %s", strerror(errno)));
320
321 if (strncmp(charset, "UTF", 3) != 0) {
322 /* don't expect perfect mappings for non UTF charsets */
323 return true;
324 }
325
326
327 torture_assert_int_equal(test, outsize3, sizeof(buf3) - size,
328 "wrong outsize3");
329
330 if (memcmp(buf3, inbuf, size) != 0) {
331 torture_comment(test, "pull bytes mismatch:");
332 show_buf("inbuf", inbuf, size);
333 show_buf(" buf3", buf3, sizeof(buf3) - outsize3);
334 torture_comment(test, "next codepoint is %u\n",
335 get_codepoint((char *)(inbuf+sizeof(buf3) - outsize3),
336 size - (sizeof(buf3) - outsize3),
337 "UTF-16LE"));
338 torture_fail(test, "");
339 }
340
341 return true;
342 }
343
344
345 /*
346 test the push_codepoint() and next_codepoint() functions for a given
347 codepoint
348 */
349 static bool test_codepoint(struct torture_context *tctx, unsigned int codepoint)
350 {
351 uint8_t buf[10];
352 ssize_t size;
353 size_t size2;
354 codepoint_t c;
355
356 size = push_codepoint_handle(lpcfg_iconv_handle(tctx->lp_ctx), (char *)buf, codepoint);
357 torture_assert(tctx,
358 size >= 0 ||
359 (codepoint >= 0xd800 && codepoint <= 0x10000),
360 "Invalid Codepoint range");
361
362 if (size < 0)
363 return true;
364
365 buf[size] = random();
366 buf[size+1] = random();
367 buf[size+2] = random();
368 buf[size+3] = random();
369
370 c = next_codepoint_handle(lpcfg_iconv_handle(tctx->lp_ctx), (char *)buf, &size2);
371
372 torture_assert(tctx, c == codepoint,
373 talloc_asprintf(tctx,
374 "next_codepoint(%u) failed - gave %u", codepoint, c));
375
376 torture_assert(tctx, size2 == size,
377 talloc_asprintf(tctx, "next_codepoint(%u) gave wrong size %d (should be %d)\n",
378 codepoint, (int)size2, (int)size));
379
380 return true;
381 }
382
383 static bool test_next_codepoint(struct torture_context *tctx)
384 {
385 unsigned int codepoint;
386 if (iconv_untestable(tctx))
387 return true;
388
389 for (codepoint=0;codepoint<(1<<20);codepoint++) {
390 if (!test_codepoint(tctx, codepoint))
391 return false;
392 }
393 return true;
394 }
395
396 static bool test_first_1m(struct torture_context *tctx)
397 {
398 unsigned int codepoint;
399 size_t size;
400 unsigned char inbuf[1000];
401 struct test_buffer_cache *c_utf8 = NULL;
402 bool ok;
403
404 if (iconv_untestable(tctx))
405 return true;
406
407 ok = test_buffer_create_cache(tctx, "UTF-8", &c_utf8);
408 torture_assert(tctx, ok, "test_buffer_create_cache");
409
410 for (codepoint=0;codepoint<(1<<20);codepoint++) {
411 if (gen_codepoint_utf16(codepoint, (char *)inbuf, &size) != 0) {
412 continue;
413 }
414
415 if (codepoint % 1000 == 0) {
416 if (torture_setting_bool(tctx, "progress", true)) {
417 torture_comment(tctx, "codepoint=%u \r", codepoint);
418 fflush(stdout);
419 }
420 }
421
422 if (!test_buffer(tctx, c_utf8, inbuf, size))
423 return false;
424 }
425 return true;
426 }
427
428 static bool test_random_5m(struct torture_context *tctx)
429 {
430 unsigned char inbuf[1000];
431 unsigned int i;
432 struct test_buffer_cache *c_utf8 = NULL;
433 struct test_buffer_cache *c_cp850 = NULL;
434 bool ok;
435
436 if (iconv_untestable(tctx))
437 return true;
438
439 ok = test_buffer_create_cache(tctx, "UTF-8", &c_utf8);
440 torture_assert(tctx, ok, "test_buffer_create_cache(UTF-8)");
441
442 ok = test_buffer_create_cache(tctx, "CP850", &c_cp850);
443 torture_assert(tctx, ok, "test_buffer_create_cache(CP850)");
444
445 for (i=0;i<500000;i++) {
446 size_t size;
447 unsigned int c;
448
449 if (i % 1000 == 0) {
450 if (torture_setting_bool(tctx, "progress", true)) {
451 torture_comment(tctx, "i=%u \r", i);
452 fflush(stdout);
453 }
454 }
455
456 size = random() % 100;
457 for (c=0;c<size;c++) {
458 if (random() % 100 < 80) {
459 inbuf[c] = random() % 128;
460 } else {
461 inbuf[c] = random();
462 }
463 if (random() % 10 == 0) {
464 inbuf[c] |= 0xd8;
465 }
466 if (random() % 10 == 0) {
467 inbuf[c] |= 0xdc;
468 }
469 }
470 if (!test_buffer(tctx, c_utf8, inbuf, size)) {
471 printf("i=%d failed UTF-8\n", i);
472 return false;
473 }
474
475 if (!test_buffer(tctx, c_cp850, inbuf, size)) {
476 printf("i=%d failed CP850\n", i);
477 return false;
478 }
479 }
480
481 TALLOC_FREE(c_utf8);
482 TALLOC_FREE(c_cp850);
483 return true;
484 }
485
486
487 static bool test_string2key(struct torture_context *tctx)
488 {
489 uint16_t *buf;
490 char *dest = NULL;
491 TALLOC_CTX *mem_ctx = talloc_new(tctx);
492 size_t len = (random()%1000)+1;
493 const uint16_t in1[10] = { 'a', 0xd805, 'b', 0xdcf0, 'c', 0, 'd', 'e', 'f', 'g' };
494 uint8_t le1[20];
495 uint8_t *munged1;
496 uint8_t *out1;
497 size_t ret;
498 int i;
499 const char *correct = "a\357\277\275b\357\277\275c\001defg";
500
501 buf = talloc_size(mem_ctx, len*2);
502 generate_random_buffer((uint8_t *)buf, len*2);
503
504 torture_comment(tctx, "converting random buffer\n");
505
506 if (!convert_string_talloc(mem_ctx, CH_UTF16MUNGED, CH_UTF8, (void *)buf, len*2, (void**)&dest, &ret)) {
507 torture_fail(tctx, "Failed to convert random buffer\n");
508 }
509
510 for (i=0;i<10;i++) {
511 SSVAL(&le1[2*i], 0, in1[i]);
512 }
513
514 torture_comment(tctx, "converting fixed buffer to UTF16\n");
515
516 if (!convert_string_talloc(mem_ctx, CH_UTF16MUNGED, CH_UTF16, (void *)le1, 20, (void**)&munged1, &ret)) {
517 torture_fail(tctx, "Failed to convert fixed buffer to UTF16_MUNGED\n");
518 }
519
520 torture_assert(tctx, ret == 20, "conversion should give 20 bytes\n");
521
522 torture_comment(tctx, "converting fixed buffer to UTF8\n");
523
524 if (!convert_string_talloc(mem_ctx, CH_UTF16MUNGED, CH_UTF8, (void *)le1, 20, (void**)&out1, &ret)) {
525 torture_fail(tctx, "Failed to convert fixed buffer to UTF8\n");
526 }
527
528 torture_assert(tctx, strcmp(correct, (const char *) out1) == 0,
529 "conversion gave incorrect result\n");
530
531 talloc_free(mem_ctx);
532
533 return true;
534 }
535
536 struct torture_suite *torture_local_iconv(TALLOC_CTX *mem_ctx)
537 {
538 struct torture_suite *suite = torture_suite_create(mem_ctx, "iconv");
539
540 torture_suite_add_simple_test(suite, "string2key",
541 test_string2key);
542
543 torture_suite_add_simple_test(suite, "next_codepoint()",
544 test_next_codepoint);
545
546 torture_suite_add_simple_test(suite, "first 1M codepoints",
547 test_first_1m);
548
549 torture_suite_add_simple_test(suite, "5M random UTF-16LE sequences",
550 test_random_5m);
551
552 torture_suite_add_simple_test(suite, "string2key",
553 test_string2key);
554 return suite;
555 }
556
557 #else
558
559 struct torture_suite *torture_local_iconv(TALLOC_CTX *mem_ctx)
560 {
561 printf("No native iconv library - can't run iconv test\n");
562 return NULL;
563 }
564
565 #endif