t0051-object.sh 7.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#!/bin/sh
#
# Copyright (c) 2015 Henry Bubert
# MIT Licensed; see the LICENSE file in this repository.
#

test_description="Test object command"

. lib/test-lib.sh

test_init_ipfs

13 14 15 16 17 18 19 20 21 22
test_patch_create_path() {
	root=$1
	name=$2
	target=$3

	test_expect_success "object patch --create works" '
		PCOUT=$(ipfs object patch --create $root add-link $name $target)
	'

	test_expect_success "output looks good" '
23 24 25
		ipfs cat "$PCOUT/$name" >tpcp_out &&
		ipfs cat "$target" >tpcp_exp &&
		test_cmp tpcp_exp tpcp_out
26 27 28
	'
}

29 30 31 32 33 34
test_object_cmd() {

	test_expect_success "'ipfs add testData' succeeds" '
		printf "Hello Mars" >expected_in &&
		ipfs add expected_in >actual_Addout
	'
35

36 37 38 39 40
	test_expect_success "'ipfs add testData' output looks good" '
		HASH="QmWkHFpYBZ9mpPRreRbMhhYWXfUhBAue3JkbbpFqwowSRb" &&
		echo "added $HASH expected_in" >expected_Addout &&
		test_cmp expected_Addout actual_Addout
	'
41

42 43 44
	test_expect_success "'ipfs object get' succeeds" '
		ipfs object get $HASH >actual_getOut
	'
45

46 47 48
	test_expect_success "'ipfs object get' output looks good" '
		test_cmp ../t0051-object-data/expected_getOut actual_getOut
	'
49

50 51 52
	test_expect_success "'ipfs object stat' succeeds" '
		ipfs object stat $HASH >actual_stat
	'
53

54 55 56 57 58 59 60 61
	test_expect_success "'ipfs object get' output looks good" '
		echo "NumLinks: 0" > expected_stat &&
		echo "BlockSize: 18" >> expected_stat &&
		echo "LinksSize: 2" >> expected_stat &&
		echo "DataSize: 16" >> expected_stat &&
		echo "CumulativeSize: 18" >> expected_stat &&
		test_cmp expected_stat actual_stat
	'
Christian Couder's avatar
Christian Couder committed
62

63 64 65
	test_expect_success "'ipfs object put file.json' succeeds" '
		ipfs object put  ../t0051-object-data/testPut.json > actual_putOut
	'
66

67 68 69 70 71
	test_expect_success "'ipfs object put file.json' output looks good" '
		HASH="QmUTSAdDi2xsNkDtLqjFgQDMEn5di3Ab9eqbrt4gaiNbUD" &&
		printf "added $HASH" > expected_putOut &&
		test_cmp expected_putOut actual_putOut
	'
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

	test_expect_success "'ipfs object put file.xml' succeeds" '
		ipfs object put  ../t0051-object-data/testPut.xml --inputenc=xml > actual_putOut
	'

	test_expect_success "'ipfs object put file.xml' output looks good" '
		HASH="QmQzNKUHy4HyEUGkqKe3q3t796ffPLQXYCkHCcXUNT5JNK" &&
		printf "added $HASH" > expected_putOut &&
		test_cmp expected_putOut actual_putOut
	'

	test_expect_success "'ipfs object put' from stdin succeeds" '
		cat ../t0051-object-data/testPut.xml | ipfs object put --inputenc=xml > actual_putStdinOut
	'

	test_expect_success "'ipfs object put broken.xml' should fail" '
		test_expect_code 1 ipfs object put ../t0051-object-data/brokenPut.xml --inputenc=xml 2>actual_putBrokenErr >actual_putBroken
	'

	test_expect_success "'ipfs object put broken.hxml' output looks good" '
		touch expected_putBroken &&
		printf "Error: no data or links in this node\n" > expected_putBrokenErr &&
		test_cmp expected_putBroken actual_putBroken &&
		test_cmp expected_putBrokenErr actual_putBrokenErr
	'
	test_expect_success "'ipfs object get --enc=xml' succeeds" '
		ipfs object get --enc=xml $HASH >utf8_xml
	'

	test_expect_success "'ipfs object put --inputenc=xml' succeeds" '
		ipfs object put --inputenc=xml <utf8_xml >actual
	'

	test_expect_failure "'ipfs object put --inputenc=xml' output looks good" '
		echo "added $HASH" >expected &&
		test_cmp expected actual
	'

110 111 112
	test_expect_success "'ipfs object put file.pb' succeeds" '
		ipfs object put --inputenc=protobuf ../t0051-object-data/testPut.pb > actual_putOut
	'
113

114 115 116 117 118
	test_expect_success "'ipfs object put file.pb' output looks good" '
		HASH="QmUTSAdDi2xsNkDtLqjFgQDMEn5di3Ab9eqbrt4gaiNbUD" &&
		printf "added $HASH" > expected_putOut &&
		test_cmp expected_putOut actual_putOut
	'
119

120 121 122
	test_expect_success "'ipfs object put' from stdin succeeds" '
		cat ../t0051-object-data/testPut.json | ipfs object put > actual_putStdinOut
	'
123

124 125 126 127 128
	test_expect_success "'ipfs object put' from stdin output looks good" '
		HASH="QmUTSAdDi2xsNkDtLqjFgQDMEn5di3Ab9eqbrt4gaiNbUD" &&
		printf "added $HASH" > expected_putStdinOut &&
		test_cmp expected_putStdinOut actual_putStdinOut
	'
129

130 131 132
	test_expect_success "'ipfs object put' from stdin (pb) succeeds" '
		cat ../t0051-object-data/testPut.pb | ipfs object put --inputenc=protobuf > actual_putPbStdinOut
	'
133

134 135 136 137 138
	test_expect_success "'ipfs object put' from stdin (pb) output looks good" '
		HASH="QmUTSAdDi2xsNkDtLqjFgQDMEn5di3Ab9eqbrt4gaiNbUD" &&
		printf "added $HASH" > expected_putStdinOut &&
		test_cmp expected_putStdinOut actual_putPbStdinOut
	'
139

140 141 142
	test_expect_success "'ipfs object put broken.json' should fail" '
		test_expect_code 1 ipfs object put ../t0051-object-data/brokenPut.json 2>actual_putBrokenErr >actual_putBroken
	'
143

144 145 146 147 148 149
	test_expect_success "'ipfs object put broken.hjson' output looks good" '
		touch expected_putBroken &&
		printf "Error: no data or links in this node\n" > expected_putBrokenErr &&
		test_cmp expected_putBroken actual_putBroken &&
		test_cmp expected_putBrokenErr actual_putBrokenErr
	'
150

Christian Couder's avatar
Christian Couder committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	test_expect_success "setup: add UTF-8 test file" '
		HASH="QmNY5sQeH9ttVCg24sizH71dNbcZTpGd7Yb3YwsKZ4jiFP" &&
		ipfs add ../t0051-object-data/UTF-8-test.txt >actual &&
		echo "added $HASH UTF-8-test.txt" >expected &&
		test_cmp expected actual
	'

	test_expect_success "'ipfs object get --enc=json' succeeds" '
		ipfs object get --enc=json $HASH >utf8_json
	'

	test_expect_success "'ipfs object put --inputenc=json' succeeds" '
		ipfs object put --inputenc=json <utf8_json >actual
	'

	test_expect_failure "'ipfs object put --inputenc=json' output looks good" '
		echo "added $HASH" >expected &&
		test_cmp expected actual
	'

171 172 173 174 175
	test_expect_success "'ipfs object patch' should work" '
		EMPTY_DIR=$(ipfs object new unixfs-dir) &&
		OUTPUT=$(ipfs object patch $EMPTY_DIR add-link foo $EMPTY_DIR)
	'

Jeromy's avatar
Jeromy committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189
	test_expect_success "multilayer ipfs patch works" '
		echo "hello world" > hwfile &&
		FILE=$(ipfs add -q hwfile) &&
		EMPTY=$(ipfs object new unixfs-dir) &&
		ONE=$(ipfs object patch $EMPTY add-link b $EMPTY) &&
		TWO=$(ipfs object patch $EMPTY add-link a $ONE) &&
		ipfs object patch $TWO add-link a/b/c $FILE > multi_patch
	'

	test_expect_success "output looks good" '
		ipfs cat $(cat multi_patch)/a/b/c > hwfile_out &&
		test_cmp hwfile hwfile_out
	'

190
	test_expect_success "ipfs object stat path succeeds" '
Jeromy's avatar
Jeromy committed
191
		ipfs object stat $(cat multi_patch)/a > obj_stat_out
192 193 194
	'

	test_expect_success "ipfs object stat output looks good" '
Jeromy's avatar
Jeromy committed
195 196 197 198 199 200
		echo NumLinks: 1 > obj_stat_exp &&
		echo BlockSize: 47 >> obj_stat_exp &&
		echo LinksSize: 45 >> obj_stat_exp &&
		echo DataSize: 2 >> obj_stat_exp &&
		echo CumulativeSize: 114 >> obj_stat_exp &&

201
		test_cmp obj_stat_exp obj_stat_out
202 203
	'

204 205 206 207 208
	test_expect_success "should have created dir within a dir" '
		ipfs ls $OUTPUT > patched_output
	'

	test_expect_success "output looks good" '
209
		echo "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn 4 foo/" > patched_exp &&
210 211 212 213 214 215 216 217 218 219 220
		test_cmp patched_exp patched_output
	'

	test_expect_success "can remove the directory" '
		ipfs object patch $OUTPUT rm-link foo > rmlink_output
	'

	test_expect_success "output should be empty" '
		echo QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn > rmlink_exp &&
		test_cmp rmlink_exp rmlink_output
	'
221

Jeromy's avatar
Jeromy committed
222 223 224 225 226 227
	test_expect_success "multilayer rm-link should work" '
		ipfs object patch $(cat multi_patch) rm-link a/b/c > multi_link_rm_out
	'

	test_expect_success "output looks good" '
		echo "QmZD3r9cZjzU8huNY2JS9TC6n8daDfT8TmE8zBSqG31Wvq" > multi_link_rm_exp &&
228
		test_cmp multi_link_rm_exp multi_link_rm_out
Jeromy's avatar
Jeromy committed
229 230
	'

231
	test_patch_create_path $EMPTY a/b/c $FILE
Jeromy's avatar
Jeromy committed
232

233
	test_patch_create_path $EMPTY a $FILE
Jeromy's avatar
Jeromy committed
234

235
	test_patch_create_path $EMPTY a/b/b/b/b $FILE
236

237 238
	test_expect_success "create bad path fails" '
		test_must_fail ipfs object patch --create $EMPTY add-link / $FILE
239
	'
240 241 242 243 244 245 246 247 248
}

# should work offline
test_object_cmd

# should work online
test_launch_ipfs_daemon
test_object_cmd
test_kill_ipfs_daemon
249 250

test_done