Skip to content

Commit

Permalink
Coveralls (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
aricart committed Aug 31, 2018
1 parent bea96bd commit 54858e2
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 7 deletions.
12 changes: 5 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ go:
install:
- go get -t ./...
- go get github.com/mattn/goveralls
- go get github.com/wadey/gocovmerge

script:
- go fmt ./...
before_script:
- $(exit $(go fmt ./... | wc -l))
- go vet ./...
- go test -v
- go test -v --race
- go test -v -covermode=count -coverprofile=coverage.out

after_success:
- if [[ "$TRAVIS_GO_VERSION" == 1.10.* ]] ; then $HOME/gopath/bin/goveralls -coverprofile coverage.out -service travis-ci; fi
script:
- if [[ "$TRAVIS_GO_VERSION" == 1.10.* ]] ; then ./scripts/cov.sh TRAVIS; else go test -v -race ./...; fi
8 changes: 8 additions & 0 deletions claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ func encode(v interface{}) (string, error) {
}

func (c *Claims) doEncode(header *Header, kp nkeys.KeyPair) (string, error) {
if header == nil {
return "", errors.New("header is required")
}

if kp == nil {
return "", errors.New("keypair is required")
}

h, err := encode(header)
if err != nil {
return "", err
Expand Down
102 changes: 102 additions & 0 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"testing"
"time"

"encoding/base64"
"encoding/json"
"github.com/nats-io/nkeys"
)

Expand Down Expand Up @@ -326,3 +328,103 @@ func TestSample(t *testing.T) {
t.Fatalf("the public key is not trusted")
}
}

func TestBadHeaderEncoding(t *testing.T) {
// the '=' will be illegal
_, err := parseHeaders("=hello=")
if err == nil {
t.Fatal("should have failed it is not encoded")
}
}

func TestBadClaimsEncoding(t *testing.T) {
// the '=' will be illegal
_, err := parseClaims("=hello=")
if err == nil {
t.Fatal("should have failed it is not encoded")
}
}

func TestBadHeaderJSON(t *testing.T) {
payload := base64.RawStdEncoding.EncodeToString([]byte("{foo: bar}"))
_, err := parseHeaders(payload)
if err == nil {
t.Fatal("should have failed bad json")
}
}

func TestBadClaimsJSON(t *testing.T) {
payload := base64.RawStdEncoding.EncodeToString([]byte("{foo: bar}"))
_, err := parseClaims(payload)
if err == nil {
t.Fatal("should have failed bad json")
}
}

func TestBadPublicKeyDecode(t *testing.T) {
c := &Claims{}
c.Issuer = "foo"
if ok := c.Verify("foo", []byte("bar")); ok {
t.Fatal("Should have failed to verify")
}
}

func TestBadSig(t *testing.T) {

// Need a private key to sign the claim
kp, err := nkeys.CreateAccount(nil)
if err != nil {
t.Fatal("unable to create account key", err)
}

claims := NewClaims()
// add a bunch of claims
claims.Nats["foo"] = "bar"

// serialize the claim to a JWT token
token, err := claims.Encode(kp)
if err != nil {
t.Fatal("error encoding token", err)
}

tokens := strings.Split(token, ".")
badToken := fmt.Sprintf("%s.%s.=hello=", tokens[0], tokens[1])
_, err = Decode(badToken)
if err == nil {
t.Fatal("should have failed to base64 decode signature")
}
}

func TestClaimsStringIsJSON(t *testing.T) {
claims := NewClaims()
// add a bunch of claims
claims.Nats["foo"] = "bar"

claims2 := NewClaims()
json.Unmarshal([]byte(claims.String()), claims2)
if claims2.Nats["foo"] != "bar" {
t.Fatal("Failed to decode expected claim from String representation")
}
}

func TestDoEncodeNilHeader(t *testing.T) {
claims := NewClaims()
_, err := claims.doEncode(nil, nil)
if err == nil {
t.Fatal("should have failed to encode")
}
if err.Error() != "header is required" {
t.Fatalf("unexpected error on encode: %v", err)
}
}

func TestDoEncodeNilKeyPair(t *testing.T) {
claims := NewClaims()
_, err := claims.doEncode(&Header{}, nil)
if err == nil {
t.Fatal("should have failed to encode")
}
if err.Error() != "keypair is required" {
t.Fatalf("unexpected error on encode: %v", err)
}
}
16 changes: 16 additions & 0 deletions scripts/cov.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash -e
# Run from directory above via ./scripts/cov.sh

rm -rf ./cov
mkdir cov
go test -v -race -covermode=atomic -coverprofile=./cov/coverage.out -coverpkg=github.com/nats-io/jwt .
gocovmerge ./cov/*.out > coverage.out
rm -rf ./cov

# If we have an arg, assume travis run and push to coveralls. Otherwise launch browser results
if [[ -n $1 ]]; then
$HOME/gopath/bin/goveralls -coverprofile=coverage.out -service travis-ci
rm -rf ./coverage.out
else
go tool cover -html=coverage.out
fi

0 comments on commit 54858e2

Please sign in to comment.