1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-01 07:20:56 +00:00
Beego/server/web/session/sess_file_test.go

428 lines
8.3 KiB
Go
Raw Normal View History

2020-07-22 14:50:08 +00:00
// Copyright 2014 beego Author. 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 session
import (
2020-08-30 15:39:07 +00:00
"context"
2020-07-22 14:50:08 +00:00
"fmt"
"os"
"sync"
"testing"
"time"
)
const sid = "Session_id"
const sidNew = "Session_id_new"
const sessionPath = "./_session_runtime"
var (
mutex sync.Mutex
)
func TestFileProvider_SessionInit(t *testing.T) {
mutex.Lock()
defer mutex.Unlock()
os.RemoveAll(sessionPath)
defer os.RemoveAll(sessionPath)
fp := &FileProvider{}
2020-08-30 15:39:07 +00:00
_ = fp.SessionInit(context.Background(), 180, sessionPath)
2020-07-22 14:50:08 +00:00
if fp.maxlifetime != 180 {
t.Error()
}
if fp.savePath != sessionPath {
t.Error()
}
}
func TestFileProvider_SessionExist(t *testing.T) {
mutex.Lock()
defer mutex.Unlock()
os.RemoveAll(sessionPath)
defer os.RemoveAll(sessionPath)
fp := &FileProvider{}
2020-08-30 15:39:07 +00:00
_ = fp.SessionInit(context.Background(), 180, sessionPath)
2020-07-22 14:50:08 +00:00
2020-08-30 15:39:07 +00:00
exists, err := fp.SessionExist(context.Background(), sid)
2020-08-10 15:04:57 +00:00
if err != nil {
2020-08-05 16:29:22 +00:00
t.Error(err)
}
if exists {
2020-07-22 14:50:08 +00:00
t.Error()
}
2020-08-30 15:39:07 +00:00
_, err = fp.SessionRead(context.Background(), sid)
2020-07-22 14:50:08 +00:00
if err != nil {
t.Error(err)
}
2020-08-30 15:39:07 +00:00
exists, err = fp.SessionExist(context.Background(), sid)
2020-08-05 16:29:22 +00:00
if err != nil {
t.Error(err)
}
if !exists {
2020-07-22 14:50:08 +00:00
t.Error()
}
}
func TestFileProvider_SessionExist2(t *testing.T) {
mutex.Lock()
defer mutex.Unlock()
os.RemoveAll(sessionPath)
defer os.RemoveAll(sessionPath)
fp := &FileProvider{}
2020-08-30 15:39:07 +00:00
_ = fp.SessionInit(context.Background(), 180, sessionPath)
2020-07-22 14:50:08 +00:00
2020-08-30 15:39:07 +00:00
exists, err := fp.SessionExist(context.Background(), sid)
2020-08-05 16:29:22 +00:00
if err != nil {
t.Error(err)
}
if exists {
2020-07-22 14:50:08 +00:00
t.Error()
}
2020-08-30 15:39:07 +00:00
exists, err = fp.SessionExist(context.Background(), "")
2020-08-05 16:29:22 +00:00
if err == nil {
t.Error()
}
if exists {
2020-07-22 14:50:08 +00:00
t.Error()
}
2020-08-30 15:39:07 +00:00
exists, err = fp.SessionExist(context.Background(), "1")
2020-08-05 16:29:22 +00:00
if err == nil {
t.Error()
}
if exists {
2020-07-22 14:50:08 +00:00
t.Error()
}
}
func TestFileProvider_SessionRead(t *testing.T) {
mutex.Lock()
defer mutex.Unlock()
os.RemoveAll(sessionPath)
defer os.RemoveAll(sessionPath)
fp := &FileProvider{}
2020-08-30 15:39:07 +00:00
_ = fp.SessionInit(context.Background(), 180, sessionPath)
2020-07-22 14:50:08 +00:00
2020-08-30 15:39:07 +00:00
s, err := fp.SessionRead(context.Background(), sid)
2020-07-22 14:50:08 +00:00
if err != nil {
t.Error(err)
}
2020-08-30 15:39:07 +00:00
_ = s.Set(nil, "sessionValue", 18975)
v := s.Get(nil, "sessionValue")
2020-07-22 14:50:08 +00:00
if v.(int) != 18975 {
t.Error()
}
}
func TestFileProvider_SessionRead1(t *testing.T) {
mutex.Lock()
defer mutex.Unlock()
os.RemoveAll(sessionPath)
defer os.RemoveAll(sessionPath)
fp := &FileProvider{}
2020-08-30 15:39:07 +00:00
_ = fp.SessionInit(context.Background(), 180, sessionPath)
2020-07-22 14:50:08 +00:00
2020-08-30 15:39:07 +00:00
_, err := fp.SessionRead(context.Background(), "")
2020-07-22 14:50:08 +00:00
if err == nil {
t.Error(err)
}
2020-08-30 15:39:07 +00:00
_, err = fp.SessionRead(context.Background(), "1")
2020-07-22 14:50:08 +00:00
if err == nil {
t.Error(err)
}
}
func TestFileProvider_SessionAll(t *testing.T) {
mutex.Lock()
defer mutex.Unlock()
os.RemoveAll(sessionPath)
defer os.RemoveAll(sessionPath)
fp := &FileProvider{}
2020-08-30 15:39:07 +00:00
_ = fp.SessionInit(context.Background(), 180, sessionPath)
2020-07-22 14:50:08 +00:00
sessionCount := 546
for i := 1; i <= sessionCount; i++ {
2020-08-30 15:39:07 +00:00
_, err := fp.SessionRead(context.Background(), fmt.Sprintf("%s_%d", sid, i))
2020-07-22 14:50:08 +00:00
if err != nil {
t.Error(err)
}
}
2020-08-30 15:39:07 +00:00
if fp.SessionAll(nil) != sessionCount {
2020-07-22 14:50:08 +00:00
t.Error()
}
}
func TestFileProvider_SessionRegenerate(t *testing.T) {
mutex.Lock()
defer mutex.Unlock()
os.RemoveAll(sessionPath)
defer os.RemoveAll(sessionPath)
fp := &FileProvider{}
2020-08-30 15:39:07 +00:00
_ = fp.SessionInit(context.Background(), 180, sessionPath)
2020-07-22 14:50:08 +00:00
2020-08-30 15:39:07 +00:00
_, err := fp.SessionRead(context.Background(), sid)
2020-07-22 14:50:08 +00:00
if err != nil {
t.Error(err)
}
2020-08-30 15:39:07 +00:00
exists, err := fp.SessionExist(context.Background(), sid)
2020-08-05 16:29:22 +00:00
if err != nil {
t.Error(err)
}
if !exists {
2020-07-22 14:50:08 +00:00
t.Error()
}
2020-08-30 15:39:07 +00:00
_, err = fp.SessionRegenerate(context.Background(), sid, sidNew)
2020-07-22 14:50:08 +00:00
if err != nil {
t.Error(err)
}
2020-08-30 15:39:07 +00:00
exists, err = fp.SessionExist(context.Background(), sid)
2020-08-05 16:29:22 +00:00
if err != nil {
t.Error(err)
}
if exists {
2020-07-22 14:50:08 +00:00
t.Error()
}
2020-08-30 15:39:07 +00:00
exists, err = fp.SessionExist(context.Background(), sidNew)
2020-08-05 16:29:22 +00:00
if err != nil {
t.Error(err)
}
if !exists {
2020-07-22 14:50:08 +00:00
t.Error()
}
}
func TestFileProvider_SessionDestroy(t *testing.T) {
mutex.Lock()
defer mutex.Unlock()
os.RemoveAll(sessionPath)
defer os.RemoveAll(sessionPath)
fp := &FileProvider{}
2020-08-30 15:39:07 +00:00
_ = fp.SessionInit(context.Background(), 180, sessionPath)
2020-07-22 14:50:08 +00:00
2020-08-30 15:39:07 +00:00
_, err := fp.SessionRead(context.Background(), sid)
2020-07-22 14:50:08 +00:00
if err != nil {
t.Error(err)
}
2020-08-30 15:39:07 +00:00
exists, err := fp.SessionExist(context.Background(), sid)
2020-08-05 16:29:22 +00:00
if err != nil {
t.Error(err)
}
if !exists {
2020-07-22 14:50:08 +00:00
t.Error()
}
2020-08-30 15:39:07 +00:00
err = fp.SessionDestroy(context.Background(), sid)
2020-07-22 14:50:08 +00:00
if err != nil {
t.Error(err)
}
2020-08-30 15:39:07 +00:00
exists, err = fp.SessionExist(context.Background(), sid)
2020-08-05 16:29:22 +00:00
if err != nil {
t.Error(err)
}
if exists {
2020-07-22 14:50:08 +00:00
t.Error()
}
}
func TestFileProvider_SessionGC(t *testing.T) {
mutex.Lock()
defer mutex.Unlock()
os.RemoveAll(sessionPath)
defer os.RemoveAll(sessionPath)
fp := &FileProvider{}
2020-08-30 15:39:07 +00:00
_ = fp.SessionInit(context.Background(), 1, sessionPath)
2020-07-22 14:50:08 +00:00
sessionCount := 412
for i := 1; i <= sessionCount; i++ {
2020-08-30 15:39:07 +00:00
_, err := fp.SessionRead(context.Background(), fmt.Sprintf("%s_%d", sid, i))
2020-07-22 14:50:08 +00:00
if err != nil {
t.Error(err)
}
}
time.Sleep(2 * time.Second)
2020-08-30 15:39:07 +00:00
fp.SessionGC(nil)
if fp.SessionAll(nil) != 0 {
2020-07-22 14:50:08 +00:00
t.Error()
}
}
func TestFileSessionStore_Set(t *testing.T) {
mutex.Lock()
defer mutex.Unlock()
os.RemoveAll(sessionPath)
defer os.RemoveAll(sessionPath)
fp := &FileProvider{}
2020-08-30 15:39:07 +00:00
_ = fp.SessionInit(context.Background(), 180, sessionPath)
2020-07-22 14:50:08 +00:00
sessionCount := 100
2020-08-30 15:39:07 +00:00
s, _ := fp.SessionRead(context.Background(), sid)
2020-07-22 14:50:08 +00:00
for i := 1; i <= sessionCount; i++ {
2020-08-30 15:39:07 +00:00
err := s.Set(nil, i, i)
2020-07-22 14:50:08 +00:00
if err != nil {
t.Error(err)
}
}
}
func TestFileSessionStore_Get(t *testing.T) {
mutex.Lock()
defer mutex.Unlock()
os.RemoveAll(sessionPath)
defer os.RemoveAll(sessionPath)
fp := &FileProvider{}
2020-08-30 15:39:07 +00:00
_ = fp.SessionInit(context.Background(), 180, sessionPath)
2020-07-22 14:50:08 +00:00
sessionCount := 100
2020-08-30 15:39:07 +00:00
s, _ := fp.SessionRead(context.Background(), sid)
2020-07-22 14:50:08 +00:00
for i := 1; i <= sessionCount; i++ {
2020-08-30 15:39:07 +00:00
_ = s.Set(nil, i, i)
2020-07-22 14:50:08 +00:00
2020-08-30 15:39:07 +00:00
v := s.Get(nil, i)
2020-07-22 14:50:08 +00:00
if v.(int) != i {
t.Error()
}
}
}
func TestFileSessionStore_Delete(t *testing.T) {
mutex.Lock()
defer mutex.Unlock()
os.RemoveAll(sessionPath)
defer os.RemoveAll(sessionPath)
fp := &FileProvider{}
2020-08-30 15:39:07 +00:00
_ = fp.SessionInit(context.Background(), 180, sessionPath)
2020-07-22 14:50:08 +00:00
2020-08-30 15:39:07 +00:00
s, _ := fp.SessionRead(context.Background(), sid)
s.Set(nil, "1", 1)
2020-07-22 14:50:08 +00:00
2020-08-30 15:39:07 +00:00
if s.Get(nil, "1") == nil {
2020-07-22 14:50:08 +00:00
t.Error()
}
2020-08-30 15:39:07 +00:00
s.Delete(nil, "1")
2020-07-22 14:50:08 +00:00
2020-08-30 15:39:07 +00:00
if s.Get(nil, "1") != nil {
2020-07-22 14:50:08 +00:00
t.Error()
}
}
func TestFileSessionStore_Flush(t *testing.T) {
mutex.Lock()
defer mutex.Unlock()
os.RemoveAll(sessionPath)
defer os.RemoveAll(sessionPath)
fp := &FileProvider{}
2020-08-30 15:39:07 +00:00
_ = fp.SessionInit(context.Background(), 180, sessionPath)
2020-07-22 14:50:08 +00:00
sessionCount := 100
2020-08-30 15:39:07 +00:00
s, _ := fp.SessionRead(context.Background(), sid)
2020-07-22 14:50:08 +00:00
for i := 1; i <= sessionCount; i++ {
2020-08-30 15:39:07 +00:00
_ = s.Set(nil, i, i)
2020-07-22 14:50:08 +00:00
}
2020-08-30 15:39:07 +00:00
_ = s.Flush(nil)
2020-07-22 14:50:08 +00:00
for i := 1; i <= sessionCount; i++ {
2020-08-30 15:39:07 +00:00
if s.Get(nil, i) != nil {
2020-07-22 14:50:08 +00:00
t.Error()
}
}
}
func TestFileSessionStore_SessionID(t *testing.T) {
mutex.Lock()
defer mutex.Unlock()
os.RemoveAll(sessionPath)
defer os.RemoveAll(sessionPath)
fp := &FileProvider{}
2020-08-30 15:39:07 +00:00
_ = fp.SessionInit(context.Background(), 180, sessionPath)
2020-07-22 14:50:08 +00:00
sessionCount := 85
for i := 1; i <= sessionCount; i++ {
2020-08-30 15:39:07 +00:00
s, err := fp.SessionRead(context.Background(), fmt.Sprintf("%s_%d", sid, i))
2020-07-22 14:50:08 +00:00
if err != nil {
t.Error(err)
}
2020-08-30 15:39:07 +00:00
if s.SessionID(nil) != fmt.Sprintf("%s_%d", sid, i) {
2020-07-22 14:50:08 +00:00
t.Error(err)
}
}
}
func TestFileSessionStore_SessionRelease(t *testing.T) {
mutex.Lock()
defer mutex.Unlock()
os.RemoveAll(sessionPath)
defer os.RemoveAll(sessionPath)
fp := &FileProvider{}
2020-08-30 15:39:07 +00:00
_ = fp.SessionInit(context.Background(), 180, sessionPath)
2020-07-22 14:50:08 +00:00
filepder.savePath = sessionPath
sessionCount := 85
for i := 1; i <= sessionCount; i++ {
2020-08-30 15:39:07 +00:00
s, err := fp.SessionRead(context.Background(), fmt.Sprintf("%s_%d", sid, i))
2020-07-22 14:50:08 +00:00
if err != nil {
t.Error(err)
}
2020-08-30 15:39:07 +00:00
s.Set(nil, i, i)
s.SessionRelease(nil, nil)
2020-07-22 14:50:08 +00:00
}
for i := 1; i <= sessionCount; i++ {
2020-08-30 15:39:07 +00:00
s, err := fp.SessionRead(context.Background(), fmt.Sprintf("%s_%d", sid, i))
2020-07-22 14:50:08 +00:00
if err != nil {
t.Error(err)
}
2020-08-30 15:39:07 +00:00
if s.Get(nil, i).(int) != i {
2020-07-22 14:50:08 +00:00
t.Error()
}
}
2020-07-22 15:00:06 +00:00
}