github.com/vipernet-xyz/tendermint-core@v0.32.0/p2p/transport_test.go (about) 1 package p2p 2 3 import ( 4 "fmt" 5 "math/rand" 6 "net" 7 "reflect" 8 "strings" 9 "testing" 10 "time" 11 12 "github.com/tendermint/tendermint/crypto/ed25519" 13 "github.com/tendermint/tendermint/p2p/conn" 14 ) 15 16 var defaultNodeName = "host_peer" 17 18 func emptyNodeInfo() NodeInfo { 19 return DefaultNodeInfo{} 20 } 21 22 // newMultiplexTransport returns a tcp connected multiplexed peer 23 // using the default MConnConfig. It's a convenience function used 24 // for testing. 25 func newMultiplexTransport( 26 nodeInfo NodeInfo, 27 nodeKey NodeKey, 28 ) *MultiplexTransport { 29 return NewMultiplexTransport( 30 nodeInfo, nodeKey, conn.DefaultMConnConfig(), 31 ) 32 } 33 34 func TestTransportMultiplexConnFilter(t *testing.T) { 35 mt := newMultiplexTransport( 36 emptyNodeInfo(), 37 NodeKey{ 38 PrivKey: ed25519.GenPrivKey(), 39 }, 40 ) 41 id := mt.nodeKey.ID() 42 43 MultiplexTransportConnFilters( 44 func(_ ConnSet, _ net.Conn, _ []net.IP) error { return nil }, 45 func(_ ConnSet, _ net.Conn, _ []net.IP) error { return nil }, 46 func(_ ConnSet, _ net.Conn, _ []net.IP) error { 47 return fmt.Errorf("rejected") 48 }, 49 )(mt) 50 51 addr, err := NewNetAddressString(IDAddressString(id, "127.0.0.1:0")) 52 if err != nil { 53 t.Fatal(err) 54 } 55 56 if err := mt.Listen(*addr); err != nil { 57 t.Fatal(err) 58 } 59 60 errc := make(chan error) 61 62 go func() { 63 addr := NewNetAddress(id, mt.listener.Addr()) 64 65 _, err := addr.Dial() 66 if err != nil { 67 errc <- err 68 return 69 } 70 71 close(errc) 72 }() 73 74 if err := <-errc; err != nil { 75 t.Errorf("connection failed: %v", err) 76 } 77 78 _, err = mt.Accept(peerConfig{}) 79 if err, ok := err.(ErrRejected); ok { 80 if !err.IsFiltered() { 81 t.Errorf("expected peer to be filtered") 82 } 83 } else { 84 t.Errorf("expected ErrRejected") 85 } 86 } 87 88 func TestTransportMultiplexConnFilterTimeout(t *testing.T) { 89 mt := newMultiplexTransport( 90 emptyNodeInfo(), 91 NodeKey{ 92 PrivKey: ed25519.GenPrivKey(), 93 }, 94 ) 95 id := mt.nodeKey.ID() 96 97 MultiplexTransportFilterTimeout(5 * time.Millisecond)(mt) 98 MultiplexTransportConnFilters( 99 func(_ ConnSet, _ net.Conn, _ []net.IP) error { 100 time.Sleep(10 * time.Millisecond) 101 return nil 102 }, 103 )(mt) 104 105 addr, err := NewNetAddressString(IDAddressString(id, "127.0.0.1:0")) 106 if err != nil { 107 t.Fatal(err) 108 } 109 110 if err := mt.Listen(*addr); err != nil { 111 t.Fatal(err) 112 } 113 114 errc := make(chan error) 115 116 go func() { 117 addr := NewNetAddress(id, mt.listener.Addr()) 118 119 _, err := addr.Dial() 120 if err != nil { 121 errc <- err 122 return 123 } 124 125 close(errc) 126 }() 127 128 if err := <-errc; err != nil { 129 t.Errorf("connection failed: %v", err) 130 } 131 132 _, err = mt.Accept(peerConfig{}) 133 if _, ok := err.(ErrFilterTimeout); !ok { 134 t.Errorf("expected ErrFilterTimeout") 135 } 136 } 137 138 func TestTransportMultiplexMaxIncomingConnections(t *testing.T) { 139 mt := newMultiplexTransport( 140 emptyNodeInfo(), 141 NodeKey{ 142 PrivKey: ed25519.GenPrivKey(), 143 }, 144 ) 145 id := mt.nodeKey.ID() 146 147 MultiplexTransportMaxIncomingConnections(0)(mt) 148 149 addr, err := NewNetAddressString(IDAddressString(id, "127.0.0.1:0")) 150 if err != nil { 151 t.Fatal(err) 152 } 153 154 if err := mt.Listen(*addr); err != nil { 155 t.Fatal(err) 156 } 157 158 errc := make(chan error) 159 160 go func() { 161 addr := NewNetAddress(id, mt.listener.Addr()) 162 163 _, err := addr.Dial() 164 if err != nil { 165 errc <- err 166 return 167 } 168 169 close(errc) 170 }() 171 172 if err := <-errc; err != nil { 173 t.Errorf("connection failed: %v", err) 174 } 175 176 _, err = mt.Accept(peerConfig{}) 177 if err == nil || !strings.Contains(err.Error(), "connection reset by peer") { 178 t.Errorf("expected connection reset by peer error, got %v", err) 179 } 180 } 181 182 func TestTransportMultiplexAcceptMultiple(t *testing.T) { 183 mt := testSetupMultiplexTransport(t) 184 laddr := NewNetAddress(mt.nodeKey.ID(), mt.listener.Addr()) 185 186 var ( 187 seed = rand.New(rand.NewSource(time.Now().UnixNano())) 188 nDialers = seed.Intn(64) + 64 189 errc = make(chan error, nDialers) 190 ) 191 192 // Setup dialers. 193 for i := 0; i < nDialers; i++ { 194 go testDialer(*laddr, errc) 195 } 196 197 // Catch connection errors. 198 for i := 0; i < nDialers; i++ { 199 if err := <-errc; err != nil { 200 t.Fatal(err) 201 } 202 } 203 204 ps := []Peer{} 205 206 // Accept all peers. 207 for i := 0; i < cap(errc); i++ { 208 p, err := mt.Accept(peerConfig{}) 209 if err != nil { 210 t.Fatal(err) 211 } 212 213 if err := p.Start(); err != nil { 214 t.Fatal(err) 215 } 216 217 ps = append(ps, p) 218 } 219 220 if have, want := len(ps), cap(errc); have != want { 221 t.Errorf("have %v, want %v", have, want) 222 } 223 224 // Stop all peers. 225 for _, p := range ps { 226 if err := p.Stop(); err != nil { 227 t.Fatal(err) 228 } 229 } 230 231 if err := mt.Close(); err != nil { 232 t.Errorf("close errored: %v", err) 233 } 234 } 235 236 func testDialer(dialAddr NetAddress, errc chan error) { 237 var ( 238 pv = ed25519.GenPrivKey() 239 dialer = newMultiplexTransport( 240 testNodeInfo(PubKeyToID(pv.PubKey()), defaultNodeName), 241 NodeKey{ 242 PrivKey: pv, 243 }, 244 ) 245 ) 246 247 _, err := dialer.Dial(dialAddr, peerConfig{}) 248 if err != nil { 249 errc <- err 250 return 251 } 252 253 // Signal that the connection was established. 254 errc <- nil 255 } 256 257 func TestTransportMultiplexAcceptNonBlocking(t *testing.T) { 258 mt := testSetupMultiplexTransport(t) 259 260 var ( 261 fastNodePV = ed25519.GenPrivKey() 262 fastNodeInfo = testNodeInfo(PubKeyToID(fastNodePV.PubKey()), "fastnode") 263 errc = make(chan error) 264 fastc = make(chan struct{}) 265 slowc = make(chan struct{}) 266 ) 267 268 // Simulate slow Peer. 269 go func() { 270 addr := NewNetAddress(mt.nodeKey.ID(), mt.listener.Addr()) 271 272 c, err := addr.Dial() 273 if err != nil { 274 errc <- err 275 return 276 } 277 278 close(slowc) 279 280 select { 281 case <-fastc: 282 // Fast peer connected. 283 case <-time.After(50 * time.Millisecond): 284 // We error if the fast peer didn't succeed. 285 errc <- fmt.Errorf("fast peer timed out") 286 } 287 288 sc, err := upgradeSecretConn(c, 20*time.Millisecond, ed25519.GenPrivKey()) 289 if err != nil { 290 errc <- err 291 return 292 } 293 294 _, err = handshake(sc, 20*time.Millisecond, 295 testNodeInfo( 296 PubKeyToID(ed25519.GenPrivKey().PubKey()), 297 "slow_peer", 298 )) 299 if err != nil { 300 errc <- err 301 return 302 } 303 }() 304 305 // Simulate fast Peer. 306 go func() { 307 <-slowc 308 309 var ( 310 dialer = newMultiplexTransport( 311 fastNodeInfo, 312 NodeKey{ 313 PrivKey: fastNodePV, 314 }, 315 ) 316 ) 317 addr := NewNetAddress(mt.nodeKey.ID(), mt.listener.Addr()) 318 319 _, err := dialer.Dial(*addr, peerConfig{}) 320 if err != nil { 321 errc <- err 322 return 323 } 324 325 close(errc) 326 close(fastc) 327 }() 328 329 if err := <-errc; err != nil { 330 t.Errorf("connection failed: %v", err) 331 } 332 333 p, err := mt.Accept(peerConfig{}) 334 if err != nil { 335 t.Fatal(err) 336 } 337 338 if have, want := p.NodeInfo(), fastNodeInfo; !reflect.DeepEqual(have, want) { 339 t.Errorf("have %v, want %v", have, want) 340 } 341 } 342 343 func TestTransportMultiplexValidateNodeInfo(t *testing.T) { 344 mt := testSetupMultiplexTransport(t) 345 346 errc := make(chan error) 347 348 go func() { 349 var ( 350 pv = ed25519.GenPrivKey() 351 dialer = newMultiplexTransport( 352 testNodeInfo(PubKeyToID(pv.PubKey()), ""), // Should not be empty 353 NodeKey{ 354 PrivKey: pv, 355 }, 356 ) 357 ) 358 359 addr := NewNetAddress(mt.nodeKey.ID(), mt.listener.Addr()) 360 361 _, err := dialer.Dial(*addr, peerConfig{}) 362 if err != nil { 363 errc <- err 364 return 365 } 366 367 close(errc) 368 }() 369 370 if err := <-errc; err != nil { 371 t.Errorf("connection failed: %v", err) 372 } 373 374 _, err := mt.Accept(peerConfig{}) 375 if err, ok := err.(ErrRejected); ok { 376 if !err.IsNodeInfoInvalid() { 377 t.Errorf("expected NodeInfo to be invalid") 378 } 379 } else { 380 t.Errorf("expected ErrRejected") 381 } 382 } 383 384 func TestTransportMultiplexRejectMissmatchID(t *testing.T) { 385 mt := testSetupMultiplexTransport(t) 386 387 errc := make(chan error) 388 389 go func() { 390 dialer := newMultiplexTransport( 391 testNodeInfo( 392 PubKeyToID(ed25519.GenPrivKey().PubKey()), "dialer", 393 ), 394 NodeKey{ 395 PrivKey: ed25519.GenPrivKey(), 396 }, 397 ) 398 addr := NewNetAddress(mt.nodeKey.ID(), mt.listener.Addr()) 399 400 _, err := dialer.Dial(*addr, peerConfig{}) 401 if err != nil { 402 errc <- err 403 return 404 } 405 406 close(errc) 407 }() 408 409 if err := <-errc; err != nil { 410 t.Errorf("connection failed: %v", err) 411 } 412 413 _, err := mt.Accept(peerConfig{}) 414 if err, ok := err.(ErrRejected); ok { 415 if !err.IsAuthFailure() { 416 t.Errorf("expected auth failure") 417 } 418 } else { 419 t.Errorf("expected ErrRejected") 420 } 421 } 422 423 func TestTransportMultiplexDialRejectWrongID(t *testing.T) { 424 mt := testSetupMultiplexTransport(t) 425 426 var ( 427 pv = ed25519.GenPrivKey() 428 dialer = newMultiplexTransport( 429 testNodeInfo(PubKeyToID(pv.PubKey()), ""), // Should not be empty 430 NodeKey{ 431 PrivKey: pv, 432 }, 433 ) 434 ) 435 436 wrongID := PubKeyToID(ed25519.GenPrivKey().PubKey()) 437 addr := NewNetAddress(wrongID, mt.listener.Addr()) 438 439 _, err := dialer.Dial(*addr, peerConfig{}) 440 if err != nil { 441 t.Logf("connection failed: %v", err) 442 if err, ok := err.(ErrRejected); ok { 443 if !err.IsAuthFailure() { 444 t.Errorf("expected auth failure") 445 } 446 } else { 447 t.Errorf("expected ErrRejected") 448 } 449 } 450 } 451 452 func TestTransportMultiplexRejectIncompatible(t *testing.T) { 453 mt := testSetupMultiplexTransport(t) 454 455 errc := make(chan error) 456 457 go func() { 458 var ( 459 pv = ed25519.GenPrivKey() 460 dialer = newMultiplexTransport( 461 testNodeInfoWithNetwork(PubKeyToID(pv.PubKey()), "dialer", "incompatible-network"), 462 NodeKey{ 463 PrivKey: pv, 464 }, 465 ) 466 ) 467 addr := NewNetAddress(mt.nodeKey.ID(), mt.listener.Addr()) 468 469 _, err := dialer.Dial(*addr, peerConfig{}) 470 if err != nil { 471 errc <- err 472 return 473 } 474 475 close(errc) 476 }() 477 478 _, err := mt.Accept(peerConfig{}) 479 if err, ok := err.(ErrRejected); ok { 480 if !err.IsIncompatible() { 481 t.Errorf("expected to reject incompatible") 482 } 483 } else { 484 t.Errorf("expected ErrRejected") 485 } 486 } 487 488 func TestTransportMultiplexRejectSelf(t *testing.T) { 489 mt := testSetupMultiplexTransport(t) 490 491 errc := make(chan error) 492 493 go func() { 494 addr := NewNetAddress(mt.nodeKey.ID(), mt.listener.Addr()) 495 496 _, err := mt.Dial(*addr, peerConfig{}) 497 if err != nil { 498 errc <- err 499 return 500 } 501 502 close(errc) 503 }() 504 505 if err := <-errc; err != nil { 506 if err, ok := err.(ErrRejected); ok { 507 if !err.IsSelf() { 508 t.Errorf("expected to reject self, got: %v", err) 509 } 510 } else { 511 t.Errorf("expected ErrRejected") 512 } 513 } else { 514 t.Errorf("expected connection failure") 515 } 516 517 _, err := mt.Accept(peerConfig{}) 518 if err, ok := err.(ErrRejected); ok { 519 if !err.IsSelf() { 520 t.Errorf("expected to reject self, got: %v", err) 521 } 522 } else { 523 t.Errorf("expected ErrRejected") 524 } 525 } 526 527 func TestTransportConnDuplicateIPFilter(t *testing.T) { 528 filter := ConnDuplicateIPFilter() 529 530 if err := filter(nil, &testTransportConn{}, nil); err != nil { 531 t.Fatal(err) 532 } 533 534 var ( 535 c = &testTransportConn{} 536 cs = NewConnSet() 537 ) 538 539 cs.Set(c, []net.IP{ 540 {10, 0, 10, 1}, 541 {10, 0, 10, 2}, 542 {10, 0, 10, 3}, 543 }) 544 545 if err := filter(cs, c, []net.IP{ 546 {10, 0, 10, 2}, 547 }); err == nil { 548 t.Errorf("expected Peer to be rejected as duplicate") 549 } 550 } 551 552 func TestTransportHandshake(t *testing.T) { 553 ln, err := net.Listen("tcp", "127.0.0.1:0") 554 if err != nil { 555 t.Fatal(err) 556 } 557 558 var ( 559 peerPV = ed25519.GenPrivKey() 560 peerNodeInfo = testNodeInfo(PubKeyToID(peerPV.PubKey()), defaultNodeName) 561 ) 562 563 go func() { 564 c, err := net.Dial(ln.Addr().Network(), ln.Addr().String()) 565 if err != nil { 566 t.Error(err) 567 return 568 } 569 570 go func(c net.Conn) { 571 _, err := cdc.MarshalBinaryLengthPrefixedWriter(c, peerNodeInfo.(DefaultNodeInfo)) 572 if err != nil { 573 t.Error(err) 574 } 575 }(c) 576 go func(c net.Conn) { 577 var ni DefaultNodeInfo 578 579 _, err := cdc.UnmarshalBinaryLengthPrefixedReader( 580 c, 581 &ni, 582 int64(MaxNodeInfoSize()), 583 ) 584 if err != nil { 585 t.Error(err) 586 } 587 }(c) 588 }() 589 590 c, err := ln.Accept() 591 if err != nil { 592 t.Fatal(err) 593 } 594 595 ni, err := handshake(c, 20*time.Millisecond, emptyNodeInfo()) 596 if err != nil { 597 t.Fatal(err) 598 } 599 600 if have, want := ni, peerNodeInfo; !reflect.DeepEqual(have, want) { 601 t.Errorf("have %v, want %v", have, want) 602 } 603 } 604 605 // create listener 606 func testSetupMultiplexTransport(t *testing.T) *MultiplexTransport { 607 var ( 608 pv = ed25519.GenPrivKey() 609 id = PubKeyToID(pv.PubKey()) 610 mt = newMultiplexTransport( 611 testNodeInfo( 612 id, "transport", 613 ), 614 NodeKey{ 615 PrivKey: pv, 616 }, 617 ) 618 ) 619 620 addr, err := NewNetAddressString(IDAddressString(id, "127.0.0.1:0")) 621 if err != nil { 622 t.Fatal(err) 623 } 624 625 if err := mt.Listen(*addr); err != nil { 626 t.Fatal(err) 627 } 628 629 return mt 630 } 631 632 type testTransportAddr struct{} 633 634 func (a *testTransportAddr) Network() string { return "tcp" } 635 func (a *testTransportAddr) String() string { return "test.local:1234" } 636 637 type testTransportConn struct{} 638 639 func (c *testTransportConn) Close() error { 640 return fmt.Errorf("close() not implemented") 641 } 642 643 func (c *testTransportConn) LocalAddr() net.Addr { 644 return &testTransportAddr{} 645 } 646 647 func (c *testTransportConn) RemoteAddr() net.Addr { 648 return &testTransportAddr{} 649 } 650 651 func (c *testTransportConn) Read(_ []byte) (int, error) { 652 return -1, fmt.Errorf("read() not implemented") 653 } 654 655 func (c *testTransportConn) SetDeadline(_ time.Time) error { 656 return fmt.Errorf("setDeadline() not implemented") 657 } 658 659 func (c *testTransportConn) SetReadDeadline(_ time.Time) error { 660 return fmt.Errorf("setReadDeadline() not implemented") 661 } 662 663 func (c *testTransportConn) SetWriteDeadline(_ time.Time) error { 664 return fmt.Errorf("setWriteDeadline() not implemented") 665 } 666 667 func (c *testTransportConn) Write(_ []byte) (int, error) { 668 return -1, fmt.Errorf("write() not implemented") 669 }