tizen 2.3.1 release
[framework/base/tizen-locale.git] / iconvdata / tst-loading.c
1 /* Tests for loading and unloading of iconv modules.
2    Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #include <iconv.h>
22 #include <mcheck.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25
26
27 /* How many load/unload operations do we do.  */
28 #define TEST_ROUNDS     5000
29
30
31 enum state { unloaded, loaded };
32
33 struct
34 {
35   const char *name;
36   enum state state;
37   iconv_t cd;
38 } modules[] =
39 {
40 #define MODULE(Name) { .name = #Name, .state = unloaded }
41   MODULE (ISO-8859-1),
42   MODULE (ISO-8859-2),
43   MODULE (ISO-8859-3),
44   MODULE (ISO-8859-4),
45   MODULE (ISO-8859-5),
46   MODULE (ISO-8859-6),
47   MODULE (ISO-8859-15),
48   MODULE (EUC-JP),
49   MODULE (EUC-KR),
50   MODULE (EUC-CN),
51   MODULE (EUC-TW),
52   MODULE (SJIS),
53   MODULE (UHC),
54   MODULE (KOI8-R),
55   MODULE (BIG5),
56   MODULE (BIG5HKSCS)
57 };
58 #define nmodules (sizeof (modules) / sizeof (modules[0]))
59
60
61 /* The test data.  */
62 static const char inbuf[] =
63 "The first step is the function to create a handle.\n"
64 "\n"
65 " - Function: iconv_t iconv_open (const char *TOCODE, const char\n"
66 "          *FROMCODE)\n"
67 "     The `iconv_open' function has to be used before starting a\n"
68 "     conversion.  The two parameters this function takes determine the\n"
69 "     source and destination character set for the conversion and if the\n"
70 "     implementation has the possibility to perform such a conversion the\n"
71 "     function returns a handle.\n"
72 "\n"
73 "     If the wanted conversion is not available the function returns\n"
74 "     `(iconv_t) -1'.  In this case the global variable `errno' can have\n"
75 "     the following values:\n"
76 "\n"
77 "    `EMFILE'\n"
78 "          The process already has `OPEN_MAX' file descriptors open.\n"
79 "\n"
80 "    `ENFILE'\n"
81 "          The system limit of open file is reached.\n"
82 "\n"
83 "    `ENOMEM'\n"
84 "          Not enough memory to carry out the operation.\n"
85 "\n"
86 "    `EINVAL'\n"
87 "          The conversion from FROMCODE to TOCODE is not supported.\n"
88 "\n"
89 "     It is not possible to use the same descriptor in different threads\n"
90 "     to perform independent conversions.  Within the data structures\n"
91 "     associated with the descriptor there is information about the\n"
92 "     conversion state.  This must not be messed up by using it in\n"
93 "     different conversions.\n"
94 "\n"
95 "     An `iconv' descriptor is like a file descriptor as for every use a\n"
96 "     new descriptor must be created.  The descriptor does not stand for\n"
97 "     all of the conversions from FROMSET to TOSET.\n"
98 "\n"
99 "     The GNU C library implementation of `iconv_open' has one\n"
100 "     significant extension to other implementations.  To ease the\n"
101 "     extension of the set of available conversions the implementation\n"
102 "     allows storing the necessary files with data and code in\n"
103 "     arbitrarily many directories.  How this extension has to be\n"
104 "     written will be explained below (*note glibc iconv\n"
105 "     Implementation::).  Here it is only important to say that all\n"
106 "     directories mentioned in the `GCONV_PATH' environment variable are\n"
107 "     considered if they contain a file `gconv-modules'.  These\n"
108 "     directories need not necessarily be created by the system\n"
109 "     administrator.  In fact, this extension is introduced to help users\n"
110 "     writing and using their own, new conversions.  Of course this does\n"
111 "     not work for security reasons in SUID binaries; in this case only\n"
112 "     the system directory is considered and this normally is\n"
113 "     `PREFIX/lib/gconv'.  The `GCONV_PATH' environment variable is\n"
114 "     examined exactly once at the first call of the `iconv_open'\n"
115 "     function.  Later modifications of the variable have no effect.\n";
116
117
118 int
119 main (void)
120 {
121   size_t count = TEST_ROUNDS;
122   int result = 0;
123
124   mtrace ();
125
126   /* Just a seed.  */
127   srandom (TEST_ROUNDS);
128
129   while (count--)
130     {
131       int idx = random () % nmodules;
132
133       if (modules[idx].state == unloaded)
134         {
135           char outbuf[10000];
136           char *inptr = (char *) inbuf;
137           size_t insize = sizeof (inbuf) - 1;
138           char *outptr = outbuf;
139           size_t outsize = sizeof (outbuf);
140
141           /* Load the module and do the conversion.  */
142           modules[idx].cd = iconv_open ("UTF-8", modules[idx].name);
143
144           if (modules[idx].cd == (iconv_t) -1)
145             {
146               printf ("opening of %s failed: %m\n", modules[idx].name);
147               result = 1;
148               break;
149             }
150
151           modules[idx].state = loaded;
152
153           /* Now a simple test.  */
154           if (iconv (modules[idx].cd, &inptr, &insize, &outptr, &outsize) != 0
155               || *inptr != '\0')
156             {
157               printf ("conversion with %s failed\n", modules[idx].name);
158               result = 1;
159             }
160         }
161       else
162         {
163           /* Unload the module.  */
164           if (iconv_close (modules[idx].cd) != 0)
165             {
166               printf ("closing of %s failed: %m\n", modules[idx].name);
167               result = 1;
168               break;
169             }
170
171           modules[idx].state = unloaded;
172         }
173     }
174
175   for (count = 0; count < nmodules; ++count)
176     if (modules[count].state == loaded && iconv_close (modules[count].cd) != 0)
177       {
178         printf ("closing of %s failed: %m\n", modules[count].name);
179         result = 1;
180       }
181
182   return result;
183 }