106 lines
3.1 KiB
Bash
Executable File
106 lines
3.1 KiB
Bash
Executable File
#!/bin/sh
|
|
# tests/test_gzip.sh - Tests for gzip/gunzip/zcat applets
|
|
_test_name="gzip"
|
|
. "$(dirname "$0")/harness.sh"
|
|
|
|
setup_tmpdir
|
|
GZIP_CMD="$VX gzip"
|
|
GUNZIP="$VX gunzip"
|
|
ZCAT="$VX zcat"
|
|
|
|
echo "Running gzip tests..."
|
|
|
|
# --- basic compression/decompression ---
|
|
printf 'Hello, World!\n' > hello.txt
|
|
cp hello.txt hello.orig
|
|
$GZIP_CMD hello.txt
|
|
check_exit "basic: compress creates .gz" 0 test -f hello.txt.gz
|
|
check_exit "basic: original removed" 1 test -f hello.txt
|
|
|
|
$GUNZIP hello.txt.gz
|
|
check_exit "basic: decompress creates original" 0 test -f hello.txt
|
|
check_file hello.txt hello.orig "basic: roundtrip matches"
|
|
|
|
# --- -k (keep) ---
|
|
cp hello.orig keeptest.txt
|
|
$GZIP_CMD -k keeptest.txt
|
|
check_exit "keep: .gz created" 0 test -f keeptest.txt.gz
|
|
check_exit "keep: original kept" 0 test -f keeptest.txt
|
|
|
|
# --- -c (stdout) ---
|
|
cp hello.orig stdout_test.txt
|
|
$GZIP_CMD -c stdout_test.txt > stdout_test.gz
|
|
check_exit "stdout: .gz created" 0 test -f stdout_test.gz
|
|
check_exit "stdout: original kept" 0 test -f stdout_test.txt
|
|
$GUNZIP -c stdout_test.gz > stdout_test_round.txt
|
|
check_file stdout_test_round.txt hello.orig "stdout: roundtrip matches"
|
|
|
|
# --- zcat ---
|
|
_got="$($ZCAT stdout_test.gz)"
|
|
_exp="$(cat hello.orig)"
|
|
check_eq "$_got" "$_exp" "zcat: output matches original"
|
|
|
|
# --- -d flag (decompress via gzip -d) ---
|
|
cp stdout_test.gz dtest.gz
|
|
$GZIP_CMD -d dtest.gz
|
|
check_exit "dflag: decompresses" 0 test -f dtest
|
|
check_file dtest hello.orig "dflag: output matches"
|
|
|
|
# --- -f (force overwrite) ---
|
|
cp hello.orig force_test.txt
|
|
$GZIP_CMD force_test.txt
|
|
cp hello.orig force_test.txt
|
|
$GZIP_CMD -f force_test.txt
|
|
check_exit "force: overwrites .gz" 0 test -f force_test.txt.gz
|
|
|
|
# --- -t (test integrity) ---
|
|
cp hello.orig ttest.txt
|
|
$GZIP_CMD ttest.txt
|
|
check_exit "test: valid gz passes" 0 $GZIP_CMD -t ttest.txt.gz
|
|
|
|
# corrupt the gzip header magic bytes
|
|
printf '\x00\x00' | dd of=ttest.txt.gz bs=1 seek=0 conv=notrunc 2>/dev/null
|
|
check_exit "test: corrupt gz fails" 1 $GZIP_CMD -t ttest.txt.gz
|
|
|
|
# --- -l (list) ---
|
|
cp hello.orig ltest.txt
|
|
$GZIP_CMD ltest.txt
|
|
$GZIP_CMD -l ltest.txt.gz > list_out 2>&1
|
|
_rc=$?
|
|
check_eq "$_rc" "0" "list: exits 0"
|
|
check_grep "compressed" list_out "list: prints header"
|
|
|
|
# --- -n (no name) ---
|
|
cp hello.orig ntest.txt
|
|
$GZIP_CMD -n ntest.txt
|
|
check_exit "nflag: compress succeeds" 0 test -f ntest.txt.gz
|
|
|
|
# --- -1 through -9 (compression levels) ---
|
|
for lvl in 1 9; do
|
|
cp hello.orig "level${lvl}.txt"
|
|
check_exit "level: -${lvl} compresses" 0 $GZIP_CMD "-${lvl}" "level${lvl}.txt"
|
|
done
|
|
|
|
# --- pipe mode (stdin/stdout) ---
|
|
$GZIP_CMD < hello.orig > pipe_test.gz
|
|
$GUNZIP < pipe_test.gz > pipe_test.txt
|
|
check_file pipe_test.txt hello.orig "pipe: stdin roundtrip"
|
|
|
|
# --- multiple files ---
|
|
printf 'aaa\n' > multi_a.txt
|
|
printf 'bbb\n' > multi_b.txt
|
|
$GZIP_CMD multi_a.txt multi_b.txt
|
|
check_exit "multi: a.gz exists" 0 test -f multi_a.txt.gz
|
|
check_exit "multi: b.gz exists" 0 test -f multi_b.txt.gz
|
|
|
|
# --- gunzip argv[0] dispatch ---
|
|
cp hello.orig gun_test.txt
|
|
$GZIP_CMD gun_test.txt
|
|
$GUNZIP gun_test.txt.gz
|
|
check_file gun_test.txt hello.orig "gunzip: dispatch works"
|
|
|
|
# --- exit codes ---
|
|
check_exit "exit: compress nonexistent fails" 1 $GZIP_CMD nonexistent_file_xyz
|
|
|
|
summary
|