-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryptopts_test.go
126 lines (114 loc) · 2.53 KB
/
encryptopts_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// SPDX-FileCopyrightText: 2024 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package securly
import (
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"testing"
"github.com/lestrrat-go/jwx/v2/jwa"
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEncryptWithRaw(t *testing.T) {
// Generate test keys
rsaPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048)
require.NoError(t, err)
ecdsaPrivateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
require.NoError(t, err)
_, ed25519PrivateKey, err := ed25519.GenerateKey(rand.Reader)
require.NoError(t, err)
tests := []struct {
name string
alg jwa.KeyEncryptionAlgorithm
rawKey any
expectErr bool
}{
{
name: "valid RSA key",
alg: jwa.RSA_OAEP,
rawKey: rsaPrivateKey,
expectErr: false,
},
{
name: "valid ECDSA key",
alg: jwa.ECDH_ES,
rawKey: ecdsaPrivateKey,
expectErr: false,
},
{
name: "valid Ed25519 key",
alg: jwa.ECDH_ES,
rawKey: ed25519PrivateKey,
expectErr: false,
},
{
name: "invalid key type",
alg: jwa.RSA_OAEP,
rawKey: "invalid-key",
expectErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opt := EncryptWithRaw(tt.alg, tt.rawKey)
enc := &encrypter{}
err := opt.apply(enc)
if tt.expectErr {
assert.Error(t, err)
} else {
require.NoError(t, err)
assert.Equal(t, tt.alg, enc.alg)
assert.NotNil(t, enc.key)
}
})
}
}
func TestValidateEncryptOption(t *testing.T) {
tests := []struct {
name string
alg jwa.KeyEncryptionAlgorithm
key jwk.Key
expectErr bool
}{
{
name: "valid algorithm and key",
alg: jwa.DIRECT,
key: mustFromRaw([]byte("a clear text key")),
expectErr: false,
},
{
name: "missing algorithm and key",
expectErr: false,
},
{
name: "missing key",
alg: jwa.RSA_OAEP,
expectErr: true,
},
{
name: "missing algorithm",
key: mustFromRaw([]byte("a clear text key")),
expectErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
enc := &encrypter{
alg: tt.alg,
key: tt.key,
}
opt := validateEncrypt()
err := opt.apply(enc)
if tt.expectErr {
require.Error(t, err)
assert.Equal(t, ErrInvalidEncryptionAlg, err)
} else {
assert.NoError(t, err)
}
})
}
}