package main import ( "testing" "time" ) func TestDevFromRouteGet(t *testing.T) { // authoritative kernel answer for the active egress out := "8.8.8.8 via 10.0.2.2 dev eth1 src 10.0.2.15 uid 0 \n cache " if devFromRouteGet(out) != "eth1" { t.Fatalf("got %q", devFromRouteGet(out)) } // a directly-connected dest: "1.2.3.4 dev wwan0 src ..." if devFromRouteGet("1.2.3.4 dev wwan0 src 1.2.3.5") != "wwan0" { t.Fatal("wwan0 not parsed") } if devFromRouteGet("unreachable 1.2.3.4") != "" { t.Fatal("expected empty for no dev") } } func TestProfileConditionsIface(t *testing.T) { p := Profile{Name: "sim", Enabled: true, MatchIface: []string{"wwan0", "usb0"}} now := at(t, "2026-07-09T12:00:00Z") if !profileConditionsHold(p, "wwan0", now) { t.Fatal("should hold on wwan0") } if profileConditionsHold(p, "eth0", now) { t.Fatal("should not hold on eth0") } } func TestProfileConditionsSchedule(t *testing.T) { p := Profile{Name: "night", Enabled: true, SchedTZ: "UTC", SchedStart: "22:00", SchedEnd: "23:00"} if !profileConditionsHold(p, "", at(t, "2026-07-09T22:30:00Z")) { t.Fatal("should hold at 22:30") } if profileConditionsHold(p, "", at(t, "2026-07-09T12:00:00Z")) { t.Fatal("should not hold at 12:00") } } func TestProfileConditionsAND(t *testing.T) { // iface AND schedule: both must hold p := Profile{Name: "x", Enabled: true, MatchIface: []string{"wwan0"}, SchedTZ: "UTC", SchedStart: "22:00", SchedEnd: "23:00"} if profileConditionsHold(p, "wwan0", at(t, "2026-07-09T12:00:00Z")) { t.Fatal("iface ok but out of window -> must not hold") } if profileConditionsHold(p, "eth0", at(t, "2026-07-09T22:30:00Z")) { t.Fatal("in window but wrong iface -> must not hold") } if !profileConditionsHold(p, "wwan0", at(t, "2026-07-09T22:30:00Z")) { t.Fatal("both hold -> must hold") } } func TestActiveWanProfilePriority(t *testing.T) { m := &Model{Globals: defaultGlobals()} m.Profiles = []Profile{ {Name: "lo", Enabled: true, Priority: 1, SchedTZ: "UTC", SchedStart: "00:00", SchedEnd: "23:59"}, {Name: "hi", Enabled: true, Priority: 9, SchedTZ: "UTC", SchedStart: "00:00", SchedEnd: "23:59"}, } p := activeWanProfile(m, at(t, "2026-07-09T12:00:00Z")) if p == nil || p.Name != "hi" { t.Fatalf("expected 'hi' by priority, got %v", p) } } func TestActiveWanProfileNoConditionsNeverActive(t *testing.T) { m := &Model{Globals: defaultGlobals()} m.Profiles = []Profile{{Name: "empty", Enabled: true}} if activeWanProfile(m, at(t, "2026-07-09T12:00:00Z")) != nil { t.Fatal("a profile with no conditions must never auto-activate") } } func TestProfileOverrideEnableDisable(t *testing.T) { saved := timeNow defer func() { timeNow = saved }() timeNow = func() time.Time { return at(t, "2026-07-09T22:30:00Z") } m := &Model{Globals: defaultGlobals()} m.Nodes = []Node{{Name: "n1", Enabled: true, URI: vlessReality}} m.Rules = []Rule{ {Name: "off-by-default", Enabled: false, Order: 10, DstDomain: []string{"ru.example"}, Target: "node:n1"}, {Name: "on-by-default", Enabled: true, Order: 11, DstDomain: []string{"keep.example"}, Target: "node:n1"}, } m.Profiles = []Profile{{ Name: "night", Enabled: true, SchedTZ: "UTC", SchedStart: "22:00", SchedEnd: "23:00", EnableRules: []string{"off-by-default"}, DisableRules: []string{"on-by-default"}, }} cfg, err := BuildConfig(m) if err != nil { t.Fatalf("BuildConfig: %v", err) } if ruleWithDomain(cfg, "ru.example") == nil { t.Fatal("profile should have enabled 'off-by-default'") } if ruleWithDomain(cfg, "keep.example") != nil { t.Fatal("profile should have disabled 'on-by-default'") } } func TestProfileDefaultTargetInjected(t *testing.T) { saved := timeNow defer func() { timeNow = saved }() timeNow = func() time.Time { return at(t, "2026-07-09T22:30:00Z") } m := &Model{Globals: defaultGlobals()} m.Nodes = []Node{{Name: "ru", Enabled: true, URI: vlessReality}} m.Profiles = []Profile{{ Name: "whitelist", Enabled: true, SchedTZ: "UTC", SchedStart: "22:00", SchedEnd: "23:00", DefaultTarget: "node:ru", }} cfg, err := BuildConfig(m) if err != nil { t.Fatalf("BuildConfig: %v", err) } if routingHasTarget(cfg, "node_ru") == nil { t.Fatal("profile default target should inject a catch-all to node_ru") } } func TestProfileInactiveNoOverride(t *testing.T) { saved := timeNow defer func() { timeNow = saved }() timeNow = func() time.Time { return at(t, "2026-07-09T12:00:00Z") } // outside window m := &Model{Globals: defaultGlobals()} m.Nodes = []Node{{Name: "n1", Enabled: true, URI: vlessReality}} m.Rules = []Rule{{Name: "off-by-default", Enabled: false, Order: 10, DstDomain: []string{"ru.example"}, Target: "node:n1"}} m.Profiles = []Profile{{ Name: "night", Enabled: true, SchedTZ: "UTC", SchedStart: "22:00", SchedEnd: "23:00", EnableRules: []string{"off-by-default"}, }} cfg, _ := BuildConfig(m) if ruleWithDomain(cfg, "ru.example") != nil { t.Fatal("inactive profile must not enable the rule") } }