R2's acceptance criteria name this case directly: the file's list changes while the default-route link stays the same, and the guard gives the new names to that same link without moving them first. Builds: design 08 sign-in-page §7, §9
527 lines
21 KiB
Go
527 lines
21 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
const meshFile = "# Managed by the mesh\nnameserver 10.42.0.2\noptions timeout:3 attempts:2 edns0\n"
|
|
|
|
// vpnFile is a VPN client's file as one writes it; the addresses and domains are documentation's.
|
|
const vpnFile = "# Dynamic resolv.conf(5) file for glibc resolver(3) generated by forticlient\n" +
|
|
"nameserver 192.0.2.53\nnameserver 192.0.2.54\nsearch corp.example cloud.example\n"
|
|
|
|
// aGuardedMachine is a guard over a temporary /etc and /run, with a clock the test moves.
|
|
func aGuardedMachine(t *testing.T) (*Guard, *time.Time, *fakeResolved) {
|
|
t.Helper()
|
|
g, now, f, _ := aGuardedMachineWithLinks(t, "tun0")
|
|
return g, now, f
|
|
}
|
|
|
|
// aGuardedMachineWithLinks is the same, over the links named, and answers the resolver too.
|
|
func aGuardedMachineWithLinks(t *testing.T, links ...string) (*Guard, *time.Time, *fakeResolved, *Resolver) {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
for _, d := range []string{"etc/node-resolver", "run"} {
|
|
if err := os.MkdirAll(filepath.Join(dir, d), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
write(t, filepath.Join(dir, "etc/node-resolver/resolv.conf"), meshFile)
|
|
write(t, filepath.Join(dir, "etc/resolv.conf"), meshFile)
|
|
now := time.Date(2026, 10, 7, 12, 0, 0, 0, time.UTC)
|
|
f := &fakeResolved{}
|
|
r := aMachine(t, f, links...)
|
|
r.FallbackFile = filepath.Join(dir, "run", "fallback.json")
|
|
g := &Guard{Path: filepath.Join(dir, "etc/resolv.conf"), KeptPath: filepath.Join(dir, "etc/node-resolver/resolv.conf"),
|
|
Dir: filepath.Join(dir, "run"), Hold: Hold, Now: func() time.Time { return now },
|
|
Resolver: r, Log: t.Logf, wake: make(chan struct{}, 1)}
|
|
return g, &now, f, r
|
|
}
|
|
|
|
func write(t *testing.T, path, content string) {
|
|
t.Helper()
|
|
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func contentOf(t *testing.T, path string) string {
|
|
t.Helper()
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return string(raw)
|
|
}
|
|
|
|
// The module's own file is left alone, and nothing is said.
|
|
func TestTheModulesOwnFileIsLeftAlone(t *testing.T) {
|
|
g, _, _ := aGuardedMachine(t)
|
|
if did := g.Tick(); did != "" || g.Displaced() != nil {
|
|
t.Errorf("the module's own file was taken for an outside write: %q", did)
|
|
}
|
|
}
|
|
|
|
// A write a module takes: kept for it to read, with its writer named from its header; once taken, the
|
|
// module's own file is back at the next look; and the history says when, who and what became of it —
|
|
// never a server or a domain.
|
|
func TestATakenWriteIsPutBackAtOnce(t *testing.T) {
|
|
g, now, _ := aGuardedMachine(t)
|
|
write(t, g.Path, vpnFile)
|
|
if did := g.Tick(); did != "holding" {
|
|
t.Fatalf("the write was %q, not held for a taker", did)
|
|
}
|
|
d := g.Displaced()
|
|
if d == nil || d.Writer != "FortiClient" || d.Content != vpnFile {
|
|
t.Fatalf("the write is not kept as written, naming its writer: %+v", d)
|
|
}
|
|
kept := filepath.Join(g.Dir, "displaced", d.ID+".conf")
|
|
if fi, err := os.Stat(kept); err != nil || fi.Mode().Perm() != 0o600 || contentOf(t, kept) != vpnFile {
|
|
t.Errorf("the write is not kept where only root reads it: %v", err)
|
|
}
|
|
*now = now.Add(2 * time.Second)
|
|
if err := g.Take(d.ID, "forticlient"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if did := g.Tick(); did != "put back, taken" {
|
|
t.Fatalf("a taken write was %q", did)
|
|
}
|
|
if contentOf(t, g.Path) != meshFile {
|
|
t.Errorf("the module's file is not back:\n%s", contentOf(t, g.Path))
|
|
}
|
|
if fi, _ := os.Stat(g.Path); fi.Mode().Perm() != 0o644 {
|
|
t.Errorf("the file is %v, not readable by everyone", fi.Mode().Perm())
|
|
}
|
|
history := contentOf(t, filepath.Join(g.Dir, "history.json"))
|
|
for _, never := range []string{"192.0.2", "corp.example", "nameserver"} {
|
|
if strings.Contains(history, never) {
|
|
t.Errorf("the history says %q, which stays on the machine:\n%s", never, history)
|
|
}
|
|
}
|
|
list := ReadHistory(filepath.Join(g.Dir, "history.json"))
|
|
if len(list) != 1 || list[0].TakenBy != "forticlient" || list[0].Ended == nil || !strings.Contains(list[0].How, "taken") {
|
|
t.Errorf("the history is %+v", list)
|
|
}
|
|
if g.Tick() != "" {
|
|
t.Error("the module's own file, back, was taken for another write")
|
|
}
|
|
}
|
|
|
|
// A write nobody takes stands for Hold — long enough for the node-engine to see it twice and say it —
|
|
// and is then put back.
|
|
func TestAWriteNobodyTakesStandsUntilItIsSaidThenGoes(t *testing.T) {
|
|
g, now, _ := aGuardedMachine(t)
|
|
write(t, g.Path, "# written by another program\nnameserver 198.51.100.1\n")
|
|
g.Tick()
|
|
*now = now.Add(61 * time.Second)
|
|
if did := g.Tick(); did != "holding" || contentOf(t, g.Path) == meshFile {
|
|
t.Fatalf("an untaken write was put back after a minute, before the node-engine's second look: %q", did)
|
|
}
|
|
if Hold < 75*time.Second {
|
|
t.Errorf("Hold is %s; two of the node-engine's 30 s looks need more", Hold)
|
|
}
|
|
*now = now.Add(Hold)
|
|
if did := g.Tick(); did != "put back, held" || contentOf(t, g.Path) != meshFile {
|
|
t.Fatalf("an untaken write was not put back after Hold: %q", did)
|
|
}
|
|
if h := ReadHistory(filepath.Join(g.Dir, "history.json")); len(h) != 1 || h[0].TakenBy != "" || h[0].Writer != "" {
|
|
t.Errorf("the history is %+v", h)
|
|
}
|
|
}
|
|
|
|
// A write the reconcile or the writer itself undoes is closed as written back by another; one written
|
|
// over before it was put back is a new one; a link in the file's place is a write too.
|
|
func TestWritesUndoneAndWrittenOverAreSaid(t *testing.T) {
|
|
g, now, _ := aGuardedMachine(t)
|
|
write(t, g.Path, vpnFile)
|
|
g.Tick()
|
|
write(t, g.Path, meshFile)
|
|
*now = now.Add(time.Second)
|
|
if did := g.Tick(); did != "ended" {
|
|
t.Errorf("a write undone by another was %q", did)
|
|
}
|
|
write(t, g.Path, vpnFile)
|
|
g.Tick()
|
|
first := g.Displaced().ID
|
|
*now = now.Add(time.Second)
|
|
write(t, g.Path, vpnFile+"search more.example\n")
|
|
g.Tick()
|
|
if g.Displaced().ID == first {
|
|
t.Error("a second write was taken for the first")
|
|
}
|
|
if err := g.Take(first, "forticlient"); err == nil {
|
|
t.Error("a write that no longer stands was taken")
|
|
}
|
|
_ = os.Remove(g.Path)
|
|
if err := os.Symlink(g.KeptPath, g.Path); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
*now = now.Add(time.Second)
|
|
g.Tick()
|
|
if d := g.Displaced(); d == nil || !strings.Contains(d.Content, "a link to") {
|
|
t.Errorf("a link in the file's place was not a write: %+v", d)
|
|
}
|
|
*now = now.Add(Hold)
|
|
g.Tick()
|
|
if fi, err := os.Lstat(g.Path); err != nil || fi.Mode()&os.ModeSymlink != 0 {
|
|
t.Errorf("the link was not replaced by the module's file: %v", err)
|
|
}
|
|
}
|
|
|
|
// Nothing is kept before the mesh rendered the module's file: there is nothing to keep it to.
|
|
func TestNothingIsGuardedBeforeTheFileIsRendered(t *testing.T) {
|
|
g, _, _ := aGuardedMachine(t)
|
|
_ = os.Remove(g.KeptPath)
|
|
write(t, g.Path, vpnFile)
|
|
if g.Tick() != "" || contentOf(t, g.Path) != vpnFile {
|
|
t.Error("the guard acted with no file of its own to keep")
|
|
}
|
|
}
|
|
|
|
// The socket: a module routes and takes the write standing now in one call, and the module's file is
|
|
// back; a bad request and an unknown verb are answered, not dropped; the socket is root's alone.
|
|
func TestTheVerbsOnTheMachine(t *testing.T) {
|
|
g, _, f := aGuardedMachine(t)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
sock := filepath.Join(g.Dir, "verbs.sock")
|
|
go func() { _ = g.Serve(ctx, sock) }()
|
|
for i := 0; i < 3000; i++ {
|
|
if _, err := os.Stat(sock); err == nil {
|
|
break
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
if fi, err := os.Stat(sock); err != nil || fi.Mode().Perm() != 0o600 {
|
|
t.Fatalf("the socket is not root's alone: %v", err)
|
|
}
|
|
call := func(line string) Reply {
|
|
t.Helper()
|
|
c, err := net.Dial("unix", sock)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer c.Close()
|
|
if _, err := c.Write([]byte(line + "\n")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw, err := bufio.NewReader(c).ReadBytes('\n')
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var r Reply
|
|
if err := json.Unmarshal(raw, &r); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return r
|
|
}
|
|
write(t, g.Path, vpnFile)
|
|
g.Tick()
|
|
shown := call(`{"verb":"displaced"}`)
|
|
pending, _ := shown.Result.(map[string]any)
|
|
if pending == nil || pending["content"] != vpnFile {
|
|
t.Fatalf("the write standing now is not shown on the machine: %+v", shown)
|
|
}
|
|
req, _ := json.Marshal(Request{Verb: "route", Args: map[string]any{"link": "tun0",
|
|
"domains": []any{"corp.example", "cloud.example"}, "servers": "192.0.2.53 192.0.2.54",
|
|
"takes": pending["id"], "by": "forticlient"}})
|
|
if r := call(string(req)); r.Error != "" {
|
|
t.Fatalf("route and take: %s", r.Error)
|
|
}
|
|
if len(f.changed) != 3 {
|
|
t.Errorf("resolved was asked %v", f.changed)
|
|
}
|
|
select {
|
|
case <-g.wake:
|
|
g.Tick()
|
|
case <-time.After(30 * time.Second):
|
|
t.Fatal("taking the write did not wake the guard")
|
|
}
|
|
if contentOf(t, g.Path) != meshFile {
|
|
t.Error("the module's file is not back once its write was taken")
|
|
}
|
|
if r := call(`not json`); !strings.Contains(r.Error, "JSON") {
|
|
t.Errorf("a bad request: %+v", r)
|
|
}
|
|
if r := call(`{"verb":"flush"}`); !strings.Contains(r.Error, "not a verb") {
|
|
t.Errorf("an unknown verb: %+v", r)
|
|
}
|
|
if r := call(`{"verb":"route","args":{"link":"tun0","domains":"internal","servers":"192.0.2.53"}}`); !strings.Contains(r.Error, "mesh's own") {
|
|
t.Errorf("the mesh's domain over the socket: %+v", r)
|
|
}
|
|
}
|
|
|
|
// Behind a network's sign-in page the mesh's resolvers are out of reach, so a link a network manager gave
|
|
// servers answers every name again until one of the mesh's resolvers answers; then it is kept from it once
|
|
// more. A link `route` routed keeps its default route off, even one the guard turned off before it was
|
|
// routed, and a guard started afresh behind the page finds the link from resolved alone. (1981/AC1)
|
|
func TestTheNetworksServersAnswerWhileTheMeshsResolversAreSilent(t *testing.T) {
|
|
g, _, f := aGuardedMachine(t)
|
|
meshAnswers := true
|
|
g.Resolver.Answers = func(_ context.Context, server string) bool { return meshAnswers && server == "10.42.0.1" }
|
|
f.dns = "Global: 10.42.0.1 10.42.0.2\nLink 2 (wlan0): 192.168.1.1\nLink 7 (tun0): 10.9.0.1\n"
|
|
ctx := context.Background()
|
|
step := func(g *Guard, domains, defaults string, want ...string) {
|
|
t.Helper()
|
|
f.domain, f.defaults, f.changed = domains, defaults, nil
|
|
g.KeepRoutes(ctx)
|
|
var got []string
|
|
for _, c := range f.changed {
|
|
got = append(got, strings.Join(c, " "))
|
|
}
|
|
if strings.Join(got, "; ") != strings.Join(want, "; ") {
|
|
t.Errorf("changed %q, want %q", got, want)
|
|
}
|
|
}
|
|
// tun0 comes up with servers and a default route, is turned off, and is then routed.
|
|
step(g, "Global: ~.\nLink 2 (wlan0): lan\nLink 7 (tun0):\n", "Link 2 (wlan0): yes\nLink 7 (tun0): yes\n",
|
|
"resolvectl default-route tun0 false", "resolvectl default-route wlan0 false")
|
|
routed := "Global: ~.\nLink 2 (wlan0): lan\nLink 7 (tun0): ~corp.example\n"
|
|
meshAnswers = false
|
|
step(g, routed, "Link 2 (wlan0): no\nLink 7 (tun0): no\n", "resolvectl default-route wlan0 true")
|
|
step(g, routed, "Link 2 (wlan0): yes\nLink 7 (tun0): no\n")
|
|
afresh, _, _ := aGuardedMachine(t)
|
|
afresh.Resolver = g.Resolver
|
|
step(afresh, routed, "Link 2 (wlan0): no\nLink 7 (tun0): no\n", "resolvectl default-route wlan0 true")
|
|
meshAnswers = true
|
|
step(g, routed, "Link 2 (wlan0): yes\nLink 7 (tun0): no\n", "resolvectl default-route wlan0 false")
|
|
}
|
|
|
|
// The uplink's names are answered over the link carrying the default route, and move with the route: the
|
|
// old link loses them and keeps its own search domain (design 08 sign-in-page §7, §9; R-4, R-5).
|
|
func TestUplinkNamesMoveWithTheDefaultRoute(t *testing.T) {
|
|
g, _, f, r := aGuardedMachineWithLinks(t, "wlan0", "wlan1")
|
|
f.dns = "Link 2 (wlan0): 192.168.1.1\n"
|
|
f.domain = "Link 2 (wlan0): lan\n"
|
|
f.ip = "default via 10.0.0.1 dev wlan0 metric 100\n"
|
|
if err := os.WriteFile(r.UplinkNamesFile, []byte("check.example\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
g.KeepUplinkNames(context.Background())
|
|
if len(f.changed) != 1 || strings.Join(f.changed[0], " ") != "resolvectl domain wlan0 lan ~check.example" {
|
|
t.Fatalf("wlan0 was given %v", f.changed)
|
|
}
|
|
if g.applied.Link != "wlan0" || strings.Join(g.applied.Names, ",") != "check.example" {
|
|
t.Fatalf("applied is %+v", g.applied)
|
|
}
|
|
applied := contentOf(t, r.UplinkNamesAppliedFile)
|
|
if !strings.Contains(applied, "wlan0") || !strings.Contains(applied, "check.example") {
|
|
t.Errorf("uplink-names.applied does not say so: %s", applied)
|
|
}
|
|
// The route moves to wlan1: wlan0, as resolved now holds it, keeps "lan" and the uplink's name.
|
|
f.domain = "Link 2 (wlan0): lan ~check.example\nLink 3 (wlan1):\n"
|
|
f.ip = "default via 10.1.0.1 dev wlan1 metric 50\n"
|
|
f.changed = nil
|
|
g.KeepUplinkNames(context.Background())
|
|
if len(f.changed) != 2 {
|
|
t.Fatalf("moving the route changed %v", f.changed)
|
|
}
|
|
if strings.Join(f.changed[0], " ") != "resolvectl domain wlan0 lan" {
|
|
t.Errorf("wlan0 lost the uplink's name but not its own: %v", f.changed[0])
|
|
}
|
|
if strings.Join(f.changed[1], " ") != "resolvectl domain wlan1 ~check.example" {
|
|
t.Errorf("wlan1 was not given the uplink's name: %v", f.changed[1])
|
|
}
|
|
if g.applied.Link != "wlan1" {
|
|
t.Errorf("applied is now %+v", g.applied)
|
|
}
|
|
}
|
|
|
|
// A changed list replaces the old one on the link, the route itself staying put (design 08 sign-in-page §9).
|
|
func TestAChangedListReplacesTheOldOneOnTheLink(t *testing.T) {
|
|
g, _, f, r := aGuardedMachineWithLinks(t, "wlan0")
|
|
f.dns = "Link 2 (wlan0): 192.168.1.1\n"
|
|
f.domain = "Link 2 (wlan0): lan\n"
|
|
f.ip = "default via 10.0.0.1 dev wlan0 metric 100\n"
|
|
if err := os.WriteFile(r.UplinkNamesFile, []byte("check.example\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
g.KeepUplinkNames(context.Background())
|
|
if len(f.changed) != 1 || strings.Join(f.changed[0], " ") != "resolvectl domain wlan0 lan ~check.example" {
|
|
t.Fatalf("the first list was given as %v", f.changed)
|
|
}
|
|
// resolved now holds it; the file's list changes, the link stays the same.
|
|
f.domain = "Link 2 (wlan0): lan ~check.example\n"
|
|
f.changed = nil
|
|
if err := os.WriteFile(r.UplinkNamesFile, []byte("other.example\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
g.KeepUplinkNames(context.Background())
|
|
if len(f.changed) != 1 || strings.Join(f.changed[0], " ") != "resolvectl domain wlan0 lan ~other.example" {
|
|
t.Errorf("the changed list was given as %v, want the old name replaced on the same link", f.changed)
|
|
}
|
|
if g.applied.Link != "wlan0" || strings.Join(g.applied.Names, ",") != "other.example" {
|
|
t.Errorf("applied is %+v", g.applied)
|
|
}
|
|
}
|
|
|
|
// A guard restarted with an applied file takes the uplink's names off the link it names, never inferring
|
|
// which (design 08 sign-in-page §8, §9; R-6).
|
|
func TestARestartedGuardTakesTheUplinkNamesOffTheLinkItNames(t *testing.T) {
|
|
g, _, f, r := aGuardedMachineWithLinks(t, "wlan0", "wlan1")
|
|
f.dns = "Link 2 (wlan0): 192.168.1.1\n"
|
|
f.domain = "Link 2 (wlan0): lan ~check.example\nLink 3 (wlan1):\n"
|
|
f.ip = "default via 10.1.0.1 dev wlan1 metric 50\n" // the route has already moved to wlan1
|
|
applied := UplinkApplied{Names: []string{"check.example"}, Link: "wlan0", At: time.Now()}
|
|
raw, _ := json.Marshal(applied)
|
|
if err := os.WriteFile(r.UplinkNamesAppliedFile, raw, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(r.UplinkNamesFile, []byte("check.example\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
g.applied = ReadUplinkApplied(r.UplinkNamesAppliedFile) // what the guard does at its start
|
|
g.KeepUplinkNames(context.Background())
|
|
if len(f.changed) != 2 || strings.Join(f.changed[0], " ") != "resolvectl domain wlan0 lan" ||
|
|
strings.Join(f.changed[1], " ") != "resolvectl domain wlan1 ~check.example" {
|
|
t.Errorf("the restarted guard did %v", f.changed)
|
|
}
|
|
}
|
|
|
|
// A link holding only the uplink's names is still given its default route back while the mesh's resolvers
|
|
// are silent: the guard's own names are left out of whether it is judged `route` routed (design 08
|
|
// sign-in-page §7, §9; DMC-2; issue 1981's AC1; R-7).
|
|
func TestALinkOfOnlyUplinkNamesStillGetsItsDefaultRouteBack(t *testing.T) {
|
|
g, _, f, r := aGuardedMachineWithLinks(t, "tun0")
|
|
g.Resolver.Answers = func(context.Context, string) bool { return false } // the mesh is silent
|
|
f.dns = "Global: 10.42.0.1\nLink 7 (tun0): 10.9.0.1\n"
|
|
f.domain = "Global: ~.\nLink 7 (tun0): ~check.example\n" // only the uplink's name, no search domain
|
|
f.defaults = "Link 7 (tun0): no\n"
|
|
applied := UplinkApplied{Names: []string{"check.example"}, Link: "tun0", At: time.Now()}
|
|
raw, _ := json.Marshal(applied)
|
|
if err := os.WriteFile(r.UplinkNamesAppliedFile, raw, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
g.KeepRoutes(context.Background())
|
|
if len(f.changed) != 1 || strings.Join(f.changed[0], " ") != "resolvectl default-route tun0 true" {
|
|
t.Errorf("the fallback did %v, want tun0 given its default route back", f.changed)
|
|
}
|
|
}
|
|
|
|
// A refusal by resolved is journalled once per change, never every round (design 08 sign-in-page §8; R-8).
|
|
func TestARefusalByResolvedIsJournalledOncePerChange(t *testing.T) {
|
|
g, _, f, r := aGuardedMachineWithLinks(t, "wlan0")
|
|
f.dns = "Link 2 (wlan0): 192.168.1.1\n"
|
|
f.ip = "default via 10.0.0.1 dev wlan0 metric 100\n"
|
|
if err := os.WriteFile(r.UplinkNamesFile, []byte("check.example\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
refuse := true
|
|
g.Resolver.Change = func(ctx context.Context, name string, args ...string) (string, error) {
|
|
if refuse && len(args) > 0 && args[0] == "domain" {
|
|
return "", errors.New("refused: not a domain mesh owns")
|
|
}
|
|
return f.change(ctx, name, args...)
|
|
}
|
|
var said []string
|
|
g.Log = func(format string, a ...any) { said = append(said, fmt.Sprintf(format, a...)) }
|
|
g.KeepUplinkNames(context.Background())
|
|
g.KeepUplinkNames(context.Background()) // the file and the link are unchanged: no new refusal to say
|
|
count := 0
|
|
for _, s := range said {
|
|
if strings.Contains(s, "refuses") {
|
|
count++
|
|
}
|
|
}
|
|
if count != 1 {
|
|
t.Errorf("the refusal was journalled %d times, want once: %v", count, said)
|
|
}
|
|
}
|
|
|
|
// The guard writes Resolver.FallbackFile at each of ADR 0688's two turns, and once more at its own first
|
|
// round, since it goes at every boot (design 08 sign-in-page §8; S-N5, R2-2; R-9).
|
|
func TestTheGuardWritesTheFallbackFileAtEachTurnAndItsFirstRound(t *testing.T) {
|
|
g, now, f := aGuardedMachine(t)
|
|
meshAnswers := true
|
|
g.Resolver.Answers = func(context.Context, string) bool { return meshAnswers }
|
|
f.dns = "Global: 10.42.0.1\n"
|
|
f.domain = "Global: ~.\n"
|
|
// The first round, the mesh answering, writes the file although nothing changed.
|
|
g.KeepRoutes(context.Background())
|
|
first := ReadFallback(g.Resolver.FallbackFile)
|
|
if first.Silent {
|
|
t.Fatalf("the first round says %+v, want not silent", first)
|
|
}
|
|
// A second round with nothing changed does not move `since`.
|
|
*now = now.Add(time.Second)
|
|
g.KeepRoutes(context.Background())
|
|
if again := ReadFallback(g.Resolver.FallbackFile); !again.Since.Equal(first.Since) {
|
|
t.Errorf("an unchanged round moved since: %v, was %v", again.Since, first.Since)
|
|
}
|
|
// The mesh goes silent: the turn is written with the new since.
|
|
*now = now.Add(time.Second)
|
|
meshAnswers = false
|
|
g.KeepRoutes(context.Background())
|
|
silentState := ReadFallback(g.Resolver.FallbackFile)
|
|
if !silentState.Silent || !silentState.Since.Equal(*now) {
|
|
t.Errorf("the silent turn is %+v, want silent since %v", silentState, *now)
|
|
}
|
|
// It answers again: the other turn.
|
|
*now = now.Add(time.Second)
|
|
meshAnswers = true
|
|
g.KeepRoutes(context.Background())
|
|
answering := ReadFallback(g.Resolver.FallbackFile)
|
|
if answering.Silent || !answering.Since.Equal(*now) {
|
|
t.Errorf("the answering turn is %+v, want not silent since %v", answering, *now)
|
|
}
|
|
}
|
|
|
|
// routes answers uplink_names with the applied list's names resolved holds on that link now, resolved's
|
|
// refusal, and mesh_resolvers from the fallback file (design 08 sign-in-page §2, §9; R-9).
|
|
func TestRoutesAnswersUplinkNamesAndMeshResolvers(t *testing.T) {
|
|
f := &fakeResolved{dns: "Link 2 (wlan0): 192.168.1.1\n", domain: "Link 2 (wlan0): lan ~check.example\n"}
|
|
r := aMachine(t, f, "wlan0")
|
|
applied := UplinkApplied{Names: []string{"check.example", "not-held.example"}, Link: "wlan0", Refused: "refused once"}
|
|
raw, _ := json.Marshal(applied)
|
|
if err := os.WriteFile(r.UplinkNamesAppliedFile, raw, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
since := time.Date(2026, 11, 24, 10, 0, 0, 0, time.UTC)
|
|
fb, _ := json.Marshal(FallbackState{Silent: true, Since: since})
|
|
if err := os.WriteFile(r.FallbackFile, fb, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
routes, err := r.Routes(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
un := r.UplinkNamesAnswer(routes)
|
|
if names, _ := un["names"].([]string); strings.Join(names, ",") != "check.example" || un["link"] != "wlan0" || un["refused"] != "refused once" {
|
|
t.Errorf("uplink_names is %+v", un)
|
|
}
|
|
mr := r.MeshResolversAnswer()
|
|
if mr["silent"] != true || !mr["since"].(time.Time).Equal(since) {
|
|
t.Errorf("mesh_resolvers is %+v", mr)
|
|
}
|
|
}
|
|
|
|
// A network joined while the mesh's resolvers are silent keeps its default route, and loses it once one answers.
|
|
// (1981/AC2)
|
|
func TestANetworkJoinedWhileTheMeshIsSilentKeepsItsDefaultRoute(t *testing.T) {
|
|
g, _, f := aGuardedMachine(t)
|
|
meshAnswers := false
|
|
g.Resolver.Answers = func(context.Context, string) bool { return meshAnswers }
|
|
f.dns = "Global: 10.42.0.1\nLink 2 (wlan0): 192.168.1.1\n"
|
|
f.domain = "Global: ~.\n"
|
|
f.defaults = "Link 2 (wlan0): yes\n"
|
|
g.KeepRoutes(context.Background())
|
|
if len(f.changed) != 0 {
|
|
t.Errorf("changed %v while the mesh was silent", f.changed)
|
|
}
|
|
meshAnswers = true
|
|
g.KeepRoutes(context.Background())
|
|
if len(f.changed) != 1 || strings.Join(f.changed[0], " ") != "resolvectl default-route wlan0 false" {
|
|
t.Errorf("changed %v once the mesh answered", f.changed)
|
|
}
|
|
}
|