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