File: cookie-protector_test.go

package info (click to toggle)
golang-github-bifurcation-mint 0.0~git20200214.93c820e-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 632 kB
  • sloc: makefile: 3
file content (33 lines) | stat: -rw-r--r-- 1,086 bytes parent folder | download
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
package mint

import (
	"bytes"
	"testing"
)

func TestCookieProtector(t *testing.T) {
	cs, err := NewDefaultCookieProtector()
	assertNotError(t, err, "creating the cookie source failed")

	t.Run("handling valid tokens", func(t *testing.T) {
		cookie := []byte("foobar")
		token, err := cs.NewToken(cookie)
		assertNotError(t, err, "creating new token failed")
		decoded, err := cs.DecodeToken(token)
		assertNotError(t, err, "decoding the token failed")
		assertDeepEquals(t, cookie, decoded)
	})

	t.Run("handling invalid tokens", func(t *testing.T) {
		_, err := cs.DecodeToken([]byte("too short"))
		assertError(t, err, "it should reject too short tokens")
		_, err = cs.DecodeToken(append(bytes.Repeat([]byte{0}, cookieNonceSize), []byte("invalid token")...))
		assertError(t, err, "it should reject invalid tokens")
		// create a valid and modify the nonce
		token, err := cs.NewToken([]byte("foobar"))
		assertNotError(t, err, "creating new token failed")
		token[0]++
		_, err = cs.DecodeToken(token)
		assertError(t, err, "it should reject a token with the wrong nonce")
	})
}