dd.test 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/bin/bash
  2. # Copyright 2013 Robin Mittal <robinmittal.it@gmail.com>
  3. # Copyright 2013 Divya Kothari <divya.s.kothari@gmail.com>
  4. [ -f testing.sh ] && . testing.sh
  5. # 'dd' command, stderr prints redirecting to /dev/null
  6. opt="2>/dev/null"
  7. #testing "name" "command" "result" "infile" "stdin"
  8. # Test suffixed number parsing; `count` is representative.
  9. testing "count=2" "dd if=input count=2 ibs=1 $opt" "hi" "high\n" ""
  10. testing "count= 2" "dd if=input 'count= 2' ibs=1 $opt" "hi" "high\n" ""
  11. toyonly testing "count=0x2" "dd if=input 'count=0x2' ibs=1 $opt" "hi" \
  12. "high\n" ""
  13. testing "count=-2" "dd if=input 'count=-2' ibs=1 2>/dev/null || echo errored" "errored\n" "" ""
  14. testing "if=(file)" "dd if=input $opt" "I WANT\n" "I WANT\n" ""
  15. testing "of=(file)" "dd of=file $opt && cat file" "I WANT\n" "" "I WANT\n"
  16. testing "if=file of=file" "dd if=input of=foo $opt && cat foo && rm -f foo" \
  17. "I WANT\n" "I WANT\n" ""
  18. testing "if=file | dd of=file" "dd if=input $opt | dd of=foo $opt &&
  19. cat foo && rm -f foo" "I WANT\n" "I WANT\n" ""
  20. testing "(stdout)" "dd $opt" "I WANT\n" "" "I WANT\n"
  21. testing "sync,noerror" \
  22. "dd if=input of=outFile seek=8860 bs=1M conv=sync,noerror $opt &&
  23. stat -c \"%s\" outFile && rm -f outFile" "9291431936\n" "I WANT\n" ""
  24. testing "if=file of=(null)" \
  25. "dd if=input of=/dev/null $opt && echo 'yes'" "yes\n" "I WANT\n" ""
  26. testing "with if of bs" \
  27. "dd if=/dev/zero of=sda.txt bs=512 count=1 $opt &&
  28. stat -c '%s' sda.txt && rm -f sda.txt" "512\n" "" ""
  29. testing "with if of ibs obs" \
  30. "dd if=/dev/zero of=sda.txt ibs=512 obs=256 count=1 $opt &&
  31. stat -c '%s' sda.txt && rm -f sda.txt" "512\n" "" ""
  32. testing "with if of ibs obs count" \
  33. "dd if=/dev/zero of=sda.txt ibs=512 obs=256 count=3 $opt &&
  34. stat -c '%s' sda.txt && rm -f sda.txt" "1536\n" "" ""
  35. ln -s input softlink
  36. testing "if=softlink" "dd if=softlink $opt" "I WANT\n" "I WANT\n" ""
  37. unlink softlink
  38. ln -s file softlink
  39. testing "if=file of=softlink" "dd if=input of=softlink $opt &&
  40. [ -f file -a -L softlink ] && cat softlink" "I WANT\n" "I WANT\n" ""
  41. unlink softlink && rm -f file
  42. testing "if=file of=file (same file)" "dd if=input of=input $opt &&
  43. [ -f input ] && cat input && echo 'yes'" "yes\n" "I WANT\n" ""
  44. testing "same file notrunc" \
  45. "dd if=input of=input conv=notrunc $opt && cat input" \
  46. "I WANT\n" "I WANT\n" ""
  47. testing "with ibs obs bs" "dd ibs=2 obs=5 bs=9 $opt" "I WANT\n" "" "I WANT\n"
  48. testing "with ibs obs bs if" "dd ibs=2 obs=5 bs=9 if=input $opt" \
  49. "I WANT\n" "I WANT\n" ""
  50. testing "with ibs obs count" "dd ibs=1 obs=1 count=1 $opt" "I" "" "I WANT\n"
  51. testing "with ibs obs count if" "dd ibs=1 obs=1 count=3 if=input $opt" \
  52. "I W" "I WANT\n" ""
  53. testing "with count" "dd count=1 $opt" "I WANT\n" "" "I WANT\n"
  54. testing "with count if" "dd count=1 if=input $opt" "I WANT\n" "I WANT\n" ""
  55. testing "with skip" "dd skip=0 $opt" "I WANT\n" "" "I WANT\n"
  56. testing "with skip if" "dd skip=0 if=input $opt" "I WANT\n" "I WANT\n" ""
  57. testing "with seek" "dd seek=0 $opt" "I WANT\n" "" "I WANT\n"
  58. testing "with seek if" "dd seek=0 if=input $opt" "I WANT\n" "I WANT\n" ""
  59. # Testing only 'notrunc', 'noerror', 'fsync', 'sync'
  60. testing "conv=notrunc" "dd conv=notrunc $opt" "I WANT\n" "" "I WANT\n"
  61. testing "conv=notrunc with IF" "dd conv=notrunc if=input $opt" "I WANT\n" \
  62. "I WANT\n" ""
  63. testing "conv=noerror" "dd conv=noerror $opt" "I WANT\n" "" "I WANT\n"
  64. testing "conv=noerror with IF" "dd conv=noerror if=input $opt" "I WANT\n" \
  65. "I WANT\n" ""
  66. testing "conv=fsync" "dd conv=fsync $opt" "I WANT\n" "" "I WANT\n"
  67. testing "conv=fsync with IF" "dd conv=fsync if=input $opt" "I WANT\n" \
  68. "I WANT\n" ""
  69. testing "conv=sync" "dd conv=sync $opt | head -n 1" "I WANT\n" "" "I WANT\n"
  70. testing "conv=sync with IF" "dd conv=sync if=input $opt | head -n 1" "I WANT\n" \
  71. "I WANT\n" ""
  72. # status=noxfer|none
  73. testing "status=noxfer" "dd if=input status=noxfer ibs=1 2>&1" "input\n6+0 records in\n0+1 records out\n" "input\n" ""
  74. testing "status=none" "dd if=input status=none ibs=1 2>&1" "input\n" "input\n" ""
  75. testing "seek stdout" "yes 2> /dev/null | dd bs=8 seek=2 count=1 > out 2> /dev/null && xxd -p out" \
  76. "00000000000000000000000000000000790a790a790a790a\n" "" ""
  77. # Duplicated options are fine.
  78. testing "conv=sync,sync" "dd conv=sync,sync $opt | head -n 1" "I WANT\n" "" "I WANT\n"
  79. # _bytes options
  80. testing "iflag=count_bytes" \
  81. "dd if=input count=2 ibs=4096 iflag=count_bytes $opt" "hi" "high" ""
  82. testing "iflag=skip_bytes" \
  83. "dd if=input skip=2 ibs=4096 iflag=skip_bytes $opt" "gh" "high" ""
  84. testing "oflag=seek_bytes" \
  85. "dd if=input of=output seek=2 obs=4096 oflag=seek_bytes status=none && \
  86. xxd -p output" "000030313233\n" "0123" ""