Documentation
¶
Overview ¶
Package sysnetdebug provides a channel-driven sysnet implementation for tests.
Index ¶
- Variables
- type System
- func (s *System) AddTunAddr(t tun.Tun, addr string) error
- func (s *System) AddTunRoute(t tun.Tun, route string) error
- func (s *System) AllocIP() subnet.IPAllocator
- func (s *System) AllocSubnet() subnet.SubnetAllocator
- func (s *System) BuildDefaultTun(opts sysnet.DefaultTunOpts) (sysnet.DefaultTun, error)
- func (s *System) BuildMatcher(rule sysnet.Rule) (sysnet.Matcher, error)
- func (s *System) BuildTun(opts sysnet.TunOpts) (tun.Tun, error)
- func (s *System) Capabilities() sysnet.CapabilityReport
- func (s *System) CapabilitiesForTun(t tun.Tun) (sysnet.TunCapabilityReport, error)
- func (s *System) CheckDefaultTunOpts(opts sysnet.DefaultTunOpts) sysnet.ValidationReport
- func (s *System) CheckRule(rule sysnet.Rule, context sysnet.RuleContext) sysnet.ValidationReport
- func (s *System) CheckTunOpts(opts sysnet.TunOpts) sysnet.ValidationReport
- func (s *System) Close() error
- func (s *System) CompleteRule(rule sysnet.Rule, context sysnet.RuleContext) ([]string, error)
- func (s *System) DefaultTunWarnings(t sysnet.DefaultTun) []sysnet.Warning
- func (s *System) GetDefaultTunPeer() (TunEntry, bool)
- func (s *System) GetTunAddrs(t tun.Tun) ([]string, error)
- func (s *System) GetTunPeer(name string) (TunEntry, bool)
- func (s *System) GetTunRoutes(t tun.Tun) ([]string, error)
- func (s *System) LocalNet() gonnect.Network
- func (s *System) OutDNS() dns.Interface
- func (s *System) OutNet() gonnect.Network
- func (s *System) Requests() chan<- dns.Request
- func (s *System) SetCapabilityReport(report sysnet.CapabilityReport) error
- func (s *System) SetTunAddrs(t tun.Tun, addrs []string) error
- func (s *System) SetTunCapabilityReport(t tun.Tun, report sysnet.TunCapabilityReport) error
- func (s *System) SetTunMTU(t tun.Tun, mtu int) error
- func (s *System) SetTunName(t tun.Tun, name string) error
- func (s *System) SetTunRoutes(t tun.Tun, routes []string) error
- func (s *System) TunWarnings(t tun.Tun) []sysnet.Warning
- func (s *System) UseDefaultCapabilityReport()
- type TunConfig
- type TunEntry
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultRules lists the process-owner rule types commonly exposed by // sysnet implementations. DefaultRules = []sysnet.RuleCapability{ { Type: "comm", Description: "Process command regexp matcher.", ValueKind: sysnet.RuleValueRegex, SemanticsID: "debug.command-regexp.v1", }, { Type: "exec", Description: "Process executable path matcher.", ValueKind: sysnet.RuleValuePath, SemanticsID: "debug.executable-path.v1", }, { Type: "cmd", Description: "Process command line regexp matcher.", ValueKind: sysnet.RuleValueRegex, SemanticsID: "debug.command-line-regexp.v1", }, { Type: "pid", Description: "Process PID.", ValueKind: sysnet.RuleValuePID, SemanticsID: "debug.pid.v1", }, { Type: "user", Description: "Name of user owning process.", ValueKind: sysnet.RuleValueName, SemanticsID: "debug.user-name.v1", }, { Type: "uid", Description: "UID owning process.", ValueKind: sysnet.RuleValueOpaque, SemanticsID: "debug.uid.v1", }, { Type: "group", Description: "Name of group owning process.", ValueKind: sysnet.RuleValueName, SemanticsID: "debug.group-name.v1", }, { Type: "gid", Description: "GID owning process.", ValueKind: sysnet.RuleValueOpaque, SemanticsID: "debug.gid.v1", }, } )
Functions ¶
This section is empty.
Types ¶
type System ¶
type System struct {
// DisableTun makes regular TUN creation unsupported.
DisableTun bool
// DisableDefaultTun makes default TUN creation unsupported.
DisableDefaultTun bool
// DisableDynTun makes dynamic regular TUN operations unsupported. Setter and
// getter methods return sysnet.ErrNotSupported through a validation error.
DisableDynTun bool
// DisableDynDefaultTun makes dynamic default-TUN operations unsupported.
DisableDynDefaultTun bool
// DisableTunNames makes creation-time names and rename unsupported for
// regular TUN devices.
DisableTunNames bool
// DisableDefaultTunNames makes creation-time names and rename unsupported for
// default TUN devices.
DisableDefaultTunNames bool
// DisableStrictMode makes strict routing profiles unsupported.
DisableStrictMode bool
// DisableDefaultTunSourceRoutes makes default-TUN source routes unsupported.
DisableDefaultTunSourceRoutes bool
// Rules replaces the default rule catalog. Leave it nil to use DefaultRules.
// Configure this field before the System is first used.
Rules []sysnet.RuleCapability
// RuleMatcher is an optional hook used by matchers created with
// BuildMatcher. When omitted, matchers return false, nil.
RuleMatcher func(rule sysnet.Rule, flow sockowner.FlowTuple) (bool, error)
// CheckRuleHook adds context-aware validation issues after capability
// checks. Configure it before concurrent use.
CheckRuleHook func(sysnet.Rule, sysnet.RuleContext) sysnet.ValidationReport
// CompleteRuleHook can inspect the exact completion context and return a
// native error.
CompleteRuleHook func(sysnet.Rule, sysnet.RuleContext) ([]string, error)
// TunNameChecker is an optional hook used by option checks and SetTunName.
// When omitted, any non-empty free name is valid.
TunNameChecker func(name string) (valid bool)
// OutDNSProvider is an optional DNS provider returned by OutDNS. When
// omitted, OutDNS uses StaticDNS through an in-memory resolver.
OutDNSProvider dns.Interface
// StaticDNS maps host names to IP addresses for the default OutDNS resolver.
// It can be omitted when DNS behavior is not relevant to the test.
StaticDNS map[string]string
// OutNetwork is an optional network returned by OutNet. When omitted, System
// returns a loopback network that allows any host.
OutNetwork gonnect.Network
// LocalNetwork is an optional network returned by LocalNet. When omitted,
// System returns a loopback network that allows any host.
LocalNetwork gonnect.Network
// CheckDefaultTunOptsHook adds validation issues after built-in checks.
// Configure it before concurrent use.
CheckDefaultTunOptsHook func(sysnet.DefaultTunOpts) sysnet.ValidationReport
// DefaultTunBuilder is an optional hook used by BuildDefaultTun to provide a
// custom tun implementation. When omitted, System creates an in-memory pipe
// and exposes its peer through GetDefaultTunPeer.
DefaultTunBuilder func(opts sysnet.DefaultTunOpts) (tun.Tun, error)
// DefaultTunWarningsHook is an optional hook used by DefaultTunWarnings.
// When omitted, DefaultTunWarnings returns nil.
DefaultTunWarningsHook func(sysnet.DefaultTun) []sysnet.Warning
// CheckTunOptsHook adds validation issues after built-in checks. Configure it
// before concurrent use.
CheckTunOptsHook func(sysnet.TunOpts) sysnet.ValidationReport
// TunBuilder is an optional hook used by BuildTun to provide a custom tun
// implementation. When omitted, System creates an in-memory pipe and exposes
// its peer through GetTunPeer.
TunBuilder func(opts sysnet.TunOpts) (tun.Tun, error)
// TunWarningsHook is an optional hook used by TunWarnings. When omitted,
// TunWarnings returns nil.
TunWarningsHook func(tun.Tun) []sysnet.Warning
// SetDNSHook runs before a default TUN changes its DNS provider. A returned
// error leaves the current provider unchanged.
SetDNSHook func(dns.Interface) error
// contains filtered or unexported fields
}
System is an in-memory sysnet.System implementation intended for tests.
The zero value is ready to use. Its default catalog reports the implemented mock operations as available. It creates pipe-backed TUN devices, exposes loopback networks for OutNet and LocalNet, publishes DefaultRules, and matches no flows. Tests can set only the public fields that they need.
This makes System useful as a focused test double for code that requires a sysnet.System implementation. For example, pass &sysnetdebug.System{} to the code under test, set Disable* fields to exercise unsupported paths, install check or builder functions to inspect options, or use GetTunPeer and GetDefaultTunPeer to observe virtual TUN devices.
Set public configuration fields before concurrent use. Use SetCapabilityReport for race-free capability updates at run time.
func (*System) AddTunAddr ¶
AddTunAddr appends addr to the recorded addresses for a tun created by this System.
func (*System) AddTunRoute ¶
AddTunRoute appends route to the recorded routes for a tun created by this System.
func (*System) AllocIP ¶
func (s *System) AllocIP() subnet.IPAllocator
AllocIP returns the shared in-memory IP allocator for this System.
func (*System) AllocSubnet ¶
func (s *System) AllocSubnet() subnet.SubnetAllocator
AllocSubnet returns the shared in-memory subnet allocator for this System.
func (*System) BuildDefaultTun ¶
func (s *System) BuildDefaultTun( opts sysnet.DefaultTunOpts, ) (sysnet.DefaultTun, error)
BuildDefaultTun creates or reconfigures the default tun for this System.
With the default configuration it creates an in-memory pipe-backed tun and exposes the peer through GetDefaultTunPeer so tests can inspect packets. When DefaultTunBuilder is set, the custom builder supplies the tun and no peer is recorded.
func (*System) BuildMatcher ¶
BuildMatcher builds a matcher for rule. The matcher delegates to RuleMatcher, or matches nothing when RuleMatcher is omitted.
func (*System) BuildTun ¶
BuildTun creates a regular tun for this System.
With the default configuration it creates an in-memory pipe-backed tun and exposes the peer through GetTunPeer so tests can inspect packets. When TunBuilder is set, the custom builder supplies the tun and no peer is recorded.
func (*System) Capabilities ¶ added in v0.55.0
func (s *System) Capabilities() sysnet.CapabilityReport
Capabilities returns an independent capability snapshot.
func (*System) CapabilitiesForTun ¶ added in v0.55.0
CapabilitiesForTun returns capabilities for a live TUN created by s.
func (*System) CheckDefaultTunOpts ¶ added in v0.55.0
func (s *System) CheckDefaultTunOpts( opts sysnet.DefaultTunOpts, ) sysnet.ValidationReport
CheckDefaultTunOpts validates opts without creating or changing a TUN.
func (*System) CheckRule ¶ added in v0.55.0
func (s *System) CheckRule( rule sysnet.Rule, context sysnet.RuleContext, ) sysnet.ValidationReport
CheckRule validates a rule in one exact use context.
func (*System) CheckTunOpts ¶ added in v0.55.0
func (s *System) CheckTunOpts(opts sysnet.TunOpts) sysnet.ValidationReport
CheckTunOpts validates opts without creating or changing a TUN.
func (*System) Close ¶
Close closes every tun created by System and stops DNS request routing. It is safe to call more than once.
func (*System) CompleteRule ¶ added in v0.55.0
CompleteRule returns completion suggestions for one exact use context.
func (*System) DefaultTunWarnings ¶ added in v0.35.0
func (s *System) DefaultTunWarnings(t sysnet.DefaultTun) []sysnet.Warning
DefaultTunWarnings returns current warnings for an active default tun created by this System. By default it returns nil; tests can install DefaultTunWarningsHook to provide warnings.
func (*System) GetDefaultTunPeer ¶
GetDefaultTunPeer returns a snapshot of the current default tun entry.
This helper is not part of sysnet.System. Tests can use it to inspect the peer side and recorded configuration of the tun created by BuildDefaultTun.
func (*System) GetTunAddrs ¶
GetTunAddrs returns the recorded addresses for a tun created by this System.
func (*System) GetTunPeer ¶
GetTunPeer returns a snapshot of a regular tun entry by name.
This helper is not part of sysnet.System. Tests can use it to inspect the peer side and recorded configuration of tuns created by BuildTun.
func (*System) GetTunRoutes ¶ added in v0.55.0
GetTunRoutes returns the recorded routes for a TUN created by this System.
func (*System) LocalNet ¶
LocalNet returns the loopback network configured by LocalNetwork, or a permissive loopback network when LocalNetwork is omitted.
func (*System) OutDNS ¶
OutDNS returns the DNS provider used for traffic that should bypass a default tun.
func (*System) OutNet ¶
OutNet returns the outbound network configured by OutNetwork, or a permissive loopback network when OutNetwork is omitted.
func (*System) Requests ¶
Requests returns the DNS request channel for the System dns.Interface implementation.
Requests sent here are forwarded to the resolver most recently installed by the current default tun's SetDNS method. Requests are dropped while no default tun DNS resolver is configured.
func (*System) SetCapabilityReport ¶ added in v0.55.0
func (s *System) SetCapabilityReport(report sysnet.CapabilityReport) error
SetCapabilityReport atomically replaces the report used by validation and operations. The System assigns its own monotonic revision. Equal semantic contents keep the current revision.
func (*System) SetTunAddrs ¶
SetTunAddrs replaces the recorded addresses for a tun created by this System.
func (*System) SetTunCapabilityReport ¶ added in v0.55.0
SetTunCapabilityReport atomically narrows the operations for one live TUN. The SystemRevision and InstanceRevision fields in report are ignored.
func (*System) SetTunName ¶
SetTunName renames a regular or default TUN created by this System.
func (*System) SetTunRoutes ¶
SetTunRoutes replaces the recorded routes for a tun created by this System.
func (*System) TunWarnings ¶ added in v0.35.0
TunWarnings returns current warnings for an active regular tun created by this System. By default it returns nil; tests can install TunWarningsHook to provide warnings.
func (*System) UseDefaultCapabilityReport ¶ added in v0.55.0
func (s *System) UseDefaultCapabilityReport()
UseDefaultCapabilityReport returns the System to its documented default capability catalog. Disable* fields and Rules configure that catalog.
type TunConfig ¶
type TunConfig struct {
// Name is the requested device name. It is empty when the backend selected
// the name.
Name string
// MTU is the tun MTU after System defaulting has been applied.
MTU int
// TunAddrs are the addresses assigned to the tun.
TunAddrs []string
// TunRoutes are the routes assigned to the tun.
TunRoutes []string
// SourceRoutes select preferred local source addresses by destination.
SourceRoutes []sysnet.TunSourceRoute
// DnsIP is the DNS endpoint address requested for a default tun.
DnsIP string
// Strict records whether strict routing was requested for a default tun.
Strict bool
// Exclude records rules that should bypass a default tun.
Exclude []sysnet.Rule
// Include records rules that should use a default tun.
Include []sysnet.Rule
}
TunConfig is a snapshot of the tun options recorded by System.
type TunEntry ¶
type TunEntry struct {
// Name is the current System name for the tun.
Name string
// Tun is the handle returned by BuildTun or BuildDefaultTun.
Tun tun.Tun
// Peer is the other end of the in-memory pipe when System built the tun
// itself. It is nil when a custom builder supplied the tun.
Peer tun.Tun
// Default reports whether this entry was created by BuildDefaultTun.
Default bool
// Config is the last configuration recorded for this tun.
Config TunConfig
}
TunEntry describes a tun created by System.