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.

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