Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
csstaub committed Dec 19, 2014
1 parent 24181ea commit 5e4ca0b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
5 changes: 5 additions & 0 deletions encoding_test.go
Expand Up @@ -70,6 +70,11 @@ func TestBase64URLDecode(t *testing.T) {
if err != nil || !bytes.Equal(val, []byte{0, 1, 2, 3}) {
t.Error("failed to decode [0x00, 0x01, 0x02, 0x03]")
}

val, err = base64URLDecode(nil)
if err == nil {
t.Error("should not decode nil value")
}
}

func TestDeflateRoundtrip(t *testing.T) {
Expand Down
7 changes: 5 additions & 2 deletions jws.go
Expand Up @@ -74,7 +74,7 @@ func (sig signatureInfo) getHeader(name string) (value interface{}, present bool
func (obj JwsObject) computeAuthData(signature *signatureInfo) []byte {
var serializedProtected string

if signature.original != nil {
if signature.original == nil {
raw, err := json.Marshal(signature.protected)
if err != nil {
// Should never happen, since we control the input.
Expand Down Expand Up @@ -129,8 +129,11 @@ func parseSignedFull(input string) (*JwsObject, error) {
return nil, err
}

// Copy value of sig
original := sig

obj.signatures[i].header = sig.Header
obj.signatures[i].original = &sig
obj.signatures[i].original = &original
}

return obj, nil
Expand Down
2 changes: 1 addition & 1 deletion signing.go
Expand Up @@ -131,7 +131,7 @@ func (ctx *genericSigner) Sign(payload []byte) (*JwsObject, error) {
serializedProtected, err := json.Marshal(protected)
if err != nil {
// We have full control over the input, so this should never happen.
panic("Error when serializing message header")
panic("error when serializing message header")
}

input := []byte(fmt.Sprintf("%s.%s",
Expand Down
55 changes: 54 additions & 1 deletion signing_test.go
Expand Up @@ -125,7 +125,7 @@ func TestRoundtripsJWSCorruptSignature(t *testing.T) {
}
}

func TestMultieRecipientJWS(t *testing.T) {
func TestMultiRecipientJWS(t *testing.T) {
signer := NewMultiSigner()

sharedKey := []byte{
Expand Down Expand Up @@ -224,3 +224,56 @@ func TestInvalidSignerAlg(t *testing.T) {
t.Error("should not accept invalid algorithm")
}
}

type allowAllVerifier struct{}

// Dummy verifier that allows everything
func (ctx allowAllVerifier) verifyPayload(payload []byte, signature []byte, alg SignatureAlgorithm) error {
return nil
}

func TestInvalidJWS(t *testing.T) {
signer, err := NewSigner(PS256, rsaTestKey)
if err != nil {
panic(err)
}

obj, err := signer.Sign([]byte("Lorem ipsum dolor sit amet"))
obj.signatures[0].header = map[string]interface{}{
"crit": []string{"TEST"},
}

ver, err := NewVerifier(&rsaTestKey.PublicKey)
if err != nil {
panic(err)
}

verifier := ver.(*genericVerifier)

// Mock out verifier
verifier.verifier = allowAllVerifier{}

_, err = verifier.Verify(obj)
if err == nil {
t.Error("should not verify message with unknown crit header")
}

// Try without alg header
obj.signatures[0].protected = map[string]interface{}{}
obj.signatures[0].header = map[string]interface{}{}

_, err = verifier.Verify(obj)
if err == nil {
t.Error("should not verify message with missing headers")
}

// Set an invalid header
obj.signatures[0].protected = map[string]interface{}{
"alg": []string{"X", "Y", "Z"},
}

_, err = verifier.Verify(obj)
if err == nil {
t.Error("should not verify message with invalid headers")
}
}

0 comments on commit 5e4ca0b

Please sign in to comment.