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.

81 lines
1.8 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. err := sw.Start()
  45. require.NoError(t, err)
  46. t.Cleanup(func() {
  47. if err := sw.Stop(); err != nil {
  48. t.Error(err)
  49. }
  50. })
  51. env.Logger = log.TestingLogger()
  52. env.P2PPeers = sw
  53. testCases := []struct {
  54. peers []string
  55. isErr bool
  56. }{
  57. {[]string{}, true},
  58. {[]string{"d51fb70907db1c6c2d5237e78379b25cf1a37ab4@127.0.0.1:41198"}, false},
  59. {[]string{"127.0.0.1:41198"}, true},
  60. }
  61. for _, tc := range testCases {
  62. res, err := UnsafeDialPeers(&rpctypes.Context{}, tc.peers, false)
  63. if tc.isErr {
  64. assert.Error(t, err)
  65. } else {
  66. assert.NoError(t, err)
  67. assert.NotNil(t, res)
  68. }
  69. }
  70. }