github.com/astaxie/beego@v1.12.3/session/sess_test.go (about)

     1  // Copyright 2014 beego Author. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package session
    16  
    17  import (
    18  	"crypto/aes"
    19  	"encoding/json"
    20  	"testing"
    21  )
    22  
    23  func Test_gob(t *testing.T) {
    24  	a := make(map[interface{}]interface{})
    25  	a["username"] = "astaxie"
    26  	a[12] = 234
    27  	a["user"] = User{"asta", "xie"}
    28  	b, err := EncodeGob(a)
    29  	if err != nil {
    30  		t.Error(err)
    31  	}
    32  	c, err := DecodeGob(b)
    33  	if err != nil {
    34  		t.Error(err)
    35  	}
    36  	if len(c) == 0 {
    37  		t.Error("decodeGob empty")
    38  	}
    39  	if c["username"] != "astaxie" {
    40  		t.Error("decode string error")
    41  	}
    42  	if c[12] != 234 {
    43  		t.Error("decode int error")
    44  	}
    45  	if c["user"].(User).Username != "asta" {
    46  		t.Error("decode struct error")
    47  	}
    48  }
    49  
    50  type User struct {
    51  	Username string
    52  	NickName string
    53  }
    54  
    55  func TestGenerate(t *testing.T) {
    56  	str := generateRandomKey(20)
    57  	if len(str) != 20 {
    58  		t.Fatal("generate length is not equal to 20")
    59  	}
    60  }
    61  
    62  func TestCookieEncodeDecode(t *testing.T) {
    63  	hashKey := "testhashKey"
    64  	blockkey := generateRandomKey(16)
    65  	block, err := aes.NewCipher(blockkey)
    66  	if err != nil {
    67  		t.Fatal("NewCipher:", err)
    68  	}
    69  	securityName := string(generateRandomKey(20))
    70  	val := make(map[interface{}]interface{})
    71  	val["name"] = "astaxie"
    72  	val["gender"] = "male"
    73  	str, err := encodeCookie(block, hashKey, securityName, val)
    74  	if err != nil {
    75  		t.Fatal("encodeCookie:", err)
    76  	}
    77  	dst, err := decodeCookie(block, hashKey, securityName, str, 3600)
    78  	if err != nil {
    79  		t.Fatal("decodeCookie", err)
    80  	}
    81  	if dst["name"] != "astaxie" {
    82  		t.Fatal("dst get map error")
    83  	}
    84  	if dst["gender"] != "male" {
    85  		t.Fatal("dst get map error")
    86  	}
    87  }
    88  
    89  func TestParseConfig(t *testing.T) {
    90  	s := `{"cookieName":"gosessionid","gclifetime":3600}`
    91  	cf := new(ManagerConfig)
    92  	cf.EnableSetCookie = true
    93  	err := json.Unmarshal([]byte(s), cf)
    94  	if err != nil {
    95  		t.Fatal("parse json error,", err)
    96  	}
    97  	if cf.CookieName != "gosessionid" {
    98  		t.Fatal("parseconfig get cookiename error")
    99  	}
   100  	if cf.Gclifetime != 3600 {
   101  		t.Fatal("parseconfig get gclifetime error")
   102  	}
   103  
   104  	cc := `{"cookieName":"gosessionid","enableSetCookie":false,"gclifetime":3600,"ProviderConfig":"{\"cookieName\":\"gosessionid\",\"securityKey\":\"beegocookiehashkey\"}"}`
   105  	cf2 := new(ManagerConfig)
   106  	cf2.EnableSetCookie = true
   107  	err = json.Unmarshal([]byte(cc), cf2)
   108  	if err != nil {
   109  		t.Fatal("parse json error,", err)
   110  	}
   111  	if cf2.CookieName != "gosessionid" {
   112  		t.Fatal("parseconfig get cookiename error")
   113  	}
   114  	if cf2.Gclifetime != 3600 {
   115  		t.Fatal("parseconfig get gclifetime error")
   116  	}
   117  	if cf2.EnableSetCookie {
   118  		t.Fatal("parseconfig get enableSetCookie error")
   119  	}
   120  	cconfig := new(cookieConfig)
   121  	err = json.Unmarshal([]byte(cf2.ProviderConfig), cconfig)
   122  	if err != nil {
   123  		t.Fatal("parse ProviderConfig err,", err)
   124  	}
   125  	if cconfig.CookieName != "gosessionid" {
   126  		t.Fatal("ProviderConfig get cookieName error")
   127  	}
   128  	if cconfig.SecurityKey != "beegocookiehashkey" {
   129  		t.Fatal("ProviderConfig get securityKey error")
   130  	}
   131  }