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