Fix parsing of proto/port

Signed-off-by: Doug Davis <dug@us.ibm.com>
This commit is contained in:
Doug Davis 2014-09-11 16:32:09 -07:00
parent ed7fb3bbda
commit 658a9d0f47
2 changed files with 28 additions and 12 deletions

View File

@ -61,21 +61,25 @@ func (p Port) Int() int {
return i
}
// Splits a port in the format of port/proto
// Splits a port in the format of proto/port
func SplitProtoPort(rawPort string) (string, string) {
parts := strings.Split(rawPort, "/")
l := len(parts)
if l == 0 {
return "", ""
}
if l == 1 {
if rawPort == "" {
return "", "" // ""/tcp is not valid, ever
}
var port string
var proto string
return "tcp", rawPort
parts := strings.Split(rawPort, "/")
if len(parts) == 0 || parts[0] == "" { // we have "" or ""/
port = ""
proto = ""
} else { // we have # or #/ or #/...
port = parts[0]
if len(parts) > 1 && parts[1] != "" {
proto = parts[1] // we have #/...
} else {
proto = "tcp" // we have # or #/
}
}
return parts[1], parts[0]
return proto, port
}
func validateProto(proto string) bool {

View File

@ -84,6 +84,18 @@ func TestSplitProtoPort(t *testing.T) {
if proto != "tcp" || port != "1234" {
t.Fatal("tcp is not the default protocol for portspec '1234'")
}
proto, port = SplitProtoPort("1234/")
if proto != "tcp" || port != "1234" {
t.Fatal("parsing '1234/' yielded:" + port + "/" + proto)
}
proto, port = SplitProtoPort("/tcp")
if proto != "" || port != "" {
t.Fatal("parsing '/tcp' yielded:" + port + "/" + proto)
}
}
func TestParsePortSpecs(t *testing.T) {