-
Notifications
You must be signed in to change notification settings - Fork 59
/
encrypt_test.go
170 lines (157 loc) · 3.65 KB
/
encrypt_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
164
165
166
167
168
169
170
package uadmin
// TestGenerateByteArray is a unit testing function for generateByteArray() function
func (t *UAdminTests) TestGenerateByteArray() {
examples := []struct {
length int
}{
{0},
{10},
{20},
{100},
}
for _, e := range examples {
code := generateByteArray(e.length)
if len(code) != e.length {
t.Errorf("length of generateByteArray(%d) = %d != %d", e.length, len(code), e.length)
}
}
}
// TestEncrypt is a unit testing function for encrypt() function
func (t *UAdminTests) TestEncrypt() {
examples := []struct {
str string
}{
{""},
{"a"},
{"abc"},
{" abc"},
{" abc "},
{"abcdefghijklmnopqrstuvwxyz"},
{"a b c d e f g h i j k l m n o p q r s t u v w x y z"},
{GenerateBase64(100)},
{GenerateBase64(200)},
{GenerateBase64(500)},
{GenerateBase64(1000)},
{GenerateBase64(10000)},
}
key := generateByteArray(32)
for _, e := range examples {
encrypted, err := encrypt(key, e.str)
if err != nil {
t.Errorf("error in running encrypt(\"%s\") %s", e.str, err)
continue
}
decrypted, err := decrypt(key, encrypted)
if err != nil {
t.Errorf("error in running decrypt(\"%s\") %s", e.str, err)
continue
}
if e.str != decrypted {
t.Errorf("original text does't match decrypted text for (%s)", e.str)
}
}
}
// TestencryptRecord is a unit testing function for encryptRecord() function
func (t *UAdminTests) TestEncryptRecord() {
type TestModel struct {
Model
Name string `uadmin:"encrypt"`
}
//Schema = map[string]ModelSchema{}
Schema["testmodel"], _ = getSchema(TestModel{})
examples := []struct {
str string
}{
{""},
{"a"},
{"abc"},
{" abc"},
{" abc "},
{"abcdefghijklmnopqrstuvwxyz"},
{"a b c d e f g h i j k l m n o p q r s t u v w x y z"},
{GenerateBase64(100)},
{GenerateBase64(200)},
{GenerateBase64(500)},
{GenerateBase64(1000)},
{GenerateBase64(10000)},
}
registered = false
for _, e := range examples {
m := TestModel{Name: e.str}
encryptRecord(&m)
if m.Name != e.str {
t.Errorf("TestEncryptRecord is encrypting without being registered")
}
}
registered = true
for _, e := range examples {
m := TestModel{Name: e.str}
encryptRecord(&m)
decryptRecord(&m)
if m.Name != e.str {
t.Errorf("original text does't match decrypted text for using TestEncryptRecord (%s)", e.str)
}
}
registered = false
//Schema = map[string]ModelSchema{}
}
// TestencryptRecord is a unit testing function for encryptRecord() function
func (t *UAdminTests) TestEncryptArray() {
type TestModel struct {
Model
Name string `uadmin:"encrypt"`
}
//Schema = map[string]ModelSchema{}
Schema["testmodel"], _ = getSchema(TestModel{})
examples := []struct {
str string
}{
{""},
{"a"},
{"abc"},
{" abc"},
{" abc "},
{"abcdefghijklmnopqrstuvwxyz"},
{"a b c d e f g h i j k l m n o p q r s t u v w x y z"},
{GenerateBase64(100)},
{GenerateBase64(200)},
{GenerateBase64(500)},
{GenerateBase64(1000)},
{GenerateBase64(10000)},
}
registered = false
for _, e := range examples {
m := []TestModel{
{Name: e.str},
{Name: e.str},
{Name: e.str},
{Name: e.str},
{Name: e.str},
}
for i := range m {
encryptArray(&m[i])
if m[i].Name != e.str {
t.Errorf("TestEncryptRecord is encrypting without being registered")
}
}
}
registered = true
for _, e := range examples {
m := []TestModel{
{Name: e.str},
{Name: e.str},
{Name: e.str},
{Name: e.str},
{Name: e.str},
}
for i := range m {
encryptArray(&m)
decryptArray(&m)
if m[i].Name != e.str {
t.Errorf("original text does't match decrypted text for using TestEncryptRecord (%s)", e.str)
}
}
}
registered = false
//Schema = map[string]ModelSchema{}
}