github.com/lestrrat-go/jwx/v2@v2.0.21/internal/keyconv/keyconv_test.go (about) 1 package keyconv_test 2 3 import ( 4 "crypto/ecdsa" 5 "crypto/rsa" 6 "testing" 7 8 "github.com/lestrrat-go/jwx/v2/internal/jwxtest" 9 "github.com/lestrrat-go/jwx/v2/internal/keyconv" 10 "github.com/lestrrat-go/jwx/v2/jwa" 11 "github.com/lestrrat-go/jwx/v2/jwk" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestKeyconv(t *testing.T) { 16 t.Run("RSA", func(t *testing.T) { 17 key, err := jwxtest.GenerateRsaKey() 18 if !assert.NoError(t, err, `rsa.GenerateKey should succeed`) { 19 return 20 } 21 t.Run("PrivateKey", func(t *testing.T) { 22 jwkKey, _ := jwk.FromRaw(key) 23 testcases := []struct { 24 Src interface{} 25 Error bool 26 }{ 27 {Src: key}, 28 {Src: *key}, 29 {Src: jwkKey}, 30 {Src: struct{}{}, Error: true}, 31 } 32 33 for _, tc := range testcases { 34 tc := tc 35 t.Run("Assign to rsa.PrivateKey", func(t *testing.T) { 36 var dst rsa.PrivateKey 37 var checker func(assert.TestingT, error, ...interface{}) bool 38 if tc.Error { 39 checker = assert.Error 40 } else { 41 checker = assert.NoError 42 } 43 44 if !checker(t, keyconv.RSAPrivateKey(&dst, tc.Src), `keyconv.RSAPrivateKey should succeed`) { 45 return 46 } 47 48 if !tc.Error { 49 // From Go 1.20 on, for purposes of our test, we need the 50 // precomputed values as well 51 dst.Precompute() 52 if !assert.Equal(t, key, &dst, `keyconv.RSAPrivateKey should produce same value`) { 53 return 54 } 55 } 56 }) 57 t.Run("Assign to *rsa.PrivateKey", func(t *testing.T) { 58 dst := &rsa.PrivateKey{} 59 var checker func(assert.TestingT, error, ...interface{}) bool 60 if tc.Error { 61 checker = assert.Error 62 } else { 63 checker = assert.NoError 64 } 65 66 if !checker(t, keyconv.RSAPrivateKey(dst, tc.Src), `keyconv.RSAPrivateKey should succeed`) { 67 return 68 } 69 if !tc.Error { 70 // From Go 1.20 on, for purposes of our test, we need the 71 // precomputed values as well 72 dst.Precompute() 73 if !assert.Equal(t, key, dst, `keyconv.RSAPrivateKey should produce same value`) { 74 return 75 } 76 } 77 }) 78 } 79 }) 80 t.Run("PublicKey", func(t *testing.T) { 81 pubkey := &key.PublicKey 82 jwkKey, _ := jwk.FromRaw(pubkey) 83 testcases := []struct { 84 Src interface{} 85 Error bool 86 }{ 87 {Src: pubkey}, 88 {Src: *pubkey}, 89 {Src: jwkKey}, 90 {Src: struct{}{}, Error: true}, 91 } 92 93 for _, tc := range testcases { 94 tc := tc 95 t.Run("Assign to rsa.PublicKey", func(t *testing.T) { 96 var dst rsa.PublicKey 97 var checker func(assert.TestingT, error, ...interface{}) bool 98 if tc.Error { 99 checker = assert.Error 100 } else { 101 checker = assert.NoError 102 } 103 104 if !checker(t, keyconv.RSAPublicKey(&dst, tc.Src), `keyconv.RSAPublicKey should succeed`) { 105 return 106 } 107 if !tc.Error { 108 if !assert.Equal(t, pubkey, &dst, `keyconv.RSAPublicKey should produce same value`) { 109 return 110 } 111 } 112 }) 113 t.Run("Assign to *rsa.PublicKey", func(t *testing.T) { 114 dst := &rsa.PublicKey{} 115 var checker func(assert.TestingT, error, ...interface{}) bool 116 if tc.Error { 117 checker = assert.Error 118 } else { 119 checker = assert.NoError 120 } 121 122 if !checker(t, keyconv.RSAPublicKey(dst, tc.Src), `keyconv.RSAPublicKey should succeed`) { 123 return 124 } 125 if !tc.Error { 126 if !assert.Equal(t, pubkey, dst, `keyconv.RSAPublicKey should produce same value`) { 127 return 128 } 129 } 130 }) 131 } 132 }) 133 }) 134 t.Run("ECDSA", func(t *testing.T) { 135 key, err := jwxtest.GenerateEcdsaKey(jwa.P521) 136 if !assert.NoError(t, err, `ecdsa.GenerateKey should succeed`) { 137 return 138 } 139 140 t.Run("PrivateKey", func(t *testing.T) { 141 jwkKey, _ := jwk.FromRaw(key) 142 testcases := []struct { 143 Src interface{} 144 Error bool 145 }{ 146 {Src: key}, 147 {Src: *key}, 148 {Src: jwkKey}, 149 {Src: struct{}{}, Error: true}, 150 } 151 152 for _, tc := range testcases { 153 tc := tc 154 t.Run("Assign to ecdsa.PrivateKey", func(t *testing.T) { 155 var dst ecdsa.PrivateKey 156 var checker func(assert.TestingT, error, ...interface{}) bool 157 if tc.Error { 158 checker = assert.Error 159 } else { 160 checker = assert.NoError 161 } 162 163 if !checker(t, keyconv.ECDSAPrivateKey(&dst, tc.Src), `keyconv.ECDSAPrivateKey should succeed`) { 164 return 165 } 166 if !tc.Error { 167 if !assert.Equal(t, key, &dst, `keyconv.ECDSAPrivateKey should produce same value`) { 168 return 169 } 170 } 171 }) 172 t.Run("Assign to *ecdsa.PrivateKey", func(t *testing.T) { 173 dst := &ecdsa.PrivateKey{} 174 var checker func(assert.TestingT, error, ...interface{}) bool 175 if tc.Error { 176 checker = assert.Error 177 } else { 178 checker = assert.NoError 179 } 180 181 if !checker(t, keyconv.ECDSAPrivateKey(dst, tc.Src), `keyconv.ECDSAPrivateKey should succeed`) { 182 return 183 } 184 if !tc.Error { 185 if !assert.Equal(t, key, dst, `keyconv.ECDSAPrivateKey should produce same value`) { 186 return 187 } 188 } 189 }) 190 } 191 }) 192 t.Run("PublicKey", func(t *testing.T) { 193 pubkey := &key.PublicKey 194 jwkKey, _ := jwk.FromRaw(pubkey) 195 testcases := []struct { 196 Src interface{} 197 Error bool 198 }{ 199 {Src: pubkey}, 200 {Src: *pubkey}, 201 {Src: jwkKey}, 202 {Src: struct{}{}, Error: true}, 203 } 204 205 for _, tc := range testcases { 206 tc := tc 207 t.Run("Assign to ecdsa.PublicKey", func(t *testing.T) { 208 var dst ecdsa.PublicKey 209 var checker func(assert.TestingT, error, ...interface{}) bool 210 if tc.Error { 211 checker = assert.Error 212 } else { 213 checker = assert.NoError 214 } 215 216 if !checker(t, keyconv.ECDSAPublicKey(&dst, tc.Src), `keyconv.ECDSAPublicKey should succeed`) { 217 return 218 } 219 if !tc.Error { 220 if !assert.Equal(t, pubkey, &dst, `keyconv.ECDSAPublicKey should produce same value`) { 221 return 222 } 223 } 224 }) 225 t.Run("Assign to *ecdsa.PublicKey", func(t *testing.T) { 226 dst := &ecdsa.PublicKey{} 227 var checker func(assert.TestingT, error, ...interface{}) bool 228 if tc.Error { 229 checker = assert.Error 230 } else { 231 checker = assert.NoError 232 } 233 234 if !checker(t, keyconv.ECDSAPublicKey(dst, tc.Src), `keyconv.ECDSAPublicKey should succeed`) { 235 return 236 } 237 if !tc.Error { 238 if !assert.Equal(t, pubkey, dst, `keyconv.ECDSAPublicKey should produce same value`) { 239 return 240 } 241 } 242 }) 243 } 244 }) 245 }) 246 }