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