#!/usr/bin/env bash # Test: behavior when a cached file is corrupted on disk # # Verifies what happens when a file in the VFS cache is corrupted between # warpgate restarts. rclone may serve the corrupted data from cache or # re-fetch from the remote — this test documents the observed behavior. # # Sequence: # 1. Create a test file on the NAS with known content. # 2. Start warpgate, read file through mount to cache it. # 3. Stop warpgate gracefully. # 4. Overwrite the cached file with garbage data. # 5. Restart warpgate with a short dir_cache_time (1s). # 6. Wait for dir cache to expire, then read through mount. # 7. Compare result against expected content and document behavior. set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" source "$SCRIPT_DIR/../harness/helpers.sh" source "$SCRIPT_DIR/../harness/mock-nas.sh" require_root setup_test_env trap teardown_test_env EXIT # Start the mock NAS and create a test file start_mock_nas nas_create_file_content "corrupt-test.txt" "original" # Generate config with short dir_cache_time so rclone rechecks the remote gen_config dir_cache_time=1s # Start warpgate and wait for readiness start_warpgate wait_for_mount wait_for_rc_api # Read file through the FUSE mount to pull it into cache content_before=$(cat "$TEST_MOUNT/corrupt-test.txt") if [[ "$content_before" != "original" ]]; then echo "FAIL: initial read returned unexpected content: $content_before" >&2 exit 1 fi assert_cached "corrupt-test.txt" echo "INFO: file cached successfully, content: '$content_before'" # Stop warpgate gracefully stop_warpgate # Corrupt the cached file by overwriting with garbage cache_file="$CACHE_DIR/vfs/nas/corrupt-test.txt" if [[ ! -f "$cache_file" ]]; then echo "FAIL: cache file not found at $cache_file" >&2 exit 1 fi echo -n "CORRUPTED-GARBAGE-DATA" > "$cache_file" echo "INFO: corrupted cache file at $cache_file" # Restart warpgate start_warpgate wait_for_mount 60 wait_for_rc_api # Wait for the dir cache to expire (1s dir_cache_time + margin) sleep 3 # Read the file through the mount again content_after=$(cat "$TEST_MOUNT/corrupt-test.txt") echo "INFO: content after restart with corrupted cache: '$content_after'" # The corrupted cache file has a different size than the NAS original # ("CORRUPTED-GARBAGE-DATA" = 22 bytes vs "original" = 8 bytes). # rclone VFS should detect the size mismatch and re-fetch from the remote. # We MUST verify the file matches the NAS original — serving corrupted data # is a data integrity failure. nas_original=$(nas_read_file "corrupt-test.txt") if [[ "$content_after" != "$nas_original" ]]; then echo "FAIL: content after restart does not match NAS original" >&2 echo " NAS original: '$nas_original'" >&2 echo " mount returned: '$content_after'" >&2 exit 1 fi echo "INFO: rclone correctly re-fetched file from remote (corruption detected)" echo "PASS: $(basename "$0" .sh)"