Sfoglia il codice sorgente

Fix parsing of proto/port

Signed-off-by: Doug Davis <dug@us.ibm.com>
Doug Davis 11 anni fa
parent
commit
658a9d0f47
2 ha cambiato i file con 27 aggiunte e 11 eliminazioni
  1. 15 11
      nat/nat.go
  2. 12 0
      nat/nat_test.go

+ 15 - 11
nat/nat.go

@@ -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) {
+	var port string
+	var proto string
+
 	parts := strings.Split(rawPort, "/")
-	l := len(parts)
-	if l == 0 {
-		return "", ""
-	}
-	if l == 1 {
-		if rawPort == "" {
-			return "", "" // ""/tcp is not valid, ever
-		}
 
-		return "tcp", 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 {

+ 12 - 0
nat/nat_test.go

@@ -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) {