2014-04-12 05:18:18 +00:00
|
|
|
// Beego (http://beego.me/)
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-04-12 05:18:18 +00:00
|
|
|
// @description beego is an open-source, high-performance web framework for the Go programming language.
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-04-12 05:18:18 +00:00
|
|
|
// @link http://github.com/astaxie/beego for the canonical source repository
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-04-12 05:18:18 +00:00
|
|
|
// @license http://github.com/astaxie/beego/blob/master/LICENSE
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-04-12 05:18:18 +00:00
|
|
|
// @authors astaxie
|
2013-05-08 14:31:23 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
2014-01-05 06:48:36 +00:00
|
|
|
"crypto/aes"
|
|
|
|
"encoding/json"
|
2013-05-08 14:31:23 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_gob(t *testing.T) {
|
|
|
|
a := make(map[interface{}]interface{})
|
|
|
|
a["username"] = "astaxie"
|
|
|
|
a[12] = 234
|
2014-05-16 16:43:51 +00:00
|
|
|
a["user"] = User{"asta", "xie"}
|
2014-04-04 02:16:34 +00:00
|
|
|
b, err := EncodeGob(a)
|
2013-05-08 14:31:23 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2014-04-04 02:16:34 +00:00
|
|
|
c, err := DecodeGob(b)
|
2013-05-08 14:31:23 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if len(c) == 0 {
|
|
|
|
t.Error("decodeGob empty")
|
|
|
|
}
|
|
|
|
if c["username"] != "astaxie" {
|
|
|
|
t.Error("decode string error")
|
|
|
|
}
|
|
|
|
if c[12] != 234 {
|
|
|
|
t.Error("decode int error")
|
|
|
|
}
|
2014-05-16 16:43:51 +00:00
|
|
|
if c["user"].(User).Username != "asta" {
|
|
|
|
t.Error("decode struct error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Username string
|
|
|
|
NickName string
|
2013-05-08 14:31:23 +00:00
|
|
|
}
|
2014-01-05 06:48:36 +00:00
|
|
|
|
|
|
|
func TestGenerate(t *testing.T) {
|
|
|
|
str := generateRandomKey(20)
|
|
|
|
if len(str) != 20 {
|
|
|
|
t.Fatal("generate length is not equal to 20")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCookieEncodeDecode(t *testing.T) {
|
|
|
|
hashKey := "testhashKey"
|
|
|
|
blockkey := generateRandomKey(16)
|
|
|
|
block, err := aes.NewCipher(blockkey)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("NewCipher:", err)
|
|
|
|
}
|
|
|
|
securityName := string(generateRandomKey(20))
|
|
|
|
val := make(map[interface{}]interface{})
|
|
|
|
val["name"] = "astaxie"
|
|
|
|
val["gender"] = "male"
|
|
|
|
str, err := encodeCookie(block, hashKey, securityName, val)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("encodeCookie:", err)
|
|
|
|
}
|
|
|
|
dst := make(map[interface{}]interface{})
|
|
|
|
dst, err = decodeCookie(block, hashKey, securityName, str, 3600)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("decodeCookie", err)
|
|
|
|
}
|
|
|
|
if dst["name"] != "astaxie" {
|
|
|
|
t.Fatal("dst get map error")
|
|
|
|
}
|
|
|
|
if dst["gender"] != "male" {
|
|
|
|
t.Fatal("dst get map error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseConfig(t *testing.T) {
|
|
|
|
s := `{"cookieName":"gosessionid","gclifetime":3600}`
|
|
|
|
cf := new(managerConfig)
|
|
|
|
cf.EnableSetCookie = true
|
|
|
|
err := json.Unmarshal([]byte(s), cf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("parse json error,", err)
|
|
|
|
}
|
|
|
|
if cf.CookieName != "gosessionid" {
|
|
|
|
t.Fatal("parseconfig get cookiename error")
|
|
|
|
}
|
|
|
|
if cf.Gclifetime != 3600 {
|
|
|
|
t.Fatal("parseconfig get gclifetime error")
|
|
|
|
}
|
|
|
|
|
|
|
|
cc := `{"cookieName":"gosessionid","enableSetCookie":false,"gclifetime":3600,"ProviderConfig":"{\"cookieName\":\"gosessionid\",\"securityKey\":\"beegocookiehashkey\"}"}`
|
|
|
|
cf2 := new(managerConfig)
|
|
|
|
cf2.EnableSetCookie = true
|
|
|
|
err = json.Unmarshal([]byte(cc), cf2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("parse json error,", err)
|
|
|
|
}
|
|
|
|
if cf2.CookieName != "gosessionid" {
|
|
|
|
t.Fatal("parseconfig get cookiename error")
|
|
|
|
}
|
|
|
|
if cf2.Gclifetime != 3600 {
|
|
|
|
t.Fatal("parseconfig get gclifetime error")
|
|
|
|
}
|
|
|
|
if cf2.EnableSetCookie != false {
|
|
|
|
t.Fatal("parseconfig get enableSetCookie error")
|
|
|
|
}
|
|
|
|
cconfig := new(cookieConfig)
|
|
|
|
err = json.Unmarshal([]byte(cf2.ProviderConfig), cconfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("parse ProviderConfig err,", err)
|
|
|
|
}
|
|
|
|
if cconfig.CookieName != "gosessionid" {
|
|
|
|
t.Fatal("ProviderConfig get cookieName error")
|
|
|
|
}
|
|
|
|
if cconfig.SecurityKey != "beegocookiehashkey" {
|
|
|
|
t.Fatal("ProviderConfig get securityKey error")
|
|
|
|
}
|
|
|
|
}
|