Files
dawnline/backend/geo/geo_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

156 lines
6.8 KiB
Go

package geo
import (
"fmt"
"strings"
"sync"
"testing"
"time"
"github.com/tickcount/luci-app-dawnline/backend/usage"
)
const now = 1790000000
func place() Place {
return Place{"lat": 45.04, "lon": 38.98, "city": "Krasnodar", "country": "Russia", "cc": "RU", "asn": 12389.0, "org": "Rostelecom"}
}
func stamped(ts float64) Place { p := place(); p["ts"] = ts; return p }
// As tests/ucode/geo.test.uc holds the ucode module to.
func TestStaleAndMerge(t *testing.T) {
c := &Cache{IPs: map[string]Place{"37.78.36.76": stamped(now - 86400), "212.233.85.1": stamped(now - 31*86400), "185.135.150.35": {"fail": float64(now - 3600)},
"185.135.150.28": {"fail": float64(now - 2*86400)}, "83.239.111.148": {"lat": 1.0, "lon": 2.0, "city": "Old", "ts": float64(now)}}}
for _, k := range []struct {
ips []string
want string
}{{[]string{"37.78.36.76"}, ""}, {[]string{"212.233.85.1"}, "212.233.85.1"}, {[]string{"185.135.150.35"}, ""}, {[]string{"185.135.150.28"}, "185.135.150.28"},
{[]string{"83.239.111.148"}, "83.239.111.148"}, {[]string{"10.0.1.164", "100.64.1.1", "", "fe80::1"}, ""}, {[]string{"1.1.1.1", "8.8.8.8", "1.1.1.1"}, "1.1.1.1 8.8.8.8"}} {
if got := strings.Join(Stale(c, k.ips, now), " "); got != k.want {
t.Errorf("stale of %v: %q, not %q", k.ips, got, k.want)
}
}
c = &Cache{IPs: map[string]Place{}}
if !Merge(c, map[string]Place{"1.1.1.1": place(), "8.8.8.8": nil}, nil, now) {
t.Error("results changed nothing")
}
if c.IPs["1.1.1.1"]["ts"] != float64(now) || c.IPs["1.1.1.1"]["city"] != "Krasnodar" || len(c.IPs["8.8.8.8"]) != 1 || c.IPs["8.8.8.8"]["fail"] != float64(now) {
t.Errorf("after the merge: %v", c.IPs)
}
if Merge(c, nil, nil, now) || Merge(c, map[string]Place{"10.0.0.1": place()}, nil, now) || c.IPs["10.0.0.1"] != nil {
t.Error("nothing, or a local address, changed the cache")
}
if Label(c.IPs["1.1.1.1"]) != "Krasnodar · Rostelecom" || Label(c.IPs["8.8.8.8"]) != "" || Label(nil) != "" || Label(Place{"city": "X"}) != "X" {
t.Error("the labels")
}
// bounded: the oldest go, never what is still shown
c = &Cache{IPs: map[string]Place{}}
for i := 0; i < 500; i++ {
c.IPs[fmt.Sprintf("1.0.%d.%d", i/250, i%250+1)] = stamped(float64(now - 1000 + i))
}
Merge(c, map[string]Place{"9.9.9.9": place(), "9.9.9.10": place()}, []string{"1.0.0.1"}, now)
if len(c.IPs) != 500 || c.IPs["1.0.0.1"] == nil || c.IPs["1.0.0.2"] != nil || c.IPs["1.0.0.3"] != nil || c.IPs["1.0.0.4"] == nil || c.IPs["9.9.9.9"] == nil {
t.Errorf("the bound: %d entries", len(c.IPs))
}
for _, ip := range []string{"8.8.8.8", "2a00:1450::1"} {
if !IsPublic(ip) {
t.Errorf("%s is not public", ip)
}
}
for _, ip := range []string{"", "192.168.1.1", "172.20.0.1", "224.0.0.1", "fe80::1", "fd00::1", "::1", "nonsense"} {
if IsPublic(ip) {
t.Errorf("%s is public", ip)
}
}
}
func TestLookups(t *testing.T) {
var asked []string
get := func(url string) map[string]any {
asked = append(asked, url)
switch {
case strings.HasPrefix(url, "https://ipwho.is/1.1.1.1?"):
return map[string]any{"success": true, "ip": "1.1.1.1", "latitude": 45.04, "longitude": 38.98, "city": "Krasnodar", "country": "Russia", "country_code": "RU",
"connection": map[string]any{"asn": 12389.0, "isp": "Rostelecom", "org": "Other"}}
case url == "https://ipapi.co/8.8.8.8/json/":
return map[string]any{"ip": "8.8.8.8", "latitude": 37.4, "longitude": -122.0, "city": "Mountain View", "country_name": "United States", "country_code": "US", "asn": "AS15169", "org": "GOOGLE"}
case url == "https://ipapi.co/json/":
return map[string]any{"ip": "203.0.113.7", "latitude": 55.7, "longitude": 37.6, "city": "Moscow", "country_name": "Russia", "country_code": "RU", "asn": "AS1", "org": "ISP"}
case strings.HasPrefix(url, "https://ipwho.is/"):
return map[string]any{"success": false}
}
return nil
}
found := LookupAll(get, "ipwho.is", []string{"1.1.1.1", "8.8.8.8", "4.4.4.4", "5.5.5.5"}, 3, time.Minute)
if len(found) != 3 || found["1.1.1.1"]["org"] != "Rostelecom" || found["1.1.1.1"]["ip"] != nil || found["8.8.8.8"]["asn"] != 15169.0 || found["8.8.8.8"]["country"] != "United States" || found["4.4.4.4"] != nil {
t.Errorf("the lookups: %v", found)
}
asked = nil
Lookup(get, "ipapi.co", "1.1.1.1")
if len(asked) != 2 || !strings.HasPrefix(asked[0], "https://ipapi.co/") {
t.Errorf("the chosen provider is not asked first: %v", asked)
}
// the router's own place: behind NAT the service is asked who we are; kept six hours, or until the address moves
c := &Cache{IPs: map[string]Place{}}
if !ResolveHome(get, "ipwho.is", c, "192.168.1.2", now) || c.Home["city"] != "Moscow" || c.Home["wan"] != "192.168.1.2" {
t.Errorf("home: %v", c.Home)
}
if ResolveHome(get, "ipwho.is", c, "192.168.1.2", now+3600) || !ResolveHome(get, "ipwho.is", c, "192.168.1.2", now+7*3600) {
t.Error("home is asked too often, or never again")
}
c.Home["wan"] = "1.1.1.1"
if ResolveHome(get, "ipwho.is", c, "1.1.1.1", now+7*3600+60) || !ResolveHome(get, "ipwho.is", c, "8.8.8.8", now+7*3600+120) || c.Home["city"] != "Mountain View" {
t.Errorf("home after the address moved: %v", c.Home)
}
}
func TestLocator(t *testing.T) {
dir := t.TempDir()
u := usage.Empty()
u.Devices["d1"] = &usage.Record{IPs: map[string]float64{"1.1.1.1": 1, "10.0.0.9": 1}}
u.Devices["d2"] = &usage.Record{IPs: map[string]float64{"2.2.2.2": 1, "3.3.3.3": 1, "4.4.4.4": 1}}
var mu sync.Mutex
asked := 0
var where map[string]string
var devices []string
done := make(chan bool, 8)
l := &Locator{Store: Store{Run: dir + "/run", Data: dir + "/data"}, Usage: func() *usage.Usage { return u },
Get: func(url string) map[string]any {
mu.Lock()
asked++
mu.Unlock()
if strings.Contains(url, "1.1.1.1") {
return map[string]any{"success": true, "latitude": 1.0, "longitude": 2.0, "city": "Krasnodar", "connection": map[string]any{"asn": 1.0, "isp": "ISP"}}
}
return nil
},
Found: func(w map[string]string, d []string) { where, devices = w, d; done <- true }}
l.Ask("ipwho.is", []string{"1.1.1.1"}, nil)
select {
case <-done:
case <-time.After(5 * time.Second):
t.Fatal("nothing was found")
}
if where["1.1.1.1"] != "Krasnodar · ISP" || len(devices) != 1 || devices[0] != "d1" {
t.Errorf("found %v for %v", where, devices)
}
if c := l.Store.Load(); c.IPs["1.1.1.1"]["city"] != "Krasnodar" {
t.Errorf("the cache: %v", c.IPs)
}
// the minute: what is shown and unknown, three a run; what is known is not asked again
mu.Lock()
asked = 0
mu.Unlock()
l.Minute(true, "ipwho.is", "")
time.Sleep(300 * time.Millisecond)
c := l.Store.Load()
if c.IPs["1.1.1.1"]["city"] != "Krasnodar" || c.IPs["2.2.2.2"]["fail"] == nil || c.IPs["3.3.3.3"]["fail"] == nil || c.IPs["4.4.4.4"]["fail"] == nil || c.IPs["10.0.0.9"] != nil {
t.Errorf("after the minute: %v", c.IPs)
}
if !l.Store.Flush() || l.Store.Flush() {
t.Error("the cache is not put on flash once")
}
l.Minute(false, "ipwho.is", "")
}