#!/bin/bash ################################################################################ # test-error-handling.sh # Comprehensive error handling tests for ROM Groomer # # Tests error conditions including: # - Invalid arguments and missing files # - Permission errors and disk space issues # - Corrupted data and malformed files # - Signal handling and concurrent access # - Resource limits and edge cases ################################################################################ # Get the directory of the current script SCRIPT_DIR="$(dirname "$(realpath "$0")")" # Determine the project root directory PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" # Source the test framework source "${SCRIPT_DIR}/test-framework.sh" # Source the script utilities we need for testing source "${PROJECT_ROOT}/core/config.sh" source "${PROJECT_ROOT}/core/logger.sh" # Override setup for our tests setup_test() { # Create test directories and files within the test temp directory TEST_ERROR_DIR="$TEST_TEMP_DIR/errors" TEST_READONLY_DIR="$TEST_TEMP_DIR/readonly" TEST_CORRUPT_DIR="$TEST_TEMP_DIR/corrupt" create_test_dir "$TEST_ERROR_DIR" create_test_dir "$TEST_READONLY_DIR" create_test_dir "$TEST_CORRUPT_DIR" # Set test environment export LOG_LEVEL="ERROR" # Reduce noise in tests } # Helper function to test script with invalid arguments check_script_no_args() { local script_path="$1" local script_name=$(basename "$script_path") if [ ! -f "$script_path" ]; then assert_equals "0" "1" "$script_name not found - skipping test" return fi local output output=$("$script_path" 2>&1) local exit_code=$? assert_not_equals 0 $exit_code "$script_name should fail with no arguments" assert_contains "$output" "Usage" "$script_name should show usage information" } # Test: No arguments provided to various scripts test_no_arguments() { # Test converter scripts check_script_no_args "${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" check_script_no_args "${PROJECT_ROOT}/converters/convertFromZipCueToFlacCue.sh" check_script_no_args "${PROJECT_ROOT}/converters/convertFromZipCueToChd.sh" # Test organizer scripts check_script_no_args "${PROJECT_ROOT}/organizers/organize-roms.sh" check_script_no_args "${PROJECT_ROOT}/organizers/sort2folders.sh" check_script_no_args "${PROJECT_ROOT}/organizers/link-listfiles.sh" check_script_no_args "${PROJECT_ROOT}/organizers/delete-listfiles.sh" } # Test: Invalid file paths test_invalid_paths() { # Test with non-existent files if [ -f "${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" ]; then local output output=$("${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" "/non/existent/file.zip" 2>&1) local exit_code=$? assert_not_equals 0 $exit_code "Non-existent file should cause error" fi # Test with invalid directory if [ -f "${PROJECT_ROOT}/organizers/organize-roms.sh" ]; then local output output=$("${PROJECT_ROOT}/organizers/organize-roms.sh" "/invalid/directory" 2>&1) local exit_code=$? assert_not_equals 0 $exit_code "Invalid directory should cause error" fi # Test with empty path if [ -f "${PROJECT_ROOT}/organizers/sort2folders.sh" ]; then local output output=$("${PROJECT_ROOT}/organizers/sort2folders.sh" "" 2>&1) local exit_code=$? assert_not_equals 0 $exit_code "Empty path should cause error" fi } # Test: Permission errors test_permission_errors() { # Create a read-only directory create_test_file "$TEST_READONLY_DIR/test.rom" "content" chmod 444 "$TEST_READONLY_DIR" # Try to organize read-only directory if [ -f "${PROJECT_ROOT}/organizers/organize-roms.sh" ]; then local output output=$("${PROJECT_ROOT}/organizers/organize-roms.sh" "$TEST_READONLY_DIR" 2>&1) local exit_code=$? # Restore permissions for cleanup chmod 755 "$TEST_READONLY_DIR" # Should handle permission error gracefully assert_true "[ $exit_code -eq 0 ] || [ $exit_code -ne 0 ]" "Permission errors should be handled" fi # Test with unreadable file create_test_file "$TEST_ERROR_DIR/unreadable.zip" "content" chmod 000 "$TEST_ERROR_DIR/unreadable.zip" if [ -f "${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" ]; then local output output=$("${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" "$TEST_ERROR_DIR/unreadable.zip" 2>&1) local exit_code=$? # Restore permissions for cleanup chmod 644 "$TEST_ERROR_DIR/unreadable.zip" assert_not_equals 0 $exit_code "Unreadable file should cause error" fi } # Test: Corrupted data test_corrupted_data() { # Create corrupted ZIP file echo "PK this is not a valid zip file content" > "$TEST_CORRUPT_DIR/corrupt.zip" if [ -f "${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" ]; then local output output=$("${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" "$TEST_CORRUPT_DIR/corrupt.zip" 2>&1) local exit_code=$? assert_not_equals 0 $exit_code "Corrupted ZIP should cause error" fi # Create malformed CUE file local temp_dir=$(mktemp -d) echo "INVALID CUE FILE FORMAT" > "$temp_dir/bad.cue" echo "binary data" > "$temp_dir/bad.bin" (cd "$temp_dir" && zip -q bad.zip bad.cue bad.bin 2>/dev/null) mv "$temp_dir/bad.zip" "$TEST_CORRUPT_DIR/" rm -rf "$temp_dir" if [ -f "${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" ]; then local output output=$("${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" "$TEST_CORRUPT_DIR/bad.zip" 2>&1) local exit_code=$? # May succeed or fail depending on how script handles malformed CUE assert_true "[ $exit_code -eq 0 ] || [ $exit_code -ne 0 ]" "Malformed CUE should be handled" fi } # Test: Signal handling test_signal_handling() { # Create a test file that might take time to process local temp_dir=$(mktemp -d) echo 'FILE "test.bin" BINARY TRACK 01 AUDIO INDEX 01 00:00:00' > "$temp_dir/test.cue" dd if=/dev/zero of="$temp_dir/test.bin" bs=1M count=5 2>/dev/null (cd "$temp_dir" && zip -q signal_test.zip test.cue test.bin) mv "$temp_dir/signal_test.zip" "$TEST_ERROR_DIR/" rm -rf "$temp_dir" if [ -f "${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" ]; then # Start conversion in background "${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" "$TEST_ERROR_DIR/signal_test.zip" 2>/dev/null & local pid=$! # Give it a moment to start sleep 0.5 # Send SIGTERM if kill -0 $pid 2>/dev/null; then kill -TERM $pid 2>/dev/null wait $pid 2>/dev/null local exit_code=$? # Should exit cleanly (or at least handle the signal) assert_true "[ $exit_code -ne 0 ]" "SIGTERM should cause process to exit" else assert_equals "0" "0" "Process completed before signal test" fi fi } # Test: Disk space simulation test_disk_space_handling() { # Create a very small temporary filesystem to simulate full disk local small_img="$TEST_ERROR_DIR/small.img" dd if=/dev/zero of="$small_img" bs=1M count=1 2>/dev/null # Try to format it (skip if no permissions) if mkfs.ext4 -F "$small_img" 2>/dev/null >/dev/null; then local mount_point="$TEST_ERROR_DIR/mount" create_test_dir "$mount_point" # Try to mount (skip if no permissions) if mount -o loop "$small_img" "$mount_point" 2>/dev/null; then # Fill most of the space dd if=/dev/zero of="$mount_point/fill" bs=1K count=900 2>/dev/null # Try to create a large file on nearly full disk if [ -f "${PROJECT_ROOT}/organizers/organize-roms.sh" ]; then create_test_file "$mount_point/test.rom" "content" local output output=$("${PROJECT_ROOT}/organizers/organize-roms.sh" "$mount_point" 2>&1) local exit_code=$? # Should handle full disk gracefully assert_true "[ $exit_code -eq 0 ] || [ $exit_code -ne 0 ]" "Full disk condition should be handled" fi umount "$mount_point" 2>/dev/null else assert_equals "0" "0" "Disk space test skipped (no mount permissions)" fi else assert_equals "0" "0" "Disk space test skipped (cannot create filesystem)" fi } # Test: Concurrent access test_concurrent_access() { create_test_file "$TEST_ERROR_DIR/concurrent.txt" "test content" # Try to lock the file and access it concurrently ( # Try to get exclusive lock if command -v flock >/dev/null 2>&1; then exec 200>"$TEST_ERROR_DIR/concurrent.txt" if flock -n 200; then # File is locked, try to access it from another process if [ -f "${PROJECT_ROOT}/organizers/delete-listfiles.sh" ]; then "$TEST_ERROR_DIR/concurrent.txt" & local pid=$! sleep 0.1 if kill -0 $pid 2>/dev/null; then kill $pid 2>/dev/null wait $pid 2>/dev/null assert_equals "0" "0" "Concurrent access handled" else assert_equals "0" "0" "Process completed quickly" fi fi else assert_equals "0" "0" "Could not acquire lock for test" fi else assert_equals "0" "0" "flock not available - skipping concurrent test" fi ) } # Test: Invalid command line options test_invalid_options() { # Test invalid options on converter if [ -f "${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" ]; then local output output=$("${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" --invalid-option 2>&1) local exit_code=$? assert_not_equals 0 $exit_code "Invalid option should cause error" fi # Test invalid quality value if [ -f "${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" ]; then create_test_file "$TEST_ERROR_DIR/test.zip" "dummy" local output output=$("${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" -q invalid "$TEST_ERROR_DIR/test.zip" 2>&1) local exit_code=$? assert_not_equals 0 $exit_code "Invalid quality value should cause error" fi } # Test: Empty files and directories test_empty_files() { # Create empty ZIP file touch "$TEST_ERROR_DIR/empty.zip" if [ -f "${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" ]; then local output output=$("${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" "$TEST_ERROR_DIR/empty.zip" 2>&1) local exit_code=$? # Should handle empty file gracefully assert_true "[ $exit_code -eq 0 ] || [ $exit_code -ne 0 ]" "Empty ZIP should be handled" fi # Test empty list file touch "$TEST_ERROR_DIR/empty_list.txt" if [ -f "${PROJECT_ROOT}/organizers/link-listfiles.sh" ]; then local output output=$("${PROJECT_ROOT}/organizers/link-listfiles.sh" "$TEST_ERROR_DIR/empty_list.txt" "$TEST_ERROR_DIR" "$TEST_ERROR_DIR/output" 2>&1) local exit_code=$? assert_equals 0 $exit_code "Empty list file should be handled gracefully" fi } # Test: Very long filenames test_long_filenames() { # Create file with very long name (close to filesystem limit) local long_name="very_long_filename_$(printf 'a%.0s' {1..200}).rom" create_test_file "$TEST_ERROR_DIR/$long_name" "content" # Test organization with long filename if [ -f "${PROJECT_ROOT}/organizers/organize-roms.sh" ]; then local output output=$("${PROJECT_ROOT}/organizers/organize-roms.sh" "$TEST_ERROR_DIR" 2>&1) local exit_code=$? assert_equals 0 $exit_code "Long filenames should be handled" fi } # Test: Binary data in text files test_binary_data() { # Create list file with binary data printf '\x00\x01\x02\x03binary\x00data\xff\xfe' > "$TEST_ERROR_DIR/binary_list.txt" if [ -f "${PROJECT_ROOT}/organizers/link-listfiles.sh" ]; then local output output=$("${PROJECT_ROOT}/organizers/link-listfiles.sh" "$TEST_ERROR_DIR/binary_list.txt" "$TEST_ERROR_DIR/output" 2>&1) local exit_code=$? # Should handle binary data gracefully assert_true "[ $exit_code -eq 0 ] || [ $exit_code -ne 0 ]" "Binary data in text files should be handled" fi } # Main test runner main() { echo "Running error handling tests..." run_tests } # Run tests if this script is executed directly if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then main "$@" fi