| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Copyright (c) 2015 Matt Bell |
| 4 | # MIT Licensed; see the LICENSE file in this repository. |
| 5 | # |
| 6 | |
| 7 | test_description="Test get command" |
| 8 | |
| 9 | . lib/test-lib.sh |
| 10 | |
| 11 | test_init_ipfs |
| 12 | |
| 13 | test_ipfs_get_flag() { |
| 14 | ext="$1"; shift |
| 15 | tar_flag="$1"; shift |
| 16 | flag="$@" |
| 17 | |
| 18 | test_expect_success "ipfs get $flag succeeds" ' |
| 19 | ipfs get "$HASH" '"$flag"' >actual |
| 20 | ' |
| 21 | |
| 22 | test_expect_success "ipfs get $flag output looks good" ' |
| 23 | printf "%s\n" "Saving archive to $HASH$ext" >expected && |
| 24 | test_cmp expected actual |
| 25 | ' |
| 26 | |
| 27 | test_expect_success "ipfs get $flag archive output is valid" ' |
| 28 | tar "$tar_flag" "$HASH$ext" && |
| 29 | test_cmp "$HASH" data && |
| 30 | rm "$HASH$ext" && |
| 31 | rm "$HASH" |
| 32 | ' |
| 33 | } |
| 34 | |
| 35 | # we use a function so that we can run it both offline + online |
| 36 | test_get_cmd() { |
| 37 | |
| 38 | test_expect_success "'ipfs get --help' succeeds" ' |
| 39 | ipfs get --help >actual |
| 40 | ' |
| 41 | |
| 42 | test_expect_success "'ipfs get --help' output looks good" ' |
| 43 | egrep "ipfs get.*<ipfs-path>" actual >/dev/null || |
| 44 | test_fsh cat actual |
| 45 | ' |
| 46 | |
| 47 | test_expect_success "ipfs get succeeds" ' |
| 48 | echo "Hello Worlds!" >data && |
| 49 | HASH=`ipfs add -q data` && |
| 50 | ipfs get "$HASH" >actual |
| 51 | ' |
| 52 | |
| 53 | test_expect_success "ipfs get output looks good" ' |
| 54 | printf "%s\n" "Saving file(s) to $HASH" >expected && |
| 55 | test_cmp expected actual |
| 56 | ' |
| 57 | |
| 58 | test_expect_success "ipfs get file output looks good" ' |
| 59 | test_cmp "$HASH" data |
| 60 | ' |
| 61 | |
| 62 | test_expect_success "ipfs get DOES NOT error when trying to overwrite a file" ' |
| 63 | ipfs get "$HASH" >actual && |
| 64 | rm "$HASH" |
| 65 | ' |
| 66 | |
| 67 | test_expect_success "ipfs get works with raw leaves" ' |
| 68 | HASH2=$(ipfs add --raw-leaves -q data) && |
| 69 | ipfs get "$HASH2" >actual2 |
| 70 | ' |
| 71 | |
| 72 | test_expect_success "ipfs get output looks good" ' |
| 73 | printf "%s\n" "Saving file(s) to $HASH2" >expected2 && |
| 74 | test_cmp expected2 actual2 |
| 75 | ' |
| 76 | |
| 77 | test_expect_success "ipfs get file output looks good" ' |
| 78 | test_cmp "$HASH2" data |
| 79 | ' |
| 80 | |
| 81 | test_ipfs_get_flag ".tar" "-xf" -a |
| 82 | |
| 83 | test_ipfs_get_flag ".tar.gz" "-zxf" -a -C |
| 84 | |
| 85 | test_ipfs_get_flag ".tar.gz" "-zxf" -a -C -l 9 |
| 86 | |
| 87 | test_expect_success "ipfs get succeeds (directory)" ' |
| 88 | mkdir -p dir && |
| 89 | touch dir/a && |
| 90 | mkdir -p dir/b && |
| 91 | echo "Hello, Worlds!" >dir/b/c && |
| 92 | HASH2=`ipfs add -r -Q dir` && |
| 93 | ipfs get "$HASH2" >actual |
| 94 | ' |
| 95 | |
| 96 | test_expect_success "ipfs get output looks good (directory)" ' |
| 97 | printf "%s\n" "Saving file(s) to $HASH2" >expected && |
| 98 | test_cmp expected actual |
| 99 | ' |
| 100 | |
| 101 | test_expect_success "ipfs get output is valid (directory)" ' |
| 102 | test_cmp dir/a "$HASH2"/a && |
| 103 | test_cmp dir/b/c "$HASH2"/b/c && |
| 104 | rm -r "$HASH2" |
| 105 | ' |
| 106 | |
| 107 | # Test issue #4720: problems when path contains a trailing slash. |
| 108 | test_expect_success "ipfs get with slash (directory)" ' |
| 109 | ipfs get "$HASH2/" && |
| 110 | test_cmp dir/a "$HASH2"/a && |
| 111 | test_cmp dir/b/c "$HASH2"/b/c && |
| 112 | rm -r "$HASH2" |
| 113 | ' |
| 114 | |
| 115 | test_expect_success "ipfs get -a -C succeeds (directory)" ' |
| 116 | ipfs get "$HASH2" -a -C >actual |
| 117 | ' |
| 118 | |
| 119 | test_expect_success "ipfs get -a -C output looks good (directory)" ' |
| 120 | printf "%s\n" "Saving archive to $HASH2.tar.gz" >expected && |
| 121 | test_cmp expected actual |
| 122 | ' |
| 123 | |
| 124 | test_expect_success "gzipped tar archive output is valid (directory)" ' |
| 125 | tar -zxf "$HASH2".tar.gz && |
| 126 | test_cmp dir/a "$HASH2"/a && |
| 127 | test_cmp dir/b/c "$HASH2"/b/c && |
| 128 | rm -r "$HASH2" |
| 129 | ' |
| 130 | |
| 131 | test_expect_success "ipfs get ../.. should fail" ' |
| 132 | test_must_fail ipfs get ../.. 2>actual && |
| 133 | test_should_contain "Error: invalid path \"../..\"" actual |
| 134 | ' |
| 135 | |
| 136 | test_expect_success "create small file" ' |
| 137 | echo "foo" > small && |
| 138 | ipfs add -q small > hash_small |
| 139 | ' |
| 140 | |
| 141 | test_expect_success "get small file" ' |
| 142 | ipfs get -o out_small $(cat hash_small) && |
| 143 | test_cmp small out_small |
| 144 | ' |
| 145 | |
| 146 | test_expect_success "create medium file" ' |
| 147 | head -c 16000 > medium && |
| 148 | ipfs add -q medium > hash_medium |
| 149 | ' |
| 150 | |
| 151 | test_expect_success "get medium file" ' |
| 152 | ipfs get -o out_medium $(cat hash_medium) && |
| 153 | test_cmp medium out_medium |
| 154 | ' |
| 155 | } |
| 156 | |
| 157 | test_get_fail() { |
| 158 | test_expect_success "create an object that has unresolvable links" ' |
| 159 | cat <<-\EOF >bad_object && |
| 160 | {"Data":{"/":{"bytes":"CAE"}},"Links":[{"Hash":{"/":"Qmd4mG6pDFDmDTn6p3hX1srP8qTbkyXKj5yjpEsiHDX3u8"},"Name":"bar","Tsize":56},{"Hash":{"/":"QmUTjwRnG28dSrFFVTYgbr6LiDLsBmRr2SaUSTGheK2YqG"},"Name":"baz","Tsize":24266},{"Hash":{"/":"QmZzaC6ydNXiR65W8VjGA73ET9MZ6VFAqUT1ngYMXcpihn"},"Name":"foo","Tsize":1897}]} |
| 161 | EOF |
| 162 | cat bad_object | ipfs dag put --store-codec dag-pb > put_out |
| 163 | ' |
| 164 | |
| 165 | test_expect_success "output looks good" ' |
| 166 | echo "bafybeifrjjol3gixedca6etdwccnvwfvhurc4wb3i5mnk2rvwvyfcgwxd4" > put_exp && |
| 167 | test_cmp put_exp put_out |
| 168 | ' |
| 169 | |
| 170 | test_expect_success "ipfs get fails" ' |
| 171 | test_expect_code 1 ipfs get QmaGidyrnX8FMbWJoxp8HVwZ1uRKwCyxBJzABnR1S2FVUr |
| 172 | ' |
| 173 | } |
| 174 | |
| 175 | # should work offline |
| 176 | test_get_cmd |
| 177 | |
| 178 | # only really works offline, will try and search network when online |
| 179 | test_get_fail |
| 180 | |
| 181 | # should work online |
| 182 | test_launch_ipfs_daemon |
| 183 | test_get_cmd |
| 184 | |
| 185 | test_expect_success "empty request to get doesn't panic and returns error" ' |
| 186 | curl -X POST "http://$API_ADDR/api/v0/get" > curl_out || true && |
| 187 | grep "argument \"ipfs-path\" is required" curl_out |
| 188 | ' |
| 189 | test_kill_ipfs_daemon |
| 190 | |
| 191 | test_done |