Fix parsing of proto/port - issue 6447
| ... | ... |
@@ -61,21 +61,25 @@ func (p Port) Int() int {
|
| 61 | 61 |
return i |
| 62 | 62 |
} |
| 63 | 63 |
|
| 64 |
-// Splits a port in the format of port/proto |
|
| 64 |
+// Splits a port in the format of proto/port |
|
| 65 | 65 |
func SplitProtoPort(rawPort string) (string, string) {
|
| 66 |
+ var port string |
|
| 67 |
+ var proto string |
|
| 68 |
+ |
|
| 66 | 69 |
parts := strings.Split(rawPort, "/") |
| 67 |
- l := len(parts) |
|
| 68 |
- if l == 0 {
|
|
| 69 |
- return "", "" |
|
| 70 |
- } |
|
| 71 |
- if l == 1 {
|
|
| 72 |
- if rawPort == "" {
|
|
| 73 |
- return "", "" // ""/tcp is not valid, ever |
|
| 74 |
- } |
|
| 75 | 70 |
|
| 76 |
- return "tcp", rawPort |
|
| 71 |
+ if len(parts) == 0 || parts[0] == "" { // we have "" or ""/
|
|
| 72 |
+ port = "" |
|
| 73 |
+ proto = "" |
|
| 74 |
+ } else { // we have # or #/ or #/...
|
|
| 75 |
+ port = parts[0] |
|
| 76 |
+ if len(parts) > 1 && parts[1] != "" {
|
|
| 77 |
+ proto = parts[1] // we have #/... |
|
| 78 |
+ } else {
|
|
| 79 |
+ proto = "tcp" // we have # or #/ |
|
| 80 |
+ } |
|
| 77 | 81 |
} |
| 78 |
- return parts[1], parts[0] |
|
| 82 |
+ return proto, port |
|
| 79 | 83 |
} |
| 80 | 84 |
|
| 81 | 85 |
func validateProto(proto string) bool {
|
| ... | ... |
@@ -84,6 +84,18 @@ func TestSplitProtoPort(t *testing.T) {
|
| 84 | 84 |
if proto != "tcp" || port != "1234" {
|
| 85 | 85 |
t.Fatal("tcp is not the default protocol for portspec '1234'")
|
| 86 | 86 |
} |
| 87 |
+ |
|
| 88 |
+ proto, port = SplitProtoPort("1234/")
|
|
| 89 |
+ |
|
| 90 |
+ if proto != "tcp" || port != "1234" {
|
|
| 91 |
+ t.Fatal("parsing '1234/' yielded:" + port + "/" + proto)
|
|
| 92 |
+ } |
|
| 93 |
+ |
|
| 94 |
+ proto, port = SplitProtoPort("/tcp")
|
|
| 95 |
+ |
|
| 96 |
+ if proto != "" || port != "" {
|
|
| 97 |
+ t.Fatal("parsing '/tcp' yielded:" + port + "/" + proto)
|
|
| 98 |
+ } |
|
| 87 | 99 |
} |
| 88 | 100 |
|
| 89 | 101 |
func TestParsePortSpecs(t *testing.T) {
|