/
sessionstore_test.go
201 lines (177 loc) · 4.8 KB
/
sessionstore_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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* Copyright (C) 2015 Fabrício Godoy <skarllot@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package web
import (
"testing"
"time"
"gopkg.in/raiqub/data.v0/memstore"
)
const TokenSalt = "CvoTVwDw685Ve0qjGn//zmHGKvoCcslYNQT4AQ9FygSk9t6NuzBHuohyO" +
"Hhqb/1omn6c"
func TestSessionLifetime(t *testing.T) {
store := memstore.New(time.Millisecond*10, false)
ts := NewSessionStore().
SalterFast([]byte(TokenSalt)).
Store(store).
Build()
t1, err := ts.Add(nil)
if err != nil {
t.Errorf("The session t1 could not be generated: %v", err)
}
t2, err := ts.Add(nil)
if err != nil {
t.Errorf("The session t2 could not be generated: %v", err)
}
if err := ts.Get(t1, nil); err != nil {
t.Error("The session t1 was not stored")
}
if err := ts.Get(t2, nil); err != nil {
t.Error("The session t2 was not stored")
}
time.Sleep(time.Millisecond * 20)
if err := ts.Get(t1, nil); err == nil {
t.Error("The session t1 was not expired")
}
if err := ts.Get(t2, nil); err == nil {
t.Error("The session t2 was not expired")
}
if err := ts.Delete(t1); err == nil {
t.Error("The expired session t1 should not be removable")
}
if err := ts.Set(t2, nil); err == nil {
t.Error("The expired session t2 should not be changeable")
}
}
func TestSessionHandling(t *testing.T) {
testValues := []struct {
ref string
token string
value int
}{
{"t1", "", 3},
{"t2", "", 6},
{"t3", "", 83679},
{"t4", "", 2748},
{"t5", "", 54},
{"t6", "", 6},
{"t7", "", 2},
{"t8", "", 8},
{"t9", "", 7},
{"t10", "", 8},
}
rmTestIndex := 6
changeValues := map[int]int{
2: 5062,
9: 4099,
}
store := memstore.New(time.Millisecond*100, false)
ts := NewSessionStore().
SalterFast([]byte(TokenSalt)).
Store(store).
Build()
if _, err := ts.Count(); err != nil {
t.Fatal("The Count() method should be supported by MemStore")
return
}
if count, _ := ts.Count(); count != 0 {
t.Errorf(
"The session cache should be empty, but it has %d items",
count)
}
lastCount, _ := ts.Count()
for i := range testValues {
var err error
item := &testValues[i]
item.token, err = ts.Add(nil)
if err != nil {
t.Errorf("A new session could not be generated: %v", err)
}
if count, _ := ts.Count(); count != lastCount+1 {
t.Errorf(
"The new session '%s' was not stored into session cache",
item.token)
}
lastCount, _ = ts.Count()
err = ts.Set(item.token, item.value)
if err != nil {
t.Errorf("The session %s could not be set", item.ref)
}
}
if count, _ := ts.Count(); count != len(testValues) {
t.Errorf("The session count do not match (%d!=%d)",
count, len(testValues))
}
for _, i := range testValues {
var v int
if err := ts.Get(i.token, &v); err != nil {
t.Errorf("The session %s could not be read", i.ref)
}
if v != i.value {
t.Errorf("The session %s was stored incorrectly", i.ref)
}
}
rmTestKey := testValues[rmTestIndex]
if err := ts.Delete(rmTestKey.token); err != nil {
t.Errorf("The session %s could not be removed", rmTestKey.ref)
}
if err := ts.Get(rmTestKey.token, nil); err == nil {
t.Errorf("The removed session %s should not be retrieved", rmTestKey.ref)
}
if count, _ := ts.Count(); count == len(testValues) {
t.Error("The session count should not match")
}
for k, v := range changeValues {
item := testValues[k]
err := ts.Set(item.token, v)
if err != nil {
t.Errorf("The session %s could not be changed", item.ref)
}
}
for k, v := range changeValues {
item := testValues[k]
var v2 int
if err := ts.Get(item.token, &v2); err != nil {
t.Errorf("The session %s could not be read", item.ref)
}
if v2 != v {
t.Errorf("The session %s was not changed", item.ref)
}
}
}
func BenchmarkSessionCreation(b *testing.B) {
store := memstore.New(time.Millisecond, false)
ts := NewSessionStore().
SalterSecure([]byte(TokenSalt)).
Store(store).
Build()
b.ResetTimer()
for i := 0; i < b.N; i++ {
ts.Add(nil)
}
}
func BenchmarkSessionCreationFast(b *testing.B) {
store := memstore.New(time.Millisecond, false)
ts := NewSessionStore().
SalterFast([]byte(TokenSalt)).
Store(store).
Build()
b.ResetTimer()
for i := 0; i < b.N; i++ {
ts.Add(nil)
}
}