1# 2# Copyright 2015 EMC Corp. 3# All rights reserved. 4# 5# Redistribution and use in source and binary forms, with or without 6# modification, are permitted provided that the following conditions are 7# met: 8# 9# * Redistributions of source code must retain the above copyright 10# notice, this list of conditions and the following disclaimer. 11# * Redistributions in binary form must reproduce the above copyright 12# notice, this list of conditions and the following disclaimer in the 13# documentation and/or other materials provided with the distribution. 14# 15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21# 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 25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26# 27# $FreeBSD$ 28# 29 30create_test_dir() 31{ 32 [ -z "$ATF_TMPDIR" ] || return 0 33 34 export ATF_TMPDIR=$(pwd) 35 36 # XXX: need to nest this because of how kyua creates $TMPDIR; otherwise 37 # it will run into EPERM issues later 38 TEST_INPUTS_DIR="${ATF_TMPDIR}/test/inputs" 39 40 atf_check -e empty -s exit:0 mkdir -m 0777 -p $TEST_INPUTS_DIR 41 cd $TEST_INPUTS_DIR 42} 43 44create_test_inputs() 45{ 46 create_test_dir 47 48 atf_check -e empty -s exit:0 mkdir -m 0755 -p a/b/1 49 atf_check -e empty -s exit:0 ln -s a/b c 50 atf_check -e empty -s exit:0 touch d 51 atf_check -e empty -s exit:0 ln d e 52 atf_check -e empty -s exit:0 touch .f 53 atf_check -e empty -s exit:0 mkdir .g 54 atf_check -e empty -s exit:0 mkfifo h 55 atf_check -e ignore -s exit:0 dd if=/dev/zero of=i count=1000 bs=1 56 atf_check -e empty -s exit:0 touch klmn 57 atf_check -e empty -s exit:0 touch opqr 58 atf_check -e empty -s exit:0 touch stuv 59 atf_check -e empty -s exit:0 install -m 0755 /dev/null wxyz 60 atf_check -e empty -s exit:0 touch 0b00000001 61 atf_check -e empty -s exit:0 touch 0b00000010 62 atf_check -e empty -s exit:0 touch 0b00000011 63 atf_check -e empty -s exit:0 touch 0b00000100 64 atf_check -e empty -s exit:0 touch 0b00000101 65 atf_check -e empty -s exit:0 touch 0b00000110 66 atf_check -e empty -s exit:0 touch 0b00000111 67 atf_check -e empty -s exit:0 touch 0b00001000 68 atf_check -e empty -s exit:0 touch 0b00001001 69 atf_check -e empty -s exit:0 touch 0b00001010 70 atf_check -e empty -s exit:0 touch 0b00001011 71 atf_check -e empty -s exit:0 touch 0b00001100 72 atf_check -e empty -s exit:0 touch 0b00001101 73 atf_check -e empty -s exit:0 touch 0b00001110 74 atf_check -e empty -s exit:0 touch 0b00001111 75} 76 77KB=1024 78MB=$(( 1024 * $KB )) 79GB=$(( 1024 * $MB )) 80TB=$(( 1024 * $GB )) 81PB=$(( 1024 * $TB )) 82 83create_test_inputs2() 84{ 85 create_test_dir 86 87 if ! getconf MIN_HOLE_SIZE "$(pwd)"; then 88 echo "getconf MIN_HOLE_SIZE $(pwd) failed; sparse files probably" \ 89 "not supported by file system" 90 mount 91 atf_skip "Test's work directory does not support sparse files;" \ 92 "try with a different TMPDIR?" 93 fi 94 95 for filesize in 1 512 $(( 2 * $KB )) $(( 10 * $KB )) $(( 512 * $KB )); \ 96 do 97 atf_check -e ignore -o empty -s exit:0 \ 98 dd if=/dev/zero of=${filesize}.file bs=1 \ 99 count=1 oseek=${filesize} conv=sparse 100 files="${files} ${filesize}.file" 101 done 102 103 for filesize in $MB $GB $TB; do 104 atf_check -e ignore -o empty -s exit:0 \ 105 dd if=/dev/zero of=${filesize}.file bs=$MB \ 106 count=1 oseek=$(( $filesize / $MB )) conv=sparse 107 files="${files} ${filesize}.file" 108 done 109} 110 111atf_test_case A_flag 112A_flag_head() 113{ 114 atf_set "descr" "Verify -A support with unprivileged users" 115} 116 117A_flag_body() 118{ 119 create_test_dir 120 121 atf_check -e empty -o empty -s exit:0 ls -A 122 123 create_test_inputs 124 125 WITH_A=$PWD/../with_A.out 126 WITHOUT_A=$PWD/../without_A.out 127 128 atf_check -e empty -o save:$WITH_A -s exit:0 ls -A 129 atf_check -e empty -o save:$WITHOUT_A -s exit:0 ls 130 131 echo "-A usage" 132 cat $WITH_A 133 echo "No -A usage" 134 cat $WITHOUT_A 135 136 for dot_path in '\.f' '\.g'; do 137 atf_check -e empty -o not-empty -s exit:0 grep "${dot_path}" \ 138 $WITH_A 139 atf_check -e empty -o empty -s not-exit:0 grep "${dot_path}" \ 140 $WITHOUT_A 141 done 142} 143 144atf_test_case A_flag_implied_when_root 145A_flag_implied_when_root_head() 146{ 147 atf_set "descr" "Verify that -A is implied for root" 148 atf_set "require.user" "root" 149} 150 151A_flag_implied_when_root_body() 152{ 153 create_test_dir 154 155 atf_check -e empty -o empty -s exit:0 ls -A 156 157 create_test_inputs 158 159 WITH_EXPLICIT=$PWD/../with_explicit_A.out 160 WITH_IMPLIED=$PWD/../with_implied_A.out 161 162 atf_check -e empty -o save:$WITH_EXPLICIT -s exit:0 ls -A 163 atf_check -e empty -o save:$WITH_IMPLIED -s exit:0 ls 164 165 echo "Explicit -A usage" 166 cat $WITH_EXPLICIT 167 echo "Implicit -A usage" 168 cat $WITH_IMPLIED 169 170 atf_check_equal "$(cat $WITH_EXPLICIT)" "$(cat $WITH_IMPLIED)" 171} 172 173atf_test_case B_flag 174B_flag_head() 175{ 176 atf_set "descr" "Verify that the output from ls -B prints out non-printable characters" 177} 178 179B_flag_body() 180{ 181 atf_check -e empty -o empty -s exit:0 touch "$(printf "y\013z")" 182 atf_check -e empty -o match:'y\\013z' -s exit:0 ls -B 183} 184 185atf_test_case C_flag 186C_flag_head() 187{ 188 atf_set "descr" "Verify that the output from ls -C is multi-column, sorted down" 189} 190 191print_index() 192{ 193 local i=1 194 local wanted_index=$1; shift 195 196 while [ $i -le $wanted_index ]; do 197 if [ $i -eq $wanted_index ]; then 198 echo $1 199 return 200 fi 201 shift 202 : $(( i += 1 )) 203 done 204} 205 206C_flag_body() 207{ 208 create_test_inputs 209 210 WITH_C=$PWD/../with_C.out 211 212 export COLUMNS=40 213 atf_check -e empty -o save:$WITH_C -s exit:0 ls -C 214 215 echo "With -C usage" 216 cat $WITH_C 217 218 paths=$(find -s . -mindepth 1 -maxdepth 1 \! -name '.*' -exec basename {} \; ) 219 set -- $paths 220 num_paths=$# 221 num_columns=2 222 223 max_num_paths_per_column=$(( $(( $num_paths + 1 )) / $num_columns )) 224 225 local i=1 226 while [ $i -le $max_num_paths_per_column ]; do 227 column_1=$(print_index $i $paths) 228 column_2=$(print_index $(( $i + $max_num_paths_per_column )) $paths) 229 #echo "paths[$(( $i + $max_num_paths_per_column ))] = $column_2" 230 expected_expr="$column_1" 231 if [ -n "$column_2" ]; then 232 expected_expr="$expected_expr[[:space:]]+$column_2" 233 fi 234 atf_check -e ignore -o not-empty -s exit:0 \ 235 egrep "$expected_expr" $WITH_C 236 : $(( i += 1 )) 237 done 238} 239 240atf_test_case D_flag 241D_flag_head() 242{ 243 atf_set "descr" "Verify that the output from ls -D modifies the time format used with ls -l" 244} 245 246D_flag_body() 247{ 248 atf_check -e empty -o empty -s exit:0 touch a.file 249 atf_check -e empty -o match:"$(stat -f '%c[[:space:]]+%N' a.file)" \ 250 -s exit:0 ls -lD '%s' 251} 252 253atf_test_case F_flag 254F_flag_head() 255{ 256 atf_set "descr" "Verify that the output from ls -F prints out appropriate symbols after files" 257} 258 259F_flag_body() 260{ 261 create_test_inputs 262 263 atf_check -e empty -s exit:0 \ 264 sh -c "pid=${ATF_TMPDIR}/nc.pid; daemon -p \$pid nc -lU j; sleep 2; pkill -F \$pid" 265 266 atf_check -e empty -o match:'a/' -s exit:0 ls -F 267 atf_check -e empty -o match:'c@' -s exit:0 ls -F 268 atf_check -e empty -o match:'h\|' -s exit:0 ls -F 269 atf_check -e empty -o match:'j=' -s exit:0 ls -F 270 #atf_check -e empty -o match:'<whiteout-file>%' -s exit:0 ls -F 271 atf_check -e empty -o match:'stuv' -s exit:0 ls -F 272 atf_check -e empty -o match:'wxyz\*' -s exit:0 ls -F 273} 274 275atf_test_case H_flag 276H_flag_head() 277{ 278 atf_set "descr" "Verify that ls -H follows symlinks" 279} 280 281H_flag_body() 282{ 283 create_test_inputs 284 285 atf_check -e empty -o match:'1' -s exit:0 ls -H c 286} 287 288atf_test_case I_flag 289I_flag_head() 290{ 291 atf_set "descr" "Verify that the output from ls -I is the same as ls for an unprivileged user" 292} 293 294I_flag_body() 295{ 296 create_test_inputs 297 298 WITH_I=$PWD/../with_I.out 299 WITHOUT_I=$PWD/../without_I.out 300 301 atf_check -e empty -o save:$WITH_I -s exit:0 ls -I 302 atf_check -e empty -o save:$WITHOUT_I -s exit:0 ls 303 304 echo "Explicit -I usage" 305 cat $WITH_I 306 echo "No -I usage" 307 cat $WITHOUT_I 308 309 atf_check_equal "$(cat $WITH_I)" "$(cat $WITHOUT_I)" 310} 311 312atf_test_case I_flag_voids_implied_A_flag_when_root 313I_flag_voids_implied_A_flag_when_root_head() 314{ 315 atf_set "descr" "Verify that -I voids out implied -A for root" 316 atf_set "require.user" "root" 317} 318 319I_flag_voids_implied_A_flag_when_root_body() 320{ 321 create_test_inputs 322 323 atf_check -o not-match:'\.f' -s exit:0 ls -I 324 atf_check -o not-match:'\.g' -s exit:0 ls -I 325 326 atf_check -o match:'\.f' -s exit:0 ls -A -I 327 atf_check -o match:'\.g' -s exit:0 ls -A -I 328} 329 330atf_test_case L_flag 331L_flag_head() 332{ 333 atf_set "descr" "Verify that -L prints out the symbolic link and conversely -P prints out the target for the symbolic link" 334} 335 336L_flag_body() 337{ 338 atf_check -e empty -o empty -s exit:0 ln -s target1/target2 link1 339 atf_check -e empty -o match:link1 -s exit:0 ls -L 340 atf_check -e empty -o not-match:target1/target2 -s exit:0 ls -L 341} 342 343atf_test_case R_flag 344R_flag_head() 345{ 346 atf_set "descr" "Verify that the output from ls -R prints out the directory contents recursively" 347} 348 349R_flag_body() 350{ 351 create_test_inputs 352 353 WITH_R=$PWD/../with_R.out 354 WITH_R_expected_output=$PWD/../with_R_expected.out 355 356 atf_check -e empty -o save:$WITH_R -s exit:0 ls -R 357 358 set -- . $(find -s . \! -name '.*' -type d) 359 while [ $# -gt 0 ]; do 360 dir=$1; shift 361 [ "$dir" != "." ] && echo "$dir:" 362 (cd $dir && ls -1A | sed -e '/^\./d') 363 [ $# -ne 0 ] && echo 364 done > $WITH_R_expected_output 365 366 echo "-R usage" 367 cat $WITH_R 368 echo "-R expected output" 369 cat $WITH_R_expected_output 370 371 atf_check_equal "$(cat $WITH_R)" "$(cat $WITH_R_expected_output)" 372} 373 374atf_test_case S_flag 375S_flag_head() 376{ 377 atf_set "descr" "Verify that -S sorts by file size, then by filename lexicographically" 378} 379 380S_flag_body() 381{ 382 create_test_dir 383 384 file_list_dir=$PWD/../files 385 386 atf_check -e empty -o empty -s exit:0 mkdir -p $file_list_dir 387 388 create_test_inputs 389 create_test_inputs2 390 391 WITH_S=$PWD/../with_S.out 392 WITHOUT_S=$PWD/../without_S.out 393 394 atf_check -e empty -o save:$WITH_S ls -D '%s' -lS 395 atf_check -e empty -o save:$WITHOUT_S ls -D '%s' -l 396 397 WITH_S_parsed=$(awk '! /^total/ { print $7 }' $WITH_S) 398 set -- $(awk '! /^total/ { print $5, $7 }' $WITHOUT_S) 399 while [ $# -gt 0 ]; do 400 size=$1; shift 401 filename=$1; shift 402 echo $filename >> $file_list_dir/${size} 403 done 404 file_lists=$(find $file_list_dir -type f -exec basename {} \; | sort -nr) 405 WITHOUT_S_parsed=$(for file_list in $file_lists; do sort < $file_list_dir/$file_list; done) 406 407 echo "-lS usage (parsed)" 408 echo "$WITH_S_parsed" 409 echo "-l usage (parsed)" 410 echo "$WITHOUT_S_parsed" 411 412 atf_check_equal "$WITHOUT_S_parsed" "$WITH_S_parsed" 413} 414 415atf_test_case T_flag 416T_flag_head() 417{ 418 atf_set "descr" "Verify -T support" 419} 420 421T_flag_body() 422{ 423 create_test_dir 424 425 atf_check -e empty -o empty -s exit:0 touch a.file 426 427 mtime_in_secs=$(stat -f %m -t %s a.file) 428 mtime=$(date -j -f %s $mtime_in_secs +"[[:space:]]+%b[[:space:]]+%e[[:space:]]+%H:%M:%S[[:space:]]+%Y") 429 430 atf_check -e empty -o match:"$mtime"'[[:space:]]+a\.file' \ 431 -s exit:0 ls -lT a.file 432} 433 434atf_test_case a_flag 435a_flag_head() 436{ 437 atf_set "descr" "Verify -a support" 438} 439 440a_flag_body() 441{ 442 create_test_dir 443 444 # Make sure "." and ".." show up with -a 445 atf_check -e empty -o match:'\.[[:space:]]+\.\.' -s exit:0 ls -ax 446 447 create_test_inputs 448 449 WITH_a=$PWD/../with_a.out 450 WITHOUT_a=$PWD/../without_a.out 451 452 atf_check -e empty -o save:$WITH_a -s exit:0 ls -a 453 atf_check -e empty -o save:$WITHOUT_a -s exit:0 ls 454 455 echo "-a usage" 456 cat $WITH_a 457 echo "No -a usage" 458 cat $WITHOUT_a 459 460 for dot_path in '\.f' '\.g'; do 461 atf_check -e empty -o not-empty -s exit:0 grep "${dot_path}" \ 462 $WITH_a 463 atf_check -e empty -o empty -s not-exit:0 grep "${dot_path}" \ 464 $WITHOUT_a 465 done 466} 467 468atf_test_case b_flag 469b_flag_head() 470{ 471 atf_set "descr" "Verify that the output from ls -b prints out non-printable characters" 472} 473 474b_flag_body() 475{ 476 atf_check -e empty -o empty -s exit:0 touch "$(printf "y\013z")" 477 atf_check -e empty -o match:'y\\vz' -s exit:0 ls -b 478} 479 480atf_test_case d_flag 481d_flag_head() 482{ 483 atf_set "descr" "Verify that -d doesn't descend down directories" 484} 485 486d_flag_body() 487{ 488 create_test_dir 489 490 output=$PWD/../output 491 492 atf_check -e empty -o empty -s exit:0 mkdir -p a/b 493 494 for path in . $PWD a; do 495 atf_check -e empty -o save:$output -s exit:0 ls -d $path 496 atf_check_equal "$(cat $output)" "$path" 497 done 498} 499 500atf_test_case f_flag 501f_flag_head() 502{ 503 atf_set "descr" "Verify that -f prints out the contents of a directory unsorted" 504} 505 506f_flag_body() 507{ 508 create_test_inputs 509 510 output=$PWD/../output 511 512 # XXX: I don't have enough understanding of how the algorithm works yet 513 # to determine more than the fact that all the entries printed out 514 # exist 515 paths=$(find -s . -mindepth 1 -maxdepth 1 \! -name '.*' -exec basename {} \; ) 516 517 atf_check -e empty -o save:$output -s exit:0 ls -f 518 519 for path in $paths; do 520 atf_check -e ignore -o not-empty -s exit:0 \ 521 egrep "^$path$" $output 522 done 523} 524 525atf_test_case g_flag 526g_flag_head() 527{ 528 atf_set "descr" "Verify that -g implies -l but omits the owner name field" 529} 530 531g_flag_body() 532{ 533 atf_check -e empty -o empty -s exit:0 touch a.file 534 535 mtime_in_secs=$(stat -f "%m" -t "%s" a.file) 536 mtime=$(date -j -f "%s" $mtime_in_secs +"%b[[:space:]]+%e[[:space:]]+%H:%M") 537 538 expected_output=$(stat -f "%Sp[[:space:]]+%l[[:space:]]+%Sg[[:space:]]+%z[[:space:]]+$mtime[[:space:]]+a\\.file" a.file) 539 540 atf_check -e empty -o match:"$expected_output" -s exit:0 ls -g a.file 541} 542 543atf_test_case h_flag 544h_flag_head() 545{ 546 atf_set "descr" "Verify that -h prints out the humanized units for file sizes with ls -l" 547 atf_set "require.progs" "bc" 548} 549 550h_flag_body() 551{ 552 # XXX: this test doesn't currently show how 999 bytes will be 999B, 553 # but 1000 bytes will be 1.0K, due to how humanize_number(3) works. 554 create_test_inputs2 555 for file in $files; do 556 file_size=$(stat -f '%z' "$file") || \ 557 atf_fail "stat'ing $file failed" 558 scale=2 559 if [ $file_size -lt $KB ]; then 560 divisor=1 561 scale=0 562 suffix=B 563 elif [ $file_size -lt $MB ]; then 564 divisor=$KB 565 suffix=K 566 elif [ $file_size -lt $GB ]; then 567 divisor=$MB 568 suffix=M 569 elif [ $file_size -lt $TB ]; then 570 divisor=$GB 571 suffix=G 572 elif [ $file_size -lt $PB ]; then 573 divisor=$TB 574 suffix=T 575 else 576 divisor=$PB 577 suffix=P 578 fi 579 580 bc_expr="$(printf "scale=%s\n%s/%s\nquit" $scale $file_size $divisor)" 581 size_humanized=$(bc -e "$bc_expr" | tr '.' '\.' | sed -e 's,\.00,,') 582 583 atf_check -e empty -o match:"$size_humanized.+$file" \ 584 -s exit:0 ls -hl $file 585 done 586} 587 588atf_test_case i_flag 589i_flag_head() 590{ 591 atf_set "descr" "Verify that -i prints out the inode for files" 592} 593 594i_flag_body() 595{ 596 create_test_inputs 597 598 paths=$(find -L . -mindepth 1) 599 [ -n "$paths" ] || atf_skip 'Could not find any paths to iterate over (!)' 600 601 for path in $paths; do 602 atf_check -e empty \ 603 -o match:"$(stat -f '[[:space:]]*%i[[:space:]]+%N' $path)" \ 604 -s exit:0 ls -d1i $path 605 done 606} 607 608atf_test_case k_flag 609k_flag_head() 610{ 611 atf_set "descr" "Verify that -k prints out the size with a block size of 1kB" 612} 613 614k_flag_body() 615{ 616 create_test_inputs2 617 for file in $files; do 618 atf_check -e empty \ 619 -o match:"[[:space:]]+$(stat -f "%z" $file)[[:space:]]+.+[[:space:]]+$file" ls -lk $file 620 done 621} 622 623atf_test_case l_flag 624l_flag_head() 625{ 626 atf_set "descr" "Verify that -l prints out the output in long format" 627} 628 629l_flag_body() 630{ 631 632 atf_check -e empty -o empty -s exit:0 touch a.file 633 634 mtime_in_secs=$(stat -f "%m" -t "%s" a.file) 635 mtime=$(date -j -f "%s" $mtime_in_secs +"%b[[:space:]]+%e[[:space:]]+%H:%M") 636 637 expected_output=$(stat -f "%Sp[[:space:]]+%l[[:space:]]+%Su[[:space:]]+%Sg[[:space:]]+%z[[:space:]]+$mtime[[:space:]]+a\\.file" a.file) 638 639 atf_check -e empty -o match:"$expected_output" -s exit:0 ls -l a.file 640} 641 642atf_test_case lcomma_flag 643lcomma_flag_head() 644{ 645 atf_set "descr" "Verify that -l, prints out the size with ',' delimiters" 646} 647 648lcomma_flag_body() 649{ 650 create_test_inputs 651 652 atf_check \ 653 -o match:'\-rw\-r\-\-r\-\-[[:space:]]+.+[[:space:]]+1,000[[:space:]]+.+i' \ 654 env LC_ALL=en_US.ISO8859-1 ls -l, i 655} 656 657atf_test_case m_flag 658m_flag_head() 659{ 660 atf_set "descr" "Verify that the output from ls -m is comma-separated" 661} 662 663m_flag_body() 664{ 665 create_test_dir 666 667 output=$PWD/../output 668 669 atf_check -e empty -o empty -s exit:0 touch ,, "a,b " c d e 670 671 atf_check -e empty -o save:$output -s exit:0 ls -m 672 673 atf_check_equal "$(cat $output)" ",,, a,b , c, d, e" 674} 675 676atf_test_case n_flag 677n_flag_head() 678{ 679 atf_set "descr" "Verify that the output from ls -n prints out numeric GIDs/UIDs instead of symbolic GIDs/UIDs" 680 atf_set "require.user" "root" 681} 682 683n_flag_body() 684{ 685 daemon_gid=$(id -g daemon) || atf_skip "could not resolve gid for daemon (!)" 686 nobody_uid=$(id -u nobody) || atf_skip "could not resolve uid for nobody (!)" 687 688 atf_check -e empty -o empty -s exit:0 touch a.file 689 atf_check -e empty -o empty -s exit:0 chown $nobody_uid:$daemon_gid a.file 690 691 atf_check -e empty \ 692 -o match:'\-rw\-r\-\-r\-\-[[:space:]]+1[[:space:]]+'"$nobody_uid[[:space:]]+$daemon_gid"'[[:space:]]+.+a\.file' \ 693 ls -n a.file 694 695} 696 697atf_test_case o_flag 698o_flag_head() 699{ 700 atf_set "descr" "Verify that the output from ls -o prints out the chflag values or '-' if none are set" 701} 702 703o_flag_body() 704{ 705 local size=12345 706 707 create_test_dir 708 709 atf_check -e ignore -o empty -s exit:0 dd if=/dev/zero of=a.file \ 710 bs=$size count=1 711 atf_check -e ignore -o empty -s exit:0 dd if=/dev/zero of=b.file \ 712 bs=$size count=1 713 atf_check -e empty -o empty -s exit:0 chflags uarch a.file 714 atf_check -e empty -o empty -s exit:0 chflags 0 b.file 715 716 atf_check -e empty -o match:"[[:space:]]+uarch[[:space:]]$size+.+a\\.file" \ 717 -s exit:0 ls -lo a.file 718 atf_check -e empty -o match:"[[:space:]]+\\-[[:space:]]$size+.+b\\.file" \ 719 -s exit:0 ls -lo b.file 720} 721 722atf_test_case p_flag 723p_flag_head() 724{ 725 atf_set "descr" "Verify that the output from ls -p prints out '/' after directories" 726} 727 728p_flag_body() 729{ 730 create_test_inputs 731 732 paths=$(find -L .) 733 [ -n "$paths" ] || atf_skip 'Could not find any paths to iterate over (!)' 734 735 for path in $paths; do 736 suffix= 737 # If path is not a symlink and is a directory, then the suffix 738 # must be "/". 739 if [ ! -L "${path}" -a -d "$path" ]; then 740 suffix=/ 741 fi 742 atf_check -e empty -o match:"$path${suffix}" -s exit:0 \ 743 ls -dp $path 744 done 745} 746 747atf_test_case q_flag_and_w_flag 748q_flag_and_w_flag_head() 749{ 750 atf_set "descr" "Verify that the output from ls -q prints out '?' for ESC and ls -w prints out the escape character" 751} 752 753q_flag_and_w_flag_body() 754{ 755 create_test_dir 756 757 test_file="$(printf "y\01z")" 758 759 atf_check -e empty -o empty -s exit:0 touch "$test_file" 760 761 atf_check -e empty -o match:'y\?z' -s exit:0 ls -q "$test_file" 762 atf_check -e empty -o match:"$test_file" -s exit:0 ls -w "$test_file" 763} 764 765atf_test_case r_flag 766r_flag_head() 767{ 768 atf_set "descr" "Verify that the output from ls -r sorts the same way as reverse sorting with sort(1)" 769} 770 771r_flag_body() 772{ 773 create_test_inputs 774 775 WITH_r=$PWD/../with_r.out 776 WITH_sort=$PWD/../with_sort.out 777 778 atf_check -e empty -o save:$WITH_r -s exit:0 ls -1r 779 atf_check -e empty -o save:$WITH_sort -s exit:0 sh -c 'ls -1 | sort -r' 780 781 echo "Sorted with -r" 782 cat $WITH_r 783 echo "Reverse sorted with sort(1)" 784 cat $WITH_sort 785 786 atf_check_equal "$(cat $WITH_r)" "$(cat $WITH_sort)" 787} 788 789atf_test_case s_flag 790s_flag_head() 791{ 792 atf_set "descr" "Verify that the output from ls -s matches the output from stat(1)" 793} 794 795s_flag_body() 796{ 797 create_test_inputs2 798 for file in $files; do 799 atf_check -e empty \ 800 -o match:"$(stat -f "%b" $file)[[:space:]]+$file" ls -s $file 801 done 802} 803 804atf_test_case t_flag 805t_flag_head() 806{ 807 atf_set "descr" "Verify that the output from ls -t sorts by modification time" 808} 809 810t_flag_body() 811{ 812 create_test_dir 813 814 atf_check -e empty -o empty -s exit:0 touch a.file 815 atf_check -e empty -o empty -s exit:0 touch b.file 816 817 atf_check -e empty -o match:'a\.file' -s exit:0 sh -c 'ls -lt | tail -n 1' 818 atf_check -e empty -o match:'b\.file.*a\.file' -s exit:0 ls -Ct 819 820 atf_check -e empty -o empty -s exit:0 rm a.file 821 atf_check -e empty -o empty -s exit:0 sh -c 'echo "i am a" > a.file' 822 823 atf_check -e empty -o match:'b\.file' -s exit:0 sh -c 'ls -lt | tail -n 1' 824 atf_check -e empty -o match:'a\.file.*b\.file' -s exit:0 ls -Ct 825} 826 827atf_test_case u_flag 828u_flag_head() 829{ 830 atf_set "descr" "Verify that the output from ls -u sorts by last access" 831} 832 833u_flag_body() 834{ 835 create_test_dir 836 837 atf_check -e empty -o empty -s exit:0 touch a.file 838 atf_check -e empty -o empty -s exit:0 touch b.file 839 840 atf_check -e empty -o match:'b\.file' -s exit:0 sh -c 'ls -lu | tail -n 1' 841 atf_check -e empty -o match:'a\.file.*b\.file' -s exit:0 ls -Cu 842 843 atf_check -e empty -o empty -s exit:0 sh -c 'echo "i am a" > a.file' 844 atf_check -e empty -o match:'i am a' -s exit:0 cat a.file 845 846 atf_check -e empty -o match:'b\.file' -s exit:0 sh -c 'ls -lu | tail -n 1' 847 atf_check -e empty -o match:'a\.file.*b\.file' -s exit:0 ls -Cu 848} 849 850atf_test_case v_flag 851v_flag_head() 852{ 853 atf_set "descr" "Verify that the output from ls -v sorts based on strverscmp(3)" 854} 855 856v_flag_body() 857{ 858 create_test_dir 859 860 atf_check -e empty -o empty -s exit:0 touch 000 00 01 010 09 0 1 9 10 861 atf_check -e empty -o match:"000.00.01.010.09.0.1.9.10" -s exit:0 sh -c 'ls -Cv' 862} 863 864atf_test_case x_flag 865x_flag_head() 866{ 867 atf_set "descr" "Verify that the output from ls -x is multi-column, sorted across" 868} 869 870x_flag_body() 871{ 872 create_test_inputs 873 874 WITH_x=$PWD/../with_x.out 875 876 atf_check -e empty -o save:$WITH_x -s exit:0 ls -x 877 878 echo "With -x usage" 879 cat $WITH_x 880 881 atf_check -e ignore -o not-empty -s exit:0 \ 882 egrep "a[[:space:]]+c[[:space:]]+d[[:space:]]+e[[:space:]]+h" $WITH_x 883 atf_check -e ignore -o not-empty -s exit:0 \ 884 egrep "i[[:space:]]+klmn[[:space:]]+opqr[[:space:]]+stuv[[:space:]]+wxyz" $WITH_x 885} 886 887atf_test_case y_flag 888y_flag_head() 889{ 890 atf_set "descr" "Verify that the output from ls -y sorts the same way as sort(1)" 891} 892 893y_flag_body() 894{ 895 create_test_inputs 896 897 WITH_sort=$PWD/../with_sort.out 898 WITH_y=$PWD/../with_y.out 899 900 atf_check -e empty -o save:$WITH_sort -s exit:0 sh -c 'ls -1 | sort' 901 atf_check -e empty -o save:$WITH_y -s exit:0 ls -1y 902 903 echo "Sorted with sort(1)" 904 cat $WITH_sort 905 echo "Sorted with -y" 906 cat $WITH_y 907 908 atf_check_equal "$(cat $WITH_sort)" "$(cat $WITH_y)" 909} 910 911atf_test_case 1_flag 9121_flag_head() 913{ 914 atf_set "descr" "Verify that -1 prints out one item per line" 915} 916 9171_flag_body() 918{ 919 create_test_inputs 920 921 WITH_1=$PWD/../with_1.out 922 WITHOUT_1=$PWD/../without_1.out 923 924 atf_check -e empty -o save:$WITH_1 -s exit:0 ls -1 925 atf_check -e empty -o save:$WITHOUT_1 -s exit:0 \ 926 sh -c 'for i in $(ls); do echo $i; done' 927 928 echo "Explicit -1 usage" 929 cat $WITH_1 930 echo "No -1 usage" 931 cat $WITHOUT_1 932 933 atf_check_equal "$(cat $WITH_1)" "$(cat $WITHOUT_1)" 934} 935 936atf_init_test_cases() 937{ 938 export BLOCKSIZE=512 939 940 atf_add_test_case A_flag 941 atf_add_test_case A_flag_implied_when_root 942 atf_add_test_case B_flag 943 atf_add_test_case C_flag 944 atf_add_test_case D_flag 945 atf_add_test_case F_flag 946 #atf_add_test_case G_flag 947 atf_add_test_case H_flag 948 atf_add_test_case I_flag 949 atf_add_test_case I_flag_voids_implied_A_flag_when_root 950 atf_add_test_case L_flag 951 #atf_add_test_case P_flag 952 atf_add_test_case R_flag 953 atf_add_test_case S_flag 954 atf_add_test_case T_flag 955 #atf_add_test_case U_flag 956 #atf_add_test_case W_flag 957 #atf_add_test_case Z_flag 958 atf_add_test_case a_flag 959 atf_add_test_case b_flag 960 #atf_add_test_case c_flag 961 atf_add_test_case d_flag 962 atf_add_test_case f_flag 963 atf_add_test_case g_flag 964 atf_add_test_case h_flag 965 atf_add_test_case i_flag 966 atf_add_test_case k_flag 967 atf_add_test_case l_flag 968 atf_add_test_case lcomma_flag 969 atf_add_test_case m_flag 970 atf_add_test_case n_flag 971 atf_add_test_case o_flag 972 atf_add_test_case p_flag 973 atf_add_test_case q_flag_and_w_flag 974 atf_add_test_case r_flag 975 atf_add_test_case s_flag 976 atf_add_test_case t_flag 977 atf_add_test_case u_flag 978 atf_add_test_case v_flag 979 atf_add_test_case x_flag 980 atf_add_test_case y_flag 981 atf_add_test_case 1_flag 982} 983