master
sh 984 lines 32 KB
Raw
1 #!/usr/bin/env bash
2 #
3 # Copyright (c) 2014 Christian Couder
4 # MIT Licensed; see the LICENSE file in this repository.
5 #
6
7 test_description="Test add and cat commands"
8
9 . lib/test-lib.sh
10
11 test_add_cat_file() {
12 test_expect_success "ipfs add --help works" '
13 ipfs add --help 2> add_help_err1 > /dev/null
14 '
15
16 test_expect_success "stdin reading message doesn't show up" '
17 test_expect_code 1 grep "ipfs: Reading from" add_help_err1 &&
18 test_expect_code 1 grep "send Ctrl-d to stop." add_help_err1
19 '
20
21 test_expect_success "ipfs help add works" '
22 ipfs help add 2> add_help_err2 > /dev/null
23 '
24
25 test_expect_success "stdin reading message doesn't show up" '
26 test_expect_code 1 grep "ipfs: Reading from" add_help_err2 &&
27 test_expect_code 1 grep "send Ctrl-d to stop." add_help_err2
28 '
29
30 test_expect_success "ipfs add succeeds" '
31 echo "Hello Worlds!" >mountdir/hello.txt &&
32 ipfs add mountdir/hello.txt >actual
33 '
34
35 test_expect_success "ipfs add output looks good" '
36 HASH="QmVr26fY1tKyspEJBniVhqxQeEjhF78XerGiqWAwraVLQH" &&
37 echo "added $HASH hello.txt" >expected &&
38 test_cmp expected actual
39 '
40
41 test_expect_success "ipfs add --only-hash succeeds" '
42 ipfs add --only-hash mountdir/hello.txt > oh_actual
43 '
44
45 test_expect_success "ipfs add --only-hash output looks good" '
46 test_cmp expected oh_actual
47 '
48
49 test_expect_success "ipfs cat succeeds" '
50 ipfs cat "$HASH" >actual
51 '
52
53 test_expect_success "ipfs cat output looks good" '
54 echo "Hello Worlds!" >expected &&
55 test_cmp expected actual
56 '
57
58 test_expect_success "ipfs cat with offset succeeds" '
59 ipfs cat --offset 10 "$HASH" >actual
60 '
61
62 test_expect_success "ipfs cat from offset output looks good" '
63 echo "ds!" >expected &&
64 test_cmp expected actual
65 '
66
67 test_expect_success "ipfs cat multiple hashes with offset succeeds" '
68 ipfs cat --offset 10 "$HASH" "$HASH" >actual
69 '
70
71 test_expect_success "ipfs cat from offset output looks good" '
72 echo "ds!" >expected &&
73 echo "Hello Worlds!" >>expected &&
74 test_cmp expected actual
75 '
76
77 test_expect_success "ipfs cat multiple hashes with offset succeeds" '
78 ipfs cat --offset 16 "$HASH" "$HASH" >actual
79 '
80
81 test_expect_success "ipfs cat from offset output looks good" '
82 echo "llo Worlds!" >expected &&
83 test_cmp expected actual
84 '
85
86 test_expect_success "ipfs cat from negative offset should fail" '
87 test_expect_code 1 ipfs cat --offset -102 "$HASH" > actual
88 '
89
90 test_expect_success "ipfs cat with length succeeds" '
91 ipfs cat --length 8 "$HASH" >actual
92 '
93
94 test_expect_success "ipfs cat with length output looks good" '
95 printf "Hello Wo" >expected &&
96 test_cmp expected actual
97 '
98
99 test_expect_success "ipfs cat multiple hashes with offset and length succeeds" '
100 ipfs cat --offset 5 --length 15 "$HASH" "$HASH" "$HASH" >actual
101 '
102
103 test_expect_success "ipfs cat multiple hashes with offset and length looks good" '
104 printf " Worlds!\nHello " >expected &&
105 test_cmp expected actual
106 '
107
108 test_expect_success "ipfs cat with exact length succeeds" '
109 ipfs cat --length $(ipfs cat "$HASH" | wc -c) "$HASH" >actual
110 '
111
112 test_expect_success "ipfs cat with exact length looks good" '
113 echo "Hello Worlds!" >expected &&
114 test_cmp expected actual
115 '
116
117 test_expect_success "ipfs cat with 0 length succeeds" '
118 ipfs cat --length 0 "$HASH" >actual
119 '
120
121 test_expect_success "ipfs cat with 0 length looks good" '
122 : >expected &&
123 test_cmp expected actual
124 '
125
126 test_expect_success "ipfs cat with oversized length succeeds" '
127 ipfs cat --length 100 "$HASH" >actual
128 '
129
130 test_expect_success "ipfs cat with oversized length looks good" '
131 echo "Hello Worlds!" >expected &&
132 test_cmp expected actual
133 '
134
135 test_expect_success "ipfs cat with negative length should fail" '
136 test_expect_code 1 ipfs cat --length -102 "$HASH" > actual
137 '
138
139 test_expect_success "ipfs cat /ipfs/file succeeds" '
140 ipfs cat /ipfs/$HASH >actual
141 '
142
143 test_expect_success "output looks good" '
144 echo "Hello Worlds!" >expected &&
145 test_cmp expected actual
146 '
147
148 test_expect_success "ipfs add -t succeeds" '
149 ipfs add -t mountdir/hello.txt >actual
150 '
151
152 test_expect_success "ipfs add -t output looks good" '
153 HASH="QmUkUQgxXeggyaD5Ckv8ZqfW8wHBX6cYyeiyqvVZYzq5Bi" &&
154 echo "added $HASH hello.txt" >expected &&
155 test_cmp expected actual
156 '
157
158 test_expect_success "ipfs add --chunker size-32 succeeds" '
159 ipfs add --chunker rabin mountdir/hello.txt >actual
160 '
161
162 test_expect_success "ipfs add --chunker size-32 output looks good" '
163 HASH="QmVr26fY1tKyspEJBniVhqxQeEjhF78XerGiqWAwraVLQH" &&
164 echo "added $HASH hello.txt" >expected &&
165 test_cmp expected actual
166 '
167
168 test_expect_success "ipfs add --chunker size-64 succeeds" '
169 ipfs add --chunker=size-64 mountdir/hello.txt >actual
170 '
171
172 test_expect_success "ipfs add --chunker size-64 output looks good" '
173 HASH="QmVr26fY1tKyspEJBniVhqxQeEjhF78XerGiqWAwraVLQH" &&
174 echo "added $HASH hello.txt" >expected &&
175 test_cmp expected actual
176 '
177
178 test_expect_success "ipfs add --chunker=size-0 failed" '
179 test_expect_code 1 ipfs add -Q --chunker=size-0 mountdir/hello.txt
180 '
181
182 test_expect_success "ipfs add --chunker rabin-36-512-1024 succeeds" '
183 ipfs add --chunker rabin-36-512-1024 mountdir/hello.txt >actual
184 '
185
186 test_expect_success "ipfs add --chunker rabin-36-512-1024 output looks good" '
187 HASH="QmVr26fY1tKyspEJBniVhqxQeEjhF78XerGiqWAwraVLQH" &&
188 echo "added $HASH hello.txt" >expected &&
189 test_cmp expected actual
190 '
191
192 test_expect_success "ipfs add --chunker rabin-12-512-1024 failed" '
193 test_expect_code 1 ipfs add -Q --chunker rabin-12-512-1024 mountdir/hello.txt
194 '
195
196 test_expect_success "ipfs add --chunker buzhash succeeds" '
197 ipfs add --chunker buzhash mountdir/hello.txt >actual
198 '
199
200 test_expect_success "ipfs add --chunker buzhash output looks good" '
201 HASH="QmVr26fY1tKyspEJBniVhqxQeEjhF78XerGiqWAwraVLQH" &&
202 echo "added $HASH hello.txt" >expected &&
203 test_cmp expected actual
204 '
205
206 test_expect_success "ipfs add on hidden file succeeds" '
207 echo "Hello Worlds!" >mountdir/.hello.txt &&
208 ipfs add mountdir/.hello.txt >actual
209 '
210
211 test_expect_success "ipfs add on hidden file output looks good" '
212 HASH="QmVr26fY1tKyspEJBniVhqxQeEjhF78XerGiqWAwraVLQH" &&
213 echo "added $HASH .hello.txt" >expected &&
214 test_cmp expected actual
215 '
216
217 test_expect_success "add zero length file" '
218 touch zero-length-file &&
219 ZEROHASH=$(ipfs add -q zero-length-file) &&
220 echo $ZEROHASH
221 '
222
223 test_expect_success "zero length file has correct hash" '
224 test "$ZEROHASH" = QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH
225 '
226
227 test_expect_success "cat zero length file" '
228 ipfs cat $ZEROHASH > zero-length-file_out
229 '
230
231 test_expect_success "make sure it looks good" '
232 test_cmp zero-length-file zero-length-file_out
233 '
234
235 test_expect_success "ipfs add --stdin-name" '
236 NAMEHASH="QmdFyxZXsFiP4csgfM5uPu99AvFiKH62CSPDw5TP92nr7w" &&
237 echo "IPFS" | ipfs add --stdin-name file.txt > actual &&
238 echo "added $NAMEHASH file.txt" > expected &&
239 test_cmp expected actual
240 '
241
242 test_expect_success "ipfs add --stdin-name -w" '
243 NAMEHASH="QmdFyxZXsFiP4csgfM5uPu99AvFiKH62CSPDw5TP92nr7w" &&
244 echo "IPFS" | ipfs add -w --stdin-name file.txt | head -n1> actual &&
245 echo "added $NAMEHASH file.txt" > expected &&
246 test_cmp expected actual
247 '
248
249 test_expect_success "ipfs cat with stdin-name" '
250 NAMEHASH=$(echo "IPFS" | ipfs add -w --stdin-name file.txt -Q) &&
251 ipfs cat /ipfs/$NAMEHASH/file.txt > expected &&
252 echo "IPFS" > actual &&
253 test_cmp expected actual
254 '
255
256 test_expect_success "ipfs add -r ." '
257 mkdir test_current_dir &&
258 echo "Hey" > test_current_dir/hey &&
259 mkdir test_current_dir/hello &&
260 echo "World" > test_current_dir/hello/world &&
261 ( cd test_current_dir &&
262 ipfs add -r -Q . > ../actual && cd ../ ) &&
263 rm -r test_current_dir
264 '
265
266 test_expect_success "ipfs add -r . output looks good" '
267 echo "QmZQWnfcqJ6hNkkPvrY9Q5X39GP3jUnUbAV4AbmbbR3Cb1" > expected
268 test_cmp expected actual
269 '
270
271 test_expect_success "ipfs add -r ./" '
272 mkdir test_current_dir &&
273 echo "Hey" > test_current_dir/hey &&
274 mkdir test_current_dir/hello &&
275 echo "World" > test_current_dir/hello/world &&
276 ( cd test_current_dir &&
277 ipfs add -r -Q ./ > ../actual && cd ../ ) &&
278 rm -r test_current_dir
279 '
280
281 test_expect_success "ipfs add -r ./ output looks good" '
282 echo "QmZQWnfcqJ6hNkkPvrY9Q5X39GP3jUnUbAV4AbmbbR3Cb1" > expected
283 test_cmp expected actual
284 '
285
286 # --cid-base=base32
287
288 test_expect_success "ipfs add --cid-base=base32 succeeds" '
289 echo "base32 test" >mountdir/base32-test.txt &&
290 ipfs add --cid-base=base32 mountdir/base32-test.txt >actual
291 '
292 test_expect_success "ipfs add --cid-base=base32 output looks good" '
293 HASHb32="bafybeibyosqxljd2eptb4ebbtvk7pb4aoxzqa6ttdsflty6rsslz5y6i34" &&
294 echo "added $HASHb32 base32-test.txt" >expected &&
295 test_cmp expected actual
296 '
297
298 test_expect_success "ipfs add --cid-base=base32 --only-hash succeeds" '
299 ipfs add --cid-base=base32 --only-hash mountdir/base32-test.txt > oh_actual
300 '
301 test_expect_success "ipfs add --cid-base=base32 --only-hash output looks good" '
302 test_cmp expected oh_actual
303 '
304
305 test_expect_success "ipfs add --cid-base=base32 --upgrade-cidv0-in-output=false succeeds" '
306 echo "base32 test" >mountdir/base32-test.txt &&
307 ipfs add --cid-base=base32 --upgrade-cidv0-in-output=false mountdir/base32-test.txt >actual
308 '
309 test_expect_success "ipfs add --cid-base=base32 --upgrade-cidv0-in-output=false output looks good" '
310 HASHv0=$(cid-fmt -v 0 -b z %s "$HASHb32") &&
311 echo "added $HASHv0 base32-test.txt" >expected &&
312 test_cmp expected actual
313 '
314
315 test_expect_success "ipfs add --cid-base=base32 --upgrade-cidv0-in-output=false --only-hash succeeds" '
316 ipfs add --cid-base=base32 --upgrade-cidv0-in-output=false --only-hash mountdir/base32-test.txt > oh_actual
317 '
318 test_expect_success "ipfs add --cid-base=base32 --upgrade-cidv0-in-output=false --only-hash output looks good" '
319 test_cmp expected oh_actual
320 '
321
322 test_expect_success "ipfs cat with base32 hash succeeds" '
323 ipfs cat "$HASHb32" >actual
324 '
325 test_expect_success "ipfs cat with base32 hash output looks good" '
326 echo "base32 test" >expected &&
327 test_cmp expected actual
328 '
329
330 test_expect_success "ipfs cat using CIDv0 hash succeeds" '
331 ipfs cat "$HASHv0" >actual
332 '
333 test_expect_success "ipfs cat using CIDv0 hash looks good" '
334 echo "base32 test" >expected &&
335 test_cmp expected actual
336 '
337
338 test_expect_success "ipfs add with multiple files succeeds" '
339 echo "Helloo Worlds!" >mountdir/hello2.txt &&
340 ipfs add mountdir/hello.txt mountdir/hello2.txt >actual
341 '
342
343 test_expect_success "ipfs add with multiple files output looks good" '
344 echo "added QmVr26fY1tKyspEJBniVhqxQeEjhF78XerGiqWAwraVLQH hello.txt" >expected &&
345 echo "added Qmf35k66MZNW2GijohUmXQEWKZU4cCGTCwK6idfnt152wJ hello2.txt" >> expected &&
346 test_cmp expected actual
347 '
348
349 test_expect_success "ipfs add with multiple files of same name and import dir succeeds" '
350 ipfs add mountdir/hello.txt mountdir/hello.txt >actual
351 '
352
353 test_expect_success "ipfs add with multiple files of same name output looks good" '
354 echo "added QmVr26fY1tKyspEJBniVhqxQeEjhF78XerGiqWAwraVLQH hello.txt" >expected &&
355 test_cmp expected actual
356 '
357
358 test_expect_success "ipfs add with multiple files of same name but different dirs fails" '
359 mkdir -p mountdir/same-file/ &&
360 cp mountdir/hello.txt mountdir/same-file/hello.txt &&
361 test_expect_code 1 ipfs add mountdir/hello.txt mountdir/same-file/hello.txt >actual &&
362 rm mountdir/same-file/hello.txt &&
363 rmdir mountdir/same-file
364 '
365
366 ## --to-files with single source
367
368 test_expect_success "ipfs add --to-files /mfspath succeeds" '
369 mkdir -p mountdir && echo "Hello MFS!" > mountdir/mfs.txt &&
370 ipfs add mountdir/mfs.txt --to-files /ipfs-add-to-files >actual
371 '
372
373 test_expect_success "ipfs add --to-files output looks good" '
374 HASH_MFS="QmVT8bL3sGBA2TwvX8JPhrv5CYZL8LLLfW7mxkUjPZsgBr" &&
375 echo "added $HASH_MFS mfs.txt" >expected &&
376 test_cmp expected actual
377 '
378
379 test_expect_success "ipfs files read succeeds" '
380 ipfs files read /ipfs-add-to-files >actual &&
381 ipfs files rm /ipfs-add-to-files
382 '
383
384 test_expect_success "ipfs cat output looks good" '
385 echo "Hello MFS!" >expected &&
386 test_cmp expected actual
387 '
388
389 test_expect_success "ipfs add --to-files requires argument" '
390 test_expect_code 1 ipfs add mountdir/mfs.txt --to-files >actual 2>&1 &&
391 test_should_contain "Error: missing argument for option \"to-files\"" actual
392 '
393
394 test_expect_success "ipfs add --to-files / (MFS root) works" '
395 echo "Hello MFS!" >expected &&
396 ipfs add mountdir/mfs.txt --to-files / &&
397 ipfs files read /mfs.txt >actual &&
398 test_cmp expected actual &&
399 ipfs files rm /mfs.txt &&
400 rm mountdir/mfs.txt
401 '
402
403 ## --to-files with multiple sources
404
405 test_expect_success "ipfs add file1 file2 --to-files /mfspath0 (without trailing slash) fails" '
406 mkdir -p test &&
407 echo "file1" > test/mfs1.txt &&
408 echo "file2" > test/mfs2.txt &&
409 test_expect_code 1 ipfs add test/mfs1.txt test/mfs2.txt --to-files /mfspath0 >actual 2>&1 &&
410 test_should_contain "MFS destination is a file: only one entry can be copied to \"/mfspath0\"" actual &&
411 ipfs files rm -r --force /mfspath0
412 '
413
414 test_expect_success "ipfs add file1 file2 --to-files /mfsfile1 (without trailing slash + with preexisting file) fails" '
415 echo test | ipfs files write --create /mfsfile1 &&
416 test_expect_code 1 ipfs add test/mfs1.txt test/mfs2.txt --to-files /mfsfile1 >actual 2>&1 &&
417 test_should_contain "Error: to-files: cannot put node in path \"/mfsfile1\"" actual &&
418 ipfs files rm -r --force /mfsfile1
419 '
420
421 test_expect_success "ipfs add file1 file2 --to-files /mfsdir1 (without trailing slash + with preexisting dir) fails" '
422 ipfs files mkdir -p /mfsdir1 &&
423 test_expect_code 1 ipfs add test/mfs1.txt test/mfs2.txt --to-files /mfsdir1 >actual 2>&1 &&
424 test_should_contain "Error: to-files: cannot put node in path \"/mfsdir1\"" actual &&
425 ipfs files rm -r --force /mfsdir1
426 '
427
428 test_expect_success "ipfs add file1 file2 --to-files /mfsdir2/ (with trailing slash) succeeds" '
429 ipfs files mkdir -p /mfsdir2 &&
430 test_expect_code 0 ipfs add --cid-version 1 test/mfs1.txt test/mfs2.txt --to-files /mfsdir2/ > actual 2>&1 &&
431 test_should_contain "added bafkreihm3rktn5z33luic3youqdsn326toaq3ekesmdvsa53sbrd3f5r3a mfs1.txt" actual &&
432 test_should_contain "added bafkreidh5zkhr2vnwa2luwmuj24xo6l3jhfgvkgtk5cyp43oxs7owzpxby mfs2.txt" actual &&
433 test_should_not_contain "Error" actual &&
434 ipfs files ls /mfsdir2/ > lsout &&
435 test_should_contain "mfs1.txt" lsout &&
436 test_should_contain "mfs2.txt" lsout &&
437 ipfs files rm -r --force /mfsdir2
438 '
439
440 test_expect_success "ipfs add file1 file2 --to-files /mfsfile2/ (with trailing slash + with preexisting file) fails" '
441 echo test | ipfs files write --create /mfsfile2 &&
442 test_expect_code 1 ipfs add test/mfs1.txt test/mfs2.txt --to-files /mfsfile2/ >actual 2>&1 &&
443 test_should_contain "Error: to-files: MFS destination \"/mfsfile2/\" is not a directory" actual &&
444 ipfs files rm -r --force /mfsfile2
445 '
446
447 ## --to-files with recursive dir
448
449 # test MFS destination without trailing slash
450 test_expect_success "ipfs add with --to-files /mfs/subdir3 fails because /mfs/subdir3 exists" '
451 ipfs files mkdir -p /mfs/subdir3 &&
452 test_expect_code 1 ipfs add -r test --to-files /mfs/subdir3 >actual 2>&1 &&
453 test_should_contain "cannot put node in path \"/mfs/subdir3\": directory already has entry by that name" actual &&
454 ipfs files rm -r --force /mfs
455 '
456
457 # test recursive import of a dir into MFS subdirectory
458 test_expect_success "ipfs add -r dir --to-files /mfs/subdir4/ succeeds (because of trailing slash)" '
459 ipfs files mkdir -p /mfs/subdir4 &&
460 ipfs add --cid-version 1 -r test --to-files /mfs/subdir4/ >actual 2>&1 &&
461 test_should_contain "added bafkreihm3rktn5z33luic3youqdsn326toaq3ekesmdvsa53sbrd3f5r3a test/mfs1.txt" actual &&
462 test_should_contain "added bafkreidh5zkhr2vnwa2luwmuj24xo6l3jhfgvkgtk5cyp43oxs7owzpxby test/mfs2.txt" actual &&
463 test_should_contain "added bafybeic7xwqwovt4g4bax6d3udp6222i63vj2rblpbim7uy2uw4a5gahha test" actual &&
464 test_should_not_contain "Error" actual
465 ipfs files ls /mfs/subdir4/ > lsout &&
466 test_should_contain "test" lsout &&
467 test_should_not_contain "mfs1.txt" lsout &&
468 test_should_not_contain "mfs2.txt" lsout &&
469 ipfs files rm -r --force /mfs
470 '
471
472 # confirm -w and --to-files are exclusive
473 # context: https://github.com/ipfs/kubo/issues/10611
474 test_expect_success "ipfs add -r -w dir --to-files /mfs/subdir5/ errors (-w and --to-files are exclusive)" '
475 ipfs files mkdir -p /mfs/subdir5 &&
476 test_expect_code 1 ipfs add -r -w test --to-files /mfs/subdir5/ >actual 2>&1 &&
477 test_should_contain "Error" actual &&
478 ipfs files rm -r --force /mfs
479 '
480
481 }
482
483 test_add_cat_5MB() {
484 ADD_FLAGS="$1"
485 EXP_HASH="$2"
486
487 test_expect_success "generate 5MB file using random-data" '
488 random-data -size=5242880 -seed=41 >mountdir/bigfile
489 '
490
491 test_expect_success "sha1 of the file looks ok" '
492 echo "11145b8c4bc8f87ea2fcfc3d55708b8cac2aadf12862" >sha1_expected &&
493 multihash -a=sha1 -e=hex mountdir/bigfile >sha1_actual &&
494 test_cmp sha1_expected sha1_actual
495 '
496
497 test_expect_success "'ipfs add $ADD_FLAGS bigfile' succeeds" '
498 ipfs add $ADD_FLAGS mountdir/bigfile >actual ||
499 test_fsh cat daemon_err
500 '
501
502 test_expect_success "'ipfs add bigfile' output looks good" '
503 echo "added $EXP_HASH bigfile" >expected &&
504 test_cmp expected actual
505 '
506 test_expect_success "'ipfs cat' succeeds" '
507 ipfs cat "$EXP_HASH" >actual
508 '
509
510 test_expect_success "'ipfs cat' output looks good" '
511 test_cmp mountdir/bigfile actual
512 '
513
514 test_expect_success FUSE "cat ipfs/bigfile succeeds" '
515 cat "ipfs/$EXP_HASH" >actual
516 '
517
518 test_expect_success FUSE "cat ipfs/bigfile looks good" '
519 test_cmp mountdir/bigfile actual
520 '
521
522 test_expect_success "remove hash" '
523 ipfs pin rm "$EXP_HASH" &&
524 ipfs block rm "$EXP_HASH"
525 '
526
527 test_expect_success "get base32 version of CID" '
528 ipfs cid base32 $EXP_HASH > base32_cid &&
529 BASE32_HASH=`cat base32_cid`
530 '
531
532 test_expect_success "ipfs add --cid-base=base32 bigfile' succeeds" '
533 ipfs add $ADD_FLAGS --cid-base=base32 mountdir/bigfile >actual ||
534 test_fsh cat daemon_err
535 '
536
537 test_expect_success "'ipfs add bigfile --cid-base=base32' output looks good" '
538 echo "added $BASE32_HASH bigfile" >expected &&
539 test_cmp expected actual
540 '
541
542 test_expect_success "'ipfs cat $BASE32_HASH' succeeds" '
543 ipfs cat "$BASE32_HASH" >actual
544 '
545 }
546
547 test_add_cat_raw() {
548 test_expect_success "add a small file with raw-leaves" '
549 echo "foobar" > afile &&
550 HASH=$(ipfs add -q --raw-leaves afile)
551 '
552
553 test_expect_success "cat that small file" '
554 ipfs cat $HASH > afile_out
555 '
556
557 test_expect_success "make sure it looks good" '
558 test_cmp afile afile_out
559 '
560
561 test_expect_success "add zero length file with raw-leaves" '
562 touch zero-length-file &&
563 ZEROHASH=$(ipfs add -q --raw-leaves zero-length-file) &&
564 echo $ZEROHASH
565 '
566
567 test_expect_success "zero length file has correct hash" '
568 test "$ZEROHASH" = bafkreihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku
569 '
570
571 test_expect_success "cat zero length file" '
572 ipfs cat $ZEROHASH > zero-length-file_out
573 '
574
575 test_expect_success "make sure it looks good" '
576 test_cmp zero-length-file zero-length-file_out
577 '
578 }
579
580 test_add_cat_derefargs() {
581 test_expect_success "create and hash zero length file" '
582 touch zero-length-file &&
583 ZEROHASH=$(ipfs add -q -n zero-length-file)
584 '
585
586 test_expect_success "create symlink and add with dereferenced arguments" '
587 ln -s zero-length-file symlink-to-zero &&
588 HASH=$(ipfs add -q -n --dereference-args symlink-to-zero) &&
589 test $HASH = $ZEROHASH
590 '
591 }
592
593 test_add_cat_expensive() {
594 ADD_FLAGS="$1"
595 HASH="$2"
596
597 test_expect_success EXPENSIVE "generate 100MB file using random-data" '
598 random-data -size=104857600 -seed=42 >mountdir/bigfile
599 '
600
601 test_expect_success EXPENSIVE "sha1 of the file looks ok" '
602 echo "11141e8c04d7cd019cc0acf0311a8ca6cf2c18413c96" >sha1_expected &&
603 multihash -a=sha1 -e=hex mountdir/bigfile >sha1_actual &&
604 test_cmp sha1_expected sha1_actual
605 '
606
607 test_expect_success EXPENSIVE "ipfs add $ADD_FLAGS bigfile succeeds" '
608 ipfs add $ADD_FLAGS mountdir/bigfile >actual
609 '
610
611 test_expect_success EXPENSIVE "ipfs add bigfile output looks good" '
612 echo "added $HASH bigfile" >expected &&
613 test_cmp expected actual
614 '
615
616 test_expect_success EXPENSIVE "ipfs cat succeeds" '
617 ipfs cat "$HASH" | multihash -a=sha1 -e=hex >sha1_actual
618 '
619
620 test_expect_success EXPENSIVE "ipfs cat output looks good" '
621 ipfs cat "$HASH" >actual &&
622 test_cmp mountdir/bigfile actual
623 '
624
625 test_expect_success EXPENSIVE "ipfs cat output hashed looks good" '
626 echo "11141e8c04d7cd019cc0acf0311a8ca6cf2c18413c96" >sha1_expected &&
627 test_cmp sha1_expected sha1_actual
628 '
629
630 test_expect_success FUSE,EXPENSIVE "cat ipfs/bigfile succeeds" '
631 cat "ipfs/$HASH" | multihash -a=sha1 -e=hex >sha1_actual
632 '
633
634 test_expect_success FUSE,EXPENSIVE "cat ipfs/bigfile looks good" '
635 test_cmp sha1_expected sha1_actual
636 '
637 }
638
639 test_add_named_pipe() {
640 test_expect_success "Adding named pipes explicitly works" '
641 mkfifo named-pipe1 &&
642 ( echo foo > named-pipe1 & echo "added $( echo foo | ipfs add -nq ) named-pipe1" > expected_named_pipes_add ) &&
643 mkfifo named-pipe2 &&
644 ( echo bar > named-pipe2 & echo "added $( echo bar | ipfs add -nq ) named-pipe2" >> expected_named_pipes_add ) &&
645 ipfs add -n named-pipe1 named-pipe2 >actual_pipe_add &&
646 rm named-pipe1 &&
647 rm named-pipe2 &&
648 test_cmp expected_named_pipes_add actual_pipe_add
649 '
650
651 test_expect_success "useful error message when recursively adding a named pipe" '
652 mkdir -p named-pipe-dir &&
653 mkfifo named-pipe-dir/named-pipe &&
654 STAT=$(generic_stat named-pipe-dir/named-pipe) &&
655 test_expect_code 1 ipfs add -r named-pipe-dir 2>actual &&
656 printf "Error: unrecognized file type for named-pipe-dir/named-pipe: $STAT\n" >expected &&
657 rm named-pipe-dir/named-pipe &&
658 rmdir named-pipe-dir &&
659 test_cmp expected actual
660 '
661 }
662
663 test_add_pwd_is_symlink() {
664 test_expect_success "ipfs add -r adds directory content when ./ is symlink" '
665 mkdir hellodir &&
666 echo "World" > hellodir/world &&
667 ln -s hellodir hellolink &&
668 ( cd hellolink &&
669 ipfs add -r . > ../actual ) &&
670 grep "added Qma9CyFdG5ffrZCcYSin2uAETygB25cswVwEYYzwfQuhTe" actual &&
671 rm -r hellodir
672 '
673 }
674
675 test_launch_ipfs_daemon_and_mount
676
677 test_expect_success "'ipfs add --help' succeeds" '
678 ipfs add --help >actual
679 '
680
681 test_expect_success "'ipfs add --help' output looks good" '
682 egrep "ipfs add.*<path>" actual >/dev/null ||
683 test_fsh cat actual
684 '
685
686 test_expect_success "'ipfs help add' succeeds" '
687 ipfs help add >actual
688 '
689
690 test_expect_success "'ipfs help add' output looks good" '
691 egrep "ipfs add.*<path>" actual >/dev/null ||
692 test_fsh cat actual
693 '
694
695 test_expect_success "'ipfs cat --help' succeeds" '
696 ipfs cat --help >actual
697 '
698
699 test_expect_success "'ipfs cat --help' output looks good" '
700 egrep "ipfs cat.*<ipfs-path>" actual >/dev/null ||
701 test_fsh cat actual
702 '
703
704 test_expect_success "'ipfs help cat' succeeds" '
705 ipfs help cat >actual
706 '
707
708 test_expect_success "'ipfs help cat' output looks good" '
709 egrep "ipfs cat.*<ipfs-path>" actual >/dev/null ||
710 test_fsh cat actual
711 '
712
713 test_add_cat_file
714
715 test_expect_success "ipfs cat succeeds with stdin opened (issue #1141)" '
716 cat mountdir/hello.txt | while read line; do ipfs cat "$HASH" >actual || exit; done
717 '
718
719 test_expect_success "ipfs cat output looks good" '
720 cat mountdir/hello.txt >expected &&
721 test_cmp expected actual
722 '
723
724 test_expect_success "ipfs cat accept hash from built input" '
725 echo "$HASH" | ipfs cat >actual
726 '
727
728 test_expect_success "ipfs cat output looks good" '
729 test_cmp expected actual
730 '
731
732 test_expect_success FUSE "cat ipfs/stuff succeeds" '
733 cat "ipfs/$HASH" >actual
734 '
735
736 test_expect_success FUSE "cat ipfs/stuff looks good" '
737 test_cmp expected actual
738 '
739
740 test_expect_success "'ipfs add -q' succeeds" '
741 echo "Hello Venus!" >mountdir/venus.txt &&
742 ipfs add -q mountdir/venus.txt >actual
743 '
744
745 test_expect_success "'ipfs add -q' output looks good" '
746 HASH="QmU5kp3BH3B8tnWUU2Pikdb2maksBNkb92FHRr56hyghh4" &&
747 echo "$HASH" >expected &&
748 test_cmp expected actual
749 '
750
751 test_expect_success "'ipfs add -q' with stdin input succeeds" '
752 echo "Hello Jupiter!" | ipfs add -q >actual
753 '
754
755 test_expect_success "'ipfs add -q' output looks good" '
756 HASH="QmUnvPcBctVTAcJpigv6KMqDvmDewksPWrNVoy1E1WP5fh" &&
757 echo "$HASH" >expected &&
758 test_cmp expected actual
759 '
760
761 test_expect_success "'ipfs cat' succeeds" '
762 ipfs cat "$HASH" >actual
763 '
764
765 test_expect_success "ipfs cat output looks good" '
766 echo "Hello Jupiter!" >expected &&
767 test_cmp expected actual
768 '
769
770 test_expect_success "'ipfs add' with stdin input succeeds" '
771 printf "Hello Neptune!\nHello Pluton!" | ipfs add >actual
772 '
773
774 test_expect_success "'ipfs add' output looks good" '
775 HASH="QmZDhWpi8NvKrekaYYhxKCdNVGWsFFe1CREnAjP1QbPaB3" &&
776 echo "added $HASH $HASH" >expected &&
777 test_cmp expected actual
778 '
779
780 test_expect_success "'ipfs cat' with built input succeeds" '
781 echo "$HASH" | ipfs cat >actual
782 '
783
784 test_expect_success "ipfs cat with built input output looks good" '
785 printf "Hello Neptune!\nHello Pluton!" >expected &&
786 test_cmp expected actual
787 '
788
789 add_directory() {
790 EXTRA_ARGS=$1
791
792 test_expect_success "'ipfs add -r $EXTRA_ARGS' succeeds" '
793 mkdir mountdir/planets &&
794 echo "Hello Mars!" >mountdir/planets/mars.txt &&
795 echo "Hello Venus!" >mountdir/planets/venus.txt &&
796 ipfs add -r $EXTRA_ARGS mountdir/planets >actual
797 '
798
799 test_expect_success "'ipfs add -r $EXTRA_ARGS' output looks good" '
800 echo "added $MARS planets/mars.txt" >expected &&
801 echo "added $VENUS planets/venus.txt" >>expected &&
802 echo "added $PLANETS planets" >>expected &&
803 test_cmp expected actual
804 '
805
806 test_expect_success "ipfs cat accept many hashes from built input" '
807 { echo "$MARS"; echo "$VENUS"; } | ipfs cat >actual
808 '
809
810 test_expect_success "ipfs cat output looks good" '
811 cat mountdir/planets/mars.txt mountdir/planets/venus.txt >expected &&
812 test_cmp expected actual
813 '
814
815 test_expect_success "ipfs cat accept many hashes as args" '
816 ipfs cat "$MARS" "$VENUS" >actual
817 '
818
819 test_expect_success "ipfs cat output looks good" '
820 test_cmp expected actual
821 '
822
823 test_expect_success "ipfs cat with both arg and stdin" '
824 echo "$MARS" | ipfs cat "$VENUS" >actual
825 '
826
827 test_expect_success "ipfs cat output looks good" '
828 cat mountdir/planets/venus.txt >expected &&
829 test_cmp expected actual
830 '
831
832 test_expect_success "ipfs cat with two args and stdin" '
833 echo "$MARS" | ipfs cat "$VENUS" "$VENUS" >actual
834 '
835
836 test_expect_success "ipfs cat output looks good" '
837 cat mountdir/planets/venus.txt mountdir/planets/venus.txt >expected &&
838 test_cmp expected actual
839 '
840
841 test_expect_success "ipfs add --quieter succeeds" '
842 ipfs add -r -Q $EXTRA_ARGS mountdir/planets >actual
843 '
844
845 test_expect_success "ipfs add --quieter returns only one correct hash" '
846 echo "$PLANETS" > expected &&
847 test_cmp expected actual
848 '
849
850 test_expect_success "cleanup" '
851 rm -r mountdir/planets
852 '
853 }
854
855 PLANETS="QmWSgS32xQEcXMeqd3YPJLrNBLSdsfYCep2U7CFkyrjXwY"
856 MARS="QmPrrHqJzto9m7SyiRzarwkqPcCSsKR2EB1AyqJfe8L8tN"
857 VENUS="QmU5kp3BH3B8tnWUU2Pikdb2maksBNkb92FHRr56hyghh4"
858 add_directory
859
860 PLANETS="QmfWfQfKCY5Ukv9peBbxM5vqWM9BzmqUSXvdCgjT2wsiBT"
861 MARS="bafkreibmlvvgdyihetgocpof6xk64kjjzdeq2e4c7hqs3krdheosk4tgj4"
862 VENUS="bafkreihfsphazrk2ilejpekyltjeh5k4yvwgjuwg26ueafohqioeo3sdca"
863 add_directory '--raw-leaves'
864
865 PLANETS="bafybeih7e5dmkyk25up5vxug4q3hrg2fxbzf23dfrac2fns5h7z4aa7ioi"
866 MARS="bafkreibmlvvgdyihetgocpof6xk64kjjzdeq2e4c7hqs3krdheosk4tgj4"
867 VENUS="bafkreihfsphazrk2ilejpekyltjeh5k4yvwgjuwg26ueafohqioeo3sdca"
868 add_directory '--cid-version=1'
869
870 PLANETS="bafybeif5tuep5ap2d7zyhbktucey75aoacxufgt6i3v4gebmixyipnyp7y"
871 MARS="bafybeiawta2ntdmsy24aro35w3homzl4ak7svr3si7l7gesvq4erglyye4"
872 VENUS="bafybeicvkvhs2fr75ynebtdjqpgm4g2fc63abqbmysupwpmcjl4gx7mzrm"
873 add_directory '--cid-version=1 --raw-leaves=false'
874
875 PLANETS="bafykbzaceaptbcs7ik5mdfpot3b4ackvxlwh7loc5jcrtkayf64ukl7zyk46e"
876 MARS="bafk2bzaceaqcxw46uzkyd2jmczoogof6pnkqt4dpiv3pwkunsv4g5rkkmecie"
877 VENUS="bafk2bzacebxnke2fb5mgzxyjuuavvcfht4fd3gvn4klkujz6k72wboynhuvfw"
878 add_directory '--hash=blake2b-256'
879
880 test_expect_success "'ipfs add -rn' succeeds" '
881 mkdir -p mountdir/moons/jupiter &&
882 mkdir -p mountdir/moons/saturn &&
883 echo "Hello Europa!" >mountdir/moons/jupiter/europa.txt &&
884 echo "Hello Titan!" >mountdir/moons/saturn/titan.txt &&
885 echo "hey you are no moon!" >mountdir/moons/mercury.txt &&
886 ipfs add -rn mountdir/moons >actual
887 '
888
889 test_expect_success "'ipfs add -rn' output looks good" '
890 MOONS="QmbGoaQZm8kjYfCiN1aBsgwhqfUBGDYTrDb91Mz7Dvq81B" &&
891 EUROPA="Qmbjg7zWdqdMaK2BucPncJQDxiALExph5k3NkQv5RHpccu" &&
892 JUPITER="QmS5mZddhFPLWFX3w6FzAy9QxyYkaxvUpsWCtZ3r7jub9J" &&
893 SATURN="QmaMagZT4rTE7Nonw8KGSK4oe1bh533yhZrCo1HihSG8FK" &&
894 TITAN="QmZzppb9WHn552rmRqpPfgU5FEiHH6gDwi3MrB9cTdPwdb" &&
895 MERCURY="QmRsTB5CpEUvDUpDgHCzb3VftZ139zrk9zs5ZcgYh9TMPJ" &&
896 echo "added $EUROPA moons/jupiter/europa.txt" >expected &&
897 echo "added $MERCURY moons/mercury.txt" >>expected &&
898 echo "added $TITAN moons/saturn/titan.txt" >>expected &&
899 echo "added $JUPITER moons/jupiter" >>expected &&
900 echo "added $SATURN moons/saturn" >>expected &&
901 echo "added $MOONS moons" >>expected &&
902 test_cmp expected actual
903 '
904
905 test_expect_success "random-data is installed" '
906 type random-data
907 '
908
909 test_add_cat_5MB "" "QmapAfmzmeWYTNztMQEhUXFcSGrsax22WRG7YN9xLdMeQq"
910
911 test_add_cat_5MB --raw-leaves "QmabWSFaPusmiZaaVZLhEUtHcj8CCvVeUfkBpKqAkKVMiS"
912
913 # note: the specified hash implies that internal nodes are stored
914 # using CidV1 and leaves are stored using raw blocks
915 test_add_cat_5MB --cid-version=1 "bafybeifwdkm32fmukqwh3jofm6ma76bcqvn6opxstsnzmya7utboi4cb2m"
916
917 # note: the specified hash implies that internal nodes are stored
918 # using CidV1 and leaves are stored using CidV1 but using the legacy
919 # format (i.e. not raw)
920 test_add_cat_5MB '--cid-version=1 --raw-leaves=false' "bafybeifq4unep5w4agr3nlynxidj2rymf6dzu6bf4ieqqildkboe5mdmne"
921
922 # note: --hash=blake2b-256 implies --cid-version=1 which implies --raw-leaves=true
923 # the specified hash represents the leaf nodes stored as raw leaves and
924 # encoded with the blake2b-256 hash function
925 test_add_cat_5MB '--hash=blake2b-256' "bafykbzacebxcnlql4oc3mtscqn32aumqkqxxv3wt7dkyrphgh6lc2gckiq6bw"
926
927 # the specified hash represents the leaf nodes stored as protoful nodes and
928 # encoded with the blake2b-256 hash function
929 test_add_cat_5MB '--hash=blake2b-256 --raw-leaves=false' "bafykbzacearibnoamkfmcagpfgk2sbgx65qftnsrh4ttd3g7ghooasfnyavme"
930
931 test_add_cat_expensive "" "Qma1WZKC3jad7e3F7GEDvkFdhPLyMEhKszBF4nBUCBGh6c"
932
933 # note: the specified hash implies that internal nodes are stored
934 # using CidV1 and leaves are stored using raw blocks
935 test_add_cat_expensive "--cid-version=1" "bafybeibdfw7nsmb3erhej2k6v4eopaswsf5yfv2ikweqa3qsc5no4jywqu"
936
937 # note: --hash=blake2b-256 implies --cid-version=1 which implies --raw-leaves=true
938 # the specified hash represents the leaf nodes stored as raw leaves and
939 # encoded with the blake2b-256 hash function
940 test_add_cat_expensive '--hash=blake2b-256' "bafykbzaceduy3thhmcf6ptfqzxberlvj7sgo4uokrvd6qwrhim6r3rgcb26qi"
941
942 test_add_named_pipe
943
944 test_add_pwd_is_symlink
945
946 test_add_cat_raw
947
948 test_expect_success "ipfs add --cid-version=9 fails" '
949 echo "context" > afile.txt &&
950 test_must_fail ipfs add --cid-version=9 afile.txt 2>&1 | tee add_out &&
951 grep -q "unknown CID version" add_out
952 '
953
954 test_kill_ipfs_daemon
955
956 # should work offline
957
958 test_add_cat_file
959
960 test_add_cat_raw
961
962 test_expect_success "ipfs add --only-hash succeeds" '
963 echo "unknown content for only-hash" | ipfs add --only-hash -q > oh_hash
964 '
965
966 test_add_cat_derefargs
967
968 #TODO: this doesn't work when online hence separated out from test_add_cat_file
969 test_expect_success "ipfs cat file fails" '
970 test_must_fail ipfs cat $(cat oh_hash)
971 '
972
973 test_add_named_pipe
974
975 test_add_pwd_is_symlink
976
977 # Test daemon in offline mode
978 test_launch_ipfs_daemon_without_network
979
980 test_add_cat_file
981
982 test_kill_ipfs_daemon
983
984 test_done