Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
二次元的罪恶王冠
beego
提交
5940ae33
B
beego
项目概览
二次元的罪恶王冠
/
beego
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
B
beego
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
5940ae33
编写于
7月 13, 2020
作者:
J
jianzhiyao
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix `index out of range` when sid len = 1
add unit test for sess_file.go
上级
3db31385
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
392 addition
and
1 deletion
+392
-1
session/sess_file.go
session/sess_file.go
+5
-1
session/sess_file_test.go
session/sess_file_test.go
+387
-0
未找到文件。
session/sess_file.go
浏览文件 @
5940ae33
...
...
@@ -15,11 +15,11 @@
package
session
import
(
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"errors"
"path"
"path/filepath"
"strings"
...
...
@@ -180,6 +180,10 @@ func (fp *FileProvider) SessionExist(sid string) bool {
filepder
.
lock
.
Lock
()
defer
filepder
.
lock
.
Unlock
()
if
len
(
sid
)
<
2
{
return
false
}
_
,
err
:=
os
.
Stat
(
path
.
Join
(
fp
.
savePath
,
string
(
sid
[
0
]),
string
(
sid
[
1
]),
sid
))
return
err
==
nil
}
...
...
session/sess_file_test.go
0 → 100644
浏览文件 @
5940ae33
// 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
(
"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
{}
_
=
fp
.
SessionInit
(
180
,
sessionPath
)
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
{}
_
=
fp
.
SessionInit
(
180
,
sessionPath
)
if
fp
.
SessionExist
(
sid
)
{
t
.
Error
()
}
_
,
err
:=
fp
.
SessionRead
(
sid
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
if
!
fp
.
SessionExist
(
sid
)
{
t
.
Error
()
}
}
func
TestFileProvider_SessionExist2
(
t
*
testing
.
T
)
{
mutex
.
Lock
()
defer
mutex
.
Unlock
()
os
.
RemoveAll
(
sessionPath
)
defer
os
.
RemoveAll
(
sessionPath
)
fp
:=
&
FileProvider
{}
_
=
fp
.
SessionInit
(
180
,
sessionPath
)
if
fp
.
SessionExist
(
sid
)
{
t
.
Error
()
}
if
fp
.
SessionExist
(
""
)
{
t
.
Error
()
}
if
fp
.
SessionExist
(
"1"
)
{
t
.
Error
()
}
}
func
TestFileProvider_SessionRead
(
t
*
testing
.
T
)
{
mutex
.
Lock
()
defer
mutex
.
Unlock
()
os
.
RemoveAll
(
sessionPath
)
defer
os
.
RemoveAll
(
sessionPath
)
fp
:=
&
FileProvider
{}
_
=
fp
.
SessionInit
(
180
,
sessionPath
)
s
,
err
:=
fp
.
SessionRead
(
sid
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
_
=
s
.
Set
(
"sessionValue"
,
18975
)
v
:=
s
.
Get
(
"sessionValue"
)
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
{}
_
=
fp
.
SessionInit
(
180
,
sessionPath
)
_
,
err
:=
fp
.
SessionRead
(
""
)
if
err
==
nil
{
t
.
Error
(
err
)
}
_
,
err
=
fp
.
SessionRead
(
"1"
)
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
{}
_
=
fp
.
SessionInit
(
180
,
sessionPath
)
sessionCount
:=
546
for
i
:=
1
;
i
<=
sessionCount
;
i
++
{
_
,
err
:=
fp
.
SessionRead
(
fmt
.
Sprintf
(
"%s_%d"
,
sid
,
i
))
if
err
!=
nil
{
t
.
Error
(
err
)
}
}
if
fp
.
SessionAll
()
!=
sessionCount
{
t
.
Error
()
}
}
func
TestFileProvider_SessionRegenerate
(
t
*
testing
.
T
)
{
mutex
.
Lock
()
defer
mutex
.
Unlock
()
os
.
RemoveAll
(
sessionPath
)
defer
os
.
RemoveAll
(
sessionPath
)
fp
:=
&
FileProvider
{}
_
=
fp
.
SessionInit
(
180
,
sessionPath
)
_
,
err
:=
fp
.
SessionRead
(
sid
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
if
!
fp
.
SessionExist
(
sid
)
{
t
.
Error
()
}
_
,
err
=
fp
.
SessionRegenerate
(
sid
,
sidNew
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
if
fp
.
SessionExist
(
sid
)
{
t
.
Error
()
}
if
!
fp
.
SessionExist
(
sidNew
)
{
t
.
Error
()
}
}
func
TestFileProvider_SessionDestroy
(
t
*
testing
.
T
)
{
mutex
.
Lock
()
defer
mutex
.
Unlock
()
os
.
RemoveAll
(
sessionPath
)
defer
os
.
RemoveAll
(
sessionPath
)
fp
:=
&
FileProvider
{}
_
=
fp
.
SessionInit
(
180
,
sessionPath
)
_
,
err
:=
fp
.
SessionRead
(
sid
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
if
!
fp
.
SessionExist
(
sid
)
{
t
.
Error
()
}
err
=
fp
.
SessionDestroy
(
sid
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
if
fp
.
SessionExist
(
sid
)
{
t
.
Error
()
}
}
func
TestFileProvider_SessionGC
(
t
*
testing
.
T
)
{
mutex
.
Lock
()
defer
mutex
.
Unlock
()
os
.
RemoveAll
(
sessionPath
)
defer
os
.
RemoveAll
(
sessionPath
)
fp
:=
&
FileProvider
{}
_
=
fp
.
SessionInit
(
1
,
sessionPath
)
sessionCount
:=
412
for
i
:=
1
;
i
<=
sessionCount
;
i
++
{
_
,
err
:=
fp
.
SessionRead
(
fmt
.
Sprintf
(
"%s_%d"
,
sid
,
i
))
if
err
!=
nil
{
t
.
Error
(
err
)
}
}
time
.
Sleep
(
2
*
time
.
Second
)
fp
.
SessionGC
()
if
fp
.
SessionAll
()
!=
0
{
t
.
Error
()
}
}
func
TestFileSessionStore_Set
(
t
*
testing
.
T
)
{
mutex
.
Lock
()
defer
mutex
.
Unlock
()
os
.
RemoveAll
(
sessionPath
)
defer
os
.
RemoveAll
(
sessionPath
)
fp
:=
&
FileProvider
{}
_
=
fp
.
SessionInit
(
180
,
sessionPath
)
sessionCount
:=
100
s
,
_
:=
fp
.
SessionRead
(
sid
)
for
i
:=
1
;
i
<=
sessionCount
;
i
++
{
err
:=
s
.
Set
(
i
,
i
)
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
{}
_
=
fp
.
SessionInit
(
180
,
sessionPath
)
sessionCount
:=
100
s
,
_
:=
fp
.
SessionRead
(
sid
)
for
i
:=
1
;
i
<=
sessionCount
;
i
++
{
_
=
s
.
Set
(
i
,
i
)
v
:=
s
.
Get
(
i
)
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
{}
_
=
fp
.
SessionInit
(
180
,
sessionPath
)
s
,
_
:=
fp
.
SessionRead
(
sid
)
s
.
Set
(
"1"
,
1
)
if
s
.
Get
(
"1"
)
==
nil
{
t
.
Error
()
}
s
.
Delete
(
"1"
)
if
s
.
Get
(
"1"
)
!=
nil
{
t
.
Error
()
}
}
func
TestFileSessionStore_Flush
(
t
*
testing
.
T
)
{
mutex
.
Lock
()
defer
mutex
.
Unlock
()
os
.
RemoveAll
(
sessionPath
)
defer
os
.
RemoveAll
(
sessionPath
)
fp
:=
&
FileProvider
{}
_
=
fp
.
SessionInit
(
180
,
sessionPath
)
sessionCount
:=
100
s
,
_
:=
fp
.
SessionRead
(
sid
)
for
i
:=
1
;
i
<=
sessionCount
;
i
++
{
_
=
s
.
Set
(
i
,
i
)
}
_
=
s
.
Flush
()
for
i
:=
1
;
i
<=
sessionCount
;
i
++
{
if
s
.
Get
(
i
)
!=
nil
{
t
.
Error
()
}
}
}
func
TestFileSessionStore_SessionID
(
t
*
testing
.
T
)
{
mutex
.
Lock
()
defer
mutex
.
Unlock
()
os
.
RemoveAll
(
sessionPath
)
defer
os
.
RemoveAll
(
sessionPath
)
fp
:=
&
FileProvider
{}
_
=
fp
.
SessionInit
(
180
,
sessionPath
)
sessionCount
:=
85
for
i
:=
1
;
i
<=
sessionCount
;
i
++
{
s
,
err
:=
fp
.
SessionRead
(
fmt
.
Sprintf
(
"%s_%d"
,
sid
,
i
))
if
err
!=
nil
{
t
.
Error
(
err
)
}
if
s
.
SessionID
()
!=
fmt
.
Sprintf
(
"%s_%d"
,
sid
,
i
)
{
t
.
Error
(
err
)
}
}
}
func
TestFileSessionStore_SessionRelease
(
t
*
testing
.
T
)
{
mutex
.
Lock
()
defer
mutex
.
Unlock
()
os
.
RemoveAll
(
sessionPath
)
defer
os
.
RemoveAll
(
sessionPath
)
fp
:=
&
FileProvider
{}
_
=
fp
.
SessionInit
(
180
,
sessionPath
)
filepder
.
savePath
=
sessionPath
sessionCount
:=
85
for
i
:=
1
;
i
<=
sessionCount
;
i
++
{
s
,
err
:=
fp
.
SessionRead
(
fmt
.
Sprintf
(
"%s_%d"
,
sid
,
i
))
if
err
!=
nil
{
t
.
Error
(
err
)
}
s
.
Set
(
i
,
i
)
s
.
SessionRelease
(
nil
)
}
for
i
:=
1
;
i
<=
sessionCount
;
i
++
{
s
,
err
:=
fp
.
SessionRead
(
fmt
.
Sprintf
(
"%s_%d"
,
sid
,
i
))
if
err
!=
nil
{
t
.
Error
(
err
)
}
if
s
.
Get
(
i
)
.
(
int
)
!=
i
{
t
.
Error
()
}
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录