1 /*
2  * Copyright (c) 2003, Artem B. Bityuckiy, SoftMine Corporation.
3  * Rights transferred to Franklin Electronic Publishers.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <iconv.h>
29 #include <errno.h>
30 #include "check.h"
31 
32 static const char * const good_names[] = {
33 #ifdef _ICONV_FROM_ENCODING_ISO_8859_5
34 "iso_8859_5", "iso-8859-5", "iso-8859_5", "IsO-8859_5"
35 #elif defined _ICONV_FROM_ENCODING_US_ASCII
36 "us_ascii", "US_ASCII", "us-ASCII", "US-ASCII"
37 #elif defined _ICONV_FROM_ENCODING_EUC_JP
38 "euc-jp", "EUC_JP", "euc-JP", "EUC-JP"
39 #elif defined _ICONV_FROM_ENCODING_UTF_8
40 "utf_8", "UTF_8", "uTf-8", "UTF-8"
41 #else
42 #endif
43 };
44 
45 static const char * const bad_names[] =
46 {" ", "iso", "8", "iso_8859_5 ", " iso_8859_5", "csisolatincyrillic ",
47  " csisolatincyrillic", "euc-", "p", "euc_jp ", "euc-jp-",
48  "us_as", "us_", "us_ascii ", " us_ascii",
49  "CCCP", "", "-1", "-", "_", "---", "___", "-_-_-", "_-_-_", NULL};
50 
51 #ifndef TEST_NLSPATH
52 #define TEST_NLSPATH "./"
53 #endif
54 
main(void)55 int main(void)
56 {
57     unsigned i, failed = 0;
58     iconv_t cd;
59 
60     puts("iconv names test");
61 
62     CHECK(setenv("NLSPATH", TEST_NLSPATH, 0) != -1);
63 
64     for (i = 0; i < sizeof(good_names)/sizeof(char *); i++)
65     {
66         printf("Trying iconv(%s, %s)", good_names[0], good_names[i]);
67         fflush(stdout);
68 
69         cd = iconv_open(good_names[0], good_names[i]);
70 
71         if (cd == (iconv_t)-1)
72         {
73             puts(" ... FAILED");
74             failed += 1;
75         }
76         else
77         {
78             puts(" ... PASSED");
79             CHECK(iconv_close(cd) != -1);
80         }
81     }
82 
83     for (i = 0; i < sizeof(bad_names)/sizeof(char *); i++)
84     {
85         printf("Trying iconv(%s, \"%s\")", good_names[0], bad_names[i]);
86         fflush(stdout);
87 
88         cd = iconv_open(good_names[0], bad_names[i]);
89 
90         if (cd != (iconv_t)-1)
91         {
92             puts(" ... FAILED");
93             failed += 1;
94         }
95         else
96             puts(" ... PASSED");
97     }
98 
99     if (failed)
100     {
101         printf("%d FAILTURES\n", failed);
102         abort();
103     }
104 
105     exit(0);
106 }
107