Blame iconvdata/bug-iconv12.c

Packit 6c4009
/* bug 19727: Testing UTF conversions with UTF16 surrogates as input.
Packit 6c4009
   Copyright (C) 2016-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
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 <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <inttypes.h>
Packit 6c4009
#include <iconv.h>
Packit 6c4009
#include <byteswap.h>
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
run_conversion (const char *from, const char *to, char *inbuf, size_t inbuflen,
Packit 6c4009
		int exp_errno, int line)
Packit 6c4009
{
Packit 6c4009
  char outbuf[16];
Packit 6c4009
  iconv_t cd;
Packit 6c4009
  char *inptr;
Packit 6c4009
  size_t inlen;
Packit 6c4009
  char *outptr;
Packit 6c4009
  size_t outlen;
Packit 6c4009
  size_t n;
Packit 6c4009
  int e;
Packit 6c4009
  int fails = 0;
Packit 6c4009
Packit 6c4009
  cd = iconv_open (to, from);
Packit 6c4009
  if (cd == (iconv_t) -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("line %d: cannot convert from %s to %s: %m\n", line, from, to);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  inptr = (char *) inbuf;
Packit 6c4009
  inlen = inbuflen;
Packit 6c4009
  outptr = outbuf;
Packit 6c4009
  outlen = sizeof (outbuf);
Packit 6c4009
Packit 6c4009
  errno = 0;
Packit 6c4009
  n = iconv (cd, &inptr, &inlen, &outptr, &outlen);
Packit 6c4009
  e = errno;
Packit 6c4009
Packit 6c4009
  if (exp_errno == 0)
Packit 6c4009
    {
Packit 6c4009
      if (n == (size_t) -1)
Packit 6c4009
	{
Packit 6c4009
	  puts ("n should be >= 0, but n == -1");
Packit 6c4009
	  fails ++;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (e != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("errno should be 0: 'Success', but errno == %d: '%s'\n"
Packit 6c4009
		  , e, strerror(e));
Packit 6c4009
	  fails ++;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      if (n != (size_t) -1)
Packit 6c4009
	{
Packit 6c4009
	  printf ("n should be -1, but n == %zd\n", n);
Packit 6c4009
	  fails ++;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (e != exp_errno)
Packit 6c4009
	{
Packit 6c4009
	  printf ("errno should be %d: '%s', but errno == %d: '%s'\n"
Packit 6c4009
		  , exp_errno, strerror (exp_errno), e, strerror (e));
Packit 6c4009
	  fails ++;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  iconv_close (cd);
Packit 6c4009
Packit 6c4009
  if (fails > 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("Errors in line %d while converting %s to %s.\n\n"
Packit 6c4009
	      , line, from, to);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return fails;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  int fails = 0;
Packit 6c4009
  char buf[4];
Packit 6c4009
Packit 6c4009
  /* This test runs iconv() with UTF character in range of an UTF16 surrogate.
Packit 6c4009
     UTF-16 high surrogate is in range 0xD800..0xDBFF and
Packit 6c4009
     UTF-16 low surrogate is in range 0xDC00..0xDFFF.
Packit 6c4009
     Converting from or to UTF-xx has to report errors in those cases.
Packit 6c4009
     In UTF-16, surrogate pairs with a high surrogate in front of a low
Packit 6c4009
     surrogate is valid.  */
Packit 6c4009
Packit 6c4009
  /* Use RUN_UCS4_UTF32_INPUT to test conversion ...
Packit 6c4009
Packit 6c4009
     ... from INTERNAL to UTF-xx[LE|BE]:
Packit 6c4009
     Converting from UCS4 to UTF-xx[LE|BE] first converts UCS4 to INTERNAL
Packit 6c4009
     without checking for UTF-16 surrogate values
Packit 6c4009
     and then converts from INTERNAL to UTF-xx[LE|BE].
Packit 6c4009
     The latter conversion has to report an error in those cases.
Packit 6c4009
Packit 6c4009
     ... from UTF-32[LE|BE] to INTERNAL:
Packit 6c4009
     Converting directly from UTF-32LE to UTF-8|16 is needed,
Packit 6c4009
     because e.g. s390x has iconv-modules which converts directly.  */
Packit 6c4009
#define RUN_UCS4_UTF32_INPUT(b0, b1, b2, b3, err, line)			\
Packit 6c4009
  buf[0] = b0;								\
Packit 6c4009
  buf[1] = b1;								\
Packit 6c4009
  buf[2] = b2;								\
Packit 6c4009
  buf[3] = b3;								\
Packit 6c4009
  fails += run_conversion ("UCS4", "UTF-8", buf, 4, err, line);		\
Packit 6c4009
  fails += run_conversion ("UCS4", "UTF-16LE", buf, 4, err, line);	\
Packit 6c4009
  fails += run_conversion ("UCS4", "UTF-16BE", buf, 4, err, line);	\
Packit 6c4009
  fails += run_conversion ("UCS4", "UTF-32LE", buf, 4, err, line);	\
Packit 6c4009
  fails += run_conversion ("UCS4", "UTF-32BE", buf, 4, err, line);	\
Packit 6c4009
  fails += run_conversion ("UTF-32BE", "WCHAR_T", buf, 4, err, line);	\
Packit 6c4009
  fails += run_conversion ("UTF-32BE", "UTF-8", buf, 4, err, line);	\
Packit 6c4009
  fails += run_conversion ("UTF-32BE", "UTF-16LE", buf, 4, err, line);	\
Packit 6c4009
  fails += run_conversion ("UTF-32BE", "UTF-16BE", buf, 4, err, line);	\
Packit 6c4009
  buf[0] = b3;								\
Packit 6c4009
  buf[1] = b2;								\
Packit 6c4009
  buf[2] = b1;								\
Packit 6c4009
  buf[3] = b0;								\
Packit 6c4009
  fails += run_conversion ("UTF-32LE", "WCHAR_T", buf, 4, err, line);	\
Packit 6c4009
  fails += run_conversion ("UTF-32LE", "UTF-8", buf, 4, err, line);	\
Packit 6c4009
  fails += run_conversion ("UTF-32LE", "UTF-16LE", buf, 4, err, line);	\
Packit 6c4009
  fails += run_conversion ("UTF-32LE", "UTF-16BE", buf, 4, err, line);
Packit 6c4009
Packit 6c4009
  /* Use UCS4/UTF32 input of 0xD7FF.  */
Packit 6c4009
  RUN_UCS4_UTF32_INPUT (0x0, 0x0, 0xD7, 0xFF, 0, __LINE__);
Packit 6c4009
Packit 6c4009
  /* Use UCS4/UTF32 input of 0xD800.  */
Packit 6c4009
  RUN_UCS4_UTF32_INPUT (0x0, 0x0, 0xD8, 0x00, EILSEQ, __LINE__);
Packit 6c4009
Packit 6c4009
  /* Use UCS4/UTF32 input of 0xDBFF.  */
Packit 6c4009
  RUN_UCS4_UTF32_INPUT (0x0, 0x0, 0xDB, 0xFF, EILSEQ, __LINE__);
Packit 6c4009
Packit 6c4009
  /* Use UCS4/UTF32 input of 0xDC00.  */
Packit 6c4009
  RUN_UCS4_UTF32_INPUT (0x0, 0x0, 0xDC, 0x00, EILSEQ, __LINE__);
Packit 6c4009
Packit 6c4009
  /* Use UCS4/UTF32 input of 0xDFFF.  */
Packit 6c4009
  RUN_UCS4_UTF32_INPUT (0x0, 0x0, 0xDF, 0xFF, EILSEQ, __LINE__);
Packit 6c4009
Packit 6c4009
  /* Use UCS4/UTF32 input of 0xE000.  */
Packit 6c4009
  RUN_UCS4_UTF32_INPUT (0x0, 0x0, 0xE0, 0x00, 0, __LINE__);
Packit 6c4009
Packit 6c4009
Packit 6c4009
  /* Use RUN_UTF16_INPUT to test conversion from UTF16[LE|BE] to INTERNAL.
Packit 6c4009
     Converting directly from UTF-16 to UTF-8|32 is needed,
Packit 6c4009
     because e.g. s390x has iconv-modules which converts directly.
Packit 6c4009
     Use len == 2 or 4 to specify one or two UTF-16 characters.  */
Packit 6c4009
#define RUN_UTF16_INPUT(b0, b1, b2, b3, len, err, line)			\
Packit 6c4009
  buf[0] = b0;								\
Packit 6c4009
  buf[1] = b1;								\
Packit 6c4009
  buf[2] = b2;								\
Packit 6c4009
  buf[3] = b3;								\
Packit 6c4009
  fails += run_conversion ("UTF-16BE", "WCHAR_T", buf, len, err, line);	\
Packit 6c4009
  fails += run_conversion ("UTF-16BE", "UTF-8", buf, len, err, line);	\
Packit 6c4009
  fails += run_conversion ("UTF-16BE", "UTF-32LE", buf, len, err, line); \
Packit 6c4009
  fails += run_conversion ("UTF-16BE", "UTF-32BE", buf, len, err, line); \
Packit 6c4009
  buf[0] = b1;								\
Packit 6c4009
  buf[1] = b0;								\
Packit 6c4009
  buf[2] = b3;								\
Packit 6c4009
  buf[3] = b2;								\
Packit 6c4009
  fails += run_conversion ("UTF-16LE", "WCHAR_T", buf, len, err, line);	\
Packit 6c4009
  fails += run_conversion ("UTF-16LE", "UTF-8", buf, len, err, line);	\
Packit 6c4009
  fails += run_conversion ("UTF-16LE", "UTF-32LE", buf, len, err, line); \
Packit 6c4009
  fails += run_conversion ("UTF-16LE", "UTF-32BE", buf, len, err, line);
Packit 6c4009
Packit 6c4009
  /* Use UTF16 input of 0xD7FF.  */
Packit 6c4009
  RUN_UTF16_INPUT (0xD7, 0xFF, 0xD7, 0xFF, 4, 0, __LINE__);
Packit 6c4009
Packit 6c4009
  /* Use [single] UTF16 high surrogate 0xD800 [with a valid character behind].
Packit 6c4009
     And check an UTF16 surrogate pair [without valid low surrogate].  */
Packit 6c4009
  RUN_UTF16_INPUT (0xD8, 0x0, 0x0, 0x0, 2, EINVAL, __LINE__);
Packit 6c4009
  RUN_UTF16_INPUT (0xD8, 0x0, 0xD7, 0xFF, 4, EILSEQ, __LINE__);
Packit 6c4009
  RUN_UTF16_INPUT (0xD8, 0x0, 0xD8, 0x0, 4, EILSEQ, __LINE__);
Packit 6c4009
  RUN_UTF16_INPUT (0xD8, 0x0, 0xE0, 0x0, 4, EILSEQ, __LINE__);
Packit 6c4009
  RUN_UTF16_INPUT (0xD8, 0x0, 0xDC, 0x0, 4, 0, __LINE__);
Packit 6c4009
Packit 6c4009
  /* Use [single] UTF16 high surrogate 0xDBFF [with a valid character behind].
Packit 6c4009
     And check an UTF16 surrogate pair [without valid low surrogate].  */
Packit 6c4009
  RUN_UTF16_INPUT (0xDB, 0xFF, 0x0, 0x0, 2, EINVAL, __LINE__);
Packit 6c4009
  RUN_UTF16_INPUT (0xDB, 0xFF, 0xD7, 0xFF, 4, EILSEQ, __LINE__);
Packit 6c4009
  RUN_UTF16_INPUT (0xDB, 0xFF, 0xDB, 0xFF, 4, EILSEQ, __LINE__);
Packit 6c4009
  RUN_UTF16_INPUT (0xDB, 0xFF, 0xE0, 0x0, 4, EILSEQ, __LINE__);
Packit 6c4009
  RUN_UTF16_INPUT (0xDB, 0xFF, 0xDF, 0xFF, 4, 0, __LINE__);
Packit 6c4009
Packit 6c4009
  /* Use single UTF16 low surrogate 0xDC00 [with a valid character behind].
Packit 6c4009
     And check an UTF16 surrogate pair [without valid high surrogate].   */
Packit 6c4009
  RUN_UTF16_INPUT (0xDC, 0x0, 0x0, 0x0, 2, EILSEQ, __LINE__);
Packit 6c4009
  RUN_UTF16_INPUT (0xDC, 0x0, 0xD7, 0xFF, 4, EILSEQ, __LINE__);
Packit 6c4009
  RUN_UTF16_INPUT (0xD8, 0x0, 0xDC, 0x0, 4, 0, __LINE__);
Packit 6c4009
  RUN_UTF16_INPUT (0xD7, 0xFF, 0xDC, 0x0, 4, EILSEQ, __LINE__);
Packit 6c4009
  RUN_UTF16_INPUT (0xDC, 0x0, 0xDC, 0x0, 4, EILSEQ, __LINE__);
Packit 6c4009
  RUN_UTF16_INPUT (0xE0, 0x0, 0xDC, 0x0, 4, EILSEQ, __LINE__);
Packit 6c4009
Packit 6c4009
  /* Use single UTF16 low surrogate 0xDFFF [with a valid character behind].
Packit 6c4009
     And check an UTF16 surrogate pair [without valid high surrogate].   */
Packit 6c4009
  RUN_UTF16_INPUT (0xDF, 0xFF, 0x0, 0x0, 2, EILSEQ, __LINE__);
Packit 6c4009
  RUN_UTF16_INPUT (0xDF, 0xFF, 0xD7, 0xFF, 4, EILSEQ, __LINE__);
Packit 6c4009
  RUN_UTF16_INPUT (0xDB, 0xFF, 0xDF, 0xFF, 4, 0, __LINE__);
Packit 6c4009
  RUN_UTF16_INPUT (0xD7, 0xFF, 0xDF, 0xFF, 4, EILSEQ, __LINE__);
Packit 6c4009
  RUN_UTF16_INPUT (0xDF, 0xFF, 0xDF, 0xFF, 4, EILSEQ, __LINE__);
Packit 6c4009
  RUN_UTF16_INPUT (0xE0, 0x0, 0xDF, 0xFF, 4, EILSEQ, __LINE__);
Packit 6c4009
Packit 6c4009
  /* Use UCS4/UTF32 input of 0xE000.  */
Packit 6c4009
  RUN_UTF16_INPUT (0xE0, 0x0, 0xE0, 0x0, 4, 0, __LINE__);
Packit 6c4009
Packit 6c4009
Packit 6c4009
  /* Use RUN_UTF8_3BYTE_INPUT to test conversion from UTF-8 to INTERNAL.
Packit 6c4009
     Converting directly from UTF-8 to UTF-16|32 is needed,
Packit 6c4009
     because e.g. s390x has iconv-modules which converts directly.  */
Packit 6c4009
#define RUN_UTF8_3BYTE_INPUT(b0, b1, b2, err, line)			\
Packit 6c4009
  buf[0] = b0;								\
Packit 6c4009
  buf[1] = b1;								\
Packit 6c4009
  buf[2] = b2;								\
Packit 6c4009
  fails += run_conversion ("UTF-8", "WCHAR_T", buf, 3, err, line);	\
Packit 6c4009
  fails += run_conversion ("UTF-8", "UTF-16LE", buf, 3, err, line);	\
Packit 6c4009
  fails += run_conversion ("UTF-8", "UTF-16BE", buf, 3, err, line);	\
Packit 6c4009
  fails += run_conversion ("UTF-8", "UTF-32LE", buf, 3, err, line);	\
Packit 6c4009
  fails += run_conversion ("UTF-8", "UTF-32BE", buf, 3, err, line);
Packit 6c4009
Packit 6c4009
  /* Use UTF-8 input of 0xD7FF.  */
Packit 6c4009
  RUN_UTF8_3BYTE_INPUT (0xED, 0x9F, 0xBF, 0, __LINE__);
Packit 6c4009
Packit 6c4009
  /* Use UTF-8 input of 0xD800.  */
Packit 6c4009
  RUN_UTF8_3BYTE_INPUT (0xED, 0xA0, 0x80, EILSEQ, __LINE__);
Packit 6c4009
Packit 6c4009
  /* Use UTF-8 input of 0xDBFF.  */
Packit 6c4009
  RUN_UTF8_3BYTE_INPUT (0xED, 0xAF, 0xBF, EILSEQ, __LINE__);
Packit 6c4009
Packit 6c4009
  /* Use UTF-8 input of 0xDC00.  */
Packit 6c4009
  RUN_UTF8_3BYTE_INPUT (0xED, 0xB0, 0x80, EILSEQ, __LINE__);
Packit 6c4009
Packit 6c4009
  /* Use UTF-8 input of 0xDFFF.  */
Packit 6c4009
  RUN_UTF8_3BYTE_INPUT (0xED, 0xBF, 0xBF, EILSEQ, __LINE__);
Packit 6c4009
Packit 6c4009
  /* Use UTF-8 input of 0xF000.  */
Packit 6c4009
  RUN_UTF8_3BYTE_INPUT (0xEF, 0x80, 0x80, 0, __LINE__);
Packit 6c4009
Packit 6c4009
  return fails > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"