t0090-get.sh 5 KB
Newer Older
Matt Bell's avatar
Matt Bell committed
1 2 3 4 5 6 7 8 9 10 11 12
#!/bin/sh
#
# Copyright (c) 2015 Matt Bell
# MIT Licensed; see the LICENSE file in this repository.
#

test_description="Test get command"

. lib/test-lib.sh

test_init_ipfs

rht's avatar
rht committed
13
test_ipfs_get_flag() {
Łukasz Magiera's avatar
Łukasz Magiera committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
  ext="$1"; shift
  tar_flag="$1"; shift
  flag="$@"

  test_expect_success "ipfs get $flag succeeds" '
    ipfs get "$HASH" '"$flag"' >actual
  '

  test_expect_success "ipfs get $flag output looks good" '
    printf "%s\n\n" "Saving archive to $HASH$ext" >expected &&
    test_cmp expected actual
  '

  test_expect_success "ipfs get $flag archive output is valid" '
    tar "$tar_flag" "$HASH$ext" &&
    test_cmp "$HASH" data &&
    rm "$HASH$ext" &&
    rm "$HASH"
  '
rht's avatar
rht committed
33 34
}

35 36 37
# we use a function so that we can run it both offline + online
test_get_cmd() {

Łukasz Magiera's avatar
Łukasz Magiera committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  test_expect_success "'ipfs get --help' succeeds" '
    ipfs get --help >actual
  '

  test_expect_success "'ipfs get --help' output looks good" '
    egrep "ipfs get.*<ipfs-path>" actual >/dev/null ||
    test_fsh cat actual
  '

  test_expect_success "ipfs get succeeds" '
    echo "Hello Worlds!" >data &&
    HASH=`ipfs add -q data` &&
    ipfs get "$HASH" >actual
  '

  test_expect_success "ipfs get output looks good" '
    printf "%s\n\n" "Saving file(s) to $HASH" >expected &&
    test_cmp expected actual
  '

  test_expect_success "ipfs get file output looks good" '
    test_cmp "$HASH" data
  '

  test_expect_success "ipfs get DOES NOT error when trying to overwrite a file" '
    ipfs get "$HASH" >actual &&
    rm "$HASH"
  '

  test_expect_success "ipfs get works with raw leaves" '
  HASH2=$(ipfs add --raw-leaves -q data) &&
    ipfs get "$HASH2" >actual2
  '

  test_expect_success "ipfs get output looks good" '
    printf "%s\n\n" "Saving file(s) to $HASH2" >expected2 &&
    test_cmp expected2 actual2
  '

  test_expect_success "ipfs get file output looks good" '
    test_cmp "$HASH2" data
  '

  test_ipfs_get_flag ".tar" "-xf" -a

  test_ipfs_get_flag ".tar.gz" "-zxf" -a -C

  test_ipfs_get_flag ".tar.gz" "-zxf" -a -C -l 9

  test_expect_success "ipfs get succeeds (directory)" '
    mkdir -p dir &&
    touch dir/a &&
    mkdir -p dir/b &&
    echo "Hello, Worlds!" >dir/b/c &&
    HASH2=`ipfs add -r -q dir | tail -n 1` &&
    ipfs get "$HASH2" >actual
  '

  test_expect_success "ipfs get output looks good (directory)" '
    printf "%s\n\n" "Saving file(s) to $HASH2" >expected &&
    test_cmp expected actual
  '

  test_expect_success "ipfs get output is valid (directory)" '
    test_cmp dir/a "$HASH2"/a &&
    test_cmp dir/b/c "$HASH2"/b/c &&
    rm -r "$HASH2"
  '

Lucas Molas's avatar
Lucas Molas committed
107 108 109 110 111 112 113 114
  # Test issue #4720: problems when path contains a trailing slash.
  test_expect_success "ipfs get with slash (directory)" '
    ipfs get "$HASH2/" &&
    test_cmp dir/a "$HASH2"/a &&
    test_cmp dir/b/c "$HASH2"/b/c &&
    rm -r "$HASH2"
  '

Łukasz Magiera's avatar
Łukasz Magiera committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
  test_expect_success "ipfs get -a -C succeeds (directory)" '
    ipfs get "$HASH2" -a -C >actual
  '

  test_expect_success "ipfs get -a -C output looks good (directory)" '
    printf "%s\n\n" "Saving archive to $HASH2.tar.gz" >expected &&
    test_cmp expected actual
  '

  test_expect_success "gzipped tar archive output is valid (directory)" '
    tar -zxf "$HASH2".tar.gz &&
    test_cmp dir/a "$HASH2"/a &&
    test_cmp dir/b/c "$HASH2"/b/c &&
    rm -r "$HASH2"
  '

  test_expect_success "ipfs get ../.. should fail" '
    echo "Error: invalid 'ipfs ref' path" >expected &&
    test_must_fail ipfs get ../.. 2>actual &&
    test_cmp expected actual
  '
Jakub Sztandera's avatar
Jakub Sztandera committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

  test_expect_success "create small file" '
    echo "foo" > small &&
    ipfs add -q small > hash_small
  '

  test_expect_success "get small file" '
    ipfs get -o out_small $(cat hash_small) &&
    test_cmp small out_small
  '

  test_expect_success "create medium file" '
    head -c 16000 > medium &&
    ipfs add -q medium > hash_medium
  '

  test_expect_success "get medium file" '
    ipfs get -o out_medium $(cat hash_medium) &&
    test_cmp medium out_medium
  '
156 157
}

158
test_get_fail() {
Łukasz Magiera's avatar
Łukasz Magiera committed
159 160
  test_expect_success "create an object that has unresolveable links" '
    cat <<-\EOF >bad_object &&
161
{ "Links": [ { "Name": "foo", "Hash": "QmZzaC6ydNXiR65W8VjGA73ET9MZ6VFAqUT1ngYMXcpihn", "Size": 1897 }, { "Name": "bar", "Hash": "Qmd4mG6pDFDmDTn6p3hX1srP8qTbkyXKj5yjpEsiHDX3u8", "Size": 56 }, { "Name": "baz", "Hash": "QmUTjwRnG28dSrFFVTYgbr6LiDLsBmRr2SaUSTGheK2YqG", "Size": 24266 } ], "Data": "\b\u0001" }
Łukasz Magiera's avatar
Łukasz Magiera committed
162 163 164 165 166 167 168 169 170 171 172 173
EOF
    cat bad_object | ipfs object put > put_out
  '

  test_expect_success "output looks good" '
    echo "added QmaGidyrnX8FMbWJoxp8HVwZ1uRKwCyxBJzABnR1S2FVUr" > put_exp &&
    test_cmp put_exp put_out
  '

  test_expect_success "ipfs get fails" '
    test_expect_code 1 ipfs get QmaGidyrnX8FMbWJoxp8HVwZ1uRKwCyxBJzABnR1S2FVUr
  '
174 175
}

176 177 178
# should work offline
test_get_cmd

179 180 181
# only really works offline, will try and search network when online
test_get_fail

182 183 184
# should work online
test_launch_ipfs_daemon
test_get_cmd
185 186

test_expect_success "empty request to get doesn't panic and returns error" '
Łukasz Magiera's avatar
Łukasz Magiera committed
187
  curl "http://$API_ADDR/api/v0/get" > curl_out || true &&
188
    grep "argument \"ipfs-path\" is required" curl_out
189
'
Matt Bell's avatar
Matt Bell committed
190 191 192
test_kill_ipfs_daemon

test_done