Skip to content

Commit 35d363a

Browse files
committed
rsa_test.go: add unit tests about invalid algorithm, use cAlgRSASign.
1 parent 471852b commit 35d363a

File tree

1 file changed

+43
-17
lines changed

1 file changed

+43
-17
lines changed

wincrypto/rsa_test.go

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func TestImportRSAPublicKeyBlob(t *testing.T) {
140140
_ = binary.Write(buf, binary.LittleEndian, blobHeader{
141141
Type: privateKeyBlob,
142142
Version: curBlobVersion,
143-
AiKeyAlg: 0x0000A400,
143+
AiKeyAlg: cAlgRSASign,
144144
})
145145

146146
publicKey, err := ImportRSAPublicKeyBlob(buf.Bytes())
@@ -153,20 +153,33 @@ func TestImportRSAPublicKeyBlob(t *testing.T) {
153153
_ = binary.Write(buf, binary.LittleEndian, blobHeader{
154154
Type: publicKeyBlob,
155155
Version: 1,
156-
AiKeyAlg: 0x0000A400,
156+
AiKeyAlg: cAlgRSASign,
157157
})
158158

159159
publicKey, err := ImportRSAPublicKeyBlob(buf.Bytes())
160160
require.EqualError(t, err, "invalid blob version")
161161
require.Nil(t, publicKey)
162162
})
163163

164+
t.Run("invalid public key algorithm", func(t *testing.T) {
165+
buf := new(bytes.Buffer)
166+
_ = binary.Write(buf, binary.LittleEndian, blobHeader{
167+
Type: publicKeyBlob,
168+
Version: curBlobVersion,
169+
AiKeyAlg: 1,
170+
})
171+
172+
publicKey, err := ImportRSAPublicKeyBlob(buf.Bytes())
173+
require.EqualError(t, err, "invalid public key algorithm")
174+
require.Nil(t, publicKey)
175+
})
176+
164177
t.Run("failed to read blob public key", func(t *testing.T) {
165178
buf := new(bytes.Buffer)
166179
_ = binary.Write(buf, binary.LittleEndian, blobHeader{
167180
Type: publicKeyBlob,
168181
Version: curBlobVersion,
169-
AiKeyAlg: 0x0000A400,
182+
AiKeyAlg: cAlgRSASign,
170183
})
171184
_ = binary.Write(buf, binary.LittleEndian, uint32(magicRSA1))
172185

@@ -180,7 +193,7 @@ func TestImportRSAPublicKeyBlob(t *testing.T) {
180193
_ = binary.Write(buf, binary.LittleEndian, blobHeader{
181194
Type: publicKeyBlob,
182195
Version: curBlobVersion,
183-
AiKeyAlg: 0x0000A400,
196+
AiKeyAlg: cAlgRSASign,
184197
})
185198
_ = binary.Write(buf, binary.LittleEndian, rsaPubKey{
186199
Magic: magicRSA2,
@@ -198,7 +211,7 @@ func TestImportRSAPublicKeyBlob(t *testing.T) {
198211
_ = binary.Write(buf, binary.LittleEndian, blobHeader{
199212
Type: publicKeyBlob,
200213
Version: curBlobVersion,
201-
AiKeyAlg: 0x0000A400,
214+
AiKeyAlg: cAlgRSASign,
202215
})
203216
_ = binary.Write(buf, binary.LittleEndian, rsaPubKey{
204217
Magic: magicRSA1,
@@ -216,7 +229,7 @@ func TestImportRSAPublicKeyBlob(t *testing.T) {
216229
_ = binary.Write(buf, binary.LittleEndian, blobHeader{
217230
Type: publicKeyBlob,
218231
Version: curBlobVersion,
219-
AiKeyAlg: 0x0000A400,
232+
AiKeyAlg: cAlgRSASign,
220233
})
221234
_ = binary.Write(buf, binary.LittleEndian, rsaPubKey{
222235
Magic: magicRSA1,
@@ -252,7 +265,7 @@ func TestImportRSAPrivateKeyBlob(t *testing.T) {
252265
_ = binary.Write(buf, binary.LittleEndian, blobHeader{
253266
Type: publicKeyBlob,
254267
Version: curBlobVersion,
255-
AiKeyAlg: 0x0000A400,
268+
AiKeyAlg: cAlgRSASign,
256269
})
257270

258271
privateKey, err := ImportRSAPrivateKeyBlob(buf.Bytes())
@@ -265,20 +278,33 @@ func TestImportRSAPrivateKeyBlob(t *testing.T) {
265278
_ = binary.Write(buf, binary.LittleEndian, blobHeader{
266279
Type: privateKeyBlob,
267280
Version: 1,
268-
AiKeyAlg: 0x0000A400,
281+
AiKeyAlg: cAlgRSASign,
269282
})
270283

271284
privateKey, err := ImportRSAPrivateKeyBlob(buf.Bytes())
272285
require.EqualError(t, err, "invalid blob version")
273286
require.Nil(t, privateKey)
274287
})
275288

289+
t.Run("invalid private key algorithm", func(t *testing.T) {
290+
buf := new(bytes.Buffer)
291+
_ = binary.Write(buf, binary.LittleEndian, blobHeader{
292+
Type: privateKeyBlob,
293+
Version: curBlobVersion,
294+
AiKeyAlg: 1,
295+
})
296+
297+
privateKey, err := ImportRSAPrivateKeyBlob(buf.Bytes())
298+
require.EqualError(t, err, "invalid private key algorithm")
299+
require.Nil(t, privateKey)
300+
})
301+
276302
t.Run("failed to read blob public key", func(t *testing.T) {
277303
buf := new(bytes.Buffer)
278304
_ = binary.Write(buf, binary.LittleEndian, blobHeader{
279305
Type: privateKeyBlob,
280306
Version: curBlobVersion,
281-
AiKeyAlg: 0x0000A400,
307+
AiKeyAlg: cAlgRSASign,
282308
})
283309
_ = binary.Write(buf, binary.LittleEndian, uint32(magicRSA1))
284310

@@ -292,7 +318,7 @@ func TestImportRSAPrivateKeyBlob(t *testing.T) {
292318
_ = binary.Write(buf, binary.LittleEndian, blobHeader{
293319
Type: privateKeyBlob,
294320
Version: curBlobVersion,
295-
AiKeyAlg: 0x0000A400,
321+
AiKeyAlg: cAlgRSASign,
296322
})
297323
_ = binary.Write(buf, binary.LittleEndian, rsaPubKey{
298324
Magic: magicRSA1,
@@ -310,7 +336,7 @@ func TestImportRSAPrivateKeyBlob(t *testing.T) {
310336
_ = binary.Write(buf, binary.LittleEndian, blobHeader{
311337
Type: privateKeyBlob,
312338
Version: curBlobVersion,
313-
AiKeyAlg: 0x0000A400,
339+
AiKeyAlg: cAlgRSASign,
314340
})
315341
_ = binary.Write(buf, binary.LittleEndian, rsaPubKey{
316342
Magic: magicRSA2,
@@ -328,7 +354,7 @@ func TestImportRSAPrivateKeyBlob(t *testing.T) {
328354
_ = binary.Write(buf, binary.LittleEndian, blobHeader{
329355
Type: privateKeyBlob,
330356
Version: curBlobVersion,
331-
AiKeyAlg: 0x0000A400,
357+
AiKeyAlg: cAlgRSASign,
332358
})
333359
_ = binary.Write(buf, binary.LittleEndian, rsaPubKey{
334360
Magic: magicRSA2,
@@ -347,7 +373,7 @@ func TestImportRSAPrivateKeyBlob(t *testing.T) {
347373
_ = binary.Write(buf, binary.LittleEndian, blobHeader{
348374
Type: privateKeyBlob,
349375
Version: curBlobVersion,
350-
AiKeyAlg: 0x0000A400,
376+
AiKeyAlg: cAlgRSASign,
351377
})
352378
_ = binary.Write(buf, binary.LittleEndian, rsaPubKey{
353379
Magic: magicRSA2,
@@ -367,7 +393,7 @@ func TestImportRSAPrivateKeyBlob(t *testing.T) {
367393
_ = binary.Write(buf, binary.LittleEndian, blobHeader{
368394
Type: privateKeyBlob,
369395
Version: curBlobVersion,
370-
AiKeyAlg: 0x0000A400,
396+
AiKeyAlg: cAlgRSASign,
371397
})
372398
_ = binary.Write(buf, binary.LittleEndian, rsaPubKey{
373399
Magic: magicRSA2,
@@ -388,7 +414,7 @@ func TestImportRSAPrivateKeyBlob(t *testing.T) {
388414
_ = binary.Write(buf, binary.LittleEndian, blobHeader{
389415
Type: privateKeyBlob,
390416
Version: curBlobVersion,
391-
AiKeyAlg: 0x0000A400,
417+
AiKeyAlg: cAlgRSASign,
392418
})
393419
_ = binary.Write(buf, binary.LittleEndian, rsaPubKey{
394420
Magic: magicRSA2,
@@ -410,7 +436,7 @@ func TestImportRSAPrivateKeyBlob(t *testing.T) {
410436
_ = binary.Write(buf, binary.LittleEndian, blobHeader{
411437
Type: privateKeyBlob,
412438
Version: curBlobVersion,
413-
AiKeyAlg: 0x0000A400,
439+
AiKeyAlg: cAlgRSASign,
414440
})
415441
_ = binary.Write(buf, binary.LittleEndian, rsaPubKey{
416442
Magic: magicRSA2,
@@ -433,7 +459,7 @@ func TestImportRSAPrivateKeyBlob(t *testing.T) {
433459
_ = binary.Write(buf, binary.LittleEndian, blobHeader{
434460
Type: privateKeyBlob,
435461
Version: curBlobVersion,
436-
AiKeyAlg: 0x0000A400,
462+
AiKeyAlg: cAlgRSASign,
437463
})
438464
_ = binary.Write(buf, binary.LittleEndian, rsaPubKey{
439465
Magic: magicRSA2,

0 commit comments

Comments
 (0)