package jwt import ( "encoding/base64" "testing" . "gopkg.in/check.v1" ) func Test(t *testing.T) { TestingT(t) } type Base64Suite struct{} var _ = Suite(&Base64Suite{}) type dataAndEncode struct { data []byte encoded []byte } func (s *Base64Suite) TestEncodeWithNoTrailing(c *C) { data := []dataAndEncode{ {[]byte{0, 0, 0}, []byte("AAAA")}, //With trailing Should be AAAA {[]byte{0, 0}, []byte("AAA")}, //With trailing Should be AAA= {[]byte{0}, []byte("AA")}, //With trailing Should be AA== } for _, d := range data { res := make([]byte, Base64EncodedBufferLen(len(d.data))) base64.URLEncoding.Encode(res, d.data) c.Check(res[:Base64EncodedStrippedLen(len(d.data))], DeepEquals, 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 := make([]byte, Base64DecodedLenFromStripped(len(encoded))) err := Base64Decode(res, []byte(encoded)) if c.Check(err, IsNil) == false { continue } c.Check(res[:Base64DecodedStrippedLen(len(encoded))], 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 := make([]byte, Base64DecodedLenFromStripped(len(encoded))) err := Base64Decode(res, []byte(encoded)) c.Check(err, ErrorMatches, errorMatches) } }