1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-08 07:59:16 +00:00

Improve unit tests for greater safety

This commit is contained in:
Tw93
2025-11-15 13:11:00 +08:00
parent 78f5341a68
commit 53ebd90d91
5 changed files with 132 additions and 7 deletions

View File

@@ -79,6 +79,27 @@ teardown() {
grep -q "ERROR: $message" "$log_file"
}
@test "rotate_log_once only checks log size once per session" {
# Create a log file exceeding the max size
local log_file="$HOME/.config/mole/mole.log"
mkdir -p "$(dirname "$log_file")"
dd if=/dev/zero of="$log_file" bs=1024 count=1100 2> /dev/null
# First call should rotate
HOME="$HOME" bash --noprofile --norc -c "source '$PROJECT_ROOT/lib/common.sh'"
[[ -f "${log_file}.old" ]]
# Verify MOLE_LOG_ROTATED was set (rotation happened)
result=$(HOME="$HOME" MOLE_LOG_ROTATED=1 bash --noprofile --norc -c "source '$PROJECT_ROOT/lib/common.sh'; echo \$MOLE_LOG_ROTATED")
[[ "$result" == "1" ]]
}
@test "drain_pending_input clears stdin buffer" {
# Test that drain_pending_input doesn't hang
result=$(echo -e "test\ninput" | HOME="$HOME" timeout 1 bash --noprofile --norc -c "source '$PROJECT_ROOT/lib/common.sh'; drain_pending_input; echo done")
[[ "$result" == "done" ]]
}
@test "bytes_to_human converts byte counts into readable units" {
output="$(
HOME="$HOME" bash --noprofile --norc << 'EOF'

View File

@@ -97,3 +97,49 @@ setup() {
fi
[ "$status" -ne 0 ]
}
@test "whitelist rejects paths with spaces" {
# Paths with spaces should be rejected by the new stricter validation
mkdir -p "$HOME/.config/mole"
echo "$HOME/.cache/invalid path" > "$WHITELIST_PATH"
# Load whitelist - path with space should be rejected
warnings=$(HOME="$HOME" bash --noprofile --norc -c "
source '$PROJECT_ROOT/bin/clean.sh'
load_whitelist_file 2>&1 | grep -c 'Invalid path format' || echo 0
")
[ "$warnings" -ge 1 ]
}
@test "whitelist rejects paths with consecutive slashes" {
mkdir -p "$HOME/.config/mole"
echo "$HOME/.cache//invalid" > "$WHITELIST_PATH"
warnings=$(HOME="$HOME" bash --noprofile --norc -c "
source '$PROJECT_ROOT/bin/clean.sh'
load_whitelist_file 2>&1 | grep -c 'Consecutive slashes' || echo 0
")
[ "$warnings" -ge 1 ]
}
@test "whitelist rejects system protected paths" {
mkdir -p "$HOME/.config/mole"
echo "/System/Library/test" > "$WHITELIST_PATH"
warnings=$(HOME="$HOME" bash --noprofile --norc -c "
source '$PROJECT_ROOT/bin/clean.sh'
load_whitelist_file 2>&1 | grep -c 'Protected system path' || echo 0
")
[ "$warnings" -ge 1 ]
}
@test "whitelist accepts valid paths with wildcards at end" {
mkdir -p "$HOME/.config/mole"
echo "$HOME/.cache/test/*" > "$WHITELIST_PATH"
# Should be accepted without warnings
HOME="$HOME" bash --noprofile --norc -c "
source '$PROJECT_ROOT/bin/clean.sh'
load_whitelist_file
" 2>&1 | grep -q "Invalid path format" && exit 1 || exit 0
}