xref: /freebsd/tools/regression/environ/envtest.t (revision 6ae1554a5d9b318f8ad53ccc39fa5a961403da73)
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# $FreeBSD$
28
29
30# Initialization.
31testndx=0
32
33
34# Testing function.
35run_test()
36{
37	lasttest="${@}"
38	result=`./envctl -t "${@}"`
39
40	if [ ${?} -ne 0 ]
41	then
42		echo "Test program failed" >&2
43		exit 1
44	fi
45
46	return
47}
48
49
50# Perform test on results.
51check_result()
52{
53	testndx=$((testndx + 1))
54
55	echo "${result}" | sed 's/[ \t]*$//' | grep -q "^${@}$"
56	if [ ${?} -eq 0 ]
57	then
58		echo "ok ${testndx}"
59	else
60		echo "not ok ${testndx} - '${lasttest}'"
61	fi
62
63	return
64}
65
66
67#
68# Regression tests
69#
70
71# How NULL will be returned by program.
72readonly NULL="\*NULL\*"
73
74# Setup environment for tests.
75readonly BAR="bar"
76readonly NEWBAR="newbar"
77export FOO=${BAR}
78
79
80# Gets from environ.
81run_test -g FOO
82check_result "${FOO}"
83
84run_test -c 3 -g FOO
85check_result "${NULL}"
86
87run_test -g FOOBAR
88check_result "${NULL}"
89
90run_test -c 3 -g FOOBAR
91check_result "${NULL}"
92
93run_test -G
94check_result "${NULL}"
95
96
97# Clear environ.
98run_test -c 1 -g FOO
99check_result "${NULL}"
100
101run_test -c 2 -g FOO
102check_result "${NULL}"
103
104run_test -c 3 -g FOO
105check_result "${NULL}"
106
107run_test -c 4 -g FOO
108check_result "${NULL}"
109
110
111# Clear environ and verify values do not return after a set.
112run_test -c 1 -g FOO -s FOO2 ${NEWBAR} 1 -g FOO -g FOO2
113check_result "${NULL} 0 0 ${NULL} ${NEWBAR}"
114
115run_test -c 2 -g FOO -s FOO2 ${NEWBAR} 1 -g FOO -g FOO2
116check_result "${NULL} 0 0 ${NULL} ${NEWBAR}"
117
118run_test -c 3 -g FOO -s FOO2 ${NEWBAR} 1 -g FOO -g FOO2
119check_result "${NULL} 0 0 ${NULL} ${NEWBAR}"
120
121run_test -c 4 -g FOO -s FOO2 ${NEWBAR} 1 -g FOO -g FOO2
122check_result "${NULL} 0 0 ${NULL} ${NEWBAR}"
123
124
125# Sets.
126run_test -s FOO ${NEWBAR} 0 -g FOO
127check_result "0 0 ${BAR}"
128
129run_test -s FOO ${NEWBAR} 1 -g FOO
130check_result "0 0 ${NEWBAR}"
131
132run_test -c 3 -s FOO ${NEWBAR} 0 -g FOO
133check_result "0 0 ${NEWBAR}"
134
135run_test -c 3 -s FOO ${NEWBAR} 1 -g FOO
136check_result "0 0 ${NEWBAR}"
137
138run_test -s "FOO=" ${NEWBAR} 1 -g FOO
139check_result "-1 22 ${BAR}"
140
141run_test -s "=FOO" ${NEWBAR} 1
142check_result "-1 22"
143
144run_test -s "=" ${NEWBAR} 1
145check_result "-1 22"
146
147run_test -s "" ${NEWBAR} 1
148check_result "-1 22"
149
150run_test -S ${NEWBAR} 1
151check_result "-1 22"
152
153run_test -s FOO ${NEWBAR} 1 -s FOO ${BAR} 1 -g FOO
154check_result "0 0 0 0 ${BAR}"
155
156run_test -c 3 -s FOO ${NEWBAR} 1 -s FOO ${BAR} 1 -g FOO
157check_result "0 0 0 0 ${BAR}"
158
159run_test -s FOO ${NEWBAR} 1 -s FOO ${BAR} 1 -s FOO ${NEWBAR} 1 -g FOO
160check_result "0 0 0 0 0 0 ${NEWBAR}"
161
162run_test -s FOO ${NEWBAR} 1 -s FOO ${BAR} 1 -s FOO ${NEWBAR} 1 -s FOO ${BAR} 1\
163	-g FOO
164check_result "0 0 0 0 0 0 0 0 ${BAR}"
165
166run_test -c 3 -s FOO ${BAR} 1 -g FOO -c 3 -s FOO ${NEWBAR} 1 -g FOO
167check_result "0 0 ${BAR} 0 0 ${NEWBAR}"
168
169
170# Unsets.
171run_test -u FOO -g FOO
172check_result "0 0 ${NULL}"
173
174run_test -c 3 -u FOO -g FOO
175check_result "0 0 ${NULL}"
176
177run_test -U
178check_result "-1 22"
179
180run_test -u ""
181check_result "-1 22"
182
183run_test -u "=${BAR}"
184check_result "-1 22"
185
186run_test -c 3 -s FOO ${NEWBAR} 1 -g FOO -u FOO -g FOO
187check_result "0 0 ${NEWBAR} 0 0 ${NULL}"
188
189run_test -c 3 -u FOO -s FOO ${BAR} 1 -g FOO -u FOO -g FOO -c 3 -u FOO\
190	-s FOO ${NEWBAR} 1 -g FOO
191check_result "0 0 0 0 ${BAR} 0 0 ${NULL} 0 0 0 0 ${NEWBAR}"
192
193
194# Puts.
195run_test -p FOO=${NEWBAR} -g FOO
196check_result "0 0 ${NEWBAR}"
197
198run_test -c 3 -p FOO=${NEWBAR} -g FOO
199check_result "0 0 ${NEWBAR}"
200
201run_test -p FOO -g FOO
202check_result "-1 22 ${BAR}"
203
204run_test -p FOO=${BAR} -p FOO=${NEWBAR} -g FOO
205check_result "0 0 0 0 ${NEWBAR}"
206
207run_test -p FOO=${BAR} -s FOO ${NEWBAR} 1 -g FOO
208check_result "0 0 0 0 ${NEWBAR}"
209
210run_test -s FOO ${NEWBAR} 1 -p FOO=${BAR} -g FOO
211check_result "0 0 0 0 ${BAR}"
212
213run_test -p FOO=${BAR} -u FOO
214check_result "0 0 0 0"
215
216run_test -p FOO=${BAR} -s FOO ${NEWBAR} 1 -u FOO
217check_result "0 0 0 0 0 0"
218
219run_test -s FOO ${NEWBAR} 1 -p FOO=${BAR} -u FOO
220check_result "0 0 0 0 0 0"
221
222run_test -s FOO ${NEWBAR} 1 -p FOO=${BAR} -c 3 -g FOO -p FOO=${NEWBAR} -g FOO
223check_result "0 0 0 0 ${NULL} 0 0 ${NEWBAR}"
224
225run_test -c 3 -p FOO=${BAR} -g FOO -c 3 -p FOO=${NEWBAR} -g FOO
226check_result "0 0 ${BAR} 0 0 ${NEWBAR}"
227
228
229# environ replacements.
230run_test -r -g FOO -s FOO ${BAR} 1 -g FOO -u FOO -g FOO
231check_result "${BAR} 0 0 ${BAR} 0 0 ${NULL}"
232
233run_test -r -g FOO -u FOO -g FOO -s FOO ${BAR} 1 -g FOO
234check_result "${BAR} 0 0 ${NULL} 0 0 ${BAR}"
235
236
237# corruption (blanking) of environ members.
238export BLANK_ME=
239export AFTER_BLANK=blanked
240run_test -b BLANK_ME -p MORE=vars -g FOO -g BLANK_ME -g AFTER_BLANK
241check_result "0 0 ${FOO} ${NULL} ${AFTER_BLANK}"
242
243run_test -b BLANK_ME -u FOO -g FOO -g AFTER_BLANK
244check_result "0 0 ${NULL} ${AFTER_BLANK}"
245
246export BLANK_ME2=
247export AFTER_BLANKS=blankD
248run_test -b BLANK_ME -b AFTER_BLANK -b BLANK_ME2 -g FOO -g AFTER_BLANKS
249check_result "${FOO} ${AFTER_BLANKS}"
250