cpio.test 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/bin/bash
  2. [ -f testing.sh ] && . testing.sh
  3. # We need to test name and file padding.
  4. # This means all possible values of strlen(name)+1 % 4,
  5. # plus file sizes of at least 0-4.
  6. touch a bb ccc dddd
  7. testing "name padding" "cpio -o -H newc|cpio -it" "a\nbb\nccc\ndddd\n" "" "a\nbb\nccc\ndddd\n"
  8. rm a bb ccc dddd
  9. touch a
  10. printf '1' >b
  11. printf '22' >c
  12. printf '333' >d
  13. testing "file padding" "cpio -o -H newc|cpio -it" "a\nb\nc\nd\n" "" "a\nb\nc\nd\n"
  14. rm a b c d
  15. touch a
  16. printf '1' >bb
  17. printf '22' >ccc
  18. printf '333' >dddd
  19. # With the proper padding, header length, and file length,
  20. # the relevant bit should be here:
  21. # 110*5 + 4*3 + 2 + 6*3 = 550 + 12 + 20 = 582
  22. # files are padded to n*4, names are padded to 2 + n*4 due to the header length
  23. testing "archive length" "cpio -o -H newc|dd ibs=2 skip=291 count=5 2>/dev/null" "TRAILER!!!" "" "a\nbb\nccc\ndddd\n"
  24. testing "archive magic" "cpio -o -H newc|dd ibs=2 count=3 2>/dev/null" "070701" "" "a\n"
  25. # check name length (8 bytes before the empty "crc")
  26. testing "name length" "cpio -o -H newc|dd ibs=2 skip=47 count=4 2>/dev/null" "00000002" "" "a\n"
  27. testing "-t" "cpio -o -H newc|cpio -it" "a\nbb\n" "" "a\nbb"
  28. testing "-t --quiet" "cpio -o -H newc|cpio -it --quiet" "a\nbb\n" "" "a\nbb"
  29. mkdir out
  30. testing "-p" "cpio -p out && find out | sort" "out\nout/a\nout/bb\n" "" "a\nbb"
  31. rm -rf out
  32. testing "-pd" "cpio -pd out && find out | sort" "out\nout/a\nout/bb\n" "" "a\nbb"
  33. rm a bb ccc dddd
  34. # archive dangling symlinks and empty files even if we cannot open them
  35. touch a; chmod a-rwx a; ln -s a/cant b
  36. toyonly testing "archives unreadable empty files" "cpio -o -H newc|cpio -it" "b\na\n" "" "b\na\n"
  37. chmod u+rw a; rm -f a b
  38. mkdir a
  39. echo "old" >a/b
  40. echo "a/b" | cpio -o -H newc >a.cpio
  41. rm -rf a
  42. testing "-i doesn't create leading directories" "cpio -i <a.cpio 2>/dev/null; [ -e a ] || echo yes" "yes\n" "" ""
  43. rm -rf a
  44. testing "-id creates leading directories" "cpio -id <a.cpio && cat a/b" "old\n" "" ""
  45. rm -rf a a.cpio
  46. mkdir a
  47. echo "old" >a/b
  48. find a | cpio -o -H newc >a.cpio
  49. testing "-i keeps existing files" "echo new >a/b && cpio -i <a.cpio 2>/dev/null; cat a/b" "new\n" "" ""
  50. testing "-id keeps existing files" "echo new >a/b && cpio -id <a.cpio 2>/dev/null; cat a/b" "new\n" "" ""
  51. testing "-iu replaces existing files; no error" "echo new >a/b && cpio -iu <a.cpio && cat a/b" "old\n" "" ""
  52. testing "-idu replaces existing files; no error" "echo new >a/b && cpio -idu <a.cpio && cat a/b" "old\n" "" ""
  53. testing "skip NUL" "for i in a b; do dd if=/dev/zero bs=512 count=1 2>/dev/null; cat a.cpio; done | cpio -t -H newc" \
  54. "a\na/b\na\na/b\n" "" ""
  55. rm -rf a a.cpio
  56. testing "error on empty file" "cpio -i 2>/dev/null || echo err" "err\n" "" ""
  57. mkdir a
  58. touch a/file
  59. ln -s a/symlink a/symlink
  60. mkdir a/dir
  61. find a | cpio -o -H newc >a.cpio
  62. if [ "$(id -u)" -eq 0 ]; then
  63. # We chown between user "root" and the last user in /etc/passwd,
  64. # and group "root" and the last group in /etc/group.
  65. USR="$(sed -n '$s/:.*//p' /etc/passwd)"
  66. GRP="$(sed -n '$s/:.*//p' /etc/group)"
  67. # Or if that fails, we assume we're on Android...
  68. : "${USR:=shell}"
  69. : "${GRP:=shell}"
  70. chown -h "${USR}:${GRP}" a/file a/symlink a/dir
  71. fi
  72. skipnot [ $(id -u) -eq 0 ]
  73. testing "-t preserve ownership" "cpio -t <a.cpio >/dev/null && stat -c '%U:%G' a/file a/symlink a/dir" "${USR}:${GRP}\n${USR}:${GRP}\n${USR}:${GRP}\n" "" ""
  74. rm -rf a a.cpio