Documentation
¶
Overview ¶
Package fake provides a scriptable in-memory implementation of firmware.Communication for testing BitBox02 client code without a device or simulator.
Index ¶
- Variables
- type Fake
- func (f *Fake) Add(h Handler) *Fake
- func (f *Fake) Always(r ResponderFunc) *Fake
- func (f *Fake) AlwaysError(err error) *Fake
- func (f *Fake) Calls() [][]byte
- func (f *Fake) Close()
- func (f *Fake) Closed() bool
- func (f *Fake) Expect(resp []byte) *Fake
- func (f *Fake) ExpectError(err error) *Fake
- func (f *Fake) ExpectMatch(m MatcherFunc, r ResponderFunc) *Fake
- func (f *Fake) OnClose(fn func()) *Fake
- func (f *Fake) PanicHandler(m MatcherFunc, v any) *Fake
- func (f *Fake) Query(req []byte) ([]byte, error)
- type Handler
- type MatcherFunc
- type ResponderFunc
Constants ¶
This section is empty.
Variables ¶
var ErrClosed = errors.New("bitbox-testkit/bitbox/fake: communication closed")
ErrClosed is returned from Query after Close has been called.
var ErrInjected = errors.New("bitbox-testkit/bitbox/fake: injected error")
ErrInjected is the canonical error used by InjectError-style scenarios.
var ErrUnexpectedQuery = errors.New("bitbox-testkit/bitbox/fake: unexpected query, no handler matched")
ErrUnexpectedQuery is returned when no handler matches an incoming query. Tests should treat this as a failure unless an UnhandledHandler is installed.
Functions ¶
This section is empty.
Types ¶
type Fake ¶
type Fake struct {
// contains filtered or unexported fields
}
Fake implements firmware.Communication using a chain of Handlers.
Handlers are tried in registration order; the first matching, non-exhausted handler responds. Calls are recorded for later assertions.
queryMu serializes the entire Query call so handler state mutations in Respond do not need their own locking. stateMu protects only the metadata fields (handlers, calls, closed, onClose), which lets a Respond implementation safely call back into Close or Add without deadlocking.
func (*Fake) Always ¶
func (f *Fake) Always(r ResponderFunc) *Fake
Always adds a handler that responds to every request indefinitely. Useful for unconditional sinks ("any further query returns generic OK").
func (*Fake) AlwaysError ¶
AlwaysError installs a fallthrough handler returning err for everything that no prior handler matched.
func (*Fake) Close ¶
func (f *Fake) Close()
Close marks the Fake closed. Subsequent Query calls return ErrClosed. Safe to call from inside a Handler's Respond.
func (*Fake) Expect ¶
Expect adds a handler that matches every request and replies with resp once. Use this to script a fixed sequence: Expect, Expect, Expect.
func (*Fake) ExpectError ¶
ExpectError adds a handler that matches once and returns err.
func (*Fake) ExpectMatch ¶
func (f *Fake) ExpectMatch(m MatcherFunc, r ResponderFunc) *Fake
ExpectMatch adds a single-use handler with explicit matcher + responder.
func (*Fake) OnClose ¶
OnClose registers a callback invoked exactly once when Close is first called. Useful for assertions or coordinating goroutines.
func (*Fake) PanicHandler ¶
func (f *Fake) PanicHandler(m MatcherFunc, v any) *Fake
PanicHandler installs a handler that calls panic(v) when matched. Lets tests verify recoverPanic shields gomobile exports from crashes.
type Handler ¶
type Handler interface {
Match(req []byte) bool
Respond(req []byte) ([]byte, error)
Exhausted() bool
}
Handler reacts to a single Query call. Match returns true to claim the query; Respond produces the answer (or error). A Handler may consume itself after one match by returning true from Exhausted.
type MatcherFunc ¶
MatcherFunc reports whether a request belongs to this handler.
func MatchEqual ¶
func MatchEqual(want []byte) MatcherFunc
MatchEqual matches requests with identical bytes.
func MatchPrefix ¶
func MatchPrefix(prefix []byte) MatcherFunc
MatchPrefix matches requests starting with prefix.
type ResponderFunc ¶
ResponderFunc produces a response (or error) for a matched request.