istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/config/host/name_test.go (about)

     1  // Copyright Istio Authors
     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  package host_test
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"istio.io/istio/pkg/config/host"
    22  )
    23  
    24  func TestNameMatches(t *testing.T) {
    25  	tests := []struct {
    26  		name string
    27  		a, b host.Name
    28  		out  bool
    29  	}{
    30  		{"empty", "", "", true},
    31  		{"first empty", "", "foo.com", false},
    32  		{"second empty", "foo.com", "", false},
    33  
    34  		{
    35  			"non-wildcard domain",
    36  			"foo.com", "foo.com", true,
    37  		},
    38  		{
    39  			"non-wildcard domain",
    40  			"bar.com", "foo.com", false,
    41  		},
    42  		{
    43  			"non-wildcard domain - order doesn't matter",
    44  			"foo.com", "bar.com", false,
    45  		},
    46  
    47  		{
    48  			"domain does not match subdomain",
    49  			"bar.foo.com", "foo.com", false,
    50  		},
    51  		{
    52  			"domain does not match subdomain - order doesn't matter",
    53  			"foo.com", "bar.foo.com", false,
    54  		},
    55  
    56  		{
    57  			"wildcard matches subdomains",
    58  			"*.com", "foo.com", true,
    59  		},
    60  		{
    61  			"wildcard matches subdomains",
    62  			"*.com", "bar.com", true,
    63  		},
    64  		{
    65  			"wildcard matches subdomains",
    66  			"*.foo.com", "bar.foo.com", true,
    67  		},
    68  
    69  		{"wildcard matches anything", "*", "foo.com", true},
    70  		{"wildcard matches anything", "*", "*.com", true},
    71  		{"wildcard matches anything", "*", "com", true},
    72  		{"wildcard matches anything", "*", "*", true},
    73  		{"wildcard matches anything", "*", "", true},
    74  
    75  		{"wildcarded domain matches wildcarded subdomain", "*.com", "*.foo.com", true},
    76  		{"wildcarded sub-domain does not match domain", "foo.com", "*.foo.com", false},
    77  		{"wildcarded sub-domain does not match domain - order doesn't matter", "*.foo.com", "foo.com", false},
    78  
    79  		{"long wildcard does not match short host", "*.foo.bar.baz", "baz", false},
    80  		{"long wildcard does not match short host - order doesn't matter", "baz", "*.foo.bar.baz", false},
    81  		{"long wildcard matches short wildcard", "*.foo.bar.baz", "*.baz", true},
    82  		{"long name matches short wildcard", "foo.bar.baz", "*.baz", true},
    83  	}
    84  
    85  	for idx, tt := range tests {
    86  		t.Run(fmt.Sprintf("[%d] %s", idx, tt.name), func(t *testing.T) {
    87  			if tt.out != tt.a.Matches(tt.b) {
    88  				t.Fatalf("%q.Matches(%q) = %t wanted %t", tt.a, tt.b, !tt.out, tt.out)
    89  			}
    90  		})
    91  	}
    92  }
    93  
    94  func TestNameSubsetOf(t *testing.T) {
    95  	tests := []struct {
    96  		name string
    97  		a, b host.Name
    98  		out  bool
    99  	}{
   100  		{"empty", "", "", true},
   101  		{"first empty", "", "foo.com", false},
   102  		{"second empty", "foo.com", "", false},
   103  
   104  		{
   105  			"non-wildcard domain",
   106  			"foo.com", "foo.com", true,
   107  		},
   108  		{
   109  			"non-wildcard domain",
   110  			"bar.com", "foo.com", false,
   111  		},
   112  		{
   113  			"non-wildcard domain - order doesn't matter",
   114  			"foo.com", "bar.com", false,
   115  		},
   116  
   117  		{
   118  			"domain does not match subdomain",
   119  			"bar.foo.com", "foo.com", false,
   120  		},
   121  		{
   122  			"domain does not match subdomain - order doesn't matter",
   123  			"foo.com", "bar.foo.com", false,
   124  		},
   125  
   126  		{
   127  			"wildcard matches subdomains",
   128  			"foo.com", "*.com", true,
   129  		},
   130  		{
   131  			"wildcard matches subdomains",
   132  			"bar.com", "*.com", true,
   133  		},
   134  		{
   135  			"wildcard matches subdomains",
   136  			"bar.foo.com", "*.foo.com", true,
   137  		},
   138  
   139  		{"wildcard matches anything", "foo.com", "*", true},
   140  		{"wildcard matches anything", "*.com", "*", true},
   141  		{"wildcard matches anything", "com", "*", true},
   142  		{"wildcard matches anything", "*", "*", true},
   143  		{"wildcard matches anything", "", "*", true},
   144  
   145  		{"wildcarded domain matches wildcarded subdomain", "*.foo.com", "*.com", true},
   146  		{"wildcarded sub-domain does not match domain", "*.foo.com", "foo.com", false},
   147  
   148  		{"long wildcard does not match short host", "*.foo.bar.baz", "baz", false},
   149  		{"long name matches short wildcard", "foo.bar.baz", "*.baz", true},
   150  	}
   151  
   152  	for idx, tt := range tests {
   153  		t.Run(fmt.Sprintf("[%d] %s", idx, tt.name), func(t *testing.T) {
   154  			if tt.out != tt.a.SubsetOf(tt.b) {
   155  				t.Fatalf("%q.SubsetOf(%q) = %t wanted %t", tt.a, tt.b, !tt.out, tt.out)
   156  			}
   157  		})
   158  	}
   159  }
   160  
   161  func BenchmarkNameMatch(b *testing.B) {
   162  	tests := []struct {
   163  		a, z    host.Name
   164  		matches bool
   165  	}{
   166  		{"foo.com", "foo.com", true},
   167  		{"*.com", "foo.com", true},
   168  		{"*.foo.com", "bar.foo.com", true},
   169  		{"*", "foo.com", true},
   170  		{"*", "*.com", true},
   171  		{"*", "", true},
   172  		{"*.com", "*.foo.com", true},
   173  		{"foo.com", "*.foo.com", false},
   174  		{"*.foo.bar.baz", "baz", false},
   175  	}
   176  	for n := 0; n < b.N; n++ {
   177  		for _, test := range tests {
   178  			doesMatch := test.a.Matches(test.z)
   179  			if doesMatch != test.matches {
   180  				b.Fatalf("does not match")
   181  			}
   182  		}
   183  	}
   184  }