cp.test 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/bin/bash
  2. [ -f testing.sh ] && . testing.sh
  3. OLDUMASK=$(umask)
  4. umask 0002
  5. # Create test file
  6. dd if=/dev/urandom of=random bs=64 count=1 2> /dev/null
  7. #testing "name" "command" "result" "infile" "stdin"
  8. testing "not enough arguments [fail]" "cp one 2>/dev/null || echo yes" \
  9. "yes\n" "" ""
  10. testing "-missing source [fail]" "cp missing two 2>/dev/null || echo yes" \
  11. "yes\n" "" ""
  12. testing "file->file" "cp random two && cmp random two && echo yes" \
  13. "yes\n" "" ""
  14. rm two
  15. mkdir two
  16. testing "file->dir" "cp random two && cmp random two/random && echo yes" \
  17. "yes\n" "" ""
  18. rm two/random
  19. testing "file->dir/file" \
  20. "cp random two/random && cmp random two/random && echo yes" \
  21. "yes\n" "" ""
  22. testing "-r dir->missing" \
  23. "cp -r two three && cmp random three/random && echo yes" \
  24. "yes\n" "" ""
  25. touch walrus
  26. testing "-r dir->file [fail]" \
  27. "cp -r two walrus 2>/dev/null || echo yes" "yes\n" "" ""
  28. touch two/three
  29. testing "-r dir hits file." \
  30. "cp -r three two 2>/dev/null || echo yes" "yes\n" "" ""
  31. rm -rf two three walrus
  32. touch two
  33. chmod 000 two
  34. skipnot [ $(id -u) -ne 0 ] # Root doesn't count.
  35. testing "file->inaccessible [fail]" \
  36. "cp random two 2>/dev/null || echo yes" "yes\n" "" ""
  37. rm -f two
  38. touch two
  39. chmod 000 two
  40. skipnot [ $(id -u) -ne 0 ] # Root doesn't count.
  41. testing "-f file->inaccessible" \
  42. "cp -f random two && cmp random two && echo yes" "yes\n" "" ""
  43. mkdir sub
  44. chmod 000 sub
  45. skipnot [ $(id -u) -ne 0 ] # Root doesn't count.
  46. testing "file->inaccessible_dir [fail]" \
  47. "cp random sub 2>/dev/null || echo yes" "yes\n" "" ""
  48. rm two
  49. rmdir sub
  50. # This test fails because our -rf deletes existing target files without
  51. # regard to what we'd be copying over it. Posix says to only do that if
  52. # we'd be copying a file over the file, but does not say _why_.
  53. #mkdir dir
  54. #touch file
  55. #testing "-rf dir file [fail]" "cp -rf dir file 2>/dev/null || echo yes" \
  56. # "yes\n" "" ""
  57. #rm -rf dir file
  58. touch one two
  59. testing "file1 file2 missing [fail]" \
  60. "cp one two missing 2>/dev/null || echo yes" "yes\n" "" ""
  61. mkdir dir
  62. testing "dir file missing [fail]" \
  63. "cp dir two missing 2>/dev/null || echo yes" "yes\n" "" ""
  64. testing "-rf dir file missing [fail]" \
  65. "cp dir two missing 2>/dev/null || echo yes" "yes\n" "" ""
  66. testing "file1 file2 file [fail]" \
  67. "cp random one two 2>/dev/null || echo yes" "yes\n" "" ""
  68. testing "file1 file2 dir" \
  69. "cp random one dir && cmp random dir/random && cmp one dir/one && echo yes" \
  70. "yes\n" "" ""
  71. rm one two random
  72. rm -rf dir
  73. mkdir -p one/two/three/four
  74. touch one/two/three/five one/{six,seven,eight}
  75. testing "-r /abspath dest" \
  76. "cp -r \"$(readlink -f one)\" dir && diff -r one dir && echo yes" \
  77. "yes\n" "" ""
  78. testing "-r dir again" "cp -r one/. dir && diff -r one dir && echo yes" \
  79. "yes\n" "" ""
  80. mkdir dir2
  81. testing "-r dir1/* dir2" \
  82. "cp -r one/* dir2 && diff -r one dir2 && echo yes" "yes\n" "" ""
  83. rm -rf one dir dir2
  84. mkdir one; touch one/two; cp one/two one/three
  85. cp -pr one/ one_ # Succeeds twice in a row
  86. testing "-pr dir/." "cp -pr one/. one_ && echo yes" "yes\n" "" ""
  87. rm -rf one one_
  88. mkdir one; touch one/two; ln -s two one/three
  89. cp -pr one/ one_ # First time ok, second mustn't fail with "File exists"
  90. testing "-pr dir/. symlink child" "cp -pr one/. one_ && echo yes" "yes\n" "" ""
  91. rm -rf one one_
  92. touch walrus
  93. chmod 644 walrus
  94. ln -s walrus woot
  95. testing "symlink dest permissions" "cp woot carpenter && stat -c %A carpenter" \
  96. "-rw-r--r--\n" "" ""
  97. testing "duplicated --preserve options" \
  98. "cp --preserve=mode,mode walrus walrus2 2>&1 || echo bad" "" "" ""
  99. rm -rf walrus woot carpenter
  100. mkdir dir
  101. echo a > file
  102. echo b > b
  103. testing "-T file" "cp -T b file && cat file" "b\n" "" ""
  104. testing "-T dir" "cp -T b dir 2>/dev/null || echo expected" "expected\n" "" ""
  105. rm b file
  106. mkdir -p b/c/d/ a/
  107. echo a > b/c/d/file
  108. testing "--parents b/c/d/file a/" "cp --parents b/c/d/file a/ && cat a/b/c/d/file" "a\n" "" ""
  109. rm -rf a/ b/
  110. echo a > file
  111. testing "-P file" "cp -P file fdst && stat -c %F fdst" "regular file\n" "" ""
  112. ln -s file lnk
  113. testing "-P symlink" "cp -P lnk ldst && stat -c %F ldst" "symbolic link\n" "" ""
  114. testing "follow symlink" "cp lnk ldst2 && stat -c %F ldst2" "regular file\n" "" ""
  115. rm file fdst lnk ldst ldst2
  116. mkdir sub
  117. testing "-t one arg" 'cp -t sub/ input && cat sub/input' 'yes\n' 'yes\n' ''
  118. toyonly testing "-Dt" 'cp -Dt sub2 input && cat sub2/input' 'and\n' 'and\n' ''
  119. rm -rf sub sub2
  120. testing '-u1' 'echo one>one; sleep .1; echo two>two; cp -u one two; cat two' \
  121. 'two\n' '' ''
  122. testing '-u2' 'echo two>two; sleep .1; echo one>one; cp -u one two; cat two' \
  123. 'one\n' '' ''
  124. mkdir a b
  125. echo potato > a/one
  126. echo potato > a/two
  127. touch b/one b/two
  128. testing '-i' 'cp -ri a/. b/. 2>/dev/null; cmp -s a/one b/one || cmp -s a/one b/two && echo yes' \
  129. 'yes\n' '' 'n\ny\n'
  130. rm -rf one two a b
  131. # cp -r ../source destdir
  132. # cp -r one/two/three missing
  133. # cp -r one/two/three two
  134. # cp file1 file2 dir
  135. # cp file1 missing file2 -> dir
  136. # Make sure it's truncating existing file
  137. # copy with -d at top level, with -d in directory, without -d at top level,
  138. # without -d in directory
  139. umask $OLDUMASK