Adds unit testing for jws
This commit is contained in:
77
jwt/jws_test.go
Normal file
77
jwt/jws_test.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"crypto/rsa"
|
||||
|
||||
. "gopkg.in/check.v1"
|
||||
)
|
||||
|
||||
type JWSSuite struct {
|
||||
signers []Signer
|
||||
hmacKey []byte
|
||||
rsaKey *rsa.PrivateKey
|
||||
}
|
||||
|
||||
var _ = Suite(&JWSSuite{})
|
||||
|
||||
func (s *JWSSuite) SetUpSuite(c *C) {
|
||||
var err error
|
||||
s.hmacKey = NewHMACKey(24)
|
||||
|
||||
s.rsaKey, err = CachedRSAkey()
|
||||
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
s.signers = []Signer{
|
||||
NewHMAC256Signer(s.hmacKey),
|
||||
NewHMAC384Signer(s.hmacKey),
|
||||
NewHMAC512Signer(s.hmacKey),
|
||||
NewRSA256Signer(s.rsaKey),
|
||||
NewRSA384Signer(s.rsaKey),
|
||||
NewRSA512Signer(s.rsaKey),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *JWSSuite) TestCanEncodeSigned(c *C) {
|
||||
type foo struct {
|
||||
A string
|
||||
B int
|
||||
}
|
||||
|
||||
j := &JOSE{}
|
||||
|
||||
for _, signer := range s.signers {
|
||||
decoded := foo{A: "blah", B: 42}
|
||||
res, err := EncodeJWS(j, decoded, signer)
|
||||
c.Check(string(res), Matches, `\A[0-9A-Za-z_\-]+\.[0-9A-Za-z_\-]+.[0-9A-Za-z_\-]+\z`)
|
||||
if c.Check(err, IsNil) == false {
|
||||
continue
|
||||
}
|
||||
redecoded := foo{}
|
||||
err = DecodeJWS(res, &redecoded, signer)
|
||||
if c.Check(err, IsNil, Commentf("Algo is: %s", signer.Algorithm())) == true {
|
||||
c.Check(redecoded, DeepEquals, decoded)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *JWSSuite) TestCanEncodeUnprotected(c *C) {
|
||||
|
||||
type foo struct {
|
||||
A string
|
||||
B int
|
||||
}
|
||||
|
||||
j := &JOSE{}
|
||||
|
||||
decoded := foo{A: "blah", B: 42}
|
||||
res, err := EncodeJWS(j, decoded, nil)
|
||||
c.Check(string(res), Matches, `\A[0-9A-Za-z_\-]+\.[0-9A-Za-z_\-]+.\z`)
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
redecoded := foo{}
|
||||
err = DecodeJWS(res, &redecoded, nil)
|
||||
c.Assert(err, IsNil)
|
||||
c.Check(redecoded, DeepEquals, decoded)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user