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.

89 lines
2.3 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/internal/p2p"
  8. "github.com/tendermint/tendermint/libs/log"
  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 }, log.TestingLogger())
  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 := &Environment{}
  22. env.Logger = log.TestingLogger()
  23. env.P2PPeers = sw
  24. testCases := []struct {
  25. seeds []string
  26. isErr bool
  27. }{
  28. {[]string{}, true},
  29. {[]string{"d51fb70907db1c6c2d5237e78379b25cf1a37ab4@127.0.0.1:41198"}, false},
  30. {[]string{"127.0.0.1:41198"}, true},
  31. }
  32. for _, tc := range testCases {
  33. res, err := env.UnsafeDialSeeds(&rpctypes.Context{}, tc.seeds)
  34. if tc.isErr {
  35. assert.Error(t, err)
  36. } else {
  37. assert.NoError(t, err)
  38. assert.NotNil(t, res)
  39. }
  40. }
  41. }
  42. func TestUnsafeDialPeers(t *testing.T) {
  43. sw := p2p.MakeSwitch(cfg.DefaultP2PConfig(), 1, "testing", "123.123.123",
  44. func(n int, sw *p2p.Switch) *p2p.Switch { return sw }, log.TestingLogger())
  45. sw.SetAddrBook(&p2p.AddrBookMock{
  46. Addrs: make(map[string]struct{}),
  47. OurAddrs: make(map[string]struct{}),
  48. PrivateAddrs: make(map[string]struct{}),
  49. })
  50. err := sw.Start()
  51. require.NoError(t, err)
  52. t.Cleanup(func() {
  53. if err := sw.Stop(); err != nil {
  54. t.Error(err)
  55. }
  56. })
  57. env := &Environment{}
  58. env.Logger = log.TestingLogger()
  59. env.P2PPeers = sw
  60. testCases := []struct {
  61. peers []string
  62. persistence, unconditional, private bool
  63. isErr bool
  64. }{
  65. {[]string{}, false, false, false, true},
  66. {[]string{"d51fb70907db1c6c2d5237e78379b25cf1a37ab4@127.0.0.1:41198"}, true, true, true, false},
  67. {[]string{"127.0.0.1:41198"}, true, true, false, true},
  68. }
  69. for _, tc := range testCases {
  70. res, err := env.UnsafeDialPeers(&rpctypes.Context{}, tc.peers, tc.persistence, tc.unconditional, tc.private)
  71. if tc.isErr {
  72. assert.Error(t, err)
  73. } else {
  74. assert.NoError(t, err)
  75. assert.NotNil(t, res)
  76. }
  77. }
  78. }