sigs.k8s.io/external-dns@v0.14.1/source/gateway_test.go (about)

     1  /*
     2  Copyright 2023 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package source
    18  
    19  import (
    20  	"strings"
    21  	"testing"
    22  
    23  	v1 "sigs.k8s.io/gateway-api/apis/v1"
    24  )
    25  
    26  func TestGatewayMatchingHost(t *testing.T) {
    27  	tests := []struct {
    28  		desc string
    29  		a, b string
    30  		host string
    31  		ok   bool
    32  	}{
    33  		{
    34  			desc: "ipv4-rejected",
    35  			a:    "1.2.3.4",
    36  			ok:   false,
    37  		},
    38  		{
    39  			desc: "ipv6-rejected",
    40  			a:    "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
    41  			ok:   false,
    42  		},
    43  		{
    44  			desc: "empty-matches-empty",
    45  			ok:   true,
    46  		},
    47  		{
    48  			desc: "empty-matches-nonempty",
    49  			a:    "example.net",
    50  			host: "example.net",
    51  			ok:   true,
    52  		},
    53  		{
    54  			desc: "simple-match",
    55  			a:    "example.net",
    56  			b:    "example.net",
    57  			host: "example.net",
    58  			ok:   true,
    59  		},
    60  		{
    61  			desc: "wildcard-matches-longer",
    62  			a:    "*.example.net",
    63  			b:    "test.example.net",
    64  			host: "test.example.net",
    65  			ok:   true,
    66  		},
    67  		{
    68  			desc: "wildcard-matches-equal-length",
    69  			a:    "*.example.net",
    70  			b:    "a.example.net",
    71  			host: "a.example.net",
    72  			ok:   true,
    73  		},
    74  		{
    75  			desc: "wildcard-matches-multiple-subdomains",
    76  			a:    "*.example.net",
    77  			b:    "foo.bar.test.example.net",
    78  			host: "foo.bar.test.example.net",
    79  			ok:   true,
    80  		},
    81  		{
    82  			desc: "wildcard-doesnt-match-parent",
    83  			a:    "*.example.net",
    84  			b:    "example.net",
    85  			ok:   false,
    86  		},
    87  		{
    88  			desc: "wildcard-must-be-complete-label",
    89  			a:    "*example.net",
    90  			b:    "test.example.net",
    91  			ok:   false,
    92  		},
    93  	}
    94  	for _, tt := range tests {
    95  		t.Run(tt.desc, func(t *testing.T) {
    96  			for i := 0; i < 2; i++ {
    97  				if host, ok := gwMatchingHost(tt.a, tt.b); host != tt.host || ok != tt.ok {
    98  					t.Errorf(
    99  						"gwMatchingHost(%q, %q); got: %q, %v; want: %q, %v",
   100  						tt.a, tt.b, host, ok, tt.host, tt.ok,
   101  					)
   102  				}
   103  				tt.a, tt.b = tt.b, tt.a
   104  			}
   105  		})
   106  
   107  	}
   108  }
   109  
   110  func TestGatewayMatchingProtocol(t *testing.T) {
   111  	tests := []struct {
   112  		route, lis string
   113  		desc       string
   114  		ok         bool
   115  	}{
   116  		{
   117  			desc:  "protocol-matches-lis-https-route-http",
   118  			route: "HTTP",
   119  			lis:   "HTTPS",
   120  			ok:    true,
   121  		},
   122  		{
   123  			desc:  "protocol-match-invalid-list-https-route-tcp",
   124  			route: "TCP",
   125  			lis:   "HTTPS",
   126  			ok:    false,
   127  		},
   128  		{
   129  			desc:  "protocol-match-valid-lis-tls-route-tls",
   130  			route: "TLS",
   131  			lis:   "TLS",
   132  			ok:    true,
   133  		},
   134  		{
   135  			desc:  "protocol-match-valid-lis-TLS-route-TCP",
   136  			route: "TCP",
   137  			lis:   "TLS",
   138  			ok:    true,
   139  		},
   140  		{
   141  			desc:  "protocol-match-valid-lis-TLS-route-TCP",
   142  			route: "TLS",
   143  			lis:   "TCP",
   144  			ok:    false,
   145  		},
   146  	}
   147  
   148  	for _, tt := range tests {
   149  		t.Run(tt.desc, func(t *testing.T) {
   150  			for i := 0; i < 2; i++ {
   151  				if ok := gwProtocolMatches(v1.ProtocolType(tt.route), v1.ProtocolType(tt.lis)); ok != tt.ok {
   152  					t.Errorf(
   153  						"gwProtocolMatches(%q, %q); got: %v; want: %v",
   154  						tt.route, tt.lis, ok, tt.ok,
   155  					)
   156  				}
   157  				//tt.a, tt.b = tt.b, tt.a
   158  			}
   159  		})
   160  
   161  	}
   162  }
   163  
   164  func TestIsDNS1123Domain(t *testing.T) {
   165  	tests := []struct {
   166  		desc string
   167  		in   string
   168  		ok   bool
   169  	}{
   170  		{
   171  			desc: "empty",
   172  			ok:   false,
   173  		},
   174  		{
   175  			desc: "label-too-long",
   176  			in:   strings.Repeat("x", 64) + ".example.net",
   177  			ok:   false,
   178  		},
   179  		{
   180  			desc: "domain-too-long",
   181  			in:   strings.Repeat("testing.", 256/(len("testing."))) + "example.net",
   182  			ok:   false,
   183  		},
   184  		{
   185  			desc: "hostname",
   186  			in:   "example",
   187  			ok:   true,
   188  		},
   189  		{
   190  			desc: "domain",
   191  			in:   "example.net",
   192  			ok:   true,
   193  		},
   194  		{
   195  			desc: "subdomain",
   196  			in:   "test.example.net",
   197  			ok:   true,
   198  		},
   199  		{
   200  			desc: "dashes",
   201  			in:   "test-with-dash.example.net",
   202  			ok:   true,
   203  		},
   204  		{
   205  			desc: "dash-prefix",
   206  			in:   "-dash-prefix.example.net",
   207  			ok:   false,
   208  		},
   209  		{
   210  			desc: "dash-suffix",
   211  			in:   "dash-suffix-.example.net",
   212  			ok:   false,
   213  		},
   214  		{
   215  			desc: "underscore",
   216  			in:   "under_score.example.net",
   217  			ok:   false,
   218  		},
   219  		{
   220  			desc: "plus",
   221  			in:   "pl+us.example.net",
   222  			ok:   false,
   223  		},
   224  		{
   225  			desc: "brackets",
   226  			in:   "bra[k]ets.example.net",
   227  			ok:   false,
   228  		},
   229  		{
   230  			desc: "parens",
   231  			in:   "pa[re]ns.example.net",
   232  			ok:   false,
   233  		},
   234  		{
   235  			desc: "wild",
   236  			in:   "*.example.net",
   237  			ok:   false,
   238  		},
   239  	}
   240  	for _, tt := range tests {
   241  		t.Run(tt.desc, func(t *testing.T) {
   242  			if ok := isDNS1123Domain(tt.in); ok != tt.ok {
   243  				t.Errorf("isDNS1123Domain(%q); got: %v; want: %v", tt.in, ok, tt.ok)
   244  			}
   245  		})
   246  	}
   247  }