1#!/bin/sh -f
2# Run available iconv(1) tests.
3# Copyright (C) 1998-2022 Free Software Foundation, Inc.
4# This file is part of the GNU C Library.
5
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.
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
14# Lesser General Public License for more details.
15
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/>.
19
20set -e
21
22codir=$1
23test_wrapper_env="$2"
24run_program_env="$3"
25
26# We use always the same temporary file.
27temp1=$codir/iconvdata/iconv-test.xxx
28temp2=$codir/iconvdata/iconv-test.yyy
29
30trap "rm -f $temp1 $temp2" 1 2 3 15
31
32# We have to have some directories in the library path.
33LIBPATH=$codir:$codir/iconvdata
34
35# How the start the iconv(1) program.
36ICONV='$codir/elf/ld.so --library-path $LIBPATH --inhibit-rpath ${from}.so \
37       $codir/iconv/iconv_prog'
38ICONV="$test_wrapper_env $run_program_env $ICONV"
39
40# Which echo?
41if (echo "testing\c"; echo 1,2,3) | grep c >/dev/null; then
42  ac_n=-n ac_c= ac_t=
43else
44  ac_n= ac_c='\c' ac_t=
45fi
46
47# We read the file named TESTS.  All non-empty lines not starting with
48# `#' are interpreted as commands.
49failed=0
50while read from to subset targets; do
51  # Ignore empty and comment lines.
52  if test -z "$subset" || test "$from" = '#'; then continue; fi
53
54  # Expand the variables now.
55  PROG=`eval echo $ICONV`
56
57  if test -n "$targets"; then
58    for t in $targets; do
59      if test -f testdata/$from; then
60	echo $ac_n "   test data: $from -> $t $ac_c"
61	$PROG -f $from -t $t testdata/$from < /dev/null > $temp1 ||
62	  { if test $? -gt 128; then exit 1; fi
63	    echo "FAILED"; failed=1; continue; }
64	echo $ac_n "OK$ac_c"
65	if test -s testdata/$from..$t; then
66	  LC_ALL=C cmp $temp1 testdata/$from..$t > /dev/null 2>&1 ||
67	    { echo "/FAILED"; failed=1; continue; }
68	  echo $ac_n "/OK$ac_c"
69	fi
70	echo $ac_n " -> $from $ac_c"
71	$PROG -f $t -t $to -o $temp2 $temp1 < /dev/null ||
72	  { if test $? -gt 128; then exit 1; fi
73	    echo "FAILED"; failed=1; continue; }
74	echo $ac_n "OK$ac_c"
75	test -s $temp1 &&
76	LC_ALL=C cmp testdata/$from $temp2 > /dev/null 2>&1 ||
77	  { echo "/FAILED"; failed=1; continue; }
78	echo "/OK"
79	rm -f $temp1 $temp2
80      fi
81
82      # Now test some bigger text, entirely in ASCII.  If ASCII is no subset
83      # of the coded character set we convert the text to this coded character
84      # set.  Otherwise we convert to all the TARGETS.
85      if test $subset = Y; then
86	echo $ac_n "      suntzu: $from -> $t -> $to $ac_c"
87	$PROG -f $from -t $t testdata/suntzus < /dev/null |
88	$PROG -f $t -t $to > $temp1 ||
89	  { if test $? -gt 128; then exit 1; fi
90	    echo "FAILED"; failed=1; continue; }
91	echo $ac_n "OK$ac_c"
92	LC_ALL=C cmp testdata/suntzus $temp1 ||
93	  { echo "/FAILED"; failed=1; continue; }
94	echo "/OK"
95      fi
96      rm -f $temp1
97
98      # And tests where iconv(1) has to handle charmaps.
99      if test "$t" = UTF8; then tc=UTF-8; else tc="$t"; fi
100      if test -f ../localedata/charmaps/$from &&
101         test -f ../localedata/charmaps/$tc &&
102	 test -f testdata/$from &&
103	 ! grep '<U....><U....>' ../localedata/charmaps/$from > /dev/null; then
104	echo $ac_n "test charmap: $from -> $t $ac_c"
105	$PROG -f ../localedata/charmaps/$from -t ../localedata/charmaps/$tc \
106	      testdata/$from < /dev/null > $temp1 ||
107	  { if test $? -gt 128; then exit 1; fi
108	    echo "FAILED"; failed=1; continue; }
109	echo $ac_n "OK$ac_c"
110	if test -s testdata/$from..$t; then
111	  LC_ALL=C cmp $temp1 testdata/$from..$t > /dev/null 2>&1 ||
112	    { echo "/FAILED"; failed=1; continue; }
113	  echo $ac_n "/OK$ac_c"
114	fi
115	echo $ac_n " -> $from $ac_c"
116	$PROG -t ../localedata/charmaps/$from -f ../localedata/charmaps/$tc \
117	      -o $temp2 $temp1 < /dev/null ||
118	  { if test $? -gt 128; then exit 1; fi
119	    echo "FAILED"; failed=1; continue; }
120	echo $ac_n "OK$ac_c"
121	test -s $temp1 &&
122	LC_ALL=C cmp testdata/$from $temp2 > /dev/null 2>&1 ||
123	  { echo "/FAILED"; failed=1; continue; }
124	echo "/OK"
125	rm -f $temp1 $temp2
126      fi
127    done
128  fi
129
130  if test "$subset" = N; then
131    echo $ac_n "      suntzu: ASCII -> $to -> ASCII $ac_c"
132    $PROG -f ASCII -t $to testdata/suntzus < /dev/null |
133    $PROG -f $to -t ASCII > $temp1 ||
134      { if test $? -gt 128; then exit 1; fi
135	echo "FAILED"; failed=1; continue; }
136    echo $ac_n "OK$ac_c"
137    LC_ALL=C cmp testdata/suntzus $temp1 ||
138      { echo "/FAILED"; failed=1; continue; }
139    echo "/OK"
140  fi
141done < TESTS
142
143# We read the file named TESTS2.  All non-empty lines not starting with
144# `#' are interpreted as commands.
145while read utf8 from filename; do
146  # Ignore empty and comment lines.
147  if test -z "$filename" || test "$utf8" = '#'; then continue; fi
148
149  # Expand the variables now.
150  PROG=`eval echo $ICONV`
151
152  # Test conversion to the endianness dependent encoding.
153  echo $ac_n "test encoder: $utf8 -> $from $ac_c"
154  $PROG -f $utf8 -t $from < testdata/${filename}..${utf8} > $temp1
155  LC_ALL=C cmp $temp1 testdata/${filename}..${from}.BE > /dev/null 2>&1 ||
156  LC_ALL=C cmp $temp1 testdata/${filename}..${from}.LE > /dev/null 2>&1 ||
157    { echo "/FAILED"; failed=1; continue; }
158  echo "OK"
159
160  # Test conversion from the endianness dependent encoding.
161  echo $ac_n "test decoder: $from -> $utf8 $ac_c"
162  $PROG -f $from -t $utf8 < testdata/${filename}..${from}.BE > $temp1
163  LC_ALL=C cmp $temp1 testdata/${filename}..${utf8} > /dev/null 2>&1 ||
164    { echo "/FAILED"; failed=1; continue; }
165  $PROG -f $from -t $utf8 < testdata/${filename}..${from}.LE > $temp1
166  LC_ALL=C cmp $temp1 testdata/${filename}..${utf8} > /dev/null 2>&1 ||
167    { echo "/FAILED"; failed=1; continue; }
168  echo "OK"
169
170  # Test byte swapping behaviour.
171  echo $ac_n "test non-BOM: ${from}BE -> ${from}LE $ac_c"
172  $PROG -f ${from}BE -t ${from}LE < testdata/${filename}..${from}.BE > $temp1
173  LC_ALL=C cmp $temp1 testdata/${filename}..${from}.LE > /dev/null 2>&1 ||
174    { echo "/FAILED"; failed=1; continue; }
175  echo "OK"
176
177  # Test byte swapping behaviour.
178  echo $ac_n "test non-BOM: ${from}LE -> ${from}BE $ac_c"
179  $PROG -f ${from}LE -t ${from}BE < testdata/${filename}..${from}.LE > $temp1
180  LC_ALL=C cmp $temp1 testdata/${filename}..${from}.BE > /dev/null 2>&1 ||
181    { echo "/FAILED"; failed=1; continue; }
182  echo "OK"
183
184done < TESTS2
185
186# Check for crashes in decoders.
187printf '\016\377\377\377\377\377\377\377' > $temp1
188for from in $iconv_modules ; do
189    echo $ac_n "test decoder $from $ac_c"
190    PROG=`eval echo $ICONV`
191    if $PROG -f $from -t UTF8 < $temp1 >/dev/null 2>&1 ; then
192	: # fall through
193    else
194	status=$?
195	if test $status -gt 1 ; then
196	    echo "/FAILED"
197	    failed=1
198	    continue
199	fi
200    fi
201    echo "OK"
202done
203
204exit $failed
205# Local Variables:
206#  mode:shell-script
207# End:
208