82 lines
2.0 KiB
Go
82 lines
2.0 KiB
Go
package store
|
|
|
|
import (
|
|
"errors"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
const identityA = "c046b893-8628-4589-ae50-619d049248a6"
|
|
const identityB = "a50b1569-aa17-4503-bfc1-cf55c10a1c24"
|
|
|
|
func TestDispatcherIdentityPersistsAndCannotChange(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "identity.db")
|
|
first, err := Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := first.BindDispatcherID(identityA); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := first.BindDispatcherID(identityA); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := first.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
second, err := Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer second.Close()
|
|
if err := second.BindDispatcherID(identityB); !errors.Is(err, ErrDispatcherIdentityMismatch) {
|
|
t.Fatalf("different identity accepted: %v", err)
|
|
}
|
|
if err := second.BindDispatcherID(identityA); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var stored string
|
|
if err := second.DB().QueryRow(`SELECT dispatcher_id FROM dispatcher_identity WHERE singleton=1`).Scan(&stored); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if stored != identityA {
|
|
t.Fatalf("identity was changed: %s", stored)
|
|
}
|
|
}
|
|
|
|
func TestDispatcherIdentityInvalidValuesDoNotBind(t *testing.T) {
|
|
s, err := Open(":memory:")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer s.Close()
|
|
for _, id := range []string{"", "dispatcher", "C046B893-8628-4589-AE50-619D049248A6", "00000000-0000-0000-0000-000000000000"} {
|
|
if err := s.BindDispatcherID(id); err == nil {
|
|
t.Fatal("invalid identity accepted")
|
|
}
|
|
}
|
|
var count int
|
|
if err := s.DB().QueryRow(`SELECT COUNT(*) FROM dispatcher_identity`).Scan(&count); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if count != 0 {
|
|
t.Fatal("invalid identity persisted")
|
|
}
|
|
if err := s.BindDispatcherID(identityA); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestDispatcherIdentityDatabaseErrorsAreNotHidden(t *testing.T) {
|
|
s, err := Open(":memory:")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.BindDispatcherID(identityA); err == nil {
|
|
t.Fatal("closed database accepted")
|
|
}
|
|
}
|