mirror of
https://github.com/luxfi/kms.git
synced 2026-10-30 16:23:36 +00:00
Squashes all pre-rewrite history into a single root commit. Previous tree: - 14506 commits including upstream Infisical fork history - Multiple merge chains carrying pre-purge content Force-rewrite per disk-space reclamation discipline. GHCR images (1.9.9, 1.9.11) remain immutable; their git lineage is no longer materialized.
145 lines
2.7 KiB
Go
145 lines
2.7 KiB
Go
package store
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/luxfi/kms/pkg/keys"
|
|
badger "github.com/luxfi/zapdb"
|
|
)
|
|
|
|
func testDB(t *testing.T) *badger.DB {
|
|
t.Helper()
|
|
opts := badger.DefaultOptions("").WithInMemory(true)
|
|
db, err := badger.Open(opts)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { db.Close() })
|
|
return db
|
|
}
|
|
|
|
func TestPutAndGet(t *testing.T) {
|
|
s, err := New(testDB(t))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ks := &keys.ValidatorKeySet{
|
|
ValidatorID: "val-1",
|
|
BLSWalletID: "bls-w-1",
|
|
CoronaWalletID: "rt-w-1",
|
|
BLSPublicKey: "04aabb",
|
|
CoronaPublicKey: "edpub1",
|
|
Threshold: 3,
|
|
Parties: 5,
|
|
Status: "active",
|
|
CreatedAt: time.Now().UTC(),
|
|
UpdatedAt: time.Now().UTC(),
|
|
}
|
|
|
|
if err := s.Put(ks); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
got, err := s.Get("val-1")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.BLSWalletID != "bls-w-1" {
|
|
t.Errorf("expected bls-w-1, got %s", got.BLSWalletID)
|
|
}
|
|
if got.CoronaPublicKey != "edpub1" {
|
|
t.Errorf("expected edpub1, got %s", got.CoronaPublicKey)
|
|
}
|
|
}
|
|
|
|
func TestPutDuplicate(t *testing.T) {
|
|
s, err := New(testDB(t))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ks := &keys.ValidatorKeySet{ValidatorID: "val-1"}
|
|
if err := s.Put(ks); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.Put(ks); err != ErrAlreadyExists {
|
|
t.Errorf("expected ErrAlreadyExists, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestGetNotFound(t *testing.T) {
|
|
s, err := New(testDB(t))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
_, err = s.Get("nonexistent")
|
|
if err != ErrNotFound {
|
|
t.Errorf("expected ErrNotFound, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestList(t *testing.T) {
|
|
s, err := New(testDB(t))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
s.Put(&keys.ValidatorKeySet{ValidatorID: "val-1"})
|
|
s.Put(&keys.ValidatorKeySet{ValidatorID: "val-2"})
|
|
|
|
list := s.List()
|
|
if len(list) != 2 {
|
|
t.Errorf("expected 2, got %d", len(list))
|
|
}
|
|
}
|
|
|
|
func TestUpdate(t *testing.T) {
|
|
s, err := New(testDB(t))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ks := &keys.ValidatorKeySet{ValidatorID: "val-1", Threshold: 3}
|
|
s.Put(ks)
|
|
|
|
ks.Threshold = 4
|
|
if err := s.Update(ks); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
got, _ := s.Get("val-1")
|
|
if got.Threshold != 4 {
|
|
t.Errorf("expected 4, got %d", got.Threshold)
|
|
}
|
|
}
|
|
|
|
func TestUpdateNotFound(t *testing.T) {
|
|
s, err := New(testDB(t))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
err = s.Update(&keys.ValidatorKeySet{ValidatorID: "nonexistent"})
|
|
if err != ErrNotFound {
|
|
t.Errorf("expected ErrNotFound, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestDelete(t *testing.T) {
|
|
s, err := New(testDB(t))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
s.Put(&keys.ValidatorKeySet{ValidatorID: "val-1"})
|
|
if err := s.Delete("val-1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.Get("val-1"); err != ErrNotFound {
|
|
t.Errorf("expected ErrNotFound after delete, got %v", err)
|
|
}
|
|
}
|