1
+ package cryptojwt_test
2
+
3
+ import (
4
+ "crypto/ecdsa"
5
+ "crypto/elliptic"
6
+ "crypto/rand"
7
+ "crypto/rsa"
8
+ "crypto/x509"
9
+ "encoding/pem"
10
+ "os"
11
+ "path/filepath"
12
+ "testing"
13
+ )
14
+
15
+ func createTempFile (t * testing.T , content []byte ) string {
16
+ t .Helper ()
17
+ tmpFile , err := os .CreateTemp (t .TempDir (), "test-*.pem" )
18
+ if err != nil {
19
+ t .Fatalf ("Failed to create temp file: %v" , err )
20
+ }
21
+ if _ , err := tmpFile .Write (content ); err != nil {
22
+ t .Fatalf ("Failed to write to temp file: %v" , err )
23
+ }
24
+ if err := tmpFile .Close (); err != nil {
25
+ t .Fatalf ("Failed to close temp file: %v" , err )
26
+ }
27
+ return tmpFile .Name ()
28
+ }
29
+
30
+ func generateRSAKeyPair (t * testing.T ) (privateKeyPath , publicKeyPath string ) {
31
+ t .Helper ()
32
+ privateKey , err := rsa .GenerateKey (rand .Reader , 2048 )
33
+ if err != nil {
34
+ t .Fatalf ("Failed to generate RSA key: %v" , err )
35
+ }
36
+
37
+ privateKeyPEM := & pem.Block {
38
+ Type : "RSA PRIVATE KEY" ,
39
+ Bytes : x509 .MarshalPKCS1PrivateKey (privateKey ),
40
+ }
41
+ privateKeyBytes := pem .EncodeToMemory (privateKeyPEM )
42
+
43
+ publicKeyBytes , err := x509 .MarshalPKIXPublicKey (& privateKey .PublicKey )
44
+ if err != nil {
45
+ t .Fatalf ("Failed to marshal public key: %v" , err )
46
+ }
47
+ publicKeyPEM := & pem.Block {
48
+ Type : "PUBLIC KEY" ,
49
+ Bytes : publicKeyBytes ,
50
+ }
51
+ publicKeyPEMBytes := pem .EncodeToMemory (publicKeyPEM )
52
+
53
+ privateKeyPath = createTempFile (t , privateKeyBytes )
54
+ publicKeyPath = createTempFile (t , publicKeyPEMBytes )
55
+ return privateKeyPath , publicKeyPath
56
+ }
57
+
58
+ func generateECDSAKeyPair (t * testing.T , curve elliptic.Curve ) (privateKeyPath , publicKeyPath string ) {
59
+ t .Helper ()
60
+ privateKey , err := ecdsa .GenerateKey (curve , rand .Reader )
61
+ if err != nil {
62
+ t .Fatalf ("Failed to generate ECDSA key: %v" , err )
63
+ }
64
+
65
+ privateKeyBytes , err := x509 .MarshalECPrivateKey (privateKey )
66
+ if err != nil {
67
+ t .Fatalf ("Failed to marshal EC private key: %v" , err )
68
+ }
69
+ privateKeyPEM := & pem.Block {
70
+ Type : "EC PRIVATE KEY" ,
71
+ Bytes : privateKeyBytes ,
72
+ }
73
+ privateKeyPEMBytes := pem .EncodeToMemory (privateKeyPEM )
74
+
75
+ publicKeyBytes , err := x509 .MarshalPKIXPublicKey (& privateKey .PublicKey )
76
+ if err != nil {
77
+ t .Fatalf ("Failed to marshal public key: %v" , err )
78
+ }
79
+ publicKeyPEM := & pem.Block {
80
+ Type : "PUBLIC KEY" ,
81
+ Bytes : publicKeyBytes ,
82
+ }
83
+ publicKeyPEMBytes := pem .EncodeToMemory (publicKeyPEM )
84
+
85
+ privateKeyPath = createTempFile (t , privateKeyPEMBytes )
86
+ publicKeyPath = createTempFile (t , publicKeyPEMBytes )
87
+ return privateKeyPath , publicKeyPath
88
+ }
89
+
90
+ func createInvalidPEMFile (t * testing.T ) string {
91
+ t .Helper ()
92
+ return createTempFile (t , []byte ("invalid pem content" ))
93
+ }
94
+
95
+ func createWrongTypePEMFile (t * testing.T , pemType string ) string {
96
+ t .Helper ()
97
+ block := & pem.Block {
98
+ Type : pemType ,
99
+ Bytes : []byte ("some data" ),
100
+ }
101
+ return createTempFile (t , pem .EncodeToMemory (block ))
102
+ }
103
+
104
+ func createMalformedECKeyFile (t * testing.T ) string {
105
+ t .Helper ()
106
+ block := & pem.Block {
107
+ Type : "EC PRIVATE KEY" ,
108
+ Bytes : []byte ("malformed ec key data" ),
109
+ }
110
+ return createTempFile (t , pem .EncodeToMemory (block ))
111
+ }
112
+
113
+ func createMalformedRSAKeyFile (t * testing.T ) string {
114
+ t .Helper ()
115
+ block := & pem.Block {
116
+ Type : "RSA PRIVATE KEY" ,
117
+ Bytes : []byte ("malformed rsa key data" ),
118
+ }
119
+ return createTempFile (t , pem .EncodeToMemory (block ))
120
+ }
121
+
122
+ func getNonExistentPath (t * testing.T ) string {
123
+ t .Helper ()
124
+ return filepath .Join (t .TempDir (), "non-existent-file.pem" )
125
+ }
126
+
127
+ const (
128
+ validPayload = `{"sub":"1234567890","name":"John Doe","iat":1516239022}`
129
+ invalidJSON = `{invalid json}`
130
+ )
0 commit comments