forked from mojocn/base64Captcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_memory_test.go
168 lines (152 loc) · 3.61 KB
/
store_memory_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
// Copyright 2017 Eric Zhou. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package base64Captcha
import (
"fmt"
"math/rand"
"testing"
"time"
)
func TestSetGet(t *testing.T) {
s := NewMemoryStore(GCLimitNumber, Expiration)
id := "captcha id"
d := "random-string"
_ = s.Set(id, d)
d2 := s.Get(id, false)
if d2 != d {
t.Errorf("saved %v, getDigits returned got %v", d, d2)
}
}
func TestGetClear(t *testing.T) {
s := NewMemoryStore(GCLimitNumber, Expiration)
id := "captcha id"
d := "932839jfffjkdss"
_ = s.Set(id, d)
d2 := s.Get(id, true)
if d != d2 {
t.Errorf("saved %v, getDigitsClear returned got %v", d, d2)
}
d2 = s.Get(id, false)
if d2 != "" {
t.Errorf("getDigitClear didn't clear (%q=%v)", id, d2)
}
}
func BenchmarkSetCollect(b *testing.B) {
b.StopTimer()
d := "fdskfew9832232r"
s := NewMemoryStore(9999, -1)
ids := make([]string, 1000)
for i := range ids {
ids[i] = fmt.Sprintf("%d", rand.Int63())
}
b.StartTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < 1000; j++ {
_ = s.Set(ids[j], d)
}
}
}
func TestMemoryStore_SetGoCollect(t *testing.T) {
s := NewMemoryStore(10, -1)
for i := 0; i <= 100; i++ {
_ = s.Set(fmt.Sprint(i), fmt.Sprint(i))
}
}
func TestMemoryStore_CollectNotExpire(t *testing.T) {
s := NewMemoryStore(10, time.Hour)
for i := 0; i < 50; i++ {
_ = s.Set(fmt.Sprint(i), fmt.Sprint(i))
}
// let background goroutine to go
time.Sleep(time.Second)
if v := s.Get("0", false); v != "0" {
t.Error("mem store get failed")
}
}
func TestNewMemoryStore(t *testing.T) {
type args struct {
collectNum int
expiration time.Duration
}
tests := []struct {
name string
args args
want Store
}{
{"", args{20, time.Hour}, nil},
{"", args{20, time.Hour * 5}, nil},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewMemoryStore(tt.args.collectNum, tt.args.expiration); got == nil {
t.Errorf("NewMemoryStore() = %v, want %v", got, tt.want)
}
})
}
}
func Test_memoryStore_Set(t *testing.T) {
thisStore := NewMemoryStore(10, time.Hour)
type args struct {
id string
value string
}
tests := []struct {
name string
s Store
args args
}{
{"", thisStore, args{RandomId(), RandomId()}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_ = tt.s.Set(tt.args.id, tt.args.value)
})
}
}
func Test_memoryStore_Verify(t *testing.T) {
thisStore := NewMemoryStore(10, time.Hour)
_ = thisStore.Set("xx", "xx")
got := thisStore.Verify("xx", "xx", false)
if !got {
t.Error("failed1")
}
got = thisStore.Verify("xx", "xx", true)
if !got {
t.Error("failed2")
}
got = thisStore.Verify("xx", "xx", true)
if got {
t.Error("failed3")
}
got = DefaultMemStore.Verify("saaf", "", true)
if got {
t.Error("CVE-2023-45292 GO-2023-2386")
}
}
func Test_memoryStore_Get(t *testing.T) {
thisStore := NewMemoryStore(10, time.Hour)
_ = thisStore.Set("xx", "xx")
got := thisStore.Get("xx", false)
if got != "xx" {
t.Error("failed1")
}
got = thisStore.Get("xx", true)
if got != "xx" {
t.Error("failed2")
}
got = thisStore.Get("xx", false)
if got == "xx" {
t.Error("failed3")
}
}