port range checking added

This commit is contained in:
tpltnt 2014-02-16 11:41:38 +01:00
parent 48983c2abf
commit 6e0280dae6
2 changed files with 29 additions and 1 deletions

12
irc.go
View File

@ -26,6 +26,7 @@ import (
"log" "log"
"net" "net"
"os" "os"
"strconv"
"strings" "strings"
"time" "time"
) )
@ -341,6 +342,15 @@ func (irc *Connection) Connect(server string) error {
if (len(irc.server) - 1) == strings.Index(irc.server, ":") { if (len(irc.server) - 1) == strings.Index(irc.server, ":") {
return errors.New("port missing") 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 { if nil == irc.Log {
return errors.New("'Log' points to nil") return errors.New("'Log' points to nil")
} }
@ -351,7 +361,7 @@ func (irc *Connection) Connect(server string) error {
return errors.New("empty 'user'") return errors.New("empty 'user'")
} }
var err error // var err error
if irc.UseTLS { if irc.UseTLS {
if irc.netsock, err = net.DialTimeout("tcp", irc.server, irc.Timeout); err == nil { if irc.netsock, err = net.DialTimeout("tcp", irc.server, irc.Timeout); err == nil {
irc.socket = tls.Client(irc.netsock, irc.TLSConfig) irc.socket = tls.Client(irc.netsock, irc.TLSConfig)

View File

@ -12,6 +12,7 @@ func TestConnection(t *testing.T) {
irccon.Debug = true irccon.Debug = true
err := irccon.Connect("irc.freenode.net:6667") err := irccon.Connect("irc.freenode.net:6667")
if err != nil { if err != nil {
t.Log(err.Error())
t.Fatal("Can't connect to freenode.") t.Fatal("Can't connect to freenode.")
} }
irccon.AddCallback("001", func(e *Event) { irccon.Join("#go-eventirc") }) irccon.AddCallback("001", func(e *Event) { irccon.Join("#go-eventirc") })
@ -36,6 +37,7 @@ func TestConnectionSSL(t *testing.T) {
irccon.UseTLS = true irccon.UseTLS = true
err := irccon.Connect("irc.freenode.net:7000") err := irccon.Connect("irc.freenode.net:7000")
if err != nil { if err != nil {
t.Log(err.Error())
t.Fatal("Can't connect to freenode.") t.Fatal("Can't connect to freenode.")
} }
irccon.AddCallback("001", func(e *Event) { irccon.Join("#go-eventirc") }) 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) { func TestConnectionMissingLog(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc") irccon := IRC("go-eventirc", "go-eventirc")
irccon.Log = nil irccon.Log = nil