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) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24#
25
26#
27# sun_solaris_getconf.sh - test the ksh93 getconf builtin for compatibility
28# with /usr/bin/getconf
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
44
45# setup
46integer mismatch     # counts mismatches between builtin and external command
47integer getconf_keys # counts tests (paranoid check to make sure the test loop works)
48export PATH=/usr/bin:/bin
49
50# prechecks
51[[ ! -f "/bin/getconf" ]] && err_exit '/bin/getconf not found.'
52[[ ! -x "/bin/getconf" ]] && err_exit '/bin/getconf not executable.'
53
54# Define test functions and store them in a string for repeated usagae
55# (we can't use "functions" (alias "typeset -f") since this does not
56# work in compiled shell scripts)
57typeset -r getconf_test_functions="$(
58cat <<EOF
59function err_exit
60{
61	print -u2 -n "\t"
62	print -u2 -r \${Command}[\$1]: "\${@:2}"
63	(( Errors++ ))
64}
65alias err_exit='err_exit \$LINENO'
66Command=\${0##*/}
67integer Errors=0
68# compare builtin getconf output with /usr/bin/getconf
69function compare_normal
70{
71    mismach=0 getconf_keys=0
72    /usr/bin/getconf -a |
73        while read i ; do
74            (( getconf_keys++ ))
75            t="\${i%:*}"
76
77            a="\$(getconf          "\$t" 2>/dev/null)"
78            b="\$(/usr/bin/getconf "\$t" 2>/dev/null)"
79
80            if [[ "\$a" != "\$b" ]] ; then
81                print -u2 "getconf/normal built mismatch: |\$t|:|\$a| != |\$b|"
82                (( mismatch++ ))
83            fi
84        done
85}
86
87# compare builtin getconf output with /usr/bin/getconf while passing a path argument
88function compare_path
89{
90    mismach=0 getconf_keys=0
91    /usr/bin/getconf -a |
92        while read i ; do
93            (( getconf_keys++ ))
94            t="\${i%:*}"
95
96            a="\$(getconf          "\$t" "/tmp" 2>/dev/null)"
97            b="\$(/usr/bin/getconf "\$t" "/tmp" 2>/dev/null)"
98
99            if [[ "\$a" != "\$b" ]] ; then
100                print -u2 "getconf/path built mismatch: |\$t|:|\$a| != |\$b|"
101                (( mismatch++ ))
102            fi
103        done
104}
105EOF
106)"
107
108print -r -- "$getconf_test_functions" | source /dev/stdin
109
110# future versions of this test should test the following ${PATH}s, too:
111# "/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin" \
112#"/usr/xpg4/bin:/bin:/usr/bin" \
113for i in \
114    "/usr/bin:/bin" \
115    "/bin:/usr/bin"
116do
117    export PATH="${i}"
118
119    ## test whether the getconf builtin is available
120    if [[ "$(builtin | fgrep "/bin/getconf")" == "" ]] ; then
121        err_exit '/bin/getconf not found in the list of builtins.'
122    fi
123
124
125    ## compare "getconf -a" output
126    if [[ "$(getconf -a)" != "$(/usr/bin/getconf -a)" ]] ; then
127        err_exit 'getconf -a output mismatch.'
128    fi
129
130
131    ## check for a key which is only supported by the AST builtin version of getconf:
132    if [[ "$(getconf LIBPREFIX)" != "lib" ]] ; then
133        err_exit 'getconf LIBPREFIX did not return "lib".'
134    fi
135
136
137    ## run normal test
138    compare_normal
139    (( getconf_keys == 0 )) && err_exit "getconf/normal not working (PATH=${PATH})."
140    (( mismatch     >  0 )) && err_exit "getconf/normal test found ${mismatch} differences (PATH=${PATH})."
141
142    # run the same test in a seperate shell
143    # (we explicitly test this because ast-ksh.2007-01-11 picks up /usr/xpg6/bin/getconf
144    # if /usr/xpg6/bin/ comes in ${PATH} before /usr/bin (this happens only of ${PATH}
145    # contains /usr/xpg6/bin before ksh93 is started)).
146    ${SHELL} -c "integer mismatch ; \
147        integer getconf_keys ; \
148        ${getconf_test_functions} ; \
149        compare_normal ;
150        (( getconf_keys == 0 )) && err_exit \"getconf/normal not working (PATH=\${PATH}).\" ; \
151        (( mismatch      > 0 )) && err_exit \"getconf/normal test found \${mismatch} differences (PATH=\${PATH}).\" ; \
152        exit $((Errors))"
153    (( Errors+=$? ))
154
155
156    ## run test with path argument
157    compare_path
158    (( getconf_keys == 0 )) && err_exit "getconf/path not working."
159    (( mismatch      > 0 )) && err_exit "getconf/path test found ${mismatch} differences."
160
161    # run the same test in a seperate shell
162    # (see comment above)
163    ${SHELL} -c "integer mismatch ; \
164        integer getconf_keys ; \
165        ${getconf_test_functions} ; \
166        compare_path ;
167        (( getconf_keys == 0 )) && err_exit \"getconf/normal not working (PATH=\${PATH}).\" ; \
168        (( mismatch      > 0 )) && err_exit \"getconf/normal test found \${mismatch} differences (PATH=\${PATH}).\" ; \
169        exit $((Errors))"
170    (( Errors+=$? ))
171done
172
173
174# tests done
175exit $((Errors))
176