mv_tests.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0-or-later
  3. # Copyright (c) International Business Machines Corp., 2001
  4. # Author: Manoj Iyer <manjo@mail.utexas.edu>
  5. #
  6. # Test basic functionality of mv command
  7. # Test #1: mv <dir1> <dir2>
  8. # move dir1 to dir2 and all its contents.
  9. # Test #2: mv -b <file1> <file2>
  10. # move file1 to file2 and backup the file2.
  11. TST_CNT=2
  12. TST_SETUP=setup
  13. TST_TESTFUNC=test
  14. TST_NEEDS_TMPDIR=1
  15. . tst_test.sh
  16. setup()
  17. {
  18. ROD_SILENT mkdir -p tst_mv.old
  19. }
  20. creat_dirnfiles()
  21. {
  22. local numdirs=$2
  23. local numfiles=$3
  24. local dirname=$4
  25. local dircnt=0
  26. local fcnt=0
  27. tst_res TINFO "Test #$1: Creating $numdirs directories."
  28. tst_res TINFO "Test #$1: filling each dir with $numfiles files."
  29. while [ $dircnt -lt $numdirs ]
  30. do
  31. dirname=$dirname/d.$dircnt
  32. ROD_SILENT mkdir -p $dirname
  33. fcnt=0
  34. while [ $fcnt -lt $numfiles ]
  35. do
  36. ROD_SILENT touch $dirname/f.$fcnt
  37. fcnt=$(($fcnt+1))
  38. done
  39. dircnt=$(($dircnt+1))
  40. done
  41. }
  42. creat_expout()
  43. {
  44. local numdir=$1
  45. local numfile=$2
  46. local dirname=$3
  47. local dircnt=0
  48. local fcnt=0
  49. echo "$dirname:" 1>>tst_mv.exp
  50. echo "d.$dircnt" 1>>tst_mv.exp
  51. while [ $dircnt -lt $numdirs ]
  52. do
  53. dirname=$dirname/d.$dircnt
  54. dircnt=$(($dircnt+1))
  55. echo "$dirname:" 1>>tst_mv.exp
  56. if [ $dircnt -lt $numdirs ]; then
  57. echo "d.$dircnt" 1>>tst_mv.exp
  58. fi
  59. fcnt=0
  60. while [ $fcnt -lt $numfiles ]
  61. do
  62. echo "f.$fcnt " 1>>tst_mv.exp
  63. fcnt=$(($fcnt+1))
  64. done
  65. printf "\n\n" 1>>tst_mv.exp
  66. done
  67. }
  68. test1()
  69. {
  70. numdirs=10
  71. numfiles=10
  72. dircnt=0
  73. fcnt=0
  74. tst_res TINFO "Test #1: mv <dir1> <dir2> will move dir1 to dir2 and" \
  75. "all its contents"
  76. creat_dirnfiles 1 $numdirs $numfiles tst_mv.old
  77. mv tst_mv.old tst_mv.new > tst_mv.err 2>&1
  78. if [ $? -ne 0 ]; then
  79. cat tst_mv.err
  80. tst_brk TFAIL "Test #1: 'mv tst_mv.old tst_mv.new' failed"
  81. fi
  82. tst_res TINFO "Test #1: creating output file"
  83. ls -R tst_mv.new > tst_mv.out 2>&1
  84. tst_res TINFO "Test #1: creating expected output file"
  85. creat_expout $numdirs $numfiles tst_mv.new
  86. tst_res TINFO "Test #1: comparing expected out and actual output file"
  87. diff -w -B -q tst_mv.out tst_mv.exp > tst_mv.err 2>&1
  88. if [ $? -ne 0 ]; then
  89. cat tst_mv.err
  90. tst_res TFAIL "Test #1: mv failed."
  91. else
  92. tst_res TINFO "Test #1: expected same as actual"
  93. if [ -f tst_mv.old ]; then
  94. tst_res TFAIL "Test #1: mv did not delete old" \
  95. "directory"
  96. else
  97. tst_res TPASS "Test #1: mv success"
  98. fi
  99. fi
  100. }
  101. test2()
  102. {
  103. tst_res TINFO "Test #2: mv -b <file1> <file2> will move dir1 to dir2"
  104. ROD_SILENT touch tmpfile1 tmpfile2
  105. MD5_old=$(md5sum tmpfile2 | awk '{print $1}')
  106. if [ $? -ne 0 ]; then
  107. tst_brk TBROK "Test #2: can't get the MD5 message of file2."
  108. fi
  109. if [ -f "tmpfile2~" ]; then
  110. tst_brk TBROK "Test #2: file tmpfile2~ should not exists."
  111. fi
  112. mv -b tmpfile1 tmpfile2
  113. if [ $? -ne 0 ]; then
  114. tst_brk TBROK "Test #2: 'mv -b tmpfile1 tmpfile2' failed."
  115. fi
  116. # if 'mv -b file1 file2' succeed, there will be "tmpfile2~" file.
  117. MD5_backup=$(md5sum tmpfile2 | awk '{print $1}')
  118. if [ $? -ne 0 ]; then
  119. tst_brk TBROK "Test #2: can not get the MD5 message of" \
  120. "backup file2."
  121. fi
  122. if [ "$MD5_old" = "$MD5_backup" -a -f "tmpfile2~" ]; then
  123. tst_res TPASS "Test #2: mv -b success"
  124. else
  125. tst_res TFAIL "Test #2: mv -b failed"
  126. fi
  127. }
  128. tst_run