| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Copyright (c) 2016 Jeromy Johnson |
| 4 | # MIT Licensed; see the LICENSE file in this repository. |
| 5 | # |
| 6 | # NOTE: This is a legacy sharness test kept for compatibility. |
| 7 | # New tests for 'ipfs repo verify' should be added to test/cli/repo_verify_test.go |
| 8 | # |
| 9 | |
| 10 | test_description="Test ipfs repo fsck" |
| 11 | |
| 12 | . lib/test-lib.sh |
| 13 | |
| 14 | test_init_ipfs |
| 15 | |
| 16 | sort_rand() { |
| 17 | case `uname` in |
| 18 | Linux|FreeBSD) |
| 19 | sort -R |
| 20 | ;; |
| 21 | Darwin) |
| 22 | ruby -e 'puts STDIN.readlines.shuffle' |
| 23 | ;; |
| 24 | *) |
| 25 | echo "unsupported system: $(uname)" |
| 26 | esac |
| 27 | } |
| 28 | |
| 29 | check_random_corruption() { |
| 30 | # Exclude well-known blocks from corruption as they cause test flakiness: |
| 31 | # - CIQL7TG2PB52XIZLLHDYIUFMHUQLMMZWBNBZSLDXFCPZ5VDNQQ2WDZQ.data: empty file block |
| 32 | # - CIQFTFEEHEDF6KLBT32BFAGLXEZL4UWFNWM4LFTLMXQBCERZ6CMLX3Y.data: empty directory block (has special handling, served from memory even when corrupted on disk) |
| 33 | to_break=$(find "$IPFS_PATH/blocks" -type f -name '*.data' | grep -v -E "CIQL7TG2PB52XIZLLHDYIUFMHUQLMMZWBNBZSLDXFCPZ5VDNQQ2WDZQ.data|CIQFTFEEHEDF6KLBT32BFAGLXEZL4UWFNWM4LFTLMXQBCERZ6CMLX3Y.data" | sort_rand | head -n 1) |
| 34 | |
| 35 | test_expect_success "back up file and overwrite it" ' |
| 36 | cp "$to_break" backup_file && |
| 37 | echo "this is super broken" > "$to_break" |
| 38 | ' |
| 39 | |
| 40 | test_expect_success "repo verify detects failure" ' |
| 41 | test_expect_code 1 ipfs repo verify |
| 42 | ' |
| 43 | |
| 44 | test_expect_success "replace the object" ' |
| 45 | cp backup_file "$to_break" |
| 46 | ' |
| 47 | |
| 48 | test_expect_success "ipfs repo verify passes just fine now" ' |
| 49 | ipfs repo verify |
| 50 | ' |
| 51 | } |
| 52 | |
| 53 | test_expect_success "create some files" ' |
| 54 | random-files -depth=3 -dirs=4 -files=10 foobar > /dev/null |
| 55 | ' |
| 56 | |
| 57 | test_expect_success "add them all" ' |
| 58 | ipfs add -r -q foobar > /dev/null |
| 59 | ' |
| 60 | |
| 61 | for i in `seq 20` |
| 62 | do |
| 63 | check_random_corruption |
| 64 | done |
| 65 | |
| 66 | test_done |