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