1#! /usr/bin/ksh
2#
3#
4# This file and its contents are supplied under the terms of the
5# Common Development and Distribution License ("CDDL"), version 1.0.
6# You may only use this file in accordance with the terms of version
7# 1.0 of the CDDL.
8#
9# A full copy of the text of the CDDL should have accompanied this
10# source.  A copy of the CDDL is also available via the Internet at
11# http://www.illumos.org/license/CDDL.
12#
13
14#
15# Copyright 2014 Garrett D'Amore <garrett@damore.org>
16# Copyright (c) 2017, Joyent, Inc.
17#
18
19XARGS=${XARGS:=/usr/bin/xargs}
20
21test_start() {
22	print "TEST STARTING ${1}: ${2}"
23}
24
25test_pass() {
26	print "TEST PASS: ${1}"
27}
28
29test_fail() {
30	print "TEST FAIL: ${1}: ${2}"
31	exit -1
32}
33
34checkrv() {
35	if [[ $? -ne 0 ]]; then
36		test_fail $1 "exit failure"
37	fi
38}
39
40compare() {
41	if [[ "$2" != "$3" ]]; then
42		test_fail $1 "compare mismatch, got [$2] expected [$3]"
43	fi
44}
45
46test1() {
47	t=test1
48	test_start $t "-I handling"
49	comp=$(echo foo bar baz other | $XARGS -I THING echo '** THING **')
50	checkrv $t
51	good='** foo bar baz other **'
52	compare $t "$comp" "$good"
53	test_pass $t
54}
55
56test2() {
57	t=test2
58	test_start $t "-n 1 handling"
59	comp=$(echo foo bar baz other | $XARGS -n 1 echo '***')
60	checkrv $t
61	good='*** foo
62*** bar
63*** baz
64*** other'
65	compare $t "$comp" "$good"
66	test_pass $t
67}
68
69test3() {
70	t=test3
71	test_start $t "-I before -n 1"
72	comp=$(echo foo bar baz other | $XARGS -I THING -n1 echo '** THING **')
73	checkrv $t
74	good='** THING ** foo
75** THING ** bar
76** THING ** baz
77** THING ** other'
78	compare $t "$comp" "$good"
79	test_pass $t
80}
81
82test4() {
83	t=test4
84	test_start $t "-n 1  before -I"
85	comp=$(echo foo bar baz other | $XARGS -n 1 -I THING echo '** THING **')
86	checkrv $t
87	good='** foo bar baz other **'
88	compare $t "$comp" "$good"
89	test_pass $t
90}
91
92test5() {
93	t=test5
94	test_start $t "-i multiple lines handling"
95	comp=$(printf "abc def\nxyz\n123" | $XARGS -n1 -i echo '[{}]')
96	checkrv $t
97	good='[abc def]
98[xyz]
99[123]'
100	compare $t "$comp" "$good"
101	test_pass $t
102}
103
104test6() {
105	t=test6
106	test_start $t "-E handling"
107	comp=$(printf "abc def xyx\n_\n123\nnope" | $XARGS -edef echo)
108	checkrv $t
109	good='abc'
110	compare $t "$comp" "$good"
111	test_pass $t
112}
113
114test7() {
115	t=test7
116	test_start $t "newlines in arguments"
117	comp=$(printf "abc def\nxyz\n\n123 456\n789" | $XARGS echo)
118	checkrv $t
119	good='abc def xyz 123 456 789'
120	compare $t "$comp" "$good"
121	test_pass $t
122}
123
124test8() {
125	t=test8
126	test_start $t "limited counts via -n3"
127	comp=$(printf "abc def ghi jkl mno 123 456 789" | $XARGS -n 3 echo '**' )
128	checkrv $t
129	good='** abc def ghi
130** jkl mno 123
131** 456 789'
132	compare $t "$comp" "$good"
133	test_pass $t
134}
135
136test9() {
137	t=test9
138	test_start $t "multiple lines via -L2"
139	comp=$(printf "abc def\n123 456\npeterpiper" | $XARGS -L2 echo '**')
140	checkrv $t
141	good='** abc def 123 456
142** peterpiper'
143	compare $t "$comp" "$good"
144	test_pass $t
145}
146
147test10() {
148	t=test10
149	test_start $t "argument sizes"
150	comp=$(printf "abc def 123 456 peter alpha\n" | $XARGS -s15 echo)
151	checkrv $t
152	good='abc def
153123 456
154peter
155alpha'
156	compare $t "$comp" "$good"
157	test_pass $t
158}
159
160test11() {
161	t=test11
162	test_start $t "bare -e"
163	comp=$(printf "abc def _ end of string" | $XARGS -e echo '**')
164	checkrv $t
165	good='** abc def _ end of string'
166	compare $t "$comp" "$good"
167	test_pass $t
168}
169
170test12() {
171	t=test12
172	test_start $t "-E ''"
173	comp=$(printf "abc def _ end of string" | $XARGS -E '' echo '**')
174	checkrv $t
175	good='** abc def _ end of string'
176	compare $t "$comp" "$good"
177	test_pass $t
178}
179
180test13() {
181	t=test13
182	test_start $t "end of string (no -E or -e)"
183	comp=$(printf "abc def _ end of string" | $XARGS echo '**')
184	checkrv $t
185	good='** abc def'
186	compare $t "$comp" "$good"
187	test_pass $t
188}
189
190test14() {
191	t=test14
192	test_start $t "trailing blank with -L"
193	comp=$(printf "abc def \n123 456\npeter\nbogus" | $XARGS -L2 echo '**')
194	checkrv $t
195	good='** abc def 123 456 peter
196** bogus'
197	compare $t "$comp" "$good"
198	test_pass $t
199}
200
201test15() {
202	t=test15
203	test_start $t "leading and embedded blanks with -i"
204	comp=$(printf "abc def\n  xyz  bogus\nnext" | $XARGS -i echo '** {}')
205	checkrv $t
206	good='** abc def
207** xyz  bogus
208** next'
209	compare $t "$comp" "$good"
210	test_pass $t
211}
212
213test16() {
214	t=test16
215	test_start $t "single character replstring"
216	comp=$(echo foo bar baz other | $XARGS -I X echo '** X **')
217	checkrv $t
218	good='** foo bar baz other **'
219	compare $t "$comp" "$good"
220	test_pass $t
221}
222
223test17() {
224	t=test17
225	test_start $t "null byte separators"
226	comp=$(print 'foo bar baz\000more data' | $XARGS -n1 -0 echo '**')
227	checkrv $t
228	good='** foo bar baz
229** more data'
230	compare $t "$comp" "$good"
231	test_pass $t
232}
233
234test18() {
235	t=test18
236	test_start $t "escape characters"
237	comp=$(printf 'foo\\ bar second" "arg third' | $XARGS -n1 echo '**')
238	checkrv $t
239	good='** foo bar
240** second arg
241** third'
242	compare $t "$comp" "$good"
243	test_pass $t
244}
245
246test19() {
247	t=test19
248	test_start $t "bad -P option (negative value)"
249	$XARGS -P -3 </dev/null 2>/dev/null
250	if [[ $? -eq 2 ]]; then
251		test_pass $t
252	else
253		test_fail $t
254	fi
255}
256
257test20() {
258	t=test20
259	test_start $t "bad -P option (bad string)"
260	$XARGS -P as3f </dev/null 2>/dev/null
261	if [[ $? -eq 2 ]]; then
262		test_pass $t
263	else
264		test_fail $t
265	fi
266}
267
268test21() {
269	t=test21
270	test_start $t "bad -P option (extraneous characters)"
271	$XARGS -P 2c </dev/null 2>/dev/null
272	if [[ $? -eq 2 ]]; then
273		test_pass $t
274	else
275		test_fail $t
276	fi
277}
278
279test1
280test2
281test3
282test4
283test5
284test6
285test7
286test8
287test9
288test10
289test11
290test12
291test13
292test14
293test15
294test16
295test17
296test18
297test19
298test20
299test21
300