sess_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. package session
  15. import (
  16. "crypto/aes"
  17. "encoding/json"
  18. "testing"
  19. )
  20. func Test_gob(t *testing.T) {
  21. a := make(map[interface{}]interface{})
  22. a["username"] = "astaxie"
  23. a[12] = 234
  24. a["user"] = User{"asta", "xie"}
  25. b, err := EncodeGob(a)
  26. if err != nil {
  27. t.Error(err)
  28. }
  29. c, err := DecodeGob(b)
  30. if err != nil {
  31. t.Error(err)
  32. }
  33. if len(c) == 0 {
  34. t.Error("decodeGob empty")
  35. }
  36. if c["username"] != "astaxie" {
  37. t.Error("decode string error")
  38. }
  39. if c[12] != 234 {
  40. t.Error("decode int error")
  41. }
  42. if c["user"].(User).Username != "asta" {
  43. t.Error("decode struct error")
  44. }
  45. }
  46. type User struct {
  47. Username string
  48. NickName string
  49. }
  50. func TestGenerate(t *testing.T) {
  51. str := generateRandomKey(20)
  52. if len(str) != 20 {
  53. t.Fatal("generate length is not equal to 20")
  54. }
  55. }
  56. func TestCookieEncodeDecode(t *testing.T) {
  57. hashKey := "testhashKey"
  58. blockkey := generateRandomKey(16)
  59. block, err := aes.NewCipher(blockkey)
  60. if err != nil {
  61. t.Fatal("NewCipher:", err)
  62. }
  63. securityName := string(generateRandomKey(20))
  64. val := make(map[interface{}]interface{})
  65. val["name"] = "astaxie"
  66. val["gender"] = "male"
  67. str, err := encodeCookie(block, hashKey, securityName, val)
  68. if err != nil {
  69. t.Fatal("encodeCookie:", err)
  70. }
  71. dst, err := decodeCookie(block, hashKey, securityName, str, 3600)
  72. if err != nil {
  73. t.Fatal("decodeCookie", err)
  74. }
  75. if dst["name"] != "astaxie" {
  76. t.Fatal("dst get map error")
  77. }
  78. if dst["gender"] != "male" {
  79. t.Fatal("dst get map error")
  80. }
  81. }
  82. func TestParseConfig(t *testing.T) {
  83. s := `{"cookieName":"gosessionid","gclifetime":3600}`
  84. cf := new(ManagerConfig)
  85. cf.EnableSetCookie = true
  86. err := json.Unmarshal([]byte(s), cf)
  87. if err != nil {
  88. t.Fatal("parse json error,", err)
  89. }
  90. if cf.CookieName != "gosessionid" {
  91. t.Fatal("parseconfig get cookiename error")
  92. }
  93. if cf.Gclifetime != 3600 {
  94. t.Fatal("parseconfig get gclifetime error")
  95. }
  96. cc := `{"cookieName":"gosessionid","enableSetCookie":false,"gclifetime":3600,"ProviderConfig":"{\"cookieName\":\"gosessionid\",\"securityKey\":\"beegocookiehashkey\"}"}`
  97. cf2 := new(ManagerConfig)
  98. cf2.EnableSetCookie = true
  99. err = json.Unmarshal([]byte(cc), cf2)
  100. if err != nil {
  101. t.Fatal("parse json error,", err)
  102. }
  103. if cf2.CookieName != "gosessionid" {
  104. t.Fatal("parseconfig get cookiename error")
  105. }
  106. if cf2.Gclifetime != 3600 {
  107. t.Fatal("parseconfig get gclifetime error")
  108. }
  109. if cf2.EnableSetCookie {
  110. t.Fatal("parseconfig get enableSetCookie error")
  111. }
  112. cconfig := new(cookieConfig)
  113. err = json.Unmarshal([]byte(cf2.ProviderConfig), cconfig)
  114. if err != nil {
  115. t.Fatal("parse ProviderConfig err,", err)
  116. }
  117. if cconfig.CookieName != "gosessionid" {
  118. t.Fatal("ProviderConfig get cookieName error")
  119. }
  120. if cconfig.SecurityKey != "beegocookiehashkey" {
  121. t.Fatal("ProviderConfig get securityKey error")
  122. }
  123. }