-
Notifications
You must be signed in to change notification settings - Fork 590
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Mattia Lavacca <lavacca.mattia@gmail.com>
- Loading branch information
Showing
4 changed files
with
169 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package util | ||
|
||
import "strings" | ||
|
||
// TODO: comment exported function and code sections | ||
func Match(pattern, value string) bool { | ||
patternParts := strings.Split(pattern, ".") | ||
valueParts := strings.Split(value, ".") | ||
|
||
var i, j int | ||
var wildcardj, wildcardi bool | ||
for i, j = 0, 0; i < len(patternParts) && j < len(valueParts); i, j = i+1, j+1 { | ||
var matchFound bool | ||
|
||
if wildcardj { | ||
for ; i < len(patternParts); i += 1 { | ||
if patternParts[i] == valueParts[j] { | ||
matchFound = true | ||
break | ||
} | ||
} | ||
if !matchFound { | ||
return false | ||
} | ||
} | ||
|
||
if wildcardi { | ||
for ; j < len(valueParts); j += 1 { | ||
if patternParts[i] == valueParts[j] { | ||
matchFound = true | ||
break | ||
} | ||
} | ||
if !matchFound { | ||
return false | ||
} | ||
} | ||
|
||
if (wildcardj || wildcardi) && (len(valueParts)-j != len(patternParts)-i) { | ||
return false | ||
} | ||
|
||
if patternParts[i] == "*" { | ||
wildcardi = true | ||
continue | ||
} | ||
if valueParts[j] == "*" { | ||
wildcardj = true | ||
continue | ||
} | ||
|
||
wildcardi, wildcardj = false, false | ||
|
||
if patternParts[i] != valueParts[j] { | ||
return false | ||
} | ||
} | ||
if len(valueParts)-j != len(patternParts)-i { | ||
return false | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package util_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/kong/kubernetes-ingress-controller/v2/internal/util" | ||
) | ||
|
||
func TestMatch(t *testing.T) { | ||
for _, tt := range []struct { | ||
name string | ||
pattern string | ||
value string | ||
expected bool | ||
}{ | ||
{ | ||
name: "same hostname", | ||
pattern: "test.com", | ||
value: "test.com", | ||
expected: true, | ||
}, | ||
{ | ||
name: "different hostname suffix", | ||
pattern: "test.com", | ||
value: "test.net", | ||
expected: false, | ||
}, | ||
{ | ||
name: "different hostname prefix", | ||
pattern: "foo.com", | ||
value: "bar.com", | ||
expected: false, | ||
}, | ||
{ | ||
name: "different hostname lengths with only suffix matching", | ||
pattern: "foo.test.com", | ||
value: "test.com", | ||
expected: false, | ||
}, | ||
{ | ||
name: "different hostname lengths with only prefix matching", | ||
pattern: "test.test.com", | ||
value: "test.test", | ||
expected: false, | ||
}, | ||
{ | ||
name: "valid wildcard for one element", | ||
pattern: "*.test.com", | ||
value: "foo.test.com", | ||
expected: true, | ||
}, | ||
{ | ||
name: "valid wildcard for many elements", | ||
pattern: "*.example.com", | ||
value: "so.many.names.example.com", | ||
expected: true, | ||
}, | ||
{ | ||
name: "not matching wildcard", | ||
pattern: "*.example.com", | ||
value: "example.com", | ||
expected: false, | ||
}, | ||
{ | ||
name: "double matching wildcard", | ||
pattern: "*.example.com", | ||
value: "*.example.com", | ||
expected: true, | ||
}, | ||
{ | ||
name: "double not matching wildcard", | ||
pattern: "*.example.com", | ||
value: "*.example.net", | ||
expected: false, | ||
}, | ||
} { | ||
// Test that the functions behave in the same way even swapping the parameters | ||
assert.Equal(t, tt.expected, util.Match(tt.pattern, tt.value), tt.name) | ||
assert.Equal(t, tt.expected, util.Match(tt.value, tt.pattern), tt.name) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters