Adds an internal library for JWS / JWT encoding

This commit is contained in:
2015-08-17 13:50:16 +02:00
parent dc107d934d
commit 4eb3bdaa13
4 changed files with 105 additions and 0 deletions

63
jwt/base64_test.go Normal file
View File

@@ -0,0 +1,63 @@
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)
}
}