2015-03-15 23:39:49 +00:00
|
|
|
package framework
|
2015-03-14 04:15:20 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2015-03-14 06:58:20 +00:00
|
|
|
|
2015-03-15 21:57:19 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
2015-03-14 04:15:20 +00:00
|
|
|
)
|
|
|
|
|
2015-03-14 06:22:48 +00:00
|
|
|
func BenchmarkBackendRoute(b *testing.B) {
|
|
|
|
patterns := []string{
|
|
|
|
"foo",
|
|
|
|
"bar/(?P<name>.+?)",
|
|
|
|
"baz/(?P<name>what)",
|
|
|
|
`aws/policy/(?P<policy>\w)`,
|
|
|
|
`aws/(?P<policy>\w)`,
|
|
|
|
}
|
|
|
|
|
|
|
|
backend := &Backend{Paths: make([]*Path, 0, len(patterns))}
|
|
|
|
for _, p := range patterns {
|
|
|
|
backend.Paths = append(backend.Paths, &Path{Pattern: p})
|
|
|
|
}
|
|
|
|
|
2015-03-14 06:25:17 +00:00
|
|
|
// Warm any caches
|
|
|
|
backend.Route("aws/policy/foo")
|
|
|
|
|
|
|
|
// Reset the timer since we did a lot above
|
2015-03-14 06:22:48 +00:00
|
|
|
b.ResetTimer()
|
2015-03-14 06:25:17 +00:00
|
|
|
|
|
|
|
// Run through and route. We do a sanity check of the return value
|
2015-03-14 06:22:48 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2015-03-14 06:25:17 +00:00
|
|
|
if p := backend.Route("aws/policy/foo"); p == nil {
|
2015-03-14 06:22:48 +00:00
|
|
|
b.Fatal("p should not be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 06:58:20 +00:00
|
|
|
func TestBackend_impl(t *testing.T) {
|
2015-03-15 21:57:19 +00:00
|
|
|
var _ logical.Backend = new(Backend)
|
2015-03-14 06:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackendHandleRequest(t *testing.T) {
|
2015-03-15 21:57:19 +00:00
|
|
|
callback := func(req *logical.Request, data *FieldData) (*logical.Response, error) {
|
|
|
|
return &logical.Response{
|
2015-03-14 06:58:20 +00:00
|
|
|
Data: map[string]interface{}{
|
|
|
|
"value": data.Get("value"),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &Backend{
|
|
|
|
Paths: []*Path{
|
|
|
|
&Path{
|
|
|
|
Pattern: "foo/bar",
|
|
|
|
Fields: map[string]*FieldSchema{
|
|
|
|
"value": &FieldSchema{Type: TypeInt},
|
|
|
|
},
|
2015-03-15 21:57:19 +00:00
|
|
|
Callbacks: map[logical.Operation]OperationFunc{
|
|
|
|
logical.ReadOperation: callback,
|
2015-03-14 07:19:25 +00:00
|
|
|
},
|
2015-03-14 06:58:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-03-15 21:57:19 +00:00
|
|
|
resp, err := b.HandleRequest(&logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
2015-03-14 07:19:25 +00:00
|
|
|
Path: "foo/bar",
|
|
|
|
Data: map[string]interface{}{"value": "42"},
|
2015-03-14 06:58:20 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if resp.Data["value"] != 42 {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackendHandleRequest_404(t *testing.T) {
|
2015-03-15 21:57:19 +00:00
|
|
|
callback := func(req *logical.Request, data *FieldData) (*logical.Response, error) {
|
|
|
|
return &logical.Response{
|
2015-03-14 06:58:20 +00:00
|
|
|
Data: map[string]interface{}{
|
|
|
|
"value": data.Get("value"),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &Backend{
|
|
|
|
Paths: []*Path{
|
|
|
|
&Path{
|
|
|
|
Pattern: `foo/bar`,
|
|
|
|
Fields: map[string]*FieldSchema{
|
|
|
|
"value": &FieldSchema{Type: TypeInt},
|
|
|
|
},
|
2015-03-15 21:57:19 +00:00
|
|
|
Callbacks: map[logical.Operation]OperationFunc{
|
|
|
|
logical.ReadOperation: callback,
|
2015-03-14 07:19:25 +00:00
|
|
|
},
|
2015-03-14 06:58:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-03-15 21:57:19 +00:00
|
|
|
_, err := b.HandleRequest(&logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
2015-03-14 07:19:25 +00:00
|
|
|
Path: "foo/baz",
|
|
|
|
Data: map[string]interface{}{"value": "84"},
|
2015-03-14 06:58:20 +00:00
|
|
|
})
|
2015-03-15 21:57:19 +00:00
|
|
|
if err != logical.ErrUnsupportedPath {
|
2015-03-14 06:58:20 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 17:12:50 +00:00
|
|
|
func TestBackendHandleRequest_help(t *testing.T) {
|
|
|
|
b := &Backend{
|
|
|
|
Paths: []*Path{
|
|
|
|
&Path{
|
|
|
|
Pattern: "foo/bar",
|
|
|
|
Fields: map[string]*FieldSchema{
|
|
|
|
"value": &FieldSchema{Type: TypeInt},
|
|
|
|
},
|
|
|
|
HelpSynopsis: "foo",
|
|
|
|
HelpDescription: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-03-15 21:57:19 +00:00
|
|
|
resp, err := b.HandleRequest(&logical.Request{
|
|
|
|
Operation: logical.HelpOperation,
|
2015-03-14 17:12:50 +00:00
|
|
|
Path: "foo/bar",
|
|
|
|
Data: map[string]interface{}{"value": "42"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if resp.Data["help"] == nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 07:19:25 +00:00
|
|
|
func TestBackendHandleRequest_unsupportedOperation(t *testing.T) {
|
2015-03-15 21:57:19 +00:00
|
|
|
callback := func(req *logical.Request, data *FieldData) (*logical.Response, error) {
|
|
|
|
return &logical.Response{
|
2015-03-14 07:19:25 +00:00
|
|
|
Data: map[string]interface{}{
|
|
|
|
"value": data.Get("value"),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &Backend{
|
|
|
|
Paths: []*Path{
|
|
|
|
&Path{
|
|
|
|
Pattern: `foo/bar`,
|
|
|
|
Fields: map[string]*FieldSchema{
|
|
|
|
"value": &FieldSchema{Type: TypeInt},
|
|
|
|
},
|
2015-03-15 21:57:19 +00:00
|
|
|
Callbacks: map[logical.Operation]OperationFunc{
|
|
|
|
logical.ReadOperation: callback,
|
2015-03-14 07:19:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-03-15 21:57:19 +00:00
|
|
|
_, err := b.HandleRequest(&logical.Request{
|
|
|
|
Operation: logical.WriteOperation,
|
2015-03-14 07:19:25 +00:00
|
|
|
Path: "foo/bar",
|
|
|
|
Data: map[string]interface{}{"value": "84"},
|
|
|
|
})
|
2015-03-15 21:57:19 +00:00
|
|
|
if err != logical.ErrUnsupportedOperation {
|
2015-03-14 07:19:25 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 06:58:20 +00:00
|
|
|
func TestBackendHandleRequest_urlPriority(t *testing.T) {
|
2015-03-15 21:57:19 +00:00
|
|
|
callback := func(req *logical.Request, data *FieldData) (*logical.Response, error) {
|
|
|
|
return &logical.Response{
|
2015-03-14 06:58:20 +00:00
|
|
|
Data: map[string]interface{}{
|
|
|
|
"value": data.Get("value"),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &Backend{
|
|
|
|
Paths: []*Path{
|
|
|
|
&Path{
|
|
|
|
Pattern: `foo/(?P<value>\d+)`,
|
|
|
|
Fields: map[string]*FieldSchema{
|
|
|
|
"value": &FieldSchema{Type: TypeInt},
|
|
|
|
},
|
2015-03-15 21:57:19 +00:00
|
|
|
Callbacks: map[logical.Operation]OperationFunc{
|
|
|
|
logical.ReadOperation: callback,
|
2015-03-14 07:19:25 +00:00
|
|
|
},
|
2015-03-14 06:58:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-03-15 21:57:19 +00:00
|
|
|
resp, err := b.HandleRequest(&logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
2015-03-14 07:19:25 +00:00
|
|
|
Path: "foo/42",
|
|
|
|
Data: map[string]interface{}{"value": "84"},
|
2015-03-14 06:58:20 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if resp.Data["value"] != 42 {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 06:17:25 +00:00
|
|
|
func TestBackendRoute(t *testing.T) {
|
|
|
|
cases := map[string]struct {
|
|
|
|
Patterns []string
|
|
|
|
Path string
|
|
|
|
Match string
|
|
|
|
}{
|
|
|
|
"no match": {
|
|
|
|
[]string{"foo"},
|
|
|
|
"bar",
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
|
|
|
|
"exact": {
|
|
|
|
[]string{"foo"},
|
|
|
|
"foo",
|
|
|
|
"foo",
|
|
|
|
},
|
|
|
|
|
|
|
|
"regexp": {
|
|
|
|
[]string{"fo+"},
|
|
|
|
"foo",
|
|
|
|
"fo+",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for n, tc := range cases {
|
|
|
|
paths := make([]*Path, len(tc.Patterns))
|
|
|
|
for i, pattern := range tc.Patterns {
|
|
|
|
paths[i] = &Path{Pattern: pattern}
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &Backend{Paths: paths}
|
|
|
|
result := b.Route(tc.Path)
|
|
|
|
match := ""
|
|
|
|
if result != nil {
|
|
|
|
match = result.Pattern
|
|
|
|
}
|
|
|
|
|
|
|
|
if match != tc.Match {
|
|
|
|
t.Fatalf("bad: %s\n\nExpected: %s\nGot: %s",
|
|
|
|
n, tc.Match, match)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 04:15:20 +00:00
|
|
|
func TestFieldSchemaDefaultOrZero(t *testing.T) {
|
|
|
|
cases := map[string]struct {
|
|
|
|
Schema *FieldSchema
|
|
|
|
Value interface{}
|
|
|
|
}{
|
|
|
|
"default set": {
|
|
|
|
&FieldSchema{Type: TypeString, Default: "foo"},
|
|
|
|
"foo",
|
|
|
|
},
|
|
|
|
|
|
|
|
"default not set": {
|
|
|
|
&FieldSchema{Type: TypeString},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
|
|
|
actual := tc.Schema.DefaultOrZero()
|
|
|
|
if !reflect.DeepEqual(actual, tc.Value) {
|
|
|
|
t.Fatalf("bad: %s\n\nExpected: %#v\nGot: %#v",
|
|
|
|
name, tc.Value, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|