k8s.io/apiserver@v0.31.1/pkg/server/dynamiccertificates/client_ca_test.go (about) 1 /* 2 Copyright 2019 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 dynamiccertificates 18 19 import "testing" 20 21 func TestDynamicCertificateContentEquals(t *testing.T) { 22 tests := []struct { 23 name string 24 lhs *dynamicCertificateContent 25 rhs *dynamicCertificateContent 26 expected bool 27 }{ 28 { 29 name: "both nil", 30 expected: true, 31 }, 32 { 33 name: "lhs nil", 34 rhs: &dynamicCertificateContent{}, 35 expected: false, 36 }, 37 { 38 name: "rhs nil", 39 lhs: &dynamicCertificateContent{}, 40 expected: false, 41 }, 42 { 43 name: "same", 44 lhs: &dynamicCertificateContent{ 45 clientCA: caBundleContent{caBundle: []byte("foo")}, 46 }, 47 rhs: &dynamicCertificateContent{ 48 clientCA: caBundleContent{caBundle: []byte("foo")}, 49 }, 50 expected: true, 51 }, 52 { 53 name: "different", 54 lhs: &dynamicCertificateContent{ 55 clientCA: caBundleContent{caBundle: []byte("foo")}, 56 }, 57 rhs: &dynamicCertificateContent{ 58 clientCA: caBundleContent{caBundle: []byte("bar")}, 59 }, 60 expected: false, 61 }, 62 { 63 name: "same with serving", 64 lhs: &dynamicCertificateContent{ 65 clientCA: caBundleContent{caBundle: []byte("foo")}, 66 servingCert: certKeyContent{cert: []byte("foo"), key: []byte("foo")}, 67 }, 68 rhs: &dynamicCertificateContent{ 69 clientCA: caBundleContent{caBundle: []byte("foo")}, 70 servingCert: certKeyContent{cert: []byte("foo"), key: []byte("foo")}, 71 }, 72 expected: true, 73 }, 74 { 75 name: "different serving cert", 76 lhs: &dynamicCertificateContent{ 77 clientCA: caBundleContent{caBundle: []byte("foo")}, 78 servingCert: certKeyContent{cert: []byte("foo"), key: []byte("foo")}, 79 }, 80 rhs: &dynamicCertificateContent{ 81 clientCA: caBundleContent{caBundle: []byte("foo")}, 82 servingCert: certKeyContent{cert: []byte("bar"), key: []byte("foo")}, 83 }, 84 expected: false, 85 }, 86 { 87 name: "different serving key", 88 lhs: &dynamicCertificateContent{ 89 clientCA: caBundleContent{caBundle: []byte("foo")}, 90 servingCert: certKeyContent{cert: []byte("foo"), key: []byte("foo")}, 91 }, 92 rhs: &dynamicCertificateContent{ 93 clientCA: caBundleContent{caBundle: []byte("foo")}, 94 servingCert: certKeyContent{cert: []byte("foo"), key: []byte("bar")}, 95 }, 96 expected: false, 97 }, 98 } 99 100 for _, test := range tests { 101 t.Run(test.name, func(t *testing.T) { 102 actual := test.lhs.Equal(test.rhs) 103 if actual != test.expected { 104 t.Error(actual) 105 } 106 }) 107 } 108 } 109 110 func TestCABundleContentEquals(t *testing.T) { 111 tests := []struct { 112 name string 113 lhs *caBundleContent 114 rhs *caBundleContent 115 expected bool 116 }{ 117 { 118 name: "both nil", 119 expected: true, 120 }, 121 { 122 name: "lhs nil", 123 rhs: &caBundleContent{}, 124 expected: false, 125 }, 126 { 127 name: "rhs nil", 128 lhs: &caBundleContent{}, 129 expected: false, 130 }, 131 { 132 name: "same", 133 lhs: &caBundleContent{caBundle: []byte("foo")}, 134 rhs: &caBundleContent{caBundle: []byte("foo")}, 135 expected: true, 136 }, 137 { 138 name: "different", 139 lhs: &caBundleContent{caBundle: []byte("foo")}, 140 rhs: &caBundleContent{caBundle: []byte("bar")}, 141 expected: false, 142 }, 143 } 144 145 for _, test := range tests { 146 t.Run(test.name, func(t *testing.T) { 147 actual := test.lhs.Equal(test.rhs) 148 if actual != test.expected { 149 t.Error(actual) 150 } 151 }) 152 } 153 }