| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Copyright (c) 2014 Jeromy Johnson |
| 4 | # MIT Licensed; see the LICENSE file in this repository. |
| 5 | # |
| 6 | |
| 7 | test_description="Test ipfs repo pinning" |
| 8 | |
| 9 | . lib/test-lib.sh |
| 10 | |
| 11 | test_pin_flag() { |
| 12 | object=$1 |
| 13 | ptype=$2 |
| 14 | expect=$3 |
| 15 | |
| 16 | echo "test_pin_flag" "$@" |
| 17 | |
| 18 | if ipfs pin ls --type="$ptype" "$object" >actual |
| 19 | then |
| 20 | test "$expect" = "true" && return |
| 21 | test_fsh cat actual |
| 22 | return |
| 23 | else |
| 24 | test "$expect" = "false" && return |
| 25 | test_fsh cat actual |
| 26 | return |
| 27 | fi |
| 28 | } |
| 29 | |
| 30 | test_pin() { |
| 31 | object=$1 |
| 32 | shift |
| 33 | |
| 34 | test_str_contains "recursive" $@ |
| 35 | [ "$?" = "0" ] && r="true" || r="false" |
| 36 | |
| 37 | test_str_contains "indirect" $@ |
| 38 | [ "$?" = "0" ] && i="true" || i="false" |
| 39 | |
| 40 | test_str_contains "direct" $@ |
| 41 | [ "$?" = "0" ] && d="true" || d="false" |
| 42 | |
| 43 | test_pin_flag "$object" "recursive" $r || return 1 |
| 44 | test_pin_flag "$object" "indirect" $i || return 1 |
| 45 | test_pin_flag "$object" "direct" $d || return 1 |
| 46 | return 0 |
| 47 | } |
| 48 | |
| 49 | |
| 50 | test_init_ipfs |
| 51 | |
| 52 | # test runs much faster without daemon. |
| 53 | # TODO: turn this back on after: |
| 54 | # https://github.com/ipfs/go-ipfs/issues/1075 |
| 55 | # test_launch_ipfs_daemon |
| 56 | |
| 57 | HASH_FILE6="QmRsBC3Y2G6VRPYGAVpZczx1W7Xw54MtM1NcLKTkn6rx3U" |
| 58 | HASH_FILE5="QmaN3PtyP8DcVGHi3Q2Fcp7CfAFVcVXKddWbHoNvaA41zf" |
| 59 | HASH_FILE4="QmV1aiVgpDknKQugrK59uBUbMrPnsQM1F9FXbFcfgEvUvH" |
| 60 | HASH_FILE3="QmZrr4Pzqp3NnMzMfbMhNe7LghfoUFHVx7c9Po9GZrhKZ7" |
| 61 | HASH_FILE2="QmSkjTornLY72QhmK9NvAz26815pTaoAL42rF8Qi3w2WBP" |
| 62 | HASH_FILE1="QmbgX4aXhSSY88GHmPQ4roizD8wFwPX8jzTLjc8VAp89x4" |
| 63 | HASH_DIR4="QmW98gV71Ns4bX7QbgWAqLiGF3SDC1JpveZSgBh4ExaSAd" |
| 64 | HASH_DIR3="QmRsCaNBMkweZ9vHT5PJRd2TT9rtNKEKyuognCEVxZxF1H" |
| 65 | HASH_DIR2="QmTUTQAgeVfughDSFukMZLbfGvetDJY7Ef5cDXkKK4abKC" |
| 66 | HASH_DIR1="QmNyZVFbgvmzguS2jVMRb8PQMNcCMJrn9E3doDhBbcPNTY" |
| 67 | HASH_NOPINDIR="QmWHjrRJYSfYKz5V9dWWSKu47GdY7NewyRhyTiroXgWcDU" |
| 68 | HASH_NOPIN_FILE1="QmUJT3GQi1dxQyTZbkaWeer9GkCn1d3W3HHRLSDr6PTcpx" |
| 69 | HASH_NOPIN_FILE2="QmarR7m9JT7qHEGhuFNZUEMAnoZ8E9QAfsthHCQ9Y2GfoT" |
| 70 | |
| 71 | DIR1="dir1" |
| 72 | DIR2="dir1/dir2" |
| 73 | DIR4="dir1/dir2/dir4" |
| 74 | DIR3="dir1/dir3" |
| 75 | FILE1="dir1/file1" |
| 76 | FILE2="dir1/file2" |
| 77 | FILE3="dir1/file3" |
| 78 | FILE4="dir1/dir2/file4" |
| 79 | FILE6="dir1/dir2/dir4/file6" |
| 80 | FILE5="dir1/dir3/file5" |
| 81 | |
| 82 | test_expect_success "'ipfs add dir' succeeds" ' |
| 83 | mkdir dir1 && |
| 84 | mkdir dir1/dir2 && |
| 85 | mkdir dir1/dir2/dir4 && |
| 86 | mkdir dir1/dir3 && |
| 87 | echo "some text 1" >dir1/file1 && |
| 88 | echo "some text 2" >dir1/file2 && |
| 89 | echo "some text 3" >dir1/file3 && |
| 90 | echo "some text 1" >dir1/dir2/file1 && |
| 91 | echo "some text 4" >dir1/dir2/file4 && |
| 92 | echo "some text 1" >dir1/dir2/dir4/file1 && |
| 93 | echo "some text 2" >dir1/dir2/dir4/file2 && |
| 94 | echo "some text 6" >dir1/dir2/dir4/file6 && |
| 95 | echo "some text 2" >dir1/dir3/file2 && |
| 96 | echo "some text 5" >dir1/dir3/file5 && |
| 97 | ipfs add -Q -r dir1 >actual && |
| 98 | echo "$HASH_DIR1" >expected && |
| 99 | ipfs repo gc && # remove the patch chaff |
| 100 | test_cmp expected actual |
| 101 | ' |
| 102 | |
| 103 | test_expect_success "objects are there" ' |
| 104 | ipfs cat "$HASH_FILE6" >FILE6_a && |
| 105 | ipfs cat "$HASH_FILE5" >FILE5_a && |
| 106 | ipfs cat "$HASH_FILE4" >FILE4_a && |
| 107 | ipfs cat "$HASH_FILE3" >FILE3_a && |
| 108 | ipfs cat "$HASH_FILE2" >FILE2_a && |
| 109 | ipfs cat "$HASH_FILE1" >FILE1_a && |
| 110 | ipfs ls "$HASH_DIR3" >DIR3_a && |
| 111 | ipfs ls "$HASH_DIR4" >DIR4_a && |
| 112 | ipfs ls "$HASH_DIR2" >DIR2_a && |
| 113 | ipfs ls "$HASH_DIR1" >DIR1_a |
| 114 | ' |
| 115 | |
| 116 | # saving this output for later |
| 117 | test_expect_success "ipfs dag get $HASH_DIR1 works" ' |
| 118 | ipfs dag get $HASH_DIR1 | jq -r ".Links[] | .Hash | .[\"/\"]" > DIR1_objlink |
| 119 | ' |
| 120 | |
| 121 | |
| 122 | test_expect_success "added dir was pinned recursively" ' |
| 123 | test_pin_flag $HASH_DIR1 recursive true |
| 124 | ' |
| 125 | |
| 126 | test_expect_success "rest were pinned indirectly" ' |
| 127 | test_pin_flag "$HASH_FILE6" indirect true |
| 128 | test_pin_flag "$HASH_FILE5" indirect true |
| 129 | test_pin_flag "$HASH_FILE4" indirect true |
| 130 | test_pin_flag "$HASH_FILE3" indirect true |
| 131 | test_pin_flag "$HASH_FILE2" indirect true |
| 132 | test_pin_flag "$HASH_FILE1" indirect true |
| 133 | test_pin_flag "$HASH_DIR3" indirect true |
| 134 | test_pin_flag "$HASH_DIR4" indirect true |
| 135 | test_pin_flag "$HASH_DIR2" indirect true |
| 136 | ' |
| 137 | |
| 138 | test_expect_success "added dir was NOT pinned indirectly" ' |
| 139 | test_pin_flag "$HASH_DIR1" indirect false |
| 140 | ' |
| 141 | |
| 142 | test_expect_success "nothing is pinned directly" ' |
| 143 | ipfs pin ls --type=direct >actual4 && |
| 144 | test_must_be_empty actual4 |
| 145 | ' |
| 146 | |
| 147 | test_expect_success "'ipfs repo gc' succeeds" ' |
| 148 | ipfs repo gc >gc_out_actual |
| 149 | ' |
| 150 | |
| 151 | test_expect_success "objects are still there" ' |
| 152 | cat FILE6_a FILE5_a FILE4_a FILE3_a FILE2_a FILE1_a >expected45 && |
| 153 | cat DIR3_a DIR4_a DIR2_a DIR1_a >>expected45 && |
| 154 | ipfs cat "$HASH_FILE6" >actual45 && |
| 155 | ipfs cat "$HASH_FILE5" >>actual45 && |
| 156 | ipfs cat "$HASH_FILE4" >>actual45 && |
| 157 | ipfs cat "$HASH_FILE3" >>actual45 && |
| 158 | ipfs cat "$HASH_FILE2" >>actual45 && |
| 159 | ipfs cat "$HASH_FILE1" >>actual45 && |
| 160 | ipfs ls "$HASH_DIR3" >>actual45 && |
| 161 | ipfs ls "$HASH_DIR4" >>actual45 && |
| 162 | ipfs ls "$HASH_DIR2" >>actual45 && |
| 163 | ipfs ls "$HASH_DIR1" >>actual45 && |
| 164 | test_cmp expected45 actual45 |
| 165 | ' |
| 166 | |
| 167 | test_expect_success "remove dir recursive pin succeeds" ' |
| 168 | echo "unpinned $HASH_DIR1" >expected5 && |
| 169 | ipfs pin rm -r "$HASH_DIR1" >actual5 && |
| 170 | test_cmp expected5 actual5 |
| 171 | ' |
| 172 | |
| 173 | test_expect_success "none are pinned any more" ' |
| 174 | test_pin "$HASH_FILE6" && |
| 175 | test_pin "$HASH_FILE5" && |
| 176 | test_pin "$HASH_FILE4" && |
| 177 | test_pin "$HASH_FILE3" && |
| 178 | test_pin "$HASH_FILE2" && |
| 179 | test_pin "$HASH_FILE1" && |
| 180 | test_pin "$HASH_DIR3" && |
| 181 | test_pin "$HASH_DIR4" && |
| 182 | test_pin "$HASH_DIR2" && |
| 183 | test_pin "$HASH_DIR1" |
| 184 | ' |
| 185 | |
| 186 | test_expect_success "pin some directly and indirectly" ' |
| 187 | ipfs pin add -r=false "$HASH_DIR1" >actual7 && |
| 188 | ipfs pin add -r=true "$HASH_DIR2" >>actual7 && |
| 189 | ipfs pin add -r=false "$HASH_FILE1" >>actual7 && |
| 190 | echo "pinned $HASH_DIR1 directly" >expected7 && |
| 191 | echo "pinned $HASH_DIR2 recursively" >>expected7 && |
| 192 | echo "pinned $HASH_FILE1 directly" >>expected7 && |
| 193 | test_cmp expected7 actual7 |
| 194 | ' |
| 195 | |
| 196 | test_expect_success "pin lists look good" ' |
| 197 | test_pin $HASH_DIR1 direct && |
| 198 | test_pin $HASH_DIR2 recursive && |
| 199 | test_pin $HASH_DIR3 && |
| 200 | test_pin $HASH_DIR4 indirect && |
| 201 | test_pin $HASH_FILE1 indirect direct && |
| 202 | test_pin $HASH_FILE2 indirect && |
| 203 | test_pin $HASH_FILE3 && |
| 204 | test_pin $HASH_FILE4 indirect && |
| 205 | test_pin $HASH_FILE5 && |
| 206 | test_pin $HASH_FILE6 indirect |
| 207 | ' |
| 208 | |
| 209 | test_expect_success "'ipfs repo gc' succeeds" ' |
| 210 | ipfs repo gc && |
| 211 | test_must_fail ipfs block stat $HASH_FILE3 && |
| 212 | test_must_fail ipfs block stat $HASH_FILE5 && |
| 213 | test_must_fail ipfs block stat $HASH_DIR3 |
| 214 | ' |
| 215 | |
| 216 | # use object links for HASH_DIR1 here because its children |
| 217 | # no longer exist |
| 218 | test_expect_success "some objects are still there" ' |
| 219 | cat FILE6_a FILE4_a FILE2_a FILE1_a >expected8 && |
| 220 | cat DIR4_a DIR2_a DIR1_objlink >>expected8 && |
| 221 | ipfs cat "$HASH_FILE6" >actual8 && |
| 222 | ipfs cat "$HASH_FILE4" >>actual8 && |
| 223 | ipfs cat "$HASH_FILE2" >>actual8 && |
| 224 | ipfs cat "$HASH_FILE1" >>actual8 && |
| 225 | ipfs ls "$HASH_DIR4" >>actual8 && |
| 226 | ipfs ls "$HASH_DIR2" >>actual8 && |
| 227 | ipfs dag get "$HASH_DIR1" | jq -r ".Links[] | .Hash | .[\"/\"]" >>actual8 && |
| 228 | test_cmp expected8 actual8 |
| 229 | ' |
| 230 | |
| 231 | # todo: make this faster somehow. |
| 232 | test_expect_success "some are no longer there" ' |
| 233 | test_must_fail ipfs cat "$HASH_FILE5" && |
| 234 | test_must_fail ipfs cat "$HASH_FILE3" && |
| 235 | test_must_fail ipfs ls "$HASH_DIR3" |
| 236 | ' |
| 237 | |
| 238 | test_launch_ipfs_daemon_without_network |
| 239 | test_expect_success "recursive pin fails without objects" ' |
| 240 | test_must_fail ipfs pin add -r "$HASH_DIR1" 2>err_expected8 && |
| 241 | grep "ipld: could not find" err_expected8 || |
| 242 | test_fsh cat err_expected8 |
| 243 | ' |
| 244 | |
| 245 | # Regression test for https://github.com/ipfs/go-ipfs/issues/4650 |
| 246 | # This test requires the daemon. Otherwise, the pin changes are reverted when |
| 247 | # the pin fails in the previous test. |
| 248 | test_expect_success "failed recursive pin does not remove direct pin" ' |
| 249 | test_pin_flag "$HASH_DIR1" direct true |
| 250 | ' |
| 251 | test_kill_ipfs_daemon |
| 252 | |
| 253 | test_expect_success "test add nopin file" ' |
| 254 | echo "test nopin data" > test_nopin_data && |
| 255 | NOPINHASH=$(ipfs add -q --pin=false test_nopin_data) && |
| 256 | test_pin_flag "$NOPINHASH" direct false && |
| 257 | test_pin_flag "$NOPINHASH" indirect false && |
| 258 | test_pin_flag "$NOPINHASH" recursive false |
| 259 | ' |
| 260 | |
| 261 | |
| 262 | test_expect_success "test add nopin dir" ' |
| 263 | mkdir nopin_dir1 && |
| 264 | echo "some nopin text 1" >nopin_dir1/file1 && |
| 265 | echo "some nopin text 2" >nopin_dir1/file2 && |
| 266 | ipfs add -Q -r --pin=false nopin_dir1 >actual && |
| 267 | echo "$HASH_NOPINDIR" >expected && |
| 268 | test_cmp actual expected && |
| 269 | test_pin_flag "$HASH_NOPINDIR" direct false && |
| 270 | test_pin_flag "$HASH_NOPINDIR" indirect false && |
| 271 | test_pin_flag "$HASH_NOPINDIR" recursive false && |
| 272 | test_pin_flag "$HASH_NOPIN_FILE1" direct false && |
| 273 | test_pin_flag "$HASH_NOPIN_FILE1" indirect false && |
| 274 | test_pin_flag "$HASH_NOPIN_FILE1" recursive false && |
| 275 | test_pin_flag "$HASH_NOPIN_FILE2" direct false && |
| 276 | test_pin_flag "$HASH_NOPIN_FILE2" indirect false && |
| 277 | test_pin_flag "$HASH_NOPIN_FILE2" recursive false |
| 278 | |
| 279 | ' |
| 280 | |
| 281 | FICTIONAL_HASH="QmXV4f9v8a56MxWKBhP3ETsz4EaafudU1cKfPaaJnenc48" |
| 282 | test_launch_ipfs_daemon |
| 283 | test_expect_success "test unpinning a hash that's not pinned" " |
| 284 | test_expect_code 1 ipfs pin rm $FICTIONAL_HASH --timeout=2s |
| 285 | test_expect_code 1 ipfs pin rm $FICTIONAL_HASH/a --timeout=2s |
| 286 | test_expect_code 1 ipfs pin rm $FICTIONAL_HASH/a/b --timeout=2s |
| 287 | " |
| 288 | test_kill_ipfs_daemon |
| 289 | |
| 290 | # test_kill_ipfs_daemon |
| 291 | |
| 292 | test_done |