vault: Validate lease values

This commit is contained in:
Armon Dadgar
2015-03-13 10:41:14 -07:00
parent e0d1c1d0d2
commit f34ade56ad
2 changed files with 51 additions and 0 deletions

View File

@@ -112,6 +112,23 @@ type Lease struct {
MaxIncrement time.Duration // Maximum increment to lease duration
}
// Validate is used to sanity check a lease
func (l *Lease) Validate() error {
if l.Duration <= 0 {
return fmt.Errorf("lease duration must be greater than zero")
}
if l.MaxDuration <= 0 {
return fmt.Errorf("maximum lease duration must be greater than zero")
}
if l.Duration > l.MaxDuration {
return fmt.Errorf("lease duration cannot be greater than maximum lease duration")
}
if l.MaxIncrement < 0 {
return fmt.Errorf("maximum lease increment cannot be negative")
}
return nil
}
// Factory is the factory function to create a logical backend.
type Factory func(map[string]string) (LogicalBackend, error)

View File

@@ -0,0 +1,34 @@
package vault
import (
"testing"
"time"
)
func TestLease_Validate(t *testing.T) {
l := &Lease{}
if err := l.Validate(); err.Error() != "lease duration must be greater than zero" {
t.Fatalf("err: %v", err)
}
l.Duration = time.Minute
if err := l.Validate(); err.Error() != "maximum lease duration must be greater than zero" {
t.Fatalf("err: %v", err)
}
l.MaxDuration = time.Second
if err := l.Validate(); err.Error() != "lease duration cannot be greater than maximum lease duration" {
t.Fatalf("err: %v", err)
}
l.MaxDuration = time.Minute
l.MaxIncrement = -1 * time.Second
if err := l.Validate(); err.Error() != "maximum lease increment cannot be negative" {
t.Fatalf("err: %v", err)
}
l.MaxIncrement = time.Second
if err := l.Validate(); err != nil {
t.Fatalf("err: %v", err)
}
}