aboutsummaryrefslogtreecommitdiff
path: root/tests/cases/test_validation.sh
blob: 03da676a03e834960a15c3f38745e8fe6b8a4635 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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