istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/security/security_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 security
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func TestSdsCertificateConfigFromResourceName(t *testing.T) {
    22  	cases := []struct {
    23  		name             string
    24  		resource         string
    25  		root             bool
    26  		key              bool
    27  		output           SdsCertificateConfig
    28  		rootResourceName string
    29  		resourceName     string
    30  	}{
    31  		{
    32  			"cert",
    33  			"file-cert:cert~key",
    34  			false,
    35  			true,
    36  			SdsCertificateConfig{"cert", "key", ""},
    37  			"",
    38  			"file-cert:cert~key",
    39  		},
    40  		{
    41  			"root cert",
    42  			"file-root:root",
    43  			true,
    44  			false,
    45  			SdsCertificateConfig{"", "", "root"},
    46  			"file-root:root",
    47  			"",
    48  		},
    49  		{
    50  			"invalid prefix",
    51  			"file:root",
    52  			false,
    53  			false,
    54  			SdsCertificateConfig{"", "", ""},
    55  			"",
    56  			"",
    57  		},
    58  		{
    59  			"invalid contents",
    60  			"file-root:root~extra",
    61  			false,
    62  			false,
    63  			SdsCertificateConfig{"", "", ""},
    64  			"",
    65  			"",
    66  		},
    67  	}
    68  	for _, tt := range cases {
    69  		t.Run(tt.name, func(t *testing.T) {
    70  			got, _ := SdsCertificateConfigFromResourceName(tt.resource)
    71  			if got != tt.output {
    72  				t.Fatalf("got %v, expected %v", got, tt.output)
    73  			}
    74  			if root := got.IsRootCertificate(); root != tt.root {
    75  				t.Fatalf("unexpected isRootCertificate got %v, expected %v", root, tt.root)
    76  			}
    77  			if key := got.IsKeyCertificate(); key != tt.key {
    78  				t.Fatalf("unexpected IsKeyCertificate got %v, expected %v", key, tt.key)
    79  			}
    80  			if root := got.GetRootResourceName(); root != tt.rootResourceName {
    81  				t.Fatalf("unexpected GetRootResourceName got %v, expected %v", root, tt.rootResourceName)
    82  			}
    83  			if cert := got.GetResourceName(); cert != tt.resourceName {
    84  				t.Fatalf("unexpected GetRootResourceName got %v, expected %v", cert, tt.resourceName)
    85  			}
    86  		})
    87  	}
    88  }