forked from bpowers/seshcookie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seshcookie_test.go
65 lines (53 loc) · 1.38 KB
/
seshcookie_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2011 Bobby Powers. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package seshcookie
import (
"bytes"
"crypto/sha1"
"testing"
"time"
)
func createKey() (encKey, hmacKey []byte) {
encSha1 := sha1.New()
encSha1.Write([]byte(time.Now().UTC().String()))
encSha1.Write([]byte("-enc"))
encKey = encSha1.Sum(nil)[:blockSize]
hmacSha1 := sha1.New()
hmacSha1.Write([]byte(time.Now().UTC().String()))
hmacSha1.Write([]byte("-hmac"))
hmacKey = hmacSha1.Sum(nil)[:blockSize]
return
}
func TestRoundtrip(t *testing.T) {
encKey, hmacKey := createKey()
orig := map[string]interface{}{"a": 1, "b": "c", "d": 1.2}
encoded, encodedHash, err := encodeCookie(orig, encKey, hmacKey)
if err != nil {
t.Errorf("encodeCookie: %s", err)
return
}
decoded, decodedHash, err := decodeCookie(encoded, encKey, hmacKey)
if err != nil {
t.Errorf("decodeCookie: %s", err)
return
}
if decoded == nil {
t.Errorf("decoded map is null")
return
}
if len(decoded) != 3 {
t.Errorf("len was %d, expected 3", len(decoded))
return
}
if !bytes.Equal(encodedHash, decodedHash) {
t.Errorf("encoded & decoded gob hash mismatches: %s, %s",
string(encodedHash), string(decodedHash))
}
for k, v := range orig {
if decoded[k] != v {
t.Errorf("expected decoded[%s] (%#v) == %#v", k,
decoded[k], v)
}
}
}