-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuntdbstorage_test.go
143 lines (120 loc) · 4.27 KB
/
buntdbstorage_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
package sessions
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/twinj/uuid"
)
func assertSessDataWithProvidedAttributes(assert *require.Assertions, sess SessionData, key string, ipAddr string, expiryIn int64) {
assert.Equal(sess.Key, key)
assert.NotZero(len(sess.Token))
// assert.NotZero(sess.ExpiryIn)
assert.True(sess.ExpiryIn < expiryIn)
assert.NotNil(sess.ExpiresAt)
assert.Equal(sess.IP, ipAddr)
assert.NotZero(sess.Timestamp)
}
func shortBuntDBSessionTest(assert *require.Assertions) {
// Init a new session with a small timeout
shortTimeout := int64(2)
cwd, _ := os.Getwd()
shortBuntDBSession, err := Init("csrf", false, shortTimeout, cwd)
// Assert all the parameters
assert.Nil(err)
assert.NotNil(shortBuntDBSession.buntDB)
assert.Equal(shortTimeout, shortBuntDBSession.ExpiryTime)
// Create a new session
var key = uuid.NewV4().String()
newSess, err := shortBuntDBSession.NewSession(key, "127.0.0.1")
assert.Nil(err)
// Assert parameters and the fact that the authtoken is still alive
assertSessDataWithProvidedAttributes(assert, newSess, key, "127.0.0.1", shortTimeout)
// Ensure that the key is deleted after this timeout
<-time.After(time.Second * time.Duration(shortTimeout))
// Fetch the session data here, after the timeout and assert that active is false
_, err = shortBuntDBSession.FetchSessionData(SessionData{Token: newSess.Token})
assert.NotNil(err, newSess.Token)
newSess, err = shortBuntDBSession.CheckStatus(newSess.Token)
assert.NotNil(err)
assert.Nil(shortBuntDBSession.buntDB.Close())
_, err = shortBuntDBSession.NewSession(key, "127.0.0.1")
assert.NotNil(err)
}
func longBuntDBSessionTest(assert *require.Assertions) {
// Init a new session with 0 timeout -> and assert that the expiry time is 86400
var longTimeout = int64(86400)
cwd, _ := os.Getwd()
longBuntDBSession, err := Init("sessions", true, 0, cwd)
assert.Nil(err)
assert.NotNil(longBuntDBSession.buntDB)
assert.Equal(longTimeout, longBuntDBSession.ExpiryTime)
// Create new session
var key = uuid.NewV4().String()
newSess, err := longBuntDBSession.NewSession(key, "127.0.0.1")
assert.Nil(err)
// Assert parameters and the fact that the authtoken is still alive
assertSessDataWithProvidedAttributes(assert, newSess, key, "127.0.0.1", longTimeout)
newSess, err = longBuntDBSession.CheckStatus(newSess.Token)
assert.Nil(err)
newSess2, err := longBuntDBSession.FetchSessionData(SessionData{Token: newSess.Token})
assert.Nil(err, newSess2.Token)
assert.Equal(newSess.Key, newSess2.Key)
assert.Equal(newSess.Token, newSess2.Token)
assert.Equal(newSess.ExpiryIn, newSess2.ExpiryIn)
assert.Equal(newSess.ExpiresAt, newSess2.ExpiresAt)
assert.Equal(newSess.IP, newSess2.IP)
assert.Equal(newSess.Timestamp, newSess2.Timestamp)
assertSessDataWithProvidedAttributes(assert, newSess2, key, "127.0.0.1", longTimeout)
// Manually delete the session using the API
err = longBuntDBSession.DeleteSession(newSess.Token)
assert.Nil(err)
// Ensure that this session no longer exists
newSess, err = longBuntDBSession.CheckStatus(newSess.Token)
assert.NotNil(err)
}
func TestBuntDBSessions(t *testing.T) {
assert := require.New(t)
shortBuntDBSessionTest(assert)
longBuntDBSessionTest(assert)
}
func BenchmarkBuntDBShort(b *testing.B) {
t := new(testing.T)
assert := require.New(t)
shortTimeout := int64(2)
cwd, _ := os.Getwd()
shortBuntDBSession, err := Init("csrf", false, shortTimeout, cwd)
assert.Nil(err)
for n := 0; n < b.N; n++ {
newSess, err := shortBuntDBSession.NewSession(uuid.NewV4().String(), "127.0.0.1")
assert.Nil(err)
assert.NotZero(len(newSess.Token))
}
}
func BenchmarkBuntDBShortFull(b *testing.B) {
t := new(testing.T)
assert := require.New(t)
for n := 0; n < b.N; n++ {
shortBuntDBSessionTest(assert)
}
}
func BenchmarkBuntDBLong(b *testing.B) {
t := new(testing.T)
assert := require.New(t)
cwd, _ := os.Getwd()
longBuntDBSession, err := Init("sessions", true, 0, cwd)
assert.Nil(err)
for n := 0; n < b.N; n++ {
var key = uuid.NewV4().String()
newSess, err := longBuntDBSession.NewSession(key, "127.0.0.1")
assert.Nil(err)
assert.NotZero(len(newSess.Token))
}
}
func BenchmarkBuntDBLongFull(b *testing.B) {
t := new(testing.T)
assert := require.New(t)
for n := 0; n < b.N; n++ {
longBuntDBSessionTest(assert)
}
}