This repository has been archived by the owner on Feb 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
formcrypt_test.go
163 lines (135 loc) · 4.12 KB
/
formcrypt_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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package formcrypt
import (
"crypto/rand"
"crypto/rsa"
"encoding/hex"
"math/big"
"net/http"
"net/http/httptest"
"reflect"
"regexp"
"testing"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
)
const SessionName = "test_session"
func TestGenerate(t *testing.T) {
// test key generation function
key1024 := Key{BitSize: 1024}
err := key1024.Generate()
if err != nil {
t.Error("failed to generate 1024-bit key" + err.Error())
}
key2048 := Key{BitSize: 2048}
err = key2048.Generate()
if err != nil {
t.Error("failed to generate 2048-bit key" + err.Error())
}
key4096 := Key{BitSize: 4096}
err = key4096.Generate()
if err != nil {
t.Error("failed to generate 4096-bit key" + err.Error())
}
// testing double generation
if key1024.Generate() == nil {
t.Error("double generation of a key variable is allowed")
}
// test whether output pk string is in the format [hex string]:[hex string]
matched, err := regexp.MatchString("^[0-9a-fA-F]+:[0-9a-fA-F]+$", key2048.GetPKHexString())
if err != nil || !matched {
t.Error("output of key.GetPKHexString() is not in the correct format")
}
}
// test whether it decode hex strings and decrypts properly
func TestDecrypt(t *testing.T) {
key := Key{BitSize: 2048}
_ = key.Generate()
// generate a random []byte
letterBytes := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
length := big.NewInt(int64(len(letterBytes)))
b := make([]byte, 32)
for i := range b {
idx, _ := rand.Int(rand.Reader, length)
b[i] = letterBytes[idx.Int64()]
}
// encrypt it with the RSA key generated
enc, err := rsa.EncryptPKCS1v15(rand.Reader, &key.Key.PublicKey, b)
if err != nil {
t.Error("failed to encrypt random string: " + err.Error())
}
plaintext, err := key.Decrypt(hex.EncodeToString(enc))
if err != nil || string(b) != plaintext {
t.Error("failed to decrypt, or decrypted data does not match with original data")
}
}
// test whether engine and session storage/retrieval is correct (for single session)
func TestEngine(t *testing.T) {
r := gin.Default()
// initialize session middleware
store := cookie.NewStore([]byte("SomeRandomKeyForEncryption"))
r.Use(sessions.Sessions(SessionName, store))
err := InitializeEngine(r, SessionName)
if err != nil {
t.Error("failed to initialize engine" + err.Error())
}
r.GET("/test", func(c *gin.Context) {
key := Key{BitSize: 2048}
_ = key.Generate()
err = key.Store(c, false)
if err != nil {
t.Error("failed to store key" + err.Error())
}
newKey, err := Load(c, false)
if err != nil || !reflect.DeepEqual(key, newKey) {
t.Error("failed to load key")
}
// testing key voiding function
err = Void(c, false)
if err != nil {
t.Error("failed to void key" + err.Error())
}
newKey, err = Load(c, false)
if err == nil || !reflect.DeepEqual(Key{}, newKey) {
t.Error("key is still readable after void() being called")
}
})
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/test", nil)
r.ServeHTTP(w, req)
}
// test whether engine and session storage/retrieval is correct (for many sessions)
func TestEngineMany(t *testing.T) {
r := gin.Default()
// initialize session middleware
store := cookie.NewStore([]byte("SomeRandomKeyForEncryption"))
r.Use(sessions.SessionsMany([]string{SessionName, "TheOtherSession"}, store))
err := InitializeEngine(r, SessionName)
if err != nil {
t.Error("failed to initialize engine" + err.Error())
}
r.GET("/test", func(c *gin.Context) {
key := Key{BitSize: 2048}
_ = key.Generate()
err = key.Store(c, true)
if err != nil {
t.Error("failed to store key" + err.Error())
}
newKey, err := Load(c, true)
if err != nil || !reflect.DeepEqual(key, newKey) {
t.Error("failed to load key")
}
// testing key voiding function
err = Void(c, true)
if err != nil {
t.Error("failed to void key" + err.Error())
}
newKey, err = Load(c, true)
if err == nil || !reflect.DeepEqual(Key{}, newKey) {
t.Error("key is still readable after void() being called")
}
})
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/test", nil)
r.ServeHTTP(w, req)
}