1826ac02aSGarrett D'Amore#! /usr/bin/ksh
2826ac02aSGarrett D'Amore#
3826ac02aSGarrett D'Amore#
4826ac02aSGarrett D'Amore# This file and its contents are supplied under the terms of the
5826ac02aSGarrett D'Amore# Common Development and Distribution License ("CDDL"), version 1.0.
6826ac02aSGarrett D'Amore# You may only use this file in accordance with the terms of version
7826ac02aSGarrett D'Amore# 1.0 of the CDDL.
8826ac02aSGarrett D'Amore#
9826ac02aSGarrett D'Amore# A full copy of the text of the CDDL should have accompanied this
10826ac02aSGarrett D'Amore# source.  A copy of the CDDL is also available via the Internet at
11826ac02aSGarrett D'Amore# http://www.illumos.org/license/CDDL.
12826ac02aSGarrett D'Amore#
13826ac02aSGarrett D'Amore
14826ac02aSGarrett D'Amore#
15826ac02aSGarrett D'Amore# Copyright 2014 Garrett D'Amore <garrett@damore.org>
16*04427e3bSAndrew Bennett# Copyright (c) 2017, Joyent, Inc.
17826ac02aSGarrett D'Amore#
18826ac02aSGarrett D'Amore
19826ac02aSGarrett D'AmoreXARGS=${XARGS:=/usr/bin/xargs}
20826ac02aSGarrett D'Amore
21826ac02aSGarrett D'Amoretest_start() {
22826ac02aSGarrett D'Amore	print "TEST STARTING ${1}: ${2}"
23826ac02aSGarrett D'Amore}
24826ac02aSGarrett D'Amore
25826ac02aSGarrett D'Amoretest_pass() {
26826ac02aSGarrett D'Amore	print "TEST PASS: ${1}"
27826ac02aSGarrett D'Amore}
28826ac02aSGarrett D'Amore
29826ac02aSGarrett D'Amoretest_fail() {
30826ac02aSGarrett D'Amore	print "TEST FAIL: ${1}: ${2}"
31826ac02aSGarrett D'Amore	exit -1
32826ac02aSGarrett D'Amore}
33826ac02aSGarrett D'Amore
34826ac02aSGarrett D'Amorecheckrv() {
35826ac02aSGarrett D'Amore	if [[ $? -ne 0 ]]; then
36826ac02aSGarrett D'Amore		test_fail $1 "exit failure"
37826ac02aSGarrett D'Amore	fi
38826ac02aSGarrett D'Amore}
39826ac02aSGarrett D'Amore
40826ac02aSGarrett D'Amorecompare() {
41826ac02aSGarrett D'Amore	if [[ "$2" != "$3" ]]; then
42826ac02aSGarrett D'Amore		test_fail $1 "compare mismatch, got [$2] expected [$3]"
43826ac02aSGarrett D'Amore	fi
44826ac02aSGarrett D'Amore}
45826ac02aSGarrett D'Amore
46826ac02aSGarrett D'Amoretest1() {
47826ac02aSGarrett D'Amore	t=test1
48826ac02aSGarrett D'Amore	test_start $t "-I handling"
49826ac02aSGarrett D'Amore	comp=$(echo foo bar baz other | $XARGS -I THING echo '** THING **')
50826ac02aSGarrett D'Amore	checkrv $t
51826ac02aSGarrett D'Amore	good='** foo bar baz other **'
52826ac02aSGarrett D'Amore	compare $t "$comp" "$good"
53826ac02aSGarrett D'Amore	test_pass $t
54826ac02aSGarrett D'Amore}
55826ac02aSGarrett D'Amore
56826ac02aSGarrett D'Amoretest2() {
57826ac02aSGarrett D'Amore	t=test2
58826ac02aSGarrett D'Amore	test_start $t "-n 1 handling"
59826ac02aSGarrett D'Amore	comp=$(echo foo bar baz other | $XARGS -n 1 echo '***')
60826ac02aSGarrett D'Amore	checkrv $t
61826ac02aSGarrett D'Amore	good='*** foo
62826ac02aSGarrett D'Amore*** bar
63826ac02aSGarrett D'Amore*** baz
64826ac02aSGarrett D'Amore*** other'
65826ac02aSGarrett D'Amore	compare $t "$comp" "$good"
66826ac02aSGarrett D'Amore	test_pass $t
67826ac02aSGarrett D'Amore}
68826ac02aSGarrett D'Amore
69826ac02aSGarrett D'Amoretest3() {
70826ac02aSGarrett D'Amore	t=test3
71826ac02aSGarrett D'Amore	test_start $t "-I before -n 1"
72826ac02aSGarrett D'Amore	comp=$(echo foo bar baz other | $XARGS -I THING -n1 echo '** THING **')
73826ac02aSGarrett D'Amore	checkrv $t
74826ac02aSGarrett D'Amore	good='** THING ** foo
75826ac02aSGarrett D'Amore** THING ** bar
76826ac02aSGarrett D'Amore** THING ** baz
77826ac02aSGarrett D'Amore** THING ** other'
78826ac02aSGarrett D'Amore	compare $t "$comp" "$good"
79826ac02aSGarrett D'Amore	test_pass $t
80826ac02aSGarrett D'Amore}
81826ac02aSGarrett D'Amore
82826ac02aSGarrett D'Amoretest4() {
83826ac02aSGarrett D'Amore	t=test4
84826ac02aSGarrett D'Amore	test_start $t "-n 1  before -I"
85826ac02aSGarrett D'Amore	comp=$(echo foo bar baz other | $XARGS -n 1 -I THING echo '** THING **')
86826ac02aSGarrett D'Amore	checkrv $t
87826ac02aSGarrett D'Amore	good='** foo bar baz other **'
88826ac02aSGarrett D'Amore	compare $t "$comp" "$good"
89826ac02aSGarrett D'Amore	test_pass $t
90826ac02aSGarrett D'Amore}
91826ac02aSGarrett D'Amore
92826ac02aSGarrett D'Amoretest5() {
93826ac02aSGarrett D'Amore	t=test5
94826ac02aSGarrett D'Amore	test_start $t "-i multiple lines handling"
95826ac02aSGarrett D'Amore	comp=$(printf "abc def\nxyz\n123" | $XARGS -n1 -i echo '[{}]')
96826ac02aSGarrett D'Amore	checkrv $t
97826ac02aSGarrett D'Amore	good='[abc def]
98826ac02aSGarrett D'Amore[xyz]
99826ac02aSGarrett D'Amore[123]'
100826ac02aSGarrett D'Amore	compare $t "$comp" "$good"
101826ac02aSGarrett D'Amore	test_pass $t
102826ac02aSGarrett D'Amore}
103826ac02aSGarrett D'Amore
104826ac02aSGarrett D'Amoretest6() {
105826ac02aSGarrett D'Amore	t=test6
106826ac02aSGarrett D'Amore	test_start $t "-E handling"
107826ac02aSGarrett D'Amore	comp=$(printf "abc def xyx\n_\n123\nnope" | $XARGS -edef echo)
108826ac02aSGarrett D'Amore	checkrv $t
109826ac02aSGarrett D'Amore	good='abc'
110826ac02aSGarrett D'Amore	compare $t "$comp" "$good"
111826ac02aSGarrett D'Amore	test_pass $t
112826ac02aSGarrett D'Amore}
113826ac02aSGarrett D'Amore
114826ac02aSGarrett D'Amoretest7() {
115826ac02aSGarrett D'Amore	t=test7
116826ac02aSGarrett D'Amore	test_start $t "newlines in arguments"
117826ac02aSGarrett D'Amore	comp=$(printf "abc def\nxyz\n\n123 456\n789" | $XARGS echo)
118826ac02aSGarrett D'Amore	checkrv $t
119826ac02aSGarrett D'Amore	good='abc def xyz 123 456 789'
120826ac02aSGarrett D'Amore	compare $t "$comp" "$good"
121826ac02aSGarrett D'Amore	test_pass $t
122826ac02aSGarrett D'Amore}
123826ac02aSGarrett D'Amore
124826ac02aSGarrett D'Amoretest8() {
125826ac02aSGarrett D'Amore	t=test8
126826ac02aSGarrett D'Amore	test_start $t "limited counts via -n3"
127826ac02aSGarrett D'Amore	comp=$(printf "abc def ghi jkl mno 123 456 789" | $XARGS -n 3 echo '**' )
128826ac02aSGarrett D'Amore	checkrv $t
129826ac02aSGarrett D'Amore	good='** abc def ghi
130826ac02aSGarrett D'Amore** jkl mno 123
131826ac02aSGarrett D'Amore** 456 789'
132826ac02aSGarrett D'Amore	compare $t "$comp" "$good"
133826ac02aSGarrett D'Amore	test_pass $t
134826ac02aSGarrett D'Amore}
135826ac02aSGarrett D'Amore
136826ac02aSGarrett D'Amoretest9() {
137826ac02aSGarrett D'Amore	t=test9
138826ac02aSGarrett D'Amore	test_start $t "multiple lines via -L2"
139826ac02aSGarrett D'Amore	comp=$(printf "abc def\n123 456\npeterpiper" | $XARGS -L2 echo '**')
140826ac02aSGarrett D'Amore	checkrv $t
141826ac02aSGarrett D'Amore	good='** abc def 123 456
142826ac02aSGarrett D'Amore** peterpiper'
143826ac02aSGarrett D'Amore	compare $t "$comp" "$good"
144826ac02aSGarrett D'Amore	test_pass $t
145826ac02aSGarrett D'Amore}
146826ac02aSGarrett D'Amore
147826ac02aSGarrett D'Amoretest10() {
148826ac02aSGarrett D'Amore	t=test10
149826ac02aSGarrett D'Amore	test_start $t "argument sizes"
150826ac02aSGarrett D'Amore	comp=$(printf "abc def 123 456 peter alpha\n" | $XARGS -s15 echo)
151826ac02aSGarrett D'Amore	checkrv $t
152826ac02aSGarrett D'Amore	good='abc def
153826ac02aSGarrett D'Amore123 456
154826ac02aSGarrett D'Amorepeter
155826ac02aSGarrett D'Amorealpha'
156826ac02aSGarrett D'Amore	compare $t "$comp" "$good"
157826ac02aSGarrett D'Amore	test_pass $t
158826ac02aSGarrett D'Amore}
159826ac02aSGarrett D'Amore
160826ac02aSGarrett D'Amoretest11() {
161826ac02aSGarrett D'Amore	t=test11
162826ac02aSGarrett D'Amore	test_start $t "bare -e"
163826ac02aSGarrett D'Amore	comp=$(printf "abc def _ end of string" | $XARGS -e echo '**')
164826ac02aSGarrett D'Amore	checkrv $t
165826ac02aSGarrett D'Amore	good='** abc def _ end of string'
166826ac02aSGarrett D'Amore	compare $t "$comp" "$good"
167826ac02aSGarrett D'Amore	test_pass $t
168826ac02aSGarrett D'Amore}
169826ac02aSGarrett D'Amore
170826ac02aSGarrett D'Amoretest12() {
171826ac02aSGarrett D'Amore	t=test12
172826ac02aSGarrett D'Amore	test_start $t "-E ''"
173826ac02aSGarrett D'Amore	comp=$(printf "abc def _ end of string" | $XARGS -E '' echo '**')
174826ac02aSGarrett D'Amore	checkrv $t
175826ac02aSGarrett D'Amore	good='** abc def _ end of string'
176826ac02aSGarrett D'Amore	compare $t "$comp" "$good"
177826ac02aSGarrett D'Amore	test_pass $t
178826ac02aSGarrett D'Amore}
179826ac02aSGarrett D'Amore
180826ac02aSGarrett D'Amoretest13() {
181826ac02aSGarrett D'Amore	t=test13
182826ac02aSGarrett D'Amore	test_start $t "end of string (no -E or -e)"
183826ac02aSGarrett D'Amore	comp=$(printf "abc def _ end of string" | $XARGS echo '**')
184826ac02aSGarrett D'Amore	checkrv $t
185826ac02aSGarrett D'Amore	good='** abc def'
186826ac02aSGarrett D'Amore	compare $t "$comp" "$good"
187826ac02aSGarrett D'Amore	test_pass $t
188826ac02aSGarrett D'Amore}
189826ac02aSGarrett D'Amore
190826ac02aSGarrett D'Amoretest14() {
191826ac02aSGarrett D'Amore	t=test14
192826ac02aSGarrett D'Amore	test_start $t "trailing blank with -L"
193826ac02aSGarrett D'Amore	comp=$(printf "abc def \n123 456\npeter\nbogus" | $XARGS -L2 echo '**')
194826ac02aSGarrett D'Amore	checkrv $t
195826ac02aSGarrett D'Amore	good='** abc def 123 456 peter
196826ac02aSGarrett D'Amore** bogus'
197826ac02aSGarrett D'Amore	compare $t "$comp" "$good"
198826ac02aSGarrett D'Amore	test_pass $t
199826ac02aSGarrett D'Amore}
200826ac02aSGarrett D'Amore
201826ac02aSGarrett D'Amoretest15() {
202826ac02aSGarrett D'Amore	t=test15
203826ac02aSGarrett D'Amore	test_start $t "leading and embedded blanks with -i"
204826ac02aSGarrett D'Amore	comp=$(printf "abc def\n  xyz  bogus\nnext" | $XARGS -i echo '** {}')
205826ac02aSGarrett D'Amore	checkrv $t
206826ac02aSGarrett D'Amore	good='** abc def
207826ac02aSGarrett D'Amore** xyz  bogus
208826ac02aSGarrett D'Amore** next'
209826ac02aSGarrett D'Amore	compare $t "$comp" "$good"
210826ac02aSGarrett D'Amore	test_pass $t
211826ac02aSGarrett D'Amore}
212826ac02aSGarrett D'Amore
213826ac02aSGarrett D'Amoretest16() {
214826ac02aSGarrett D'Amore	t=test16
215826ac02aSGarrett D'Amore	test_start $t "single character replstring"
216826ac02aSGarrett D'Amore	comp=$(echo foo bar baz other | $XARGS -I X echo '** X **')
217826ac02aSGarrett D'Amore	checkrv $t
218826ac02aSGarrett D'Amore	good='** foo bar baz other **'
219826ac02aSGarrett D'Amore	compare $t "$comp" "$good"
220826ac02aSGarrett D'Amore	test_pass $t
221826ac02aSGarrett D'Amore}
222826ac02aSGarrett D'Amore
223826ac02aSGarrett D'Amoretest17() {
224826ac02aSGarrett D'Amore	t=test17
225826ac02aSGarrett D'Amore	test_start $t "null byte separators"
226826ac02aSGarrett D'Amore	comp=$(print 'foo bar baz\000more data' | $XARGS -n1 -0 echo '**')
227826ac02aSGarrett D'Amore	checkrv $t
228826ac02aSGarrett D'Amore	good='** foo bar baz
229826ac02aSGarrett D'Amore** more data'
230826ac02aSGarrett D'Amore	compare $t "$comp" "$good"
231826ac02aSGarrett D'Amore	test_pass $t
232826ac02aSGarrett D'Amore}
233826ac02aSGarrett D'Amore
234826ac02aSGarrett D'Amoretest18() {
235826ac02aSGarrett D'Amore	t=test18
236826ac02aSGarrett D'Amore	test_start $t "escape characters"
237826ac02aSGarrett D'Amore	comp=$(printf 'foo\\ bar second" "arg third' | $XARGS -n1 echo '**')
238826ac02aSGarrett D'Amore	checkrv $t
239826ac02aSGarrett D'Amore	good='** foo bar
240826ac02aSGarrett D'Amore** second arg
241826ac02aSGarrett D'Amore** third'
242826ac02aSGarrett D'Amore	compare $t "$comp" "$good"
243826ac02aSGarrett D'Amore	test_pass $t
244826ac02aSGarrett D'Amore}
245826ac02aSGarrett D'Amore
246*04427e3bSAndrew Bennetttest19() {
247*04427e3bSAndrew Bennett	t=test19
248*04427e3bSAndrew Bennett	test_start $t "bad -P option (negative value)"
249*04427e3bSAndrew Bennett	$XARGS -P -3 </dev/null 2>/dev/null
250*04427e3bSAndrew Bennett	if [[ $? -eq 2 ]]; then
251*04427e3bSAndrew Bennett		test_pass $t
252*04427e3bSAndrew Bennett	else
253*04427e3bSAndrew Bennett		test_fail $t
254*04427e3bSAndrew Bennett	fi
255*04427e3bSAndrew Bennett}
256*04427e3bSAndrew Bennett
257*04427e3bSAndrew Bennetttest20() {
258*04427e3bSAndrew Bennett	t=test20
259*04427e3bSAndrew Bennett	test_start $t "bad -P option (bad string)"
260*04427e3bSAndrew Bennett	$XARGS -P as3f </dev/null 2>/dev/null
261*04427e3bSAndrew Bennett	if [[ $? -eq 2 ]]; then
262*04427e3bSAndrew Bennett		test_pass $t
263*04427e3bSAndrew Bennett	else
264*04427e3bSAndrew Bennett		test_fail $t
265*04427e3bSAndrew Bennett	fi
266*04427e3bSAndrew Bennett}
267*04427e3bSAndrew Bennett
268*04427e3bSAndrew Bennetttest21() {
269*04427e3bSAndrew Bennett	t=test21
270*04427e3bSAndrew Bennett	test_start $t "bad -P option (extraneous characters)"
271*04427e3bSAndrew Bennett	$XARGS -P 2c </dev/null 2>/dev/null
272*04427e3bSAndrew Bennett	if [[ $? -eq 2 ]]; then
273*04427e3bSAndrew Bennett		test_pass $t
274*04427e3bSAndrew Bennett	else
275*04427e3bSAndrew Bennett		test_fail $t
276*04427e3bSAndrew Bennett	fi
277*04427e3bSAndrew Bennett}
278*04427e3bSAndrew Bennett
279826ac02aSGarrett D'Amoretest1
280826ac02aSGarrett D'Amoretest2
281826ac02aSGarrett D'Amoretest3
282826ac02aSGarrett D'Amoretest4
283826ac02aSGarrett D'Amoretest5
284826ac02aSGarrett D'Amoretest6
285826ac02aSGarrett D'Amoretest7
286826ac02aSGarrett D'Amoretest8
287826ac02aSGarrett D'Amoretest9
288826ac02aSGarrett D'Amoretest10
289826ac02aSGarrett D'Amoretest11
290826ac02aSGarrett D'Amoretest12
291826ac02aSGarrett D'Amoretest13
292826ac02aSGarrett D'Amoretest14
293826ac02aSGarrett D'Amoretest15
294826ac02aSGarrett D'Amoretest16
295826ac02aSGarrett D'Amoretest17
296826ac02aSGarrett D'Amoretest18
297*04427e3bSAndrew Bennetttest19
298*04427e3bSAndrew Bennetttest20
299*04427e3bSAndrew Bennetttest21
300