diff options
| author | Craig Jennings <c@cjennings.net> | 2026-01-29 12:18:12 -0600 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-01-29 12:18:12 -0600 |
| commit | f272f2a14c60ef853bb860c0612ad931d5a21d74 (patch) | |
| tree | ae07730fa28db2309c524c00fd2689d934252aa4 /tests/cases/test_validation.sh | |
| parent | 2bc8c5c7b416d506e9c46d9f381c73f5f9f052b9 (diff) | |
| download | rsyncshot-f272f2a14c60ef853bb860c0612ad931d5a21d74.tar.gz rsyncshot-f272f2a14c60ef853bb860c0612ad931d5a21d74.zip | |
Add SSH remote backup support, new commands, and test suite
- Add remote mode for SSH-based backups to servers like TrueNAS
- Add SSH_IDENTITY_FILE config for non-root SSH keys
- Add new commands: backup, status, list, dryrun
- Add dependency checks for rsync, ssh, flock
- Add timestamped logging
- Fix: duplicate cron jobs on repeated setup
- Fix: use mktemp for temp files
- Fix: use portable sed instead of grep -oP
- Fix: strengthen input validation with regex anchors
- Fix: handle paths with spaces (newline-separated includes)
- Change license from MIT to GPL v3
- Add automated test suite (25 tests)
- Update README with new features and testing docs
Diffstat (limited to 'tests/cases/test_validation.sh')
| -rwxr-xr-x | tests/cases/test_validation.sh | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/tests/cases/test_validation.sh b/tests/cases/test_validation.sh new file mode 100755 index 0000000..03da676 --- /dev/null +++ b/tests/cases/test_validation.sh @@ -0,0 +1,106 @@ +#!/usr/bin/env bash +# ============================================================================== +# Input Validation Tests +# ============================================================================== + +source "$(dirname "${BASH_SOURCE[0]}")/../lib/test_helpers.sh" + +# ------------------------------------------------------------------------------ +# Test: Help works without root +# ------------------------------------------------------------------------------ +test_help_without_root() { + local output + output=$("$SCRIPT_PATH" help 2>&1) + local exit_code=$? + + assert_exit_code 0 "$exit_code" "help should exit with 0" || return 1 + assert_contains "$output" "rsyncshot" "help should mention rsyncshot" || return 1 + assert_contains "$output" "Usage" "help should show usage" || return 1 +} + +# ------------------------------------------------------------------------------ +# Test: Rejects non-alphabetic snapshot type +# ------------------------------------------------------------------------------ +test_rejects_numeric_snapshot_type() { + local output + output=$(sudo "$SCRIPT_PATH" "123" "5" 2>&1) + local exit_code=$? + + assert_exit_code 1 "$exit_code" "should reject numeric snapshot type" || return 1 + assert_contains "$output" "must be alphabetic" "should show alphabetic error" || return 1 +} + +# ------------------------------------------------------------------------------ +# Test: Rejects mixed alphanumeric snapshot type +# ------------------------------------------------------------------------------ +test_rejects_mixed_snapshot_type() { + local output + output=$(sudo "$SCRIPT_PATH" "hourly123" "5" 2>&1) + local exit_code=$? + + assert_exit_code 1 "$exit_code" "should reject mixed snapshot type" || return 1 + assert_contains "$output" "must be alphabetic" "should show alphabetic error" || return 1 +} + +# ------------------------------------------------------------------------------ +# Test: Rejects non-numeric retention count +# ------------------------------------------------------------------------------ +test_rejects_alpha_retention_count() { + local output + output=$(sudo "$SCRIPT_PATH" "manual" "abc" 2>&1) + local exit_code=$? + + assert_exit_code 1 "$exit_code" "should reject alphabetic count" || return 1 + assert_contains "$output" "must be a number" "should show number error" || return 1 +} + +# ------------------------------------------------------------------------------ +# Test: Rejects mixed alphanumeric retention count +# ------------------------------------------------------------------------------ +test_rejects_mixed_retention_count() { + local output + output=$(sudo "$SCRIPT_PATH" "manual" "5abc" 2>&1) + local exit_code=$? + + assert_exit_code 1 "$exit_code" "should reject mixed count" || return 1 + assert_contains "$output" "must be a number" "should show number error" || return 1 +} + +# ------------------------------------------------------------------------------ +# Test: Accepts valid alphabetic snapshot types +# ------------------------------------------------------------------------------ +test_accepts_valid_snapshot_types() { + # We use dryrun to avoid actual backup, and expect it to fail on missing config + # but it should get past the validation stage + local output + output=$(sudo "$SCRIPT_PATH" dryrun "hourly" "24" 2>&1) + + # Should not contain the validation error (might fail for other reasons like missing config) + assert_not_contains "$output" "must be alphabetic" "should accept valid type" || return 1 +} + +# ------------------------------------------------------------------------------ +# Run tests +# ------------------------------------------------------------------------------ +run_validation_tests() { + echo "" + echo "Running validation tests..." + echo "------------------------------------------------------------" + + setup_test_env + + run_test "help works without root" test_help_without_root + run_test "rejects numeric snapshot type" test_rejects_numeric_snapshot_type + run_test "rejects mixed alphanumeric snapshot type" test_rejects_mixed_snapshot_type + run_test "rejects alphabetic retention count" test_rejects_alpha_retention_count + run_test "rejects mixed retention count" test_rejects_mixed_retention_count + run_test "accepts valid snapshot types" test_accepts_valid_snapshot_types + + teardown_test_env +} + +# Run if executed directly +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then + run_validation_tests + print_summary +fi |
