sess_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 := make(map[interface{}]interface{})
  72. dst, err = decodeCookie(block, hashKey, securityName, str, 3600)
  73. if err != nil {
  74. t.Fatal("decodeCookie", err)
  75. }
  76. if dst["name"] != "astaxie" {
  77. t.Fatal("dst get map error")
  78. }
  79. if dst["gender"] != "male" {
  80. t.Fatal("dst get map error")
  81. }
  82. }
  83. func TestParseConfig(t *testing.T) {
  84. s := `{"cookieName":"gosessionid","gclifetime":3600}`
  85. cf := new(ManagerConfig)
  86. cf.EnableSetCookie = true
  87. err := json.Unmarshal([]byte(s), cf)
  88. if err != nil {
  89. t.Fatal("parse json error,", err)
  90. }
  91. if cf.CookieName != "gosessionid" {
  92. t.Fatal("parseconfig get cookiename error")
  93. }
  94. if cf.Gclifetime != 3600 {
  95. t.Fatal("parseconfig get gclifetime error")
  96. }
  97. cc := `{"cookieName":"gosessionid","enableSetCookie":false,"gclifetime":3600,"ProviderConfig":"{\"cookieName\":\"gosessionid\",\"securityKey\":\"beegocookiehashkey\"}"}`
  98. cf2 := new(ManagerConfig)
  99. cf2.EnableSetCookie = true
  100. err = json.Unmarshal([]byte(cc), cf2)
  101. if err != nil {
  102. t.Fatal("parse json error,", err)
  103. }
  104. if cf2.CookieName != "gosessionid" {
  105. t.Fatal("parseconfig get cookiename error")
  106. }
  107. if cf2.Gclifetime != 3600 {
  108. t.Fatal("parseconfig get gclifetime error")
  109. }
  110. if cf2.EnableSetCookie != false {
  111. t.Fatal("parseconfig get enableSetCookie error")
  112. }
  113. cconfig := new(cookieConfig)
  114. err = json.Unmarshal([]byte(cf2.ProviderConfig), cconfig)
  115. if err != nil {
  116. t.Fatal("parse ProviderConfig err,", err)
  117. }
  118. if cconfig.CookieName != "gosessionid" {
  119. t.Fatal("ProviderConfig get cookieName error")
  120. }
  121. if cconfig.SecurityKey != "beegocookiehashkey" {
  122. t.Fatal("ProviderConfig get securityKey error")
  123. }
  124. }