Blame iconvdata/tst-loading.c

Packit 6c4009
/* Tests for loading and unloading of iconv modules.
Packit 6c4009
   Copyright (C) 2000-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <iconv.h>
Packit 6c4009
#include <mcheck.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* How many load/unload operations do we do.  */
Packit 6c4009
#define TEST_ROUNDS	5000
Packit 6c4009
Packit 6c4009
Packit 6c4009
enum state { unloaded, loaded };
Packit 6c4009
Packit 6c4009
struct
Packit 6c4009
{
Packit 6c4009
  const char *name;
Packit 6c4009
  enum state state;
Packit 6c4009
  iconv_t cd;
Packit 6c4009
} modules[] =
Packit 6c4009
{
Packit 6c4009
#define MODULE(Name) { .name = #Name, .state = unloaded }
Packit 6c4009
  MODULE (ISO-8859-1),
Packit 6c4009
  MODULE (ISO-8859-2),
Packit 6c4009
  MODULE (ISO-8859-3),
Packit 6c4009
  MODULE (ISO-8859-4),
Packit 6c4009
  MODULE (ISO-8859-5),
Packit 6c4009
  MODULE (ISO-8859-6),
Packit 6c4009
  MODULE (ISO-8859-15),
Packit 6c4009
  MODULE (EUC-JP),
Packit 6c4009
  MODULE (EUC-KR),
Packit 6c4009
  MODULE (EUC-CN),
Packit 6c4009
  MODULE (EUC-TW),
Packit 6c4009
  MODULE (SJIS),
Packit 6c4009
  MODULE (UHC),
Packit 6c4009
  MODULE (KOI8-R),
Packit 6c4009
  MODULE (BIG5),
Packit 6c4009
  MODULE (BIG5HKSCS)
Packit 6c4009
};
Packit 6c4009
#define nmodules (sizeof (modules) / sizeof (modules[0]))
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* The test data.  */
Packit 6c4009
static const char inbuf[] =
Packit 6c4009
"The first step is the function to create a handle.\n"
Packit 6c4009
"\n"
Packit 6c4009
" - Function: iconv_t iconv_open (const char *TOCODE, const char\n"
Packit 6c4009
"          *FROMCODE)\n"
Packit 6c4009
"     The `iconv_open' function has to be used before starting a\n"
Packit 6c4009
"     conversion.  The two parameters this function takes determine the\n"
Packit 6c4009
"     source and destination character set for the conversion and if the\n"
Packit 6c4009
"     implementation has the possibility to perform such a conversion the\n"
Packit 6c4009
"     function returns a handle.\n"
Packit 6c4009
"\n"
Packit 6c4009
"     If the wanted conversion is not available the function returns\n"
Packit 6c4009
"     `(iconv_t) -1'.  In this case the global variable `errno' can have\n"
Packit 6c4009
"     the following values:\n"
Packit 6c4009
"\n"
Packit 6c4009
"    `EMFILE'\n"
Packit 6c4009
"          The process already has `OPEN_MAX' file descriptors open.\n"
Packit 6c4009
"\n"
Packit 6c4009
"    `ENFILE'\n"
Packit 6c4009
"          The system limit of open file is reached.\n"
Packit 6c4009
"\n"
Packit 6c4009
"    `ENOMEM'\n"
Packit 6c4009
"          Not enough memory to carry out the operation.\n"
Packit 6c4009
"\n"
Packit 6c4009
"    `EINVAL'\n"
Packit 6c4009
"          The conversion from FROMCODE to TOCODE is not supported.\n"
Packit 6c4009
"\n"
Packit 6c4009
"     It is not possible to use the same descriptor in different threads\n"
Packit 6c4009
"     to perform independent conversions.  Within the data structures\n"
Packit 6c4009
"     associated with the descriptor there is information about the\n"
Packit 6c4009
"     conversion state.  This must not be messed up by using it in\n"
Packit 6c4009
"     different conversions.\n"
Packit 6c4009
"\n"
Packit 6c4009
"     An `iconv' descriptor is like a file descriptor as for every use a\n"
Packit 6c4009
"     new descriptor must be created.  The descriptor does not stand for\n"
Packit 6c4009
"     all of the conversions from FROMSET to TOSET.\n"
Packit 6c4009
"\n"
Packit 6c4009
"     The GNU C library implementation of `iconv_open' has one\n"
Packit 6c4009
"     significant extension to other implementations.  To ease the\n"
Packit 6c4009
"     extension of the set of available conversions the implementation\n"
Packit 6c4009
"     allows storing the necessary files with data and code in\n"
Packit 6c4009
"     arbitrarily many directories.  How this extension has to be\n"
Packit 6c4009
"     written will be explained below (*note glibc iconv\n"
Packit 6c4009
"     Implementation::).  Here it is only important to say that all\n"
Packit 6c4009
"     directories mentioned in the `GCONV_PATH' environment variable are\n"
Packit 6c4009
"     considered if they contain a file `gconv-modules'.  These\n"
Packit 6c4009
"     directories need not necessarily be created by the system\n"
Packit 6c4009
"     administrator.  In fact, this extension is introduced to help users\n"
Packit 6c4009
"     writing and using their own, new conversions.  Of course this does\n"
Packit 6c4009
"     not work for security reasons in SUID binaries; in this case only\n"
Packit 6c4009
"     the system directory is considered and this normally is\n"
Packit 6c4009
"     `PREFIX/lib/gconv'.  The `GCONV_PATH' environment variable is\n"
Packit 6c4009
"     examined exactly once at the first call of the `iconv_open'\n"
Packit 6c4009
"     function.  Later modifications of the variable have no effect.\n";
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  size_t count = TEST_ROUNDS;
Packit 6c4009
  int result = 0;
Packit 6c4009
Packit 6c4009
  mtrace ();
Packit 6c4009
Packit 6c4009
  /* Just a seed.  */
Packit 6c4009
  srandom (TEST_ROUNDS);
Packit 6c4009
Packit 6c4009
  while (count--)
Packit 6c4009
    {
Packit 6c4009
      int idx = random () % nmodules;
Packit 6c4009
Packit 6c4009
      if (modules[idx].state == unloaded)
Packit 6c4009
	{
Packit 6c4009
	  char outbuf[10000];
Packit 6c4009
	  char *inptr = (char *) inbuf;
Packit 6c4009
	  size_t insize = sizeof (inbuf) - 1;
Packit 6c4009
	  char *outptr = outbuf;
Packit 6c4009
	  size_t outsize = sizeof (outbuf);
Packit 6c4009
Packit 6c4009
	  /* Load the module and do the conversion.  */
Packit 6c4009
	  modules[idx].cd = iconv_open ("UTF-8", modules[idx].name);
Packit 6c4009
Packit 6c4009
	  if (modules[idx].cd == (iconv_t) -1)
Packit 6c4009
	    {
Packit 6c4009
	      printf ("opening of %s failed: %m\n", modules[idx].name);
Packit 6c4009
	      result = 1;
Packit 6c4009
	      break;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  modules[idx].state = loaded;
Packit 6c4009
Packit 6c4009
	  /* Now a simple test.  */
Packit 6c4009
	  if (iconv (modules[idx].cd, &inptr, &insize, &outptr, &outsize) != 0
Packit 6c4009
	      || *inptr != '\0')
Packit 6c4009
	    {
Packit 6c4009
	      printf ("conversion with %s failed\n", modules[idx].name);
Packit 6c4009
	      result = 1;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  /* Unload the module.  */
Packit 6c4009
	  if (iconv_close (modules[idx].cd) != 0)
Packit 6c4009
	    {
Packit 6c4009
	      printf ("closing of %s failed: %m\n", modules[idx].name);
Packit 6c4009
	      result = 1;
Packit 6c4009
	      break;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  modules[idx].state = unloaded;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  for (count = 0; count < nmodules; ++count)
Packit 6c4009
    if (modules[count].state == loaded && iconv_close (modules[count].cd) != 0)
Packit 6c4009
      {
Packit 6c4009
	printf ("closing of %s failed: %m\n", modules[count].name);
Packit 6c4009
	result = 1;
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TIMEOUT 30
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"