Back to index

glibc  2.9
tst-loading.c
Go to the documentation of this file.
00001 /* Tests for loading and unloading of iconv modules.
00002    Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <iconv.h>
00022 #include <mcheck.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 
00026 
00027 /* How many load/unload operations do we do.  */
00028 #define TEST_ROUNDS  5000
00029 
00030 
00031 enum state { unloaded, loaded };
00032 
00033 struct
00034 {
00035   const char *name;
00036   enum state state;
00037   iconv_t cd;
00038 } modules[] =
00039 {
00040 #define MODULE(Name) { .name = #Name, .state = unloaded }
00041   MODULE (ISO-8859-1),
00042   MODULE (ISO-8859-2),
00043   MODULE (ISO-8859-3),
00044   MODULE (ISO-8859-4),
00045   MODULE (ISO-8859-5),
00046   MODULE (ISO-8859-6),
00047   MODULE (ISO-8859-15),
00048   MODULE (EUC-JP),
00049   MODULE (EUC-KR),
00050   MODULE (EUC-CN),
00051   MODULE (EUC-TW),
00052   MODULE (SJIS),
00053   MODULE (UHC),
00054   MODULE (KOI8-R),
00055   MODULE (BIG5),
00056   MODULE (BIG5HKSCS)
00057 };
00058 #define nmodules (sizeof (modules) / sizeof (modules[0]))
00059 
00060 
00061 /* The test data.  */
00062 static const char inbuf[] =
00063 "The first step is the function to create a handle.\n"
00064 "\n"
00065 " - Function: iconv_t iconv_open (const char *TOCODE, const char\n"
00066 "          *FROMCODE)\n"
00067 "     The `iconv_open' function has to be used before starting a\n"
00068 "     conversion.  The two parameters this function takes determine the\n"
00069 "     source and destination character set for the conversion and if the\n"
00070 "     implementation has the possibility to perform such a conversion the\n"
00071 "     function returns a handle.\n"
00072 "\n"
00073 "     If the wanted conversion is not available the function returns\n"
00074 "     `(iconv_t) -1'.  In this case the global variable `errno' can have\n"
00075 "     the following values:\n"
00076 "\n"
00077 "    `EMFILE'\n"
00078 "          The process already has `OPEN_MAX' file descriptors open.\n"
00079 "\n"
00080 "    `ENFILE'\n"
00081 "          The system limit of open file is reached.\n"
00082 "\n"
00083 "    `ENOMEM'\n"
00084 "          Not enough memory to carry out the operation.\n"
00085 "\n"
00086 "    `EINVAL'\n"
00087 "          The conversion from FROMCODE to TOCODE is not supported.\n"
00088 "\n"
00089 "     It is not possible to use the same descriptor in different threads\n"
00090 "     to perform independent conversions.  Within the data structures\n"
00091 "     associated with the descriptor there is information about the\n"
00092 "     conversion state.  This must not be messed up by using it in\n"
00093 "     different conversions.\n"
00094 "\n"
00095 "     An `iconv' descriptor is like a file descriptor as for every use a\n"
00096 "     new descriptor must be created.  The descriptor does not stand for\n"
00097 "     all of the conversions from FROMSET to TOSET.\n"
00098 "\n"
00099 "     The GNU C library implementation of `iconv_open' has one\n"
00100 "     significant extension to other implementations.  To ease the\n"
00101 "     extension of the set of available conversions the implementation\n"
00102 "     allows storing the necessary files with data and code in\n"
00103 "     arbitrarily many directories.  How this extension has to be\n"
00104 "     written will be explained below (*note glibc iconv\n"
00105 "     Implementation::).  Here it is only important to say that all\n"
00106 "     directories mentioned in the `GCONV_PATH' environment variable are\n"
00107 "     considered if they contain a file `gconv-modules'.  These\n"
00108 "     directories need not necessarily be created by the system\n"
00109 "     administrator.  In fact, this extension is introduced to help users\n"
00110 "     writing and using their own, new conversions.  Of course this does\n"
00111 "     not work for security reasons in SUID binaries; in this case only\n"
00112 "     the system directory is considered and this normally is\n"
00113 "     `PREFIX/lib/gconv'.  The `GCONV_PATH' environment variable is\n"
00114 "     examined exactly once at the first call of the `iconv_open'\n"
00115 "     function.  Later modifications of the variable have no effect.\n";
00116 
00117 
00118 int
00119 main (void)
00120 {
00121   size_t count = TEST_ROUNDS;
00122   int result = 0;
00123 
00124   mtrace ();
00125 
00126   /* Just a seed.  */
00127   srandom (TEST_ROUNDS);
00128 
00129   while (count--)
00130     {
00131       int idx = random () % nmodules;
00132 
00133       if (modules[idx].state == unloaded)
00134        {
00135          char outbuf[10000];
00136          char *inptr = (char *) inbuf;
00137          size_t insize = sizeof (inbuf) - 1;
00138          char *outptr = outbuf;
00139          size_t outsize = sizeof (outbuf);
00140 
00141          /* Load the module and do the conversion.  */
00142          modules[idx].cd = iconv_open ("UTF-8", modules[idx].name);
00143 
00144          if (modules[idx].cd == (iconv_t) -1)
00145            {
00146              printf ("opening of %s failed: %m\n", modules[idx].name);
00147              result = 1;
00148              break;
00149            }
00150 
00151          modules[idx].state = loaded;
00152 
00153          /* Now a simple test.  */
00154          if (iconv (modules[idx].cd, &inptr, &insize, &outptr, &outsize) != 0
00155              || *inptr != '\0')
00156            {
00157              printf ("conversion with %s failed\n", modules[idx].name);
00158              result = 1;
00159            }
00160        }
00161       else
00162        {
00163          /* Unload the module.  */
00164          if (iconv_close (modules[idx].cd) != 0)
00165            {
00166              printf ("closing of %s failed: %m\n", modules[idx].name);
00167              result = 1;
00168              break;
00169            }
00170 
00171          modules[idx].state = unloaded;
00172        }
00173     }
00174 
00175   for (count = 0; count < nmodules; ++count)
00176     if (modules[count].state == loaded && iconv_close (modules[count].cd) != 0)
00177       {
00178        printf ("closing of %s failed: %m\n", modules[count].name);
00179        result = 1;
00180       }
00181 
00182   return result;
00183 }