]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * "$Id: testi18n.c 7560 2008-05-13 06:34:04Z mike $" | |
3 | * | |
4 | * Internationalization test for Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 2007-2008 by Apple Inc. | |
7 | * Copyright 1997-2006 by Easy Software Products. | |
8 | * | |
9 | * These coded instructions, statements, and computer programs are the | |
10 | * property of Apple Inc. and are protected by Federal copyright | |
11 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
12 | * which should have been included with this file. If this file is | |
13 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
14 | * | |
15 | * This file is subject to the Apple OS-Developed Software exception. | |
16 | * | |
17 | * Contents: | |
18 | * | |
19 | * main() - Main entry for internationalization test module. | |
20 | * print_utf8() - Print UTF-8 string with (optional) message. | |
21 | */ | |
22 | ||
23 | /* | |
24 | * Include necessary headers... | |
25 | */ | |
26 | ||
27 | #include <stdio.h> | |
28 | #include <stdlib.h> | |
29 | #include <errno.h> | |
30 | #include <time.h> | |
31 | #include <unistd.h> | |
32 | ||
33 | #include "i18n.h" | |
34 | #include "string.h" | |
35 | ||
36 | ||
37 | /* | |
38 | * Local functions... | |
39 | */ | |
40 | ||
41 | static void print_utf8(const char *msg, const cups_utf8_t *src); | |
42 | ||
43 | ||
44 | /* | |
45 | * 'main()' - Main entry for internationalization test module. | |
46 | */ | |
47 | ||
48 | int /* O - Exit code */ | |
49 | main(int argc, /* I - Argument Count */ | |
50 | char *argv[]) /* I - Arguments */ | |
51 | { | |
52 | FILE *fp; /* File pointer */ | |
53 | int count; /* File line counter */ | |
54 | int status, /* Status of current test */ | |
55 | errors; /* Error count */ | |
56 | char line[1024]; /* File line source string */ | |
57 | int len; /* Length (count) of string */ | |
58 | char legsrc[1024], /* Legacy source string */ | |
59 | legdest[1024], /* Legacy destination string */ | |
60 | *legptr; /* Pointer into legacy string */ | |
61 | cups_utf8_t utf8latin[] = /* UTF-8 Latin-1 source */ | |
62 | { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xC3, 0x84, 0x2E, 0x00 }; | |
63 | /* "A != <A WITH DIAERESIS>." - use ISO 8859-1 */ | |
64 | cups_utf8_t utf8repla[] = /* UTF-8 Latin-1 replacement */ | |
65 | { 0x41, 0x20, 0xE2, 0x89, 0xA2, 0x20, 0xC3, 0x84, 0x2E, 0x00 }; | |
66 | /* "A <NOT IDENTICAL TO> <A WITH DIAERESIS>." */ | |
67 | cups_utf8_t utf8greek[] = /* UTF-8 Greek source string */ | |
68 | { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xCE, 0x91, 0x2E, 0x00 }; | |
69 | /* "A != <ALPHA>." - use ISO 8859-7 */ | |
70 | cups_utf8_t utf8japan[] = /* UTF-8 Japanese source */ | |
71 | { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xEE, 0x9C, 0x80, 0x2E, 0x00 }; | |
72 | /* "A != <PRIVATE U+E700>." - use Windows 932 or EUC-JP */ | |
73 | cups_utf8_t utf8taiwan[] = /* UTF-8 Chinese source */ | |
74 | { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xE4, 0xB9, 0x82, 0x2E, 0x00 }; | |
75 | /* "A != <CJK U+4E42>." - use Windows 950 (Big5) or EUC-TW */ | |
76 | cups_utf8_t utf8dest[1024]; /* UTF-8 destination string */ | |
77 | cups_utf32_t utf32dest[1024]; /* UTF-32 destination string */ | |
78 | ||
79 | ||
80 | /* | |
81 | * Make sure we have a symbolic link from the data directory to a | |
82 | * "charmaps" directory, and then point the library at it... | |
83 | */ | |
84 | ||
85 | if (access("charmaps", 0)) | |
86 | symlink("../data", "charmaps"); | |
87 | ||
88 | putenv("CUPS_DATADIR=."); | |
89 | ||
90 | /* | |
91 | * Start with some conversion tests from a UTF-8 test file. | |
92 | */ | |
93 | ||
94 | errors = 0; | |
95 | ||
96 | if ((fp = fopen("utf8demo.txt", "r")) == NULL) | |
97 | { | |
98 | perror("utf8demo.txt"); | |
99 | return (1); | |
100 | } | |
101 | ||
102 | /* | |
103 | * cupsUTF8ToUTF32 | |
104 | */ | |
105 | ||
106 | fputs("cupsUTF8ToUTF32 of utfdemo.txt: ", stdout); | |
107 | ||
108 | for (count = 0, status = 0; fgets(line, sizeof(line), fp);) | |
109 | { | |
110 | count ++; | |
111 | ||
112 | if (cupsUTF8ToUTF32(utf32dest, (cups_utf8_t *)line, 1024) < 0) | |
113 | { | |
114 | printf("FAIL (UTF-8 to UTF-32 on line %d)\n", count); | |
115 | errors ++; | |
116 | status = 1; | |
117 | break; | |
118 | } | |
119 | } | |
120 | ||
121 | if (!status) | |
122 | puts("PASS"); | |
123 | ||
124 | /* | |
125 | * cupsUTF8ToCharset(CUPS_EUC_JP) | |
126 | */ | |
127 | ||
128 | fputs("cupsUTF8ToCharset(CUPS_EUC_JP) of utfdemo.txt: ", stdout); | |
129 | ||
130 | rewind(fp); | |
131 | ||
132 | for (count = 0, status = 0; fgets(line, sizeof(line), fp);) | |
133 | { | |
134 | count ++; | |
135 | ||
136 | len = cupsUTF8ToCharset(legdest, (cups_utf8_t *)line, 1024, CUPS_EUC_JP); | |
137 | if (len < 0) | |
138 | { | |
139 | printf("FAIL (UTF-8 to EUC-JP on line %d)\n", count); | |
140 | errors ++; | |
141 | status = 1; | |
142 | break; | |
143 | } | |
144 | } | |
145 | ||
146 | if (!status) | |
147 | puts("PASS"); | |
148 | ||
149 | fclose(fp); | |
150 | ||
151 | /* | |
152 | * Test charmap load for ISO-8859-1... | |
153 | */ | |
154 | ||
155 | fputs("_cupsCharmapGet(CUPS_ISO8859_1): ", stdout); | |
156 | ||
157 | if (!_cupsCharmapGet(CUPS_ISO8859_1)) | |
158 | { | |
159 | errors ++; | |
160 | puts("FAIL"); | |
161 | } | |
162 | else | |
163 | puts("PASS"); | |
164 | ||
165 | /* | |
166 | * Test charmap load for Windows-932 (Shift-JIS)... | |
167 | */ | |
168 | ||
169 | fputs("_cupsCharmapGet(CUPS_WINDOWS_932): ", stdout); | |
170 | ||
171 | if (!_cupsCharmapGet(CUPS_WINDOWS_932)) | |
172 | { | |
173 | errors ++; | |
174 | puts("FAIL"); | |
175 | } | |
176 | else | |
177 | puts("PASS"); | |
178 | ||
179 | /* | |
180 | * Test VBCS charmap load for EUC-JP... | |
181 | */ | |
182 | ||
183 | fputs("_cupsCharmapGet(CUPS_EUC_JP): ", stdout); | |
184 | ||
185 | if (!_cupsCharmapGet(CUPS_EUC_JP)) | |
186 | { | |
187 | errors ++; | |
188 | puts("FAIL"); | |
189 | } | |
190 | else | |
191 | puts("PASS"); | |
192 | ||
193 | /* | |
194 | * Test VBCS charmap load for EUC-TW... | |
195 | */ | |
196 | ||
197 | fputs("_cupsCharmapGet(CUPS_EUC_TW): ", stdout); | |
198 | ||
199 | if (!_cupsCharmapGet(CUPS_EUC_TW)) | |
200 | { | |
201 | errors ++; | |
202 | puts("FAIL"); | |
203 | } | |
204 | else | |
205 | puts("PASS"); | |
206 | ||
207 | /* | |
208 | * Test UTF-8 to legacy charset (ISO 8859-1)... | |
209 | */ | |
210 | ||
211 | fputs("cupsUTF8ToCharset(CUPS_ISO8859_1): ", stdout); | |
212 | ||
213 | legdest[0] = 0; | |
214 | ||
215 | len = cupsUTF8ToCharset(legdest, utf8latin, 1024, CUPS_ISO8859_1); | |
216 | if (len < 0) | |
217 | { | |
218 | printf("FAIL (len=%d)\n", len); | |
219 | errors ++; | |
220 | } | |
221 | else | |
222 | puts("PASS"); | |
223 | ||
224 | /* | |
225 | * cupsCharsetToUTF8 | |
226 | */ | |
227 | ||
228 | fputs("cupsCharsetToUTF8(CUPS_ISO8859_1): ", stdout); | |
229 | ||
230 | strcpy(legsrc, legdest); | |
231 | ||
232 | len = cupsCharsetToUTF8(utf8dest, legsrc, 1024, CUPS_ISO8859_1); | |
233 | if (len != strlen((char *)utf8latin)) | |
234 | { | |
235 | printf("FAIL (len=%d, expected %d)\n", len, (int)strlen((char *)utf8latin)); | |
236 | print_utf8(" utf8latin", utf8latin); | |
237 | print_utf8(" utf8dest", utf8dest); | |
238 | errors ++; | |
239 | } | |
240 | else if (memcmp(utf8latin, utf8dest, len)) | |
241 | { | |
242 | puts("FAIL (results do not match)"); | |
243 | print_utf8(" utf8latin", utf8latin); | |
244 | print_utf8(" utf8dest", utf8dest); | |
245 | errors ++; | |
246 | } | |
247 | else if (cupsUTF8ToCharset(legdest, utf8repla, 1024, CUPS_ISO8859_1) < 0) | |
248 | { | |
249 | puts("FAIL (replacement characters do not work!)"); | |
250 | errors ++; | |
251 | } | |
252 | else | |
253 | puts("PASS"); | |
254 | ||
255 | /* | |
256 | * Test UTF-8 to/from legacy charset (ISO 8859-7)... | |
257 | */ | |
258 | ||
259 | fputs("cupsUTF8ToCharset(CUPS_ISO8859_7): ", stdout); | |
260 | ||
261 | if (cupsUTF8ToCharset(legdest, utf8greek, 1024, CUPS_ISO8859_7) < 0) | |
262 | { | |
263 | puts("FAIL"); | |
264 | errors ++; | |
265 | } | |
266 | else | |
267 | { | |
268 | for (legptr = legdest; *legptr && *legptr != '?'; legptr ++); | |
269 | ||
270 | if (*legptr) | |
271 | { | |
272 | puts("FAIL (unknown character)"); | |
273 | errors ++; | |
274 | } | |
275 | else | |
276 | puts("PASS"); | |
277 | } | |
278 | ||
279 | fputs("cupsCharsetToUTF8(CUPS_ISO8859_7): ", stdout); | |
280 | ||
281 | strcpy(legsrc, legdest); | |
282 | ||
283 | len = cupsCharsetToUTF8(utf8dest, legsrc, 1024, CUPS_ISO8859_7); | |
284 | if (len != strlen((char *)utf8greek)) | |
285 | { | |
286 | printf("FAIL (len=%d, expected %d)\n", len, (int)strlen((char *)utf8greek)); | |
287 | print_utf8(" utf8greek", utf8greek); | |
288 | print_utf8(" utf8dest", utf8dest); | |
289 | errors ++; | |
290 | } | |
291 | else if (memcmp(utf8greek, utf8dest, len)) | |
292 | { | |
293 | puts("FAIL (results do not match)"); | |
294 | print_utf8(" utf8greek", utf8greek); | |
295 | print_utf8(" utf8dest", utf8dest); | |
296 | errors ++; | |
297 | } | |
298 | else | |
299 | puts("PASS"); | |
300 | ||
301 | /* | |
302 | * Test UTF-8 to/from legacy charset (Windows 932)... | |
303 | */ | |
304 | ||
305 | fputs("cupsUTF8ToCharset(CUPS_WINDOWS_932): ", stdout); | |
306 | ||
307 | if (cupsUTF8ToCharset(legdest, utf8japan, 1024, CUPS_WINDOWS_932) < 0) | |
308 | { | |
309 | puts("FAIL"); | |
310 | errors ++; | |
311 | } | |
312 | else | |
313 | { | |
314 | for (legptr = legdest; *legptr && *legptr != '?'; legptr ++); | |
315 | ||
316 | if (*legptr) | |
317 | { | |
318 | puts("FAIL (unknown character)"); | |
319 | errors ++; | |
320 | } | |
321 | else | |
322 | puts("PASS"); | |
323 | } | |
324 | ||
325 | fputs("cupsCharsetToUTF8(CUPS_WINDOWS_932): ", stdout); | |
326 | ||
327 | strcpy(legsrc, legdest); | |
328 | ||
329 | len = cupsCharsetToUTF8(utf8dest, legsrc, 1024, CUPS_WINDOWS_932); | |
330 | if (len != strlen((char *)utf8japan)) | |
331 | { | |
332 | printf("FAIL (len=%d, expected %d)\n", len, (int)strlen((char *)utf8japan)); | |
333 | print_utf8(" utf8japan", utf8japan); | |
334 | print_utf8(" utf8dest", utf8dest); | |
335 | errors ++; | |
336 | } | |
337 | else if (memcmp(utf8japan, utf8dest, len)) | |
338 | { | |
339 | puts("FAIL (results do not match)"); | |
340 | print_utf8(" utf8japan", utf8japan); | |
341 | print_utf8(" utf8dest", utf8dest); | |
342 | errors ++; | |
343 | } | |
344 | else | |
345 | puts("PASS"); | |
346 | ||
347 | /* | |
348 | * Test UTF-8 to/from legacy charset (EUC-JP)... | |
349 | */ | |
350 | ||
351 | fputs("cupsUTF8ToCharset(CUPS_EUC_JP): ", stdout); | |
352 | ||
353 | if (cupsUTF8ToCharset(legdest, utf8japan, 1024, CUPS_EUC_JP) < 0) | |
354 | { | |
355 | puts("FAIL"); | |
356 | errors ++; | |
357 | } | |
358 | else | |
359 | { | |
360 | for (legptr = legdest; *legptr && *legptr != '?'; legptr ++); | |
361 | ||
362 | if (*legptr) | |
363 | { | |
364 | puts("FAIL (unknown character)"); | |
365 | errors ++; | |
366 | } | |
367 | else | |
368 | puts("PASS"); | |
369 | } | |
370 | ||
371 | fputs("cupsCharsetToUTF8(CUPS_EUC_JP): ", stdout); | |
372 | ||
373 | strcpy(legsrc, legdest); | |
374 | ||
375 | len = cupsCharsetToUTF8(utf8dest, legsrc, 1024, CUPS_EUC_JP); | |
376 | if (len != strlen((char *)utf8japan)) | |
377 | { | |
378 | printf("FAIL (len=%d, expected %d)\n", len, (int)strlen((char *)utf8japan)); | |
379 | print_utf8(" utf8japan", utf8japan); | |
380 | print_utf8(" utf8dest", utf8dest); | |
381 | errors ++; | |
382 | } | |
383 | else if (memcmp(utf8japan, utf8dest, len)) | |
384 | { | |
385 | puts("FAIL (results do not match)"); | |
386 | print_utf8(" utf8japan", utf8japan); | |
387 | print_utf8(" utf8dest", utf8dest); | |
388 | errors ++; | |
389 | } | |
390 | else | |
391 | puts("PASS"); | |
392 | ||
393 | /* | |
394 | * Test UTF-8 to/from legacy charset (Windows 950)... | |
395 | */ | |
396 | ||
397 | fputs("cupsUTF8ToCharset(CUPS_WINDOWS_950): ", stdout); | |
398 | ||
399 | if (cupsUTF8ToCharset(legdest, utf8taiwan, 1024, CUPS_WINDOWS_950) < 0) | |
400 | { | |
401 | puts("FAIL"); | |
402 | errors ++; | |
403 | } | |
404 | else | |
405 | { | |
406 | for (legptr = legdest; *legptr && *legptr != '?'; legptr ++); | |
407 | ||
408 | if (*legptr) | |
409 | { | |
410 | puts("FAIL (unknown character)"); | |
411 | errors ++; | |
412 | } | |
413 | else | |
414 | puts("PASS"); | |
415 | } | |
416 | ||
417 | fputs("cupsCharsetToUTF8(CUPS_WINDOWS_950): ", stdout); | |
418 | ||
419 | strcpy(legsrc, legdest); | |
420 | ||
421 | len = cupsCharsetToUTF8(utf8dest, legsrc, 1024, CUPS_WINDOWS_950); | |
422 | if (len != strlen((char *)utf8taiwan)) | |
423 | { | |
424 | printf("FAIL (len=%d, expected %d)\n", len, (int)strlen((char *)utf8taiwan)); | |
425 | print_utf8(" utf8taiwan", utf8taiwan); | |
426 | print_utf8(" utf8dest", utf8dest); | |
427 | errors ++; | |
428 | } | |
429 | else if (memcmp(utf8taiwan, utf8dest, len)) | |
430 | { | |
431 | puts("FAIL (results do not match)"); | |
432 | print_utf8(" utf8taiwan", utf8taiwan); | |
433 | print_utf8(" utf8dest", utf8dest); | |
434 | errors ++; | |
435 | } | |
436 | else | |
437 | puts("PASS"); | |
438 | ||
439 | /* | |
440 | * Test UTF-8 to/from legacy charset (EUC-TW)... | |
441 | */ | |
442 | ||
443 | fputs("cupsUTF8ToCharset(CUPS_EUC_TW): ", stdout); | |
444 | ||
445 | if (cupsUTF8ToCharset(legdest, utf8taiwan, 1024, CUPS_EUC_TW) < 0) | |
446 | { | |
447 | puts("FAIL"); | |
448 | errors ++; | |
449 | } | |
450 | else | |
451 | { | |
452 | for (legptr = legdest; *legptr && *legptr != '?'; legptr ++); | |
453 | ||
454 | if (*legptr) | |
455 | { | |
456 | puts("FAIL (unknown character)"); | |
457 | errors ++; | |
458 | } | |
459 | else | |
460 | puts("PASS"); | |
461 | } | |
462 | ||
463 | fputs("cupsCharsetToUTF8(CUPS_EUC_TW): ", stdout); | |
464 | ||
465 | strcpy(legsrc, legdest); | |
466 | ||
467 | len = cupsCharsetToUTF8(utf8dest, legsrc, 1024, CUPS_EUC_TW); | |
468 | if (len != strlen((char *)utf8taiwan)) | |
469 | { | |
470 | printf("FAIL (len=%d, expected %d)\n", len, (int)strlen((char *)utf8taiwan)); | |
471 | print_utf8(" utf8taiwan", utf8taiwan); | |
472 | print_utf8(" utf8dest", utf8dest); | |
473 | errors ++; | |
474 | } | |
475 | else if (memcmp(utf8taiwan, utf8dest, len)) | |
476 | { | |
477 | puts("FAIL (results do not match)"); | |
478 | print_utf8(" utf8taiwan", utf8taiwan); | |
479 | print_utf8(" utf8dest", utf8dest); | |
480 | errors ++; | |
481 | } | |
482 | else | |
483 | puts("PASS"); | |
484 | ||
485 | #if 0 | |
486 | /* | |
487 | * Test UTF-8 (16-bit) to UTF-32 (w/ BOM)... | |
488 | */ | |
489 | if (verbose) | |
490 | printf("\ntesti18n: Testing UTF-8 to UTF-32 (w/ BOM)...\n"); | |
491 | len = cupsUTF8ToUTF32(utf32dest, utf8good, 1024); | |
492 | if (len < 0) | |
493 | return (1); | |
494 | if (verbose) | |
495 | { | |
496 | print_utf8(" utf8good ", utf8good); | |
497 | print_utf32(" utf32dest", utf32dest); | |
498 | } | |
499 | memcpy (utf32src, utf32dest, (len + 1) * sizeof(cups_utf32_t)); | |
500 | len = cupsUTF32ToUTF8(utf8dest, utf32src, 1024); | |
501 | if (len < 0) | |
502 | return (1); | |
503 | if (len != strlen ((char *) utf8good)) | |
504 | return (1); | |
505 | if (memcmp(utf8good, utf8dest, len) != 0) | |
506 | return (1); | |
507 | ||
508 | /* | |
509 | * Test invalid UTF-8 (16-bit) to UTF-32 (w/ BOM)... | |
510 | */ | |
511 | if (verbose) | |
512 | printf("\ntesti18n: Testing UTF-8 bad 16-bit source string...\n"); | |
513 | len = cupsUTF8ToUTF32(utf32dest, utf8bad, 1024); | |
514 | if (len >= 0) | |
515 | return (1); | |
516 | if (verbose) | |
517 | print_utf8(" utf8bad ", utf8bad); | |
518 | ||
519 | /* | |
520 | * Test _cupsCharmapFlush()... | |
521 | */ | |
522 | if (verbose) | |
523 | printf("\ntesti18n: Testing _cupsCharmapFlush()...\n"); | |
524 | _cupsCharmapFlush(); | |
525 | return (0); | |
526 | #endif /* 0 */ | |
527 | ||
528 | return (errors > 0); | |
529 | } | |
530 | ||
531 | ||
532 | /* | |
533 | * 'print_utf8()' - Print UTF-8 string with (optional) message. | |
534 | */ | |
535 | ||
536 | static void | |
537 | print_utf8(const char *msg, /* I - Message String */ | |
538 | const cups_utf8_t *src) /* I - UTF-8 Source String */ | |
539 | { | |
540 | const char *prefix; /* Prefix string */ | |
541 | ||
542 | ||
543 | if (msg) | |
544 | printf("%s:", msg); | |
545 | ||
546 | for (prefix = " "; *src; src ++) | |
547 | { | |
548 | printf("%s%02x", prefix, *src); | |
549 | ||
550 | if ((src[0] & 0x80) && (src[1] & 0x80)) | |
551 | prefix = ""; | |
552 | else | |
553 | prefix = " "; | |
554 | } | |
555 | ||
556 | putchar('\n'); | |
557 | } | |
558 | ||
559 | ||
560 | /* | |
561 | * End of "$Id: testi18n.c 7560 2008-05-13 06:34:04Z mike $" | |
562 | */ |