...

Source file src/go.etcd.io/etcd/client/pkg/v3/tlsutil/versions_test.go

Documentation: go.etcd.io/etcd/client/pkg/v3/tlsutil

     1  // Copyright 2023 The etcd 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 tlsutil
    16  
    17  import (
    18  	"crypto/tls"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  )
    23  
    24  func TestGetVersion(t *testing.T) {
    25  	tests := []struct {
    26  		name        string
    27  		version     string
    28  		want        uint16
    29  		expectError bool
    30  	}{
    31  		{
    32  			name:    "TLS1.2",
    33  			version: "TLS1.2",
    34  			want:    tls.VersionTLS12,
    35  		},
    36  		{
    37  			name:    "TLS1.3",
    38  			version: "TLS1.3",
    39  			want:    tls.VersionTLS13,
    40  		},
    41  		{
    42  			name:    "Empty version",
    43  			version: "",
    44  			want:    0,
    45  		},
    46  		{
    47  			name:        "Converting invalid version string to TLS version",
    48  			version:     "not_existing",
    49  			expectError: true,
    50  		},
    51  	}
    52  
    53  	for _, tt := range tests {
    54  		t.Run(tt.name, func(t *testing.T) {
    55  			got, err := GetTLSVersion(tt.version)
    56  			if err != nil {
    57  				assert.True(t, tt.expectError, "GetTLSVersion() returned error while expecting success: %v", err)
    58  				return
    59  			}
    60  			assert.Equal(t, tt.want, got)
    61  		})
    62  	}
    63  }
    64  

View as plain text