Archived
57 lines
2.0 KiB
Go
57 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/announce"
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
)
|
|
|
|
// announcedInMemory is the bucket of the states announced, in memory.
|
|
type announcedInMemory map[string][]byte
|
|
|
|
type announcedEntry struct {
|
|
jetstream.KeyValueEntry
|
|
value []byte
|
|
}
|
|
|
|
func (e announcedEntry) Value() []byte { return e.value }
|
|
|
|
func (k announcedInMemory) Get(_ context.Context, key string) (jetstream.KeyValueEntry, error) {
|
|
v, ok := k[key]
|
|
if !ok {
|
|
return nil, jetstream.ErrKeyNotFound
|
|
}
|
|
return announcedEntry{value: v}, nil
|
|
}
|
|
|
|
func (k announcedInMemory) Put(_ context.Context, key string, value []byte) (uint64, error) {
|
|
k[key] = value
|
|
return uint64(time.Now().UnixNano()), nil
|
|
}
|
|
|
|
// D2 (ADR 0495 clause 2; issue 1790): a module's word is kept under its name, changed only when it changes, and a word
|
|
// about another module than its subject's is refused.
|
|
func TestAModulesWordIsKeptUnderItsNameAndOnlyItsOwn(t *testing.T) {
|
|
k := announcedInMemory{}
|
|
word, _ := announce.Encode(announce.States{Module: "keeper", States: []announce.State{{Name: "things", Readable: true}}})
|
|
changed, err := keepAnnouncement(t.Context(), k, announce.Subject("keeper"), word)
|
|
if err != nil || !changed || string(k["keeper"]) != string(word) {
|
|
t.Fatalf("the first word: %v %v %q", changed, err, k["keeper"])
|
|
}
|
|
if changed, err := keepAnnouncement(t.Context(), k, announce.Subject("keeper"), word); err != nil || changed {
|
|
t.Fatalf("the same word again changed the bucket: %v %v", changed, err)
|
|
}
|
|
other, _ := announce.Encode(announce.States{Module: "victim", States: []announce.State{{Name: "secrets", Readable: true}}})
|
|
if _, err := keepAnnouncement(t.Context(), k, announce.Subject("keeper"), other); err == nil ||
|
|
!strings.Contains(err.Error(), "only what it keeps itself") {
|
|
t.Fatalf("a word about another module was kept: %v", err)
|
|
}
|
|
if _, found := k["victim"]; found {
|
|
t.Fatal("a word about another module was kept")
|
|
}
|
|
}
|