blob: 9a89787961a0d0c18b1d85aeae9e21d745e7c76b (
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
#!/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
}
# ------------------------------------------------------------------------------
# Test: Rejects a retention count of zero
# ------------------------------------------------------------------------------
test_rejects_zero_count() {
local output
output=$(sudo "$SCRIPT_PATH" "manual" "0" 2>&1)
local exit_code=$?
assert_exit_code 1 "$exit_code" "should reject count 0" || return 1
assert_contains "$output" "at least 1" "should explain minimum count" || return 1
}
# ------------------------------------------------------------------------------
# Test: Refuses an empty MOUNTDIR (would resolve to the filesystem root)
# ------------------------------------------------------------------------------
test_rejects_empty_mountdir() {
setup_test_env
cat > "$TEST_CONFIG_DIR/config" <<EOF
REMOTE_HOST=""
MOUNTDIR=""
EOF
create_test_includes >/dev/null
create_test_excludes >/dev/null
local output
output=$(sudo INSTALLHOME="$TEST_CONFIG_DIR" RSYNCSHOT_SKIP_MOUNT_CHECK=1 "$SCRIPT_PATH" manual 1 2>&1)
local exit_code=$?
teardown_test_env
assert_exit_code 1 "$exit_code" "empty MOUNTDIR should be rejected" || return 1
assert_contains "$output" "MOUNTDIR is empty" "should explain the empty-MOUNTDIR refusal" || return 1
}
# ------------------------------------------------------------------------------
# Test: status produces no stderr noise (the grep "0\n0" integer-test bug)
# ------------------------------------------------------------------------------
test_status_no_stderr_noise() {
setup_test_env
create_test_config >/dev/null
create_test_includes >/dev/null
create_test_excludes >/dev/null
local stderr_file="$TEST_DIR/status.stderr"
sudo INSTALLHOME="$TEST_CONFIG_DIR" RSYNCSHOT_SKIP_MOUNT_CHECK=1 "$SCRIPT_PATH" status >/dev/null 2>"$stderr_file"
local err
err=$(cat "$stderr_file")
teardown_test_env
assert_no_stderr "$err" "status should emit no stderr (no 'integer expression' noise)" || 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
run_test "rejects retention count of zero" test_rejects_zero_count
run_test "refuses empty MOUNTDIR" test_rejects_empty_mountdir
run_test "status emits no stderr noise" test_status_no_stderr_noise
teardown_test_env
}
# Run if executed directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
run_validation_tests
print_summary
fi
|