2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-03-15 21:26:48 +00:00
|
|
|
package logical
|
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2015-03-15 21:26:48 +00:00
|
|
|
"reflect"
|
2015-10-29 20:59:08 +00:00
|
|
|
"time"
|
2016-08-19 20:45:17 +00:00
|
|
|
|
2019-01-09 00:48:57 +00:00
|
|
|
testing "github.com/mitchellh/go-testing-interface"
|
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/logging"
|
2015-03-15 21:26:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestRequest is a helper to create a purely in-memory Request struct.
|
2021-02-18 20:40:18 +00:00
|
|
|
func TestRequest(t testing.T, op Operation, path string) *Request {
|
2015-03-15 21:26:48 +00:00
|
|
|
return &Request{
|
2019-07-03 00:59:48 +00:00
|
|
|
Operation: op,
|
|
|
|
Path: path,
|
|
|
|
Data: make(map[string]interface{}),
|
|
|
|
Storage: new(InmemStorage),
|
2019-07-03 01:01:34 +00:00
|
|
|
Connection: &Connection{},
|
2015-03-15 21:26:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestStorage is a helper that can be used from unit tests to verify
|
|
|
|
// the behavior of a Storage impl.
|
2018-06-03 22:14:51 +00:00
|
|
|
func TestStorage(t testing.T, s Storage) {
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err := s.List(context.Background(), "")
|
2015-03-15 21:26:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("list error: %s", err)
|
|
|
|
}
|
|
|
|
if len(keys) > 0 {
|
|
|
|
t.Fatalf("should have no keys to start: %#v", keys)
|
|
|
|
}
|
|
|
|
|
|
|
|
entry := &StorageEntry{Key: "foo", Value: []byte("bar")}
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := s.Put(context.Background(), entry); err != nil {
|
2015-03-15 21:26:48 +00:00
|
|
|
t.Fatalf("put error: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
actual, err := s.Get(context.Background(), "foo")
|
2015-03-15 21:26:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("get error: %s", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, entry) {
|
|
|
|
t.Fatalf("wrong value. Expected: %#v\nGot: %#v", entry, actual)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err = s.List(context.Background(), "")
|
2015-03-15 21:26:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("list error: %s", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(keys, []string{"foo"}) {
|
|
|
|
t.Fatalf("bad keys: %#v", keys)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := s.Delete(context.Background(), "foo"); err != nil {
|
2015-03-15 21:26:48 +00:00
|
|
|
t.Fatalf("put error: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err = s.List(context.Background(), "")
|
2015-03-15 21:26:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("list error: %s", err)
|
|
|
|
}
|
|
|
|
if len(keys) > 0 {
|
|
|
|
t.Fatalf("should have no keys to start: %#v", keys)
|
|
|
|
}
|
|
|
|
}
|
2015-10-29 20:59:08 +00:00
|
|
|
|
|
|
|
func TestSystemView() *StaticSystemView {
|
|
|
|
defaultLeaseTTLVal := time.Hour * 24
|
|
|
|
maxLeaseTTLVal := time.Hour * 24 * 2
|
|
|
|
return &StaticSystemView{
|
|
|
|
DefaultLeaseTTLVal: defaultLeaseTTLVal,
|
|
|
|
MaxLeaseTTLVal: maxLeaseTTLVal,
|
2022-12-07 18:29:51 +00:00
|
|
|
VersionString: "testVersionString",
|
2015-10-29 20:59:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackendConfig() *BackendConfig {
|
2016-08-19 20:45:17 +00:00
|
|
|
bc := &BackendConfig{
|
2018-04-03 00:46:59 +00:00
|
|
|
Logger: logging.NewVaultLogger(log.Trace),
|
2015-10-29 20:59:08 +00:00
|
|
|
System: TestSystemView(),
|
2020-09-08 22:53:15 +00:00
|
|
|
Config: make(map[string]string),
|
2015-10-29 20:59:08 +00:00
|
|
|
}
|
2016-08-19 20:45:17 +00:00
|
|
|
|
|
|
|
return bc
|
2015-10-29 20:59:08 +00:00
|
|
|
}
|