2015-06-30 21:04:18 +00:00
|
|
|
package salt
|
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2015-06-30 21:04:18 +00:00
|
|
|
"crypto/sha1"
|
|
|
|
"crypto/sha256"
|
|
|
|
"testing"
|
|
|
|
|
2015-12-16 17:56:20 +00:00
|
|
|
"github.com/hashicorp/go-uuid"
|
2015-06-30 21:04:18 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSalt(t *testing.T) {
|
|
|
|
inm := &logical.InmemStorage{}
|
|
|
|
conf := &Config{}
|
|
|
|
|
|
|
|
salt, err := NewSalt(inm, conf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-06-30 23:47:49 +00:00
|
|
|
if !salt.DidGenerate() {
|
|
|
|
t.Fatalf("expected generation")
|
|
|
|
}
|
|
|
|
|
2015-06-30 21:04:18 +00:00
|
|
|
// Verify the salt exists
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err := inm.Get(context.Background(), DefaultLocation)
|
2015-06-30 21:04:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("missing salt")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new salt, should restore
|
|
|
|
salt2, err := NewSalt(inm, conf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-06-30 23:47:49 +00:00
|
|
|
if salt2.DidGenerate() {
|
|
|
|
t.Fatalf("unexpected generation")
|
|
|
|
}
|
|
|
|
|
2015-06-30 21:04:18 +00:00
|
|
|
// Check for a match
|
|
|
|
if salt.salt != salt2.salt {
|
|
|
|
t.Fatalf("salt mismatch: %s %s", salt.salt, salt2.salt)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify a match
|
|
|
|
id := "foobarbaz"
|
|
|
|
sid1 := salt.SaltID(id)
|
|
|
|
sid2 := salt2.SaltID(id)
|
|
|
|
|
|
|
|
if sid1 != sid2 {
|
|
|
|
t.Fatalf("mismatch")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaltID(t *testing.T) {
|
2016-01-13 18:40:08 +00:00
|
|
|
salt, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-30 21:04:18 +00:00
|
|
|
id := "foobarbaz"
|
|
|
|
|
|
|
|
sid1 := SaltID(salt, id, SHA1Hash)
|
|
|
|
sid2 := SaltID(salt, id, SHA1Hash)
|
|
|
|
|
|
|
|
if len(sid1) != sha1.Size*2 {
|
|
|
|
t.Fatalf("Bad len: %d %s", len(sid1), sid1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if sid1 != sid2 {
|
|
|
|
t.Fatalf("mismatch")
|
|
|
|
}
|
|
|
|
|
|
|
|
sid1 = SaltID(salt, id, SHA256Hash)
|
|
|
|
sid2 = SaltID(salt, id, SHA256Hash)
|
|
|
|
|
|
|
|
if len(sid1) != sha256.Size*2 {
|
|
|
|
t.Fatalf("Bad len: %d", len(sid1))
|
|
|
|
}
|
|
|
|
|
|
|
|
if sid1 != sid2 {
|
|
|
|
t.Fatalf("mismatch")
|
|
|
|
}
|
|
|
|
}
|