diff --git a/irc.go b/irc.go index 2b923a9..4ba006e 100644 --- a/irc.go +++ b/irc.go @@ -26,6 +26,7 @@ import ( "log" "net" "os" + "strconv" "strings" "time" ) @@ -341,6 +342,15 @@ func (irc *Connection) Connect(server string) error { if (len(irc.server) - 1) == strings.Index(irc.server, ":") { return errors.New("port missing") } + // check for valid range + ports := strings.Split(irc.server, ":")[1] + port, err := strconv.Atoi(ports) + if nil != err { + return errors.New("extracting port failed") + } + if !((port >= 0) && (port <= 65535)) { + return errors.New("port number outside valid range") + } if nil == irc.Log { return errors.New("'Log' points to nil") } @@ -351,7 +361,7 @@ func (irc *Connection) Connect(server string) error { return errors.New("empty 'user'") } - var err error +// var err error if irc.UseTLS { if irc.netsock, err = net.DialTimeout("tcp", irc.server, irc.Timeout); err == nil { irc.socket = tls.Client(irc.netsock, irc.TLSConfig) diff --git a/irc_test.go b/irc_test.go index 1a8b58c..ecef112 100644 --- a/irc_test.go +++ b/irc_test.go @@ -12,6 +12,7 @@ func TestConnection(t *testing.T) { irccon.Debug = true err := irccon.Connect("irc.freenode.net:6667") if err != nil { + t.Log(err.Error()) t.Fatal("Can't connect to freenode.") } irccon.AddCallback("001", func(e *Event) { irccon.Join("#go-eventirc") }) @@ -36,6 +37,7 @@ func TestConnectionSSL(t *testing.T) { irccon.UseTLS = true err := irccon.Connect("irc.freenode.net:7000") if err != nil { + t.Log(err.Error()) t.Fatal("Can't connect to freenode.") } irccon.AddCallback("001", func(e *Event) { irccon.Join("#go-eventirc") }) @@ -81,6 +83,22 @@ func TestConnectionMissingPort(t *testing.T) { } } +func TestConnectionNegativePort(t *testing.T) { + irccon := IRC("go-eventirc", "go-eventirc") + err := irccon.Connect("chat.freenode.net:-1") + if err == nil { + t.Fatal("negative port number not detected") + } +} + +func TestConnectionTooLargePort(t *testing.T) { + irccon := IRC("go-eventirc", "go-eventirc") + err := irccon.Connect("chat.freenode.net:65536") + if err == nil { + t.Fatal("too large port number not detected") + } +} + func TestConnectionMissingLog(t *testing.T) { irccon := IRC("go-eventirc", "go-eventirc") irccon.Log = nil