| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | test_description="Test experimental p2p commands" |
| 4 | |
| 5 | . lib/test-lib.sh |
| 6 | |
| 7 | # start iptb + wait for peering |
| 8 | test_expect_success 'init iptb' ' |
| 9 | iptb testbed create -type localipfs --count 3 --init |
| 10 | ' |
| 11 | |
| 12 | test_expect_success 'generate test data' ' |
| 13 | echo "ABCDEF" > test0.bin && |
| 14 | echo "012345" > test1.bin |
| 15 | ' |
| 16 | |
| 17 | startup_cluster 3 |
| 18 | |
| 19 | test_expect_success 'peer ids' ' |
| 20 | PEERID_0=$(iptb attr get 0 id) && |
| 21 | PEERID_1=$(iptb attr get 1 id) |
| 22 | ' |
| 23 | check_test_ports() { |
| 24 | test_expect_success "test ports are closed" ' |
| 25 | (! (netstat -aln | grep "LISTEN" | grep -E "[.:]10101 ")) && |
| 26 | (! (netstat -aln | grep "LISTEN" | grep -E "[.:]10102 ")) && |
| 27 | (! (netstat -aln | grep "LISTEN" | grep -E "[.:]10103 ")) && |
| 28 | (! (netstat -aln | grep "LISTEN" | grep -E "[.:]10104 ")) |
| 29 | ' |
| 30 | } |
| 31 | check_test_ports |
| 32 | |
| 33 | test_expect_success 'fail without config option being enabled' ' |
| 34 | test_must_fail ipfsi 0 p2p stream ls |
| 35 | ' |
| 36 | |
| 37 | test_expect_success "enable filestore config setting" ' |
| 38 | ipfsi 0 config --json Experimental.Libp2pStreamMounting true |
| 39 | ipfsi 1 config --json Experimental.Libp2pStreamMounting true |
| 40 | ipfsi 2 config --json Experimental.Libp2pStreamMounting true |
| 41 | ' |
| 42 | |
| 43 | test_expect_success 'start p2p listener' ' |
| 44 | ipfsi 0 p2p listen /x/p2p-test /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log |
| 45 | ' |
| 46 | |
| 47 | test_expect_success 'cannot re-register p2p listener' ' |
| 48 | test_must_fail ipfsi 0 p2p listen /x/p2p-test /ip4/127.0.0.1/tcp/10103 2>&1 > listener-stdouterr.log |
| 49 | ' |
| 50 | |
| 51 | # Server to client communications |
| 52 | |
| 53 | spawn_sending_server() { |
| 54 | test_expect_success 'S->C Spawn sending server' ' |
| 55 | ma-pipe-unidir --listen --pidFile=listener.pid send /ip4/127.0.0.1/tcp/10101 < test0.bin & |
| 56 | |
| 57 | test_wait_for_file 30 100ms listener.pid && |
| 58 | kill -0 $(cat listener.pid) |
| 59 | ' |
| 60 | } |
| 61 | |
| 62 | test_server_to_client() { |
| 63 | test_expect_success 'S->C Connect and receive data' ' |
| 64 | ma-pipe-unidir recv /ip4/127.0.0.1/tcp/10102 > client.out |
| 65 | ' |
| 66 | |
| 67 | test_expect_success 'S->C Ensure server finished' ' |
| 68 | test ! -f listener.pid |
| 69 | ' |
| 70 | |
| 71 | test_expect_success 'S->C Output looks good' ' |
| 72 | test_cmp client.out test0.bin |
| 73 | ' |
| 74 | } |
| 75 | |
| 76 | spawn_sending_server |
| 77 | |
| 78 | test_expect_success 'S->C(/p2p/peerID) Setup client side' ' |
| 79 | ipfsi 1 p2p forward /x/p2p-test /ip4/127.0.0.1/tcp/10102 /p2p/${PEERID_0} 2>&1 > dialer-stdouterr.log |
| 80 | ' |
| 81 | |
| 82 | test_expect_success 'S->C Setup(dnsaddr/addr/p2p/peerID) client side' ' |
| 83 | ipfsi 1 p2p forward /x/p2p-test /ip4/127.0.0.1/tcp/10103 /dnsaddr/bootstrap.libp2p.io/p2p/${PEERID_0} 2>&1 > dialer-stdouterr.log |
| 84 | ' |
| 85 | |
| 86 | test_expect_success 'S->C Setup(dnsaddr/addr) client side' ' |
| 87 | ipfsi 1 p2p forward /x/p2p-test /ip4/127.0.0.1/tcp/10104 /dnsaddr/example-dnsaddr.multiformats.io 2>&1 > dialer-stdouterr.log |
| 88 | ' |
| 89 | |
| 90 | |
| 91 | test_expect_success 'S->C Output is empty' ' |
| 92 | test_must_be_empty dialer-stdouterr.log |
| 93 | ' |
| 94 | |
| 95 | test_expect_success "'ipfs p2p ls | grep' succeeds" ' |
| 96 | ipfsi 1 p2p ls | grep "/x/p2p-test /ip4/127.0.0.1/tcp/10104" |
| 97 | ' |
| 98 | |
| 99 | test_server_to_client |
| 100 | |
| 101 | test_expect_success 'S->C Connect with dead server' ' |
| 102 | ma-pipe-unidir recv /ip4/127.0.0.1/tcp/10102 > client.out |
| 103 | ' |
| 104 | |
| 105 | test_expect_success 'S->C Output is empty' ' |
| 106 | test_must_be_empty client.out |
| 107 | ' |
| 108 | |
| 109 | spawn_sending_server |
| 110 | |
| 111 | test_server_to_client |
| 112 | |
| 113 | test_expect_success 'S->C Close local listener' ' |
| 114 | ipfsi 1 p2p close -p /x/p2p-test |
| 115 | ' |
| 116 | |
| 117 | check_test_ports |
| 118 | |
| 119 | # Client to server communications |
| 120 | |
| 121 | test_expect_success 'C->S Spawn receiving server' ' |
| 122 | ma-pipe-unidir --listen --pidFile=listener.pid recv /ip4/127.0.0.1/tcp/10101 > server.out & |
| 123 | |
| 124 | test_wait_for_file 30 100ms listener.pid && |
| 125 | kill -0 $(cat listener.pid) |
| 126 | ' |
| 127 | |
| 128 | test_expect_success 'C->S Setup client side' ' |
| 129 | ipfsi 1 p2p forward /x/p2p-test /ip4/127.0.0.1/tcp/10102 /p2p/${PEERID_0} 2>&1 > dialer-stdouterr.log |
| 130 | ' |
| 131 | |
| 132 | test_expect_success 'C->S Connect and receive data' ' |
| 133 | ma-pipe-unidir send /ip4/127.0.0.1/tcp/10102 < test1.bin |
| 134 | ' |
| 135 | |
| 136 | test_expect_success 'C->S Ensure server finished' ' |
| 137 | go-sleep 250ms && |
| 138 | test ! -f listener.pid |
| 139 | ' |
| 140 | |
| 141 | test_expect_success 'C->S Output looks good' ' |
| 142 | test_cmp server.out test1.bin |
| 143 | ' |
| 144 | |
| 145 | test_expect_success 'C->S Close local listener' ' |
| 146 | ipfsi 1 p2p close -p /x/p2p-test |
| 147 | ' |
| 148 | |
| 149 | check_test_ports |
| 150 | |
| 151 | # Checking port |
| 152 | |
| 153 | test_expect_success "cannot accept 0 port in 'ipfs p2p listen'" ' |
| 154 | test_must_fail ipfsi 2 p2p listen /x/p2p-test/0 /ip4/127.0.0.1/tcp/0 |
| 155 | ' |
| 156 | |
| 157 | test_expect_success "'ipfs p2p forward' accept 0 port" ' |
| 158 | ipfsi 2 p2p forward /x/p2p-test/0 /ip4/127.0.0.1/tcp/0 /p2p/$PEERID_0 |
| 159 | ' |
| 160 | |
| 161 | test_expect_success "'ipfs p2p ls' output looks good" ' |
| 162 | echo "true" > forward_0_expected && |
| 163 | ipfsi 2 p2p ls | awk '\''{print $2}'\'' | sed "s/.*\///" | awk -F: '\''{if($1>0)print"true"}'\'' > forward_0_actual && |
| 164 | ipfsi 2 p2p close -p /x/p2p-test/0 && |
| 165 | test_cmp forward_0_expected forward_0_actual |
| 166 | ' |
| 167 | |
| 168 | # Listing streams |
| 169 | |
| 170 | test_expect_success "'ipfs p2p ls' succeeds" ' |
| 171 | echo "/x/p2p-test /p2p/$PEERID_0 /ip4/127.0.0.1/tcp/10101" > expected && |
| 172 | ipfsi 0 p2p ls > actual |
| 173 | ' |
| 174 | |
| 175 | test_expect_success "'ipfs p2p ls' output looks good" ' |
| 176 | test_cmp expected actual |
| 177 | ' |
| 178 | |
| 179 | test_expect_success "Cannot re-register app handler" ' |
| 180 | test_must_fail ipfsi 0 p2p listen /x/p2p-test /ip4/127.0.0.1/tcp/10101 |
| 181 | ' |
| 182 | |
| 183 | test_expect_success "'ipfs p2p stream ls' output is empty" ' |
| 184 | ipfsi 0 p2p stream ls > actual && |
| 185 | test_must_be_empty actual |
| 186 | ' |
| 187 | |
| 188 | check_test_ports |
| 189 | |
| 190 | test_expect_success "Setup: Idle stream" ' |
| 191 | ma-pipe-unidir --listen --pidFile=listener.pid recv /ip4/127.0.0.1/tcp/10101 & |
| 192 | |
| 193 | ipfsi 1 p2p forward /x/p2p-test /ip4/127.0.0.1/tcp/10102 /p2p/$PEERID_0 && |
| 194 | ma-pipe-unidir --pidFile=client.pid recv /ip4/127.0.0.1/tcp/10102 & |
| 195 | |
| 196 | test_wait_for_file 30 100ms listener.pid && |
| 197 | test_wait_for_file 30 100ms client.pid && |
| 198 | kill -0 $(cat listener.pid) && kill -0 $(cat client.pid) |
| 199 | ' |
| 200 | |
| 201 | test_expect_success "'ipfs p2p stream ls' succeeds" ' |
| 202 | echo "3 /x/p2p-test /p2p/$PEERID_1 /ip4/127.0.0.1/tcp/10101" > expected |
| 203 | ipfsi 0 p2p stream ls > actual |
| 204 | ' |
| 205 | |
| 206 | test_expect_success "'ipfs p2p stream ls' output looks good" ' |
| 207 | test_cmp expected actual |
| 208 | ' |
| 209 | |
| 210 | test_expect_success "'ipfs p2p stream close' closes stream" ' |
| 211 | ipfsi 0 p2p stream close 3 && |
| 212 | ipfsi 0 p2p stream ls > actual && |
| 213 | [ ! -f listener.pid ] && [ ! -f client.pid ] && |
| 214 | test_must_be_empty actual |
| 215 | ' |
| 216 | |
| 217 | test_expect_success "'ipfs p2p close' closes remote handler" ' |
| 218 | ipfsi 0 p2p close -p /x/p2p-test && |
| 219 | ipfsi 0 p2p ls > actual && |
| 220 | test_must_be_empty actual |
| 221 | ' |
| 222 | |
| 223 | test_expect_success "'ipfs p2p close' closes local handler" ' |
| 224 | ipfsi 1 p2p close -p /x/p2p-test && |
| 225 | ipfsi 1 p2p ls > actual && |
| 226 | test_must_be_empty actual |
| 227 | ' |
| 228 | |
| 229 | check_test_ports |
| 230 | |
| 231 | test_expect_success "Setup: Idle stream(2)" ' |
| 232 | ma-pipe-unidir --listen --pidFile=listener.pid recv /ip4/127.0.0.1/tcp/10101 & |
| 233 | |
| 234 | ipfsi 0 p2p listen /x/p2p-test2 /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log && |
| 235 | ipfsi 1 p2p forward /x/p2p-test2 /ip4/127.0.0.1/tcp/10102 /p2p/$PEERID_0 2>&1 > dialer-stdouterr.log && |
| 236 | ma-pipe-unidir --pidFile=client.pid recv /ip4/127.0.0.1/tcp/10102 & |
| 237 | |
| 238 | test_wait_for_file 30 100ms listener.pid && |
| 239 | test_wait_for_file 30 100ms client.pid && |
| 240 | kill -0 $(cat listener.pid) && kill -0 $(cat client.pid) |
| 241 | ' |
| 242 | |
| 243 | test_expect_success "'ipfs p2p stream ls' succeeds(2)" ' |
| 244 | echo "4 /x/p2p-test2 /p2p/$PEERID_1 /ip4/127.0.0.1/tcp/10101" > expected |
| 245 | ipfsi 0 p2p stream ls > actual |
| 246 | test_cmp expected actual |
| 247 | ' |
| 248 | |
| 249 | test_expect_success "'ipfs p2p close -a' closes remote app handlers" ' |
| 250 | ipfsi 0 p2p close -a && |
| 251 | ipfsi 0 p2p ls > actual && |
| 252 | test_must_be_empty actual |
| 253 | ' |
| 254 | |
| 255 | test_expect_success "'ipfs p2p close -a' closes local app handlers" ' |
| 256 | ipfsi 1 p2p close -a && |
| 257 | ipfsi 1 p2p ls > actual && |
| 258 | test_must_be_empty actual |
| 259 | ' |
| 260 | |
| 261 | test_expect_success "'ipfs p2p stream close -a' closes streams" ' |
| 262 | ipfsi 0 p2p stream close -a && |
| 263 | ipfsi 0 p2p stream ls > actual && |
| 264 | [ ! -f listener.pid ] && [ ! -f client.pid ] && |
| 265 | test_must_be_empty actual |
| 266 | ' |
| 267 | |
| 268 | check_test_ports |
| 269 | |
| 270 | test_expect_success "'ipfs p2p close' closes app numeric handlers" ' |
| 271 | ipfsi 0 p2p listen /x/1234 /ip4/127.0.0.1/tcp/10101 && |
| 272 | ipfsi 0 p2p close -p /x/1234 && |
| 273 | ipfsi 0 p2p ls > actual && |
| 274 | test_must_be_empty actual |
| 275 | ' |
| 276 | |
| 277 | test_expect_success "'ipfs p2p close' closes by target addr" ' |
| 278 | ipfsi 0 p2p listen /x/p2p-test /ip4/127.0.0.1/tcp/10101 && |
| 279 | ipfsi 0 p2p close -t /ip4/127.0.0.1/tcp/10101 && |
| 280 | ipfsi 0 p2p ls > actual && |
| 281 | test_must_be_empty actual |
| 282 | ' |
| 283 | |
| 284 | test_expect_success "'ipfs p2p close' closes right listeners" ' |
| 285 | ipfsi 0 p2p listen /x/p2p-test /ip4/127.0.0.1/tcp/10101 && |
| 286 | ipfsi 0 p2p forward /x/p2p-test /ip4/127.0.0.1/tcp/10101 /p2p/$PEERID_1 && |
| 287 | echo "/x/p2p-test /p2p/$PEERID_0 /ip4/127.0.0.1/tcp/10101" > expected && |
| 288 | |
| 289 | ipfsi 0 p2p close -l /ip4/127.0.0.1/tcp/10101 && |
| 290 | ipfsi 0 p2p ls > actual && |
| 291 | test_cmp expected actual |
| 292 | ' |
| 293 | |
| 294 | check_test_ports |
| 295 | |
| 296 | test_expect_success "'ipfs p2p close' closes by listen addr" ' |
| 297 | ipfsi 0 p2p close -l /p2p/$PEERID_0 && |
| 298 | ipfsi 0 p2p ls > actual && |
| 299 | test_must_be_empty actual |
| 300 | ' |
| 301 | |
| 302 | # Peer reporting |
| 303 | |
| 304 | test_expect_success 'start p2p listener reporting peer' ' |
| 305 | ipfsi 0 p2p listen /x/p2p-test /ip4/127.0.0.1/tcp/10101 --report-peer-id 2>&1 > listener-stdouterr.log |
| 306 | ' |
| 307 | |
| 308 | test_expect_success 'C->S Spawn receiving server' ' |
| 309 | ma-pipe-unidir --listen --pidFile=listener.pid recv /ip4/127.0.0.1/tcp/10101 > server.out & |
| 310 | |
| 311 | test_wait_for_file 30 100ms listener.pid && |
| 312 | kill -0 $(cat listener.pid) |
| 313 | ' |
| 314 | |
| 315 | test_expect_success 'C->S Setup client side' ' |
| 316 | ipfsi 1 p2p forward /x/p2p-test /ip4/127.0.0.1/tcp/10102 /p2p/${PEERID_0} 2>&1 > dialer-stdouterr.log |
| 317 | ' |
| 318 | |
| 319 | test_expect_success 'C->S Connect and receive data' ' |
| 320 | ma-pipe-unidir send /ip4/127.0.0.1/tcp/10102 < test1.bin |
| 321 | ' |
| 322 | |
| 323 | test_expect_success 'C->S Ensure server finished' ' |
| 324 | go-sleep 250ms && |
| 325 | test ! -f listener.pid |
| 326 | ' |
| 327 | |
| 328 | test_expect_success 'C->S Output looks good' ' |
| 329 | echo ${PEERID_1} > expected && |
| 330 | cat test1.bin >> expected && |
| 331 | test_cmp server.out expected |
| 332 | ' |
| 333 | |
| 334 | test_expect_success 'C->S Close listeners' ' |
| 335 | ipfsi 1 p2p close -p /x/p2p-test && |
| 336 | ipfsi 0 p2p close -p /x/p2p-test && |
| 337 | |
| 338 | ipfsi 0 p2p ls > actual && |
| 339 | test_must_be_empty actual && |
| 340 | |
| 341 | ipfsi 1 p2p ls > actual && |
| 342 | test_must_be_empty actual |
| 343 | ' |
| 344 | |
| 345 | test_expect_success "non /x/ scoped protocols are not allowed" ' |
| 346 | test_must_fail ipfsi 0 p2p listen /its/not/a/x/path /ip4/127.0.0.1/tcp/10101 2> actual && |
| 347 | echo "Error: protocol name must be within '"'"'/x/'"'"' namespace" > expected |
| 348 | test_cmp expected actual |
| 349 | ' |
| 350 | |
| 351 | check_test_ports |
| 352 | |
| 353 | test_expect_success 'start p2p listener on custom proto' ' |
| 354 | ipfsi 0 p2p listen --allow-custom-protocol /p2p-test /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log && |
| 355 | test_must_be_empty listener-stdouterr.log |
| 356 | ' |
| 357 | |
| 358 | spawn_sending_server |
| 359 | |
| 360 | test_expect_success 'S->C Setup client side (custom proto)' ' |
| 361 | ipfsi 1 p2p forward --allow-custom-protocol /p2p-test /ip4/127.0.0.1/tcp/10102 /p2p/${PEERID_0} 2>&1 > dialer-stdouterr.log |
| 362 | ' |
| 363 | |
| 364 | test_server_to_client |
| 365 | |
| 366 | test_expect_success 'C->S Close local listener' ' |
| 367 | ipfsi 1 p2p close -p /p2p-test |
| 368 | ipfsi 1 p2p ls > actual && |
| 369 | test_must_be_empty actual |
| 370 | ' |
| 371 | |
| 372 | check_test_ports |
| 373 | |
| 374 | test_expect_success 'stop iptb' ' |
| 375 | iptb stop |
| 376 | ' |
| 377 | |
| 378 | check_test_ports |
| 379 | |
| 380 | test_done |
| 381 |