blob: de56a92d637cb886edc2bca5ee53ec055668f767 [file] [log] [blame]
package flag
import (
"flag"
"os"
"reflect"
"testing"
tsecurity "veyron.io/veyron/veyron/lib/testutil/security"
vsecurity "veyron.io/veyron/veyron/security"
"veyron.io/veyron/veyron2/security"
)
func TestNewAuthorizerOrDie(t *testing.T) {
type flagValue map[string]string
testNewAuthorizerOrDie := func(flags flagValue, wantAuth security.Authorizer, wantPanic bool) {
defer func() {
if gotPanic := (recover() != nil); wantPanic != gotPanic {
t.Errorf("AuthorizerFromFlags() with flags %v, got panic: %v, want panic: %v ", flags, gotPanic, wantPanic)
}
}()
if got := NewAuthorizerOrDie(); !reflect.DeepEqual(got, wantAuth) {
t.Errorf("AuthorizerFromFlags() with flags %v: got Authorizer: %v, want: %v", flags, got, wantAuth)
}
}
clearACLFlags := func() {
flag.Set("acl", "")
flag.Set("acl_file", "")
}
var (
acl1 = security.ACL{}
acl2 = security.ACL{In: map[security.BlessingPattern]security.LabelSet{
"veyron/alice": security.LabelSet(security.ReadLabel | security.WriteLabel),
"veyron/bob": security.LabelSet(security.ReadLabel),
}}
)
acl2File := tsecurity.SaveACLToFile(acl2)
defer os.Remove(acl2File)
testdata := []struct {
flags flagValue
wantAuth security.Authorizer
wantPanic bool
}{
{
flags: flagValue{},
wantAuth: nil,
},
{
flags: flagValue{"acl": "{}"},
wantAuth: vsecurity.NewACLAuthorizer(acl1),
},
{
flags: flagValue{"acl": `{"In":{"veyron/alice":"RW", "veyron/bob": "R"}}`},
wantAuth: vsecurity.NewACLAuthorizer(acl2),
},
{
flags: flagValue{"acl": `{"In":{"veyron/bob":"R", "veyron/alice": "WR"}}`},
wantAuth: vsecurity.NewACLAuthorizer(acl2),
},
{
flags: flagValue{"acl_file": acl2File},
wantAuth: vsecurity.NewFileACLAuthorizer(acl2File),
},
{
flags: flagValue{"acl_file": acl2File, "acl": `{"In":{"veyron/alice":"RW", "veyron/bob": "R"}}`},
wantPanic: true,
},
}
for _, d := range testdata {
clearACLFlags()
for f, v := range d.flags {
flag.Set(f, v)
}
testNewAuthorizerOrDie(d.flags, d.wantAuth, d.wantPanic)
}
}