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# Copyright 2026 Oxide Computer Company 18# 19 20XARGS=${XARGS:=/usr/bin/xargs} 21 22test_start() { 23 print "TEST STARTING ${1}: ${2}" 24} 25 26test_pass() { 27 print "TEST PASS: ${1}" 28} 29 30test_fail() { 31 print "TEST FAIL: ${1}: ${2}" 32 exit -1 33} 34 35checkrv() { 36 if (( $? != 0 )); then 37 test_fail $1 "exit failure" 38 fi 39} 40 41compare() { 42 if [[ "$2" != "$3" ]]; then 43 test_fail $1 "compare mismatch, got [$2] expected [$3]" 44 fi 45} 46 47test1() { 48 t=test1 49 test_start $t "-I handling" 50 comp=$(echo foo bar baz other | $XARGS -I THING echo '** THING **') 51 checkrv $t 52 good='** foo bar baz other **' 53 compare $t "$comp" "$good" 54 test_pass $t 55} 56 57test2() { 58 t=test2 59 test_start $t "-n 1 handling" 60 comp=$(echo foo bar baz other | $XARGS -n 1 echo '***') 61 checkrv $t 62 good='*** foo 63*** bar 64*** baz 65*** other' 66 compare $t "$comp" "$good" 67 test_pass $t 68} 69 70test3() { 71 t=test3 72 test_start $t "-I before -n 1" 73 comp=$(echo foo bar baz other | $XARGS -I THING -n1 echo '** THING **') 74 checkrv $t 75 good='** THING ** foo 76** THING ** bar 77** THING ** baz 78** THING ** other' 79 compare $t "$comp" "$good" 80 test_pass $t 81} 82 83test4() { 84 t=test4 85 test_start $t "-n 1 before -I" 86 comp=$(echo foo bar baz other | $XARGS -n 1 -I THING echo '** THING **') 87 checkrv $t 88 good='** foo bar baz other **' 89 compare $t "$comp" "$good" 90 test_pass $t 91} 92 93test5() { 94 t=test5 95 test_start $t "-i multiple lines handling" 96 comp=$(printf "abc def\nxyz\n123" | $XARGS -n1 -i echo '[{}]') 97 checkrv $t 98 good='[abc def] 99[xyz] 100[123]' 101 compare $t "$comp" "$good" 102 test_pass $t 103} 104 105test6() { 106 t=test6 107 test_start $t "-E handling" 108 comp=$(printf "abc def xyx\n_\n123\nnope" | $XARGS -edef echo) 109 checkrv $t 110 good='abc' 111 compare $t "$comp" "$good" 112 test_pass $t 113} 114 115test7() { 116 t=test7 117 test_start $t "newlines in arguments" 118 comp=$(printf "abc def\nxyz\n\n123 456\n789" | $XARGS echo) 119 checkrv $t 120 good='abc def xyz 123 456 789' 121 compare $t "$comp" "$good" 122 test_pass $t 123} 124 125test8() { 126 t=test8 127 test_start $t "limited counts via -n3" 128 comp=$(printf "abc def ghi jkl mno 123 456 789" | $XARGS -n 3 echo '**' ) 129 checkrv $t 130 good='** abc def ghi 131** jkl mno 123 132** 456 789' 133 compare $t "$comp" "$good" 134 test_pass $t 135} 136 137test9() { 138 t=test9 139 test_start $t "multiple lines via -L2" 140 comp=$(printf "abc def\n123 456\npeterpiper" | $XARGS -L2 echo '**') 141 checkrv $t 142 good='** abc def 123 456 143** peterpiper' 144 compare $t "$comp" "$good" 145 test_pass $t 146} 147 148test10() { 149 t=test10 150 test_start $t "argument sizes" 151 comp=$(printf "abc def 123 456 peter alpha\n" | $XARGS -s15 echo) 152 checkrv $t 153 good='abc def 154123 456 155peter 156alpha' 157 compare $t "$comp" "$good" 158 test_pass $t 159} 160 161test11() { 162 t=test11 163 test_start $t "bare -e" 164 comp=$(printf "abc def _ end of string" | $XARGS -e echo '**') 165 checkrv $t 166 good='** abc def _ end of string' 167 compare $t "$comp" "$good" 168 test_pass $t 169} 170 171test12() { 172 t=test12 173 test_start $t "-E ''" 174 comp=$(printf "abc def _ end of string" | $XARGS -E '' echo '**') 175 checkrv $t 176 good='** abc def _ end of string' 177 compare $t "$comp" "$good" 178 test_pass $t 179} 180 181test13() { 182 t=test13 183 test_start $t "end of string (no -E or -e)" 184 comp=$(printf "abc def _ end of string" | $XARGS echo '**') 185 checkrv $t 186 good='** abc def' 187 compare $t "$comp" "$good" 188 test_pass $t 189} 190 191test14() { 192 t=test14 193 test_start $t "trailing blank with -L" 194 comp=$(printf "abc def \n123 456\npeter\nbogus" | $XARGS -L2 echo '**') 195 checkrv $t 196 good='** abc def 123 456 peter 197** bogus' 198 compare $t "$comp" "$good" 199 test_pass $t 200} 201 202test15() { 203 t=test15 204 test_start $t "leading and embedded blanks with -i" 205 comp=$(printf "abc def\n xyz bogus\nnext" | $XARGS -i echo '** {}') 206 checkrv $t 207 good='** abc def 208** xyz bogus 209** next' 210 compare $t "$comp" "$good" 211 test_pass $t 212} 213 214test16() { 215 t=test16 216 test_start $t "single character replstring" 217 comp=$(echo foo bar baz other | $XARGS -I X echo '** X **') 218 checkrv $t 219 good='** foo bar baz other **' 220 compare $t "$comp" "$good" 221 test_pass $t 222} 223 224test17() { 225 t=test17 226 test_start $t "null byte separators" 227 comp=$(print 'foo bar baz\000more data' | $XARGS -n1 -0 echo '**') 228 checkrv $t 229 good='** foo bar baz 230** more data' 231 compare $t "$comp" "$good" 232 test_pass $t 233} 234 235test18() { 236 t=test18 237 test_start $t "escape characters" 238 comp=$(printf 'foo\\ bar second" "arg third' | $XARGS -n1 echo '**') 239 checkrv $t 240 good='** foo bar 241** second arg 242** third' 243 compare $t "$comp" "$good" 244 test_pass $t 245} 246 247test19() { 248 t=test19 249 test_start $t "bad -P option (negative value)" 250 $XARGS -P -3 </dev/null 2>/dev/null 251 if (( $? == 2 )); then 252 test_pass $t 253 else 254 test_fail $t 255 fi 256} 257 258test20() { 259 t=test20 260 test_start $t "bad -P option (bad string)" 261 $XARGS -P as3f </dev/null 2>/dev/null 262 if (( $? == 2 )); then 263 test_pass $t 264 else 265 test_fail $t 266 fi 267} 268 269test21() { 270 t=test21 271 test_start $t "bad -P option (extraneous characters)" 272 $XARGS -P 2c </dev/null 2>/dev/null 273 if (( $? == 2 )); then 274 test_pass $t 275 else 276 test_fail $t 277 fi 278} 279 280test22() { 281 t=test22 282 test_start $t "missing utility exits 127 and stops processing input" 283 err=$(printf "a\nb\nc" | $XARGS -n1 /nonexistent/utility \ 284 2>&1 >/dev/null) 285 rv=$? 286 if (( rv != 127 )); then 287 test_fail $t "exit status $rv != 127" 288 fi 289 lines=$(print -r -- "$err" | wc -l) 290 if (( lines != 1 )); then 291 test_fail $t "expected a single diagnostic, got [$err]" 292 fi 293 test_pass $t 294} 295 296test23() { 297 t=test23 298 test_start $t "non-executable utility exits 126" 299 tf=/var/tmp/xargstest.$$ 300 print '#!/bin/sh' > $tf 301 chmod 0644 $tf 302 printf "a" | $XARGS $tf >/dev/null 2>&1 303 rv=$? 304 rm -f $tf 305 if (( rv != 126 )); then 306 test_fail $t "exit status $rv != 126" 307 fi 308 test_pass $t 309} 310 311test24() { 312 t=test24 313 test_start $t "process creation failure exits 123" 314 printf "a" | ppriv -e -s A-proc_fork $XARGS echo >/dev/null 2>&1 315 rv=$? 316 if (( rv != 123 )); then 317 test_fail $t "exit status $rv != 123" 318 fi 319 test_pass $t 320} 321 322test1 323test2 324test3 325test4 326test5 327test6 328test7 329test8 330test9 331test10 332test11 333test12 334test13 335test14 336test15 337test16 338test17 339test18 340test19 341test20 342test21 343test22 344test23 345test24 346