| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Copyright (c) 2016 Jeromy Johnson |
| 4 | # MIT Licensed; see the LICENSE file in this repository. |
| 5 | # |
| 6 | |
| 7 | test_description="Test robustness of garbage collector" |
| 8 | |
| 9 | . lib/test-lib.sh |
| 10 | set -e |
| 11 | |
| 12 | to_raw_cid() { |
| 13 | ipfs cid format -b b --mc raw -v 1 "$1" |
| 14 | } |
| 15 | |
| 16 | test_gc_robust_part1() { |
| 17 | |
| 18 | test_expect_success "add a 1MB file with --raw-leaves" ' |
| 19 | random-data -size=1048576 -seed=56 > afile && |
| 20 | HASH1=`ipfs add --raw-leaves -q --cid-version 1 afile` && |
| 21 | REFS=`ipfs refs -r $HASH1` && |
| 22 | read LEAF1 LEAF2 LEAF3 LEAF4 < <(echo $REFS) |
| 23 | ' |
| 24 | |
| 25 | test_expect_success "find data blocks for added file" ' |
| 26 | HASH1MH=`cid-fmt -b base32 "%M" $HASH1` && |
| 27 | LEAF1MH=`cid-fmt -b base32 "%M" $LEAF1` && |
| 28 | LEAF2MH=`cid-fmt -b base32 "%M" $LEAF2` && |
| 29 | HASH1FILE=`find .ipfs/blocks -type f | grep -i $HASH1MH` && |
| 30 | LEAF1FILE=`find .ipfs/blocks -type f | grep -i $LEAF1MH` && |
| 31 | LEAF2FILE=`find .ipfs/blocks -type f | grep -i $LEAF2MH` |
| 32 | ' |
| 33 | |
| 34 | test_expect_success "remove a leaf node from the repo manually" ' |
| 35 | rm "$LEAF1FILE" |
| 36 | ' |
| 37 | |
| 38 | test_expect_success "check that the node is removed" ' |
| 39 | test_must_fail ipfs cat $HASH1 |
| 40 | ' |
| 41 | |
| 42 | test_expect_success "'ipfs repo gc' should still be fine" ' |
| 43 | ipfs repo gc |
| 44 | ' |
| 45 | |
| 46 | test_expect_success "corrupt the root node of 1MB file" ' |
| 47 | test -e "$HASH1FILE" && |
| 48 | dd if=/dev/zero of="$HASH1FILE" count=1 bs=100 conv=notrunc |
| 49 | ' |
| 50 | |
| 51 | test_expect_success "'ipfs repo gc' should abort without removing anything" ' |
| 52 | test_must_fail ipfs repo gc 2>&1 | tee gc_err && |
| 53 | grep -q "could not retrieve links for $HASH1" gc_err && |
| 54 | grep -q "aborted" gc_err |
| 55 | ' |
| 56 | |
| 57 | test_expect_success "leaf nodes were not removed after gc" ' |
| 58 | ipfs cat $LEAF3 > /dev/null && |
| 59 | ipfs cat $LEAF4 > /dev/null |
| 60 | ' |
| 61 | |
| 62 | test_expect_success "unpin the 1MB file" ' |
| 63 | ipfs pin rm $HASH1 |
| 64 | ' |
| 65 | |
| 66 | # make sure the permission problem is fixed on exit, otherwise cleanup |
| 67 | # will fail |
| 68 | trap "chmod 700 `dirname "$LEAF2FILE"` 2> /dev/null || true" 0 |
| 69 | |
| 70 | test_expect_success "create a permission problem" ' |
| 71 | chmod 500 `dirname "$LEAF2FILE"` && |
| 72 | test_must_fail ipfs block rm $LEAF2 2>&1 | tee block_rm_err && |
| 73 | grep -q "permission denied" block_rm_err |
| 74 | ' |
| 75 | |
| 76 | # repo gc outputs raw multihashes. We check HASH1 with block stat rather than |
| 77 | # grepping the output since it's not a raw multihash |
| 78 | test_expect_success "'ipfs repo gc' should still run and remove as much as possible" ' |
| 79 | test_must_fail ipfs repo gc 2>&1 | tee repo_gc_out && |
| 80 | grep -q "could not remove $LEAF2" repo_gc_out && |
| 81 | grep -q "removed $(to_raw_cid $LEAF3)" repo_gc_out && |
| 82 | grep -q "removed $(to_raw_cid $LEAF4)" repo_gc_out && |
| 83 | test_must_fail ipfs block stat $HASH1 |
| 84 | ' |
| 85 | |
| 86 | test_expect_success "fix the permission problem" ' |
| 87 | chmod 700 `dirname "$LEAF2FILE"` |
| 88 | ' |
| 89 | |
| 90 | test_expect_success "'ipfs repo gc' should be ok now" ' |
| 91 | ipfs repo gc | tee repo_gc_out |
| 92 | grep -q "removed $(to_raw_cid $LEAF2)" repo_gc_out |
| 93 | ' |
| 94 | } |
| 95 | |
| 96 | test_gc_robust_part2() { |
| 97 | |
| 98 | test_expect_success "add 1MB file normally (i.e., without raw leaves)" ' |
| 99 | random-data -size=1048576 -seed=56 > afile && |
| 100 | HASH2=`ipfs add -q afile` |
| 101 | ' |
| 102 | |
| 103 | LEAF1=QmcNNR6JSCUhJ9nyoVQgBhABPgcgdsuYJgdSB1f2g6BF5c |
| 104 | LEAF1FILE=.ipfs/blocks/RA/CIQNA5C3BLRUX3LZ7X6UTOV3KSHLARNXVDK3W5KUO6GVHNRP4SGLRAY.data |
| 105 | |
| 106 | LEAF2=QmPvtiBLgwuwF2wyf9VL8PaYgSt1XwGJ2Yu4AscRGEQvqR |
| 107 | LEAF2FILE=.ipfs/blocks/RN/CIQBPIKEATBI7TIHVYRQJZAKEWF2H22PXW3A7LCEPB6MFFL7IA2CRNA.data |
| 108 | |
| 109 | |
| 110 | test_expect_success "add some additional unpinned content" ' |
| 111 | random-data -size=1000 -seed=3 > junk1 && |
| 112 | random-data -size=1000 -seed=4 > junk2 && |
| 113 | JUNK1=`ipfs add --pin=false -q junk1` && |
| 114 | JUNK2=`ipfs add --pin=false -q junk2` |
| 115 | ' |
| 116 | |
| 117 | test_expect_success "remove a leaf node from the repo manually" ' |
| 118 | rm "$LEAF1FILE" |
| 119 | ' |
| 120 | |
| 121 | test_expect_success "'ipfs repo gc' should abort" ' |
| 122 | test_must_fail ipfs repo gc 2>&1 | tee repo_gc_out && |
| 123 | grep -q "could not retrieve links for $LEAF1" repo_gc_out && |
| 124 | grep -q "aborted" repo_gc_out |
| 125 | ' |
| 126 | |
| 127 | test_expect_success "test that garbage collector really aborted" ' |
| 128 | ipfs cat $JUNK1 > /dev/null && |
| 129 | ipfs cat $JUNK2 > /dev/null |
| 130 | ' |
| 131 | |
| 132 | test_expect_success "corrupt a key" ' |
| 133 | test -e "$LEAF2FILE" && |
| 134 | dd if=/dev/zero of="$LEAF2FILE" count=1 bs=100 conv=notrunc |
| 135 | ' |
| 136 | |
| 137 | test_expect_success "'ipfs repo gc' should abort with two errors" ' |
| 138 | test_must_fail ipfs repo gc 2>&1 | tee repo_gc_out && |
| 139 | grep -q "could not retrieve links for $LEAF1" repo_gc_out && |
| 140 | grep -q "could not retrieve links for $LEAF2" repo_gc_out && |
| 141 | grep -q "aborted" repo_gc_out |
| 142 | ' |
| 143 | |
| 144 | test_expect_success "'ipfs repo gc --stream-errors' should abort and report each error separately" ' |
| 145 | test_must_fail ipfs repo gc --stream-errors 2>&1 | tee repo_gc_out && |
| 146 | grep -q "Error: could not retrieve links for $LEAF1" repo_gc_out && |
| 147 | grep -q "Error: could not retrieve links for $LEAF2" repo_gc_out && |
| 148 | grep -q "Error: garbage collection aborted" repo_gc_out |
| 149 | ' |
| 150 | |
| 151 | test_expect_success "unpin 1MB file" ' |
| 152 | ipfs pin rm $HASH2 |
| 153 | ' |
| 154 | |
| 155 | test_expect_success "'ipfs repo gc' should be fine now" ' |
| 156 | ipfs repo gc | tee repo_gc_out && |
| 157 | grep -q "removed $(to_raw_cid $HASH2)" repo_gc_out && |
| 158 | grep -q "removed $(to_raw_cid $LEAF2)" repo_gc_out |
| 159 | ' |
| 160 | } |
| 161 | |
| 162 | test_init_ipfs |
| 163 | |
| 164 | test_gc_robust_part1 |
| 165 | test_gc_robust_part2 |
| 166 | |
| 167 | test_launch_ipfs_daemon_without_network |
| 168 | |
| 169 | test_gc_robust_part1 |
| 170 | test_gc_robust_part2 |
| 171 | |
| 172 | test_kill_ipfs_daemon |
| 173 | |
| 174 | test_done |
| 175 |