You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

619 lines
17 KiB

  1. package p2p_test
  2. import (
  3. "context"
  4. "io"
  5. "net"
  6. "testing"
  7. "time"
  8. "github.com/fortytw2/leaktest"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. "github.com/tendermint/tendermint/crypto/ed25519"
  12. "github.com/tendermint/tendermint/internal/p2p"
  13. "github.com/tendermint/tendermint/libs/bytes"
  14. "github.com/tendermint/tendermint/types"
  15. )
  16. // transportFactory is used to set up transports for tests.
  17. type transportFactory func(t *testing.T) p2p.Transport
  18. // testTransports is a registry of transport factories for withTransports().
  19. var testTransports = map[string]transportFactory{}
  20. // withTransports is a test helper that runs a test against all transports
  21. // registered in testTransports.
  22. func withTransports(t *testing.T, tester func(*testing.T, transportFactory)) {
  23. t.Helper()
  24. for name, transportFactory := range testTransports {
  25. transportFactory := transportFactory
  26. t.Run(name, func(t *testing.T) {
  27. t.Cleanup(leaktest.Check(t))
  28. tester(t, transportFactory)
  29. })
  30. }
  31. }
  32. func TestTransport_AcceptClose(t *testing.T) {
  33. // Just test accept unblock on close, happy path is tested widely elsewhere.
  34. withTransports(t, func(t *testing.T, makeTransport transportFactory) {
  35. a := makeTransport(t)
  36. // In-progress Accept should error on concurrent close.
  37. errCh := make(chan error, 1)
  38. go func() {
  39. time.Sleep(200 * time.Millisecond)
  40. errCh <- a.Close()
  41. }()
  42. _, err := a.Accept()
  43. require.Error(t, err)
  44. require.Equal(t, io.EOF, err)
  45. require.NoError(t, <-errCh)
  46. // Closed transport should return error immediately.
  47. _, err = a.Accept()
  48. require.Error(t, err)
  49. require.Equal(t, io.EOF, err)
  50. })
  51. }
  52. func TestTransport_DialEndpoints(t *testing.T) {
  53. ipTestCases := []struct {
  54. ip net.IP
  55. ok bool
  56. }{
  57. {net.IPv4zero, true},
  58. {net.IPv6zero, true},
  59. {nil, false},
  60. {net.IPv4bcast, false},
  61. {net.IPv4allsys, false},
  62. {[]byte{1, 2, 3}, false},
  63. {[]byte{1, 2, 3, 4, 5}, false},
  64. }
  65. withTransports(t, func(t *testing.T, makeTransport transportFactory) {
  66. a := makeTransport(t)
  67. endpoints := a.Endpoints()
  68. require.NotEmpty(t, endpoints)
  69. endpoint := endpoints[0]
  70. // Spawn a goroutine to simply accept any connections until closed.
  71. go func() {
  72. for {
  73. conn, err := a.Accept()
  74. if err != nil {
  75. return
  76. }
  77. _ = conn.Close()
  78. }
  79. }()
  80. // Dialing self should work.
  81. conn, err := a.Dial(ctx, endpoint)
  82. require.NoError(t, err)
  83. require.NoError(t, conn.Close())
  84. // Dialing empty endpoint should error.
  85. _, err = a.Dial(ctx, p2p.Endpoint{})
  86. require.Error(t, err)
  87. // Dialing without protocol should error.
  88. noProtocol := endpoint
  89. noProtocol.Protocol = ""
  90. _, err = a.Dial(ctx, noProtocol)
  91. require.Error(t, err)
  92. // Dialing with invalid protocol should error.
  93. fooProtocol := endpoint
  94. fooProtocol.Protocol = "foo"
  95. _, err = a.Dial(ctx, fooProtocol)
  96. require.Error(t, err)
  97. // Tests for networked endpoints (with IP).
  98. if len(endpoint.IP) > 0 && endpoint.Protocol != p2p.MemoryProtocol {
  99. for _, tc := range ipTestCases {
  100. tc := tc
  101. t.Run(tc.ip.String(), func(t *testing.T) {
  102. e := endpoint
  103. e.IP = tc.ip
  104. conn, err := a.Dial(ctx, e)
  105. if tc.ok {
  106. require.NoError(t, conn.Close())
  107. require.NoError(t, err)
  108. } else {
  109. require.Error(t, err, "endpoint=%s", e)
  110. }
  111. })
  112. }
  113. // Non-networked endpoints should error.
  114. noIP := endpoint
  115. noIP.IP = nil
  116. noIP.Port = 0
  117. noIP.Path = "foo"
  118. _, err := a.Dial(ctx, noIP)
  119. require.Error(t, err)
  120. } else {
  121. // Tests for non-networked endpoints (no IP).
  122. noPath := endpoint
  123. noPath.Path = ""
  124. _, err = a.Dial(ctx, noPath)
  125. require.Error(t, err)
  126. }
  127. })
  128. }
  129. func TestTransport_Dial(t *testing.T) {
  130. // Most just tests dial failures, happy path is tested widely elsewhere.
  131. withTransports(t, func(t *testing.T, makeTransport transportFactory) {
  132. a := makeTransport(t)
  133. b := makeTransport(t)
  134. require.NotEmpty(t, a.Endpoints())
  135. require.NotEmpty(t, b.Endpoints())
  136. aEndpoint := a.Endpoints()[0]
  137. bEndpoint := b.Endpoints()[0]
  138. // Context cancellation should error. We can't test timeouts since we'd
  139. // need a non-responsive endpoint.
  140. cancelCtx, cancel := context.WithCancel(ctx)
  141. cancel()
  142. _, err := a.Dial(cancelCtx, bEndpoint)
  143. require.Error(t, err)
  144. require.Equal(t, err, context.Canceled)
  145. // Unavailable endpoint should error.
  146. err = b.Close()
  147. require.NoError(t, err)
  148. _, err = a.Dial(ctx, bEndpoint)
  149. require.Error(t, err)
  150. // Dialing from a closed transport should still work.
  151. errCh := make(chan error, 1)
  152. go func() {
  153. conn, err := a.Accept()
  154. if err == nil {
  155. _ = conn.Close()
  156. }
  157. errCh <- err
  158. }()
  159. conn, err := b.Dial(ctx, aEndpoint)
  160. require.NoError(t, err)
  161. require.NoError(t, conn.Close())
  162. require.NoError(t, <-errCh)
  163. })
  164. }
  165. func TestTransport_Endpoints(t *testing.T) {
  166. withTransports(t, func(t *testing.T, makeTransport transportFactory) {
  167. a := makeTransport(t)
  168. b := makeTransport(t)
  169. // Both transports return valid and different endpoints.
  170. aEndpoints := a.Endpoints()
  171. bEndpoints := b.Endpoints()
  172. require.NotEmpty(t, aEndpoints)
  173. require.NotEmpty(t, bEndpoints)
  174. require.NotEqual(t, aEndpoints, bEndpoints)
  175. for _, endpoint := range append(aEndpoints, bEndpoints...) {
  176. err := endpoint.Validate()
  177. require.NoError(t, err, "invalid endpoint %q", endpoint)
  178. }
  179. // When closed, the transport should no longer return any endpoints.
  180. err := a.Close()
  181. require.NoError(t, err)
  182. require.Empty(t, a.Endpoints())
  183. require.NotEmpty(t, b.Endpoints())
  184. })
  185. }
  186. func TestTransport_Protocols(t *testing.T) {
  187. withTransports(t, func(t *testing.T, makeTransport transportFactory) {
  188. a := makeTransport(t)
  189. protocols := a.Protocols()
  190. endpoints := a.Endpoints()
  191. require.NotEmpty(t, protocols)
  192. require.NotEmpty(t, endpoints)
  193. for _, endpoint := range endpoints {
  194. require.Contains(t, protocols, endpoint.Protocol)
  195. }
  196. })
  197. }
  198. func TestTransport_String(t *testing.T) {
  199. withTransports(t, func(t *testing.T, makeTransport transportFactory) {
  200. a := makeTransport(t)
  201. require.NotEmpty(t, a.String())
  202. })
  203. }
  204. func TestConnection_Handshake(t *testing.T) {
  205. withTransports(t, func(t *testing.T, makeTransport transportFactory) {
  206. a := makeTransport(t)
  207. b := makeTransport(t)
  208. ab, ba := dialAccept(t, a, b)
  209. // A handshake should pass the given keys and NodeInfo.
  210. aKey := ed25519.GenPrivKey()
  211. aInfo := types.NodeInfo{
  212. NodeID: types.NodeIDFromPubKey(aKey.PubKey()),
  213. ProtocolVersion: types.ProtocolVersion{
  214. P2P: 1,
  215. Block: 2,
  216. App: 3,
  217. },
  218. ListenAddr: "listenaddr",
  219. Network: "network",
  220. Version: "1.2.3",
  221. Channels: bytes.HexBytes([]byte{0xf0, 0x0f}),
  222. Moniker: "moniker",
  223. Other: types.NodeInfoOther{
  224. TxIndex: "txindex",
  225. RPCAddress: "rpc.domain.com",
  226. },
  227. }
  228. bKey := ed25519.GenPrivKey()
  229. bInfo := types.NodeInfo{NodeID: types.NodeIDFromPubKey(bKey.PubKey())}
  230. errCh := make(chan error, 1)
  231. go func() {
  232. // Must use assert due to goroutine.
  233. peerInfo, peerKey, err := ba.Handshake(ctx, bInfo, bKey)
  234. if err == nil {
  235. assert.Equal(t, aInfo, peerInfo)
  236. assert.Equal(t, aKey.PubKey(), peerKey)
  237. }
  238. errCh <- err
  239. }()
  240. peerInfo, peerKey, err := ab.Handshake(ctx, aInfo, aKey)
  241. require.NoError(t, err)
  242. require.Equal(t, bInfo, peerInfo)
  243. require.Equal(t, bKey.PubKey(), peerKey)
  244. require.NoError(t, <-errCh)
  245. })
  246. }
  247. func TestConnection_HandshakeCancel(t *testing.T) {
  248. withTransports(t, func(t *testing.T, makeTransport transportFactory) {
  249. a := makeTransport(t)
  250. b := makeTransport(t)
  251. // Handshake should error on context cancellation.
  252. ab, ba := dialAccept(t, a, b)
  253. timeoutCtx, cancel := context.WithTimeout(ctx, 1*time.Minute)
  254. cancel()
  255. _, _, err := ab.Handshake(timeoutCtx, types.NodeInfo{}, ed25519.GenPrivKey())
  256. require.Error(t, err)
  257. require.Equal(t, context.Canceled, err)
  258. _ = ab.Close()
  259. _ = ba.Close()
  260. // Handshake should error on context timeout.
  261. ab, ba = dialAccept(t, a, b)
  262. timeoutCtx, cancel = context.WithTimeout(ctx, 200*time.Millisecond)
  263. defer cancel()
  264. _, _, err = ab.Handshake(timeoutCtx, types.NodeInfo{}, ed25519.GenPrivKey())
  265. require.Error(t, err)
  266. require.Equal(t, context.DeadlineExceeded, err)
  267. _ = ab.Close()
  268. _ = ba.Close()
  269. })
  270. }
  271. func TestConnection_FlushClose(t *testing.T) {
  272. withTransports(t, func(t *testing.T, makeTransport transportFactory) {
  273. a := makeTransport(t)
  274. b := makeTransport(t)
  275. ab, _ := dialAcceptHandshake(t, a, b)
  276. err := ab.Close()
  277. require.NoError(t, err)
  278. _, _, err = ab.ReceiveMessage()
  279. require.Error(t, err)
  280. require.Equal(t, io.EOF, err)
  281. err = ab.SendMessage(chID, []byte("closed"))
  282. require.Error(t, err)
  283. require.Equal(t, io.EOF, err)
  284. })
  285. }
  286. func TestConnection_LocalRemoteEndpoint(t *testing.T) {
  287. withTransports(t, func(t *testing.T, makeTransport transportFactory) {
  288. a := makeTransport(t)
  289. b := makeTransport(t)
  290. ab, ba := dialAcceptHandshake(t, a, b)
  291. // Local and remote connection endpoints correspond to each other.
  292. require.NotEmpty(t, ab.LocalEndpoint())
  293. require.NotEmpty(t, ba.LocalEndpoint())
  294. require.Equal(t, ab.LocalEndpoint(), ba.RemoteEndpoint())
  295. require.Equal(t, ab.RemoteEndpoint(), ba.LocalEndpoint())
  296. })
  297. }
  298. func TestConnection_SendReceive(t *testing.T) {
  299. withTransports(t, func(t *testing.T, makeTransport transportFactory) {
  300. a := makeTransport(t)
  301. b := makeTransport(t)
  302. ab, ba := dialAcceptHandshake(t, a, b)
  303. // Can send and receive a to b.
  304. err := ab.SendMessage(chID, []byte("foo"))
  305. require.NoError(t, err)
  306. ch, msg, err := ba.ReceiveMessage()
  307. require.NoError(t, err)
  308. require.Equal(t, []byte("foo"), msg)
  309. require.Equal(t, chID, ch)
  310. // Can send and receive b to a.
  311. err = ba.SendMessage(chID, []byte("bar"))
  312. require.NoError(t, err)
  313. _, msg, err = ab.ReceiveMessage()
  314. require.NoError(t, err)
  315. require.Equal(t, []byte("bar"), msg)
  316. // Connections should still be active after closing the transports.
  317. err = a.Close()
  318. require.NoError(t, err)
  319. err = b.Close()
  320. require.NoError(t, err)
  321. err = ab.SendMessage(chID, []byte("still here"))
  322. require.NoError(t, err)
  323. ch, msg, err = ba.ReceiveMessage()
  324. require.NoError(t, err)
  325. require.Equal(t, chID, ch)
  326. require.Equal(t, []byte("still here"), msg)
  327. // Close one side of the connection. Both sides should then error
  328. // with io.EOF when trying to send or receive.
  329. err = ba.Close()
  330. require.NoError(t, err)
  331. _, _, err = ab.ReceiveMessage()
  332. require.Error(t, err)
  333. require.Equal(t, io.EOF, err)
  334. err = ab.SendMessage(chID, []byte("closed"))
  335. require.Error(t, err)
  336. require.Equal(t, io.EOF, err)
  337. _, _, err = ba.ReceiveMessage()
  338. require.Error(t, err)
  339. require.Equal(t, io.EOF, err)
  340. err = ba.SendMessage(chID, []byte("closed"))
  341. require.Error(t, err)
  342. require.Equal(t, io.EOF, err)
  343. })
  344. }
  345. func TestConnection_String(t *testing.T) {
  346. withTransports(t, func(t *testing.T, makeTransport transportFactory) {
  347. a := makeTransport(t)
  348. b := makeTransport(t)
  349. ab, _ := dialAccept(t, a, b)
  350. require.NotEmpty(t, ab.String())
  351. })
  352. }
  353. func TestEndpoint_NodeAddress(t *testing.T) {
  354. var (
  355. ip4 = []byte{1, 2, 3, 4}
  356. ip4in6 = net.IPv4(1, 2, 3, 4)
  357. ip6 = []byte{0xb1, 0x0c, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01}
  358. id = types.NodeID("00112233445566778899aabbccddeeff00112233")
  359. )
  360. testcases := []struct {
  361. endpoint p2p.Endpoint
  362. expect p2p.NodeAddress
  363. }{
  364. // Valid endpoints.
  365. {
  366. p2p.Endpoint{Protocol: "tcp", IP: ip4, Port: 8080, Path: "path"},
  367. p2p.NodeAddress{Protocol: "tcp", Hostname: "1.2.3.4", Port: 8080, Path: "path"},
  368. },
  369. {
  370. p2p.Endpoint{Protocol: "tcp", IP: ip4in6, Port: 8080, Path: "path"},
  371. p2p.NodeAddress{Protocol: "tcp", Hostname: "1.2.3.4", Port: 8080, Path: "path"},
  372. },
  373. {
  374. p2p.Endpoint{Protocol: "tcp", IP: ip6, Port: 8080, Path: "path"},
  375. p2p.NodeAddress{Protocol: "tcp", Hostname: "b10c::1", Port: 8080, Path: "path"},
  376. },
  377. {
  378. p2p.Endpoint{Protocol: "memory", Path: "foo"},
  379. p2p.NodeAddress{Protocol: "memory", Path: "foo"},
  380. },
  381. {
  382. p2p.Endpoint{Protocol: "memory", Path: string(id)},
  383. p2p.NodeAddress{Protocol: "memory", Path: string(id)},
  384. },
  385. // Partial (invalid) endpoints.
  386. {p2p.Endpoint{}, p2p.NodeAddress{}},
  387. {p2p.Endpoint{Protocol: "tcp"}, p2p.NodeAddress{Protocol: "tcp"}},
  388. {p2p.Endpoint{IP: net.IPv4(1, 2, 3, 4)}, p2p.NodeAddress{Hostname: "1.2.3.4"}},
  389. {p2p.Endpoint{Port: 8080}, p2p.NodeAddress{}},
  390. {p2p.Endpoint{Path: "path"}, p2p.NodeAddress{Path: "path"}},
  391. }
  392. for _, tc := range testcases {
  393. tc := tc
  394. t.Run(tc.endpoint.String(), func(t *testing.T) {
  395. // Without NodeID.
  396. expect := tc.expect
  397. require.Equal(t, expect, tc.endpoint.NodeAddress(""))
  398. // With NodeID.
  399. expect.NodeID = id
  400. require.Equal(t, expect, tc.endpoint.NodeAddress(expect.NodeID))
  401. })
  402. }
  403. }
  404. func TestEndpoint_String(t *testing.T) {
  405. var (
  406. ip4 = []byte{1, 2, 3, 4}
  407. ip4in6 = net.IPv4(1, 2, 3, 4)
  408. ip6 = []byte{0xb1, 0x0c, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01}
  409. nodeID = types.NodeID("00112233445566778899aabbccddeeff00112233")
  410. )
  411. testcases := []struct {
  412. endpoint p2p.Endpoint
  413. expect string
  414. }{
  415. // Non-networked endpoints.
  416. {p2p.Endpoint{Protocol: "memory", Path: string(nodeID)}, "memory:" + string(nodeID)},
  417. {p2p.Endpoint{Protocol: "file", Path: "foo"}, "file:///foo"},
  418. {p2p.Endpoint{Protocol: "file", Path: "👋"}, "file:///%F0%9F%91%8B"},
  419. // IPv4 endpoints.
  420. {p2p.Endpoint{Protocol: "tcp", IP: ip4}, "tcp://1.2.3.4"},
  421. {p2p.Endpoint{Protocol: "tcp", IP: ip4in6}, "tcp://1.2.3.4"},
  422. {p2p.Endpoint{Protocol: "tcp", IP: ip4, Port: 8080}, "tcp://1.2.3.4:8080"},
  423. {p2p.Endpoint{Protocol: "tcp", IP: ip4, Port: 8080, Path: "/path"}, "tcp://1.2.3.4:8080/path"},
  424. {p2p.Endpoint{Protocol: "tcp", IP: ip4, Path: "path/👋"}, "tcp://1.2.3.4/path/%F0%9F%91%8B"},
  425. // IPv6 endpoints.
  426. {p2p.Endpoint{Protocol: "tcp", IP: ip6}, "tcp://b10c::1"},
  427. {p2p.Endpoint{Protocol: "tcp", IP: ip6, Port: 8080}, "tcp://[b10c::1]:8080"},
  428. {p2p.Endpoint{Protocol: "tcp", IP: ip6, Port: 8080, Path: "/path"}, "tcp://[b10c::1]:8080/path"},
  429. {p2p.Endpoint{Protocol: "tcp", IP: ip6, Path: "path/👋"}, "tcp://b10c::1/path/%F0%9F%91%8B"},
  430. // Partial (invalid) endpoints.
  431. {p2p.Endpoint{}, ""},
  432. {p2p.Endpoint{Protocol: "tcp"}, "tcp:"},
  433. {p2p.Endpoint{IP: []byte{1, 2, 3, 4}}, "1.2.3.4"},
  434. {p2p.Endpoint{IP: []byte{0xb1, 0x0c, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01}}, "b10c::1"},
  435. {p2p.Endpoint{Port: 8080}, ""},
  436. {p2p.Endpoint{Path: "foo"}, "/foo"},
  437. }
  438. for _, tc := range testcases {
  439. tc := tc
  440. t.Run(tc.expect, func(t *testing.T) {
  441. require.Equal(t, tc.expect, tc.endpoint.String())
  442. })
  443. }
  444. }
  445. func TestEndpoint_Validate(t *testing.T) {
  446. var (
  447. ip4 = []byte{1, 2, 3, 4}
  448. ip4in6 = net.IPv4(1, 2, 3, 4)
  449. ip6 = []byte{0xb1, 0x0c, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01}
  450. )
  451. testcases := []struct {
  452. endpoint p2p.Endpoint
  453. expectValid bool
  454. }{
  455. // Valid endpoints.
  456. {p2p.Endpoint{Protocol: "tcp", IP: ip4}, true},
  457. {p2p.Endpoint{Protocol: "tcp", IP: ip4in6}, true},
  458. {p2p.Endpoint{Protocol: "tcp", IP: ip6}, true},
  459. {p2p.Endpoint{Protocol: "tcp", IP: ip4, Port: 8008}, true},
  460. {p2p.Endpoint{Protocol: "tcp", IP: ip4, Port: 8080, Path: "path"}, true},
  461. {p2p.Endpoint{Protocol: "memory", Path: "path"}, true},
  462. // Invalid endpoints.
  463. {p2p.Endpoint{}, false},
  464. {p2p.Endpoint{IP: ip4}, false},
  465. {p2p.Endpoint{Protocol: "tcp"}, false},
  466. {p2p.Endpoint{Protocol: "tcp", IP: []byte{1, 2, 3}}, false},
  467. {p2p.Endpoint{Protocol: "tcp", Port: 8080, Path: "path"}, false},
  468. }
  469. for _, tc := range testcases {
  470. tc := tc
  471. t.Run(tc.endpoint.String(), func(t *testing.T) {
  472. err := tc.endpoint.Validate()
  473. if tc.expectValid {
  474. require.NoError(t, err)
  475. } else {
  476. require.Error(t, err)
  477. }
  478. })
  479. }
  480. }
  481. // dialAccept is a helper that dials b from a and returns both sides of the
  482. // connection.
  483. func dialAccept(t *testing.T, a, b p2p.Transport) (p2p.Connection, p2p.Connection) {
  484. t.Helper()
  485. endpoints := b.Endpoints()
  486. require.NotEmpty(t, endpoints, "peer not listening on any endpoints")
  487. ctx, cancel := context.WithTimeout(ctx, time.Second)
  488. defer cancel()
  489. acceptCh := make(chan p2p.Connection, 1)
  490. errCh := make(chan error, 1)
  491. go func() {
  492. conn, err := b.Accept()
  493. errCh <- err
  494. acceptCh <- conn
  495. }()
  496. dialConn, err := a.Dial(ctx, endpoints[0])
  497. require.NoError(t, err)
  498. acceptConn := <-acceptCh
  499. require.NoError(t, <-errCh)
  500. t.Cleanup(func() {
  501. _ = dialConn.Close()
  502. _ = acceptConn.Close()
  503. })
  504. return dialConn, acceptConn
  505. }
  506. // dialAcceptHandshake is a helper that dials and handshakes b from a and
  507. // returns both sides of the connection.
  508. func dialAcceptHandshake(t *testing.T, a, b p2p.Transport) (p2p.Connection, p2p.Connection) {
  509. t.Helper()
  510. ab, ba := dialAccept(t, a, b)
  511. ctx, cancel := context.WithTimeout(ctx, time.Second)
  512. defer cancel()
  513. errCh := make(chan error, 1)
  514. go func() {
  515. privKey := ed25519.GenPrivKey()
  516. nodeInfo := types.NodeInfo{NodeID: types.NodeIDFromPubKey(privKey.PubKey())}
  517. _, _, err := ba.Handshake(ctx, nodeInfo, privKey)
  518. errCh <- err
  519. }()
  520. privKey := ed25519.GenPrivKey()
  521. nodeInfo := types.NodeInfo{NodeID: types.NodeIDFromPubKey(privKey.PubKey())}
  522. _, _, err := ab.Handshake(ctx, nodeInfo, privKey)
  523. require.NoError(t, err)
  524. timer := time.NewTimer(2 * time.Second)
  525. defer timer.Stop()
  526. select {
  527. case err := <-errCh:
  528. require.NoError(t, err)
  529. case <-timer.C:
  530. require.Fail(t, "handshake timed out")
  531. }
  532. return ab, ba
  533. }