| 1 |
package mint |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
func TestCookieProtector(t *testing.T) { |
| 9 |
cs, err := NewDefaultCookieProtector() |
| 10 |
assertNotError(t, err, "creating the cookie source failed") |
| 11 |
|
| 12 |
t.Run("handling valid tokens", func(t *testing.T) { |
| 13 |
cookie := []byte("foobar") |
| 14 |
token, err := cs.NewToken(cookie) |
| 15 |
assertNotError(t, err, "creating new token failed") |
| 16 |
decoded, err := cs.DecodeToken(token) |
| 17 |
assertNotError(t, err, "decoding the token failed") |
| 18 |
assertDeepEquals(t, cookie, decoded) |
| 19 |
}) |
| 20 |
|
| 21 |
t.Run("handling invalid tokens", func(t *testing.T) { |
| 22 |
_, err := cs.DecodeToken([]byte("too short")) |
| 23 |
assertError(t, err, "it should reject too short tokens") |
| 24 |
_, err = cs.DecodeToken(append(bytes.Repeat([]byte{0}, cookieNonceSize), []byte("invalid token")...)) |
| 25 |
assertError(t, err, "it should reject invalid tokens") |
| 26 |
|
| 27 |
token, err := cs.NewToken([]byte("foobar")) |
| 28 |
assertNotError(t, err, "creating new token failed") |
| 29 |
token[0]++ |
| 30 |
_, err = cs.DecodeToken(token) |
| 31 |
assertError(t, err, "it should reject a token with the wrong nonce") |
| 32 |
}) |
| 33 |
} |