github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/utils/azure/endpoints_test.go (about)

     1  // Copyright 2022 Gravitational, Inc
     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 azure
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/require"
    21  )
    22  
    23  func TestIsMSSQLServerEndpoint(t *testing.T) {
    24  	t.Parallel()
    25  
    26  	for _, tc := range []struct {
    27  		desc     string
    28  		endpoint string
    29  		result   bool
    30  	}{
    31  		// valid
    32  		{"only suffix", ".database.windows.net", true},
    33  		{"suffix with port", ".database.windows.net:1604", true},
    34  		{"full name", "random.database.windows.net:1604", true},
    35  		// invalid
    36  		{"empty", "", false},
    37  		{"without suffix", "hello:1604", false},
    38  		{"wrong suffix", "hello.database.azure.com:1604", false},
    39  	} {
    40  		t.Run(tc.desc, func(t *testing.T) {
    41  			require.Equal(t, tc.result, IsMSSQLServerEndpoint(tc.endpoint))
    42  		})
    43  	}
    44  }
    45  
    46  func TestParseMSSQLEndpoint(t *testing.T) {
    47  	t.Parallel()
    48  
    49  	for _, tc := range []struct {
    50  		desc     string
    51  		endpoint string
    52  		valid    bool
    53  		name     string
    54  	}{
    55  		// valid
    56  		{"valid", "random.database.windows.net:1604", true, "random"},
    57  		// invalid
    58  		{"empty", "", false, ""},
    59  		{"malformed address", "abc", false, ""},
    60  		{"only suffix", ".database.windows.net:1604", false, ""},
    61  		{"without suffix", "example.com:1604", false, ""},
    62  		{"without port", "random.database.windows.net", false, ""},
    63  		{"wrong suffix", "random.database.azure.com:1604", false, ""},
    64  		{"more segments than supported", "hello.random.database.windows.net:1604", false, ""},
    65  	} {
    66  		t.Run(tc.desc, func(t *testing.T) {
    67  			name, err := ParseMSSQLEndpoint(tc.endpoint)
    68  			if tc.valid {
    69  				require.NoError(t, err)
    70  			} else {
    71  				require.Error(t, err)
    72  			}
    73  			require.Equal(t, tc.name, name)
    74  		})
    75  	}
    76  }
    77  
    78  func TestIsAzureEndpoint(t *testing.T) {
    79  	tests := []struct {
    80  		name     string
    81  		hostname string
    82  		want     bool
    83  	}{
    84  		{
    85  			name:     "empty",
    86  			hostname: "",
    87  			want:     false,
    88  		},
    89  		{
    90  			name:     "valid endpoint",
    91  			hostname: "management.azure.com",
    92  			want:     true,
    93  		},
    94  		{
    95  			name:     "valid endpoint prefix",
    96  			hostname: "subdomain.core.windows.net",
    97  			want:     true,
    98  		},
    99  		{
   100  			name:     "invalid endpoint, with valid prefix",
   101  			hostname: "core.windows.net.example.com",
   102  			want:     false,
   103  		},
   104  		{
   105  			name:     "invalid endpoint",
   106  			hostname: "not-azure.example.com",
   107  			want:     false,
   108  		},
   109  		{
   110  			name:     "invalid endpoint, suffix match without dot",
   111  			hostname: "my-azurefd.net",
   112  			want:     false,
   113  		},
   114  		{
   115  			name:     "valid endpoint, suffix matches with dot",
   116  			hostname: "my.azurefd.net",
   117  			want:     true,
   118  		},
   119  	}
   120  	for _, tt := range tests {
   121  		t.Run(tt.name, func(t *testing.T) {
   122  			require.Equal(t, tt.want, IsAzureEndpoint(tt.hostname))
   123  		})
   124  	}
   125  }