C-6: import noderesolver (mesh-networking's seat definition) at the zero version; routes, route, unroute and uplink-names answer with its RoutesAnswer, RouteAnswer, UnrouteAnswer and UplinkNamesAnswer, never a map or a parallel struct, and uplink-names' input names is a JSON array (arrayArg), never a comma- or space-split string. C-8: the names file write's 15 s is the stated constant WriteUplinkNamesBound, not an inline literal. Builds: design 08 sign-in-page §2, §5, §6, §9, DQ-12, C-6, C-8
364 lines
13 KiB
Go
364 lines
13 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// fakeResolved answers resolvectl, and `ip route show default`, as the machine would, and records what was
|
|
// asked of it.
|
|
type fakeResolved struct {
|
|
dns, domain, defaults string
|
|
ip string
|
|
changed [][]string
|
|
}
|
|
|
|
func (f *fakeResolved) read(_ context.Context, name string, args ...string) (string, error) {
|
|
if name == "ip" {
|
|
if len(args) > 0 && args[0] == "-6" {
|
|
return "", nil
|
|
}
|
|
return f.ip, nil
|
|
}
|
|
switch args[0] {
|
|
case "dns":
|
|
return f.dns, nil
|
|
case "domain":
|
|
return f.domain, nil
|
|
case "default-route":
|
|
return f.defaults, nil
|
|
}
|
|
return "", nil
|
|
}
|
|
|
|
func (f *fakeResolved) change(_ context.Context, name string, args ...string) (string, error) {
|
|
f.changed = append(f.changed, append([]string{name}, args...))
|
|
return "", nil
|
|
}
|
|
|
|
// aMachine is a resolver over a fake resolved and a /sys/class/net holding the links named.
|
|
func aMachine(t *testing.T, f *fakeResolved, links ...string) *Resolver {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
for _, l := range links {
|
|
if err := os.MkdirAll(filepath.Join(dir, "net", l), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
suffix := filepath.Join(dir, "suffix")
|
|
if err := os.WriteFile(suffix, []byte("internal\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return &Resolver{Change: f.change, Read: f.read, NetDir: filepath.Join(dir, "net"), SuffixFile: suffix,
|
|
PrivateInterfaceFile: filepath.Join(dir, "private-interface"),
|
|
UplinkNamesFile: filepath.Join(dir, "uplink-names"),
|
|
UplinkNamesAppliedFile: filepath.Join(dir, "uplink-names.applied"),
|
|
FallbackFile: filepath.Join(dir, "fallback.json"),
|
|
Now: func() time.Time { return time.Date(2026, 11, 24, 12, 0, 0, 0, time.UTC) },
|
|
WriteUplinkNames: func(_ context.Context, content []byte) error {
|
|
return os.WriteFile(filepath.Join(dir, "uplink-names"), content, 0o644)
|
|
}}
|
|
}
|
|
|
|
// A route sends the domains to the servers over the link, and only them: the link is first told it is
|
|
// no default route, then given its domains as routing domains, then its servers.
|
|
func TestARouteSendsOnlyItsDomainsOverItsLink(t *testing.T) {
|
|
f := &fakeResolved{}
|
|
r := aMachine(t, f, "tun0")
|
|
got, err := r.Route(context.Background(), "tun0", Split("Corp.Example. ~cloud.example, corp.example"), Split("10.9.0.1 10.9.0.2"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := []string{
|
|
"resolvectl default-route tun0 false",
|
|
"resolvectl domain tun0 ~corp.example ~cloud.example",
|
|
"resolvectl dns tun0 10.9.0.1 10.9.0.2",
|
|
}
|
|
if len(f.changed) != len(want) {
|
|
t.Fatalf("resolved was asked %v", f.changed)
|
|
}
|
|
for i, w := range want {
|
|
if strings.Join(f.changed[i], " ") != w {
|
|
t.Errorf("step %d was %v, not %s", i, f.changed[i], w)
|
|
}
|
|
}
|
|
if len(got.Domains) != 2 || len(got.Servers) != 2 {
|
|
t.Errorf("the route says %+v", got)
|
|
}
|
|
}
|
|
|
|
// The mesh's own domain, the root, a link that is not here and loopback are refused, and resolved is
|
|
// asked nothing.
|
|
func TestARouteThatWouldTakeTheMeshsNamesIsRefused(t *testing.T) {
|
|
for name, c := range map[string]struct{ link, domains, servers, says string }{
|
|
"the mesh's domain": {"tun0", "corp.example internal", "10.9.0.1", "mesh's own domain"},
|
|
"under it": {"tun0", "anchor.internal", "10.9.0.1", "mesh's own domain"},
|
|
"the root": {"tun0", "~.", "10.9.0.1", "every name"},
|
|
"no domain": {"tun0", "", "10.9.0.1", "no domain"},
|
|
"not a domain": {"tun0", "a b/c", "10.9.0.1", "not a domain"},
|
|
"no server": {"tun0", "corp.example", "", "no server"},
|
|
"not an address": {"tun0", "corp.example", "dns.corp.example", "not an address"},
|
|
"a link not here": {"tun9", "corp.example", "10.9.0.1", "no link"},
|
|
"loopback": {"lo", "corp.example", "10.9.0.1", "loopback"},
|
|
"not a link's name": {"../etc", "corp.example", "10.9.0.1", "not a link"},
|
|
"an unspecified one": {"tun0", "corp.example", "0.0.0.0", "cannot answer"},
|
|
"a multicast address": {"tun0", "corp.example", "224.0.0.251", "cannot answer"},
|
|
} {
|
|
f := &fakeResolved{}
|
|
r := aMachine(t, f, "tun0", "lo")
|
|
_, err := r.Route(context.Background(), c.link, Split(c.domains), Split(c.servers))
|
|
if err == nil || !strings.Contains(err.Error(), c.says) {
|
|
t.Errorf("%s: %v", name, err)
|
|
}
|
|
if len(f.changed) != 0 {
|
|
t.Errorf("%s: resolved was changed anyway: %v", name, f.changed)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Routes reads resolved: the mesh's resolvers as the global scope, and each link with servers of its
|
|
// own, its routing domains without the `~` and whether it is a default route.
|
|
func TestRoutesReadWhatResolvedSendsWhere(t *testing.T) {
|
|
f := &fakeResolved{
|
|
dns: "Global: 10.42.0.1 10.42.0.3\nLink 2 (wlan0): 192.168.1.1\nLink 3 (mesh0):\nLink 7 (tun0): 10.9.0.1 10.9.0.2\n",
|
|
domain: "Global: ~.\nLink 2 (wlan0):\nLink 3 (mesh0):\nLink 7 (tun0): ~corp.example ~cloud.example\n",
|
|
defaults: "Link 2 (wlan0): yes\nLink 3 (mesh0): yes\nLink 7 (tun0): no\n",
|
|
}
|
|
got, err := aMachine(t, f).Routes(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Join(got.Mesh.Servers, " ") != "10.42.0.1 10.42.0.3" || strings.Join(got.Mesh.Domains, " ") != "." {
|
|
t.Errorf("the mesh's scope is %+v", got.Mesh)
|
|
}
|
|
if len(got.Links) != 2 || got.Links[0].Link != "tun0" && got.Links[1].Link != "tun0" {
|
|
t.Fatalf("the links with servers are %+v", got.Links)
|
|
}
|
|
for _, l := range got.Links {
|
|
if l.Link == "tun0" && (strings.Join(l.Domains, " ") != "corp.example cloud.example" || *l.DefaultRoute) {
|
|
t.Errorf("tun0 is %+v", l)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A link a network manager gave servers is kept from answering every name: its default route goes, its
|
|
// own domains stay; a link without servers, and one already routed, are left alone.
|
|
func TestOnlyTheMeshsResolversAnswerEveryName(t *testing.T) {
|
|
f := &fakeResolved{
|
|
dns: "Global: 10.42.0.1\nLink 2 (wlan0): 192.168.1.1\nLink 3 (mesh0):\nLink 7 (tun0): 10.9.0.1\n",
|
|
domain: "Global: ~.\nLink 2 (wlan0): lan\nLink 7 (tun0): ~corp.example\n",
|
|
defaults: "Link 2 (wlan0): yes\nLink 3 (mesh0): yes\nLink 7 (tun0): no\n",
|
|
}
|
|
changed, err := aMachine(t, f).OnlyTheMeshIsADefaultRoute(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Join(changed, " ") != "wlan0" || len(f.changed) != 1 ||
|
|
strings.Join(f.changed[0], " ") != "resolvectl default-route wlan0 false" {
|
|
t.Errorf("changed %v by %v", changed, f.changed)
|
|
}
|
|
}
|
|
|
|
// Taking a route away reverts the link; a link that went has nothing to take away.
|
|
func TestUnrouteRevertsTheLink(t *testing.T) {
|
|
f := &fakeResolved{}
|
|
r := aMachine(t, f, "tun0")
|
|
if _, err := r.Unroute(context.Background(), "tun0"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := r.Unroute(context.Background(), "tun1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(f.changed) != 1 || strings.Join(f.changed[0], " ") != "resolvectl revert tun0" {
|
|
t.Errorf("resolved was asked %v", f.changed)
|
|
}
|
|
}
|
|
|
|
// Escalation: root runs the command as given; the runtime's account through sudo without a prompt.
|
|
func TestChangesAreEscalatedOnlyWhenNotRoot(t *testing.T) {
|
|
if p, a := escalated(0, "resolvectl", []string{"revert", "tun0"}); p != "resolvectl" || len(a) != 2 {
|
|
t.Errorf("as root: %s %v", p, a)
|
|
}
|
|
if p, a := escalated(1000, "resolvectl", []string{"revert", "tun0"}); p != "sudo" || strings.Join(a, " ") != "-n resolvectl revert tun0" {
|
|
t.Errorf("as the account: %s %v", p, a)
|
|
}
|
|
}
|
|
|
|
// Given names, uplink-names writes the file one a line and answers them (design 08 sign-in-page §2, §6;
|
|
// R-1).
|
|
func TestUplinkNamesWritesTheFileAndAnswersThem(t *testing.T) {
|
|
f := &fakeResolved{}
|
|
r := aMachine(t, f, "tun0")
|
|
got, err := r.SetUplinkNames(context.Background(), []string{"Check.Example", "check2.example"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Join(got.Names, " ") != "check.example check2.example" {
|
|
t.Errorf("the answer names %v", got.Names)
|
|
}
|
|
if got.SetAt == "" {
|
|
t.Error("set_at was not stamped")
|
|
}
|
|
raw, err := os.ReadFile(r.UplinkNamesFile)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(raw) != "check.example\ncheck2.example\n" {
|
|
t.Errorf("the file holds %q", raw)
|
|
}
|
|
}
|
|
|
|
// An empty list empties the file (design 08 sign-in-page §2; R-2).
|
|
func TestAnEmptyListEmptiesTheUplinkNamesFile(t *testing.T) {
|
|
f := &fakeResolved{}
|
|
r := aMachine(t, f, "tun0")
|
|
if _, err := r.SetUplinkNames(context.Background(), []string{"check.example"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := r.SetUplinkNames(context.Background(), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(got.Names) != 0 {
|
|
t.Errorf("the answer names %v, not empty", got.Names)
|
|
}
|
|
raw, err := os.ReadFile(r.UplinkNamesFile)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(raw) != "" {
|
|
t.Errorf("the file holds %q, not emptied", raw)
|
|
}
|
|
}
|
|
|
|
// Each refusal of §2 is answered naming the name and the reason, and writes nothing (design 08
|
|
// sign-in-page §2; acceptance test 4; R-3).
|
|
func TestEachRefusalOfUplinkNamesIsNamedAndWritesNothing(t *testing.T) {
|
|
for name, c := range map[string]struct{ given, says string }{
|
|
"the root": {".", "root"},
|
|
"empty string": {"", "root"},
|
|
"a wildcard": {"*.example", "wildcard"},
|
|
"a trailing dot": {"check.example.", "trailing dot"},
|
|
"a label over 63": {strings.Repeat("a", 64) + ".example", "label over 63 bytes"},
|
|
"the mesh's domain": {"internal", "mesh's own domain"},
|
|
"under it": {"check.internal", "mesh's own domain"},
|
|
"not a host name": {"a b/c", "not a host name"},
|
|
} {
|
|
f := &fakeResolved{}
|
|
r := aMachine(t, f, "tun0")
|
|
_, err := r.SetUplinkNames(context.Background(), []string{c.given})
|
|
if err == nil || !strings.Contains(err.Error(), c.says) {
|
|
t.Errorf("%s: %v", name, err)
|
|
}
|
|
if _, statErr := os.Stat(r.UplinkNamesFile); statErr == nil {
|
|
t.Errorf("%s: the file was written: %s", name, contentOf(t, r.UplinkNamesFile))
|
|
}
|
|
}
|
|
f := &fakeResolved{}
|
|
r := aMachine(t, f, "tun0")
|
|
nine := make([]string, 9)
|
|
for i := range nine {
|
|
nine[i] = fmt.Sprintf("check%d.example", i)
|
|
}
|
|
_, err := r.SetUplinkNames(context.Background(), nine)
|
|
if err == nil || !strings.Contains(err.Error(), "at most 8") {
|
|
t.Errorf("more than 8 names: %v", err)
|
|
}
|
|
if _, statErr := os.Stat(r.UplinkNamesFile); statErr == nil {
|
|
t.Error("more than 8 names: the file was written")
|
|
}
|
|
}
|
|
|
|
// The link carrying the default route is the lowest-metric one that is neither routed (a VPN's) nor the
|
|
// private network's; none when there is none (design 08 sign-in-page §7, §9; DQ-1, DQ-2).
|
|
func TestDefaultRouteLinkIsTheLowestMetricOneThatIsNeitherRoutedNorPrivate(t *testing.T) {
|
|
f := &fakeResolved{}
|
|
r := aMachine(t, f, "tun0", "wlan0", "mesh0")
|
|
if err := os.WriteFile(r.PrivateInterfaceFile, []byte("mesh0\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f.ip = "default via 10.0.0.1 dev wlan0 metric 600\ndefault via 10.1.0.1 dev mesh0 metric 50\n"
|
|
f.dns = "Link 7 (tun0): 10.9.0.1\n"
|
|
f.domain = "Link 7 (tun0): ~corp.example\n"
|
|
routes, err := r.Routes(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
link, err := r.DefaultRouteLink(context.Background(), routes)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if link != "wlan0" {
|
|
t.Errorf("the link is %q, not wlan0 (mesh0 is the private interface)", link)
|
|
}
|
|
// A link `route` routed is also never chosen.
|
|
f.ip = "default via 10.0.0.1 dev tun0 metric 10\ndefault via 10.2.0.1 dev wlan0 metric 600\n"
|
|
link, err = r.DefaultRouteLink(context.Background(), routes)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if link != "wlan0" {
|
|
t.Errorf("the link is %q, not wlan0 (tun0 is routed)", link)
|
|
}
|
|
// None when there is none.
|
|
f.ip = "default via 10.0.0.1 dev tun0 metric 10\ndefault via 10.1.0.1 dev mesh0 metric 50\n"
|
|
if link, err = r.DefaultRouteLink(context.Background(), routes); err != nil || link != "" {
|
|
t.Errorf("the link is %q, want none", link)
|
|
}
|
|
}
|
|
|
|
// One of the mesh's resolvers answers when it says anything, a name not found included; one that is silent
|
|
// does not, and the guard does not wait on it past the config value answer-within, read from the file the
|
|
// mesh renders, or a second before it has. (1981/AC3)
|
|
func TestAResolverAnswersUnlessItIsSilent(t *testing.T) {
|
|
conn, err := net.ListenPacket("udp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer conn.Close()
|
|
go func() {
|
|
buf := make([]byte, 512)
|
|
for {
|
|
n, from, err := conn.ReadFrom(buf)
|
|
if err != nil {
|
|
return
|
|
}
|
|
reply := append([]byte{}, buf[:n]...)
|
|
reply[2], reply[3] = 0x81, 0x83 // a reply, recursion available, the name not found
|
|
conn.WriteTo(reply, from)
|
|
}
|
|
}()
|
|
silent, err := net.ListenPacket("udp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer silent.Close()
|
|
r := aMachine(t, &fakeResolved{})
|
|
r.AnswerWithinFile = filepath.Join(t.TempDir(), "answer-within")
|
|
if got := r.answerWithin(); got != DefaultAnswerWithin {
|
|
t.Errorf("before the file is rendered, a resolver is given %v", got)
|
|
}
|
|
if err := os.WriteFile(r.AnswerWithinFile, []byte("# written by the mesh\nseconds=0.1\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := r.answerWithin(); got != 100*time.Millisecond {
|
|
t.Errorf("with answer-within 0.1, a resolver is given %v", got)
|
|
}
|
|
if !r.asks(context.Background(), conn.LocalAddr().String()) {
|
|
t.Error("a resolver that said the name is not found did not answer")
|
|
}
|
|
start := time.Now()
|
|
if r.asks(context.Background(), silent.LocalAddr().String()) {
|
|
t.Error("a silent resolver answered")
|
|
}
|
|
if took := time.Since(start); took > 300*time.Millisecond {
|
|
t.Errorf("a silent resolver was waited on %v", took)
|
|
}
|
|
}
|