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