#!/bin/bash ################################################################################ # test-converters.sh # Comprehensive tests for ROM converter scripts # # Tests all conversion functionality including: # - ZIP to OGG/CUE conversion # - ZIP to FLAC/CUE conversion # - ZIP to CHD conversion # - Error handling and edge cases # - Performance with various file sizes ################################################################################ # 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_CONVERTERS_DIR="$TEST_TEMP_DIR/converters" TEST_INPUT_DIR="$TEST_TEMP_DIR/input" TEST_OUTPUT_DIR="$TEST_TEMP_DIR/output" create_test_dir "$TEST_CONVERTERS_DIR" create_test_dir "$TEST_INPUT_DIR" create_test_dir "$TEST_OUTPUT_DIR" # Set test environment export LOG_LEVEL="ERROR" # Reduce noise in tests } # Helper function to create test ZIP with CUE/BIN create_test_zip_with_cue() { local zip_name="$1" local cue_content="$2" local bin_size="${3:-1024}" # Default 1KB local temp_dir=$(mktemp -d) echo -e "$cue_content" > "${temp_dir}/test.cue" dd if=/dev/zero of="${temp_dir}/test.bin" bs=1 count="$bin_size" 2>/dev/null (cd "$temp_dir" && zip -q "$zip_name" test.cue test.bin) mv "${temp_dir}/${zip_name}" "${TEST_INPUT_DIR}/" rm -rf "$temp_dir" } # Helper function to create invalid ZIP create_invalid_zip() { local zip_name="$1" echo "PK this is not a valid zip file" > "${TEST_INPUT_DIR}/${zip_name}" } # Test: Valid ZIP to OGG conversion test_valid_zip_to_ogg_conversion() { local cue_content='FILE "test.bin" BINARY\n TRACK 01 MODE1/2352\n INDEX 01 00:00:00\n TRACK 02 AUDIO\n INDEX 00 00:00:00\n INDEX 01 00:02:00' create_test_zip_with_cue "test_valid.zip" "$cue_content" 2048 # Run converter local output output=$("${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" "${TEST_INPUT_DIR}/test_valid.zip" 2>&1) local exit_code=$? assert_equals 0 $exit_code "Valid ZIP conversion should succeed" # Check if CUE file was updated (should exist in same directory as ZIP) assert_file_exists "${TEST_INPUT_DIR}/test.cue" "Updated CUE file should be created" } # Test: Invalid ZIP file handling test_invalid_zip_file() { create_invalid_zip "invalid.zip" # Run converter and expect failure local output output=$("${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" "${TEST_INPUT_DIR}/invalid.zip" 2>&1) local exit_code=$? assert_not_equals 0 $exit_code "Invalid ZIP should fail with non-zero exit code" } # Test: Missing CUE file in ZIP test_missing_cue_file() { local temp_dir=$(mktemp -d) dd if=/dev/zero of="${temp_dir}/test.bin" bs=1 count=1024 2>/dev/null (cd "$temp_dir" && zip -q "no_cue.zip" test.bin) mv "${temp_dir}/no_cue.zip" "${TEST_INPUT_DIR}/" rm -rf "$temp_dir" # Run converter local output output=$("${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" "${TEST_INPUT_DIR}/no_cue.zip" 2>&1) local exit_code=$? # Should handle gracefully (may succeed with no conversion or fail appropriately) assert_true "[ $exit_code -eq 0 ] || [ $exit_code -ne 0 ]" "Missing CUE file should be handled" } # Test: Quality parameter test_quality_parameter() { local cue_content='FILE "test.bin" BINARY\n TRACK 01 AUDIO\n INDEX 01 00:00:00' create_test_zip_with_cue "test_quality.zip" "$cue_content" 1024 # Run with quality parameter local output output=$("${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" -q 5 "${TEST_INPUT_DIR}/test_quality.zip" 2>&1) local exit_code=$? assert_equals 0 $exit_code "Quality parameter should be accepted" } # Test: Delete original option test_delete_original() { local cue_content='FILE "test.bin" BINARY\n TRACK 01 AUDIO\n INDEX 01 00:00:00' create_test_zip_with_cue "test_delete.zip" "$cue_content" 1024 # Verify file exists before deletion assert_file_exists "${TEST_INPUT_DIR}/test_delete.zip" "ZIP file should exist before deletion test" # Run with delete option local output output=$("${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" -d "${TEST_INPUT_DIR}/test_delete.zip" 2>&1) local exit_code=$? if [ $exit_code -eq 0 ]; then # Check if original ZIP was deleted (only if conversion succeeded) assert_file_not_exists "${TEST_INPUT_DIR}/test_delete.zip" "Delete option should remove original ZIP on success" else # If conversion failed, file should still exist assert_file_exists "${TEST_INPUT_DIR}/test_delete.zip" "Original ZIP should remain if conversion failed" fi } # Test: Recursive directory processing test_recursive_processing() { # Create subdirectory with ZIP create_test_dir "${TEST_INPUT_DIR}/subdir" local cue_content='FILE "test.bin" BINARY\n TRACK 01 AUDIO\n INDEX 01 00:00:00' local temp_dir=$(mktemp -d) echo -e "$cue_content" > "${temp_dir}/test.cue" dd if=/dev/zero of="${temp_dir}/test.bin" bs=1 count=1024 2>/dev/null (cd "$temp_dir" && zip -q "recursive_test.zip" test.cue test.bin) mv "${temp_dir}/recursive_test.zip" "${TEST_INPUT_DIR}/subdir/" rm -rf "$temp_dir" # Run recursive local output output=$("${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" -r "${TEST_INPUT_DIR}" 2>&1) local exit_code=$? assert_equals 0 $exit_code "Recursive processing should work" } # Test: Special characters in filenames test_special_characters() { local special_name="test with spaces & special (chars).zip" local cue_content='FILE "test.bin" BINARY\n TRACK 01 AUDIO\n INDEX 01 00:00:00' local temp_dir=$(mktemp -d) echo -e "$cue_content" > "${temp_dir}/test.cue" dd if=/dev/zero of="${temp_dir}/test.bin" bs=1 count=1024 2>/dev/null (cd "$temp_dir" && zip -q "${special_name}" test.cue test.bin) mv "${temp_dir}/${special_name}" "${TEST_INPUT_DIR}/" rm -rf "$temp_dir" # Run converter local output output=$("${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" "${TEST_INPUT_DIR}/${special_name}" 2>&1) local exit_code=$? assert_equals 0 $exit_code "Special characters in filename should be handled" } # Test: No arguments provided test_no_arguments() { local output output=$("${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" 2>&1) local exit_code=$? assert_not_equals 0 $exit_code "No arguments should cause error" assert_contains "$output" "Usage" "Should show usage information" } # Test: Non-existent file test_nonexistent_file() { local output output=$("${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" "/nonexistent/file.zip" 2>&1) local exit_code=$? assert_not_equals 0 $exit_code "Non-existent file should cause error" } # Test: Empty ZIP file test_empty_zip() { local temp_dir=$(mktemp -d) # Create a truly empty ZIP file touch "${temp_dir}/empty.zip" printf "PK\x03\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" > "${temp_dir}/empty.zip" printf "PK\x05\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" >> "${temp_dir}/empty.zip" mv "${temp_dir}/empty.zip" "${TEST_INPUT_DIR}/" rm -rf "$temp_dir" local output output=$("${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" "${TEST_INPUT_DIR}/empty.zip" 2>&1) local exit_code=$? # Should handle empty ZIP gracefully (likely with error) assert_true "[ $exit_code -eq 0 ] || [ $exit_code -ne 0 ]" "Empty ZIP should be handled gracefully" } # Test: Help option test_help_option() { local output output=$("${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" --help 2>&1) local exit_code=$? # Help should exit with 1 (usage) but show usage info assert_contains "$output" "Usage" "Help option should show usage" } # Test: Large file handling (simulated) test_large_file_handling() { local cue_content='FILE "large.bin" BINARY\n TRACK 01 AUDIO\n INDEX 01 00:00:00' # Create a larger test file (1MB) local temp_dir=$(mktemp -d) echo -e "$cue_content" > "${temp_dir}/large.cue" dd if=/dev/zero of="${temp_dir}/large.bin" bs=1M count=1 2>/dev/null (cd "$temp_dir" && zip -q "large.zip" large.cue large.bin) mv "${temp_dir}/large.zip" "${TEST_INPUT_DIR}/" rm -rf "$temp_dir" # Run converter with timeout to prevent hanging timeout 30s "${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" "${TEST_INPUT_DIR}/large.zip" 2>/dev/null local exit_code=$? # Should complete within timeout (exit code 124 means timeout) assert_not_equals 124 $exit_code "Large file conversion should complete within 30 seconds" } # Test: Output validation and quality test_output_validation() { local cue_content='FILE "test.bin" BINARY\n TRACK 01 MODE1/2352\n INDEX 01 00:00:00\n TRACK 02 AUDIO\n INDEX 00 00:00:00\n INDEX 01 00:02:00' create_test_zip_with_cue "test_output.zip" "$cue_content" 4096 # Run converter local output output=$("${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" "${TEST_INPUT_DIR}/test_output.zip" 2>&1) local exit_code=$? assert_equals 0 $exit_code "Conversion should succeed" # Check if OGG file was created if [ -f "${TEST_INPUT_DIR}/test.ogg" ]; then assert_file_exists "${TEST_INPUT_DIR}/test.ogg" "OGG file should be created" # Verify OGG file is not empty and has correct format assert_true "[ -s '${TEST_INPUT_DIR}/test.ogg' ]" "OGG file should not be empty" # Check if file command recognizes it as OGG local file_type=$(file "${TEST_INPUT_DIR}/test.ogg" 2>/dev/null || echo "unknown") assert_contains "$file_type" "Ogg" "Output should be recognized as OGG format" fi # Check if CUE file was updated to reference OGG if [ -f "${TEST_INPUT_DIR}/test.cue" ]; then local cue_content_updated=$(cat "${TEST_INPUT_DIR}/test.cue") assert_contains "$cue_content_updated" "test.ogg" "CUE file should reference OGG file" fi } # Test: Missing audio conversion tools test_missing_audio_tools() { # Skip this test if we can't reliably test missing dependencies # The converter script has fallback mechanisms that make this test complex local cue_content='FILE "test.bin" BINARY\n TRACK 01 AUDIO\n INDEX 01 00:00:00' create_test_zip_with_cue "test_no_tools.zip" "$cue_content" 1024 # Run converter normally - just verify it doesn't crash local output output=$("${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" "${TEST_INPUT_DIR}/test_no_tools.zip" 2>&1) local exit_code=$? # Should handle the conversion attempt gracefully (may succeed or fail) assert_true "[ $exit_code -eq 0 ] || [ $exit_code -ne 0 ]" "Should handle conversion attempt gracefully" # Log the result for debugging if [ "$exit_code" -eq 0 ]; then echo " Info: Conversion succeeded (tools available or fallback worked)" else echo " Info: Conversion failed (exit code: $exit_code)" fi } # Test: Concurrent processing safety test_concurrent_processing() { # Create multiple test files local cue_content='FILE "test.bin" BINARY\n TRACK 01 AUDIO\n INDEX 01 00:00:00' for i in {1..3}; do create_test_zip_with_cue "concurrent_test_${i}.zip" "$cue_content" 1024 done # Run multiple conversions in parallel local pids=() for i in {1..3}; do "${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" "${TEST_INPUT_DIR}/concurrent_test_${i}.zip" & pids+=($!) done # Wait for all to complete local all_success=true for pid in "${pids[@]}"; do if ! wait "$pid"; then all_success=false fi done assert_true "$all_success" "Concurrent processing should handle multiple files safely" } # Test: File permissions and cleanup test_file_permissions_cleanup() { local cue_content='FILE "test.bin" BINARY\n TRACK 01 AUDIO\n INDEX 01 00:00:00' create_test_zip_with_cue "test_permissions.zip" "$cue_content" 1024 # Set restrictive permissions on input file chmod 444 "${TEST_INPUT_DIR}/test_permissions.zip" # Run converter local output output=$("${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" "${TEST_INPUT_DIR}/test_permissions.zip" 2>&1) local exit_code=$? # Should handle read-only files appropriately assert_true "[ $exit_code -eq 0 ] || [ $exit_code -ne 0 ]" "Should handle read-only files gracefully" # Check no temp files are left behind local temp_files=$(find "${TEST_INPUT_DIR}" -name "*.tmp" -o -name "oggcue-*" 2>/dev/null | wc -l) assert_equals 0 $temp_files "No temporary files should be left behind" } # Test: Stress test with malformed CUE content test_malformed_cue_content() { # Create ZIP with malformed CUE file local malformed_cue='INVALID CUE CONTENT\nRANDOM GARBAGE\nFILE "test.bin" WRONG_FORMAT' local temp_dir=$(mktemp -d) echo -e "$malformed_cue" > "${temp_dir}/malformed.cue" dd if=/dev/zero of="${temp_dir}/test.bin" bs=1 count=1024 2>/dev/null (cd "$temp_dir" && zip -q "malformed.zip" malformed.cue test.bin) mv "${temp_dir}/malformed.zip" "${TEST_INPUT_DIR}/" rm -rf "$temp_dir" # Run converter local output output=$("${PROJECT_ROOT}/converters/convertFromZipCueToOggCue.sh" "${TEST_INPUT_DIR}/malformed.zip" 2>&1) local exit_code=$? # Should handle malformed CUE files gracefully assert_true "[ $exit_code -eq 0 ] || [ $exit_code -ne 0 ]" "Should handle malformed CUE files gracefully" } # Main test runner main() { echo "Running converter tests..." run_tests } # Run tests if this script is executed directly if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then main "$@" fi