Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Macaroon-specific tests #344

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions macaroons/constraints_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package macaroons

import (
"errors"
"fmt"
"testing"
"time"

"gopkg.in/macaroon-bakery.v1/bakery"
"gopkg.in/macaroon-bakery.v1/bakery/checkers"
macaroon "gopkg.in/macaroon.v1"
)

type macError struct {
message string
}

func (err macError) Error() string {
return err.message
}

func testConstraint(constraint Constraint, ok checkers.Checker,
failFn func() checkers.Checker) error {
macParams := bakery.NewServiceParams{}
svc, err := bakery.NewService(macParams)
if err != nil {
return errors.New("Failed to create a new service")
}
mac, err := svc.NewMacaroon("", nil, nil)
if err != nil {
return errors.New("Failed to create a new macaroon")
}

mac, err = AddConstraints(mac, constraint)
if err != nil {
return errors.New("Failed to add macaroon constraint")
}

okChecker := checkers.New(ok)
if err := svc.Check(macaroon.Slice{mac}, okChecker); err != nil {
msg := "Correct checker failed: %v"
return macError{fmt.Sprintf(msg, ok)}
}

fail := failFn()
failChecker := checkers.New(fail)
if err := svc.Check(macaroon.Slice{mac}, failChecker); err == nil {
msg := "Incorrect checker succeeded: %v"
return macError{fmt.Sprintf(msg, fail)}
}
return nil
}

func TestAllowConstraint(t *testing.T) {
if err := testConstraint(
AllowConstraint("op1", "op2", "op4"),
AllowChecker("op1"),
func() checkers.Checker {
return AllowChecker("op3")
},
); err != nil {
t.Fatalf(err.Error())
}
}

func TestTimeoutConstraint(t *testing.T) {
if err := testConstraint(
TimeoutConstraint(1),
TimeoutChecker(),
func() checkers.Checker {
time.Sleep(time.Second)
return TimeoutChecker()
},
); err != nil {
t.Fatalf(err.Error())
}
}

func TestIPLockConstraint(t *testing.T) {
if err := testConstraint(
IPLockConstraint("127.0.0.1"),
IPLockChecker("127.0.0.1"),
func() checkers.Checker {
return IPLockChecker("0.0.0.0")
},
); err != nil {
t.Fatalf(err.Error())
}
}

func TestIPLockEmptyIP(t *testing.T) {
if err := testConstraint(
IPLockConstraint(""),
IPLockChecker("127.0.0.1"),
func() checkers.Checker {
return IPLockChecker("0.0.0.0")
},
); err != nil {
if _, ok := err.(macError); !ok {
t.Fatalf("IPLock with an empty IP should always pass")
}
}
}

func TestIPLockBadIP(t *testing.T) {
if err := IPLockConstraint("127.0.0/800"); err == nil {
t.Fatalf("IPLockConstraint with bad IP should fail")
}
}