Files
narco/jwt/base64_test.go

64 lines
1.3 KiB
Go

package jwt
import (
"testing"
. "gopkg.in/check.v1"
)
func Test(t *testing.T) {
TestingT(t)
}
type Base64Suite struct{}
var _ = Suite(&Base64Suite{})
type dataAndEncode struct {
data []byte
encoded string
}
func (s *Base64Suite) TestEncodeWithNoTrailing(c *C) {
data := []dataAndEncode{
{[]byte{0, 0, 0}, "AAAA"}, //With trailing Should be AAAA
{[]byte{0, 0}, "AAA"}, //With trailing Should be AAA=
{[]byte{0}, "AA"}, //With trailing Should be AA==
}
for _, d := range data {
res := Base64Encode(d.data)
c.Check(res, Equals, d.encoded)
}
}
func (s *Base64Suite) TestDecodeWithNoTrailing(c *C) {
data := map[string][]byte{
"AAAA": []byte{0, 0, 0},
"AAA": []byte{0, 0},
"AA": []byte{0},
}
for encoded, expected := range data {
res, err := Base64Decode(encoded)
if c.Check(err, IsNil) == false {
continue
}
c.Check(res, DeepEquals, expected)
}
}
func (s *Base64Suite) TestDetectBadFormat(c *C) {
data := map[string]string{
"A": `jwt: Invalid base64 string \(length:[0-9]+ % 4 == 1\): 'A'`,
"ABCD%===": `jwt: illegal base64 data at input byte [0-9]+`,
}
for encoded, errorMatches := range data {
res, err := Base64Decode(encoded)
c.Check(err, ErrorMatches, errorMatches)
c.Check(res, IsNil)
}
}