xref: /illumos-gate/usr/src/cmd/ast/libshell/common/tests/sun_solaris_locale_misc.sh (revision b30d193948be5a7794d7ae3ba0ed9c2f72c88e0f)
1*b30d1939SAndy Fiddaman#
2*b30d1939SAndy Fiddaman# CDDL HEADER START
3*b30d1939SAndy Fiddaman#
4*b30d1939SAndy Fiddaman# The contents of this file are subject to the terms of the
5*b30d1939SAndy Fiddaman# Common Development and Distribution License (the "License").
6*b30d1939SAndy Fiddaman# You may not use this file except in compliance with the License.
7*b30d1939SAndy Fiddaman#
8*b30d1939SAndy Fiddaman# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*b30d1939SAndy Fiddaman# or http://www.opensolaris.org/os/licensing.
10*b30d1939SAndy Fiddaman# See the License for the specific language governing permissions
11*b30d1939SAndy Fiddaman# and limitations under the License.
12*b30d1939SAndy Fiddaman#
13*b30d1939SAndy Fiddaman# When distributing Covered Code, include this CDDL HEADER in each
14*b30d1939SAndy Fiddaman# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*b30d1939SAndy Fiddaman# If applicable, add the following below this CDDL HEADER, with the
16*b30d1939SAndy Fiddaman# fields enclosed by brackets "[]" replaced with your own identifying
17*b30d1939SAndy Fiddaman# information: Portions Copyright [yyyy] [name of copyright owner]
18*b30d1939SAndy Fiddaman#
19*b30d1939SAndy Fiddaman# CDDL HEADER END
20*b30d1939SAndy Fiddaman#
21*b30d1939SAndy Fiddaman
22*b30d1939SAndy Fiddaman#
23*b30d1939SAndy Fiddaman# Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24*b30d1939SAndy Fiddaman#
25*b30d1939SAndy Fiddaman
26*b30d1939SAndy Fiddaman#
27*b30d1939SAndy Fiddaman# This test module contains misc l10n tests
28*b30d1939SAndy Fiddaman#
29*b30d1939SAndy Fiddaman#
30*b30d1939SAndy Fiddaman
31*b30d1939SAndy Fiddaman# test setup
32*b30d1939SAndy Fiddamanfunction err_exit
33*b30d1939SAndy Fiddaman{
34*b30d1939SAndy Fiddaman	print -u2 -n "\t"
35*b30d1939SAndy Fiddaman	print -u2 -r ${Command}[$1]: "${@:2}"
36*b30d1939SAndy Fiddaman	(( Errors < 127 && Errors++ ))
37*b30d1939SAndy Fiddaman}
38*b30d1939SAndy Fiddamanalias err_exit='err_exit $LINENO'
39*b30d1939SAndy Fiddaman
40*b30d1939SAndy Fiddamanset -o nounset
41*b30d1939SAndy FiddamanCommand=${0##*/}
42*b30d1939SAndy Fiddamaninteger Errors=0
43*b30d1939SAndy Fiddaman
44*b30d1939SAndy Fiddamantypeset ocwd
45*b30d1939SAndy Fiddamantypeset tmpdir
46*b30d1939SAndy Fiddaman
47*b30d1939SAndy Fiddaman# create temporary test directory
48*b30d1939SAndy Fiddamanocwd="$PWD"
49*b30d1939SAndy Fiddamantmpdir="$(mktemp -t -d "test_sun_solaris_locale_misc.XXXXXXXX")" || err_exit "Cannot create temporary directory"
50*b30d1939SAndy Fiddaman
51*b30d1939SAndy Fiddamancd "${tmpdir}" || { err_exit "cd ${tmpdir} failed." ; exit $((Errors)) ; }
52*b30d1939SAndy Fiddaman
53*b30d1939SAndy Fiddaman#
54*b30d1939SAndy Fiddaman# utility functions
55*b30d1939SAndy Fiddaman#
56*b30d1939SAndy Fiddaman
57*b30d1939SAndy Fiddamanfunction string_has_multibyte_characters
58*b30d1939SAndy Fiddaman{
59*b30d1939SAndy Fiddaman	typeset str="$1"
60*b30d1939SAndy Fiddaman	integer bytecount
61*b30d1939SAndy Fiddaman	integer mbcharactercount
62*b30d1939SAndy Fiddaman
63*b30d1939SAndy Fiddaman	(( mbcharactercount=$(LC_ALL="en_US.UTF-8" wc -C <<<"${str}") ))
64*b30d1939SAndy Fiddaman	(( bytecount=$(wc -c <<<"${str}") ))
65*b30d1939SAndy Fiddaman
66*b30d1939SAndy Fiddaman	(( bytecount != mbcharactercount )) && return 0
67*b30d1939SAndy Fiddaman	return 1
68*b30d1939SAndy Fiddaman}
69*b30d1939SAndy Fiddaman
70*b30d1939SAndy Fiddaman#
71*b30d1939SAndy Fiddaman# test functions
72*b30d1939SAndy Fiddaman#
73*b30d1939SAndy Fiddaman
74*b30d1939SAndy Fiddaman# test whether LC_ALL correctly overrides LC_MESSAGES in the choice of the system message
75*b30d1939SAndy Fiddaman# catalog
76*b30d1939SAndy Fiddaman# 1. This test assumes that the machine has ko_KR.UTF-8 + matching message catalogs installed
77*b30d1939SAndy Fiddaman# 2. We run this test in a |fork()|'ed subshell to isolate it from the other tests
78*b30d1939SAndy Fiddamanfunction test_lc_all_override1
79*b30d1939SAndy Fiddaman{
80*b30d1939SAndy Fiddaman	typeset out
81*b30d1939SAndy Fiddaman
82*b30d1939SAndy Fiddaman	(
83*b30d1939SAndy Fiddaman		ulimit -c 0 # force ksh93 to |fork()| for this subshell
84*b30d1939SAndy Fiddaman
85*b30d1939SAndy Fiddaman		unset ${!LC_*} LANG
86*b30d1939SAndy Fiddaman		#export LANG=en_US.UTF-8
87*b30d1939SAndy Fiddaman		export LC_ALL="en_US.UTF-8"
88*b30d1939SAndy Fiddaman
89*b30d1939SAndy Fiddaman		integer ch_val
90*b30d1939SAndy Fiddaman		integer korean_count=0
91*b30d1939SAndy Fiddaman		${SHELL} -c 'LC_MESSAGES=C ${SHELL} -c "cd no_dir_llkk ; export LC_ALL="ko_KR.UTF-8" ; cd "no_dir_ooo" ; true"' >"out" 2>&1  || err_exit "Test shell failed with non-zero exit code $?"
92*b30d1939SAndy Fiddaman
93*b30d1939SAndy Fiddaman		while read -N1 c ; do
94*b30d1939SAndy Fiddaman			(( ch_val='${c} ))
95*b30d1939SAndy Fiddaman
96*b30d1939SAndy Fiddaman			(( ch_val >= 0xac00 && ch_val <= 0xdfff )) && (( korean_count++ ))
97*b30d1939SAndy Fiddaman		done <"out"
98*b30d1939SAndy Fiddaman
99*b30d1939SAndy Fiddaman		# Solaris 11/B110 returns 13 characters for this test
100*b30d1939SAndy Fiddaman		(( korean_count >= 10 )) || err_exit "Expected at least 10 korean characters, got ${korean_count}"
101*b30d1939SAndy Fiddaman
102*b30d1939SAndy Fiddaman		rm "out"
103*b30d1939SAndy Fiddaman
104*b30d1939SAndy Fiddaman		exit $((Errors))
105*b30d1939SAndy Fiddaman	)
106*b30d1939SAndy Fiddaman	(( Errors += $? ))
107*b30d1939SAndy Fiddaman	return 0
108*b30d1939SAndy Fiddaman}
109*b30d1939SAndy Fiddaman
110*b30d1939SAndy Fiddaman# test whether the shell internally selects the correct message catalogs
111*b30d1939SAndy Fiddaman# when the value of LC_* or LANG is restored to a "previous" value (e.g.
112*b30d1939SAndy Fiddaman# subshell, function) or gets "reset" (e.g. unset)
113*b30d1939SAndy Fiddamanfunction test_lc_l10n_scope1
114*b30d1939SAndy Fiddaman{
115*b30d1939SAndy Fiddaman	compound -r -a testgroups=(
116*b30d1939SAndy Fiddaman		(
117*b30d1939SAndy Fiddaman			name="subshell"
118*b30d1939SAndy Fiddaman			typeset -a tests=(
119*b30d1939SAndy Fiddaman				'LC_ALL="C" ;		cd "nosuchdir2" ; (LC_ALL="ja_JP.UTF-8" ;	cd "nosuchdir2") ; cd "nosuchdir2" ; true'
120*b30d1939SAndy Fiddaman				'LC_MESSAGES="C" ;	cd "nosuchdir2" ; (LC_MESSAGES="ja_JP.UTF-8" ;	cd "nosuchdir2") ; cd "nosuchdir2" ; true'
121*b30d1939SAndy Fiddaman				'LANG="C" ;		cd "nosuchdir2" ; (LANG="ja_JP.UTF-8" ;		cd "nosuchdir2") ; cd "nosuchdir2" ; true'
122*b30d1939SAndy Fiddaman			)
123*b30d1939SAndy Fiddaman		)
124*b30d1939SAndy Fiddaman		(
125*b30d1939SAndy Fiddaman			name="unset"
126*b30d1939SAndy Fiddaman			typeset -a tests=(
127*b30d1939SAndy Fiddaman				'LC_ALL="C" ;		cd "nosuchdir2" ; LC_ALL="ja_JP.UTF-8" ;	cd "nosuchdir2" ; unset LC_ALL ;	cd "nosuchdir2" ; true'
128*b30d1939SAndy Fiddaman				'LC_MESSAGES="C" ;	cd "nosuchdir2" ; LC_MESSAGES="ja_JP.UTF-8" ;	cd "nosuchdir2" ; unset LC_MESSAGES ;	cd "nosuchdir2" ; true'
129*b30d1939SAndy Fiddaman				'LANG="C" ;		cd "nosuchdir2" ; LANG="ja_JP.UTF-8" ;		cd "nosuchdir2" ; unset LANG ;		cd "nosuchdir2" ; true'
130*b30d1939SAndy Fiddaman			)
131*b30d1939SAndy Fiddaman		)
132*b30d1939SAndy Fiddaman		(
133*b30d1939SAndy Fiddaman			name="empty LC_xxx"
134*b30d1939SAndy Fiddaman			typeset -a tests=(
135*b30d1939SAndy Fiddaman				'LC_ALL="C" ;		cd "nosuchdir2" ; LC_ALL="ja_JP.UTF-8" ;	cd "nosuchdir2" ; LC_ALL="" ;		cd "nosuchdir2" ; true'
136*b30d1939SAndy Fiddaman				'LC_MESSAGES="C" ;	cd "nosuchdir2" ; LC_MESSAGES="ja_JP.UTF-8" ;	cd "nosuchdir2" ; LC_MESSAGES="" ;	cd "nosuchdir2" ; true'
137*b30d1939SAndy Fiddaman				'LANG="C" ;		cd "nosuchdir2" ; LANG="ja_JP.UTF-8" ;		cd "nosuchdir2" ; LANG="" ;		cd "nosuchdir2" ; true'
138*b30d1939SAndy Fiddaman			)
139*b30d1939SAndy Fiddaman		)
140*b30d1939SAndy Fiddaman		(
141*b30d1939SAndy Fiddaman			name="function"
142*b30d1939SAndy Fiddaman			typeset -a tests=(
143*b30d1939SAndy Fiddaman				'LC_ALL="C" ;		cd "nosuchdir2" ; function x { typeset LC_ALL="ja_JP.UTF-8" ;		cd "nosuchdir2" ; } ; x ; cd "nosuchdir2" ; true'
144*b30d1939SAndy Fiddaman				'LC_MESSAGES="C" ;	cd "nosuchdir2" ; function x { typeset LC_MESSAGES="ja_JP.UTF-8" ;	cd "nosuchdir2" ; } ; x ; cd "nosuchdir2" ; true'
145*b30d1939SAndy Fiddaman				'LANG="C" ;		cd "nosuchdir2" ; function x { typeset LANG="ja_JP.UTF-8" ;		cd "nosuchdir2" ; } ; x ; cd "nosuchdir2" ; true'
146*b30d1939SAndy Fiddaman			)
147*b30d1939SAndy Fiddaman		)
148*b30d1939SAndy Fiddaman	)
149*b30d1939SAndy Fiddaman
150*b30d1939SAndy Fiddaman
151*b30d1939SAndy Fiddaman	typeset tgi ti out2
152*b30d1939SAndy Fiddaman
153*b30d1939SAndy Fiddaman	for tgi in "${!testgroups[@]}" ; do
154*b30d1939SAndy Fiddaman		nameref tg=testgroups[${tgi}]
155*b30d1939SAndy Fiddaman
156*b30d1939SAndy Fiddaman		for ti in "${!tg.tests[@]}" ; do
157*b30d1939SAndy Fiddaman			nameref ts=tg.tests[${ti}]
158*b30d1939SAndy Fiddaman
159*b30d1939SAndy Fiddaman			${SHELL} -c "unset LANG \${!LC_*} ; ${SHELL} -c \"${ts}\"" >out 2>&1 || err_exit "test returned non-zero exit code $?"
160*b30d1939SAndy Fiddaman			out2="${
161*b30d1939SAndy Fiddaman				while read -r line ; do
162*b30d1939SAndy Fiddaman					string_has_multibyte_characters "${line}" && print -n "A" || print -n "_"
163*b30d1939SAndy Fiddaman				done <"out"
164*b30d1939SAndy Fiddaman				print ""
165*b30d1939SAndy Fiddaman			}"
166*b30d1939SAndy Fiddaman			if [[ "${out2}" != '_A_' ]] ; then
167*b30d1939SAndy Fiddaman				err_exit "test '${tg.name}'/'$ts' failed: Expected '_A_', got '${out2}'"
168*b30d1939SAndy Fiddaman				#cat out
169*b30d1939SAndy Fiddaman			fi
170*b30d1939SAndy Fiddaman		done
171*b30d1939SAndy Fiddaman	done
172*b30d1939SAndy Fiddaman
173*b30d1939SAndy Fiddaman	rm "out"
174*b30d1939SAndy Fiddaman
175*b30d1939SAndy Fiddaman	return 0
176*b30d1939SAndy Fiddaman}
177*b30d1939SAndy Fiddaman
178*b30d1939SAndy Fiddaman
179*b30d1939SAndy Fiddaman# run tests
180*b30d1939SAndy Fiddamantest_lc_all_override1
181*b30d1939SAndy Fiddamantest_lc_l10n_scope1
182*b30d1939SAndy Fiddaman
183*b30d1939SAndy Fiddaman
184*b30d1939SAndy Fiddamancd "${ocwd}"
185*b30d1939SAndy Fiddamanrmdir "${tmpdir}" || err_exit "Cannot remove temporary directory ${tmpdir}".
186*b30d1939SAndy Fiddaman
187*b30d1939SAndy Fiddaman# tests done
188*b30d1939SAndy Fiddamanexit $((Errors))
189