gzip.test 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/bin/bash
  2. [ -f testing.sh ] && . testing.sh
  3. #testing "name" "command" "result" "infile" "stdin"
  4. # Compress files.
  5. # On success, the input files are removed and replaced by new
  6. # files with the .gz suffix.
  7. echo -n "foo " > f1
  8. echo "bar" > f2
  9. testing "with input files" "gzip f1 f2 &&
  10. test -f f1.gz && test -f f2.gz &&
  11. ! test -f f1 && ! test -f f2 &&
  12. zcat f1.gz f2.gz" "foo bar\n" "" ""
  13. rm -f f1 f2 f1.gz f2.gz
  14. # With no files, compresses stdin to stdout.
  15. testing "no files (stdin to stdout)" "echo hello world | gzip > f.gz &&
  16. test -f f.gz && zcat f.gz" "hello world\n" "" ""
  17. rm -f f.gz
  18. # -c Output to stdout
  19. echo -n "foo " > f1
  20. echo "bar" > f2
  21. testing "with input files and -c" "gzip -c f1 f2 > out.gz &&
  22. ! test -f f1.gz && ! test -f f2.gz &&
  23. test -f f1 && test -f f2 &&
  24. zcat out.gz" "foo bar\n" "" ""
  25. rm -f f1 f2 out.gz
  26. # -d Decompress (act as gunzip)
  27. echo "hello world" | gzip > f.gz
  28. testing "-d (act as gunzip)" "gzip -d f.gz &&
  29. test -f f && ! test -f f.gz && cat f" "hello world\n" "" ""
  30. rm -f f.gz f
  31. echo "hello world" | gzip > f.gz
  32. testing "-dc (act as zcat)" "gzip -dc f.gz &&
  33. ! test -f f && test -f f.gz" "hello world\n" "" ""
  34. rm -f f.gz f
  35. # -f Force: allow overwrite of output file
  36. echo "hello world" > f1
  37. echo "precious data" > f1.gz
  38. testing "no overwrite without -f" \
  39. "gzip f1 2>/dev/null || echo refused && cat f1 f1.gz" \
  40. "refused\nhello world\nprecious data\n" "" ""
  41. testing "overwrite with -f" \
  42. "gzip -f f1 && echo allowed && ! test -f f1 && zcat f1.gz" \
  43. "allowed\nhello world\n" "" ""
  44. rm -f f1 f1.gz
  45. # -k Keep input files (don't remove)
  46. echo "hello world" > f1
  47. testing "-k" "gzip -k f1 && cat f1 && zcat f1.gz" \
  48. "hello world\nhello world\n" "" ""
  49. rm -f f1 f1.gz
  50. # Test that -9 compresses better than -1.
  51. for i in $(seq 1 1000) ; do echo "hello world" >> x ; done
  52. gzip -c1 x > x1.gz
  53. gzip -c9 x > x9.gz
  54. testing "-1 vs -9" \
  55. "test $(stat -c '%s' x1.gz) -gt $(stat -c '%s' x9.gz) && echo okay" \
  56. "okay\n" "" ""
  57. rm -f x x1.gz x9.gz
  58. # Test that gzip preserves permissions and times.
  59. export TZ=UTC
  60. echo "hello world" > f1
  61. chmod 0411 f1
  62. touch -a -t 197801020304 f1
  63. touch -m -t 198704030201 f1
  64. testing "permissions/times preservation" \
  65. "gzip -k f1 && TZ=UTC stat -c '%a %Y' f1 && stat -c '%a %X %Y' f1.gz" \
  66. "411 544413660\n411 252558240 544413660\n" "" ""
  67. rm -f f1 f1.gz
  68. testing "reject non-gzip" "gzip -dc $FILES/blkid/msdos.bz2 2>/dev/null ||
  69. echo rejected" "rejected\n" "" ""