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

test_description="test the unix files api"

. lib/test-lib.sh

test_init_ipfs

Kevin Atkinson's avatar
Kevin Atkinson committed
13 14 15 16 17
create_files() {
	FILE1=$(echo foo | ipfs add "$@" -q) &&
	FILE2=$(echo bar | ipfs add "$@" -q) &&
	FILE3=$(echo baz | ipfs add "$@" -q) &&
	mkdir -p stuff_test &&
Jeromy's avatar
Jeromy committed
18 19 20
	echo cats > stuff_test/a &&
	echo dogs > stuff_test/b &&
	echo giraffes > stuff_test/c &&
Kevin Atkinson's avatar
Kevin Atkinson committed
21 22
	DIR1=$(ipfs add -r "$@" -q stuff_test | tail -n1)
}
Jeromy's avatar
Jeromy committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

verify_path_exists() {
	# simply running ls on a file should be a good 'check'
	ipfs files ls $1
}

verify_dir_contents() {
	dir=$1
	shift
	rm -f expected
	touch expected
	for e in $@
	do
		echo $e >> expected
	done

	test_expect_success "can list dir" '
		ipfs files ls $dir > output
	'

	test_expect_success "dir entries look good" '
		test_sort_cmp output expected
	'
}

48 49 50 51 52
test_sharding() {
	test_expect_success "make a directory" '
		ipfs files mkdir /foo
	'

Jeromy's avatar
Jeromy committed
53
	test_expect_success "can make 100 files in a directory" '
54
		printf "" > list_exp_raw
Jeromy's avatar
Jeromy committed
55
		for i in `seq 100`
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
		do
			echo $i | ipfs files write --create /foo/file$i
			echo file$i >> list_exp_raw
		done
	'

	test_expect_success "listing works" '
		ipfs files ls /foo |sort > list_out &&
		sort list_exp_raw > list_exp &&
		test_cmp list_exp list_out
	'

	test_expect_success "can read a file from sharded directory" '
		ipfs files read /foo/file65 > file_out &&
		echo "65" > file_exp &&
		test_cmp file_out file_exp
	'
Jeromy's avatar
Jeromy committed
73

74 75 76 77 78 79 80 81 82 83
	test_expect_success "can pin a file from sharded directory" '
		ipfs files stat --hash /foo/file42 > pin_file_hash &&
		ipfs pin add < pin_file_hash > pin_hash
	'

	test_expect_success "can unpin a file from sharded directory" '
		read -r _ HASH _ < pin_hash &&
		ipfs pin rm $HASH
	'

Jeromy's avatar
Jeromy committed
84 85 86 87 88
	test_expect_success "output object was really sharded" '
		ipfs files stat --hash /foo > expected_foo_hash &&
		echo QmPkwLJTYZRGPJ8Lazr9qPdrLmswPtUjaDbEpmR9jEh1se > actual_foo_hash &&
		test_cmp expected_foo_hash actual_foo_hash
	'
89 90
}

Jeromy's avatar
Jeromy committed
91
test_files_api() {
Kevin Atkinson's avatar
Kevin Atkinson committed
92 93
	ROOT_HASH=$1

Jeromy's avatar
Jeromy committed
94 95 96 97
	test_expect_success "can mkdir in root" '
		ipfs files mkdir /cats
	'

98 99 100 101 102 103
	test_expect_success "'files ls' lists root by default" '
		ipfs files ls >actual &&
		echo "cats" >expected &&
		test_cmp expected actual
	'

Jeromy's avatar
Jeromy committed
104 105 106 107 108 109 110
	test_expect_success "directory was created" '
		verify_path_exists /cats
	'

	test_expect_success "directory is empty" '
		verify_dir_contents /cats
	'
111 112 113 114 115 116 117 118 119 120 121 122
	# we do verification of stat formatting now as we depend on it

	test_expect_success "stat works" '
		ipfs files stat / >stat
	'

	test_expect_success "hash is first line of stat" '
		ipfs ls $(head -1 stat) | grep "cats"
	'

	test_expect_success "stat --hash gives only hash" '
		ipfs files stat --hash / >actual &&
123
		head -n1 stat >expected &&
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
		test_cmp expected actual
	'

	test_expect_success "stat with multiple format options should fail" '
		test_must_fail ipfs files stat --hash --size /
	'

	test_expect_success "compare hash option with format" '
		ipfs files stat --hash / >expected &&
		ipfs files stat --format='"'"'<hash>'"'"' / >actual &&
		test_cmp expected actual
	'
	test_expect_success "compare size option with format" '
		ipfs files stat --size / >expected &&
		ipfs files stat --format='"'"'<cumulsize>'"'"' / >actual &&
		test_cmp expected actual
	'
Jeromy's avatar
Jeromy committed
141

Jeromy's avatar
Jeromy committed
142
	test_expect_success "check root hash" '
143
		ipfs files stat --hash / > roothash
Jeromy's avatar
Jeromy committed
144 145 146 147 148 149 150
	'

	test_expect_success "cannot mkdir /" '
		test_expect_code 1 ipfs files mkdir /
	'

	test_expect_success "check root hash was not changed" '
151
		ipfs files stat --hash / > roothashafter &&
Jeromy's avatar
Jeromy committed
152 153 154
		test_cmp roothash roothashafter
	'

Jeromy's avatar
Jeromy committed
155 156 157 158 159 160 161 162
	test_expect_success "can put files into directory" '
		ipfs files cp /ipfs/$FILE1 /cats/file1
	'

	test_expect_success "file shows up in directory" '
		verify_dir_contents /cats file1
	'

Kevin Atkinson's avatar
Kevin Atkinson committed
163 164 165 166 167 168
	test_expect_success "file has correct hash and size in directory" '
		echo "file1	$FILE1	4" > ls_l_expected &&
		ipfs files ls -l /cats > ls_l_actual &&
		test_cmp ls_l_expected ls_l_actual
	'

Jeromy's avatar
Jeromy committed
169 170 171 172 173 174
	test_expect_success "can read file" '
		ipfs files read /cats/file1 > file1out
	'

	test_expect_success "output looks good" '
		echo foo > expected &&
Jeromy's avatar
Jeromy committed
175
		test_cmp expected file1out
Jeromy's avatar
Jeromy committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
	'

	test_expect_success "can put another file into root" '
		ipfs files cp /ipfs/$FILE2 /file2
	'

	test_expect_success "file shows up in root" '
		verify_dir_contents / file2 cats
	'

	test_expect_success "can read file" '
		ipfs files read /file2 > file2out
	'

	test_expect_success "output looks good" '
		echo bar > expected &&
Jeromy's avatar
Jeromy committed
192
		test_cmp expected file2out
Jeromy's avatar
Jeromy committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
	'

	test_expect_success "can make deep directory" '
		ipfs files mkdir -p /cats/this/is/a/dir
	'

	test_expect_success "directory was created correctly" '
		verify_path_exists /cats/this/is/a/dir &&
		verify_dir_contents /cats this file1 &&
		verify_dir_contents /cats/this is &&
		verify_dir_contents /cats/this/is a &&
		verify_dir_contents /cats/this/is/a dir &&
		verify_dir_contents /cats/this/is/a/dir
	'

	test_expect_success "can copy file into new dir" '
		ipfs files cp /ipfs/$FILE3 /cats/this/is/a/dir/file3
	'

	test_expect_success "can read file" '
		ipfs files read /cats/this/is/a/dir/file3 > output
	'

	test_expect_success "output looks good" '
		echo baz > expected &&
Jeromy's avatar
Jeromy committed
218
		test_cmp expected output
Jeromy's avatar
Jeromy committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
	'

	test_expect_success "file shows up in dir" '
		verify_dir_contents /cats/this/is/a/dir file3
	'

	test_expect_success "can remove file" '
		ipfs files rm /cats/this/is/a/dir/file3
	'

	test_expect_success "file no longer appears" '
		verify_dir_contents /cats/this/is/a/dir
	'

	test_expect_success "can remove dir" '
		ipfs files rm -r /cats/this/is/a/dir
	'

	test_expect_success "dir no longer appears" '
		verify_dir_contents /cats/this/is/a
	'

	test_expect_success "can remove file from root" '
		ipfs files rm /file2
	'

	test_expect_success "file no longer appears" '
		verify_dir_contents / cats
	'

Jeromy's avatar
Jeromy committed
249
	test_expect_success "check root hash" '
250
		ipfs files stat --hash / > roothash
Jeromy's avatar
Jeromy committed
251 252 253 254 255 256 257
	'

	test_expect_success "cannot remove root" '
		test_expect_code 1 ipfs files rm -r /
	'

	test_expect_success "check root hash was not changed" '
258
		ipfs files stat --hash / > roothashafter &&
Jeromy's avatar
Jeromy committed
259 260 261
		test_cmp roothash roothashafter
	'

Jeromy's avatar
Jeromy committed
262 263 264 265 266 267 268 269
	# test read options

	test_expect_success "read from offset works" '
		ipfs files read -o 1 /cats/file1 > output
	'

	test_expect_success "output looks good" '
		echo oo > expected &&
Jeromy's avatar
Jeromy committed
270
		test_cmp expected output
Jeromy's avatar
Jeromy committed
271 272 273 274 275 276 277 278
	'

	test_expect_success "read with size works" '
		ipfs files read -n 2 /cats/file1 > output
	'

	test_expect_success "output looks good" '
		printf fo > expected &&
Jeromy's avatar
Jeromy committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
		test_cmp expected output
	'

	test_expect_success "cannot read from negative offset" '
		test_expect_code 1 ipfs files read --offset -3 /cats/file1
	'

	test_expect_success "read from offset 0 works" '
		ipfs files read --offset 0 /cats/file1 > output
	'

	test_expect_success "output looks good" '
		echo foo > expected &&
		test_cmp expected output
	'

	test_expect_success "read last byte works" '
		ipfs files read --offset 2 /cats/file1 > output
	'

	test_expect_success "output looks good" '
		echo o > expected &&
		test_cmp expected output
	'

	test_expect_success "offset past end of file fails" '
		test_expect_code 1 ipfs files read --offset 5 /cats/file1
	'

	test_expect_success "cannot read negative count bytes" '
		test_expect_code 1 ipfs read --count -1 /cats/file1
	'

	test_expect_success "reading zero bytes prints nothing" '
		ipfs files read --count 0 /cats/file1 > output
	'

	test_expect_success "output looks good" '
		printf "" > expected &&
		test_cmp expected output
	'

	test_expect_success "count > len(file) prints entire file" '
		ipfs files read --count 200 /cats/file1 > output
	'

	test_expect_success "output looks good" '
		echo foo > expected &&
		test_cmp expected output
Jeromy's avatar
Jeromy committed
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
	'

	# test write

	test_expect_success "can write file" '
		echo "ipfs rocks" > tmpfile &&
		cat tmpfile | ipfs files write --create /cats/ipfs
	'

	test_expect_success "file was created" '
		verify_dir_contents /cats ipfs file1 this
	'

	test_expect_success "can read file we just wrote" '
		ipfs files read /cats/ipfs > output
	'

	test_expect_success "can write to offset" '
		echo "is super cool" | ipfs files write -o 5 /cats/ipfs
	'

	test_expect_success "file looks correct" '
		echo "ipfs is super cool" > expected &&
		ipfs files read /cats/ipfs > output &&
Jeromy's avatar
Jeromy committed
352 353 354 355
		test_cmp expected output
	'

	test_expect_success "cant write to negative offset" '
356
		ipfs files stat --hash /cats/ipfs > filehash &&
Jeromy's avatar
Jeromy committed
357 358 359 360
		test_expect_code 1 ipfs files write --offset -1 /cats/ipfs < output
	'

	test_expect_success "verify file was not changed" '
361
		ipfs files stat --hash /cats/ipfs > afterhash &&
Jeromy's avatar
Jeromy committed
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
		test_cmp filehash afterhash
	'

	test_expect_success "write new file for testing" '
		echo foobar | ipfs files write --create /fun
	'

	test_expect_success "write to offset past end works" '
		echo blah | ipfs files write --offset 50 /fun
	'

	test_expect_success "can read file" '
		ipfs files read /fun > sparse_output
	'

	test_expect_success "output looks good" '
		echo foobar > sparse_expected &&
		echo blah | dd of=sparse_expected bs=50 seek=1 &&
		test_cmp sparse_expected sparse_output
	'

	test_expect_success "cleanup" '
		ipfs files rm /fun
	'

	test_expect_success "cannot write to directory" '
388
		ipfs files stat --hash /cats > dirhash &&
Jeromy's avatar
Jeromy committed
389 390 391 392
		test_expect_code 1 ipfs files write /cats < output
	'

	test_expect_success "verify dir was not changed" '
393
		ipfs files stat --hash /cats > afterdirhash &&
Jeromy's avatar
Jeromy committed
394 395 396 397 398 399 400 401 402
		test_cmp dirhash afterdirhash
	'

	test_expect_success "cannot write to nonexistant path" '
		test_expect_code 1 ipfs files write /cats/bar/ < output
	'

	test_expect_success "no new paths were created" '
		verify_dir_contents /cats file1 ipfs this
Jeromy's avatar
Jeromy committed
403 404
	'

405
	test_expect_success "write 'no-flush' succeeds" '
Jeromy's avatar
Jeromy committed
406 407 408 409 410 411
		echo "testing" | ipfs files write -f=false -e /cats/walrus
	'

	test_expect_success "root hash not bubbled up yet" '
		test -z "$ONLINE" ||
		(ipfs refs local > refsout &&
Kevin Atkinson's avatar
Kevin Atkinson committed
412
		test_expect_code 1 grep $ROOT_HASH refsout)
413 414 415
	'

	test_expect_success "changes bubbled up to root on inspection" '
416
		ipfs files stat --hash / > root_hash
417 418 419
	'

	test_expect_success "root hash looks good" '
Kevin Atkinson's avatar
Kevin Atkinson committed
420
		export EXP_ROOT_HASH="$ROOT_HASH" &&
Jeromy's avatar
Jeromy committed
421
		echo $EXP_ROOT_HASH > root_hash_exp &&
422 423 424
		test_cmp root_hash_exp root_hash
	'

Jeromy's avatar
Jeromy committed
425 426 427 428
	test_expect_success "flush root succeeds" '
		ipfs files flush /
	'

Jeromy's avatar
Jeromy committed
429 430 431 432 433 434
	# test mv
	test_expect_success "can mv dir" '
		ipfs files mv /cats/this/is /cats/
	'

	test_expect_success "mv worked" '
435
		verify_dir_contents /cats file1 ipfs this is walrus &&
Jeromy's avatar
Jeromy committed
436 437 438 439 440 441 442 443 444 445
		verify_dir_contents /cats/this
	'

	test_expect_success "cleanup, remove 'cats'" '
		ipfs files rm -r /cats
	'

	test_expect_success "cleanup looks good" '
		verify_dir_contents /
	'
Jeromy's avatar
Jeromy committed
446

Jeromy's avatar
Jeromy committed
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
	# test truncating
	test_expect_success "create a new file" '
		echo "some content" | ipfs files write --create /cats
	'

	test_expect_success "truncate and write over that file" '
		echo "fish" | ipfs files write --truncate /cats
	'

	test_expect_success "output looks good" '
		ipfs files read /cats > file_out &&
		echo "fish" > file_exp &&
		test_cmp file_out file_exp
	'

	test_expect_success "cleanup" '
		ipfs files rm /cats
	'

Jeromy's avatar
Jeromy committed
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
	# test flush flags
	test_expect_success "mkdir --flush works" '
		ipfs files mkdir --flush --parents /flushed/deep
	'

	test_expect_success "mkdir --flush works a second time" '
		ipfs files mkdir --flush --parents /flushed/deep
	'

	test_expect_success "dir looks right" '
		verify_dir_contents / flushed
	'

	test_expect_success "child dir looks right" '
		verify_dir_contents /flushed deep
	'

	test_expect_success "cleanup" '
		ipfs files rm -r /flushed
	'

	test_expect_success "child dir looks right" '
		verify_dir_contents /
	'
Jeromy's avatar
Jeromy committed
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512

	# test for https://github.com/ipfs/go-ipfs/issues/2654
	test_expect_success "create and remove dir" '
		ipfs files mkdir /test_dir &&
		ipfs files rm -r "/test_dir"
	'

	test_expect_success "create test file" '
		echo "content" | ipfs files write -e "/test_file"
	'

	test_expect_success "copy test file onto test dir" '
		ipfs files cp "/test_file" "/test_dir"
	'

	test_expect_success "test /test_dir" '
		ipfs files stat "/test_dir" | grep -q "^Type: file"
	'

	test_expect_success "clean up /test_dir and /test_file" '
		ipfs files rm -r /test_dir &&
		ipfs files rm -r /test_file
	'
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530

	test_expect_success "make a directory and a file" '
		ipfs files mkdir /adir &&
		echo "blah" | ipfs files write --create /foobar
	'

	test_expect_success "copy a file into a directory" '
		ipfs files cp /foobar /adir/
	'

	test_expect_success "file made it into directory" '
		ipfs files ls /adir | grep foobar
	'

	test_expect_success "clean up" '
		ipfs files rm -r /foobar &&
		ipfs files rm -r /adir
	'
Kevin Atkinson's avatar
Kevin Atkinson committed
531

Jeromy's avatar
Jeromy committed
532
	test_expect_success "root mfs entry is empty" '
Kevin Atkinson's avatar
Kevin Atkinson committed
533 534 535 536 537 538
		verify_dir_contents /
	'

        test_expect_success "repo gc" '
		ipfs repo gc
	'
Jeromy's avatar
Jeromy committed
539 540 541
}

# test offline and online
Kevin Atkinson's avatar
Kevin Atkinson committed
542 543 544 545
test_expect_success "can create some files for testing" '
	create_files
'
test_files_api QmcwKfTMCT7AaeiD92hWjnZn9b6eh9NxnhfSzN5x2vnDpt
Jeromy's avatar
Jeromy committed
546

Kevin Atkinson's avatar
Kevin Atkinson committed
547 548
test_expect_success "can create some files for testing with raw-leaves" '
	create_files --raw-leaves
Jeromy's avatar
Jeromy committed
549
'
Kevin Atkinson's avatar
Kevin Atkinson committed
550
test_files_api QmTpKiKcAj4sbeesN6vrs5w3QeVmd4QmGpxRL81hHut4dZ
Jeromy's avatar
Jeromy committed
551

Kevin Atkinson's avatar
Kevin Atkinson committed
552
test_launch_ipfs_daemon --offline
Jeromy's avatar
Jeromy committed
553 554

ONLINE=1 # set online flag so tests can easily tell
Kevin Atkinson's avatar
Kevin Atkinson committed
555 556 557 558 559 560 561 562 563 564 565
test_expect_success "can create some files for testing" '
	create_files
'
test_files_api QmcwKfTMCT7AaeiD92hWjnZn9b6eh9NxnhfSzN5x2vnDpt

test_expect_success "can create some files for testing with raw-leaves" '
	create_files --raw-leaves
'
test_files_api QmTpKiKcAj4sbeesN6vrs5w3QeVmd4QmGpxRL81hHut4dZ

test_kill_ipfs_daemon --offline
Jeromy's avatar
Jeromy committed
566 567 568 569 570

test_expect_success "enable sharding in config" '
	ipfs config --json Experimental.ShardingEnabled true
'

Kevin Atkinson's avatar
Kevin Atkinson committed
571
test_launch_ipfs_daemon --offline
572
test_sharding
Jeromy's avatar
Jeromy committed
573
test_kill_ipfs_daemon
Jeromy's avatar
Jeromy committed
574

Jeromy's avatar
Jeromy committed
575
test_done