istio.io/istio@v0.0.0-20240520182934-d79c90f27776/security/pkg/pki/util/dual_use_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 util 16 17 import ( 18 "strings" 19 "testing" 20 ) 21 22 func TestDualUseCommonName(t *testing.T) { 23 tt := []struct { 24 name string 25 host string 26 expectedCN string 27 expectErr bool 28 }{ 29 { 30 name: "single host", 31 host: "a.com", 32 expectedCN: "a.com", 33 }, 34 { 35 name: "multiple hosts", 36 host: "a.com,b.org,c.groups", 37 expectedCN: "a.com", 38 }, 39 { 40 name: "long host", 41 host: strings.Repeat("a", 61) + ".com", 42 expectErr: true, 43 }, 44 } 45 46 for _, tc := range tt { 47 cn, err := DualUseCommonName(tc.host) 48 if tc.expectErr { 49 if err == nil { 50 t.Errorf("[%s] passed - expected error", tc.name) 51 } 52 continue 53 } 54 55 if err != nil { 56 t.Errorf("[%s] unexpected error: %v", tc.name, err) 57 } 58 59 if tc.expectedCN != cn { 60 t.Errorf("[%s] unexpected CN: wanted %q got %q", tc.name, tc.expectedCN, cn) 61 } 62 } 63 }