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.

87 lines
2.2 KiB

  1. package core
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/require"
  6. cfg "github.com/tendermint/tendermint/config"
  7. "github.com/tendermint/tendermint/libs/log"
  8. "github.com/tendermint/tendermint/p2p"
  9. rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types"
  10. )
  11. func TestUnsafeDialSeeds(t *testing.T) {
  12. sw := p2p.MakeSwitch(cfg.DefaultP2PConfig(), 1, "testing", "123.123.123",
  13. func(n int, sw *p2p.Switch) *p2p.Switch { return sw })
  14. err := sw.Start()
  15. require.NoError(t, err)
  16. t.Cleanup(func() {
  17. if err := sw.Stop(); err != nil {
  18. t.Error(err)
  19. }
  20. })
  21. env.Logger = log.TestingLogger()
  22. env.P2PPeers = sw
  23. testCases := []struct {
  24. seeds []string
  25. isErr bool
  26. }{
  27. {[]string{}, true},
  28. {[]string{"d51fb70907db1c6c2d5237e78379b25cf1a37ab4@127.0.0.1:41198"}, false},
  29. {[]string{"127.0.0.1:41198"}, true},
  30. }
  31. for _, tc := range testCases {
  32. res, err := UnsafeDialSeeds(&rpctypes.Context{}, tc.seeds)
  33. if tc.isErr {
  34. assert.Error(t, err)
  35. } else {
  36. assert.NoError(t, err)
  37. assert.NotNil(t, res)
  38. }
  39. }
  40. }
  41. func TestUnsafeDialPeers(t *testing.T) {
  42. sw := p2p.MakeSwitch(cfg.DefaultP2PConfig(), 1, "testing", "123.123.123",
  43. func(n int, sw *p2p.Switch) *p2p.Switch { return sw })
  44. sw.SetAddrBook(&p2p.AddrBookMock{
  45. Addrs: make(map[string]struct{}),
  46. OurAddrs: make(map[string]struct{}),
  47. PrivateAddrs: make(map[string]struct{}),
  48. })
  49. err := sw.Start()
  50. require.NoError(t, err)
  51. t.Cleanup(func() {
  52. if err := sw.Stop(); err != nil {
  53. t.Error(err)
  54. }
  55. })
  56. env.Logger = log.TestingLogger()
  57. env.P2PPeers = sw
  58. testCases := []struct {
  59. peers []string
  60. persistence, unconditional, private bool
  61. isErr bool
  62. }{
  63. {[]string{}, false, false, false, true},
  64. {[]string{"d51fb70907db1c6c2d5237e78379b25cf1a37ab4@127.0.0.1:41198"}, true, true, true, false},
  65. {[]string{"127.0.0.1:41198"}, true, true, false, true},
  66. }
  67. for _, tc := range testCases {
  68. res, err := UnsafeDialPeers(&rpctypes.Context{}, tc.peers, tc.persistence, tc.unconditional, tc.private)
  69. if tc.isErr {
  70. assert.Error(t, err)
  71. } else {
  72. assert.NoError(t, err)
  73. assert.NotNil(t, res)
  74. }
  75. }
  76. }