Debian Codegraph @

pkg/server/dynamiccertificates/cert_key_test.go

raw
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 (
20 "testing"
21 )
22
23 func TestCertKeyContentEquals(t *testing.T) {
24 tests := []struct {
25 name string
26 lhs *certKeyContent
27 rhs *certKeyContent
28 expected bool
29 }{
30 {
31 name: "both nil",
32 expected: true,
33 },
34 {
35 name: "lhs nil",
36 rhs: &certKeyContent{},
37 expected: false,
38 },
39 {
40 name: "rhs nil",
41 lhs: &certKeyContent{},
42 expected: false,
43 },
44 {
45 name: "same",
46 lhs: &certKeyContent{cert: []byte("foo"), key: []byte("baz")},
47 rhs: &certKeyContent{cert: []byte("foo"), key: []byte("baz")},
48 expected: true,
49 },
50 {
51 name: "different cert",
52 lhs: &certKeyContent{cert: []byte("foo"), key: []byte("baz")},
53 rhs: &certKeyContent{cert: []byte("bar"), key: []byte("baz")},
54 expected: false,
55 },
56 {
57 name: "different key",
58 lhs: &certKeyContent{cert: []byte("foo"), key: []byte("baz")},
59 rhs: &certKeyContent{cert: []byte("foo"), key: []byte("qux")},
60 expected: false,
61 },
62 {
63 name: "different cert and key",
64 lhs: &certKeyContent{cert: []byte("foo"), key: []byte("baz")},
65 rhs: &certKeyContent{cert: []byte("bar"), key: []byte("qux")},
66 expected: false,
67 },
68 }
69
70 for _, test := range tests {
71 t.Run(test.name, func(t *testing.T) {
72 actual := test.lhs.Equal(test.rhs)
73 if actual != test.expected {
74 t.Error(actual)
75 }
76 })
77 }
78 }
79
80 func TestSNICertKeyContentEquals(t *testing.T) {
81 tests := []struct {
82 name string
83 lhs *sniCertKeyContent
84 rhs *sniCertKeyContent
85 expected bool
86 }{
87 {
88 name: "both nil",
89 expected: true,
90 },
91 {
92 name: "lhs nil",
93 rhs: &sniCertKeyContent{},
94 expected: false,
95 },
96 {
97 name: "rhs nil",
98 lhs: &sniCertKeyContent{},
99 expected: false,
100 },
101 {
102 name: "same",
103 lhs: &sniCertKeyContent{certKeyContent: certKeyContent{cert: []byte("foo"), key: []byte("baz")}, sniNames: []string{"a"}},
104 rhs: &sniCertKeyContent{certKeyContent: certKeyContent{cert: []byte("foo"), key: []byte("baz")}, sniNames: []string{"a"}},
105 expected: true,
106 },
107 {
108 name: "different cert",
109 lhs: &sniCertKeyContent{certKeyContent: certKeyContent{cert: []byte("foo"), key: []byte("baz")}, sniNames: []string{"a"}},
110 rhs: &sniCertKeyContent{certKeyContent: certKeyContent{cert: []byte("bar"), key: []byte("baz")}, sniNames: []string{"a"}},
111 expected: false,
112 },
113 {
114 name: "different key",
115 lhs: &sniCertKeyContent{certKeyContent: certKeyContent{cert: []byte("foo"), key: []byte("baz")}, sniNames: []string{"a"}},
116 rhs: &sniCertKeyContent{certKeyContent: certKeyContent{cert: []byte("foo"), key: []byte("qux")}, sniNames: []string{"a"}},
117 expected: false,
118 },
119 {
120 name: "different cert and key",
121 lhs: &sniCertKeyContent{certKeyContent: certKeyContent{cert: []byte("foo"), key: []byte("baz")}, sniNames: []string{"a"}},
122 rhs: &sniCertKeyContent{certKeyContent: certKeyContent{cert: []byte("bar"), key: []byte("qux")}, sniNames: []string{"a"}},
123 expected: false,
124 },
125 {
126 name: "different names",
127 lhs: &sniCertKeyContent{certKeyContent: certKeyContent{cert: []byte("foo"), key: []byte("baz")}, sniNames: []string{"a"}},
128 rhs: &sniCertKeyContent{certKeyContent: certKeyContent{cert: []byte("foo"), key: []byte("baz")}, sniNames: []string{"b"}},
129 expected: false,
130 },
131 {
132 name: "extra names",
133 lhs: &sniCertKeyContent{certKeyContent: certKeyContent{cert: []byte("foo"), key: []byte("baz")}, sniNames: []string{"a"}},
134 rhs: &sniCertKeyContent{certKeyContent: certKeyContent{cert: []byte("foo"), key: []byte("baz")}, sniNames: []string{"a", "b"}},
135 expected: false,
136 },
137 }
138
139 for _, test := range tests {
140 t.Run(test.name, func(t *testing.T) {
141 actual := test.lhs.Equal(test.rhs)
142 if actual != test.expected {
143 t.Error(actual)
144 }
145 })
146 }
147 }