Debian Codegraph @

plugin/proto/algorithm_test.go

raw
1 // Copyright The Notary Project Authors.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package proto
15
16 import (
17 "testing"
18
19 "github.com/notaryproject/notation-core-go/signature"
20 )
21
22 func TestEncodeKeySpec(t *testing.T) {
23 tests := []struct {
24 name string
25 keySpec signature.KeySpec
26 expected KeySpec
27 }{
28 {
29 name: "EC 256",
30 keySpec: signature.KeySpec{
31 Type: signature.KeyTypeEC,
32 Size: 256,
33 },
34 expected: KeySpecEC256,
35 },
36 {
37 name: "EC 384",
38 keySpec: signature.KeySpec{
39 Type: signature.KeyTypeEC,
40 Size: 384,
41 },
42 expected: KeySpecEC384,
43 },
44 {
45 name: "EC 521",
46 keySpec: signature.KeySpec{
47 Type: signature.KeyTypeEC,
48 Size: 521,
49 },
50 expected: KeySpecEC521,
51 },
52 {
53 name: "RSA 2048",
54 keySpec: signature.KeySpec{
55 Type: signature.KeyTypeRSA,
56 Size: 2048,
57 },
58 expected: KeySpecRSA2048,
59 },
60 {
61 name: "RSA 3072",
62 keySpec: signature.KeySpec{
63 Type: signature.KeyTypeRSA,
64 Size: 3072,
65 },
66 expected: KeySpecRSA3072,
67 },
68 {
69 name: "RSA 4096",
70 keySpec: signature.KeySpec{
71 Type: signature.KeyTypeRSA,
72 Size: 4096,
73 },
74 expected: KeySpecRSA4096,
75 },
76 {
77 name: "Unsupported key spec",
78 keySpec: signature.KeySpec{
79 Type: 0,
80 Size: 0,
81 },
82 expected: "",
83 },
84 }
85
86 for _, tt := range tests {
87 t.Run(tt.name, func(t *testing.T) {
88 if name, _ := EncodeKeySpec(tt.keySpec); name != tt.expected {
89 t.Fatalf("unexpected keySpec name, expect: %v, got: %v", tt.expected, name)
90 }
91 })
92 }
93 }
94
95 func TestHashAlgorithmFromKeySpec(t *testing.T) {
96 tests := []struct {
97 name string
98 keySpec signature.KeySpec
99 expected HashAlgorithm
100 }{
101 {
102 name: "EC 256",
103 keySpec: signature.KeySpec{
104 Type: signature.KeyTypeEC,
105 Size: 256,
106 },
107 expected: HashAlgorithmSHA256,
108 },
109 {
110 name: "EC 384",
111 keySpec: signature.KeySpec{
112 Type: signature.KeyTypeEC,
113 Size: 384,
114 },
115 expected: HashAlgorithmSHA384,
116 },
117 {
118 name: "EC 521",
119 keySpec: signature.KeySpec{
120 Type: signature.KeyTypeEC,
121 Size: 521,
122 },
123 expected: HashAlgorithmSHA512,
124 },
125 {
126 name: "RSA 2048",
127 keySpec: signature.KeySpec{
128 Type: signature.KeyTypeRSA,
129 Size: 2048,
130 },
131 expected: HashAlgorithmSHA256,
132 },
133 {
134 name: "RSA 3072",
135 keySpec: signature.KeySpec{
136 Type: signature.KeyTypeRSA,
137 Size: 3072,
138 },
139 expected: HashAlgorithmSHA384,
140 },
141 {
142 name: "RSA 4096",
143 keySpec: signature.KeySpec{
144 Type: signature.KeyTypeRSA,
145 Size: 4096,
146 },
147 expected: HashAlgorithmSHA512,
148 },
149 {
150 name: "Unsupported key spec",
151 keySpec: signature.KeySpec{
152 Type: 0,
153 Size: 0,
154 },
155 expected: "",
156 },
157 }
158
159 for _, tt := range tests {
160 t.Run(tt.name, func(t *testing.T) {
161 if name, _ := HashAlgorithmFromKeySpec(tt.keySpec); name != tt.expected {
162 t.Fatalf("unexpected keySpec hash function name, expect: %v, got: %v", tt.expected, name)
163 }
164 })
165 }
166 }
167
168 func TestDecodeKeySpec(t *testing.T) {
169 tests := []struct {
170 name string
171 raw KeySpec
172 expected signature.KeySpec
173 expectErr bool
174 }{
175 {
176 name: "EC 256",
177 raw: KeySpecEC256,
178 expected: signature.KeySpec{
179 Type: signature.KeyTypeEC,
180 Size: 256,
181 },
182 expectErr: false,
183 },
184 {
185 name: "EC 384",
186 raw: KeySpecEC384,
187 expected: signature.KeySpec{
188 Type: signature.KeyTypeEC,
189 Size: 384,
190 },
191 expectErr: false,
192 },
193 {
194 name: "EC 521",
195 raw: KeySpecEC521,
196 expected: signature.KeySpec{
197 Type: signature.KeyTypeEC,
198 Size: 521,
199 },
200 expectErr: false,
201 },
202 {
203 name: "RSA 2048",
204 raw: KeySpecRSA2048,
205 expected: signature.KeySpec{
206 Type: signature.KeyTypeRSA,
207 Size: 2048,
208 },
209 expectErr: false,
210 },
211 {
212 name: "RSA 3072",
213 raw: KeySpecRSA3072,
214 expected: signature.KeySpec{
215 Type: signature.KeyTypeRSA,
216 Size: 3072,
217 },
218 expectErr: false,
219 },
220 {
221 name: "RSA 4096",
222 raw: KeySpecRSA4096,
223 expected: signature.KeySpec{
224 Type: signature.KeyTypeRSA,
225 Size: 4096,
226 },
227 expectErr: false,
228 },
229 {
230 name: "Unsupported key spec",
231 raw: "unsupported",
232 expected: signature.KeySpec{},
233 expectErr: true,
234 },
235 }
236
237 for _, tt := range tests {
238 t.Run(tt.name, func(t *testing.T) {
239 keySpec, err := DecodeKeySpec(tt.raw)
240 if keySpec != tt.expected {
241 t.Fatalf("unexpected parsed keySpec name, expect: %v, got: %v", tt.expected, keySpec)
242 }
243 if (err != nil) != tt.expectErr {
244 t.Fatalf("expect ParseKeySpec error: %v, got: %v", tt.expectErr, err)
245 }
246 })
247 }
248 }
249
250 func TestEncodeSigningAlgorithm(t *testing.T) {
251 tests := []struct {
252 name string
253 alg signature.Algorithm
254 expected SignatureAlgorithm
255 }{
256 {
257 name: "RSASSA-PSS with SHA-256",
258 alg: signature.AlgorithmPS256,
259 expected: SignatureAlgorithmRSASSA_PSS_SHA256,
260 },
261 {
262 name: "RSASSA-PSS with SHA-384",
263 alg: signature.AlgorithmPS384,
264 expected: SignatureAlgorithmRSASSA_PSS_SHA384,
265 },
266 {
267 name: "RSASSA-PSS with SHA-512",
268 alg: signature.AlgorithmPS512,
269 expected: SignatureAlgorithmRSASSA_PSS_SHA512,
270 },
271 {
272 name: "ECDSA on secp256r1 with SHA-256",
273 alg: signature.AlgorithmES256,
274 expected: SignatureAlgorithmECDSA_SHA256,
275 },
276 {
277 name: "ECDSA on secp384r1 with SHA-384",
278 alg: signature.AlgorithmES384,
279 expected: SignatureAlgorithmECDSA_SHA384,
280 },
281 {
282 name: "ECDSA on secp521r1 with SHA-512",
283 alg: signature.AlgorithmES512,
284 expected: SignatureAlgorithmECDSA_SHA512,
285 },
286 {
287 name: "unsupported algorithm",
288 alg: 0,
289 expected: "",
290 },
291 }
292 for _, tt := range tests {
293 t.Run(tt.name, func(t *testing.T) {
294 if name, _ := EncodeSigningAlgorithm(tt.alg); name != tt.expected {
295 t.Fatalf("unexpected signing algorithm name, expect: %v, got: %v", tt.expected, name)
296 }
297 })
298 }
299 }
300
301 func TestParseSigningAlgorithm(t *testing.T) {
302 tests := []struct {
303 name string
304 raw SignatureAlgorithm
305 expected signature.Algorithm
306 expectErr bool
307 }{
308 {
309 name: "RSASSA-PSS with SHA-256",
310 raw: SignatureAlgorithmRSASSA_PSS_SHA256,
311 expected: signature.AlgorithmPS256,
312 expectErr: false,
313 },
314 {
315 name: "RSASSA-PSS with SHA-384",
316 raw: SignatureAlgorithmRSASSA_PSS_SHA384,
317 expected: signature.AlgorithmPS384,
318 expectErr: false,
319 },
320 {
321 name: "RSASSA-PSS with SHA-512",
322 raw: SignatureAlgorithmRSASSA_PSS_SHA512,
323 expected: signature.AlgorithmPS512,
324 expectErr: false,
325 },
326 {
327 name: "ECDSA on secp256r1 with SHA-256",
328 raw: SignatureAlgorithmECDSA_SHA256,
329 expected: signature.AlgorithmES256,
330 expectErr: false,
331 },
332 {
333 name: "ECDSA on secp384r1 with SHA-384",
334 raw: SignatureAlgorithmECDSA_SHA384,
335 expected: signature.AlgorithmES384,
336 expectErr: false,
337 },
338 {
339 name: "ECDSA on secp521r1 with SHA-512",
340 raw: SignatureAlgorithmECDSA_SHA512,
341 expected: signature.AlgorithmES512,
342 expectErr: false,
343 },
344 {
345 name: "unsupported algorithm",
346 raw: "",
347 expected: 0,
348 expectErr: true,
349 },
350 }
351 for _, tt := range tests {
352 t.Run(tt.name, func(t *testing.T) {
353 alg, err := DecodeSigningAlgorithm(tt.raw)
354 if alg != tt.expected {
355 t.Fatalf("unexpected signing algorithm, expect: %v, got: %v", tt.expected, alg)
356 }
357 if (err != nil) != tt.expectErr {
358 t.Fatalf("expect ParseSigningAlgorithFromName error: %v, got: %v", tt.expectErr, err)
359 }
360 })
361 }
362 }