ChangeLog update from my last commit
[glibc.git] / iconvdata / tst-loading.c
blob67f3d1be4286493172534fdde86add5e601cbd5c
1 /* Tests for loading and unloading of iconv modules.
2 Copyright (C) 2000-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
20 #include <iconv.h>
21 #include <mcheck.h>
22 #include <stdio.h>
23 #include <stdlib.h>
26 /* How many load/unload operations do we do. */
27 #define TEST_ROUNDS 5000
30 enum state { unloaded, loaded };
32 struct
34 const char *name;
35 enum state state;
36 iconv_t cd;
37 } modules[] =
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)
57 #define nmodules (sizeof (modules) / sizeof (modules[0]))
60 /* The test data. */
61 static 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";
117 static int
118 do_test (void)
120 size_t count = TEST_ROUNDS;
121 int result = 0;
123 mtrace ();
125 /* Just a seed. */
126 srandom (TEST_ROUNDS);
128 while (count--)
130 int idx = random () % nmodules;
132 if (modules[idx].state == unloaded)
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);
140 /* Load the module and do the conversion. */
141 modules[idx].cd = iconv_open ("UTF-8", modules[idx].name);
143 if (modules[idx].cd == (iconv_t) -1)
145 printf ("opening of %s failed: %m\n", modules[idx].name);
146 result = 1;
147 break;
150 modules[idx].state = loaded;
152 /* Now a simple test. */
153 if (iconv (modules[idx].cd, &inptr, &insize, &outptr, &outsize) != 0
154 || *inptr != '\0')
156 printf ("conversion with %s failed\n", modules[idx].name);
157 result = 1;
160 else
162 /* Unload the module. */
163 if (iconv_close (modules[idx].cd) != 0)
165 printf ("closing of %s failed: %m\n", modules[idx].name);
166 result = 1;
167 break;
170 modules[idx].state = unloaded;
174 for (count = 0; count < nmodules; ++count)
175 if (modules[count].state == loaded && iconv_close (modules[count].cd) != 0)
177 printf ("closing of %s failed: %m\n", modules[count].name);
178 result = 1;
181 return result;
184 #define TIMEOUT 30
185 #define TEST_FUNCTION do_test ()
186 #include "../test-skeleton.c"