Files
dawnline/backend/model/behind_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

262 lines
8.1 KiB
Go

package model
import (
"reflect"
"testing"
"github.com/tickcount/luci-app-dawnline/backend/ipnet"
"github.com/tickcount/luci-app-dawnline/backend/uci"
)
func TestWhatMayStandBehindARouter(t *testing.T) {
for _, c := range []struct{ in, net, why string }{
{"192.168.2.0/24", "192.168.2.0/24", ""},
{"192.168.2.7/24", "192.168.2.0/24", ""},
{"10.5.0.0/16", "10.5.0.0/16", ""},
{"172.31.255.0/30", "172.31.255.0/30", ""},
{"192.168.2.0", "", "form"},
{"192.168.2.0/31", "", "form"},
{"192.168.2.0/24 ", "", "form"},
{"home", "", "form"},
{"", "", "form"},
{"10.0.0.0/8", "", "wide"},
{"10.4.0.0/15", "", "wide"},
{"8.8.8.0/24", "", "public"},
{"172.32.0.0/24", "", "public"},
{"100.64.0.0/24", "", "public"},
{"169.254.1.0/24", "", "public"},
} {
n, why := BehindNet(c.in)
if why != c.why || (why == "" && n.String() != c.net) {
t.Errorf("%q: %s %q, want %s %q", c.in, n, why, c.net, c.why)
}
}
}
// A household for the networks behind routers: two servers, one that keeps devices within circles.
const sites = `
config server 'home'
option subnet '10.77.0.0/24'
option mesh 'all'
config server 'cabin'
option subnet '10.78.0.0/24'
option mesh 'circles'
config circle 'fam'
option server 'cabin'
config person 'anna'
option server 'home'
option access 'both'
config person 'boris'
option server 'home'
option access 'lan'
config person 'clara'
option server 'home'
option access 'internet'
config person 'eva'
option server 'home'
option access 'lan'
list service 'nas'
config person 'hana'
option server 'cabin'
option access 'both'
option circle 'fam'
config person 'ivan'
option server 'cabin'
option access 'both'
option circle 'fam'
config person 'kira'
option server 'cabin'
option access 'both'
config device 'r1'
option person 'anna'
option kind 'router'
option address '10.77.0.2'
list behind_net '192.168.2.9/24'
list behind_net '10.77.0.128/25'
list behind_net 'the garage'
list behind_net '192.168.2.128/25'
list behind_net '192.168.50.0/24'
list behind_net '10.78.0.0/16'
config device 'phone'
option person 'anna'
option address '10.77.0.3'
list behind_net '192.168.9.0/24'
config device 'r2'
option person 'boris'
option kind 'router'
option address '10.77.0.4'
list behind_net '192.168.2.0/23'
list behind_net '172.16.9.0/24'
config device 'r3'
option person 'clara'
option kind 'router'
option address '10.77.0.5'
option behind_net '192.168.30.0/24'
config device 'r4'
option person 'eva'
option kind 'router'
option address '10.77.0.6'
list behind_net '192.168.40.0/24'
config device 'r5'
option person 'hana'
option kind 'router'
option address '10.78.0.2'
list behind_net '192.168.60.0/24'
config device 'iphone'
option person 'ivan'
option address '10.78.0.3'
config device 'kphone'
option person 'kira'
option address '10.78.0.4'
`
func household(t *testing.T, text string) *Model {
t.Helper()
c, err := uci.Parse(text)
if err != nil {
t.Fatal(err)
}
return Load(c, nil)
}
func TestNetworksInEffect(t *testing.T) {
m := household(t, sites)
// the router has 192.168.50.0/24 itself; a tunnel's subnet is never behind anyone; r1 came first
got := m.BehindOf([]string{"192.168.50.0/24", "192.168.77.0/24"})
want := map[string][]string{"r1": {"192.168.2.0/24"}, "r2": {"172.16.9.0/24"}, "r3": {"192.168.30.0/24"}, "r4": {"192.168.40.0/24"}, "r5": {"192.168.60.0/24"}}
if !reflect.DeepEqual(got, want) {
t.Errorf("in effect: %v, want %v", got, want)
}
// what the router has itself is not known: only the settings speak
if got := m.BehindOf(nil)["r1"]; !reflect.DeepEqual(got, []string{"192.168.2.0/24", "192.168.50.0/24"}) {
t.Errorf("r1 with nothing known of the router: %v", got)
}
// more than a router may have: the first BehindMost count
many := "config server 's'\n\toption subnet '10.77.0.0/24'\n\nconfig person 'p'\n\toption server 's'\n\nconfig device 'r'\n\toption person 'p'\n\toption kind 'router'\n"
for i := 0; i < BehindMost+3; i++ {
many += "\tlist behind_net '192.168." + string(rune('0'+i/10)) + string(rune('0'+i%10)) + ".0/24'\n"
}
if got := household(t, many).BehindOf(nil)["r"]; len(got) != BehindMost || got[BehindMost-1] != "192.168.7.0/24" {
t.Errorf("over the most: %v", got)
}
}
func TestWhatANetworkRunsInto(t *testing.T) {
m := household(t, sites)
own := []string{"192.168.50.0/24"}
for _, c := range []struct {
net, device string
want *Clash
}{
{"192.168.70.0/24", "", nil},
{"10.77.0.0/25", "", &Clash{Net: "10.77.0.0/24", Server: "home"}},
{"10.78.0.0/16", "", &Clash{Net: "10.78.0.0/24", Server: "cabin"}},
{"192.168.50.128/25", "", &Clash{Net: "192.168.50.0/24"}},
{"192.168.60.0/24", "", &Clash{Net: "192.168.60.0/24", Device: "r5"}},
// its own network is no clash
{"192.168.60.0/24", "r5", nil},
{"172.16.0.0/16", "r5", &Clash{Net: "172.16.9.0/24", Device: "r2"}},
} {
n, _ := ipnet.Any(c.net)
if got := m.BehindClash(n, c.device, own); !reflect.DeepEqual(got, c.want) {
t.Errorf("%s for %q: %+v, want %+v", c.net, c.device, got, c.want)
}
}
}
func TestWhoReachesWhatIsBehind(t *testing.T) {
m := household(t, sites)
behind := m.BehindOf([]string{"192.168.50.0/24"})
for _, c := range []struct {
person, device string
want []string
}{
// everyone reaches everyone on the server: every network of people who are part of home, but its own
{"anna", "phone", []string{"192.168.2.0/24", "172.16.9.0/24"}},
{"anna", "r1", []string{"172.16.9.0/24"}},
{"boris", "r2", []string{"192.168.2.0/24"}},
// the internet alone, or a few home services: no part of home
{"clara", "r3", nil},
{"eva", "r4", nil},
// within circles: the circle's router, and nobody else's
{"ivan", "iphone", []string{"192.168.60.0/24"}},
{"kira", "kphone", nil},
{"hana", "r5", nil},
} {
if got := m.BehindReach(m.People[c.person], c.device, behind); !reflect.DeepEqual(got, c.want) {
t.Errorf("%s's %s reaches %v, want %v", c.person, c.device, got, c.want)
}
}
m.Servers["home"].Mesh = "off"
if got := m.BehindReach(m.People["anna"], "phone", behind); got != nil {
t.Errorf("devices do not reach each other, yet anna's phone reaches %v", got)
}
}
// What a router's owner asked for is read with the router, means nothing on another kind of device, and is
// in effect nowhere while the router waits.
func TestRequestsAsWritten(t *testing.T) {
m := household(t, sites+`
config device 'r6'
option person 'kira'
option kind 'router'
option address '10.78.0.5'
option waiting '1'
option asked '1700000000'
list ask_net '192.168.61.0/24'
list behind_net '192.168.62.0/24'
config device 'r7'
option person 'kira'
option kind 'router'
option address '10.78.0.6'
option declined 'because'
list declined_net '192.168.63.0/24'
config device 'tablet'
option person 'kira'
option kind 'tablet'
option address '10.78.0.7'
option waiting '1'
option declined 'talk'
list ask_net '192.168.64.0/24'
`)
r6, r7, tablet := m.Devices["r6"], m.Devices["r7"], m.Devices["tablet"]
if !r6.Waiting || r6.Asked != 1700000000 || !reflect.DeepEqual(r6.Ask, []string{"192.168.61.0/24"}) || r6.Declined != "" {
t.Errorf("a router that waits: %+v", r6)
}
if r7.Waiting || r7.Declined != "plain" || !reflect.DeepEqual(r7.DeclinedNets, []string{"192.168.63.0/24"}) || r7.Asked != 0 {
t.Errorf("a network turned down for a reason nobody knows: %+v", r7)
}
if tablet.Waiting || tablet.Ask != nil || tablet.Declined != "" {
t.Errorf("a tablet asks for nothing: %+v", tablet)
}
if got := m.BehindOf(nil)["r6"]; got != nil {
t.Errorf("a waiting router's network is in effect: %v", got)
}
if m.Servers["home"].PortalRouters != "ask" || m.Servers["home"].PortalNets != "ask" {
t.Errorf("the rules a server starts with: %q, %q", m.Servers["home"].PortalRouters, m.Servers["home"].PortalNets)
}
set := household(t, "config server 's'\n\toption portal_routers 'free'\n\toption portal_nets 'free'\n\nconfig server 'o'\n\toption portal_routers 'off'\n\toption portal_nets 'never'\n")
if s, o := set.Servers["s"], set.Servers["o"]; s.PortalRouters != "free" || s.PortalNets != "free" || o.PortalRouters != "off" || o.PortalNets != "ask" {
t.Errorf("the rules as set: %+v, %+v", s, o)
}
}