diff --git a/network/config.go b/network/config.go index 04b259976..ffd545cac 100644 --- a/network/config.go +++ b/network/config.go @@ -4,6 +4,7 @@ import ( "fmt" "time" + "github.com/multiformats/go-multiaddr" "github.com/pactus-project/pactus/util/errors" ) @@ -61,6 +62,16 @@ func DefaultConfig() *Config { } } +func validateAddresses(address []string) error { + for _, addr := range address { + _, err := multiaddr.NewMultiaddr(addr) + if err != nil { + return err + } + } + return nil +} + // SanityCheck performs basic checks on the configuration. func (conf *Config) SanityCheck() error { if conf.EnableRelay { @@ -68,5 +79,8 @@ func (conf *Config) SanityCheck() error { return errors.Errorf(errors.ErrInvalidConfig, "at least one relay address should be defined") } } - return nil + if err := validateAddresses(conf.RelayAddrs); err != nil { + return err + } + return validateAddresses(conf.Listens) } diff --git a/network/config_test.go b/network/config_test.go index b1f267f95..6dad6b65a 100644 --- a/network/config_test.go +++ b/network/config_test.go @@ -6,12 +6,30 @@ import ( "github.com/stretchr/testify/assert" ) -func TestConf(t *testing.T) { +func TestDefaultConfigCheck(t *testing.T) { conf := DefaultConfig() conf.EnableRelay = true assert.Error(t, conf.SanityCheck()) - conf.EnableRelay = false + conf.Listens = []string{""} + assert.Error(t, conf.SanityCheck()) + + conf.Listens = []string{"127.0.0.1"} + assert.Error(t, conf.SanityCheck()) + + conf.Listens = []string{"/ip4"} + assert.Error(t, conf.SanityCheck()) + + conf.RelayAddrs = []string{"/ip4"} + assert.Error(t, conf.SanityCheck()) + + conf.RelayAddrs = []string{} + conf.Listens = []string{} + + conf.RelayAddrs = []string{"/ip4/127.0.0.1"} + assert.NoError(t, conf.SanityCheck()) + + conf.Listens = []string{"/ip4/127.0.0.1"} assert.NoError(t, conf.SanityCheck()) }