Files
mesh-host/cmd/mesh-host/ownname_test.go
T
jschoubben f11be5d529 Answer the node's links in the node-engine seat's converged
The holder of node-packet-filter says when an allowance opens an outward
link and reads no shape of the loaded table to know one, so converged
answers the tunnel's interface and the outward links from the one
classification the filter render uses; links it cannot read refuse it.

Builds: design 33 control-verbs §10, §14; design 08 control-verbs §2; design 47 H H-A-325
2026-11-21 22:32:36 +01:00

179 lines
7.9 KiB
Go

package main
import (
"context"
"errors"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
"git.novox.be/novox/mesh-contracts/go/seats/nodeengine"
condseat "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
"git.novox.be/novox/mesh-networking/modules/mesh-wireguard/go/overlay"
"github.com/novox/mesh-host/internal/link"
"github.com/novox/mesh-host/internal/store"
)
// Design 47 G §527, §634, A-149: the node-engine's own module is the module of its applied record at its own fingerprint,
// and its finding writer is made, observing as that module, once the name is known: at once from the record, else when the
// first composing names it. No name is fixed in code.
func TestTheEnginesOwnNameIsReadFromItsAppliedRecordOrItsFirstComposing(t *testing.T) {
fp := "src2:" + strings.Repeat("a", 64)
applied := appliedStoreFor(filepath.Join(t.TempDir(), "state", "state.json"))
for module, f := range map[string]string{"node": "", "the-engine": fp, "other": "src2:" + strings.Repeat("b", 64)} {
if err := applied.Keep(module, []byte(`{"module":"`+module+`","fp":"`+f+`"}`)); err != nil {
t.Fatal(err)
}
}
for _, tc := range []struct{ fp, want string }{{fp, "the-engine"}, {"", ""}, {"src2:" + strings.Repeat("c", 64), ""}} {
if got := ownModuleApplied(applied, tc.fp); got != tc.want {
t.Errorf("the own module at %q is %q, want %q", tc.fp, got, tc.want)
}
}
own := link.NewOwnName()
made := make(chan string, 1)
late := &lateFindings{own: own, make: func(module string) (*findingsOfTheNode, error) {
made <- module
return nil, context.Canceled // the writer itself is the SDK's: only when it is made is tested here
}}
ctx, stop := context.WithCancel(context.Background())
defer stop()
var said []string
late.Start(ctx, func(s string) { said = append(said, s) })
if err := late.Write(ctx, condseat.Finding{}); err == nil || late.Held() != nil {
t.Fatalf("a finding was taken before the engine's own module was known: %v", err)
}
if len(made) != 0 {
t.Fatal("the writer was made before a name")
}
own.Set("the-engine") // the first composing names it
select {
case m := <-made:
if m != "the-engine" || late.observer() != "the-engine" {
t.Fatalf("the writer was made as %q, observing as %q", m, late.observer())
}
case <-time.After(5 * time.Second):
t.Fatal("the writer was never made after the name was known")
}
// Design 33 control-verbs §10, §14: the seat's verb converged answers the node's convergence state as the record keeps
// it, adopted with its found firewall's kind or converged, since its mode last changed; a record with no mode is refused.
// H-A-325: it answers too the tunnel and the outward links as the engine classifies them for its render; links it cannot
// read refuse it.
t.Run("converged answers the record's convergence state", func(t *testing.T) {
links := func() ([]string, error) { return []string{"eth1"}, nil }
path := filepath.Join(t.TempDir(), "state.json")
found := time.Date(2026, 10, 2, 9, 0, 0, 0, time.UTC)
st := store.State{Firewall: &store.FoundFirewall{Kind: "ufw", FoundAt: found}}
if err := store.Save(path, st); err != nil {
t.Fatal(err)
}
if got, err := convergedOf(path, links); err == nil {
t.Errorf("a record with no mode answers %+v, want refused", got)
}
st.SetMode(store.ModeAdopted, found)
st.SetMode(store.ModeAdopted, found.Add(time.Hour)) // said again: its time is kept
if err := store.Save(path, st); err != nil {
t.Fatal(err)
}
acts := &link.HandActs{Converged: func() (any, error) { return convergedOf(path, links) }}
if got, err := acts.Call(link.EngineCall{Verb: link.ActConverged}); err != nil ||
!reflect.DeepEqual(got, nodeengine.ConvergedAnswer{State: "adopted", Since: "2026-10-02T09:00:00Z", Found: "ufw",
Tunnel: overlay.Interface, Outward: []string{"eth1"}}) {
t.Errorf("an adopted node answers %+v, %v; want adopted since its adoption, found ufw", got, err)
}
st.SetMode(store.ModeConverged, found.Add(48*time.Hour))
if err := store.Save(path, st); err != nil {
t.Fatal(err)
}
if got, err := convergedOf(path, links); err != nil || !reflect.DeepEqual(got, nodeengine.ConvergedAnswer{State: "converged",
Since: "2026-10-04T09:00:00Z", Tunnel: overlay.Interface, Outward: []string{"eth1"}}) {
t.Errorf("a converged node answers %+v, %v; want converged since it converged, no found firewall", got, err)
}
if got, err := convergedOf(path, func() ([]string, error) { return nil, nil }); err != nil || got.Outward == nil {
t.Errorf("a node with no outward link answers %+v, %v; want an empty list, never null", got, err)
}
if got, err := convergedOf(path, func() ([]string, error) { return nil, errors.New("no /proc") }); err == nil {
t.Errorf("links that cannot be read answer %+v, want refused", got)
}
})
}
// Design 47 H, H-A-60: how often an entry whose ask failed is asked again is the file the manifest's `entry-asks` feeds, five when it
// holds nothing usable.
func TestTheEntryAsksAreTheManifestsSetting(t *testing.T) {
was := entryAsksFile
defer func() { entryAsksFile = was }()
for content, want := range map[string]int{"2\n": 2, "": 5, "0": 5, "x": 5} {
entryAsksFile = filepath.Join(t.TempDir(), "w")
if err := os.WriteFile(entryAsksFile, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
if got := entryAsks(); got != want {
t.Errorf("the file %q gives %v, want %v", content, got, want)
}
}
entryAsksFile = filepath.Join(t.TempDir(), "missing")
if got := entryAsks(); got != 5 {
t.Errorf("no file gives %v", got)
}
}
// §633: the engine's bound for the first seat list is the file the manifest's `view-waits-minutes` feeds, five minutes
// when it holds nothing usable.
func TestTheViewWaitsBoundIsTheManifestsSetting(t *testing.T) {
was := viewWaitsFile
defer func() { viewWaitsFile = was }()
for content, want := range map[string]time.Duration{"2\n": 2 * time.Minute, "": 5 * time.Minute, "0": 5 * time.Minute, "x": 5 * time.Minute} {
viewWaitsFile = filepath.Join(t.TempDir(), "w")
if err := os.WriteFile(viewWaitsFile, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
if got := viewWaitsBound(); got != want {
t.Errorf("the file %q gives %v, want %v", content, got, want)
}
}
viewWaitsFile = filepath.Join(t.TempDir(), "missing")
if got := viewWaitsBound(); got != 5*time.Minute {
t.Errorf("no file gives %v", got)
}
// H-A-146, principle 11: the bound after which a failed resolution is asked again is the file `resolve-again-minutes` feeds,
// one minute when it holds nothing usable (1 to 60).
wasAgain := resolveAgainFile
defer func() { resolveAgainFile = wasAgain }()
for content, want := range map[string]time.Duration{"7\n": 7 * time.Minute, "60": time.Hour, "61": time.Minute, "": time.Minute, "0": time.Minute, "x": time.Minute} {
resolveAgainFile = filepath.Join(t.TempDir(), "w")
if err := os.WriteFile(resolveAgainFile, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
if got := resolveAgainBound(); got != want {
t.Errorf("the file %q gives %v, want %v", content, got, want)
}
}
resolveAgainFile = filepath.Join(t.TempDir(), "missing")
if got := resolveAgainBound(); got != time.Minute {
t.Errorf("no file gives %v", got)
}
// Design 08 control-verbs §1, H-A-314 (5): the compose-ask bound is the file `compose-ask-bound` feeds, in seconds, ten when
// it holds nothing usable.
wasAsk := composeAskBoundFile
defer func() { composeAskBoundFile = wasAsk }()
for content, want := range map[string]time.Duration{"3\n": 3 * time.Second, "": 10 * time.Second, "0": 10 * time.Second, "x": 10 * time.Second} {
composeAskBoundFile = filepath.Join(t.TempDir(), "a")
if err := os.WriteFile(composeAskBoundFile, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
if got := composeAskBound(); got != want {
t.Errorf("the file %q gives %v, want %v", content, got, want)
}
}
composeAskBoundFile = filepath.Join(t.TempDir(), "missing")
if got := composeAskBound(); got != 10*time.Second {
t.Errorf("no file gives %v", got)
}
}