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 <newlib.h>
31 #include "check.h"
32 
33 static const char * const good_names[] = {
34 #ifdef _ICONV_FROM_ENCODING_ISO_8859_5
35 "iso_8859_5", "iso-8859-5", "iso-8859_5", "IsO-8859_5"
36 #elif defined _ICONV_FROM_ENCODING_US_ASCII
37 "us_ascii", "US_ASCII", "us-ASCII", "US-ASCII"
38 #elif defined _ICONV_FROM_ENCODING_EUC_JP
39 "euc-jp", "EUC_JP", "euc-JP", "EUC-JP"
40 #elif defined _ICONV_FROM_ENCODING_UTF_8
41 "utf_8", "UTF_8", "uTf-8", "UTF-8"
42 #else
43 #endif
44 };
45 
46 static const char * const bad_names[] =
47 {" ", "iso", "8", "iso_8859_5 ", " iso_8859_5", "csisolatincyrillic ",
48  " csisolatincyrillic", "euc-", "p", "euc_jp ", "euc-jp-",
49  "us_as", "us_", "us_ascii ", " us_ascii",
50  "CCCP", "", "-1", "-", "_", "---", "___", "-_-_-", "_-_-_", NULL};
51 
52 #ifndef TEST_NLSPATH
53 #define TEST_NLSPATH "./"
54 #endif
55 
main(void)56 int main(void)
57 {
58     unsigned i, failed = 0;
59     iconv_t cd;
60 
61     puts("iconv names test");
62 
63     CHECK(setenv("NLSPATH", TEST_NLSPATH, 0) != -1);
64 
65     for (i = 0; i < sizeof(good_names)/sizeof(char *); i++)
66     {
67         printf("Trying iconv(%s, %s)", good_names[0], good_names[i]);
68         fflush(stdout);
69 
70         cd = iconv_open(good_names[0], good_names[i]);
71 
72         if (cd == (iconv_t)-1)
73         {
74             puts(" ... FAILED");
75             failed += 1;
76         }
77         else
78         {
79             puts(" ... PASSED");
80             CHECK(iconv_close(cd) != -1);
81         }
82     }
83 
84     for (i = 0; i < sizeof(bad_names)/sizeof(char *); i++)
85     {
86         printf("Trying iconv(%s, \"%s\")", good_names[0], bad_names[i]);
87         fflush(stdout);
88 
89         cd = iconv_open(good_names[0], bad_names[i]);
90 
91         if (cd != (iconv_t)-1)
92         {
93             puts(" ... FAILED");
94             failed += 1;
95         }
96         else
97             puts(" ... PASSED");
98     }
99 
100     if (failed)
101     {
102         printf("%d FAILTURES\n", failed);
103         abort();
104     }
105 
106     exit(0);
107 }
108