xref: /freebsd/tools/regression/environ/envtest.t (revision 5f4c09dd85bff675e0ca63c55ea3c517e0fddfcc)
1#!/bin/sh
2#
3# Copyright (c) 2007-2008 Sean C. Farley <scf@FreeBSD.org>
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer,
11#    without modification, immediately at the beginning of the file.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27
28
29# Initialization.
30testndx=0
31
32
33# Testing function.
34run_test()
35{
36	lasttest="${@}"
37	result=`./envctl -t "${@}"`
38
39	if [ ${?} -ne 0 ]
40	then
41		echo "Test program failed" >&2
42		exit 1
43	fi
44
45	return
46}
47
48
49# Perform test on results.
50check_result()
51{
52	testndx=$((testndx + 1))
53
54	echo "${result}" | sed 's/[ \t]*$//' | grep -q "^${@}$"
55	if [ ${?} -eq 0 ]
56	then
57		echo "ok ${testndx}"
58	else
59		echo "not ok ${testndx} - '${lasttest}'"
60	fi
61
62	return
63}
64
65
66#
67# Regression tests
68#
69
70# How NULL will be returned by program.
71readonly NULL="\*NULL\*"
72
73# Setup environment for tests.
74readonly BAR="bar"
75readonly NEWBAR="newbar"
76export FOO=${BAR}
77
78
79# Gets from environ.
80run_test -g FOO
81check_result "${FOO}"
82
83run_test -c 3 -g FOO
84check_result "${NULL}"
85
86run_test -g FOOBAR
87check_result "${NULL}"
88
89run_test -c 3 -g FOOBAR
90check_result "${NULL}"
91
92run_test -G
93check_result "${NULL}"
94
95
96# Clear environ.
97run_test -c 1 -g FOO
98check_result "${NULL}"
99
100run_test -c 2 -g FOO
101check_result "${NULL}"
102
103run_test -c 3 -g FOO
104check_result "${NULL}"
105
106run_test -c 4 -g FOO
107check_result "${NULL}"
108
109
110# Clear environ and verify values do not return after a set.
111run_test -c 1 -g FOO -s FOO2 ${NEWBAR} 1 -g FOO -g FOO2
112check_result "${NULL} 0 0 ${NULL} ${NEWBAR}"
113
114run_test -c 2 -g FOO -s FOO2 ${NEWBAR} 1 -g FOO -g FOO2
115check_result "${NULL} 0 0 ${NULL} ${NEWBAR}"
116
117run_test -c 3 -g FOO -s FOO2 ${NEWBAR} 1 -g FOO -g FOO2
118check_result "${NULL} 0 0 ${NULL} ${NEWBAR}"
119
120run_test -c 4 -g FOO -s FOO2 ${NEWBAR} 1 -g FOO -g FOO2
121check_result "${NULL} 0 0 ${NULL} ${NEWBAR}"
122
123
124# Sets.
125run_test -s FOO ${NEWBAR} 0 -g FOO
126check_result "0 0 ${BAR}"
127
128run_test -s FOO ${NEWBAR} 1 -g FOO
129check_result "0 0 ${NEWBAR}"
130
131run_test -c 3 -s FOO ${NEWBAR} 0 -g FOO
132check_result "0 0 ${NEWBAR}"
133
134run_test -c 3 -s FOO ${NEWBAR} 1 -g FOO
135check_result "0 0 ${NEWBAR}"
136
137run_test -s "FOO=" ${NEWBAR} 1 -g FOO
138check_result "-1 22 ${BAR}"
139
140run_test -s "=FOO" ${NEWBAR} 1
141check_result "-1 22"
142
143run_test -s "=" ${NEWBAR} 1
144check_result "-1 22"
145
146run_test -s "" ${NEWBAR} 1
147check_result "-1 22"
148
149run_test -S ${NEWBAR} 1
150check_result "-1 22"
151
152run_test -s FOO ${NEWBAR} 1 -s FOO ${BAR} 1 -g FOO
153check_result "0 0 0 0 ${BAR}"
154
155run_test -c 3 -s FOO ${NEWBAR} 1 -s FOO ${BAR} 1 -g FOO
156check_result "0 0 0 0 ${BAR}"
157
158run_test -s FOO ${NEWBAR} 1 -s FOO ${BAR} 1 -s FOO ${NEWBAR} 1 -g FOO
159check_result "0 0 0 0 0 0 ${NEWBAR}"
160
161run_test -s FOO ${NEWBAR} 1 -s FOO ${BAR} 1 -s FOO ${NEWBAR} 1 -s FOO ${BAR} 1\
162	-g FOO
163check_result "0 0 0 0 0 0 0 0 ${BAR}"
164
165run_test -c 3 -s FOO ${BAR} 1 -g FOO -c 3 -s FOO ${NEWBAR} 1 -g FOO
166check_result "0 0 ${BAR} 0 0 ${NEWBAR}"
167
168
169# Unsets.
170run_test -u FOO -g FOO
171check_result "0 0 ${NULL}"
172
173run_test -c 3 -u FOO -g FOO
174check_result "0 0 ${NULL}"
175
176run_test -U
177check_result "-1 22"
178
179run_test -u ""
180check_result "-1 22"
181
182run_test -u "=${BAR}"
183check_result "-1 22"
184
185run_test -c 3 -s FOO ${NEWBAR} 1 -g FOO -u FOO -g FOO
186check_result "0 0 ${NEWBAR} 0 0 ${NULL}"
187
188run_test -c 3 -u FOO -s FOO ${BAR} 1 -g FOO -u FOO -g FOO -c 3 -u FOO\
189	-s FOO ${NEWBAR} 1 -g FOO
190check_result "0 0 0 0 ${BAR} 0 0 ${NULL} 0 0 0 0 ${NEWBAR}"
191
192
193# Puts.
194run_test -p FOO=${NEWBAR} -g FOO
195check_result "0 0 ${NEWBAR}"
196
197run_test -c 3 -p FOO=${NEWBAR} -g FOO
198check_result "0 0 ${NEWBAR}"
199
200run_test -p FOO -g FOO
201check_result "-1 22 ${BAR}"
202
203run_test -p FOO=${BAR} -p FOO=${NEWBAR} -g FOO
204check_result "0 0 0 0 ${NEWBAR}"
205
206run_test -p FOO=${BAR} -s FOO ${NEWBAR} 1 -g FOO
207check_result "0 0 0 0 ${NEWBAR}"
208
209run_test -s FOO ${NEWBAR} 1 -p FOO=${BAR} -g FOO
210check_result "0 0 0 0 ${BAR}"
211
212run_test -p FOO=${BAR} -u FOO
213check_result "0 0 0 0"
214
215run_test -p FOO=${BAR} -s FOO ${NEWBAR} 1 -u FOO
216check_result "0 0 0 0 0 0"
217
218run_test -s FOO ${NEWBAR} 1 -p FOO=${BAR} -u FOO
219check_result "0 0 0 0 0 0"
220
221run_test -s FOO ${NEWBAR} 1 -p FOO=${BAR} -c 3 -g FOO -p FOO=${NEWBAR} -g FOO
222check_result "0 0 0 0 ${NULL} 0 0 ${NEWBAR}"
223
224run_test -c 3 -p FOO=${BAR} -g FOO -c 3 -p FOO=${NEWBAR} -g FOO
225check_result "0 0 ${BAR} 0 0 ${NEWBAR}"
226
227
228# environ replacements.
229run_test -r -g FOO -s FOO ${BAR} 1 -g FOO -u FOO -g FOO
230check_result "${BAR} 0 0 ${BAR} 0 0 ${NULL}"
231
232run_test -r -g FOO -u FOO -g FOO -s FOO ${BAR} 1 -g FOO
233check_result "${BAR} 0 0 ${NULL} 0 0 ${BAR}"
234
235
236# corruption (blanking) of environ members.
237export BLANK_ME=
238export AFTER_BLANK=blanked
239run_test -b BLANK_ME -p MORE=vars -g FOO -g BLANK_ME -g AFTER_BLANK
240check_result "0 0 ${FOO} ${NULL} ${AFTER_BLANK}"
241
242run_test -b BLANK_ME -u FOO -g FOO -g AFTER_BLANK
243check_result "0 0 ${NULL} ${AFTER_BLANK}"
244
245export BLANK_ME2=
246export AFTER_BLANKS=blankD
247run_test -b BLANK_ME -b AFTER_BLANK -b BLANK_ME2 -g FOO -g AFTER_BLANKS
248check_result "${FOO} ${AFTER_BLANKS}"
249