export_test.go RawHistoryFolder
1package daemonmux
2
3import (
4 "google.golang.org/protobuf/proto"
5
6 "github.com/bnema/vev/internal/adapters/clock"
7 "github.com/bnema/vev/internal/ports"
8 "github.com/bnema/vev/internal/protocol"
9 "github.com/bnema/vev/internal/protocol/wire"
10)
11
12// Test-only convenience constructors and accessors (export_test.go pattern):
13// production always supplies its own explicit ceilings, clock, or budget
14// (see pump.go, supervisor.go), so these package-default shorthands only
15// exist for tests that don't care about a non-default value.
16
17// Accepts reports whether policy matches an admitted entry, for tests over the
18// legacy single-policy binding shape.
19func (b ServerBinding) Accepts(policy ports.BrokerPolicy) bool {
20 _, ok := b.Accepted(policy)
21 return ok
22}
23
24// Policy returns the sole entry's policy for legacy single-policy test
25// fixtures. It returns the zero value for a multi-entry authority.
26func (b ServerBinding) Policy() ports.BrokerPolicy {
27 if len(b.entries) == 1 {
28 return b.entries[0].Policy
29 }
30 return ports.BrokerPolicy{}
31}
32
33// Origin returns the sole entry's locality for legacy single-policy test
34// fixtures. It returns the unknown origin for a multi-entry authority.
35func (b ServerBinding) Origin() ports.SessionConnectionOrigin {
36 if len(b.entries) == 1 {
37 return b.entries[0].Origin
38 }
39 return ports.SessionOriginUnknown
40}
41
42// NewStreamEngine returns an open engine bound to one physical connection
43// under the package default MuxCeilings.
44func NewStreamEngine() *StreamEngine {
45 return newStreamEngine(DefaultMuxCeilings())
46}
47
48// NewListener returns a listener under the package default handshake budget,
49// accept-queue bound, and wall clock.
50func NewListener(pump *Pump, accepted ServerPolicyAdmission) (*Listener, error) {
51 return newListener(pump, protocol.HandshakeTimeout, MaxAcceptQueue, clock.New(), accepted)
52}
53
54// NewScheduler returns an empty outbound scheduler under the package default
55// MuxCeilings.
56func NewScheduler(engine *StreamEngine) *Scheduler {
57 return newScheduler(engine, DefaultMuxCeilings())
58}
59
60// Children reports the count of currently owned physical children.
61func (s *ServerSupervisor) Children() int {
62 if s == nil {
63 return 0
64 }
65 s.mu.Lock()
66 defer s.mu.Unlock()
67 return len(s.children)
68}
69
70// DecodePreambleRequestBytes strictly decodes one serialized daemonmux client
71// preamble under the 4 KiB bound, the strict scan, the generated unmarshal,
72// and DecodePreambleRequest's semantic checks. Production decodes a preamble
73// inline through the same steps (see handshake.go); this composed helper
74// exists for tests and the package fuzz target.
75func DecodePreambleRequestBytes(payload []byte) (MuxPreambleRequest, error) {
76 if err := CheckPreambleSize(payload); err != nil {
77 return MuxPreambleRequest{}, err
78 }
79 message := &wire.MuxPreambleRequest{}
80 if err := wire.ScanEnvelope(message, payload); err != nil {
81 return MuxPreambleRequest{}, err
82 }
83 if err := (proto.UnmarshalOptions{DiscardUnknown: false}.Unmarshal(payload, message)); err != nil {
84 return MuxPreambleRequest{}, err
85 }
86 return DecodePreambleRequest(message)
87}