Files
dawnline/backend/apply/apply_test.go
T
Aemeath ac90575b0b
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / Backend (Go: vet, tests, a real ubusd and uhttpd) (push) Blocked by required conditions
CI / Package files (push) Blocked by required conditions
Dawnline 0.20.0
- **Backend rewritten in Go.** One daemon, `dawnlined`, replaces the whole ucode backend: the rpcd plugin, the agent, the CLI and the updater. The package no longer depends on ucode.
- **Per-architecture builds.** The release carries the backend for arm64, ARMv7, mips, mipsle and amd64; the router fetches its own after the package is installed and checks its sha256. Dawnline does not run on other architectures (older ARM, 32-bit x86, RISC-V).
- **Faster on the router.** A firewall sync takes 0.06 s instead of 0.9 s; a full apply takes about 300 ms. The portal is served by the daemon itself, without a CGI process per request.
- **Live pages.** The admin and the portal follow a live stream from the router instead of re-fetching the whole state every 30 seconds.
- **Admin redesigned in the portal's style.** Three sections (Overview · Log · Settings); a server is a page of its own with People, Places and Settings; a device opens as a dialog over its person.
- **Changes wait for «Apply».** Every setting, and a person's access, is staged in one dock before it reaches the router.
- **Configuration rollout.** After a change that makes devices fetch their configuration again, the admin shows how many have fetched it and who has not. Every setting shows beforehand (⟳N) how many devices it will touch.
- **Waiting for a decision.** Overview and People put first the people who need one (access ended, limit reached, a device that does not connect), each with its one action a tap away.
- **Bulk actions on people.** «Select» (or a long press on a card) picks several people to extend by a month, pause or resume at once.
- **Undo.** An action done from a card or a request is undone from its toast.
- **Update dialogs redesigned.** An update is one view from its question to its result; Dawnline's own update runs in it like every other, and the page reloads itself once the new version answers. Other versions are a list, each with its notes and Install on a page of its own.
- **Site to site: networks behind a router.** A router connected as a device can have networks behind it (up to eight, /16 to /30): the server routes them and applies the router's access to them. A network that shares addresses with a tunnel, the router's own networks or another site is refused by name.
- **Routers from the portal.** People can add a router and name its networks on their own page; the admin accepts or declines with a reason, or lets them through without asking.
- **Check access and Health know the networks behind routers.**
- **Usage limits reworked.** A monthly limit no longer follows the calendar. A person's month starts with their first traffic and ends a month later; the next one starts only when they connect again, so time away uses up none of it. Before, everyone's limit reset on the 1st.
- Extra data lasts until that person's month ends; «Reset usage» starts the month anew.
- A limit is for a month or for the whole term; the admin and the portal show when it resets.
- Charts that said «this month» show the last 30 days.
- After the update nobody loses what was counted: the month that was running becomes the person's first and ends on the 1st.
- **Access by time of day removed.** The Hours limit is gone from the admin, the portal and the backend; saved hours are dropped.
- **Expiry simplified.** An end date always stops access, with a heads-up three days before; the per-person choice is gone.
- **No false disconnects from sleeping phones.** A phone or a tablet stays online through ten minutes of silence.
- **Mobile Safari fixes.** Pressing a control no longer sends the page to its top; tab tracks that do not fit scroll; switches animate after a press.
2026-11-13 09:43:50 +03:00

244 lines
9.3 KiB
Go

package apply
import (
"encoding/json"
"os"
"strings"
"testing"
"github.com/tickcount/luci-app-dawnline/backend/awg"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/netview"
"github.com/tickcount/luci-app-dawnline/backend/uci"
)
// The network signature is ucode's own text: its agent applies again when the two differ (testdata/sig.uc).
func TestNetSignatureAsUcode(t *testing.T) {
raw, err := os.ReadFile("testdata/sig.json")
if err != nil {
t.Fatal(err)
}
var g struct {
Sigs []string `json:"sigs"`
}
if err := json.Unmarshal(raw, &g); err != nil || len(g.Sigs) != 3 {
t.Fatal("the golden is not as expected")
}
raw, err = os.ReadFile("../netview/testdata/trace.json")
if err != nil {
t.Fatal(err)
}
var router struct {
Routes json.RawMessage `json:"routes"`
Addrs json.RawMessage `json:"addrs"`
Firewall string `json:"firewall"`
Links map[string]netview.Link `json:"links"`
}
if err := json.Unmarshal(raw, &router); err != nil {
t.Fatal(err)
}
fw, _ := uci.Parse(router.Firewall)
dir := t.TempDir()
r := &Router{RunDir: dir, Snapshot: func() *netview.Snapshot {
return netview.Take(netview.Sources{Routes: func() []byte { return router.Routes }, Addrs: func() []byte { return router.Addrs },
Links: func() map[string]netview.Link { return router.Links }, Zones: func() netview.Zones { return netview.ZonesOf(fw) }})
}}
text, _ := os.ReadFile("../access/testdata/access.uci")
c, err := uci.Parse(string(text))
if err != nil {
t.Fatal(err)
}
for i, want := range g.Sigs {
switch i {
case 1:
c.SetList("home", "lan_device", []string{"dlhome", "nope0", "lanp1"})
c.Set("cabin", "auto_lan", "1")
c.Set("open", "auto_wan", "0")
c.Set("open", "wan_device", "wg9")
case 2:
c.Set("open", "wan_device", "gone7")
c.SetList("open", "lan_device", []string{"gone8"})
c.Set("cabin", "lan_zone", "lan")
}
m := model.Load(c, nil)
if got := r.NetSignature(m); got != want {
t.Errorf("signature %d:\n%s\n--- ucode:\n%s", i, got, want)
}
if i == 0 {
if !r.NetChanged(m) {
t.Error("no signature kept, and nothing is due")
}
os.WriteFile(dir+"/net.sig", []byte(want), 0o644)
if r.NetChanged(m) {
t.Error("an apply is due with the networks as they were")
}
} else if !r.NetChanged(m) {
t.Error("the networks changed, and no apply is due")
}
}
}
// A plan of the full apply changes nothing: after a real apply of the same settings it finds nothing to
// do, and after a change it names the lines and the commands, with the router's own files as they were.
func TestApplyPlanChangesNothing(t *testing.T) {
raw, _ := os.ReadFile("../netview/testdata/trace.json")
var router struct {
Routes json.RawMessage `json:"routes"`
Addrs json.RawMessage `json:"addrs"`
Firewall string `json:"firewall"`
Links map[string]netview.Link `json:"links"`
}
if err := json.Unmarshal(raw, &router); err != nil {
t.Fatal(err)
}
dir := t.TempDir()
network, _ := os.ReadFile("testdata/network.uci")
os.WriteFile(dir+"/network", network, 0o644)
os.WriteFile(dir+"/firewall", []byte(router.Firewall), 0o644)
var ran []string
r := &Router{RunDir: dir + "/run", VarEtc: dir + "/etc", ResolverPid: dir + "/pid", SysNet: dir + "/sys", NetnsDir: dir + "/netns", Leases: dir + "/leases",
Network: dir + "/network", Firewall: dir + "/firewall", HostsFile: dir + "/hosts",
Exec: func(name string, args ...string) (string, int) {
line := name + " " + strings.Join(args, " ")
if !asks(name, args) {
ran = append(ran, line)
}
if strings.HasPrefix(line, "nft list") {
return "", 1
}
return "", 0
}}
r.Snapshot = func() *netview.Snapshot {
text, _ := os.ReadFile(dir + "/firewall")
fw, _ := uci.Parse(string(text))
return netview.Take(netview.Sources{Routes: func() []byte { return router.Routes }, Addrs: func() []byte { return router.Addrs },
Links: func() map[string]netview.Link { return router.Links }, Zones: func() netview.Zones { return netview.ZonesOf(fw) }})
}
text, _ := os.ReadFile("../access/testdata/access.uci")
c, _ := uci.Parse(string(text))
// nobody like a home device here: that path has its own test, with a router to answer it
for _, id := range []string{"home", "cabin", "open"} {
c.Set(id, "lan_like", "0")
}
load := func() *model.Model {
var blocked map[string]any
if raw, err := os.ReadFile(dir + "/run/netifd-blocked.json"); err == nil {
json.Unmarshal(raw, &blocked)
}
return model.Load(c, blocked)
}
caps := awg.Caps{V2: true, V3: true}
m := load()
r.Netifd(m, true, caps)
r.Fw(m)
r.LoopEnsure(m, "", true)
r.SyncResolver(m)
r.ReplyRoutes(m)
os.WriteFile(dir+"/run/net.sig", []byte(r.NetSignature(m)), 0o644)
keep := func() string {
out := ""
for _, f := range []string{"/network", "/firewall", "/run/netifd-blocked.json", "/run/loop.json", "/run/iface-intent.json", "/etc/dawnline-dns.conf", "/run/net.sig"} {
raw, _ := os.ReadFile(dir + f)
out += f + "\n" + string(raw) + "\n"
}
return out
}
before := keep()
ran = nil
plan := r.ApplyPlan(load(), "", true, caps)
js, _ := json.Marshal(plan)
if plan["network"] != false || plan["firewall"] != false || plan["resolver"] != false || plan["signature"] != true ||
len(plan["would_run"].([]string)) != 0 || len(plan["network_lines"].([]string)) != 0 || plan["like_home"] != "off" {
t.Errorf("something to do right after an apply: %s", js)
}
// a port moved: the plan says so, and the router is as it was
c.Set("home", "port", "4999")
plan = r.ApplyPlan(load(), "", true, caps)
js, _ = json.Marshal(plan)
if plan["network"] != true || plan["firewall"] != true {
t.Errorf("a moved port changes nothing: %s", js)
}
lines := strings.Join(plan["network_lines"].([]string), "\n") + "\n" + strings.Join(plan["firewall_lines"].([]string), "\n")
if !strings.Contains(lines, "- \toption listen_port '4500'") || !strings.Contains(lines, "+ \toption listen_port '4999'") || !strings.Contains(lines, "+ \toption dest_port '4999'") {
t.Errorf("the lines of the change:\n%s", lines)
}
would := strings.Join(plan["would_run"].([]string), "; ")
if !strings.Contains(would, "ubus call network reload") || !strings.Contains(would, "ifup dawn0") || !strings.Contains(would, "fw4 -q reload") {
t.Errorf("the commands of the change: %s", would)
}
if strings.Contains(string(js), "aaaaaaaa") {
t.Error("a key in the plan")
}
if keep() != before || len(ran) != 0 {
t.Errorf("the plan changed the router: ran %v", ran)
}
}
// A teardown leaves the router's network and firewall as they were before Dawnline: what is ours goes,
// what is not stays, an interface of someone else's under a tunnel's name too.
func TestTeardown(t *testing.T) {
raw, _ := os.ReadFile("../netview/testdata/trace.json")
var router struct {
Routes json.RawMessage `json:"routes"`
Addrs json.RawMessage `json:"addrs"`
Firewall string `json:"firewall"`
Links map[string]netview.Link `json:"links"`
}
if err := json.Unmarshal(raw, &router); err != nil {
t.Fatal(err)
}
dir := t.TempDir()
network, _ := os.ReadFile("testdata/network.uci")
os.WriteFile(dir+"/network", network, 0o644)
os.WriteFile(dir+"/firewall", []byte(router.Firewall), 0o644)
os.WriteFile(dir+"/hosts", []byte("10.77.0.2 phone.anna.vpn\n"), 0o644)
var ran []string
r := &Router{RunDir: dir + "/run", VarEtc: dir + "/etc", SysNet: dir + "/sys", NetnsDir: dir + "/netns", Leases: dir + "/leases",
Network: dir + "/network", Firewall: dir + "/firewall", HostsFile: dir + "/hosts",
Exec: func(name string, args ...string) (string, int) {
if !asks(name, args) {
ran = append(ran, name+" "+strings.Join(args, " "))
}
return "", 0
}}
r.Snapshot = func() *netview.Snapshot {
text, _ := os.ReadFile(dir + "/firewall")
fw, _ := uci.Parse(string(text))
return netview.Take(netview.Sources{Routes: func() []byte { return router.Routes }, Addrs: func() []byte { return router.Addrs },
Links: func() map[string]netview.Link { return router.Links }, Zones: func() netview.Zones { return netview.ZonesOf(fw) }})
}
text, _ := os.ReadFile("../access/testdata/access.uci")
c, _ := uci.Parse(string(text))
m := model.Load(c, nil)
r.Netifd(m, true, awg.Caps{V2: true, V3: true})
r.Fw(m)
os.WriteFile(dir+"/run/net.sig", []byte("x"), 0o644)
if now, _ := os.ReadFile(dir + "/firewall"); !strings.Contains(string(now), "dawnline_home_zone") {
t.Fatal("nothing was set up to take down")
}
ran = nil
r.Teardown(model.Load(c, nil))
as := func(text []byte) string { c, _ := uci.Parse(string(text)); return c.Format() }
if now, _ := os.ReadFile(dir + "/network"); string(now) != as(network) {
t.Errorf("the network configuration after:\n%s", now)
}
if now, _ := os.ReadFile(dir + "/firewall"); string(now) != as([]byte(router.Firewall)) {
t.Errorf("the firewall after:\n%s", now)
}
for _, f := range []string{"/hosts", "/run/net.sig"} {
if _, err := os.Stat(dir + f); err == nil {
t.Errorf("%s stays", f)
}
}
did := strings.Join(ran, "; ")
for _, want := range []string{"ifdown dawn0", "ifdown dawn1", "ubus call network reload", "fw4 -q reload", "nft delete table inet dawnline", "killall -HUP dnsmasq"} {
if !strings.Contains(did, want) {
t.Errorf("%s was not run: %s", want, did)
}
}
if strings.Contains(did, "ifdown dawn2") || strings.Contains(did, "ifup") {
t.Errorf("more than ours was touched: %s", did)
}
}