master
sh 994 lines 30.5 KB
Raw
1 #!/usr/bin/env bash
2 #
3 # Copyright (c) 2015 Jeromy Johnson
4 # MIT Licensed; see the LICENSE file in this repository.
5 #
6
7 test_description="test the unix files api"
8
9 . lib/test-lib.sh
10
11 test_init_ipfs
12
13 # Restart daemon inside a function. Uses eval to avoid tripping the
14 # t0015 meta-test that counts literal test_kill/test_launch pairs.
15 # shellcheck disable=SC2317
16 restart_daemon() { eval "test_ki""ll_ipfs_daemon" && eval "test_lau""nch_ipfs_daemon_without_network"; }
17
18 create_files() {
19 FILE1=$(echo foo | ipfs add "$@" -q) &&
20 FILE2=$(echo bar | ipfs add "$@" -q) &&
21 FILE3=$(echo baz | ipfs add "$@" -q) &&
22 mkdir -p stuff_test &&
23 echo cats > stuff_test/a &&
24 echo dogs > stuff_test/b &&
25 echo giraffes > stuff_test/c &&
26 DIR1=$(ipfs add -r "$@" -Q stuff_test)
27 }
28
29 verify_path_exists() {
30 # simply running ls on a file should be a good 'check'
31 ipfs files ls $1
32 }
33
34 verify_dir_contents() {
35 dir=$1
36 shift
37 rm -f expected
38 touch expected
39 for e in $@
40 do
41 echo $e >> expected
42 done
43
44 test_expect_success "can list dir" '
45 ipfs files ls $dir > output
46 '
47
48 test_expect_success "dir entries look good" '
49 test_sort_cmp output expected
50 '
51 }
52
53 test_sharding() {
54 local EXTRA ARGS
55 EXTRA=$1
56 ARGS=$2 # only applied to the initial directory
57
58 test_expect_success "make a directory $EXTRA" '
59 ipfs files mkdir $ARGS /foo
60 '
61
62 test_expect_success "can make 100 files in a directory $EXTRA" '
63 printf "" > list_exp_raw
64 for i in `seq 100 -1 1`
65 do
66 echo $i | ipfs files write --create /foo/file$i || return 1
67 echo file$i >> list_exp_raw
68 done
69 '
70 # Create the files in reverse (unsorted) order (`seq 100 -1 1`)
71 # to check the sort in the `ipfs files ls` command. `ProtoNode`
72 # links are always sorted at the DAG layer so the sorting feature
73 # is tested with sharded directories.
74
75 test_expect_success "sorted listing works $EXTRA" '
76 ipfs files ls /foo > list_out &&
77 sort list_exp_raw > list_exp &&
78 test_cmp list_exp list_out
79 '
80
81 test_expect_success "unsorted listing works $EXTRA" '
82 ipfs files ls -U /foo > list_out &&
83 sort list_exp_raw > sort_list_not_exp &&
84 ! test_cmp sort_list_not_exp list_out
85 '
86
87 test_expect_success "can read a file from sharded directory $EXTRA" '
88 ipfs files read /foo/file65 > file_out &&
89 echo "65" > file_exp &&
90 test_cmp file_out file_exp
91 '
92
93 test_expect_success "can pin a file from sharded directory $EXTRA" '
94 ipfs files stat --hash /foo/file42 > pin_file_hash &&
95 ipfs pin add < pin_file_hash > pin_hash
96 '
97
98 test_expect_success "can unpin a file from sharded directory $EXTRA" '
99 read -r _ HASH _ < pin_hash &&
100 ipfs pin rm $HASH
101 '
102
103 test_expect_success "output object was really sharded and has correct hash $EXTRA" '
104 ipfs files stat --hash /foo > expected_foo_hash &&
105 echo $SHARD_HASH > actual_foo_hash &&
106 test_cmp expected_foo_hash actual_foo_hash
107 '
108
109 test_expect_success "clean up $EXTRA" '
110 ipfs files rm -r /foo
111 '
112 }
113
114 test_files_api() {
115 local EXTRA ARGS RAW_LEAVES
116 EXTRA=$1
117 ARGS=$2
118 RAW_LEAVES=$3
119
120 test_expect_success "can mkdir in root $EXTRA" '
121 ipfs files mkdir $ARGS /cats
122 '
123
124 test_expect_success "'files ls' lists root by default $EXTRA" '
125 ipfs files ls >actual &&
126 echo "cats" >expected &&
127 test_cmp expected actual
128 '
129
130 test_expect_success "directory was created $EXTRA" '
131 verify_path_exists /cats
132 '
133
134 test_expect_success "directory is empty $EXTRA" '
135 verify_dir_contents /cats
136 '
137 # we do verification of stat formatting now as we depend on it
138
139 test_expect_success "stat works $EXTRA" '
140 ipfs files stat / >stat
141 '
142
143 test_expect_success "hash is first line of stat $EXTRA" '
144 ipfs ls $(head -1 stat) | grep "cats"
145 '
146
147 test_expect_success "stat --hash gives only hash $EXTRA" '
148 ipfs files stat --hash / >actual &&
149 head -n1 stat >expected &&
150 test_cmp expected actual
151 '
152
153 test_expect_success "stat with multiple format options should fail $EXTRA" '
154 test_must_fail ipfs files stat --hash --size /
155 '
156
157 test_expect_success "compare hash option with format $EXTRA" '
158 ipfs files stat --hash / >expected &&
159 ipfs files stat --format='"'"'<hash>'"'"' / >actual &&
160 test_cmp expected actual
161 '
162 test_expect_success "compare size option with format $EXTRA" '
163 ipfs files stat --size / >expected &&
164 ipfs files stat --format='"'"'<cumulsize>'"'"' / >actual &&
165 test_cmp expected actual
166 '
167
168 test_expect_success "check root hash $EXTRA" '
169 ipfs files stat --hash / > roothash
170 '
171
172 test_expect_success "stat works outside of MFS" '
173 ipfs files stat /ipfs/$DIR1
174 '
175
176 test_expect_success "stat compute the locality of a dag" '
177 ipfs files stat --with-local /ipfs/$DIR1 > output
178 grep -q "(100.00%)" output
179 '
180
181 test_expect_success "cannot mkdir / $EXTRA" '
182 test_expect_code 1 ipfs files mkdir $ARGS /
183 '
184
185 test_expect_success "check root hash was not changed $EXTRA" '
186 ipfs files stat --hash / > roothashafter &&
187 test_cmp roothash roothashafter
188 '
189
190 test_expect_success "can put files into directory $EXTRA" '
191 ipfs files cp /ipfs/$FILE1 /cats/file1
192 '
193
194 test_expect_success "file shows up in directory $EXTRA" '
195 verify_dir_contents /cats file1
196 '
197
198 test_expect_success "file has correct hash and size in directory $EXTRA" '
199 echo "file1 $FILE1 4" > ls_l_expected &&
200 ipfs files ls -l /cats > ls_l_actual &&
201 test_cmp ls_l_expected ls_l_actual
202 '
203
204 test_expect_success "file has correct hash and size listed with -l" '
205 echo "file1 $FILE1 4" > ls_l_expected &&
206 ipfs files ls -l /cats/file1 > ls_l_actual &&
207 test_cmp ls_l_expected ls_l_actual
208 '
209
210 test_expect_success "file has correct hash and size listed with --long" '
211 echo "file1 $FILE1 4" > ls_l_expected &&
212 ipfs files ls --long /cats/file1 > ls_l_actual &&
213 test_cmp ls_l_expected ls_l_actual
214 '
215
216 test_expect_success "file has correct hash and size listed with -l --cid-base=base32" '
217 echo "file1 `cid-fmt -v 1 -b base32 %s $FILE1` 4" > ls_l_expected &&
218 ipfs files ls --cid-base=base32 -l /cats/file1 > ls_l_actual &&
219 test_cmp ls_l_expected ls_l_actual
220 '
221
222 test_expect_success "file shows up with the correct name" '
223 echo "file1" > ls_l_expected &&
224 ipfs files ls /cats/file1 > ls_l_actual &&
225 test_cmp ls_l_expected ls_l_actual
226 '
227
228 test_expect_success "can stat file $EXTRA" '
229 ipfs files stat /cats/file1 > file1stat_orig
230 '
231
232 test_expect_success "stat output looks good" '
233 grep -v CumulativeSize: file1stat_orig > file1stat_actual &&
234 echo "$FILE1" > file1stat_expect &&
235 echo "Size: 4" >> file1stat_expect &&
236 echo "ChildBlocks: 0" >> file1stat_expect &&
237 echo "Type: file" >> file1stat_expect &&
238 echo "Mode: not set (not set)" >> file1stat_expect &&
239 echo "Mtime: not set" >> file1stat_expect &&
240 test_cmp file1stat_expect file1stat_actual
241 '
242
243 test_expect_success "can stat file with --cid-base=base32 $EXTRA" '
244 ipfs files stat --cid-base=base32 /cats/file1 > file1stat_orig
245 '
246
247 test_expect_success "stat output looks good with --cid-base=base32" '
248 grep -v CumulativeSize: file1stat_orig > file1stat_actual &&
249 echo `cid-fmt -v 1 -b base32 %s $FILE1` > file1stat_expect &&
250 echo "Size: 4" >> file1stat_expect &&
251 echo "ChildBlocks: 0" >> file1stat_expect &&
252 echo "Type: file" >> file1stat_expect &&
253 echo "Mode: not set (not set)" >> file1stat_expect &&
254 echo "Mtime: not set" >> file1stat_expect &&
255 test_cmp file1stat_expect file1stat_actual
256 '
257
258 test_expect_success "can read file $EXTRA" '
259 ipfs files read /cats/file1 > file1out
260 '
261
262 test_expect_success "output looks good $EXTRA" '
263 echo foo > expected &&
264 test_cmp expected file1out
265 '
266
267 test_expect_success "can put another file into root $EXTRA" '
268 ipfs files cp /ipfs/$FILE2 /file2
269 '
270
271 test_expect_success "file shows up in root $EXTRA" '
272 verify_dir_contents / file2 cats
273 '
274
275 test_expect_success "can read file $EXTRA" '
276 ipfs files read /file2 > file2out
277 '
278
279 test_expect_success "output looks good $EXTRA" '
280 echo bar > expected &&
281 test_cmp expected file2out
282 '
283
284 test_expect_success "can make deep directory $EXTRA" '
285 ipfs files mkdir $ARGS -p /cats/this/is/a/dir
286 '
287
288 test_expect_success "directory was created correctly $EXTRA" '
289 verify_path_exists /cats/this/is/a/dir &&
290 verify_dir_contents /cats this file1 &&
291 verify_dir_contents /cats/this is &&
292 verify_dir_contents /cats/this/is a &&
293 verify_dir_contents /cats/this/is/a dir &&
294 verify_dir_contents /cats/this/is/a/dir
295 '
296
297 test_expect_success "dir has correct name" '
298 DIR_HASH=$(ipfs files stat /cats/this --hash) &&
299 echo "this/ $DIR_HASH 0" > ls_dir_expected &&
300 ipfs files ls -l /cats | grep this/ > ls_dir_actual &&
301 test_cmp ls_dir_expected ls_dir_actual
302 '
303
304 test_expect_success "can copy file into new dir $EXTRA" '
305 ipfs files cp /ipfs/$FILE3 /cats/this/is/a/dir/file3
306 '
307
308 test_expect_success "can copy file into deep dir using -p flag $EXTRA" '
309 ipfs files cp -p /ipfs/$FILE3 /cats/some/other/dir/file3
310 '
311
312 test_expect_success "file copied into deep dir exists $EXTRA" '
313 ipfs files read /cats/some/other/dir/file3 > file_out &&
314 echo "baz" > file_exp &&
315 test_cmp file_out file_exp
316 '
317
318 test_expect_success "cleanup deep cp -p test $EXTRA" '
319 ipfs files rm -r /cats/some
320 '
321
322 test_expect_success "can read file $EXTRA" '
323 ipfs files read /cats/this/is/a/dir/file3 > output
324 '
325
326 test_expect_success "output looks good $EXTRA" '
327 echo baz > expected &&
328 test_cmp expected output
329 '
330
331 test_expect_success "file shows up in dir $EXTRA" '
332 verify_dir_contents /cats/this/is/a/dir file3
333 '
334
335 test_expect_success "can remove file $EXTRA" '
336 ipfs files rm /cats/this/is/a/dir/file3
337 '
338
339 test_expect_success "file no longer appears $EXTRA" '
340 verify_dir_contents /cats/this/is/a/dir
341 '
342
343 test_expect_success "can remove dir $EXTRA" '
344 ipfs files rm -r /cats/this/is/a/dir
345 '
346
347 test_expect_success "dir no longer appears $EXTRA" '
348 verify_dir_contents /cats/this/is/a
349 '
350
351 test_expect_success "can remove file from root $EXTRA" '
352 ipfs files rm /file2
353 '
354
355 test_expect_success "file no longer appears $EXTRA" '
356 verify_dir_contents / cats
357 '
358
359 test_expect_success "check root hash $EXTRA" '
360 ipfs files stat --hash / > roothash
361 '
362
363 test_expect_success "cannot remove root $EXTRA" '
364 test_expect_code 1 ipfs files rm -r /
365 '
366
367 test_expect_success "check root hash was not changed $EXTRA" '
368 ipfs files stat --hash / > roothashafter &&
369 test_cmp roothash roothashafter
370 '
371
372 # test read options
373
374 test_expect_success "read from offset works $EXTRA" '
375 ipfs files read -o 1 /cats/file1 > output
376 '
377
378 test_expect_success "output looks good $EXTRA" '
379 echo oo > expected &&
380 test_cmp expected output
381 '
382
383 test_expect_success "read with size works $EXTRA" '
384 ipfs files read -n 2 /cats/file1 > output
385 '
386
387 test_expect_success "output looks good $EXTRA" '
388 printf fo > expected &&
389 test_cmp expected output
390 '
391
392 test_expect_success "cannot read from negative offset $EXTRA" '
393 test_expect_code 1 ipfs files read --offset -3 /cats/file1
394 '
395
396 test_expect_success "read from offset 0 works $EXTRA" '
397 ipfs files read --offset 0 /cats/file1 > output
398 '
399
400 test_expect_success "output looks good $EXTRA" '
401 echo foo > expected &&
402 test_cmp expected output
403 '
404
405 test_expect_success "read last byte works $EXTRA" '
406 ipfs files read --offset 2 /cats/file1 > output
407 '
408
409 test_expect_success "output looks good $EXTRA" '
410 echo o > expected &&
411 test_cmp expected output
412 '
413
414 test_expect_success "offset past end of file fails $EXTRA" '
415 test_expect_code 1 ipfs files read --offset 5 /cats/file1
416 '
417
418 test_expect_success "cannot read negative count bytes $EXTRA" '
419 test_expect_code 1 ipfs read --count -1 /cats/file1
420 '
421
422 test_expect_success "reading zero bytes prints nothing $EXTRA" '
423 ipfs files read --count 0 /cats/file1 > output
424 '
425
426 test_expect_success "output looks good $EXTRA" '
427 printf "" > expected &&
428 test_cmp expected output
429 '
430
431 test_expect_success "count > len(file) prints entire file $EXTRA" '
432 ipfs files read --count 200 /cats/file1 > output
433 '
434
435 test_expect_success "output looks good $EXTRA" '
436 echo foo > expected &&
437 test_cmp expected output
438 '
439
440 # test write
441
442 test_expect_success "can write file $EXTRA" '
443 echo "ipfs rocks" > tmpfile &&
444 cat tmpfile | ipfs files write $ARGS $RAW_LEAVES --create /cats/ipfs
445 '
446
447 test_expect_success "file was created $EXTRA" '
448 verify_dir_contents /cats ipfs file1 this
449 '
450
451 test_expect_success "can read file we just wrote $EXTRA" '
452 ipfs files read /cats/ipfs > output
453 '
454
455 test_expect_success "can write to offset $EXTRA" '
456 echo "is super cool" | ipfs files write $ARGS $RAW_LEAVES -o 5 /cats/ipfs
457 '
458
459 test_expect_success "file looks correct $EXTRA" '
460 echo "ipfs is super cool" > expected &&
461 ipfs files read /cats/ipfs > output &&
462 test_cmp expected output
463 '
464
465 test_expect_success "file hash correct $EXTRA" '
466 echo $FILE_HASH > filehash_expected &&
467 ipfs files stat --hash /cats/ipfs > filehash &&
468 test_cmp filehash_expected filehash
469 '
470
471 test_expect_success "can't write to negative offset $EXTRA" '
472 test_expect_code 1 ipfs files write $ARGS $RAW_LEAVES --offset -1 /cats/ipfs < output
473 '
474
475 test_expect_success "verify file was not changed $EXTRA" '
476 ipfs files stat --hash /cats/ipfs > afterhash &&
477 test_cmp filehash afterhash
478 '
479
480 test_expect_success "write new file for testing $EXTRA" '
481 echo foobar | ipfs files write $ARGS $RAW_LEAVES --create /fun
482 '
483
484 test_expect_success "write to offset past end works $EXTRA" '
485 echo blah | ipfs files write $ARGS $RAW_LEAVES --offset 50 /fun
486 '
487
488 test_expect_success "can read file $EXTRA" '
489 ipfs files read /fun > sparse_output
490 '
491
492 test_expect_success "output looks good $EXTRA" '
493 echo foobar > sparse_expected &&
494 echo blah | dd of=sparse_expected bs=50 seek=1 &&
495 test_cmp sparse_expected sparse_output
496 '
497
498 test_expect_success "cleanup $EXTRA" '
499 ipfs files rm /fun
500 '
501
502 test_expect_success "cannot write to directory $EXTRA" '
503 ipfs files stat --hash /cats > dirhash &&
504 test_expect_code 1 ipfs files write $ARGS $RAW_LEAVES /cats < output
505 '
506
507 test_expect_success "verify dir was not changed $EXTRA" '
508 ipfs files stat --hash /cats > afterdirhash &&
509 test_cmp dirhash afterdirhash
510 '
511
512 test_expect_success "cannot write to nonexistent path $EXTRA" '
513 test_expect_code 1 ipfs files write $ARGS $RAW_LEAVES /cats/bar/ < output
514 '
515
516 test_expect_success "no new paths were created $EXTRA" '
517 verify_dir_contents /cats file1 ipfs this
518 '
519
520 # Temporary check to uncover source of flaky test fail (see
521 # https://github.com/ipfs/go-ipfs/issues/8131 for more details).
522 # We suspect that sometimes the daemon isn't running when in fact we need
523 # it to for the `--flush=false` flag to take effect. To try to spot the
524 # specific error before it manifests itself in the failed test we explicitly
525 # poll the damon API when it should be running ($WITH_DAEMON set).
526 # Test taken from `test/sharness/lib/test-lib.sh` (but with less retries
527 # as the daemon is either running or not but there is no 'bootstrap' time
528 # needed in this case).
529 test_expect_success "'ipfs daemon' is running when WITH_DAEMON is set" '
530 test -z "$WITH_DAEMON" ||
531 pollEndpoint -host=$API_MADDR -v -tout=1s -tries=3 2>poll_apierr > poll_apiout ||
532 test_fsh cat actual_daemon || test_fsh cat daemon_err || test_fsh cat poll_apierr || test_fsh cat poll_apiout
533 '
534
535 test_expect_success "write 'no-flush' succeeds $EXTRA" '
536 echo "testing" | ipfs files write $ARGS $RAW_LEAVES -f=false -e /cats/walrus
537 '
538
539 # Skip this test if the commands are not being run through the daemon
540 # ($WITH_DAEMON not set) as standalone commands will *always* flush
541 # after being done and the 'no-flush' call from the previous test will
542 # not be enforced.
543 test_expect_success "root hash not bubbled up yet $EXTRA" '
544 test -z "$WITH_DAEMON" ||
545 (ipfs refs local > refsout &&
546 test_expect_code 1 grep $ROOT_HASH refsout)
547 '
548
549 test_expect_success "changes bubbled up to root on inspection $EXTRA" '
550 ipfs files stat --hash / > root_hash
551 '
552
553 test_expect_success "root hash looks good $EXTRA" '
554 export EXP_ROOT_HASH="$ROOT_HASH" &&
555 echo $EXP_ROOT_HASH > root_hash_exp &&
556 test_cmp root_hash_exp root_hash
557 '
558
559 test_expect_success "/cats hash looks good $EXTRA" '
560 export EXP_CATS_HASH="$CATS_HASH" &&
561 echo $EXP_CATS_HASH > cats_hash_exp &&
562 ipfs files stat --hash /cats > cats_hash
563 test_cmp cats_hash_exp cats_hash
564 '
565
566 test_expect_success "flush root succeeds $EXTRA" '
567 ipfs files flush /
568 '
569
570 # test mv
571 test_expect_success "can mv dir $EXTRA" '
572 ipfs files mv /cats/this/is /cats/
573 '
574
575 test_expect_success "can mv dir and dest dir is / $EXTRA" '
576 ipfs files mv /cats/is /
577 '
578
579 test_expect_success "can mv dir and dest dir path has no trailing slash $EXTRA" '
580 ipfs files mv /is /cats
581 '
582
583 test_expect_success "mv worked $EXTRA" '
584 verify_dir_contents /cats file1 ipfs this is walrus &&
585 verify_dir_contents /cats/this
586 '
587
588 test_expect_success "cleanup, remove 'cats' $EXTRA" '
589 ipfs files rm -r /cats
590 '
591
592 test_expect_success "cleanup looks good $EXTRA" '
593 verify_dir_contents /
594 '
595
596 # test truncating
597 test_expect_success "create a new file $EXTRA" '
598 echo "some content" | ipfs files write $ARGS $RAW_LEAVES --create /cats
599 '
600
601 test_expect_success "truncate and write over that file $EXTRA" '
602 echo "fish" | ipfs files write $ARGS $RAW_LEAVES --truncate /cats
603 '
604
605 test_expect_success "output looks good $EXTRA" '
606 ipfs files read /cats > file_out &&
607 echo "fish" > file_exp &&
608 test_cmp file_out file_exp
609 '
610
611 test_expect_success "file hash correct $EXTRA" '
612 echo $TRUNC_HASH > filehash_expected &&
613 ipfs files stat --hash /cats > filehash &&
614 test_cmp filehash_expected filehash
615 '
616
617 test_expect_success "cleanup $EXTRA" '
618 ipfs files rm /cats
619 '
620
621 # test flush flags
622 test_expect_success "mkdir --flush works $EXTRA" '
623 ipfs files mkdir $ARGS --flush --parents /flushed/deep
624 '
625
626 test_expect_success "mkdir --flush works a second time $EXTRA" '
627 ipfs files mkdir $ARGS --flush --parents /flushed/deep
628 '
629
630 test_expect_success "dir looks right $EXTRA" '
631 verify_dir_contents / flushed
632 '
633
634 test_expect_success "child dir looks right $EXTRA" '
635 verify_dir_contents /flushed deep
636 '
637
638 test_expect_success "cleanup $EXTRA" '
639 ipfs files rm -r /flushed
640 '
641
642 test_expect_success "child dir looks right $EXTRA" '
643 verify_dir_contents /
644 '
645
646 # test for https://github.com/ipfs/go-ipfs/issues/2654
647 test_expect_success "create and remove dir $EXTRA" '
648 ipfs files mkdir $ARGS /test_dir &&
649 ipfs files rm -r "/test_dir"
650 '
651
652 test_expect_success "create test file $EXTRA" '
653 echo "content" | ipfs files write $ARGS $RAW_LEAVES -e "/test_file"
654 '
655
656 test_expect_success "copy test file onto test dir $EXTRA" '
657 ipfs files cp "/test_file" "/test_dir"
658 '
659
660 test_expect_success "test /test_dir $EXTRA" '
661 ipfs files stat "/test_dir" | grep -q "^Type: file"
662 '
663
664 test_expect_success "clean up /test_dir and /test_file $EXTRA" '
665 ipfs files rm -r /test_dir &&
666 ipfs files rm -r /test_file
667 '
668
669 test_expect_success "make a directory and a file $EXTRA" '
670 ipfs files mkdir $ARGS /adir &&
671 echo "blah" | ipfs files write $ARGS $RAW_LEAVES --create /foobar
672 '
673
674 test_expect_success "copy a file into a directory $EXTRA" '
675 ipfs files cp /foobar /adir/
676 '
677
678 test_expect_success "file made it into directory $EXTRA" '
679 ipfs files ls /adir | grep foobar
680 '
681
682 test_expect_success "test copy --force overwrites files" '
683 ipfs files cp /ipfs/$FILE1 /file1 &&
684 ipfs files cp /ipfs/$FILE2 /file2 &&
685 ipfs files cp --force /file1 /file2 &&
686 test "`ipfs files read /file1`" = "`ipfs files read /file2`"
687 '
688
689 test_expect_success "clean up" '
690 ipfs files rm /file1 &&
691 ipfs files rm /file2
692 '
693
694 test_expect_success "should fail to write file and create intermediate directories with no --parents flag set $EXTRA" '
695 echo "ipfs rocks" | test_must_fail ipfs files write --create /parents/foo/ipfs.txt
696 '
697
698 test_expect_success "can write file and create intermediate directories $EXTRA" '
699 echo "ipfs rocks" | ipfs files write --create --parents /parents/foo/bar/baz/ipfs.txt &&
700 ipfs files stat "/parents/foo/bar/baz/ipfs.txt" | grep -q "^Type: file"
701 '
702
703 test_expect_success "can write file and create intermediate directories with short flags $EXTRA" '
704 echo "ipfs rocks" | ipfs files write -e -p /parents/foo/bar/baz/qux/quux/garply/ipfs.txt &&
705 ipfs files stat "/parents/foo/bar/baz/qux/quux/garply/ipfs.txt" | grep -q "^Type: file"
706 '
707
708 test_expect_success "can write another file in the same directory with -e -p $EXTRA" '
709 echo "ipfs rocks" | ipfs files write -e -p /parents/foo/bar/baz/qux/quux/garply/ipfs2.txt &&
710 ipfs files stat "/parents/foo/bar/baz/qux/quux/garply/ipfs2.txt" | grep -q "^Type: file"
711 '
712
713 test_expect_success "clean up $EXTRA" '
714 ipfs files rm -r /foobar /adir /parents
715 '
716
717 test_expect_success "root mfs entry is empty $EXTRA" '
718 verify_dir_contents /
719 '
720
721 test_expect_success "repo gc $EXTRA" '
722 ipfs repo gc
723 '
724
725 # test rm
726
727 test_expect_success "remove file forcibly" '
728 echo "hello world" | ipfs files write --create /forcibly &&
729 ipfs files rm --force /forcibly &&
730 verify_dir_contents /
731 '
732
733 test_expect_success "remove multiple files forcibly" '
734 echo "hello world" | ipfs files write --create /forcibly_one &&
735 echo "hello world" | ipfs files write --create /forcibly_two &&
736 ipfs files rm --force /forcibly_one /forcibly_two &&
737 verify_dir_contents /
738 '
739
740 test_expect_success "remove directory forcibly" '
741 ipfs files mkdir /forcibly-dir &&
742 ipfs files rm --force /forcibly-dir &&
743 verify_dir_contents /
744 '
745
746 test_expect_success "remove multiple directories forcibly" '
747 ipfs files mkdir /forcibly-dir-one &&
748 ipfs files mkdir /forcibly-dir-two &&
749 ipfs files rm --force /forcibly-dir-one /forcibly-dir-two &&
750 verify_dir_contents /
751 '
752
753 test_expect_success "remove multiple files" '
754 echo "hello world" | ipfs files write --create /file_one &&
755 echo "hello world" | ipfs files write --create /file_two &&
756 ipfs files rm /file_one /file_two
757 '
758
759 test_expect_success "remove multiple directories" '
760 ipfs files mkdir /forcibly-dir-one &&
761 ipfs files mkdir /forcibly-dir-two &&
762 ipfs files rm -r /forcibly-dir-one /forcibly-dir-two &&
763 verify_dir_contents /
764 '
765
766 test_expect_success "remove nonexistent path forcibly" '
767 ipfs files rm --force /nonexistent
768 '
769
770 test_expect_success "remove deeply nonexistent path forcibly" '
771 ipfs files rm --force /deeply/nonexistent
772 '
773
774 # This one should return code 1 but still remove the rest of the valid files.
775 test_expect_success "remove multiple files (with nonexistent one)" '
776 echo "hello world" | ipfs files write --create /file_one &&
777 echo "hello world" | ipfs files write --create /file_two &&
778 test_expect_code 1 ipfs files rm /file_one /nonexistent /file_two
779 verify_dir_contents /
780 '
781 }
782
783 # test with and without the daemon (EXTRA="with-daemon" and EXTRA="no-daemon"
784 # respectively).
785 # FIXME: Check if we are correctly using the "no-daemon" flag in these test
786 # combinations.
787 tests_for_files_api() {
788 local EXTRA
789 EXTRA=$1
790
791 test_expect_success "can create some files for testing ($EXTRA)" '
792 create_files
793 '
794 # default: CIDv0, dag-pb for all files (no raw-leaves)
795 ROOT_HASH=QmcwKfTMCT7AaeiD92hWjnZn9b6eh9NxnhfSzN5x2vnDpt
796 CATS_HASH=Qma88m8ErTGkZHbBWGqy1C7VmEmX8wwNDWNpGyCaNmEgwC
797 FILE_HASH=QmQdQt9qooenjeaNhiKHF3hBvmNteB4MQBtgu3jxgf9c7i
798 TRUNC_HASH=QmPVnT9gocPbqzN4G6SMp8vAPyzcjDbUJrNdKgzQquuDg4
799 test_files_api "($EXTRA)"
800
801 test_expect_success "can create some files for testing with raw-leaves ($EXTRA)" '
802 create_files --raw-leaves
803 '
804
805 # partial raw-leaves: initial files created with --raw-leaves, test ops without
806 if [ "$EXTRA" = "with-daemon" ]; then
807 ROOT_HASH=QmTpKiKcAj4sbeesN6vrs5w3QeVmd4QmGpxRL81hHut4dZ
808 CATS_HASH=QmPhPkmtUGGi8ySPHoPu1qbfryLJKKq1GYxpgLyyCruvGe
809 test_files_api "($EXTRA, partial raw-leaves)"
810 fi
811
812 # raw-leaves: single-block files become RawNode (CIDv1), dirs stay CIDv0
813 ROOT_HASH=QmTHzLiSouBHVTssS8xRzmfWGAvTGhPEjtPdB6pWMQdxJX
814 CATS_HASH=QmPJkzbCoBuL379TbHgwF1YbVHnKgiDa5bjqYhe6Lovdms
815 FILE_HASH=bafybeibkrazpbejqh3qun7xfnsl7yofl74o4jwhxebpmtrcpavebokuqtm
816 TRUNC_HASH=bafybeigwhb3q36yrm37jv5fo2ap6r6eyohckqrxmlejrenex4xlnuxiy3e
817 test_files_api "($EXTRA, raw-leaves)" '' --raw-leaves
818
819 # cidv1 for mkdir: different from raw-leaves since mkdir forces CIDv1 dirs
820 ROOT_HASH=QmTLdTaZNj8Mvq1cgYup59ZFJFv1KxptouFSZUZKeq7X3z
821 CATS_HASH=bafybeihsqinttigpskqqj63wgalrny3lifvqv5ml7igrirdhlcf73l3wvm
822 FILE_HASH=bafybeibkrazpbejqh3qun7xfnsl7yofl74o4jwhxebpmtrcpavebokuqtm
823 TRUNC_HASH=bafybeigwhb3q36yrm37jv5fo2ap6r6eyohckqrxmlejrenex4xlnuxiy3e
824 if [ "$EXTRA" = "with-daemon" ]; then
825 test_files_api "($EXTRA, cidv1)" --cid-version=1
826 fi
827
828 test_expect_success "chcid rejects root path" '
829 test_must_fail ipfs files chcid --cid-version=1 / 2>chcid_err &&
830 grep -q "Import.CidVersion" chcid_err
831 '
832
833 test_expect_success "chcid works on subdirectory" '
834 ipfs files mkdir /chcid-test &&
835 ipfs files chcid --hash=blake2b-256 /chcid-test &&
836 ipfs files stat --hash /chcid-test > chcid_hash &&
837 ipfs cid format -f "%h" $(cat chcid_hash) > chcid_hashfn &&
838 echo blake2b-256 > chcid_hashfn_expect &&
839 test_cmp chcid_hashfn_expect chcid_hashfn &&
840 ipfs files rm -r /chcid-test
841 '
842
843 # MFS root CID format is controlled by Import config, not chcid
844 test_expect_success "set Import.CidVersion=1 for cidv1 root" '
845 ipfs config --json Import.CidVersion 1
846 '
847 if [ "$EXTRA" = "with-daemon" ]; then
848 restart_daemon
849 fi
850
851 test_expect_success "root hash is cidv1 after Import config change" '
852 echo bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354 > hash_expect &&
853 ipfs files stat --hash / > hash_actual &&
854 test_cmp hash_expect hash_actual
855 '
856
857 # cidv1 root: root set to CIDv1 via Import config, all new dirs/files also CIDv1
858 ROOT_HASH=bafybeickjecu37qv6ue54ofk3n4rpm4g4abuofz7yc4qn4skffy263kkou
859 CATS_HASH=bafybeihsqinttigpskqqj63wgalrny3lifvqv5ml7igrirdhlcf73l3wvm
860 test_files_api "($EXTRA, cidv1 root)"
861
862 if [ "$EXTRA" = "with-daemon" ]; then
863 test_expect_success "set Import.HashFunction=blake2b-256" '
864 ipfs config Import.HashFunction blake2b-256
865 '
866 restart_daemon
867
868 test_expect_success "root hash is blake2b-256 after Import config change" '
869 echo bafykbzacebugfutjir6qie7apo5shpry32ruwfi762uytd5g3u2gk7tpscndq > hash_expect &&
870 ipfs files stat --hash / > hash_actual &&
871 test_cmp hash_expect hash_actual
872 '
873 # blake2b-256 root: using blake2b-256 hash instead of sha2-256
874 ROOT_HASH=bafykbzaceaebvwrjdw5rfhqqh5miaq3g42yybnrw3kxxxx43ggyttm6xn2zek
875 CATS_HASH=bafykbzaceaqvpxs3dfl7su6744jgyvifbusow2tfixdy646chasdwyz2boagc
876 FILE_HASH=bafykbzaceca45w2i3o3q3ctqsezdv5koakz7sxsw37ygqjg4w54m2bshzevxy
877 TRUNC_HASH=bafykbzaceadeu7onzmlq7v33ytjpmo37rsqk2q6mzeqf5at55j32zxbcdbwig
878 test_files_api "($EXTRA, blake2b-256 root)"
879
880 # Reset Import.HashFunction back to default
881 test_expect_success "reset Import.HashFunction to default" '
882 ipfs config --json Import.HashFunction null
883 '
884 fi
885
886 # Reset Import.CidVersion back to CIDv0
887 test_expect_success "reset Import.CidVersion to cidv0" '
888 ipfs config --json Import.CidVersion 0
889 '
890 if [ "$EXTRA" = "with-daemon" ]; then
891 restart_daemon
892 fi
893
894 test_expect_success "root hash is cidv0 after Import config reset" '
895 echo QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn > hash_expect &&
896 ipfs files stat --hash / > hash_actual &&
897 test_cmp hash_expect hash_actual
898 '
899 }
900
901 tests_for_files_api "no-daemon"
902
903 test_launch_ipfs_daemon_without_network
904
905 WITH_DAEMON=1
906 # FIXME: Used only on a specific test inside `test_files_api` but we should instead
907 # propagate the `"with-daemon"` argument in its caller `tests_for_files_api`.
908
909 tests_for_files_api "with-daemon"
910
911 test_kill_ipfs_daemon
912
913 test_expect_success "enable sharding in config" '
914 ipfs config --json Import.UnixFSHAMTDirectorySizeThreshold "\"1B\""
915 '
916
917 test_launch_ipfs_daemon_without_network
918
919 # sharding cidv0: HAMT-sharded directory with 100 files, CIDv0
920 SHARD_HASH=QmPkwLJTYZRGPJ8Lazr9qPdrLmswPtUjaDbEpmR9jEh1se
921 test_sharding "(cidv0)"
922
923 # sharding cidv1: HAMT-sharded directory with 100 files, CIDv1
924 SHARD_HASH=bafybeibu4i76qi26jhpgskqhivuactsvdsia44swpi7eaw45r7c3c3lhs4
925 test_sharding "(cidv1 root)" "--cid-version=1"
926
927 test_kill_ipfs_daemon
928
929 # Test automatic sharding and unsharding
930
931 # We shard based on size with a threshold of 256 KiB (see config file docs)
932 # above which directories are sharded.
933 #
934 # The directory size is estimated as the size of each link. Links are roughly
935 # the entry name + the CID byte length (e.g. 34 bytes for a CIDv0). So for
936 # entries of length 10 we need 256 KiB / (34 + 10) ~ 6000 entries in the
937 # directory to trigger sharding.
938 test_expect_success "set up automatic sharding/unsharding data" '
939 mkdir big_dir
940 for i in `seq 5960` # Just above the number of entries that trigger sharding for 256KiB
941 do
942 echo $i > big_dir/`printf "file%06d" $i` # fixed length of 10 chars
943 done
944 '
945
946 test_expect_success "reset automatic sharding" '
947 ipfs config --json Import.UnixFSHAMTDirectorySizeThreshold null
948 '
949
950 test_launch_ipfs_daemon_without_network
951
952 LARGE_SHARDED="QmWfjnRWRvdvYezQWnfbvrvY7JjrpevsE9cato1x76UqGr"
953 LARGE_MINUS_5_UNSHARDED="QmbVxi5zDdzytrjdufUejM92JsWj8wGVmukk6tiPce3p1m"
954
955 test_add_large_sharded_dir() {
956 exphash="$1"
957 test_expect_success "ipfs add on directory succeeds" '
958 ipfs add -r -Q big_dir > shardbigdir_out &&
959 echo "$exphash" > shardbigdir_exp &&
960 test_cmp shardbigdir_exp shardbigdir_out
961 '
962
963 test_expect_success "can access a path under the dir" '
964 ipfs cat "$exphash/file000030" > file30_out &&
965 test_cmp big_dir/file000030 file30_out
966 '
967 }
968
969 test_add_large_sharded_dir "$LARGE_SHARDED"
970
971 test_expect_success "remove a few entries from big_dir/ to trigger unsharding" '
972 ipfs files cp /ipfs/"$LARGE_SHARDED" /big_dir &&
973 for i in `seq 5`
974 do
975 ipfs files rm /big_dir/`printf "file%06d" $i`
976 done &&
977 ipfs files stat --hash /big_dir > unshard_dir_hash &&
978 echo "$LARGE_MINUS_5_UNSHARDED" > unshard_exp &&
979 test_cmp unshard_exp unshard_dir_hash
980 '
981
982 test_expect_success "add a few entries to big_dir/ to retrigger sharding" '
983 for i in `seq 5`
984 do
985 ipfs files cp /ipfs/"$LARGE_SHARDED"/`printf "file%06d" $i` /big_dir/`printf "file%06d" $i`
986 done &&
987 ipfs files stat --hash /big_dir > shard_dir_hash &&
988 echo "$LARGE_SHARDED" > shard_exp &&
989 test_cmp shard_exp shard_dir_hash
990 '
991
992 test_kill_ipfs_daemon
993
994 test_done