xref: /freebsd/tools/regression/environ/envtest.t (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1#!/bin/sh
2#
3# Copyright (c) 2007 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# Setup environment for tests.
72readonly BAR="bar"
73readonly NEWBAR="newbar"
74export FOO=${BAR}
75
76
77# Gets from environ.
78run_test -g FOO
79check_result "${FOO}"
80
81run_test -c -g FOO
82check_result ""
83
84run_test -g FOOBAR
85check_result ""
86
87run_test -c -g FOOBAR
88check_result ""
89
90run_test -G
91check_result ""
92
93
94# Sets.
95run_test -s FOO ${NEWBAR} 0 -g FOO
96check_result "0 0 ${BAR}"
97
98run_test -s FOO ${NEWBAR} 1 -g FOO
99check_result "0 0 ${NEWBAR}"
100
101run_test -c -s FOO ${NEWBAR} 0 -g FOO
102check_result "0 0 ${NEWBAR}"
103
104run_test -c -s FOO ${NEWBAR} 1 -g FOO
105check_result "0 0 ${NEWBAR}"
106
107run_test -s "FOO=" ${NEWBAR} 1 -g FOO
108check_result "-1 22 ${BAR}"
109
110run_test -s "=FOO" ${NEWBAR} 1
111check_result "-1 22"
112
113run_test -s "=" ${NEWBAR} 1
114check_result "-1 22"
115
116run_test -s "" ${NEWBAR} 1
117check_result "-1 22"
118
119run_test -S ${NEWBAR} 1
120check_result "-1 22"
121
122run_test -s FOO ${NEWBAR} 1 -s FOO ${BAR} 1 -g FOO
123check_result "0 0 0 0 ${BAR}"
124
125run_test -c -s FOO ${NEWBAR} 1 -s FOO ${BAR} 1 -g FOO
126check_result "0 0 0 0 ${BAR}"
127
128run_test -s FOO ${NEWBAR} 1 -s FOO ${BAR} 1 -s FOO ${NEWBAR} 1 -g FOO
129check_result "0 0 0 0 0 0 ${NEWBAR}"
130
131run_test -s FOO ${NEWBAR} 1 -s FOO ${BAR} 1 -s FOO ${NEWBAR} 1 -s FOO ${BAR} 1\
132	-g FOO
133check_result "0 0 0 0 0 0 0 0 ${BAR}"
134
135run_test -c -s FOO ${BAR} 1 -g FOO -c -s FOO ${NEWBAR} 1 -g FOO
136check_result "0 0 ${BAR} 0 0 ${NEWBAR}"
137
138
139# Unsets.
140run_test -u FOO -g FOO
141check_result "0 0"
142
143run_test -c -u FOO -g FOO
144check_result "0 0"
145
146run_test -U
147check_result "-1 22"
148
149run_test -u ""
150check_result "-1 22"
151
152run_test -u "=${BAR}"
153check_result "-1 22"
154
155run_test -c -s FOO ${NEWBAR} 1 -g FOO -u FOO -g FOO
156check_result "0 0 ${NEWBAR} 0 0"
157
158run_test -c -u FOO -s FOO ${BAR} 1 -g FOO -u FOO -g FOO -c -u FOO\
159	-s FOO ${NEWBAR} 1 -g FOO
160check_result "0 0 0 0 ${BAR} 0 0  0 0 0 0 ${NEWBAR}"
161
162
163# Puts.
164run_test -p FOO=${NEWBAR} -g FOO
165check_result "0 0 ${NEWBAR}"
166
167run_test -c -p FOO=${NEWBAR} -g FOO
168check_result "0 0 ${NEWBAR}"
169
170run_test -p FOO -g FOO
171check_result "-1 22 ${BAR}"
172
173run_test -p FOO=${BAR} -p FOO=${NEWBAR} -g FOO
174check_result "0 0 0 0 ${NEWBAR}"
175
176run_test -p FOO=${BAR} -s FOO ${NEWBAR} 1 -g FOO
177check_result "0 0 0 0 ${NEWBAR}"
178
179run_test -s FOO ${NEWBAR} 1 -p FOO=${BAR} -g FOO
180check_result "0 0 0 0 ${BAR}"
181
182run_test -p FOO=${BAR} -u FOO
183check_result "0 0 0 0"
184
185run_test -p FOO=${BAR} -s FOO ${NEWBAR} 1 -u FOO
186check_result "0 0 0 0 0 0"
187
188run_test -s FOO ${NEWBAR} 1 -p FOO=${BAR} -u FOO
189check_result "0 0 0 0 0 0"
190
191run_test -s FOO ${NEWBAR} 1 -p FOO=${BAR} -c -g FOO -p FOO=${NEWBAR} -g FOO
192check_result "0 0 0 0  0 0 ${NEWBAR}"
193
194run_test -c -p FOO=${BAR} -g FOO -c -p FOO=${NEWBAR} -g FOO
195check_result "0 0 ${BAR} 0 0 ${NEWBAR}"
196
197
198# environ replacements.
199run_test -r -g FOO -s FOO ${BAR} 1 -g FOO -u FOO -g FOO
200check_result "${BAR} 0 0 ${BAR} 0 0"
201
202run_test -r -g FOO -u FOO -g FOO -s FOO ${BAR} 1 -g FOO
203check_result "${BAR} 0 0  0 0 ${BAR}"
204