github.com/elfadel/cilium@v1.6.12/pkg/fqdn/matchpattern/matchpattern_test.go (about) 1 // Copyright 2018 Authors of Cilium 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // +build !privileged_tests 16 17 package matchpattern 18 19 import ( 20 "regexp" 21 "testing" 22 23 . "gopkg.in/check.v1" 24 ) 25 26 // Hook up gocheck into the "go test" runner. 27 func Test(t *testing.T) { 28 TestingT(t) 29 } 30 31 type MatchPatternTestSuite struct{} 32 33 var _ = Suite(&MatchPatternTestSuite{}) 34 35 // TestMatchPatternREConversion tests that we can validate and convert a 36 // matchPattern to a compilable regexp. 37 // It tests: 38 // cilium.io. -> cilium[.]io[.] 39 // *.cilium.io. -> [-a-zA-Z0-9]+.cilium[.]io[.] 40 // *cilium.io. -> "([a-zA-Z0-9]+[.])?cilium[.]io[.] 41 func (ts *MatchPatternTestSuite) TestMatchPatternREConversion(c *C) { 42 for source, target := range map[string]string{ 43 "cilium.io.": "^cilium[.]io[.]$", 44 "*.cilium.io.": "^" + allowedDNSCharsREGroup + "*[.]cilium[.]io[.]$", 45 "*": "(^(" + allowedDNSCharsREGroup + "+[.])+$)|(^[.]$)", 46 ".": "^[.]$", 47 } { 48 reStr := ToRegexp(source) 49 _, err := regexp.Compile(reStr) 50 c.Assert(err, IsNil, Commentf("Regexp generated from pattern %sis not valid", source)) 51 c.Assert(reStr, Equals, target, Commentf("Regexp generated from pattern %s isn't expected", source)) 52 } 53 } 54 55 // TestMatchPatternMatching tests that patterns actually match what we expect: 56 // cilium.io. matches only cilium.io. 57 // *.cilium.io. matches anysub.cilium.io. but not cilium.io. 58 // *cilium.io. matches anysub.cilium.io. and cilium.io. 59 // *.ci*.io. matches anysub.cilium.io. anysub.ci.io., anysub.ciliumandmore.io. but not cilium.io. 60 func (ts *MatchPatternTestSuite) TestMatchPatternMatching(c *C) { 61 for _, testCase := range []struct { 62 pattern string 63 accept []string 64 reject []string 65 }{ 66 { 67 pattern: "cilium.io.", 68 accept: []string{"cilium.io."}, 69 reject: []string{"", "anysub.cilium.io.", "anysub.ci.io.", "anysub.ciliumandmore.io."}, 70 }, 71 { 72 pattern: "*.cilium.io.", 73 accept: []string{"anysub.cilium.io."}, 74 reject: []string{"", "cilium.io.", "anysub.ci.io.", "anysub.ciliumandmore.io."}, 75 }, 76 { 77 pattern: "*.ci*.io.", 78 accept: []string{"anysub.cilium.io.", "anysub.ci.io.", "anysub.ciliumandmore.io."}, 79 reject: []string{"", "cilium.io."}, 80 }, 81 { 82 pattern: "*", 83 accept: []string{".", "io.", "cilium.io.", "svc.cluster.local.", "service.namesace.svc.cluster.local.", "_foobar._tcp.cilium.io."}, // the last is for SRV RFC-2782 and DNS-SD RFC6763 84 reject: []string{"", ".io.", ".cilium.io.", ".svc.cluster.local.", "cilium.io"}, // note no final . on this last one 85 }, 86 { 87 pattern: ".", 88 accept: []string{"."}, 89 reject: []string{"", ".io.", ".cilium.io"}, 90 }, 91 92 // These are more explicit tests for SRV RFC-2782 and DNS-SD RFC6763 93 { 94 pattern: "_foobar._tcp.cilium.io.", 95 accept: []string{"_foobar._tcp.cilium.io."}, 96 reject: []string{"", "_tcp.cilium.io.", "cilium.io."}, 97 }, 98 { 99 pattern: "*.*.cilium.io.", 100 accept: []string{"_foobar._tcp.cilium.io."}, 101 reject: []string{""}, 102 }, 103 } { 104 reStr := ToRegexp(testCase.pattern) 105 re, err := regexp.Compile(reStr) 106 c.Assert(err, IsNil, Commentf("Regexp generated from pattern is not valid")) 107 for _, accept := range testCase.accept { 108 c.Assert(re.MatchString(accept), Equals, true, Commentf("Regexp generated from pattern %s/%s rejected a correct DNS name %s", testCase.pattern, re, accept)) 109 } 110 for _, reject := range testCase.reject { 111 c.Assert(re.MatchString(reject), Equals, false, Commentf("Regexp generated from pattern %s/%s accepted a bad DNS name %s", testCase.pattern, re, reject)) 112 } 113 } 114 } 115 116 // TestMatchPatternSanitize tests that Sanitize handles any special cases 117 func (ts *MatchPatternTestSuite) TestMatchPatternSanitize(c *C) { 118 for source, target := range map[string]string{ 119 "*": "*", 120 "*.": "*.", 121 "*.com": "*.com.", 122 } { 123 sanitized := Sanitize(source) 124 c.Assert(sanitized, Equals, target, Commentf("matchPattern: %s not sanitized correctly", source)) 125 } 126 }