t0050-block.sh 5.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#!/bin/sh
#
# Copyright (c) 2014 Christian Couder
# MIT Licensed; see the LICENSE file in this repository.
#

test_description="Test block command"

. lib/test-lib.sh

11
test_init_ipfs
12

13 14
HASH="QmRKqGMAM6EZngbpjSqrvYzq5Qd8b1bSWymjSUY9zQSNDk"

15 16 17 18
#
# "block put tests"
#

19
test_expect_success "'ipfs block put' succeeds" '
Łukasz Magiera's avatar
Łukasz Magiera committed
20 21
  echo "Hello Mars!" >expected_in &&
  ipfs block put <expected_in >actual_out
22 23 24
'

test_expect_success "'ipfs block put' output looks good" '
Łukasz Magiera's avatar
Łukasz Magiera committed
25 26
  echo "$HASH" >expected_out &&
  test_cmp expected_out actual_out
27 28
'

29 30 31 32
#
# "block get" tests
#

33
test_expect_success "'ipfs block get' succeeds" '
Łukasz Magiera's avatar
Łukasz Magiera committed
34
  ipfs block get $HASH >actual_in
35 36 37
'

test_expect_success "'ipfs block get' output looks good" '
Łukasz Magiera's avatar
Łukasz Magiera committed
38
  test_cmp expected_in actual_in
39 40
'

41 42 43 44
#
# "block stat" tests
#

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
45
test_expect_success "'ipfs block stat' succeeds" '
Łukasz Magiera's avatar
Łukasz Magiera committed
46
  ipfs block stat $HASH >actual_stat
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
47 48
'

Kevin Atkinson's avatar
Kevin Atkinson committed
49
test_expect_success "'ipfs block stat' output looks good" '
Łukasz Magiera's avatar
Łukasz Magiera committed
50 51 52
  echo "Key: $HASH" >expected_stat &&
  echo "Size: 12" >>expected_stat &&
  test_cmp expected_stat actual_stat
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
53 54
'

55 56 57 58
#
# "block rm" tests
#

Kevin Atkinson's avatar
Kevin Atkinson committed
59
test_expect_success "'ipfs block rm' succeeds" '
Łukasz Magiera's avatar
Łukasz Magiera committed
60
  ipfs block rm $HASH >actual_rm
Kevin Atkinson's avatar
Kevin Atkinson committed
61 62 63
'

test_expect_success "'ipfs block rm' output looks good" '
Łukasz Magiera's avatar
Łukasz Magiera committed
64 65
  echo "removed $HASH" > expected_rm &&
  test_cmp expected_rm actual_rm
Kevin Atkinson's avatar
Kevin Atkinson committed
66 67 68
'

test_expect_success "'ipfs block rm' block actually removed" '
Łukasz Magiera's avatar
Łukasz Magiera committed
69
  test_must_fail ipfs block stat $HASH
Kevin Atkinson's avatar
Kevin Atkinson committed
70 71 72 73 74 75 76 77
'

DIRHASH=QmdWmVmM6W2abTgkEfpbtA1CJyTWS2rhuUB9uP1xV8Uwtf
FILE1HASH=Qmae3RedM7SNkWGsdzYzsr6svmsFdsva4WoTvYYsWhUSVz
FILE2HASH=QmUtkGLvPf63NwVzLPKPUYgwhn8ZYPWF6vKWN3fZ2amfJF
FILE3HASH=Qmesmmf1EEG1orJb6XdK6DabxexsseJnCfw8pqWgonbkoj

test_expect_success "add and pin directory" '
Łukasz Magiera's avatar
Łukasz Magiera committed
78 79 80 81 82 83
  mkdir adir &&
  echo "file1" > adir/file1 &&
  echo "file2" > adir/file2 &&
  echo "file3" > adir/file3 &&
  ipfs add -r adir
  ipfs pin add -r $DIRHASH
Kevin Atkinson's avatar
Kevin Atkinson committed
84 85 86
'

test_expect_success "can't remove pinned block" '
Łukasz Magiera's avatar
Łukasz Magiera committed
87
  test_must_fail ipfs block rm $DIRHASH 2> block_rm_err
Kevin Atkinson's avatar
Kevin Atkinson committed
88 89 90
'

test_expect_success "can't remove pinned block: output looks good" '
Łukasz Magiera's avatar
Łukasz Magiera committed
91
  grep -q "$DIRHASH: pinned: recursive" block_rm_err
Kevin Atkinson's avatar
Kevin Atkinson committed
92 93 94
'

test_expect_success "can't remove indirectly pinned block" '
Łukasz Magiera's avatar
Łukasz Magiera committed
95
  test_must_fail ipfs block rm $FILE1HASH 2> block_rm_err
Kevin Atkinson's avatar
Kevin Atkinson committed
96 97 98
'

test_expect_success "can't remove indirectly pinned block: output looks good" '
Łukasz Magiera's avatar
Łukasz Magiera committed
99
  grep -q "$FILE1HASH: pinned via $DIRHASH" block_rm_err
Kevin Atkinson's avatar
Kevin Atkinson committed
100 101
'

102
test_expect_success "remove pin" '
Łukasz Magiera's avatar
Łukasz Magiera committed
103
  ipfs pin rm -r $DIRHASH
Kevin Atkinson's avatar
Kevin Atkinson committed
104 105 106
'

test_expect_success "multi-block 'ipfs block rm' succeeds" '
Łukasz Magiera's avatar
Łukasz Magiera committed
107
  ipfs block rm $FILE1HASH $FILE2HASH $FILE3HASH > actual_rm
Kevin Atkinson's avatar
Kevin Atkinson committed
108 109 110
'

test_expect_success "multi-block 'ipfs block rm' output looks good" '
Łukasz Magiera's avatar
Łukasz Magiera committed
111 112 113
  grep -F -q "removed $FILE1HASH" actual_rm &&
  grep -F -q "removed $FILE2HASH" actual_rm &&
  grep -F -q "removed $FILE3HASH" actual_rm
Kevin Atkinson's avatar
Kevin Atkinson committed
114 115
'

116
test_expect_success "'add some blocks' succeeds" '
Łukasz Magiera's avatar
Łukasz Magiera committed
117 118
  echo "Hello Mars!" | ipfs block put &&
  echo "Hello Venus!" | ipfs block put
119 120 121
'

test_expect_success "add and pin directory" '
Łukasz Magiera's avatar
Łukasz Magiera committed
122 123
  ipfs add -r adir
  ipfs pin add -r $DIRHASH
124 125 126 127
'

HASH=QmRKqGMAM6EZngbpjSqrvYzq5Qd8b1bSWymjSUY9zQSNDk
HASH2=QmdnpnsaEj69isdw5sNzp3h3HkaDz7xKq7BmvFFBzNr5e7
Jeromy's avatar
Jeromy committed
128
RANDOMHASH=QmRKqGMAM6EbngbZjSqrvYzq5Qd8b1bSWymjSUY9zQSNDq
129 130

test_expect_success "multi-block 'ipfs block rm' mixed" '
Łukasz Magiera's avatar
Łukasz Magiera committed
131
  test_must_fail ipfs block rm $FILE1HASH $DIRHASH $HASH $FILE3HASH $RANDOMHASH $HASH2 2> block_rm_err
132 133 134
'

test_expect_success "pinned block not removed" '
Łukasz Magiera's avatar
Łukasz Magiera committed
135 136
  ipfs block stat $FILE1HASH &&
  ipfs block stat $FILE3HASH
137 138 139
'

test_expect_success "non-pinned blocks removed" '
Łukasz Magiera's avatar
Łukasz Magiera committed
140 141
  test_must_fail ipfs block stat $HASH &&
  test_must_fail ipfs block stat $HASH2
142 143 144
'

test_expect_success "error reported on removing non-existent block" '
Łukasz Magiera's avatar
Łukasz Magiera committed
145
  grep -q "cannot remove $RANDOMHASH" block_rm_err
146 147
'

148
test_expect_success "'add some blocks' succeeds" '
Łukasz Magiera's avatar
Łukasz Magiera committed
149 150
  echo "Hello Mars!" | ipfs block put &&
  echo "Hello Venus!" | ipfs block put
151 152 153
'

test_expect_success "multi-block 'ipfs block rm -f' with non existent blocks succeed" '
Łukasz Magiera's avatar
Łukasz Magiera committed
154
  ipfs block rm -f $HASH $RANDOMHASH $HASH2
155 156 157
'

test_expect_success "existent blocks removed" '
Łukasz Magiera's avatar
Łukasz Magiera committed
158 159
  test_must_fail ipfs block stat $HASH &&
  test_must_fail ipfs block stat $HASH2
160 161 162
'

test_expect_success "'add some blocks' succeeds" '
Łukasz Magiera's avatar
Łukasz Magiera committed
163 164
  echo "Hello Mars!" | ipfs block put &&
  echo "Hello Venus!" | ipfs block put
165 166 167
'

test_expect_success "multi-block 'ipfs block rm -q' produces no output" '
Łukasz Magiera's avatar
Łukasz Magiera committed
168 169
  ipfs block rm -q $HASH $HASH2 > block_rm_out &&
  test ! -s block_rm_out
170 171
'

172
test_expect_success "can set cid format on block put" '
Łukasz Magiera's avatar
Łukasz Magiera committed
173
  HASH=$(ipfs block put --format=protobuf ../t0051-object-data/testPut.pb)
174 175 176
'

test_expect_success "created an object correctly!" '
Łukasz Magiera's avatar
Łukasz Magiera committed
177 178 179
  ipfs object get $HASH > obj_out &&
  echo "{\"Links\":[],\"Data\":\"test json for sharness test\"}" > obj_exp &&
  test_cmp obj_out obj_exp
180 181 182
'

test_expect_success "block get output looks right" '
Łukasz Magiera's avatar
Łukasz Magiera committed
183 184
  ipfs block get $HASH > pb_block_out &&
  test_cmp pb_block_out ../t0051-object-data/testPut.pb
185 186
'

187
test_expect_success "can set multihash type and length on block put" '
Łukasz Magiera's avatar
Łukasz Magiera committed
188
  HASH=$(echo "foooo" | ipfs block put --format=raw --mhtype=sha3 --mhlen=16)
189 190 191
'

test_expect_success "output looks good" '
Łukasz Magiera's avatar
Łukasz Magiera committed
192
  test "z25ScPysKoxJBcPxczn9NvuHiZU5" = "$HASH"
193 194 195
'

test_expect_success "can read block with different hash" '
Łukasz Magiera's avatar
Łukasz Magiera committed
196 197 198
  ipfs block get $HASH > blk_get_out &&
  echo "foooo" > blk_get_exp &&
  test_cmp blk_get_exp blk_get_out
199
'
200 201 202 203
#
# Misc tests
#

204
test_expect_success "'ipfs block stat' with nothing from stdin doesnt crash" '
Łukasz Magiera's avatar
Łukasz Magiera committed
205
  test_expect_code 1 ipfs block stat < /dev/null 2> stat_out
206 207 208
'

test_expect_success "no panic in output" '
Łukasz Magiera's avatar
Łukasz Magiera committed
209
  test_expect_code 1 grep "panic" stat_out
210 211
'

212 213 214 215 216 217 218 219 220 221 222 223
test_expect_success "can set multihash type and length on block put without format" '
  HASH=$(echo "foooo" | ipfs block put --mhtype=sha3 --mhlen=16)
'

test_expect_success "output looks good" '
  test "z2APJNN6rqZTWPpv7gYFHzh7ZEDX" = "$HASH"
'

test_expect_success "put with sha3 and cidv0 fails" '
  echo "foooo" | test_must_fail ipfs block put --mhtype=sha3 --mhlen=16 --format=v0
'

224
test_done