fix(apply): chain: as a subscription fetch detour resolved to a name nothing answers to

`fetch_detour=chain:<X>` never worked. engine.ViaToTag maps "chain:X" to the
bare tag "X", but the generator materialises a chain as one wrapper per hop —
chain-<X>-h1..chain-<X>-hN — and routes into the LAST one. The lookup missed and
the update failed with "unknown outbound tag".

It failed CLOSED, so the feed was never pulled over the plain WAN by this path.
But the miss had a sharp edge: when a node or group happened to share the
chain's name, the lookup HIT it, and the subscription was fetched through a
completely different outbound with nothing said.

Applier.HTTPClient now resolves chain: before the engine sees it, against the
tags the RUNNING box actually holds (outbounds unioned with endpoints — a WG hop
is an endpoint and Outbounds() does not list those), mirroring the generator:
the highest-indexed chain-<X>-h<i> wrapper is the entry, and a chain that
flattens to one hop IS that hop. Every other via form is passed through
untouched.

The case the generator cannot serve is named rather than papered over: chains
are built lazily, only for a chain some enabled rule/egress/DNS detour targets,
and a fetch detour is not one of those references — so a chain nothing else
points at has no outbounds at all. That, and every other miss, is an explicit
refusal wrapping engine.ErrOutboundUnknown (the panel already maps it to 400).
Never a fall back to direct: that would put the feed and the owner's real
address on the plain WAN, which is the thing fetch_via=proxy is set to avoid.

Tests are mutation-checked. Pre-fix behaviour resolves "work"/"solo" and kills
every chain case; first-hop-instead-of-last, member-copies-count-as-hops,
dropped pass-through, and a silent direct fallback each kill their own test.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BHw89tdWddzhjUc4bAH4tS
This commit is contained in:
omarandClaude Opus 5 committed 2026-07-26 23:09:35 +03:00
1 parent add90b5b2f
commit 564033cd10
2 files changed
+617 -2

No files matched your search

+251 -2
View File
@@ -297,20 +297,269 @@ func chainNamesFromModel() []string {
// HTTPClient returns an http.Client that dials THROUGH the running engine's
// outbound named by `via` (feedback #1/#8). It delegates to the engine; a stopped
// engine yields engine.ErrEngineStopped and an unknown tag engine.ErrOutboundUnknown.
//
// `chain:<name>` is resolved HERE, before the engine sees it, because a chain has
// no outbound named after itself — see resolveVia.
func (a *Applier) HTTPClient(via string) (*http.Client, error) {
if a.eng == nil {
return nil, engine.ErrEngineStopped
}
via, err := a.resolveVia(via)
if err != nil {
return nil, err
}
return a.eng.HTTPClient(via)
}
// chainViaPrefix is the `via`/detour selector that names a multi-hop chain.
const chainViaPrefix = "chain:"
// resolveVia rewrites a `chain:<name>` selector into the outbound tag the RUNNING
// box actually carries for that chain; every other form is passed through
// untouched for engine.ViaToTag to handle.
//
// # Why this exists
//
// engine.ViaToTag maps "chain:X" to the bare tag "X", and there is no outbound
// called "X": the generator materialises a chain as one wrapper per hop, tagged
// chain-<X>-h1..chain-<X>-hN (see shater/generate/chain.go), and routes into the
// LAST one. So `fetch_detour=chain:X` looked "X" up in the OutboundManager, missed,
// and failed with ErrOutboundUnknown — the feature never worked. Worse than a plain
// miss: when a NODE OR GROUP happens to share the chain's name, the lookup HITS it
// and the subscription is fetched through a completely different outbound, silently.
//
// The resolution mirrors the generator, including the case the generator cannot
// serve at all: chains are built LAZILY, only for a chain some enabled rule/egress/
// DNS detour targets, and a fetch detour is not one of those references. A chain
// nothing else points at therefore has no outbounds in the box, and the honest
// answer is a named refusal — never a quiet fall back to direct, which would pull
// the feed over the plain WAN with the owner's real address, the exact thing
// fetch_via=proxy exists to prevent.
//
// A stopped engine is reported as engine.ErrEngineStopped; every resolution failure
// wraps engine.ErrOutboundUnknown, which the panel already maps to 400.
func (a *Applier) resolveVia(via string) (string, error) {
if _, ok := cutPrefixFold(strings.TrimSpace(via), chainViaPrefix); !ok {
return via, nil
}
tags := a.runningTags()
if tags == nil {
return "", engine.ErrEngineStopped
}
// The model is only needed for the single-hop case below, so it is read lazily
// and a failed read (off-router, no `uci`) is not fatal: chainOutboundTag then
// sees no chain definitions and says so, which is the truthful answer.
var chains []model.Chain
if m, err := model.ReadUCI(); err == nil && m != nil {
chains = m.Chains
}
return resolveViaTag(via, tags, chains)
}
// resolveViaTag is resolveVia's pure core: no engine, no UCI, just the running
// box's tag set and the model's chain definitions. Split out so every branch is
// reachable from a unit test without a box.
func resolveViaTag(via string, tags map[string]bool, chains []model.Chain) (string, error) {
name, ok := cutPrefixFold(strings.TrimSpace(via), chainViaPrefix)
if !ok {
return via, nil
}
return chainOutboundTag(name, tags, chains, map[string]bool{})
}
// chainOutboundTag resolves a chain NAME to the tag the running box carries for it.
//
// Two shapes exist, both produced by shater/generate/chain.go:
//
// 1. Every chain with more than one hop (or with an `egress:` entry hop) is built
// as wrappers chain-<name>-h1..chain-<name>-hN; the LAST one is the entry the
// traffic is routed into. Member copies of a group hop are tagged
// chain-<name>-h<i>-<member> and are NOT hops — the digits check rejects them.
// 2. A chain that flattens to a single real hop is not wrapped at all: it IS that
// hop, and resolves to the hop's own tag.
//
// Anything else is refused by name. The kind switch is a closed, positive list on
// purpose: an unlisted spelling must land in a refusal the operator can read, not
// in a lookup that might accidentally hit an unrelated outbound.
func chainOutboundTag(name string, tags map[string]bool, chains []model.Chain, seen map[string]bool) (string, error) {
name = strings.TrimSpace(name)
if name == "" {
return "", fmt.Errorf("%w: detour %q names no chain", engine.ErrOutboundUnknown, chainViaPrefix)
}
if seen[name] {
return "", fmt.Errorf("%w: chain %q is part of a reference cycle", engine.ErrOutboundUnknown, name)
}
seen[name] = true
// (1) Hop wrappers in the running box: the highest hop index is the entry.
if tag, ok := chainEntryTag(name, tags); ok {
return tag, nil
}
// (2) No wrappers. Either the chain collapses to one hop, or it was never built.
def := findChainDef(chains, name)
if def == nil {
return "", fmt.Errorf("%w: no chain named %q is defined", engine.ErrOutboundUnknown, name)
}
hops := make([]string, 0, len(def.Hops))
for _, h := range def.Hops {
if h = strings.TrimSpace(h); h != "" {
hops = append(hops, h)
}
}
if len(hops) == 0 {
return "", fmt.Errorf("%w: chain %q has no hops", engine.ErrOutboundUnknown, name)
}
if len(hops) > 1 {
return "", errChainNotBuilt(name)
}
hop := hops[0]
kind, base := model.SplitTarget(hop)
switch strings.ToLower(kind) {
case "chain":
// A chain whose only hop is another chain IS that chain (the generator
// splices sub-chains in place before wrapping anything).
return chainOutboundTag(base, tags, chains, seen)
case "node", "group":
if tags[base] {
return base, nil
}
return "", errChainHopMissing(name, hop)
case "egress":
// An egress hop is a chain's ENTRY interface, never its exit. Alone it leaves
// nothing to route into, which is what the generator reports as "no hops".
return "", fmt.Errorf("%w: chain %q has only the egress entry hop %q and no exit to fetch through",
engine.ErrOutboundUnknown, name, hop)
case "direct", "block":
// The generator's single-hop shortcut would alias these to the terminal
// route target. We refuse instead, and the divergence is deliberate: this is
// a FETCH detour, and "fetch the subscription through direct" is the
// un-tunnelled fetch that fetch_via=proxy was set to avoid. A named refusal
// is the recoverable side of the mistake; a silent direct fetch is not.
// (The generator itself rejects direct/block in every multi-hop chain.)
return "", fmt.Errorf("%w: chain %q's only hop is %q, a terminal route target and not a tunnel — set fetch_via=direct if an un-tunnelled fetch is what you want",
engine.ErrOutboundUnknown, name, hop)
default:
// Bare name: model.SplitTarget puts a colon-less hop in KIND, so the hop
// text itself is the node/group name (same rule buildHopWrapper follows).
if tags[hop] {
return hop, nil
}
return "", errChainHopMissing(name, hop)
}
}
// errChainNotBuilt names the case that has no fix at this layer: the chain is
// configured, but nothing the generator walks refers to it, so the box was built
// without it. Said in full because the operator otherwise sees a correct-looking
// config and an "unknown outbound" with no way to connect the two.
func errChainNotBuilt(name string) error {
return fmt.Errorf("%w: chain %q is defined but the running engine holds no outbound for it — a chain is only built when an enabled rule, egress or DNS detour targets it, and a subscription fetch detour is not one of those; point a rule at %q, or fetch through node:/group:/egress: instead",
engine.ErrOutboundUnknown, name, name)
}
// errChainHopMissing reports a single-hop chain whose hop is not in the box (a
// disabled/unparseable node, a group with no usable members, a typo).
func errChainHopMissing(name, hop string) error {
return fmt.Errorf("%w: chain %q is the single hop %q, which the running engine holds no outbound for",
engine.ErrOutboundUnknown, name, hop)
}
// chainEntryTag finds the chain's ENTRY wrapper in a tag set: the tag
// "chain-<name>-h<i>" with the largest all-digit i. This is the same tag grammar
// engine/grouphealth.go and engine/grouptest.go project chains out of the running
// pool with; a member copy ("chain-<name>-h<i>-<member>") fails the digits test and
// is skipped, as is another chain that merely shares the prefix.
func chainEntryTag(name string, tags map[string]bool) (string, bool) {
prefix := "chain-" + name + "-h"
best, bestTag := -1, ""
for tag := range tags {
rest, ok := strings.CutPrefix(tag, prefix)
if !ok {
continue
}
idx, ok := allDigits(rest)
if !ok {
continue
}
if idx > best {
best, bestTag = idx, tag
}
}
return bestTag, best >= 0
}
// allDigits parses a non-empty all-digit string.
func allDigits(s string) (int, bool) {
if s == "" {
return 0, false
}
n := 0
for _, c := range s {
if c < '0' || c > '9' {
return 0, false
}
n = n*10 + int(c-'0')
}
return n, true
}
// findChainDef returns the model.Chain named name, or nil.
func findChainDef(chains []model.Chain, name string) *model.Chain {
for i := range chains {
if strings.TrimSpace(chains[i].Name) == name {
return &chains[i]
}
}
return nil
}
// cutPrefixFold is strings.CutPrefix with a case-insensitive prefix match, so
// "CHAIN:x" is the selector "chain:x" — engine.ViaToTag accepts the same spellings.
func cutPrefixFold(s, prefix string) (string, bool) {
if len(s) >= len(prefix) && strings.EqualFold(s[:len(prefix)], prefix) {
return s[len(prefix):], true
}
return "", false
}
// runningTags is the set of outbound tags the RUNNING box can dial. Outbounds and
// endpoints are UNIONED: a WG/AWG hop is an endpoint, and OutboundManager.Outbounds()
// does not list those even though its Outbound(tag) lookup falls through to the
// endpoint manager — so both are dialable and both must be visible here. nil (not
// empty) when no box is running, which the caller reports as ErrEngineStopped.
func (a *Applier) runningTags() map[string]bool {
inst := a.eng.Instance()
if inst == nil {
return nil
}
om := inst.Outbound()
if om == nil {
return nil
}
tags := make(map[string]bool)
for _, ob := range om.Outbounds() {
tags[ob.Tag()] = true
}
if em := inst.Endpoint(); em != nil {
for _, ep := range em.Endpoints() {
tags[ep.Tag()] = true
}
}
return tags
}
// UpdateSubscription fetches the named subscription, folds the parsed nodes into
// the model (replacing exactly that sub's cache), persists them to the sub's
// JSON cache file (model.SaveSubCache) + the userinfo counters to UCI, and
// reconciles so the running engine picks them up (feedback #8). When the
// subscription's FetchVia=="proxy" the fetch is routed THROUGH the engine
// outbound named by its FetchDetour (group:/node:/egress:/direct); otherwise the
// fetch is DIRECT. Zero-node safety is inherited from subscribe.UpdateSubscription
// outbound named by its FetchDetour (group:/node:/egress:/chain:/direct); a detour
// that cannot be resolved FAILS the update rather than falling back — a silent
// direct fetch would put the feed, and the owner's real address, on the plain WAN.
// With FetchVia!="proxy" the fetch is DIRECT, as configured.
// Zero-node safety is inherited from subscribe.UpdateSubscription
// (a bad body leaves the cache untouched); nothing is written on a failed parse.
// Returns the node count now cached.
//
+366
View File
@@ -0,0 +1,366 @@
package apply
import (
"errors"
"strings"
"testing"
"github.com/sagernet/sing-box/shater/engine"
"github.com/sagernet/sing-box/shater/generate"
"github.com/sagernet/sing-box/shater/model"
)
// A subscription's fetch_detour is resolved by Applier.HTTPClient, and `chain:<X>`
// is the one form the engine cannot resolve on its own: a chain has no outbound
// named after itself. These tests pin the resolution against the tag grammar the
// GENERATOR actually emits, and pin what happens on every miss — the answer must
// never be "fetch it direct", because that puts the feed and the router's real
// address on the plain WAN, which is what fetch_via=proxy exists to prevent.
// tagSet builds a running-box tag set for the pure resolver.
func tagSet(tags ...string) map[string]bool {
out := make(map[string]bool, len(tags))
for _, t := range tags {
out[t] = true
}
return out
}
// ssURI is a parseable share link so generate can build a real outbound from it.
func ssURI(host, name string) string {
return "ss://YWVzLTI1Ni1nY206cGFzcw@" + host + ":8388#" + name
}
// TestChainDetourTagMatchesGenerator is the tripwire: it runs the REAL generator
// over a model whose rule targets a chain, and requires that the tag resolveViaTag
// hands the engine is one the generator actually emitted — and specifically the
// chain's entry (highest hop index), not the chain's own name.
//
// This is the test that fails on the unfixed code: engine.ViaToTag("chain:work")
// yields "work", which appears nowhere in the emitted config.
func TestChainDetourTagMatchesGenerator(t *testing.T) {
m := &model.Model{
Globals: model.Globals{Enabled: true},
Nodes: []model.Node{
{Name: "n1", Enabled: true, URI: ssURI("1.2.3.4", "n1")},
{Name: "n2", Enabled: true, URI: ssURI("5.6.7.8", "n2")},
},
Chains: []model.Chain{{Name: "work", Hops: []string{"node:n1", "node:n2"}}},
Rules: []model.Rule{
{Name: "r", Enabled: true, Target: "chain:work", DstPort: "443"},
},
}
opts, _, err := generate.GenerateWithWarnings(m)
if err != nil {
t.Fatalf("generate: %v", err)
}
emitted := map[string]bool{}
for _, ob := range opts.Outbounds {
emitted[ob.Tag] = true
}
for _, ep := range opts.Endpoints {
emitted[ep.Tag] = true
}
if !emitted["chain-work-h2"] {
t.Fatalf("fixture broken: generator emitted no chain-work-h2; tags=%v", emitted)
}
got, err := resolveViaTag("chain:work", emitted, m.Chains)
if err != nil {
t.Fatalf("resolveViaTag(chain:work): %v", err)
}
if !emitted[got] {
t.Fatalf("resolved %q, which the generator never emitted (tags=%v)", got, emitted)
}
if got != "chain-work-h2" {
t.Fatalf("resolved %q, want the chain ENTRY chain-work-h2", got)
}
}
// TestChainDetourEntryIsTheLastHop pins entry = highest hop index, and that a group
// hop's member copies (chain-<n>-h<i>-<member>) are not mistaken for hops. Dialling
// a member copy instead of its wrapper is not a harmless near-miss: the wrapper IS
// the group's selector, so the copy pins one member and throws the balancing away.
func TestChainDetourEntryIsTheLastHop(t *testing.T) {
cases := []struct {
name string
tags map[string]bool
want string
}{
{
name: "highest index wins, 10 beats 2",
tags: tagSet(
"direct", "block", "n1", "n2", "n3",
"chain-work-h1", "chain-work-h1-n1",
"chain-work-h2", "chain-work-h2-n2", "chain-work-h2-n3",
"chain-work-h10", "chain-work-h10-n3",
"chain-workshop-h1", // another chain sharing the prefix
),
want: "chain-work-h10",
},
{
// The digits check is what makes this case come out right: every tag here
// starts with "chain-work-h", and only one of them is a hop.
name: "a lone group hop resolves to the WRAPPER, never to a member copy",
tags: tagSet(
"direct", "n1", "n2",
"chain-work-h1",
"chain-work-h1-n1", "chain-work-h1-n2", "chain-work-h1-zzz",
),
want: "chain-work-h1",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got, err := resolveViaTag("chain:work", tc.tags, nil)
if err != nil {
t.Fatalf("resolveViaTag: %v", err)
}
if got != tc.want {
t.Fatalf("entry = %q, want %q", got, tc.want)
}
})
}
}
// TestChainDetourNotBuiltRefusesInsteadOfDirect is the leak guard. A chain nothing
// else references is never materialised, so no wrapper exists. The resolver must
// say so by name — and must NOT resolve to "direct" or to a node/group that merely
// shares the chain's name.
func TestChainDetourNotBuiltRefusesInsteadOfDirect(t *testing.T) {
// "work" is ALSO a group tag here: the trap the unfixed code fell into, where
// engine.ViaToTag("chain:work") -> "work" hits an unrelated outbound.
tags := tagSet("direct", "block", "n1", "n2", "work")
chains := []model.Chain{{Name: "work", Hops: []string{"node:n1", "node:n2"}}}
got, err := resolveViaTag("chain:work", tags, chains)
if err == nil {
t.Fatalf("unbuilt chain resolved to %q, want a refusal", got)
}
if got != "" {
t.Fatalf("refusal also returned a tag %q; a caller could dial it", got)
}
if !errors.Is(err, engine.ErrOutboundUnknown) {
t.Fatalf("err = %v, want it to wrap engine.ErrOutboundUnknown (panel maps that to 400)", err)
}
for _, want := range []string{"chain \"work\"", "only built when an enabled rule"} {
if !strings.Contains(err.Error(), want) {
t.Fatalf("err %q does not explain the cause (missing %q)", err, want)
}
}
}
// TestChainDetourUndefinedIsNamed: a fetch_detour pointing at a chain that does not
// exist at all must say exactly that.
func TestChainDetourUndefinedIsNamed(t *testing.T) {
got, err := resolveViaTag("chain:ghost", tagSet("direct", "ghost"), nil)
if err == nil {
t.Fatalf("undefined chain resolved to %q, want a refusal", got)
}
if !errors.Is(err, engine.ErrOutboundUnknown) {
t.Fatalf("err = %v, want engine.ErrOutboundUnknown", err)
}
if !strings.Contains(err.Error(), "no chain named \"ghost\"") {
t.Fatalf("err %q does not name the missing chain", err)
}
}
// TestChainDetourSingleHop covers the shape the generator does NOT wrap: a chain
// that flattens to one hop IS that hop.
func TestChainDetourSingleHop(t *testing.T) {
tags := tagSet("direct", "block", "n1", "grp")
cases := []struct {
name string
chains []model.Chain
via string
want string
}{
{
name: "node hop",
chains: []model.Chain{{Name: "solo", Hops: []string{"node:n1"}}},
via: "chain:solo",
want: "n1",
},
{
name: "group hop",
chains: []model.Chain{{Name: "solo", Hops: []string{"group:grp"}}},
via: "chain:solo",
want: "grp",
},
{
name: "bare hop",
chains: []model.Chain{{Name: "solo", Hops: []string{"n1"}}},
via: "chain:solo",
want: "n1",
},
{
name: "sub-chain hop is spliced",
chains: []model.Chain{
{Name: "outer", Hops: []string{"chain:inner"}},
{Name: "inner", Hops: []string{"node:n1"}},
},
via: "chain:outer",
want: "n1",
},
{
name: "blank hops are ignored",
chains: []model.Chain{{Name: "solo", Hops: []string{"", "node:n1", " "}}},
via: "chain:solo",
want: "n1",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got, err := resolveViaTag(tc.via, tags, tc.chains)
if err != nil {
t.Fatalf("resolveViaTag(%q): %v", tc.via, err)
}
if got != tc.want {
t.Fatalf("resolveViaTag(%q) = %q, want %q", tc.via, got, tc.want)
}
})
}
}
// TestChainDetourRefusals pins every remaining miss as a NAMED refusal with no tag,
// so none of them can degrade into a fetch through the wrong outbound.
func TestChainDetourRefusals(t *testing.T) {
tags := tagSet("direct", "block", "n1", "egress-wg0")
cases := []struct {
name string
chains []model.Chain
via string
wantMsg string
}{
{
name: "hop not in the running box",
chains: []model.Chain{{Name: "solo", Hops: []string{"node:gone"}}},
via: "chain:solo",
wantMsg: "holds no outbound for",
},
{
name: "egress entry hop alone has no exit",
chains: []model.Chain{{Name: "solo", Hops: []string{"egress:wg0"}}},
via: "chain:solo",
wantMsg: "no exit to fetch through",
},
{
name: "direct is not a tunnel hop",
chains: []model.Chain{{Name: "solo", Hops: []string{"direct"}}},
via: "chain:solo",
wantMsg: "terminal route target",
},
{
name: "block is not a tunnel hop",
chains: []model.Chain{{Name: "solo", Hops: []string{"block"}}},
via: "chain:solo",
wantMsg: "terminal route target",
},
{
name: "chain with no hops",
chains: []model.Chain{{Name: "solo", Hops: []string{" "}}},
via: "chain:solo",
wantMsg: "has no hops",
},
{
name: "chain: with no name",
chains: nil,
via: "chain:",
wantMsg: "names no chain",
},
{
name: "self-referencing chain terminates",
chains: []model.Chain{{Name: "loop", Hops: []string{"chain:loop"}}},
via: "chain:loop",
wantMsg: "reference cycle",
},
{
name: "mutual cycle terminates",
chains: []model.Chain{
{Name: "a", Hops: []string{"chain:b"}},
{Name: "b", Hops: []string{"chain:a"}},
},
via: "chain:a",
wantMsg: "reference cycle",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got, err := resolveViaTag(tc.via, tags, tc.chains)
if err == nil {
t.Fatalf("resolveViaTag(%q) = %q, want a refusal", tc.via, got)
}
if got != "" {
t.Fatalf("refusal also returned tag %q", got)
}
if got == "direct" {
t.Fatalf("refusal degraded to direct — that is a WAN leak")
}
if !errors.Is(err, engine.ErrOutboundUnknown) {
t.Fatalf("err = %v, want engine.ErrOutboundUnknown", err)
}
if !strings.Contains(err.Error(), tc.wantMsg) {
t.Fatalf("err %q does not contain %q", err, tc.wantMsg)
}
})
}
}
// TestChainDetourCaseInsensitive: engine.ViaToTag accepts "CHAIN:x"; so must this,
// or a spelling the engine would have honoured falls through as a bare tag.
func TestChainDetourCaseInsensitive(t *testing.T) {
tags := tagSet("chain-work-h1", "chain-work-h2")
for _, via := range []string{"chain:work", "Chain:work", "CHAIN:work", " chain:work "} {
got, err := resolveViaTag(via, tags, nil)
if err != nil {
t.Fatalf("resolveViaTag(%q): %v", via, err)
}
if got != "chain-work-h2" {
t.Fatalf("resolveViaTag(%q) = %q, want chain-work-h2", via, got)
}
}
}
// TestNonChainDetoursUnchanged is the regression guard: every other fetch_detour
// form must reach engine.ViaToTag byte-for-byte as configured, so group:/node:/
// egress:/direct/bare keep behaving exactly as before this resolver existed.
func TestNonChainDetoursUnchanged(t *testing.T) {
// A tag set that WOULD satisfy a chain lookup, to catch a resolver that starts
// treating everything as a chain name.
tags := tagSet("direct", "auto", "n1", "egress-wg0", "chain-auto-h1", "chain-n1-h1")
chains := []model.Chain{{Name: "auto", Hops: []string{"node:n1"}}}
for _, via := range []string{"", "direct", "DIRECT", "group:auto", "node:n1", "egress:wg0", "n1", " group:auto "} {
got, err := resolveViaTag(via, tags, chains)
if err != nil {
t.Fatalf("resolveViaTag(%q): %v", via, err)
}
if got != via {
t.Fatalf("resolveViaTag(%q) = %q, want it passed through unchanged for engine.ViaToTag", via, got)
}
}
}
// TestHTTPClientChainOnStoppedEngine: with no box running there are no tags to
// resolve against, and the caller must hear "engine stopped" — not a confusing
// claim about the chain, and not a client dialling anything.
func TestHTTPClientChainOnStoppedEngine(t *testing.T) {
a := New(engine.New(), nil)
for _, via := range []string{"chain:work", "group:auto", "direct", ""} {
c, err := a.HTTPClient(via)
if !errors.Is(err, engine.ErrEngineStopped) {
t.Fatalf("HTTPClient(%q): err = %v, want engine.ErrEngineStopped", via, err)
}
if c != nil {
t.Fatalf("HTTPClient(%q) returned a client %v on a stopped engine", via, c)
}
}
}
// TestHTTPClientNilEngine keeps the pre-existing nil-engine contract.
func TestHTTPClientNilEngine(t *testing.T) {
a := &Applier{}
if _, err := a.HTTPClient("chain:work"); !errors.Is(err, engine.ErrEngineStopped) {
t.Fatalf("err = %v, want engine.ErrEngineStopped", err)
}
}